ShadowmapsResolve.ankiprog 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2009-2020, 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, 0, UVec2(1));
  7. ANKI_SPECIALIZATION_CONSTANT_U32(CLUSTER_COUNT_X, 2, 1u);
  8. ANKI_SPECIALIZATION_CONSTANT_U32(CLUSTER_COUNT_Y, 3, 1u);
  9. #define LIGHT_SET 0
  10. #define LIGHT_COMMON_UNIS_BINDING 3
  11. #define LIGHT_LIGHTS_BINDING 4
  12. #define LIGHT_CLUSTERS_BINDING 7
  13. #include <shaders/ClusteredShadingCommon.glsl>
  14. const UVec2 WORKGROUP_SIZE = UVec2(8, 8);
  15. layout(local_size_x = WORKGROUP_SIZE.x, local_size_y = WORKGROUP_SIZE.y, local_size_z = 1) in;
  16. layout(set = 0, binding = 0, rgba8) uniform image2D out_img;
  17. layout(set = 0, binding = 1) uniform sampler u_linearAnyClampSampler;
  18. layout(set = 0, binding = 2) uniform texture2D u_depthRt;
  19. void main()
  20. {
  21. SKIP_OUT_OF_BOUNDS_INVOCATIONS();
  22. // World position
  23. const Vec2 uv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(FB_SIZE);
  24. const Vec2 ndc = UV_TO_NDC(uv);
  25. const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
  26. const Vec4 worldPos4 = u_invViewProjMat * Vec4(ndc, depth, 1.0);
  27. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  28. // Cluster
  29. const U32 clusterIdx = computeClusterIndex(u_clustererMagic, uv, worldPos, CLUSTER_COUNT_X, CLUSTER_COUNT_Y);
  30. U32 idxOffset = u_clusters[clusterIdx];
  31. U32 shadowCasterCountPerFragment = 0;
  32. const U32 maxShadowCastersPerFragment = 4;
  33. F32 shadowFactors[maxShadowCastersPerFragment] = F32[](0.0, 0.0, 0.0, 0.0);
  34. // Dir light
  35. if(u_dirLight.m_active != 0u && u_dirLight.m_cascadeCount > 0)
  36. {
  37. const Vec4 viewPos4 = u_invProjMat * Vec4(ndc, depth, 1.0);
  38. const F32 positiveZViewSpace = -(viewPos4.z / viewPos4.w);
  39. F32 shadowFactor;
  40. if(positiveZViewSpace < u_dirLight.m_effectiveShadowDistance)
  41. {
  42. const U32 cascadeIdx =
  43. computeShadowCascadeIndex(positiveZViewSpace, u_dirLight.m_shadowCascadesDistancePower,
  44. u_dirLight.m_effectiveShadowDistance, u_dirLight.m_cascadeCount);
  45. shadowFactor =
  46. computeShadowFactorDirLight(u_dirLight, cascadeIdx, worldPos, u_shadowTex, u_linearAnyClampSampler);
  47. F32 distanceFadeFactor = saturate(positiveZViewSpace / u_dirLight.m_effectiveShadowDistance);
  48. distanceFadeFactor = pow(distanceFadeFactor, 8.0);
  49. shadowFactor += distanceFadeFactor;
  50. }
  51. else
  52. {
  53. shadowFactor = 1.0;
  54. }
  55. shadowFactors[0] = shadowFactor;
  56. ++shadowCasterCountPerFragment;
  57. }
  58. // Point lights
  59. U32 idx;
  60. ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
  61. {
  62. PointLight light = u_pointLights[idx];
  63. ANKI_BRANCH if(light.m_shadowAtlasTileScale >= 0.0)
  64. {
  65. const Vec3 frag2Light = light.m_position - worldPos;
  66. const F32 shadowFactor =
  67. computeShadowFactorPointLight(light, frag2Light, u_shadowTex, u_linearAnyClampSampler);
  68. shadowFactors[min(maxShadowCastersPerFragment - 1, shadowCasterCountPerFragment++)] = shadowFactor;
  69. }
  70. }
  71. // Spot lights
  72. ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
  73. {
  74. SpotLight light = u_spotLights[idx];
  75. ANKI_BRANCH if(light.m_shadowmapId >= 0.0)
  76. {
  77. const F32 shadowFactor =
  78. computeShadowFactorSpotLight(light, worldPos, u_shadowTex, u_linearAnyClampSampler);
  79. shadowFactors[min(maxShadowCastersPerFragment - 1, shadowCasterCountPerFragment++)] = shadowFactor;
  80. }
  81. }
  82. // Store
  83. imageStore(out_img, IVec2(gl_GlobalInvocationID.xy),
  84. Vec4(shadowFactors[0], shadowFactors[1], shadowFactors[2], shadowFactors[3]));
  85. }
  86. #pragma anki end