TemporalAAResolve.ankiprog 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2009-2021, 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 VARIANCE_CLIPPING 0 1
  6. #pragma anki mutator YCBCR 0 1
  7. ANKI_SPECIALIZATION_CONSTANT_F32(VARIANCE_CLIPPING_GAMMA, 0u);
  8. ANKI_SPECIALIZATION_CONSTANT_F32(BLEND_FACTOR, 1u);
  9. ANKI_SPECIALIZATION_CONSTANT_UVEC2(FB_SIZE, 2u);
  10. #pragma anki start comp
  11. #include <AnKi/Shaders/Functions.glsl>
  12. #include <AnKi/Shaders/PackFunctions.glsl>
  13. #include <AnKi/Shaders/TonemappingFunctions.glsl>
  14. const UVec2 WORKGROUP_SIZE = UVec2(8, 8);
  15. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y, local_size_z = 1) in;
  16. layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
  17. layout(set = 0, binding = 1) uniform texture2D u_depthRt;
  18. layout(set = 0, binding = 2) uniform texture2D u_inputRt;
  19. layout(set = 0, binding = 3) uniform texture2D u_historyRt;
  20. layout(set = 0, binding = 4) uniform texture2D u_motionVectorsTex;
  21. layout(set = 0, binding = 5) writeonly uniform image2D u_outImg;
  22. layout(set = 0, binding = 6) writeonly uniform image2D u_tonemappedImg;
  23. const U32 TONEMAPPING_SET = 0u;
  24. const U32 TONEMAPPING_BINDING = 7u;
  25. #include <AnKi/Shaders/TonemappingResources.glsl>
  26. #if YCBCR
  27. # define sample(s, uv) rgbToYCbCr(textureLod(s, u_linearAnyClampSampler, uv, 0.0).rgb)
  28. # define sampleOffset(s, uv, x, y) \
  29. rgbToYCbCr(textureLodOffset(sampler2D(s, u_linearAnyClampSampler), uv, 0.0, IVec2(x, y)).rgb)
  30. #else
  31. # define sample(s, uv) textureLod(s, u_linearAnyClampSampler, uv, 0.0).rgb
  32. # define sampleOffset(s, uv, x, y) textureLodOffset(sampler2D(s, u_linearAnyClampSampler), uv, 0.0, IVec2(x, y)).rgb
  33. #endif
  34. void main()
  35. {
  36. if(skipOutOfBoundsInvocations(WORKGROUP_SIZE, FB_SIZE))
  37. {
  38. return;
  39. }
  40. const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
  41. const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
  42. // Get prev uv coords
  43. const Vec2 oldUv = uv + textureLod(u_motionVectorsTex, u_linearAnyClampSampler, uv, 0.0).rg;
  44. // Read textures
  45. Vec3 historyCol = sample(u_historyRt, oldUv);
  46. const Vec3 crntCol = sample(u_inputRt, uv);
  47. // Remove ghosting by clamping the history color to neighbour's AABB
  48. const Vec3 near0 = sampleOffset(u_inputRt, uv, 1, 0);
  49. const Vec3 near1 = sampleOffset(u_inputRt, uv, 0, 1);
  50. const Vec3 near2 = sampleOffset(u_inputRt, uv, -1, 0);
  51. const Vec3 near3 = sampleOffset(u_inputRt, uv, 0, -1);
  52. #if VARIANCE_CLIPPING
  53. const Vec3 m1 = crntCol + near0 + near1 + near2 + near3;
  54. const Vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
  55. const Vec3 mu = m1 / 5.0;
  56. const Vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
  57. const Vec3 boxMin = mu - VARIANCE_CLIPPING_GAMMA * sigma;
  58. const Vec3 boxMax = mu + VARIANCE_CLIPPING_GAMMA * sigma;
  59. #else
  60. const Vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
  61. const Vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
  62. #endif
  63. historyCol = clamp(historyCol, boxMin, boxMax);
  64. // Remove jitter (T. Lottes)
  65. #if YCBCR
  66. const F32 lum0 = crntCol.r;
  67. const F32 lum1 = historyCol.r;
  68. const F32 maxLum = boxMax.r;
  69. #else
  70. const F32 lum0 = computeLuminance(invertibleTonemap(crntCol));
  71. const F32 lum1 = computeLuminance(invertibleTonemap(historyCol));
  72. const F32 maxLum = 1.0;
  73. #endif
  74. F32 diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum + EPSILON));
  75. diff = 1.0 - diff;
  76. diff = diff * diff;
  77. const F32 feedback = mix(0.0, BLEND_FACTOR, diff);
  78. // Write result
  79. Vec3 outColor = mix(historyCol, crntCol, feedback);
  80. #if YCBCR
  81. outColor = yCbCrToRgb(outColor);
  82. #endif
  83. imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy), Vec4(outColor, 0.0));
  84. imageStore(u_tonemappedImg, IVec2(gl_GlobalInvocationID.xy), Vec4(tonemap(outColor, u_exposureThreshold0), 0.0));
  85. }
  86. #pragma anki end