ClusteredShading.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. Mat4 m_textureMatrices[MAX_SHADOW_CASCADES];
  60. };
  61. const U32 SIZEOF_DIR_LIGHT = 2 * SIZEOF_VEC4 + MAX_SHADOW_CASCADES * SIZEOF_MAT4;
  62. ANKI_SHADER_STATIC_ASSERT(sizeof(DirectionalLight) == SIZEOF_DIR_LIGHT)
  63. // Representation of a reflection probe
  64. struct ReflectionProbe
  65. {
  66. Vec3 m_position; // Position of the probe in world space
  67. F32 m_cubemapIndex; // Slice in cubemap array texture
  68. Vec3 m_aabbMin;
  69. F32 m_padding0;
  70. Vec3 m_aabbMax;
  71. F32 m_padding1;
  72. };
  73. const U32 SIZEOF_REFLECTION_PROBE = 3 * SIZEOF_VEC4;
  74. ANKI_SHADER_STATIC_ASSERT(sizeof(ReflectionProbe) == SIZEOF_REFLECTION_PROBE)
  75. // Decal
  76. struct Decal
  77. {
  78. Vec4 m_diffUv;
  79. Vec4 m_normRoughnessUv;
  80. Mat4 m_texProjectionMat;
  81. Vec4 m_blendFactors;
  82. };
  83. const U32 SIZEOF_DECAL = 3 * SIZEOF_VEC4 + SIZEOF_MAT4;
  84. ANKI_SHADER_STATIC_ASSERT(sizeof(Decal) == SIZEOF_DECAL)
  85. // Fog density volume
  86. struct FogDensityVolume
  87. {
  88. Vec3 m_aabbMinOrSphereCenter;
  89. U32 m_isBox;
  90. Vec3 m_aabbMaxOrSphereRadiusSquared;
  91. F32 m_density;
  92. };
  93. const U32 SIZEOF_FOG_DENSITY_VOLUME = 2u * SIZEOF_VEC4;
  94. ANKI_SHADER_STATIC_ASSERT(sizeof(FogDensityVolume) == SIZEOF_FOG_DENSITY_VOLUME)
  95. // Global illumination probe
  96. struct GlobalIlluminationProbe
  97. {
  98. Vec3 m_aabbMin;
  99. U32 m_textureIndex;
  100. Vec3 m_aabbMax;
  101. F32 m_halfTexelSizeU; // (1.0 / textureSize(texArr[m_textureIndex]).x) / 2.0
  102. // Used to calculate a factor that is zero when fragPos is close to AABB bounds and 1.0 at m_fadeDistance and less.
  103. F32 m_fadeDistance;
  104. F32 m_padding0;
  105. F32 m_padding1;
  106. F32 m_padding2;
  107. };
  108. const U32 SIZEOF_GLOBAL_ILLUMINATION_PROBE = 3u * SIZEOF_VEC4;
  109. ANKI_SHADER_STATIC_ASSERT(sizeof(GlobalIlluminationProbe) == SIZEOF_GLOBAL_ILLUMINATION_PROBE)
  110. // Common uniforms for light shading passes
  111. struct LightingUniforms
  112. {
  113. Vec4 m_unprojectionParams;
  114. Vec2 m_rendererSize;
  115. F32 m_time;
  116. F32 m_near;
  117. Vec3 m_cameraPos;
  118. F32 m_far;
  119. ClustererMagicValues m_clustererMagicValues;
  120. ClustererMagicValues m_prevClustererMagicValues;
  121. UVec4 m_clusterCount;
  122. Vec3 m_padding;
  123. U32 m_lightVolumeLastCluster;
  124. Mat4 m_viewMat;
  125. Mat4 m_invViewMat;
  126. Mat4 m_projMat;
  127. Mat4 m_invProjMat;
  128. Mat4 m_viewProjMat;
  129. Mat4 m_invViewProjMat;
  130. Mat4 m_prevViewProjMat;
  131. Mat4 m_prevViewProjMatMulInvViewProjMat; // Used to re-project previous frames
  132. DirectionalLight m_dirLight;
  133. };
  134. const U32 SIZEOF_LIGHTING_UNIFORMS = 9u * SIZEOF_VEC4 + 8u * SIZEOF_MAT4 + SIZEOF_DIR_LIGHT;
  135. ANKI_SHADER_STATIC_ASSERT(sizeof(LightingUniforms) == SIZEOF_LIGHTING_UNIFORMS)
  136. ANKI_SHADER_FUNC_INLINE F32 computeClusterKf(ClustererMagicValues magic, Vec3 worldPos)
  137. {
  138. const F32 fz = sqrt(dot(magic.m_val0.xyz(), worldPos) - magic.m_val0.w());
  139. return fz;
  140. }
  141. ANKI_SHADER_FUNC_INLINE U32 computeClusterK(ClustererMagicValues magic, Vec3 worldPos)
  142. {
  143. return U32(computeClusterKf(magic, worldPos));
  144. }
  145. // Compute cluster index
  146. ANKI_SHADER_FUNC_INLINE U32 computeClusterIndex(ClustererMagicValues magic, Vec2 uv, Vec3 worldPos, U32 clusterCountX,
  147. U32 clusterCountY)
  148. {
  149. const UVec2 xy = UVec2(uv * Vec2(F32(clusterCountX), F32(clusterCountY)));
  150. const U32 k = computeClusterK(magic, worldPos);
  151. return k * (clusterCountX * clusterCountY) + xy.y() * clusterCountX + xy.x();
  152. }
  153. // Compute the Z of the near plane given a cluster idx
  154. ANKI_SHADER_FUNC_INLINE F32 computeClusterNearf(ClustererMagicValues magic, F32 fk)
  155. {
  156. return magic.m_val1.x() * fk * fk + magic.m_val1.y();
  157. }
  158. // Compute the Z of the near plane given a cluster idx
  159. ANKI_SHADER_FUNC_INLINE F32 computeClusterNear(ClustererMagicValues magic, U32 k)
  160. {
  161. return computeClusterNearf(magic, F32(k));
  162. }
  163. // Compute the UV coordinates of a volume texture that encloses the clusterer
  164. ANKI_SHADER_FUNC_INLINE Vec3 computeClustererVolumeTextureUvs(ClustererMagicValues magic, Vec2 uv, Vec3 worldPos,
  165. U32 clusterCountZ)
  166. {
  167. const F32 k = computeClusterKf(magic, worldPos);
  168. return Vec3(uv, k / F32(clusterCountZ));
  169. }
  170. ANKI_END_NAMESPACE