Taa.frag.glsl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "shaders/Functions.glsl"
  6. #include "shaders/Pack.glsl"
  7. #include "shaders/Tonemapping.glsl"
  8. #define YCBCR 1
  9. const float BLEND_FACTOR = 1.0 / 8.0;
  10. layout(location = 0) in vec2 in_uv;
  11. layout(location = 0) out vec3 out_color;
  12. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
  13. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_inputRt;
  14. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_historyRt;
  15. layout(ANKI_UBO_BINDING(0, 0), std140, row_major) uniform u0_
  16. {
  17. mat4 u_prevViewProjMatMulInvViewProjMat;
  18. };
  19. #if YCBCR
  20. #define sample(s, uv) rgbToYCbCr(textureLod(s, uv, 0.0).rgb)
  21. #define sampleOffset(s, uv, x, y) rgbToYCbCr(textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb)
  22. #else
  23. #define sample(s, uv) textureLod(s, uv, 0.0).rgb
  24. #define sampleOffset(s, uv, x, y) textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb
  25. #endif
  26. void main()
  27. {
  28. float depth = textureLod(u_depthRt, in_uv, 0.0).r;
  29. // Get prev uv coords
  30. vec4 v4 = u_prevViewProjMatMulInvViewProjMat * UV_TO_NDC(vec4(in_uv, depth, 1.0));
  31. vec2 oldUv = NDC_TO_UV(v4.xy / v4.w);
  32. // Read textures
  33. vec3 historyCol = sample(u_historyRt, oldUv);
  34. vec3 crntCol = sample(u_inputRt, in_uv);
  35. // Remove ghosting by clamping the history color to neighbour's AABB
  36. vec3 near0 = sampleOffset(u_inputRt, in_uv, 1, 0);
  37. vec3 near1 = sampleOffset(u_inputRt, in_uv, 0, 1);
  38. vec3 near2 = sampleOffset(u_inputRt, in_uv, -1, 0);
  39. vec3 near3 = sampleOffset(u_inputRt, in_uv, 0, -1);
  40. vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
  41. vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
  42. historyCol = clamp(historyCol, boxMin, boxMax);
  43. // Remove jitter (T. Lottes)
  44. #if YCBCR
  45. float lum0 = crntCol.r;
  46. float lum1 = historyCol.r;
  47. float maxLum = boxMax.r;
  48. #else
  49. float lum0 = computeLuminance(crntCol);
  50. float lum1 = computeLuminance(historyCol);
  51. float maxLum = computeLuminance(boxMax);
  52. #endif
  53. float diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum));
  54. diff = 1.0 - diff;
  55. diff = diff * diff;
  56. float feedback = mix(0.0, BLEND_FACTOR, diff);
  57. // Write result
  58. #if YCBCR
  59. out_color = yCbCrToRgb(mix(historyCol, crntCol, feedback));
  60. #else
  61. out_color = mix(historyCol, crntCol, feedback);
  62. #endif
  63. }