ClusteredShadingTypes.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 once
  6. #include <AnKi/Shaders/Include/Common.h>
  7. ANKI_BEGIN_NAMESPACE
  8. // Enum of clusterer object types
  9. const U32 CLUSTER_OBJECT_TYPE_POINT_LIGHT = 0u;
  10. const U32 CLUSTER_OBJECT_TYPE_SPOT_LIGHT = 1u;
  11. const U32 CLUSTER_OBJECT_TYPE_DECAL = 2u;
  12. const U32 CLUSTER_OBJECT_TYPE_FOG_DENSITY_VOLUME = 3u;
  13. const U32 CLUSTER_OBJECT_TYPE_REFLECTION_PROBE = 4u;
  14. const U32 CLUSTER_OBJECT_TYPE_GLOBAL_ILLUMINATION_PROBE = 5u;
  15. const U32 CLUSTER_OBJECT_TYPE_COUNT = 6u; ///< Point and spot lights, refl and GI probes, decals and fog volumes.
  16. // Limits
  17. const U32 MAX_VISIBLE_POINT_LIGHTS = 64u;
  18. const U32 MAX_VISIBLE_SPOT_LIGHTS = 64u;
  19. const U32 MAX_VISIBLE_DECALS = 64u;
  20. const U32 MAX_VISIBLE_FOG_DENSITY_VOLUMES = 16u;
  21. const U32 MAX_VISIBLE_REFLECTION_PROBES = 16u;
  22. const U32 MAX_VISIBLE_GLOBAL_ILLUMINATION_PROBES = 8u;
  23. // Other consts
  24. const F32 CLUSTER_OBJECT_FRUSTUM_NEAR_PLANE = 0.1f / 4.0f; ///< The near plane of various clusterer object frustums.
  25. const U32 MAX_SHADOW_CASCADES2 = 4u;
  26. const F32 SUBSURFACE_MIN = 0.01f;
  27. /// Point light.
  28. struct PointLight
  29. {
  30. Vec3 m_position; ///< Position in world space.
  31. Vec3 m_diffuseColor;
  32. F32 m_radius; ///< Radius
  33. F32 m_squareRadiusOverOne; ///< 1/(radius^2).
  34. U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
  35. F32 m_shadowAtlasTileScale; ///< UV scale for all tiles.
  36. Vec2 m_shadowAtlasTileOffsets[6u];
  37. };
  38. const U32 _ANKI_SIZEOF_PointLight = 22u * ANKI_SIZEOF(U32);
  39. ANKI_SHADER_STATIC_ASSERT(sizeof(PointLight) == _ANKI_SIZEOF_PointLight);
  40. /// Spot light.
  41. struct SpotLight
  42. {
  43. Vec3 m_position; ///< Position in world space.
  44. Vec3 m_edgePoints[4u]; ///< Edge points in world space.
  45. Vec3 m_diffuseColor;
  46. F32 m_radius; ///< Max distance.
  47. F32 m_squareRadiusOverOne; ///< 1/(radius^2).
  48. U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
  49. Vec3 m_direction; ///< Light direction.
  50. F32 m_outerCos;
  51. F32 m_innerCos;
  52. Vec2 m_padding;
  53. Mat4 m_textureMatrix;
  54. };
  55. const U32 _ANKI_SIZEOF_SpotLight = 28u * ANKI_SIZEOF(U32) + ANKI_SIZEOF(Mat4);
  56. ANKI_SHADER_STATIC_ASSERT(sizeof(SpotLight) == _ANKI_SIZEOF_SpotLight);
  57. /// Spot light different view. This is the same structure as SpotLight but it's designed for binning.
  58. struct SpotLightBinning
  59. {
  60. Vec3 m_edgePoints[5u]; ///< Edge points in world space.
  61. Vec3 m_diffuseColor;
  62. F32 m_radius; ///< Max distance.
  63. F32 m_squareRadiusOverOne; ///< 1/(radius^2).
  64. U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
  65. Vec3 m_direction; ///< Light direction.
  66. F32 m_outerCos;
  67. F32 m_innerCos;
  68. Vec2 m_padding;
  69. Mat4 m_textureMatrix;
  70. };
  71. const U32 _ANKI_SIZEOF_SpotLightBinning = _ANKI_SIZEOF_SpotLight;
  72. ANKI_SHADER_STATIC_ASSERT(sizeof(SpotLightBinning) == _ANKI_SIZEOF_SpotLightBinning);
  73. ANKI_SHADER_STATIC_ASSERT(alignof(SpotLightBinning) == alignof(SpotLight));
  74. /// Directional light (sun).
  75. struct DirectionalLight
  76. {
  77. Vec3 m_diffuseColor;
  78. U32 m_cascadeCount; ///< If it's zero then it doesn't cast shadow.
  79. Vec3 m_direction;
  80. U32 m_active;
  81. F32 m_effectiveShadowDistance;
  82. F32 m_shadowCascadesDistancePower;
  83. U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
  84. U32 m_padding;
  85. Mat4 m_textureMatrices[MAX_SHADOW_CASCADES2];
  86. };
  87. const U32 _ANKI_SIZEOF_DirectionalLight = 12u * ANKI_SIZEOF(U32) + MAX_SHADOW_CASCADES2 * ANKI_SIZEOF(Mat4);
  88. ANKI_SHADER_STATIC_ASSERT(sizeof(DirectionalLight) == _ANKI_SIZEOF_DirectionalLight);
  89. /// Representation of a reflection probe.
  90. struct ReflectionProbe
  91. {
  92. Vec3 m_position; ///< Position of the probe in world space.
  93. F32 m_cubemapIndex; ///< Index in the cubemap array texture.
  94. Vec3 m_aabbMin;
  95. Vec3 m_aabbMax;
  96. };
  97. const U32 _ANKI_SIZEOF_ReflectionProbe = 10u * ANKI_SIZEOF(U32);
  98. ANKI_SHADER_STATIC_ASSERT(sizeof(ReflectionProbe) == _ANKI_SIZEOF_ReflectionProbe);
  99. /// Decal.
  100. struct Decal
  101. {
  102. Vec4 m_diffuseUv;
  103. Vec4 m_normRoughnessUv;
  104. Vec4 m_blendFactors;
  105. Mat4 m_textureMatrix;
  106. Mat4 m_invertedTransform;
  107. Vec3 m_obbExtend;
  108. F32 m_padding;
  109. };
  110. const U32 _ANKI_SIZEOF_Decal = 4u * ANKI_SIZEOF(Vec4) + 2u * ANKI_SIZEOF(Mat4);
  111. ANKI_SHADER_STATIC_ASSERT(sizeof(Decal) == _ANKI_SIZEOF_Decal);
  112. /// Fog density volume.
  113. struct FogDensityVolume
  114. {
  115. Vec3 m_aabbMinOrSphereCenter;
  116. U32 m_isBox;
  117. Vec3 m_aabbMaxOrSphereRadiusSquared;
  118. F32 m_density;
  119. };
  120. const U32 _ANKI_SIZEOF_FogDensityVolume = 2u * ANKI_SIZEOF(Vec4);
  121. ANKI_SHADER_STATIC_ASSERT(sizeof(FogDensityVolume) == _ANKI_SIZEOF_FogDensityVolume);
  122. /// Global illumination probe
  123. struct GlobalIlluminationProbe
  124. {
  125. Vec3 m_aabbMin;
  126. Vec3 m_aabbMax;
  127. U32 m_textureIndex; ///< Index to the array of volume textures.
  128. F32 m_halfTexelSizeU; ///< (1.0 / textureSize(texArr[textureIndex]).x) / 2.0
  129. /// Used to calculate a factor that is zero when fragPos is close to AABB bounds and 1.0 at fadeDistance and less.
  130. F32 m_fadeDistance;
  131. };
  132. const U32 _ANKI_SIZEOF_GlobalIlluminationProbe = 9u * ANKI_SIZEOF(U32);
  133. ANKI_SHADER_STATIC_ASSERT(sizeof(GlobalIlluminationProbe) == _ANKI_SIZEOF_GlobalIlluminationProbe);
  134. /// Common matrices.
  135. struct CommonMatrices
  136. {
  137. Mat4 m_cameraTransform ANKI_CPP_CODE(= Mat4::getIdentity());
  138. Mat4 m_view ANKI_CPP_CODE(= Mat4::getIdentity());
  139. Mat4 m_projection ANKI_CPP_CODE(= Mat4::getIdentity());
  140. Mat4 m_viewProjection ANKI_CPP_CODE(= Mat4::getIdentity());
  141. Mat4 m_jitter ANKI_CPP_CODE(= Mat4::getIdentity());
  142. Mat4 m_projectionJitter ANKI_CPP_CODE(= Mat4::getIdentity());
  143. Mat4 m_viewProjectionJitter ANKI_CPP_CODE(= Mat4::getIdentity());
  144. Mat4 m_invertedViewProjectionJitter ANKI_CPP_CODE(= Mat4::getIdentity()); ///< To unproject in world space.
  145. Mat4 m_invertedViewProjection ANKI_CPP_CODE(= Mat4::getIdentity());
  146. Mat4 m_invertedProjectionJitter ANKI_CPP_CODE(= Mat4::getIdentity()); ///< To unproject in view space.
  147. Mat4 m_invertedView ANKI_CPP_CODE(= Mat4::getIdentity());
  148. Vec4 m_unprojectionParameters ANKI_CPP_CODE(= Vec4(0.0f)); ///< To unproject to view space. Jitter not considered.
  149. };
  150. const U32 _ANKI_SIZEOF_CommonMatrices = 11u * ANKI_SIZEOF(Mat4) + ANKI_SIZEOF(Vec4);
  151. ANKI_SHADER_STATIC_ASSERT(sizeof(CommonMatrices) == _ANKI_SIZEOF_CommonMatrices);
  152. /// Common uniforms for light shading passes.
  153. struct ClusteredShadingUniforms
  154. {
  155. Vec2 m_renderingSize;
  156. F32 m_time;
  157. U32 m_frame;
  158. Vec4 m_nearPlaneWSpace;
  159. F32 m_near;
  160. F32 m_far;
  161. Vec3 m_cameraPosition;
  162. UVec2 m_tileCounts;
  163. U32 m_zSplitCount;
  164. F32 m_zSplitCountOverFrustumLength; ///< m_zSplitCount/(far-near)
  165. Vec2 m_zSplitMagic; ///< It's the "a" and "b" of computeZSplitClusterIndex(). See there for details.
  166. U32 m_tileSize;
  167. U32 m_lightVolumeLastZSplit;
  168. /// This are some additive counts used to map a flat index to the index of the specific object.
  169. U32 m_objectCountsUpTo[CLUSTER_OBJECT_TYPE_COUNT];
  170. U32 m_padding;
  171. CommonMatrices m_matrices;
  172. CommonMatrices m_previousMatrices;
  173. DirectionalLight m_directionalLight;
  174. };
  175. const U32 _ANKI_SIZEOF_ClusteredShadingUniforms =
  176. 28u * ANKI_SIZEOF(U32) + 2u * ANKI_SIZEOF(CommonMatrices) + ANKI_SIZEOF(DirectionalLight);
  177. ANKI_SHADER_STATIC_ASSERT(sizeof(ClusteredShadingUniforms) == _ANKI_SIZEOF_ClusteredShadingUniforms);
  178. /// Information that a tile or a Z-split will contain.
  179. struct Cluster
  180. {
  181. U64 m_pointLightsMask;
  182. U64 m_spotLightsMask;
  183. U64 m_decalsMask;
  184. U32 m_fogDensityVolumesMask;
  185. U32 m_reflectionProbesMask;
  186. U32 m_giProbesMask;
  187. U32 m_padding; ///< Add some padding to be 100% sure nothing will break.
  188. };
  189. const U32 _ANKI_SIZEOF_Cluster = 5u * ANKI_SIZEOF(U64);
  190. ANKI_SHADER_STATIC_ASSERT(sizeof(Cluster) == _ANKI_SIZEOF_Cluster);
  191. ANKI_END_NAMESPACE