ShadowmapsResolve.ankiprog 4.1 KB

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