roughness_limiter.glsl 1.7 KB

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