TemporalAAResolve.ankiprog 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 SHARPEN 0 1 2 // 0: disabled, 1: vertical, 2: horizontal
  6. #pragma anki mutator VARIANCE_CLIPPING 0 1
  7. #pragma anki mutator TONEMAP_FIX 0 1
  8. #pragma anki mutator YCBCR 0 1
  9. ANKI_SPECIALIZATION_CONSTANT_F32(VARIANCE_CLIPPING_GAMMA, 0u);
  10. ANKI_SPECIALIZATION_CONSTANT_F32(BLEND_FACTOR, 1u);
  11. ANKI_SPECIALIZATION_CONSTANT_UVEC2(FB_SIZE, 2u);
  12. #pragma anki start comp
  13. #include <AnKi/Shaders/Functions.glsl>
  14. #include <AnKi/Shaders/Pack.glsl>
  15. #include <AnKi/Shaders/Tonemapping.glsl>
  16. const UVec2 WORKGROUP_SIZE = UVec2(8, 8);
  17. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y, local_size_z = 1) in;
  18. layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
  19. layout(set = 0, binding = 1) uniform texture2D u_depthRt;
  20. layout(set = 0, binding = 2) uniform texture2D u_inputRt;
  21. layout(set = 0, binding = 3) uniform texture2D u_historyRt;
  22. layout(set = 0, binding = 4) uniform texture2D u_motionVectorsTex;
  23. layout(set = 0, binding = 5) writeonly uniform image2D u_outImg;
  24. #if TONEMAP_FIX
  25. # define TONEMAPPING_SET 0
  26. # define TONEMAPPING_BINDING 6
  27. # include <AnKi/Shaders/TonemappingResources.glsl>
  28. #endif
  29. #if YCBCR
  30. # define sample(s, uv) rgbToYCbCr(textureLod(s, u_linearAnyClampSampler, uv, 0.0).rgb)
  31. # define sampleOffset(s, uv, x, y) \
  32. rgbToYCbCr(textureLodOffset(sampler2D(s, u_linearAnyClampSampler), uv, 0.0, IVec2(x, y)).rgb)
  33. #else
  34. # define sample(s, uv) textureLod(s, u_linearAnyClampSampler, uv, 0.0).rgb
  35. # define sampleOffset(s, uv, x, y) textureLodOffset(sampler2D(s, u_linearAnyClampSampler), uv, 0.0, IVec2(x, y)).rgb
  36. #endif
  37. #define VELOCITY 0
  38. Vec3 sharpen(Vec2 uv)
  39. {
  40. const Vec3 center = sample(u_inputRt, uv);
  41. #if SHARPEN == 1
  42. Vec3 near = sampleOffset(u_inputRt, uv, 1, 0) + sampleOffset(u_inputRt, uv, -1, 0);
  43. #else
  44. Vec3 near = sampleOffset(u_inputRt, uv, 0, 1) + sampleOffset(u_inputRt, uv, 0, -1);
  45. #endif
  46. near *= 0.5;
  47. const F32 sharpness = 1.0;
  48. return center + max(Vec3(0.0), center - near) * sharpness;
  49. }
  50. void main()
  51. {
  52. if((FB_SIZE.x % WORKGROUP_SIZE.x) != 0u || (FB_SIZE.y % WORKGROUP_SIZE.y) != 0u) // This check is free
  53. {
  54. if(gl_GlobalInvocationID.x >= FB_SIZE.x || gl_GlobalInvocationID.y >= FB_SIZE.y)
  55. {
  56. return;
  57. }
  58. }
  59. const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
  60. const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
  61. // Get prev uv coords
  62. const Vec2 oldUv = uv + textureLod(u_motionVectorsTex, u_linearAnyClampSampler, uv, 0.0).rg;
  63. // Read textures
  64. Vec3 historyCol = sample(u_historyRt, oldUv);
  65. #if SHARPEN > 0
  66. const Vec3 crntCol = sharpen(uv);
  67. #else
  68. const Vec3 crntCol = sample(u_inputRt, uv);
  69. #endif
  70. // Remove ghosting by clamping the history color to neighbour's AABB
  71. const Vec3 near0 = sampleOffset(u_inputRt, uv, 1, 0);
  72. const Vec3 near1 = sampleOffset(u_inputRt, uv, 0, 1);
  73. const Vec3 near2 = sampleOffset(u_inputRt, uv, -1, 0);
  74. const Vec3 near3 = sampleOffset(u_inputRt, uv, 0, -1);
  75. #if VARIANCE_CLIPPING
  76. const Vec3 m1 = crntCol + near0 + near1 + near2 + near3;
  77. const Vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
  78. const Vec3 mu = m1 / 5.0;
  79. const Vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
  80. const Vec3 boxMin = mu - VARIANCE_CLIPPING_GAMMA * sigma;
  81. const Vec3 boxMax = mu + VARIANCE_CLIPPING_GAMMA * sigma;
  82. #else
  83. const Vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
  84. const Vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
  85. #endif
  86. historyCol = clamp(historyCol, boxMin, boxMax);
  87. // Remove jitter (T. Lottes)
  88. #if YCBCR
  89. const F32 lum0 = crntCol.r;
  90. const F32 lum1 = historyCol.r;
  91. const F32 maxLum = boxMax.r;
  92. #elif TONEMAP_FIX
  93. const F32 lum0 = computeLuminance(tonemap(crntCol, u_exposureThreshold0));
  94. const F32 lum1 = computeLuminance(tonemap(historyCol, u_exposureThreshold0));
  95. // F32 maxLum = computeLuminance(tonemap(boxMax, u_exposureThreshold0));
  96. const F32 maxLum = 1.0;
  97. #else
  98. const F32 lum0 = computeLuminance(crntCol);
  99. const F32 lum1 = computeLuminance(historyCol);
  100. const F32 maxLum = computeLuminance(boxMax);
  101. #endif
  102. F32 diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum + EPSILON));
  103. diff = 1.0 - diff;
  104. diff = diff * diff;
  105. const F32 feedback = mix(0.0, BLEND_FACTOR, diff);
  106. // Write result
  107. #if YCBCR
  108. const Vec3 outColor = yCbCrToRgb(mix(historyCol, crntCol, feedback));
  109. #else
  110. const Vec3 outColor = mix(historyCol, crntCol, feedback);
  111. #endif
  112. imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy), Vec4(outColor, 0.0));
  113. }
  114. #pragma anki end