DepthAwareBlur.ankiprog 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <!--
  2. Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  3. All rights reserved.
  4. Code licensed under the BSD License.
  5. http://www.anki3d.org/LICENSE
  6. -->
  7. <shaderProgram>
  8. <mutators>
  9. <mutator name="HORIZONTAL" values="0 1"/>
  10. <mutator name="KERNEL_SIZE" values="3 5 7 9 11 13 15"/>
  11. <mutator name="COLOR_COMPONENTS" values="4 3 1"/>
  12. </mutators>
  13. <shaders>
  14. <shader type="vert">
  15. <source><![CDATA[
  16. #include "shaders/QuadVert.glsl"
  17. ]]></source>
  18. </shader>
  19. <shader type="frag">
  20. <inputs>
  21. <input name="TEXTURE_SIZE" type="uvec2" const="1"/>
  22. </inputs>
  23. <source><![CDATA[
  24. #include "shaders/GaussianBlurCommon.glsl"
  25. #include "shaders/Tonemapping.glsl"
  26. #include "shaders/Functions.glsl"
  27. // Determine color type
  28. #if COLOR_COMPONENTS == 4
  29. # define COL_TYPE vec4
  30. # define TEX_FETCH rgba
  31. #elif COLOR_COMPONENTS == 3
  32. # define COL_TYPE vec3
  33. # define TEX_FETCH rgb
  34. #elif COLOR_COMPONENTS == 1
  35. # define COL_TYPE float
  36. # define TEX_FETCH r
  37. #else
  38. # error See file
  39. #endif
  40. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_tex;
  41. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_depthRt;
  42. layout(location = 0) in vec2 in_uv;
  43. layout(location = 0) out COL_TYPE out_color;
  44. float computeDepthWeight(float refDepth, float depth)
  45. {
  46. float diff = abs(refDepth - depth);
  47. float weight = 1.0 / (EPSILON + diff);
  48. return sqrt(weight);
  49. }
  50. float readDepth(vec2 uv)
  51. {
  52. return textureLod(u_depthRt, uv, 0.0).r;
  53. }
  54. void main()
  55. {
  56. #if HORIZONTAL
  57. const vec2 TEXEL_SIZE = vec2(1.0 / float(TEXTURE_SIZE.x), 0.0);
  58. #else
  59. const vec2 TEXEL_SIZE = vec2(0.0, 1.0 / float(TEXTURE_SIZE.y));
  60. #endif
  61. out_color = texture(u_tex, in_uv).TEX_FETCH;
  62. float refDepth = readDepth(in_uv);
  63. float weight = 1.0;
  64. vec2 texCoordOffset = 1.5 * TEXEL_SIZE;
  65. for(uint i = 0u; i < STEP_COUNT; ++i)
  66. {
  67. vec2 uv = in_uv + texCoordOffset;
  68. COL_TYPE col = texture(u_tex, uv).TEX_FETCH;
  69. float w = computeDepthWeight(refDepth, readDepth(uv));
  70. out_color += col * w;
  71. weight += w;
  72. uv = in_uv - texCoordOffset;
  73. col = texture(u_tex, uv).TEX_FETCH;
  74. w = computeDepthWeight(refDepth, readDepth(uv));
  75. out_color += col * w;
  76. weight += w;
  77. texCoordOffset += 2.0 * TEXEL_SIZE;
  78. }
  79. out_color = out_color / weight;
  80. }
  81. ]]></source>
  82. </shader>
  83. </shaders>
  84. </shaderProgram>