IndirectDiffuseDenoise.glsl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma anki mutator BLUR_ORIENTATION 0 1 // 0: in X axis, 1: in Y axis
  6. #include <AnKi/Shaders/Include/MiscRendererTypes.h>
  7. #include <AnKi/Shaders/PackFunctions.glsl>
  8. #include <AnKi/Shaders/Functions.glsl>
  9. #include <AnKi/Shaders/BilateralFilter.glsl>
  10. layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
  11. layout(set = 0, binding = 1) uniform ANKI_RP texture2D u_toDenoiseTex;
  12. layout(set = 0, binding = 2) uniform texture2D u_depthTex;
  13. #if defined(ANKI_COMPUTE_SHADER)
  14. const UVec2 WORKGROUP_SIZE = UVec2(8u, 8u);
  15. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y) in;
  16. layout(set = 0, binding = 3) writeonly uniform ANKI_RP image2D u_outImg;
  17. #else
  18. layout(location = 0) in Vec2 in_uv;
  19. layout(location = 0) out Vec3 out_color;
  20. #endif
  21. layout(push_constant, std430, row_major) uniform b_pc
  22. {
  23. IndirectDiffuseDenoiseUniforms u_unis;
  24. };
  25. Vec3 unproject(Vec2 ndc, F32 depth)
  26. {
  27. const Vec4 worldPos4 = u_unis.m_invertedViewProjectionJitterMat * Vec4(ndc, depth, 1.0);
  28. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  29. return worldPos;
  30. }
  31. void main()
  32. {
  33. #if defined(ANKI_COMPUTE_SHADER)
  34. if(skipOutOfBoundsInvocations(WORKGROUP_SIZE, u_unis.m_viewportSize))
  35. {
  36. return;
  37. }
  38. const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / u_unis.m_viewportSizef;
  39. #else
  40. const Vec2 uv = in_uv;
  41. #endif
  42. // Reference
  43. const F32 depthCenter = textureLod(u_depthTex, u_linearAnyClampSampler, uv, 0.0).r;
  44. if(depthCenter == 1.0)
  45. {
  46. #if defined(ANKI_COMPUTE_SHADER)
  47. imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy), Vec4(0.0));
  48. #else
  49. out_color = Vec3(0.0);
  50. #endif
  51. return;
  52. }
  53. const Vec3 positionCenter = unproject(UV_TO_NDC(uv), depthCenter);
  54. // Sample
  55. ANKI_RP F32 weight = EPSILON_RP;
  56. ANKI_RP Vec3 color = Vec3(0.0);
  57. for(F32 i = -u_unis.m_sampleCountDiv2; i <= u_unis.m_sampleCountDiv2; i += 1.0)
  58. {
  59. const Vec2 texelSize = 1.0 / u_unis.m_viewportSizef;
  60. #if BLUR_ORIENTATION == 0
  61. const Vec2 sampleUv = Vec2(uv.x + i * texelSize.x, uv.y);
  62. #else
  63. const Vec2 sampleUv = Vec2(uv.x, uv.y + i * texelSize.y);
  64. #endif
  65. const F32 depthTap = textureLod(u_depthTex, u_linearAnyClampSampler, sampleUv, 0.0).r;
  66. ANKI_RP F32 w = calculateBilateralWeightDepth(depthCenter, depthTap, 1.0);
  67. // w *= gaussianWeight(0.4, abs(F32(i)) / (u_unis.m_sampleCountDiv2 * 2.0 + 1.0));
  68. weight += w;
  69. color += textureLod(u_toDenoiseTex, u_linearAnyClampSampler, sampleUv, 0.0).xyz * w;
  70. }
  71. // Normalize and store
  72. color /= weight;
  73. #if defined(ANKI_COMPUTE_SHADER)
  74. imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy), Vec4(color, 0.0));
  75. // imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy), textureLod(u_toDenoiseTex, u_linearAnyClampSampler, uv,
  76. // 0.0));
  77. #else
  78. out_color = color;
  79. #endif
  80. }