ClusteredShadingTypes.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright (C) 2009-2023, 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/GpuSceneTypes.h>
  7. #define ANKI_CLUSTERED_SHADING_USE_64BIT ANKI_SUPPORTS_64BIT_TYPES
  8. ANKI_BEGIN_NAMESPACE
  9. // Limits
  10. #if ANKI_CLUSTERED_SHADING_USE_64BIT
  11. constexpr U32 kMaxVisibleLights = 64u;
  12. constexpr U32 kMaxVisibleDecals = 64u;
  13. #else
  14. constexpr U32 kMaxVisibleLights = 32u;
  15. constexpr U32 kMaxVisibleDecals = 32u;
  16. #endif
  17. constexpr U32 kMaxVisibleFogDensityVolumes = 16u;
  18. constexpr U32 kMaxVisibleReflectionProbes = 16u;
  19. constexpr U32 kMaxVisibleGlobalIlluminationProbes = 8u;
  20. // Other consts
  21. constexpr RF32 kClusterObjectFrustumNearPlane = 0.1f / 4.0f; ///< Near plane of all clusterer object frustums.
  22. constexpr RF32 kSubsurfaceMin = 0.01f;
  23. constexpr U32 kMaxZsplitCount = 128u;
  24. /// A union of all fields of spot and point lights. Since we don't have unions in HLSL we had to get creative.
  25. struct LightUnion
  26. {
  27. Vec3 m_position; ///< Position in world space.
  28. RF32 m_radius; ///< Radius
  29. RVec3 m_diffuseColor;
  30. U32 m_lightType; ///< 0 is point and 1 is spot
  31. U32 m_shadowLayer; ///< Shadow layer used in RT shadows
  32. U32 m_shadow;
  33. F32 m_innerCos; ///< SPOT LIGHTS
  34. F32 m_outerCos; ///< SPOT LIGHTS
  35. RVec3 m_direction; ///< SPOT LIGHTS: Light direction.
  36. F32 m_shadowAtlasTileScale; ///< POINT LIGHTS: UV scale for all tiles.
  37. /// When it's a point light this is an array of 6 Vec2s (but because of padding it's actually Vec4s). When it's a spot light the first 4 Vec4s are
  38. /// the rows of the texture matrix
  39. Vec4 m_spotLightMatrixOrPointLightUvViewports[6u];
  40. };
  41. /// Point light.
  42. /// WARNING: Don't change the layout without looking at LightUnion.
  43. struct PointLight
  44. {
  45. Vec3 m_position; ///< Position in world space.
  46. RF32 m_radius; ///< Radius
  47. RVec3 m_diffuseColor;
  48. U32 m_padding1;
  49. U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
  50. F32 m_shadow;
  51. F32 m_padding2;
  52. U32 m_padding3;
  53. RVec3 m_padding4;
  54. F32 m_shadowAtlasTileScale; ///< UV scale for all tiles.
  55. Vec4 m_shadowAtlasTileOffsets[6u]; ///< It's a array of Vec2 but because of padding round it up.
  56. };
  57. static_assert(sizeof(PointLight) == sizeof(LightUnion));
  58. /// Spot light.
  59. /// WARNING: Don't change the layout without looking at LightUnion.
  60. struct SpotLight
  61. {
  62. Vec3 m_position; ///< Position in world space.
  63. RF32 m_radius; ///< Radius
  64. RVec3 m_diffuseColor;
  65. U32 m_padding1;
  66. U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
  67. U32 m_shadow;
  68. F32 m_innerCos;
  69. F32 m_outerCos;
  70. RVec3 m_direction;
  71. F32 m_padding2;
  72. Mat4 m_textureMatrix;
  73. Vec4 m_padding3[2];
  74. };
  75. static_assert(sizeof(SpotLight) == sizeof(LightUnion));
  76. /// Directional light (sun).
  77. struct DirectionalLight
  78. {
  79. RVec3 m_diffuseColor;
  80. U32 m_shadowCascadeCount; ///< If it's zero then it doesn't cast shadow.
  81. RVec3 m_direction;
  82. U32 m_active;
  83. Vec4 m_shadowCascadeDistances;
  84. Vec3 m_padding0;
  85. U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
  86. Mat4 m_textureMatrices[kMaxShadowCascades];
  87. };
  88. constexpr U32 kSizeof_DirectionalLight = 4u * sizeof(Vec4) + kMaxShadowCascades * sizeof(Mat4);
  89. static_assert(sizeof(DirectionalLight) == kSizeof_DirectionalLight);
  90. static_assert(kMaxShadowCascades == 4u); // Because m_shadowCascadeDistances is a Vec4
  91. /// Representation of a reflection probe.
  92. typedef GpuSceneReflectionProbe ReflectionProbe;
  93. /// Decal.
  94. typedef GpuSceneDecal Decal;
  95. /// Fog density volume.
  96. typedef GpuSceneFogDensityVolume FogDensityVolume;
  97. /// Global illumination probe
  98. typedef GpuSceneGlobalIlluminationProbe GlobalIlluminationProbe;
  99. /// Common matrices.
  100. struct CommonMatrices
  101. {
  102. Mat3x4 m_cameraTransform;
  103. Mat3x4 m_view;
  104. Mat4 m_projection;
  105. Mat4 m_viewProjection;
  106. Mat4 m_jitter;
  107. Mat4 m_projectionJitter;
  108. Mat4 m_viewProjectionJitter;
  109. Mat4 m_invertedViewProjectionJitter; ///< To unproject in world space.
  110. Mat4 m_invertedViewProjection;
  111. Mat4 m_invertedProjectionJitter; ///< To unproject in view space.
  112. /// It's being used to reproject a clip space position of the current frame to the previous frame. Its value should
  113. /// be m_jitter * m_prevFrame.m_viewProjection * m_invertedViewProjectionJitter. At first it unprojects the current
  114. /// position to world space, all fine here. Then it projects to the previous frame as if the previous frame was
  115. /// using the current frame's jitter matrix.
  116. Mat4 m_reprojection;
  117. /// To unproject to view space. Jitter not considered.
  118. /// @code
  119. /// F32 z = m_unprojectionParameters.z / (m_unprojectionParameters.w + depth);
  120. /// Vec2 xy = ndc * m_unprojectionParameters.xy * z;
  121. /// pos = Vec3(xy, z);
  122. /// @endcode
  123. Vec4 m_unprojectionParameters;
  124. };
  125. constexpr U32 kSizeof_CommonMatrices = 43u * sizeof(Vec4);
  126. static_assert(sizeof(CommonMatrices) == kSizeof_CommonMatrices);
  127. /// Common uniforms for light shading passes.
  128. struct ClusteredShadingUniforms
  129. {
  130. Vec2 m_renderingSize;
  131. F32 m_time;
  132. U32 m_frame;
  133. Vec4 m_nearPlaneWSpace;
  134. Vec3 m_cameraPosition;
  135. F32 m_reflectionProbesMipCount;
  136. UVec2 m_tileCounts;
  137. U32 m_zSplitCount;
  138. F32 m_zSplitCountOverFrustumLength; ///< m_zSplitCount/(far-near)
  139. Vec2 m_zSplitMagic; ///< It's the "a" and "b" of computeZSplitClusterIndex(). See there for details.
  140. U32 m_tileSize;
  141. U32 m_lightVolumeLastZSplit;
  142. UVec2 m_padding0;
  143. F32 m_near;
  144. F32 m_far;
  145. DirectionalLight m_directionalLight;
  146. CommonMatrices m_matrices;
  147. CommonMatrices m_previousMatrices;
  148. };
  149. // Define the type of some cluster object masks
  150. #if ANKI_GLSL
  151. # if ANKI_CLUSTERED_SHADING_USE_64BIT
  152. # define ExtendedClusterObjectMask U64
  153. # else
  154. # define ExtendedClusterObjectMask U32
  155. # endif
  156. #else
  157. # if ANKI_CLUSTERED_SHADING_USE_64BIT
  158. typedef U64 ExtendedClusterObjectMask;
  159. # else
  160. typedef U32 ExtendedClusterObjectMask;
  161. # endif
  162. #endif
  163. /// Information that a tile or a Z-split will contain.
  164. struct Cluster
  165. {
  166. ExtendedClusterObjectMask m_pointLightsMask;
  167. ExtendedClusterObjectMask m_spotLightsMask;
  168. ExtendedClusterObjectMask m_decalsMask;
  169. U32 m_fogDensityVolumesMask;
  170. U32 m_reflectionProbesMask;
  171. U32 m_giProbesMask;
  172. // Pad to 16byte
  173. #if ANKI_CLUSTERED_SHADING_USE_64BIT
  174. U32 m_padding0;
  175. U32 m_padding1;
  176. U32 m_padding2;
  177. #else
  178. U32 m_padding0;
  179. U32 m_padding1;
  180. #endif
  181. };
  182. #if ANKI_CLUSTERED_SHADING_USE_64BIT
  183. constexpr U32 kSizeof_Cluster = 3u * sizeof(Vec4);
  184. static_assert(sizeof(Cluster) == kSizeof_Cluster);
  185. #else
  186. constexpr U32 kSizeof_Cluster = 2u * sizeof(Vec4);
  187. static_assert(sizeof(Cluster) == kSizeof_Cluster);
  188. #endif
  189. constexpr ANKI_ARRAY(U32, GpuSceneNonRenderableObjectType::kCount, kClusteredObjectSizes2) = {
  190. sizeof(LightUnion), sizeof(Decal), sizeof(FogDensityVolume), sizeof(ReflectionProbe), sizeof(GlobalIlluminationProbe)};
  191. constexpr ANKI_ARRAY(U32, GpuSceneNonRenderableObjectType::kCount, kMaxVisibleClusteredObjects2) = {
  192. kMaxVisibleLights, kMaxVisibleDecals, kMaxVisibleFogDensityVolumes, kMaxVisibleReflectionProbes, kMaxVisibleGlobalIlluminationProbes};
  193. ANKI_END_NAMESPACE