ssao_minify.glsl 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* clang-format off */
  2. [vertex]
  3. layout(location = 0) in highp vec4 vertex_attrib;
  4. /* clang-format on */
  5. void main() {
  6. gl_Position = vertex_attrib;
  7. }
  8. /* clang-format off */
  9. [fragment]
  10. #ifdef MINIFY_START
  11. #define SDEPTH_TYPE highp sampler2D
  12. uniform float camera_z_far;
  13. uniform float camera_z_near;
  14. /* clang-format on */
  15. #else
  16. #define SDEPTH_TYPE mediump usampler2D
  17. #endif
  18. uniform SDEPTH_TYPE source_depth; //texunit:0
  19. uniform ivec2 from_size;
  20. uniform int source_mipmap;
  21. layout(location = 0) out mediump uint depth;
  22. void main() {
  23. ivec2 ssP = ivec2(gl_FragCoord.xy);
  24. // Rotated grid subsampling to avoid XY directional bias or Z precision bias while downsampling.
  25. // On DX9, the bit-and can be implemented with floating-point modulo
  26. #ifdef MINIFY_START
  27. float fdepth = texelFetch(source_depth, clamp(ssP * 2 + ivec2(ssP.y & 1, ssP.x & 1), ivec2(0), from_size - ivec2(1)), source_mipmap).r;
  28. fdepth = fdepth * 2.0 - 1.0;
  29. #ifdef USE_ORTHOGONAL_PROJECTION
  30. fdepth = ((fdepth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
  31. #else
  32. fdepth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - fdepth * (camera_z_far - camera_z_near));
  33. #endif
  34. fdepth /= camera_z_far;
  35. depth = uint(clamp(fdepth * 65535.0, 0.0, 65535.0));
  36. #else
  37. depth = texelFetch(source_depth, clamp(ssP * 2 + ivec2(ssP.y & 1, ssP.x & 1), ivec2(0), from_size - ivec2(1)), source_mipmap).r;
  38. #endif
  39. }