MotionBlur.ankiprog 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Motion blur based on "A Reconstruction Filter for Plausible Motion Blur"
  6. #pragma anki mutator SAMPLE_COUNT 8 16 32
  7. #pragma anki mutator TILE_SIZE 8 16 32
  8. #pragma anki technique MaxTileVelocity comp mutators TILE_SIZE
  9. #pragma anki technique MaxNeighbourTileVelocity comp mutators TILE_SIZE
  10. #pragma anki technique Reconstruct vert pixel comp
  11. #include <AnKi/Shaders/QuadVert.hlsl>
  12. Vec2 computeMaxVelocity(Vec2 a, Vec2 b)
  13. {
  14. const F32 alen2 = dot(a, a);
  15. const F32 blen2 = dot(b, b);
  16. return (alen2 > blen2) ? a : b;
  17. }
  18. // ===========================================================================
  19. // MaxTileVelocity =
  20. // ===========================================================================
  21. #if ANKI_TECHNIQUE_MaxTileVelocity
  22. # define NUMTHREADS 64
  23. SamplerState g_linearClampSampler : register(s0);
  24. Texture2D<Vec4> g_motionVectorsTex : register(t0);
  25. RWTexture2D<Vec4> g_maxVelocityTex : register(u0);
  26. groupshared Vec2 g_maxVelocities[NUMTHREADS];
  27. struct Consts
  28. {
  29. Vec2 m_resolution;
  30. Vec2 m_padding;
  31. };
  32. ANKI_FAST_CONSTANTS(Consts, g_consts)
  33. [numthreads(8, 8, 1)] void main(UVec2 svDispatchThreadId : SV_DISPATCHTHREADID, U32 svGroupIndex : SV_GROUPINDEX, UVec2 svGroupId : SV_GROUPID)
  34. {
  35. // Gather the thread result
  36. const F32 pixelsPerThread = TILE_SIZE / 8;
  37. Vec2 maxV = 0.0;
  38. for(F32 x = 0.0; x < pixelsPerThread; x += 1.0)
  39. {
  40. for(F32 y = 0; y < pixelsPerThread; y += 1.0)
  41. {
  42. const Vec2 uv = (svDispatchThreadId * pixelsPerThread + Vec2(x, y) + 0.5) / g_consts.m_resolution;
  43. const Vec2 v = g_motionVectorsTex.SampleLevel(g_linearClampSampler, uv, 0.0);
  44. maxV = computeMaxVelocity(maxV, v);
  45. }
  46. }
  47. // Parallel reduction
  48. g_maxVelocities[svGroupIndex] = maxV;
  49. GroupMemoryBarrierWithGroupSync();
  50. [loop] for(U32 s = NUMTHREADS / 2u; s > 0u; s >>= 1u)
  51. {
  52. if(svGroupIndex < s)
  53. {
  54. g_maxVelocities[svGroupIndex] = computeMaxVelocity(g_maxVelocities[svGroupIndex], g_maxVelocities[svGroupIndex + s]);
  55. }
  56. GroupMemoryBarrierWithGroupSync();
  57. }
  58. // Write the result
  59. g_maxVelocityTex[svGroupId] = Vec4(g_maxVelocities[0], 0.0, 0.0);
  60. }
  61. #endif // ANKI_TECHNIQUE_MaxTileVelocity
  62. // ===========================================================================
  63. // MaxNeighbourTileVelocity =
  64. // ===========================================================================
  65. #if ANKI_TECHNIQUE_MaxNeighbourTileVelocity
  66. Texture2D<Vec4> g_maxVelocityTex : register(t0);
  67. RWTexture2D<Vec4> g_maxNeighbourVelTex : register(u0);
  68. void sample(IVec2 svDispatchThreadId, IVec2 offset, inout Vec2 maxVel)
  69. {
  70. const Vec2 v = g_maxVelocityTex[svDispatchThreadId + offset].xy;
  71. maxVel = computeMaxVelocity(maxVel, v);
  72. }
  73. [numthreads(8, 8, 1)] void main(UVec2 svDispatchThreadId : SV_DISPATCHTHREADID)
  74. {
  75. Vec2 maxv = 0.0;
  76. for(I32 i = -1; i <= 1; ++i)
  77. {
  78. for(I32 j = -1; j <= 1; ++j)
  79. {
  80. const Vec2 v = g_maxVelocityTex[svDispatchThreadId + IVec2(i, j)].xy;
  81. maxv = computeMaxVelocity(maxv, v);
  82. }
  83. }
  84. g_maxNeighbourVelTex[svDispatchThreadId] = Vec4(maxv, 0.0, 0.0);
  85. }
  86. #endif // MaxNeighbourTileVelocity
  87. // ===========================================================================
  88. // Reconstruct =
  89. // ===========================================================================
  90. #if ANKI_TECHNIQUE_Reconstruct && (ANKI_COMPUTE_SHADER || ANKI_PIXEL_SHADER)
  91. # include <AnKi/Shaders/ImportanceSampling.hlsl>
  92. # include <AnKi/Shaders/Functions.hlsl>
  93. Texture2D<Vec4> g_colorTex : register(t0);
  94. Texture2D<Vec4> g_depthTex : register(t1);
  95. Texture2D<Vec4> g_neighbourMaxTex : register(t2);
  96. Texture2D<Vec4> g_motionVectorsTex : register(t3);
  97. SamplerState g_linearAnyClampSampler : register(s0);
  98. # if ANKI_COMPUTE_SHADER
  99. RWTexture2D<Vec4> g_blurredTex : register(u0);
  100. # endif
  101. struct Constants
  102. {
  103. Vec2 m_depthLinearizationParams;
  104. U32 m_frame;
  105. F32 m_far;
  106. };
  107. ANKI_FAST_CONSTANTS(Constants, g_consts);
  108. F32 cone(Vec2 x, Vec2 y, Vec2 u)
  109. {
  110. return saturate(1.0 - length(x - y) / length(u));
  111. }
  112. F32 cylinder(Vec2 x, Vec2 y, Vec2 u)
  113. {
  114. return 1.0 - smoothstep(0.95 * length(u), 1.05 * length(u), length(x - y));
  115. }
  116. F32 softDepthCompare(F32 za, F32 zb)
  117. {
  118. return saturate(1.0 - (za - zb) / 0.01);
  119. }
  120. F32 readDepth(Vec2 uv)
  121. {
  122. const F32 d = g_depthTex.SampleLevel(g_linearAnyClampSampler, uv, 0.0).x;
  123. return -linearizeDepthOptimal(d, g_consts.m_depthLinearizationParams.x, g_consts.m_depthLinearizationParams.y) * g_consts.m_far;
  124. }
  125. # if ANKI_COMPUTE_SHADER
  126. [numthreads(8, 8, 1)] void main(UVec2 svDispatchThreadId : SV_DISPATCHTHREADID)
  127. {
  128. Vec2 colorTexSize;
  129. g_colorTex.GetDimensions(colorTexSize.x, colorTexSize.y);
  130. const Vec2 uv = (Vec2(svDispatchThreadId) + 0.5) / colorTexSize;
  131. const Vec2 x = Vec2(svDispatchThreadId) + 0.5;
  132. # else
  133. Vec3 main(Vec2 uv : TEXCOORDS, Vec4 svPosition : SV_POSITION) : SV_TARGET0
  134. {
  135. Vec2 colorTexSize;
  136. g_colorTex.GetDimensions(colorTexSize.x, colorTexSize.y);
  137. const Vec2 x = svPosition.xy;
  138. # endif
  139. Vec3 outColor;
  140. const Vec2 un = g_neighbourMaxTex.SampleLevel(g_linearAnyClampSampler, uv, 0.0).xy * colorTexSize;
  141. const Vec3 cx = g_colorTex.SampleLevel(g_linearAnyClampSampler, uv, 0.0).xyz;
  142. if(dot(un, un) < 0.5 * 0.5)
  143. {
  144. // Velocity less than 0.5 pixels. Skip blurring
  145. outColor = cx;
  146. }
  147. else
  148. {
  149. # if ANKI_COMPUTE_SHADER
  150. const Vec2 noise2 = spatioTemporalNoise(svDispatchThreadId, g_consts.m_frame);
  151. # else
  152. const Vec2 noise2 = spatioTemporalNoise(svPosition.xy, g_consts.m_frame);
  153. # endif
  154. const F32 j = noise2.x - 0.5;
  155. const F32 zx = readDepth(uv);
  156. const F32 vx = g_motionVectorsTex.SampleLevel(g_linearAnyClampSampler, uv, 0.0).xy * colorTexSize;
  157. F32 weight = 1.0 / max(length(vx), 0.5);
  158. outColor = cx * weight;
  159. for(F32 i = 0.0; i < SAMPLE_COUNT; i += 1.0)
  160. {
  161. if(i == (SAMPLE_COUNT - 1) / 2)
  162. {
  163. continue;
  164. }
  165. const F32 t = lerp(-1.0, 1.0, (i + j + 1.0) / (SAMPLE_COUNT + 1));
  166. const Vec2 y = x + un * t + 0.5;
  167. const F32 zy = readDepth(y / colorTexSize);
  168. const F32 f = softDepthCompare(zx, zy); // 1 means zy if forground
  169. const F32 b = softDepthCompare(zy, zx); // 1 means zy if background
  170. const Vec2 vy = g_motionVectorsTex.SampleLevel(g_linearAnyClampSampler, y / colorTexSize, 0.0).xy * colorTexSize;
  171. const F32 a = f * cone(y, x, vy) + b * cone(x, y, vx) + cylinder(y, x, vy) * cylinder(x, y, vx) * 2.0;
  172. weight += a;
  173. outColor += a * g_colorTex.SampleLevel(g_linearAnyClampSampler, y / colorTexSize, 0.0).xyz;
  174. }
  175. outColor /= weight;
  176. }
  177. # if ANKI_COMPUTE_SHADER
  178. g_blurredTex[svDispatchThreadId] = Vec4(outColor, 0.0);
  179. # else
  180. return outColor;
  181. # endif
  182. }
  183. #endif // ANKI_TECHNIQUE_Reconstruct && (ANKI_COMPUTE_SHADER || ANKI_PIXEL_SHADER)