ClusteredShading.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // Mainly contains light related structures. Everything is packed to align with std140
  6. #pragma once
  7. #include <shaders/glsl_cpp_common/Common.h>
  8. ANKI_BEGIN_NAMESPACE
  9. // Consts
  10. const U32 TYPED_OBJECT_COUNT = 6u; // Point lights, spot lights, refl probes, GI probes, decals and fog volumes
  11. const F32 INVALID_TEXTURE_INDEX = -1.0f;
  12. const F32 LIGHT_FRUSTUM_NEAR_PLANE = 0.1f / 4.0f; // The near plane on the shadow map frustums.
  13. const U32 MAX_SHADOW_CASCADES = 4u;
  14. const F32 SUBSURFACE_MIN = 0.05f;
  15. const U32 MAX_VISIBLE_GLOBAL_ILLUMINATION_PROBES = 8u; // Global illumination clipmap count.
  16. // See the documentation in the ClustererBin class.
  17. struct ClustererMagicValues
  18. {
  19. Vec4 m_val0;
  20. Vec4 m_val1;
  21. };
  22. // Point light
  23. struct PointLight
  24. {
  25. Vec3 m_position; // Position in world space
  26. F32 m_squareRadiusOverOne; // 1/(radius^2)
  27. Vec3 m_diffuseColor;
  28. F32 m_shadowAtlasTileScale; // UV scale for all tiles
  29. Vec3 m_padding;
  30. F32 m_radius; // Radius
  31. Vec4 m_shadowAtlasTileOffsets[3u]; // It's a Vec4 because of the std140 limitations
  32. };
  33. const U32 SIZEOF_POINT_LIGHT = 6 * SIZEOF_VEC4;
  34. ANKI_SHADER_STATIC_ASSERT(sizeof(PointLight) == SIZEOF_POINT_LIGHT)
  35. // Spot light
  36. struct SpotLight
  37. {
  38. Vec3 m_position; // Position in world space
  39. F32 m_squareRadiusOverOne; // 1/(radius^2)
  40. Vec3 m_diffuseColor;
  41. F32 m_shadowmapId; // Shadowmap tex ID
  42. Vec3 m_dir; // Light direction
  43. F32 m_radius; // Max distance
  44. F32 m_outerCos;
  45. F32 m_innerCos;
  46. F32 m_padding0;
  47. F32 m_padding1;
  48. Mat4 m_texProjectionMat;
  49. };
  50. const U32 SIZEOF_SPOT_LIGHT = 4 * SIZEOF_VEC4 + SIZEOF_MAT4;
  51. ANKI_SHADER_STATIC_ASSERT(sizeof(SpotLight) == SIZEOF_SPOT_LIGHT)
  52. // Directional light (sun)
  53. struct DirectionalLight
  54. {
  55. Vec3 m_diffuseColor;
  56. U32 m_cascadeCount; // If it's zero then it doesn't case shadow
  57. Vec3 m_dir;
  58. U32 m_active;
  59. Vec2 m_padding;
  60. F32 m_effectiveShadowDistance;
  61. F32 m_shadowCascadesDistancePower;
  62. Mat4 m_textureMatrices[MAX_SHADOW_CASCADES];
  63. };
  64. const U32 SIZEOF_DIR_LIGHT = 3 * SIZEOF_VEC4 + MAX_SHADOW_CASCADES * SIZEOF_MAT4;
  65. ANKI_SHADER_STATIC_ASSERT(sizeof(DirectionalLight) == SIZEOF_DIR_LIGHT)
  66. // Representation of a reflection probe
  67. struct ReflectionProbe
  68. {
  69. Vec3 m_position; // Position of the probe in world space
  70. F32 m_cubemapIndex; // Slice in cubemap array texture
  71. Vec3 m_aabbMin;
  72. F32 m_padding0;
  73. Vec3 m_aabbMax;
  74. F32 m_padding1;
  75. };
  76. const U32 SIZEOF_REFLECTION_PROBE = 3 * SIZEOF_VEC4;
  77. ANKI_SHADER_STATIC_ASSERT(sizeof(ReflectionProbe) == SIZEOF_REFLECTION_PROBE)
  78. // Decal
  79. struct Decal
  80. {
  81. Vec4 m_diffUv;
  82. Vec4 m_normRoughnessUv;
  83. Mat4 m_texProjectionMat;
  84. Vec4 m_blendFactors;
  85. };
  86. const U32 SIZEOF_DECAL = 3 * SIZEOF_VEC4 + SIZEOF_MAT4;
  87. ANKI_SHADER_STATIC_ASSERT(sizeof(Decal) == SIZEOF_DECAL)
  88. // Fog density volume
  89. struct FogDensityVolume
  90. {
  91. Vec3 m_aabbMinOrSphereCenter;
  92. U32 m_isBox;
  93. Vec3 m_aabbMaxOrSphereRadiusSquared;
  94. F32 m_density;
  95. };
  96. const U32 SIZEOF_FOG_DENSITY_VOLUME = 2u * SIZEOF_VEC4;
  97. ANKI_SHADER_STATIC_ASSERT(sizeof(FogDensityVolume) == SIZEOF_FOG_DENSITY_VOLUME)
  98. // Global illumination probe
  99. struct GlobalIlluminationProbe
  100. {
  101. Vec3 m_aabbMin;
  102. U32 m_textureIndex;
  103. Vec3 m_aabbMax;
  104. F32 m_halfTexelSizeU; // (1.0 / textureSize(texArr[m_textureIndex]).x) / 2.0
  105. // Used to calculate a factor that is zero when fragPos is close to AABB bounds and 1.0 at m_fadeDistance and less.
  106. F32 m_fadeDistance;
  107. F32 m_padding0;
  108. F32 m_padding1;
  109. F32 m_padding2;
  110. };
  111. const U32 SIZEOF_GLOBAL_ILLUMINATION_PROBE = 3u * SIZEOF_VEC4;
  112. ANKI_SHADER_STATIC_ASSERT(sizeof(GlobalIlluminationProbe) == SIZEOF_GLOBAL_ILLUMINATION_PROBE)
  113. // Common uniforms for light shading passes
  114. struct LightingUniforms
  115. {
  116. Vec4 m_unprojectionParams;
  117. Vec2 m_rendererSize;
  118. F32 m_time;
  119. F32 m_near;
  120. Vec3 m_cameraPos;
  121. F32 m_far;
  122. ClustererMagicValues m_clustererMagicValues;
  123. ClustererMagicValues m_prevClustererMagicValues;
  124. UVec4 m_clusterCount;
  125. Vec3 m_padding;
  126. U32 m_lightVolumeLastCluster;
  127. Mat4 m_viewMat;
  128. Mat4 m_invViewMat;
  129. Mat4 m_projMat;
  130. Mat4 m_invProjMat;
  131. Mat4 m_viewProjMat;
  132. Mat4 m_invViewProjMat;
  133. Mat4 m_prevViewProjMat;
  134. Mat4 m_prevViewProjMatMulInvViewProjMat; // Used to re-project previous frames
  135. DirectionalLight m_dirLight;
  136. };
  137. const U32 SIZEOF_LIGHTING_UNIFORMS = 9u * SIZEOF_VEC4 + 8u * SIZEOF_MAT4 + SIZEOF_DIR_LIGHT;
  138. ANKI_SHADER_STATIC_ASSERT(sizeof(LightingUniforms) == SIZEOF_LIGHTING_UNIFORMS)
  139. ANKI_SHADER_FUNC_INLINE F32 computeClusterKf(ClustererMagicValues magic, Vec3 worldPos)
  140. {
  141. const F32 fz = sqrt(dot(magic.m_val0.xyz(), worldPos) - magic.m_val0.w());
  142. return fz;
  143. }
  144. ANKI_SHADER_FUNC_INLINE U32 computeClusterK(ClustererMagicValues magic, Vec3 worldPos)
  145. {
  146. return U32(computeClusterKf(magic, worldPos));
  147. }
  148. // Compute cluster index
  149. ANKI_SHADER_FUNC_INLINE U32 computeClusterIndex(ClustererMagicValues magic, Vec2 uv, Vec3 worldPos, U32 clusterCountX,
  150. U32 clusterCountY)
  151. {
  152. const UVec2 xy = UVec2(uv * Vec2(F32(clusterCountX), F32(clusterCountY)));
  153. const U32 k = computeClusterK(magic, worldPos);
  154. return k * (clusterCountX * clusterCountY) + xy.y() * clusterCountX + xy.x();
  155. }
  156. // Compute the Z of the near plane given a cluster idx
  157. ANKI_SHADER_FUNC_INLINE F32 computeClusterNearf(ClustererMagicValues magic, F32 fk)
  158. {
  159. return magic.m_val1.x() * fk * fk + magic.m_val1.y();
  160. }
  161. // Compute the Z of the near plane given a cluster idx
  162. ANKI_SHADER_FUNC_INLINE F32 computeClusterNear(ClustererMagicValues magic, U32 k)
  163. {
  164. return computeClusterNearf(magic, F32(k));
  165. }
  166. // Compute the UV coordinates of a volume texture that encloses the clusterer
  167. ANKI_SHADER_FUNC_INLINE Vec3 computeClustererVolumeTextureUvs(ClustererMagicValues magic, Vec2 uv, Vec3 worldPos,
  168. U32 clusterCountZ)
  169. {
  170. const F32 k = computeClusterKf(magic, worldPos);
  171. return Vec3(uv, k / F32(clusterCountZ));
  172. }
  173. // Compute the far plane of a shadow cascade. "p" is the power that defines the distance curve.
  174. // "effectiveShadowDistance" is the far plane of the last cascade.
  175. ANKI_SHADER_FUNC_INLINE F32 computeShadowCascadeDistance(U32 cascadeIdx, F32 p, F32 effectiveShadowDistance,
  176. U32 shadowCascadeCount)
  177. {
  178. return pow((F32(cascadeIdx) + 1.0f) / F32(shadowCascadeCount), p) * effectiveShadowDistance;
  179. }
  180. // The reverse of computeShadowCascadeDistance().
  181. ANKI_SHADER_FUNC_INLINE U32 computeShadowCascadeIndex(F32 distance, F32 p, F32 effectiveShadowDistance,
  182. U32 shadowCascadeCount)
  183. {
  184. const F32 shadowCascadeCountf = F32(shadowCascadeCount);
  185. F32 idx = pow(distance / effectiveShadowDistance, 1.0f / p) * shadowCascadeCountf;
  186. idx = min(idx, shadowCascadeCountf - 1.0f);
  187. return U32(idx);
  188. }
  189. ANKI_END_NAMESPACE