TemporalAAResolve.ankiprog 4.0 KB

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