ClusteredShadingTypes.h 9.3 KB

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