ssao_blur.glslf 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var PRECISION highp
  6. #var SSAO_BLUR_DEPTH 0
  7. /*============================================================================*/
  8. precision PRECISION sampler2D;
  9. #include <precision_statement.glslf>
  10. #include <std.glsl>
  11. #if SSAO_BLUR_DEPTH
  12. #include <depth_fetch.glslf>
  13. #endif
  14. uniform sampler2D u_ssao_mask;
  15. uniform vec2 u_texel_size;
  16. /*==============================================================================
  17. SHADER INTERFACE
  18. ==============================================================================*/
  19. GLSL_IN vec2 v_texcoord;
  20. //------------------------------------------------------------------------------
  21. GLSL_OUT vec4 GLSL_OUT_FRAG_COLOR;
  22. /*============================================================================*/
  23. #if SSAO_BLUR_DEPTH
  24. uniform sampler2D u_depth;
  25. uniform vec2 u_camera_range;
  26. uniform float u_ssao_blur_discard_value;
  27. float read_depth(in vec2 coord) {
  28. return depth_fetch(u_depth, coord, u_camera_range);
  29. }
  30. #endif
  31. /*==============================================================================
  32. MAIN
  33. ==============================================================================*/
  34. void main() {
  35. float sum = 0.0;
  36. #if SSAO_BLUR_DEPTH
  37. float kdeph = read_depth(v_texcoord);
  38. float weight = 0.0;
  39. float amplif = u_ssao_blur_discard_value * 100.0;
  40. #endif
  41. vec2 hlim = vec2(-2.0); // -2.0 - solve problem with aliasing
  42. for (int i = 0; i < 4; ++i) {
  43. for (int j = 0; j < 4; ++j) {
  44. vec2 offset = (hlim + vec2(float(i), float(j))) * u_texel_size;
  45. float svalue = GLSL_TEXTURE(u_ssao_mask, v_texcoord + offset).a;
  46. #if SSAO_BLUR_DEPTH
  47. float sdeph = read_depth(v_texcoord + offset);
  48. float test = 1.0 - clamp(abs(sdeph - kdeph) * amplif, 0.0, 1.0);
  49. sum += svalue * test;
  50. weight += test;
  51. #else
  52. sum += svalue;
  53. #endif
  54. }
  55. }
  56. #if SSAO_BLUR_DEPTH
  57. GLSL_OUT_FRAG_COLOR = vec4(GLSL_TEXTURE(u_ssao_mask, v_texcoord).rgb, sum / weight);
  58. #else
  59. GLSL_OUT_FRAG_COLOR = vec4(GLSL_TEXTURE(u_ssao_mask, v_texcoord).rgb, sum / 16.0);
  60. #endif
  61. }