DepthAwareBlurGeneric.frag.glsl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "shaders/Functions.glsl"
  6. #include "shaders/GaussianBlurCommon.glsl"
  7. // Preprocessor switches sanity checks
  8. #if !defined(VPASS) && !defined(HPASS)
  9. #error See file
  10. #endif
  11. #if !(defined(COL_RGBA) || defined(COL_RGB) || defined(COL_R))
  12. #error See file
  13. #endif
  14. #if !defined(TEXTURE_SIZE)
  15. #error See file
  16. #endif
  17. // Determine color type
  18. #if defined(COL_RGBA)
  19. #define COL_TYPE vec4
  20. #elif defined(COL_RGB)
  21. #define COL_TYPE vec3
  22. #elif defined(COL_R)
  23. #define COL_TYPE float
  24. #endif
  25. // Determine tex fetch
  26. #if defined(COL_RGBA)
  27. #define TEX_FETCH rgba
  28. #elif defined(COL_RGB)
  29. #define TEX_FETCH rgb
  30. #elif defined(COL_R)
  31. #define TEX_FETCH r
  32. #endif
  33. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_colorTex;
  34. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_depthTex;
  35. layout(location = 0) in vec2 in_uv;
  36. layout(location = 0) out COL_TYPE out_color;
  37. layout(std140, ANKI_UBO_BINDING(0, 0)) uniform ubo0_
  38. {
  39. vec4 u_linearizeDepthCfDepthThresholdPad1;
  40. };
  41. #define u_linearizeDepthCf u_linearizeDepthCfDepthThresholdPad1.xy
  42. #define u_depthThreshold u_linearizeDepthCfDepthThresholdPad1.z
  43. float readLinearDepth(vec2 uv)
  44. {
  45. float d = texture(u_depthTex, uv).r;
  46. return linearizeDepthOptimal(d, u_linearizeDepthCf.x, u_linearizeDepthCf.y);
  47. }
  48. float computeDepthWeight(vec2 uv, float refDepth)
  49. {
  50. float d = readLinearDepth(uv);
  51. float diff = abs(refDepth - d);
  52. float depthWeight = 1.0 / (EPSILON + diff);
  53. return depthWeight;
  54. }
  55. void main()
  56. {
  57. #if defined(VPASS)
  58. const vec2 TEXEL_SIZE = vec2(0.0, 1.0 / TEXTURE_SIZE.y);
  59. #else
  60. const vec2 TEXEL_SIZE = vec2(1.0 / TEXTURE_SIZE.x, 0.0);
  61. #endif
  62. out_color = COL_TYPE(0.0);
  63. float refDepth = readLinearDepth(in_uv);
  64. float weight = 0.0;
  65. for(uint i = 0u; i < STEP_COUNT; ++i)
  66. {
  67. vec2 texCoordOffset = OFFSETS[i] * TEXEL_SIZE;
  68. vec2 uv = in_uv + texCoordOffset;
  69. float w = WEIGHTS[i] * computeDepthWeight(uv, refDepth);
  70. out_color += texture(u_colorTex, uv).TEX_FETCH * w;
  71. weight += w;
  72. uv = in_uv - texCoordOffset;
  73. w = WEIGHTS[i] * computeDepthWeight(uv, refDepth);
  74. out_color += texture(u_colorTex, uv).TEX_FETCH * w;
  75. weight += w;
  76. }
  77. out_color = out_color / weight;
  78. }