GpuSceneTypes.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // This file contains the GPU scene data types.
  6. // These types have in-place initialization for some members. Members that are used in visibility testing. These default values aim to essentially
  7. // make these objects not visible.
  8. #pragma once
  9. #include <AnKi/Shaders/Include/MeshTypes.h>
  10. #include <AnKi/Shaders/Include/ParticleTypes.h>
  11. ANKI_BEGIN_NAMESPACE
  12. // Some far distance that will make objects not visible. Don't use kMaxF32 because this value will be used in math ops and it might overflow
  13. constexpr F32 kSomeFarDistance = 100000.0f;
  14. // Note: All offsets in bytes
  15. struct GpuSceneRenderable
  16. {
  17. U32 m_worldTransformsIndex; // First index points to the crnt transform and the 2nd to the previous.
  18. U32 m_constantsOffset;
  19. U32 m_meshLodsIndex; // Points to the array of GpuSceneMeshLod. kMaxLodCount are reserved for each renderable.
  20. U32 m_boneTransformsOffset; // Array of Mat3x4 or 0 if its not a skin.
  21. U32 m_particleEmitterIndex; // Index to the GpuSceneParticleEmitter array or kMaxU32 if it's not an emitter.
  22. U32 m_particleEmitterIndex2; // Index to the GpuSceneParticleEmitter2 array or kMaxU32 if it's not an emitter.
  23. U32 m_rtShadowsShaderHandleIndex; // The index of the shader handle in the array of library's handles.
  24. U32 m_rtMaterialFetchShaderHandleIndex; // The index of the shader handle in the array of library's handles.
  25. U32 m_uuid; // A UUID specific for this renderable. Not related to the scene object
  26. U32 m_sceneNodeUuid;
  27. U32 m_diffuseColor : 24; // The average diffuse color of the renderable. Blue is in low bits.
  28. U32 m_padding : 8;
  29. };
  30. // Almost similar to GpuSceneRenderable but with only what the material shaders need. Make it as small as possible
  31. struct GpuScenePerDraw
  32. {
  33. U32 m_worldTransformsIndex : 20;
  34. U32 m_particleEmitterIndex : 11;
  35. U32 m_isParticleEmitter : 1;
  36. U32 m_constantsOffset;
  37. U32 m_meshLodIndex; // Points to a single GpuSceneMeshLod in the mesh lods.
  38. U32 m_boneTransformsOffset;
  39. };
  40. static_assert(sizeof(GpuScenePerDraw) == sizeof(UVec4));
  41. // Minimal data passed to the vertex shaders in the case of meshlet rendering (both SW and HW).
  42. struct GpuSceneMeshletInstance
  43. {
  44. U32 m_worldTransformsIndex_25bit_meshletPrimitiveCount_7bit;
  45. U32 m_constantsOffset;
  46. U32 m_meshletGeometryDescriptorIndex; // Index in the UGB.
  47. U32 m_boneTransformsOffsetOrParticleEmitterIndex;
  48. };
  49. static_assert(kMaxPrimitivesPerMeshlet < ((1u << 7u) - 1));
  50. // Used in visibility testing.
  51. struct GpuSceneRenderableBoundingVolume
  52. {
  53. Vec3 m_aabbMin ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  54. F32 m_sphereRadius ANKI_CPP_CODE(= 0.0f);
  55. Vec3 m_aabbMax ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  56. U32 m_renderableIndex : 20;
  57. U32 m_renderStateBucket : 12;
  58. };
  59. static_assert(sizeof(GpuSceneRenderableBoundingVolume) == sizeof(Vec4) * 2);
  60. // Represents the geometry data of a single LOD of an indexed mesh.
  61. struct GpuSceneMeshLod
  62. {
  63. U32 m_vertexOffsets[(U32)VertexStreamId::kMeshRelatedCount];
  64. U32 m_indexCount;
  65. U32 m_firstIndex; // In sizeof(indexType)
  66. U32 m_lod;
  67. U32 m_firstMeshletBoundingVolume; // In sizeof(MeshletBoundingVolume)
  68. U32 m_firstMeshletGeometryDescriptor; // In sizeof(MeshletGeometryDescriptor)
  69. U32 m_meshletCount; // Can be zero if the mesh doesn't support mesh shading (or mesh shading is off)
  70. U32 m_padding1;
  71. Vec3 m_positionTranslation;
  72. F32 m_positionScale;
  73. UVec2 m_blasAddress;
  74. U32 m_tlasInstanceMask; // Mask that goes to AccelerationStructureInstance::m_mask
  75. U32 m_padding3;
  76. };
  77. static_assert(sizeof(GpuSceneMeshLod) == sizeof(Vec4) * 5);
  78. struct GpuSceneParticleEmitter
  79. {
  80. U32 m_vertexOffsets[(U32)VertexStreamId::kParticleRelatedCount];
  81. U32 m_aliveParticleCount;
  82. };
  83. static_assert(sizeof(GpuSceneParticleEmitter) == sizeof(Vec4) * 2);
  84. // Contains common properties for all particle emitters. Primary use is for the simulation
  85. struct GpuSceneParticleEmitter2
  86. {
  87. U32 m_particleStateSteamOffsets[(U32)ParticleProperty::kCount]; // Points to arrays of particle properties. In the GPU scene
  88. U32 m_aliveParticleCount; // The number of the alive particles
  89. U32 m_aliveParticleIndicesOffset; // Points to arrays of indices of the alive particles. Used when rendering
  90. U32 m_particleCount; // The total number of particles
  91. F32 m_emissionPeriod; // How often the emitter emits new particles. In secs. Required
  92. F32 m_timeLeftForNextEmission;
  93. U32 m_particlesPerEmission;
  94. U32 m_particleEmitterPropertiesOffset; // Points to a AnKiParticleEmitterProperties struct that is located in the GPU scene
  95. Vec3 m_particleAabbMin;
  96. U32 m_reinitializeOnNextUpdate ANKI_CPP_CODE(= 1); // Re-init all particles on next update
  97. Vec3 m_particleAabbMax;
  98. U32 m_worldTransformsIndex;
  99. U32 m_uuid; // This is the UUID of the ParticleEmitterComponent
  100. U32 m_padding0;
  101. U32 m_padding1;
  102. U32 m_padding2;
  103. };
  104. static_assert(sizeof(GpuSceneParticleEmitter2) % sizeof(Vec4) == 0);
  105. // A hash of all visible renderables. If it matches between vis tests then skip the drawcalls. Touched only by the GPU.
  106. struct GpuSceneLightVisibleRenderablesHash
  107. {
  108. U32 m_hash;
  109. };
  110. // Point or spot light.
  111. struct GpuSceneLight
  112. {
  113. Vec3 m_position ANKI_CPP_CODE(= Vec3(kSomeFarDistance)); // Position in world space.
  114. F32 m_radius ANKI_CPP_CODE(= 0.0f); // Radius.
  115. Vec3 m_diffuseColor;
  116. U32 m_visibleRenderablesHashIndex; // Points to a GpuSceneLightVisibleRenderablesHash
  117. U32 m_isPointLight : 1;
  118. U32 m_isSpotLight : 1;
  119. U32 m_shadow : 1;
  120. U32 m_cpuFeedback : 1; // If true the GPU visibility will inform the CPU about it
  121. U32 m_padding : 28;
  122. U32 m_componentArrayIndex; // Array index of the LightComponent in the CPU scene.
  123. U32 m_uuid; // The UUID of that light. If it's zero the GPU will not inform the CPU about it.
  124. F32 m_innerCos; // Only for spot light.
  125. Vec3 m_direction; // Only for spot light. Light direction.
  126. F32 m_outerCos; // Only for spot light.
  127. Vec4 m_edgePoints[4u]; // Edge points in world space. Only for spot light.
  128. // If it's a spot light the 4 first rows are the texture matrix. If it's point light it's the UV viewports in the shadow atlas
  129. Vec4 m_spotLightMatrixOrPointLightUvViewports[6u];
  130. };
  131. // Representation of a reflection probe.
  132. struct GpuSceneReflectionProbe
  133. {
  134. Vec3 m_position; // Position of the probe in world space.
  135. U32 m_cubeTexture : 30; // Bindless index of the reflection texture
  136. U32 m_cpuFeedback : 1;
  137. Vec3 m_aabbMin ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  138. U32 m_uuid; // The UUID of that probe
  139. Vec3 m_aabbMax ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  140. U32 m_componentArrayIndex; // Array in the CPU scene.
  141. };
  142. constexpr U32 kSizeof_GpuSceneReflectionProbe = 3u * sizeof(Vec4);
  143. static_assert(sizeof(GpuSceneReflectionProbe) == kSizeof_GpuSceneReflectionProbe);
  144. // Global illumination probe
  145. struct GpuSceneGlobalIlluminationProbe
  146. {
  147. Vec3 m_aabbMin ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  148. U32 m_uuid; // The UUID of that probe
  149. Vec3 m_aabbMax ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  150. U32 m_componentArrayIndex; // Array in the CPU scene.
  151. U32 m_volumeTexture; // Bindless index of the irradiance volume texture.
  152. F32 m_halfTexelSizeU; // (1.0 / textureSize(texArr[textureIndex]).x) / 2.0
  153. F32 m_fadeDistance; // Used to calculate a factor that is zero when fragPos is close to AABB bounds and 1.0 at fadeDistance and less
  154. U32 m_cpuFeedback;
  155. };
  156. constexpr U32 kSizeof_GpuSceneGlobalIlluminationProbe = 3u * sizeof(Vec4);
  157. static_assert(sizeof(GpuSceneGlobalIlluminationProbe) == kSizeof_GpuSceneGlobalIlluminationProbe);
  158. // Decal
  159. struct GpuSceneDecal
  160. {
  161. U32 m_diffuseTexture;
  162. U32 m_roughnessMetalnessTexture;
  163. F32 m_diffuseBlendFactor;
  164. F32 m_roughnessMetalnessFactor;
  165. Mat4 m_textureMatrix;
  166. Vec3 m_sphereCenter ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  167. F32 m_sphereRadius ANKI_CPP_CODE(= 0.0f);
  168. };
  169. constexpr U32 kSizeof_GpuSceneDecal = 2u * sizeof(Vec4) + 1u * sizeof(Mat4);
  170. static_assert(sizeof(GpuSceneDecal) == kSizeof_GpuSceneDecal);
  171. // Fog density volume
  172. struct GpuSceneFogDensityVolume
  173. {
  174. Vec3 m_aabbMinOrSphereCenter ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  175. U32 m_isBox ANKI_CPP_CODE(= 1);
  176. Vec3 m_aabbMaxOrSphereRadius ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
  177. F32 m_density;
  178. };
  179. constexpr U32 kSizeof_GpuSceneFogDensityVolume = 2u * sizeof(Vec4);
  180. static_assert(sizeof(GpuSceneFogDensityVolume) == kSizeof_GpuSceneFogDensityVolume);
  181. enum class GpuSceneNonRenderableObjectType : U32
  182. {
  183. kLight,
  184. kDecal,
  185. kFogDensityVolume,
  186. kReflectionProbe,
  187. kGlobalIlluminationProbe,
  188. kCount,
  189. kFirst = 0
  190. };
  191. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(GpuSceneNonRenderableObjectType)
  192. #define ANKI_GPU_SCENE_NON_RENDERABLE_OBJECT_TYPE_LIGHT 0
  193. #define ANKI_GPU_SCENE_NON_RENDERABLE_OBJECT_TYPE_DECAL 1
  194. #define ANKI_GPU_SCENE_NON_RENDERABLE_OBJECT_TYPE_FOG_DENSITY_VOLUME 2
  195. #define ANKI_GPU_SCENE_NON_RENDERABLE_OBJECT_TYPE_REFLECTION_PROBE 3
  196. #define ANKI_GPU_SCENE_NON_RENDERABLE_OBJECT_TYPE_GLOBAL_ILLUMINATION_PROBE 4
  197. #define ANKI_GPU_SCENE_NON_RENDERABLE_OBJECT_TYPE_COUNT 5
  198. enum class GpuSceneNonRenderableObjectTypeBit : U32
  199. {
  200. kNone = 0,
  201. kPLight = 1 << 0,
  202. kDecal = 1 << 1,
  203. kFogDensityVolume = 1 << 2,
  204. kReflectionProbe = 1 << 3,
  205. kGlobalIlluminationProbe = 1 << 4,
  206. };
  207. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(GpuSceneNonRenderableObjectTypeBit)
  208. // Non-renderable types that require GPU to CPU feedback
  209. enum class GpuSceneNonRenderableObjectTypeWithFeedback : U32
  210. {
  211. kLight,
  212. kReflectionProbe,
  213. kGlobalIlluminationProbe,
  214. kCount,
  215. kFirst = 0
  216. };
  217. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(GpuSceneNonRenderableObjectTypeWithFeedback)
  218. ANKI_END_NAMESPACE