LightShading.ankiprog 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (C) 2009-2022, 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. #pragma anki start vert
  10. #include <AnKi/Shaders/QuadVert.glsl>
  11. #pragma anki end
  12. #pragma anki start frag
  13. #include <AnKi/Shaders/PackFunctions.glsl>
  14. #include <AnKi/Shaders/Functions.glsl>
  15. #include <AnKi/Shaders/RtShadows.glsl>
  16. #define CLUSTERED_SHADING_SET 0u
  17. #define CLUSTERED_SHADING_UNIFORMS_BINDING 0u
  18. #define CLUSTERED_SHADING_LIGHTS_BINDING 1u
  19. #define CLUSTERED_SHADING_CLUSTERS_BINDING 4u
  20. #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  21. layout(set = 0, binding = 5) uniform sampler u_nearestAnyClampSampler;
  22. layout(set = 0, binding = 6) uniform sampler u_trilinearClampSampler;
  23. layout(set = 0, binding = 7) uniform ANKI_RP texture2D u_gbuffer0Tex;
  24. layout(set = 0, binding = 8) uniform ANKI_RP texture2D u_gbuffer1Tex;
  25. layout(set = 0, binding = 9) uniform ANKI_RP texture2D u_gbuffer2Tex;
  26. layout(set = 0, binding = 10) uniform texture2D u_msDepthRt;
  27. #if USE_SHADOW_LAYERS
  28. layout(set = 0, binding = 11) uniform utexture2D u_shadowLayersTex;
  29. #else
  30. layout(set = 0, binding = 12) uniform ANKI_RP texture2D u_resolvedSm;
  31. #endif
  32. layout(location = 0) in Vec2 in_uv;
  33. layout(location = 0) out ANKI_RP Vec3 out_color;
  34. // Common code for lighting
  35. #define LIGHTING_COMMON_BRDF() \
  36. const ANKI_RP Vec3 frag2Light = light.m_position - worldPos; \
  37. const ANKI_RP Vec3 l = normalize(frag2Light); \
  38. const ANKI_RP Vec3 specC = specularIsotropicLobe(gbuffer, viewDir, l); \
  39. const ANKI_RP Vec3 diffC = diffuseLobe(gbuffer.m_diffuse); \
  40. const ANKI_RP F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light); \
  41. ANKI_RP F32 lambert = max(0.0, dot(gbuffer.m_normal, l));
  42. void main()
  43. {
  44. const F32 depth = textureLod(u_msDepthRt, u_nearestAnyClampSampler, in_uv, 0.0).r;
  45. const Vec2 ndc = UV_TO_NDC(in_uv);
  46. if(depth == 1.0)
  47. {
  48. out_color = Vec3(0.0);
  49. return;
  50. }
  51. // Get world position
  52. const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
  53. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  54. // Get the cluster
  55. Cluster cluster = getClusterFragCoord(Vec3(gl_FragCoord.xy, depth), TILE_SIZE, TILE_COUNTS, Z_SPLIT_COUNT,
  56. u_clusteredShading.m_zSplitMagic.x, u_clusteredShading.m_zSplitMagic.y);
  57. // out_color = clusterHeatmap(cluster, 1u << CLUSTER_OBJECT_TYPE_POINT_LIGHT); return;
  58. // Decode GBuffer
  59. GbufferInfo gbuffer;
  60. unpackGBufferNoVelocity(textureLod(u_gbuffer0Tex, u_nearestAnyClampSampler, in_uv, 0.0),
  61. textureLod(u_gbuffer1Tex, u_nearestAnyClampSampler, in_uv, 0.0),
  62. textureLod(u_gbuffer2Tex, u_nearestAnyClampSampler, in_uv, 0.0), gbuffer);
  63. gbuffer.m_subsurface = max(gbuffer.m_subsurface, SUBSURFACE_MIN);
  64. // SM
  65. #if USE_SHADOW_LAYERS
  66. ANKI_RP F32 resolvedSm[MAX_RT_SHADOW_LAYERS];
  67. unpackRtShadows(textureLod(u_shadowLayersTex, u_nearestAnyClampSampler, in_uv, 0.0), resolvedSm);
  68. #else
  69. ANKI_RP Vec4 resolvedSm = textureLod(u_resolvedSm, u_trilinearClampSampler, in_uv, 0.0);
  70. U32 resolvedSmIdx = 0u;
  71. #endif
  72. // Ambient and emissive color
  73. out_color = gbuffer.m_emission;
  74. // Dir light
  75. const ANKI_RP Vec3 viewDir = normalize(u_clusteredShading.m_cameraPosition - worldPos);
  76. const DirectionalLight dirLight = u_clusteredShading.m_directionalLight;
  77. if(dirLight.m_active != 0u)
  78. {
  79. ANKI_RP F32 shadowFactor;
  80. if(dirLight.m_cascadeCount > 0u)
  81. {
  82. #if USE_SHADOW_LAYERS
  83. shadowFactor = resolvedSm[dirLight.m_shadowLayer];
  84. #else
  85. shadowFactor = resolvedSm[0];
  86. ++resolvedSmIdx;
  87. #endif
  88. }
  89. else
  90. {
  91. shadowFactor = 1.0;
  92. }
  93. const ANKI_RP Vec3 l = -dirLight.m_direction;
  94. const ANKI_RP F32 lambert = max(gbuffer.m_subsurface, dot(l, gbuffer.m_normal));
  95. const ANKI_RP Vec3 diffC = diffuseLobe(gbuffer.m_diffuse);
  96. const ANKI_RP Vec3 specC = specularIsotropicLobe(gbuffer, viewDir, l);
  97. out_color += (diffC + specC) * dirLight.m_diffuseColor * (shadowFactor * lambert);
  98. }
  99. // Point lights
  100. ANKI_LOOP while(cluster.m_pointLightsMask != ExtendedClusterObjectMask(0))
  101. {
  102. const I32 idx = findLSB2(cluster.m_pointLightsMask);
  103. cluster.m_pointLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  104. const PointLight light = u_pointLights2[idx];
  105. LIGHTING_COMMON_BRDF();
  106. ANKI_BRANCH if(light.m_shadowAtlasTileScale >= 0.0)
  107. {
  108. #if USE_SHADOW_LAYERS
  109. const ANKI_RP F32 shadow = resolvedSm[light.m_shadowLayer];
  110. #else
  111. const ANKI_RP F32 shadow = resolvedSm[resolvedSmIdx++];
  112. #endif
  113. lambert *= shadow;
  114. }
  115. out_color += (diffC + specC) * light.m_diffuseColor * (att * max(gbuffer.m_subsurface, lambert));
  116. }
  117. // Spot lights
  118. ANKI_LOOP while(cluster.m_spotLightsMask != ExtendedClusterObjectMask(0))
  119. {
  120. const I32 idx = findLSB2(cluster.m_spotLightsMask);
  121. cluster.m_spotLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  122. const SpotLight light = u_spotLights[idx];
  123. LIGHTING_COMMON_BRDF();
  124. const F32 spot = computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_direction);
  125. ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
  126. {
  127. #if USE_SHADOW_LAYERS
  128. const ANKI_RP F32 shadow = resolvedSm[light.m_shadowLayer];
  129. #else
  130. const ANKI_RP F32 shadow = resolvedSm[resolvedSmIdx++];
  131. #endif
  132. lambert *= shadow;
  133. }
  134. out_color += (diffC + specC) * light.m_diffuseColor * (att * spot * max(gbuffer.m_subsurface, lambert));
  135. }
  136. out_color = saturateRp(out_color);
  137. }
  138. #pragma anki end