LightShading.ankiprog 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 USE_SHADOW_LAYERS 0 1
  6. ANKI_SPECIALIZATION_CONSTANT_UVEC2(TILE_COUNTS, 0u);
  7. ANKI_SPECIALIZATION_CONSTANT_U32(Z_SPLIT_COUNT, 2u);
  8. ANKI_SPECIALIZATION_CONSTANT_U32(TILE_SIZE, 3u);
  9. ANKI_SPECIALIZATION_CONSTANT_U32(IR_MIPMAP_COUNT, 4u);
  10. #pragma anki start vert
  11. #include <AnKi/Shaders/QuadVert.glsl>
  12. #pragma anki end
  13. #pragma anki start frag
  14. #include <AnKi/Shaders/PackFunctions.glsl>
  15. #include <AnKi/Shaders/Functions.glsl>
  16. #include <AnKi/Shaders/RtShadows.glsl>
  17. #define CLUSTERED_SHADING_SET 0
  18. #define CLUSTERED_SHADING_UNIFORMS_BINDING 0
  19. #define CLUSTERED_SHADING_LIGHTS_BINDING 1
  20. #define CLUSTERED_SHADING_REFLECTIONS_BINDING 4
  21. #define CLUSTERED_SHADING_CLUSTERS_BINDING 7
  22. #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  23. layout(set = 0, binding = 8) uniform sampler u_nearestAnyClampSampler;
  24. layout(set = 0, binding = 9) uniform sampler u_trilinearClampSampler;
  25. layout(set = 0, binding = 10) uniform texture2D u_msRt0;
  26. layout(set = 0, binding = 11) uniform texture2D u_msRt1;
  27. layout(set = 0, binding = 12) uniform texture2D u_msRt2;
  28. layout(set = 0, binding = 13) uniform texture2D u_msDepthRt;
  29. layout(set = 0, binding = 14) uniform texture2D u_ssrRt;
  30. #if USE_SHADOW_LAYERS
  31. layout(set = 0, binding = 15) uniform utexture2D u_shadowLayersTex;
  32. #else
  33. layout(set = 0, binding = 16) uniform texture2D u_resolvedSm;
  34. #endif
  35. layout(location = 0) in Vec2 in_uv;
  36. layout(location = 0) out Vec3 out_color;
  37. // Common code for lighting
  38. #define LIGHTING_COMMON_BRDF() \
  39. const Vec3 frag2Light = light.m_position - worldPos; \
  40. const Vec3 l = normalize(frag2Light); \
  41. const Vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l); \
  42. const Vec3 diffC = diffuseLambert(gbuffer.m_diffuse); \
  43. const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light); \
  44. F32 lambert = max(0.0, dot(gbuffer.m_normal, l));
  45. void main()
  46. {
  47. const F32 depth = textureLod(u_msDepthRt, u_nearestAnyClampSampler, in_uv, 0.0).r;
  48. const Vec2 ndc = UV_TO_NDC(in_uv);
  49. if(depth == 1.0)
  50. {
  51. out_color = Vec3(0.0);
  52. return;
  53. }
  54. // Get world position
  55. const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
  56. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  57. // Get the cluster
  58. Cluster cluster = getClusterFragCoord(Vec3(gl_FragCoord.xy, depth), TILE_SIZE, TILE_COUNTS, Z_SPLIT_COUNT,
  59. u_clusteredShading.m_zSplitMagic.x, u_clusteredShading.m_zSplitMagic.y);
  60. // out_color = clusterHeatmap(cluster, 1u << CLUSTER_OBJECT_TYPE_POINT_LIGHT); return;
  61. // Decode GBuffer
  62. GbufferInfo gbuffer;
  63. readGBuffer(u_msRt0, u_msRt1, u_msRt2, u_nearestAnyClampSampler, in_uv, 0.0, gbuffer);
  64. gbuffer.m_subsurface = max(gbuffer.m_subsurface, SUBSURFACE_MIN);
  65. // SM
  66. #if USE_SHADOW_LAYERS
  67. F32 resolvedSm[MAX_RT_SHADOW_LAYERS];
  68. unpackRtShadows(textureLod(u_shadowLayersTex, u_nearestAnyClampSampler, in_uv, 0.0), resolvedSm);
  69. #else
  70. Vec4 resolvedSm = textureLod(u_resolvedSm, u_trilinearClampSampler, in_uv, 0.0);
  71. U32 resolvedSmIdx = 0u;
  72. #endif
  73. // Ambient and emissive color
  74. out_color = gbuffer.m_diffuse * gbuffer.m_emission;
  75. // Dir light
  76. const Vec3 viewDir = normalize(u_clusteredShading.m_cameraPosition - worldPos);
  77. const DirectionalLight dirLight = u_clusteredShading.m_directionalLight;
  78. if(dirLight.m_active != 0u)
  79. {
  80. F32 shadowFactor;
  81. if(dirLight.m_cascadeCount > 0u)
  82. {
  83. #if USE_SHADOW_LAYERS
  84. shadowFactor = resolvedSm[dirLight.m_shadowLayer];
  85. #else
  86. shadowFactor = resolvedSm[0];
  87. ++resolvedSmIdx;
  88. #endif
  89. }
  90. else
  91. {
  92. shadowFactor = 1.0;
  93. }
  94. const Vec3 l = -dirLight.m_direction;
  95. const F32 lambert = max(gbuffer.m_subsurface, dot(l, gbuffer.m_normal));
  96. const Vec3 diffC = diffuseLambert(gbuffer.m_diffuse);
  97. const Vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l);
  98. out_color += (diffC + specC) * dirLight.m_diffuseColor * (shadowFactor * lambert);
  99. }
  100. // Point lights
  101. ANKI_LOOP while(cluster.m_pointLightsMask != ExtendedClusterObjectMask(0))
  102. {
  103. const I32 idx = findLSB2(cluster.m_pointLightsMask);
  104. cluster.m_pointLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  105. const PointLight light = u_pointLights2[idx];
  106. LIGHTING_COMMON_BRDF();
  107. ANKI_BRANCH if(light.m_shadowAtlasTileScale >= 0.0)
  108. {
  109. #if USE_SHADOW_LAYERS
  110. const F32 shadow = resolvedSm[light.m_shadowLayer];
  111. #else
  112. const F32 shadow = resolvedSm[resolvedSmIdx++];
  113. #endif
  114. lambert *= shadow;
  115. }
  116. out_color += (diffC + specC) * light.m_diffuseColor * (att * max(gbuffer.m_subsurface, lambert));
  117. }
  118. // Spot lights
  119. ANKI_LOOP while(cluster.m_spotLightsMask != ExtendedClusterObjectMask(0))
  120. {
  121. const I32 idx = findLSB2(cluster.m_spotLightsMask);
  122. cluster.m_spotLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  123. const SpotLight light = u_spotLights2[idx];
  124. LIGHTING_COMMON_BRDF();
  125. const F32 spot = computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_direction);
  126. ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
  127. {
  128. #if USE_SHADOW_LAYERS
  129. const F32 shadow = resolvedSm[light.m_shadowLayer];
  130. #else
  131. const F32 shadow = resolvedSm[resolvedSmIdx++];
  132. #endif
  133. lambert *= shadow;
  134. }
  135. out_color += (diffC + specC) * light.m_diffuseColor * (att * spot * max(gbuffer.m_subsurface, lambert));
  136. }
  137. // Indirect specular
  138. {
  139. // Do the probe read
  140. Vec3 specIndirect = Vec3(0.0);
  141. const Vec3 reflDir = reflect(-viewDir, gbuffer.m_normal);
  142. const F32 reflLod = F32(IR_MIPMAP_COUNT - 1u) * gbuffer.m_roughness;
  143. if(bitCount(cluster.m_reflectionProbesMask) == 1)
  144. {
  145. // Only one probe, do a fast path without blend weight
  146. const ReflectionProbe probe = u_reflectionProbes[findLSB2(cluster.m_reflectionProbesMask)];
  147. // Sample
  148. const Vec3 cubeUv = intersectProbe(worldPos, reflDir, probe.m_aabbMin, probe.m_aabbMax, probe.m_position);
  149. const Vec4 cubeArrUv = Vec4(cubeUv, probe.m_cubemapIndex);
  150. specIndirect = textureLod(u_reflectionsTex, u_trilinearClampSampler, cubeArrUv, reflLod).rgb;
  151. }
  152. else
  153. {
  154. // Zero or more than one probes, do a slow path that blends them together
  155. F32 totalBlendWeight = EPSILON;
  156. // Loop probes
  157. ANKI_LOOP while(cluster.m_reflectionProbesMask != 0u)
  158. {
  159. const U32 idx = U32(findLSB2(cluster.m_reflectionProbesMask));
  160. cluster.m_reflectionProbesMask &= ~(1u << idx);
  161. const ReflectionProbe probe = u_reflectionProbes[idx];
  162. // Compute blend weight
  163. const F32 blendWeight = computeProbeBlendWeight(worldPos, probe.m_aabbMin, probe.m_aabbMax, 0.2);
  164. totalBlendWeight += blendWeight;
  165. // Sample reflections
  166. const Vec3 cubeUv =
  167. intersectProbe(worldPos, reflDir, probe.m_aabbMin, probe.m_aabbMax, probe.m_position);
  168. const Vec4 cubeArrUv = Vec4(cubeUv, probe.m_cubemapIndex);
  169. const Vec3 c = textureLod(u_reflectionsTex, u_trilinearClampSampler, cubeArrUv, reflLod).rgb;
  170. specIndirect += c * blendWeight;
  171. }
  172. // Normalize the colors
  173. specIndirect /= totalBlendWeight;
  174. }
  175. // Read the SSL result
  176. const Vec4 ssr = textureLod(u_ssrRt, u_trilinearClampSampler, in_uv, 0.0);
  177. // Combine the SSR and probe reflections and write the result
  178. const Vec3 finalSpecIndirect = specIndirect * ssr.a + ssr.rgb;
  179. // Compute env BRDF
  180. const F32 NoV = max(EPSILON, dot(gbuffer.m_normal, viewDir));
  181. const Vec3 env =
  182. envBRDF(gbuffer.m_specular, gbuffer.m_roughness, u_integrationLut, u_trilinearClampSampler, NoV);
  183. out_color += finalSpecIndirect * env;
  184. }
  185. }
  186. #pragma anki end