RtShadowsRayGen.ankiprog 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 RAYS_PER_PIXEL 1 2 4 8
  6. #pragma anki library RtShadows
  7. #pragma anki ray_type 0
  8. #pragma anki start rgen
  9. #include <AnKi/Shaders/ImportanceSampling.glsl>
  10. #include <AnKi/Shaders/Pack.glsl>
  11. #include <AnKi/Shaders/RtShadows.glsl>
  12. #define CLUSTERED_SHADING_SET 0
  13. #define CLUSTERED_SHADING_UNIFORMS_BINDING 0
  14. #define CLUSTERED_SHADING_LIGHTS_BINDING 1
  15. #define CLUSTERED_SHADING_CLUSTERS_BINDING 4
  16. #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  17. // Used by the hit shaders. When changing the binding you need to update other shaders
  18. layout(set = 0, binding = 5) uniform sampler u_trilinearRepeatSampler;
  19. layout(set = 0, binding = 6) uniform uimage2D u_shadowsImage;
  20. layout(set = 0, binding = 7) uniform utexture2D u_historyShadowsTex;
  21. layout(set = 0, binding = 8) uniform sampler u_linearAnyClampSampler;
  22. layout(set = 0, binding = 9) uniform sampler u_nearestAnyClampSampler;
  23. layout(set = 0, binding = 10) uniform texture2D u_depthRt;
  24. layout(set = 0, binding = 11) uniform texture2D u_motionVectorsRt;
  25. layout(set = 0, binding = 12) uniform texture2D u_motionVectorsRejectionRt;
  26. layout(set = 0, binding = 13) uniform texture2D u_normalRt;
  27. layout(set = 0, binding = 14) uniform accelerationStructureEXT u_tlas;
  28. layout(set = 0, binding = 15) uniform texture2D u_prevHistoryLengthTex;
  29. layout(set = 0, binding = 16) uniform image2D u_historyLengthImage;
  30. layout(set = 0, binding = 17) uniform texture2D u_prevMomentsTex;
  31. layout(set = 0, binding = 18) uniform image2D u_momentsImage;
  32. layout(set = 0, binding = 19) uniform texture2D u_blueNoiseTex;
  33. ANKI_BINDLESS_SET(1); // Used by the hit shaders
  34. layout(push_constant, std430) uniform b_pc
  35. {
  36. RtShadowsUniforms u_unis;
  37. };
  38. layout(location = 0) rayPayloadEXT F32 g_payload;
  39. F32 trace(const Vec3 rayOrigin, const Vec3 rayDir, F32 tMax)
  40. {
  41. const U32 flags = gl_RayFlagsOpaqueEXT | gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsSkipClosestHitShaderEXT;
  42. const U32 cullMask = 0xFFu;
  43. const U32 sbtRecordOffset = 0u;
  44. const U32 sbtRecordStride = 0u;
  45. const U32 missIndex = 0u;
  46. const F32 tMin = 0.1;
  47. const I32 payloadLocation = 0;
  48. g_payload = 0.0;
  49. traceRayEXT(u_tlas, flags, cullMask, sbtRecordOffset, sbtRecordStride, missIndex, rayOrigin, tMin, rayDir, tMax,
  50. payloadLocation);
  51. return g_payload;
  52. }
  53. void main()
  54. {
  55. // World position
  56. const Vec2 uv = (Vec2(gl_LaunchIDEXT.xy) + 0.5) / Vec2(gl_LaunchSizeEXT.xy);
  57. const Vec2 ndc = UV_TO_NDC(uv);
  58. const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
  59. const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
  60. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  61. if(depth == 1.0)
  62. {
  63. imageStore(u_shadowsImage, IVec2(gl_LaunchIDEXT.xy), UVec4(0));
  64. imageStore(u_momentsImage, IVec2(gl_LaunchIDEXT.xy), Vec4(0.0));
  65. // Set to max history length because this pixel won't need any processing from further compute stages
  66. imageStore(u_historyLengthImage, IVec2(gl_LaunchIDEXT.xy),
  67. Vec4(RT_SHADOWS_MAX_HISTORY_LENGTH / RT_SHADOWS_MAX_HISTORY_LENGTH, 0.0, 0.0, 0.0));
  68. return;
  69. }
  70. // World normal
  71. const Vec3 normal = readNormalFromGBuffer(u_normalRt, u_linearAnyClampSampler, uv);
  72. // Cluster
  73. Cluster cluster = getClusterFragCoord(Vec3(uv * u_clusteredShading.m_renderingSize, depth));
  74. F32 shadowFactors[MAX_RT_SHADOW_LAYERS];
  75. zeroRtShadowLayers(shadowFactors);
  76. // Get a random factor
  77. Vec3 random[RAYS_PER_PIXEL];
  78. for(I32 i = 0; i < RAYS_PER_PIXEL; ++i)
  79. {
  80. const U32 frameIdx = u_clusteredShading.m_frame * U32(RAYS_PER_PIXEL + i);
  81. #if 0
  82. const UVec3 irandom = rand3DPCG16(UVec3(gl_LaunchIDEXT.xy, frameIdx));
  83. random[i] = Vec3(irandom) / F32(0xFFFF) * 2.0 - 1.0; // In [-1.0, 1.0]
  84. #else
  85. random[i] =
  86. textureLod(u_blueNoiseTex, u_trilinearRepeatSampler, Vec2(gl_LaunchSizeEXT.xy) / Vec2(64.0) * uv, 0.0).rgb;
  87. random[i] = animateBlueNoise(random[i], frameIdx);
  88. random[i] = random[i] * 2.0 - 1.0; // In [-1.0, 1.0]
  89. #endif
  90. }
  91. // Dir light
  92. const DirectionalLight dirLight = u_clusteredShading.m_directionalLight;
  93. ANKI_BRANCH if(dirLight.m_active != 0u && dirLight.m_cascadeCount > 0u)
  94. {
  95. for(I32 i = 0; i < RAYS_PER_PIXEL; ++i)
  96. {
  97. const Vec3 dirLightPos = worldPos + -dirLight.m_direction * 10.0 + random[i];
  98. const Vec3 rayDir = normalize(dirLightPos - worldPos);
  99. const F32 lambertTerm = dot(rayDir, normal);
  100. ANKI_BRANCH if(lambertTerm > 0.0)
  101. {
  102. shadowFactors[dirLight.m_shadowLayer] += trace(worldPos, rayDir, 10000.0) / F32(RAYS_PER_PIXEL);
  103. }
  104. }
  105. }
  106. // Point lights
  107. ANKI_LOOP while(cluster.m_pointLightsMask != 0ul)
  108. {
  109. const I32 idx = findLSB64(cluster.m_pointLightsMask);
  110. cluster.m_pointLightsMask &= ~(1ul << U64(idx));
  111. const PointLight light = u_pointLights2[idx];
  112. ANKI_BRANCH if(light.m_shadowAtlasTileScale >= 0.0)
  113. {
  114. for(I32 i = 0; i < RAYS_PER_PIXEL; ++i)
  115. {
  116. const Vec3 lightPos = light.m_position + 0.05 * light.m_radius * random[i];
  117. const Vec3 toLight = lightPos - worldPos;
  118. const F32 distanceToLight = length(toLight);
  119. const Vec3 rayDir = toLight / distanceToLight; // normalize
  120. const Bool inside = distanceToLight < light.m_radius;
  121. const F32 lambertTerm = dot(rayDir, normal);
  122. ANKI_BRANCH if(inside && lambertTerm > 0.0)
  123. {
  124. shadowFactors[light.m_shadowLayer] +=
  125. trace(worldPos, rayDir, distanceToLight) / F32(RAYS_PER_PIXEL);
  126. }
  127. }
  128. }
  129. }
  130. // Spot lights
  131. ANKI_LOOP while(cluster.m_spotLightsMask != 0ul)
  132. {
  133. const I32 idx = findLSB64(cluster.m_spotLightsMask);
  134. cluster.m_spotLightsMask &= ~(1ul << U64(idx));
  135. const SpotLight light = u_spotLights2[idx];
  136. ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
  137. {
  138. for(I32 i = 0; i < RAYS_PER_PIXEL; ++i)
  139. {
  140. const Vec3 lightPos = light.m_position + 0.05 * light.m_radius * random[i];
  141. const Vec3 toLight = lightPos - worldPos;
  142. const F32 distanceToLight = length(toLight);
  143. const Vec3 rayDir = toLight / distanceToLight; // normalize
  144. const F32 lambertTerm = dot(rayDir, normal);
  145. ANKI_BRANCH if(lambertTerm > 0.0)
  146. {
  147. shadowFactors[light.m_shadowLayer] +=
  148. trace(worldPos, rayDir, distanceToLight) / F32(RAYS_PER_PIXEL);
  149. }
  150. }
  151. }
  152. }
  153. // Compute history length
  154. const Vec2 historyUv = uv + textureLod(u_motionVectorsRt, u_linearAnyClampSampler, uv, 0.0).xy;
  155. const F32 historyRejectionFactor = textureLod(u_motionVectorsRejectionRt, u_linearAnyClampSampler, uv, 0.0).x;
  156. F32 historyLength;
  157. if(historyRejectionFactor >= 0.5)
  158. {
  159. // Rejection factor too high, reset the temporal history for all layers
  160. historyLength = 1.0 / RT_SHADOWS_MAX_HISTORY_LENGTH;
  161. }
  162. else
  163. {
  164. // Sample seems stable, increment its temporal history
  165. historyLength = textureLod(u_prevHistoryLengthTex, u_linearAnyClampSampler, historyUv, 0.0).r;
  166. historyLength += 1.0 / RT_SHADOWS_MAX_HISTORY_LENGTH;
  167. }
  168. // Store history length
  169. imageStore(u_historyLengthImage, IVec2(gl_LaunchIDEXT.xy), Vec4(historyLength));
  170. // Compute blend fractor. Use nearest sampler because it's an integer texture
  171. const F32 lowestBlendFactor = 0.1;
  172. const F32 stableFrames = 4.0;
  173. const F32 lerp = min(1.0, (historyLength * RT_SHADOWS_MAX_HISTORY_LENGTH - 1.0) / stableFrames);
  174. const F32 blendFactor = mix(1.0, lowestBlendFactor, lerp);
  175. // Blend with history
  176. const UVec4 packedhistory = textureLod(u_historyShadowsTex, u_nearestAnyClampSampler, historyUv, 0.0);
  177. F32 history[MAX_RT_SHADOW_LAYERS];
  178. unpackRtShadows(packedhistory, history);
  179. for(U32 i = 0u; i < MAX_RT_SHADOW_LAYERS; ++i)
  180. {
  181. const F32 lerp = min(1.0, u_unis.historyRejectFactor[i] + blendFactor);
  182. shadowFactors[i] = mix(history[i], shadowFactors[i], lerp);
  183. }
  184. // Store the shadows image
  185. const UVec4 packed = packRtShadows(shadowFactors);
  186. imageStore(u_shadowsImage, IVec2(gl_LaunchIDEXT.xy), packed);
  187. // Compute the moments that will give temporal variance
  188. Vec2 moments = Vec2(0.0);
  189. ANKI_UNROLL for(U32 i = 0u; i < MAX_RT_SHADOW_LAYERS; ++i)
  190. {
  191. moments.x += shadowFactors[i];
  192. }
  193. moments.y = moments.x * moments.x;
  194. // Blend the moments
  195. const Vec2 prevMoments = textureLod(u_prevMomentsTex, u_linearAnyClampSampler, historyUv, 0.0).xy;
  196. F32 momentsBlendFactor = 0.2;
  197. momentsBlendFactor = mix(momentsBlendFactor, 1.0, historyRejectionFactor);
  198. moments = mix(prevMoments, moments, momentsBlendFactor);
  199. // Store the moments
  200. imageStore(u_momentsImage, IVec2(gl_LaunchIDEXT.xy), Vec4(moments, 0.0, 0.0));
  201. }
  202. #pragma anki end