roughness_limiter.glsl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  5. layout(set = 0, binding = 0) uniform sampler2D source_normal;
  6. layout(r8, set = 1, binding = 0) uniform restrict writeonly image2D dest_roughness;
  7. layout(push_constant, std430) uniform Params {
  8. ivec2 screen_size;
  9. float curve;
  10. uint pad;
  11. }
  12. params;
  13. #define HALF_PI 1.5707963267948966
  14. void main() {
  15. // Pixel being shaded
  16. ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
  17. if (any(greaterThan(pos, params.screen_size))) { //too large, do nothing
  18. return;
  19. }
  20. vec3 normal_accum = vec3(0.0);
  21. float accum = 0.0;
  22. for (int i = 0; i <= 1; i++) {
  23. for (int j = 0; j <= 1; j++) {
  24. normal_accum += normalize(texelFetch(source_normal, pos + ivec2(i, j), 0).xyz * 2.0 - 1.0);
  25. accum += 1.0;
  26. }
  27. }
  28. normal_accum /= accum;
  29. float r = length(normal_accum);
  30. float limit;
  31. if (r < 1.0) {
  32. float threshold = 0.4;
  33. /*
  34. //Formula from Filament, does not make sense to me.
  35. float r2 = r * r;
  36. float kappa = (3.0f * r - r * r2) / (1.0f - r2);
  37. float variance = 0.25f / kappa;
  38. limit = sqrt(min(2.0f * variance, threshold * threshold));
  39. */
  40. /*
  41. //Formula based on probability distribution graph
  42. float width = acos(max(0.0,r)); // convert to angle (width)
  43. float roughness = pow(width,1.7)*0.854492; //approximate (crappy) formula to convert to roughness
  44. limit = min(sqrt(roughness), threshold); //convert to perceptual roughness and apply threshold
  45. */
  46. limit = min(sqrt(pow(acos(max(0.0, r)) / HALF_PI, params.curve)), threshold); //convert to perceptual roughness and apply threshold
  47. //limit = 0.5;
  48. } else {
  49. limit = 0.0;
  50. }
  51. imageStore(dest_roughness, pos, vec4(limit));
  52. }