IndirectDiffuse.ankiprog 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // Does SSGI and GI probe sampling
  6. ANKI_SPECIALIZATION_CONSTANT_U32(SAMPLE_COUNT, 6u);
  7. #define ENABLE_SSGI true
  8. #define ENABLE_PROBES true
  9. #define REMOVE_FIREFLIES false
  10. #define REPROJECT_LIGHTBUFFER false
  11. #define SSGI_PROBE_COMBINE(ssgiColor, probeColor) ((ssgiColor) + (probeColor))
  12. #pragma anki start comp
  13. #include <AnKi/Shaders/Functions.glsl>
  14. #include <AnKi/Shaders/PackFunctions.glsl>
  15. #include <AnKi/Shaders/ImportanceSampling.glsl>
  16. #include <AnKi/Shaders/TonemappingFunctions.glsl>
  17. #include <AnKi/Shaders/Include/IndirectDiffuseTypes.h>
  18. const UVec2 WORKGROUP_SIZE = UVec2(8, 8);
  19. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y) in;
  20. #define CLUSTERED_SHADING_SET 0
  21. #define CLUSTERED_SHADING_UNIFORMS_BINDING 0
  22. #define CLUSTERED_SHADING_GI_BINDING 1
  23. #define CLUSTERED_SHADING_CLUSTERS_BINDING 3
  24. #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  25. layout(set = 0, binding = 4) writeonly uniform image2D u_outImage;
  26. layout(set = 0, binding = 5) uniform sampler u_linearAnyClampSampler;
  27. layout(set = 0, binding = 6) uniform texture2D u_gbufferRt2;
  28. layout(set = 0, binding = 7) uniform texture2D u_depthRt;
  29. layout(set = 0, binding = 8) uniform texture2D u_lightBufferRt;
  30. layout(set = 0, binding = 9) uniform texture2D u_historyTex;
  31. layout(set = 0, binding = 10) uniform texture2D u_motionVectorsTex;
  32. layout(set = 0, binding = 11) uniform texture2D u_motionVectorsRejectionTex;
  33. layout(push_constant, std430) uniform b_pc
  34. {
  35. IndirectDiffuseUniforms u_unis;
  36. };
  37. Vec4 cheapProject(Vec4 point)
  38. {
  39. return projectPerspective(point, u_unis.m_projectionMat.x, u_unis.m_projectionMat.y, u_unis.m_projectionMat.z,
  40. u_unis.m_projectionMat.w);
  41. }
  42. void main()
  43. {
  44. const UVec2 fixedGlobalInvocationId = min(gl_GlobalInvocationID.xy, u_unis.m_viewportSize);
  45. const Vec2 fragCoord = Vec2(fixedGlobalInvocationId.xy) + 0.5;
  46. const Vec2 uv = fragCoord / u_unis.m_viewportSizef;
  47. const Vec2 ndc = UV_TO_NDC(uv);
  48. // Get normal
  49. const Vec3 worldNormal = readNormalFromGBuffer(u_gbufferRt2, u_linearAnyClampSampler, uv);
  50. const Vec3 viewNormal = u_clusteredShading.m_matrices.m_viewRotation * worldNormal;
  51. // Get origin
  52. const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
  53. Vec4 v4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
  54. const Vec3 worldPos = v4.xyz / v4.w;
  55. v4 = u_clusteredShading.m_matrices.m_invertedProjectionJitter * Vec4(ndc, depth, 1.0);
  56. const Vec3 viewPos = v4.xyz / v4.w;
  57. // SSGI
  58. Vec3 outColor = Vec3(0.0);
  59. F32 ssao = 0.0;
  60. if(ENABLE_SSGI)
  61. {
  62. // Find the projected radius
  63. const Vec3 sphereLimit = viewPos + Vec3(u_unis.m_radius, 0.0, 0.0);
  64. const Vec4 projSphereLimit = cheapProject(Vec4(sphereLimit, 1.0));
  65. const Vec2 projSphereLimit2 = projSphereLimit.xy / projSphereLimit.w;
  66. const F32 projRadius = length(projSphereLimit2 - ndc);
  67. // Loop to compute
  68. const UVec2 random = rand3DPCG16(UVec3(gl_GlobalInvocationID.xy, u_clusteredShading.m_frame)).xy;
  69. for(U32 i = 0u; i < u_unis.m_sampleCount; ++i)
  70. {
  71. const Vec2 point = UV_TO_NDC(hammersleyRandom16(i, u_unis.m_sampleCount, random));
  72. const Vec2 finalDiskPoint = ndc + point * projRadius;
  73. // Do a cheap unproject in view space
  74. const F32 d = textureLod(u_depthRt, u_linearAnyClampSampler, NDC_TO_UV(finalDiskPoint), 0.0).r;
  75. const F32 z = u_clusteredShading.m_matrices.m_unprojectionParameters.z
  76. / (u_clusteredShading.m_matrices.m_unprojectionParameters.w + d);
  77. const Vec2 xy = finalDiskPoint * u_clusteredShading.m_matrices.m_unprojectionParameters.xy * z;
  78. const Vec3 s = Vec3(xy, z);
  79. // Compute factor
  80. const Vec3 dir = s - viewPos;
  81. const F32 len = length(dir);
  82. const Vec3 n = dir / len;
  83. const F32 NoL = max(0.0, dot(viewNormal, n));
  84. // const F32 distFactor = 1.0 - sin(min(1.0, len / u_unis.m_radius) * PI / 2.0);
  85. const F32 distFactor = 1.0 - min(1.0, len / u_unis.m_radius);
  86. // Compute the UV for sampling the pyramid
  87. const Vec2 crntFrameUv = NDC_TO_UV(finalDiskPoint);
  88. Vec2 lastFrameUv;
  89. if(REPROJECT_LIGHTBUFFER)
  90. {
  91. lastFrameUv =
  92. crntFrameUv + textureLod(u_motionVectorsTex, u_linearAnyClampSampler, crntFrameUv, 0.0).xy;
  93. }
  94. else
  95. {
  96. lastFrameUv = crntFrameUv;
  97. }
  98. // Append color
  99. const F32 w = distFactor * NoL;
  100. const Vec3 c = textureLod(u_lightBufferRt, u_linearAnyClampSampler, lastFrameUv, 100.0).xyz;
  101. outColor += c * w;
  102. // Compute SSAO as well
  103. ssao += max(dot(viewNormal, dir) + u_unis.m_ssaoBias, EPSILON) / max(len * len, EPSILON);
  104. }
  105. const F32 scount = 1.0 / u_unis.m_sampleCountf;
  106. outColor *= scount * 2.0 * PI;
  107. ssao *= scount;
  108. }
  109. ssao = min(1.0, 1.0 - ssao * u_unis.m_ssaoStrength);
  110. if(ENABLE_PROBES)
  111. {
  112. // Sample probes
  113. Vec3 probeColor = Vec3(0.0);
  114. // Get the cluster
  115. Cluster cluster = getClusterFragCoord(Vec3(fragCoord * 2.0, depth));
  116. // Get world position
  117. const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
  118. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  119. if(bitCount(cluster.m_giProbesMask) == 1)
  120. {
  121. // All subgroups point to the same probe and there is only one probe, do a fast path without blend weight
  122. const GlobalIlluminationProbe probe = u_giProbes[findLSB2(cluster.m_giProbesMask)];
  123. // Sample
  124. probeColor = sampleGlobalIllumination(worldPos, worldNormal, probe, u_globalIlluminationTextures,
  125. u_linearAnyClampSampler);
  126. }
  127. else
  128. {
  129. // Zero or more than one probes, do a slow path that blends them together
  130. F32 totalBlendWeight = EPSILON;
  131. // Loop probes
  132. ANKI_LOOP while(cluster.m_giProbesMask != 0u)
  133. {
  134. const U32 idx = U32(findLSB2(cluster.m_giProbesMask));
  135. cluster.m_giProbesMask &= ~(1u << idx);
  136. const GlobalIlluminationProbe probe = u_giProbes[idx];
  137. // Compute blend weight
  138. const F32 blendWeight =
  139. computeProbeBlendWeight(worldPos, probe.m_aabbMin, probe.m_aabbMax, probe.m_fadeDistance);
  140. totalBlendWeight += blendWeight;
  141. // Sample
  142. const Vec3 c = sampleGlobalIllumination(worldPos, worldNormal, probe, u_globalIlluminationTextures,
  143. u_linearAnyClampSampler);
  144. probeColor += c * blendWeight;
  145. }
  146. // Normalize
  147. probeColor /= totalBlendWeight;
  148. }
  149. outColor = SSGI_PROBE_COMBINE(outColor, probeColor);
  150. }
  151. // Remove fireflies
  152. if(REMOVE_FIREFLIES)
  153. {
  154. const F32 lum = computeLuminance(outColor) + 0.001;
  155. const F32 averageLum = (subgroupAdd(lum) / F32(gl_SubgroupSize)) * 2.0;
  156. const F32 newLum = min(lum, averageLum);
  157. outColor *= newLum / lum;
  158. }
  159. // Apply SSAO
  160. outColor *= ssao;
  161. // Blend color with history
  162. {
  163. const Vec2 historyUv = uv + textureLod(u_motionVectorsTex, u_linearAnyClampSampler, uv, 0.0).xy;
  164. const F32 historyRejectionFactor = textureLod(u_motionVectorsRejectionTex, u_linearAnyClampSampler, uv, 0.0).x;
  165. const F32 lowestBlendFactor = 0.1;
  166. const F32 blendFactor = mix(lowestBlendFactor, 1.0, historyRejectionFactor);
  167. // Blend with history
  168. const Vec3 history = textureLod(u_historyTex, u_linearAnyClampSampler, historyUv, 0.0).rgb;
  169. outColor = mix(history, outColor, blendFactor);
  170. }
  171. // Store color
  172. imageStore(u_outImage, IVec2(fixedGlobalInvocationId), Vec4(outColor, 1.0));
  173. }
  174. #pragma anki end