ShadowmapsResolve.glsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. ANKI_SPECIALIZATION_CONSTANT_UVEC2(FB_SIZE, 0u);
  6. ANKI_SPECIALIZATION_CONSTANT_UVEC2(TILE_COUNTS, 2u);
  7. ANKI_SPECIALIZATION_CONSTANT_U32(Z_SPLIT_COUNT, 4u);
  8. ANKI_SPECIALIZATION_CONSTANT_U32(TILE_SIZE, 5u);
  9. #define CLUSTERED_SHADING_SET 0u
  10. #define CLUSTERED_SHADING_UNIFORMS_BINDING 0u
  11. #define CLUSTERED_SHADING_LIGHTS_BINDING 1u
  12. #define CLUSTERED_SHADING_CLUSTERS_BINDING 4u
  13. #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  14. layout(set = 0, binding = 5) uniform sampler u_linearAnyClampSampler;
  15. layout(set = 0, binding = 6) uniform texture2D u_depthRt;
  16. #if defined(ANKI_COMPUTE_SHADER)
  17. const UVec2 WORKGROUP_SIZE = UVec2(8, 8);
  18. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y, local_size_z = 1) in;
  19. layout(set = 0, binding = 7, rgba8) writeonly uniform ANKI_RP image2D u_outImg;
  20. #else
  21. layout(location = 0) in Vec2 in_uv;
  22. layout(location = 0) out Vec4 out_color;
  23. #endif
  24. void main()
  25. {
  26. #if defined(ANKI_COMPUTE_SHADER)
  27. if(skipOutOfBoundsInvocations(WORKGROUP_SIZE, FB_SIZE))
  28. {
  29. return;
  30. }
  31. const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
  32. #else
  33. const Vec2 uv = in_uv;
  34. #endif
  35. // World position
  36. const Vec2 ndc = UV_TO_NDC(uv);
  37. const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
  38. const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
  39. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  40. // Cluster
  41. const Vec2 fragCoord = uv * u_clusteredShading.m_renderingSize;
  42. Cluster cluster = getClusterFragCoord(Vec3(fragCoord, depth), TILE_SIZE, TILE_COUNTS, Z_SPLIT_COUNT,
  43. u_clusteredShading.m_zSplitMagic.x, u_clusteredShading.m_zSplitMagic.y);
  44. // Layers
  45. U32 shadowCasterCountPerFragment = 0u;
  46. const U32 maxShadowCastersPerFragment = 4u;
  47. ANKI_RP F32 shadowFactors[maxShadowCastersPerFragment] = F32[](0.0, 0.0, 0.0, 0.0);
  48. // Dir light
  49. const DirectionalLight dirLight = u_clusteredShading.m_directionalLight;
  50. if(dirLight.m_active != 0u && dirLight.m_cascadeCount > 0u)
  51. {
  52. const ANKI_RP F32 positiveZViewSpace =
  53. testPlanePoint(u_clusteredShading.m_nearPlaneWSpace.xyz, u_clusteredShading.m_nearPlaneWSpace.w, worldPos)
  54. + u_clusteredShading.m_near;
  55. ANKI_RP F32 shadowFactor;
  56. if(positiveZViewSpace < dirLight.m_effectiveShadowDistance)
  57. {
  58. F32 cascadeBlendFactor;
  59. const UVec2 cascadeIndices = computeShadowCascadeIndex2(
  60. positiveZViewSpace, dirLight.m_shadowCascadesDistancePower, dirLight.m_effectiveShadowDistance,
  61. dirLight.m_cascadeCount, cascadeBlendFactor);
  62. const F32 shadowFactorCascadeA = computeShadowFactorDirLight(dirLight, cascadeIndices.x, worldPos,
  63. u_shadowAtlasTex, u_linearAnyClampSampler);
  64. if(cascadeBlendFactor < 0.01 || cascadeIndices.x == cascadeIndices.y)
  65. {
  66. // Don't blend cascades
  67. shadowFactor = shadowFactorCascadeA;
  68. }
  69. else
  70. {
  71. // Blend cascades
  72. const F32 shadowFactorCascadeB = computeShadowFactorDirLight(dirLight, cascadeIndices.y, worldPos,
  73. u_shadowAtlasTex, u_linearAnyClampSampler);
  74. shadowFactor = mix(shadowFactorCascadeA, shadowFactorCascadeB, cascadeBlendFactor);
  75. }
  76. ANKI_RP F32 distanceFadeFactor = saturate(positiveZViewSpace / dirLight.m_effectiveShadowDistance);
  77. distanceFadeFactor = pow(distanceFadeFactor, 8.0);
  78. shadowFactor += distanceFadeFactor;
  79. }
  80. else
  81. {
  82. shadowFactor = 1.0;
  83. }
  84. shadowFactors[0] = shadowFactor;
  85. ++shadowCasterCountPerFragment;
  86. }
  87. // Point lights
  88. ANKI_LOOP while(cluster.m_pointLightsMask != ExtendedClusterObjectMask(0))
  89. {
  90. const I32 idx = findLSB2(cluster.m_pointLightsMask);
  91. cluster.m_pointLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  92. const PointLight light = u_pointLights2[idx];
  93. ANKI_BRANCH if(light.m_shadowAtlasTileScale >= 0.0)
  94. {
  95. const Vec3 frag2Light = light.m_position - worldPos;
  96. const ANKI_RP F32 shadowFactor =
  97. computeShadowFactorPointLight(light, frag2Light, u_shadowAtlasTex, u_linearAnyClampSampler);
  98. shadowFactors[min(maxShadowCastersPerFragment - 1u, shadowCasterCountPerFragment++)] = shadowFactor;
  99. }
  100. }
  101. // Spot lights
  102. ANKI_LOOP while(cluster.m_spotLightsMask != ExtendedClusterObjectMask(0))
  103. {
  104. const I32 idx = findLSB2(cluster.m_spotLightsMask);
  105. cluster.m_spotLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  106. const SpotLight light = u_spotLights[idx];
  107. ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
  108. {
  109. const ANKI_RP F32 shadowFactor =
  110. computeShadowFactorSpotLight(light, worldPos, u_shadowAtlasTex, u_linearAnyClampSampler);
  111. shadowFactors[min(maxShadowCastersPerFragment - 1u, shadowCasterCountPerFragment++)] = shadowFactor;
  112. }
  113. }
  114. // Store
  115. #if defined(ANKI_COMPUTE_SHADER)
  116. imageStore(u_outImg, IVec2(gl_GlobalInvocationID.xy),
  117. Vec4(shadowFactors[0], shadowFactors[1], shadowFactors[2], shadowFactors[3]));
  118. #else
  119. out_color = Vec4(shadowFactors[0], shadowFactors[1], shadowFactors[2], shadowFactors[3]);
  120. #endif
  121. }