TemporalAAResolve.ankiprog 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!--
  2. Copyright (C) 2009-2017, 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. <shaders>
  9. <shader type="vert">
  10. <source><![CDATA[
  11. #include "shaders/QuadVert.glsl"
  12. ]]></source>
  13. </shader>
  14. <shader type="frag">
  15. <source><![CDATA[
  16. #include "shaders/Functions.glsl"
  17. #include "shaders/Pack.glsl"
  18. #include "shaders/Tonemapping.glsl"
  19. // Config
  20. #define VARIANCE_CLIPPING 1
  21. const float VARIANCE_CLIPPING_GAMMA = 1.75;
  22. #define YCBCR 0
  23. const float BLEND_FACTOR = 1.0 / 16.0; // Keep it low to have a better result
  24. #define TONEMAP_FIX 1
  25. layout(location = 0) in vec2 in_uv;
  26. layout(location = 0) out vec3 out_color;
  27. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
  28. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_inputRt;
  29. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_historyRt;
  30. layout(ANKI_UBO_BINDING(0, 0), std140, row_major) uniform u0_
  31. {
  32. mat4 u_prevViewProjMatMulInvViewProjMat;
  33. };
  34. #if TONEMAP_FIX
  35. #define TONEMAPPING_RESOURCE readonly buffer
  36. #include "shaders/TonemappingResources.glsl"
  37. #endif
  38. #if YCBCR
  39. #define sample(s, uv) rgbToYCbCr(textureLod(s, uv, 0.0).rgb)
  40. #define sampleOffset(s, uv, x, y) rgbToYCbCr(textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb)
  41. #else
  42. #define sample(s, uv) textureLod(s, uv, 0.0).rgb
  43. #define sampleOffset(s, uv, x, y) textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb
  44. #endif
  45. void main()
  46. {
  47. float depth = textureLod(u_depthRt, in_uv, 0.0).r;
  48. // Get prev uv coords
  49. vec4 v4 = u_prevViewProjMatMulInvViewProjMat * vec4(UV_TO_NDC(in_uv), depth, 1.0);
  50. vec2 oldUv = NDC_TO_UV(v4.xy / v4.w);
  51. // Read textures
  52. vec3 historyCol = sample(u_historyRt, oldUv);
  53. vec3 crntCol = sample(u_inputRt, in_uv);
  54. // Remove ghosting by clamping the history color to neighbour's AABB
  55. vec3 near0 = sampleOffset(u_inputRt, in_uv, 1, 0);
  56. vec3 near1 = sampleOffset(u_inputRt, in_uv, 0, 1);
  57. vec3 near2 = sampleOffset(u_inputRt, in_uv, -1, 0);
  58. vec3 near3 = sampleOffset(u_inputRt, in_uv, 0, -1);
  59. #if VARIANCE_CLIPPING
  60. vec3 m1 = crntCol + near0 + near1 + near2 + near3;
  61. vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
  62. vec3 mu = m1 / 5.0;
  63. vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
  64. vec3 boxMin = mu - VARIANCE_CLIPPING_GAMMA * sigma;
  65. vec3 boxMax = mu + VARIANCE_CLIPPING_GAMMA * sigma;
  66. #else
  67. vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
  68. vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
  69. #endif
  70. historyCol = clamp(historyCol, boxMin, boxMax);
  71. // Remove jitter (T. Lottes)
  72. #if YCBCR
  73. float lum0 = crntCol.r;
  74. float lum1 = historyCol.r;
  75. float maxLum = boxMax.r;
  76. #elif TONEMAP_FIX
  77. float lum0 = computeLuminance(tonemap(crntCol, u_exposureThreshold0));
  78. float lum1 = computeLuminance(tonemap(historyCol, u_exposureThreshold0));
  79. //float maxLum = computeLuminance(tonemap(boxMax, u_exposureThreshold0));
  80. float maxLum = 1.0;
  81. #else
  82. float lum0 = computeLuminance(crntCol);
  83. float lum1 = computeLuminance(historyCol);
  84. float maxLum = computeLuminance(boxMax);
  85. #endif
  86. float diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum + EPSILON));
  87. diff = 1.0 - diff;
  88. diff = diff * diff;
  89. float feedback = mix(0.0, BLEND_FACTOR, diff);
  90. // Write result
  91. #if YCBCR
  92. out_color = yCbCrToRgb(mix(historyCol, crntCol, feedback));
  93. #else
  94. out_color = mix(historyCol, crntCol, feedback);
  95. #endif
  96. }
  97. ]]></source>
  98. </shader>
  99. </shaders>
  100. </shaderProgram>