| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Shaders/Include/GpuSceneTypes.h>
- #define ANKI_CLUSTERED_SHADING_USE_64BIT ANKI_SUPPORTS_64BIT_TYPES
- ANKI_BEGIN_NAMESPACE
- // Limits
- #if ANKI_CLUSTERED_SHADING_USE_64BIT
- constexpr U32 kMaxVisibleLights = 64u;
- constexpr U32 kMaxVisibleDecals = 64u;
- #else
- constexpr U32 kMaxVisibleLights = 32u;
- constexpr U32 kMaxVisibleDecals = 32u;
- #endif
- constexpr U32 kMaxVisibleFogDensityVolumes = 16u;
- constexpr U32 kMaxVisibleReflectionProbes = 16u;
- constexpr U32 kMaxVisibleGlobalIlluminationProbes = 8u;
- // Other consts
- constexpr RF32 kClusterObjectFrustumNearPlane = 0.1f / 4.0f; ///< Near plane of all clusterer object frustums.
- constexpr RF32 kSubsurfaceMin = 0.01f;
- constexpr U32 kMaxZsplitCount = 128u;
- /// A union of all fields of spot and point lights. Since we don't have unions in HLSL we had to get creative.
- struct LightUnion
- {
- Vec3 m_position; ///< Position in world space.
- RF32 m_radius; ///< Radius
- RVec3 m_diffuseColor;
- U32 m_lightType; ///< 0 is point and 1 is spot
- U32 m_shadowLayer; ///< Shadow layer used in RT shadows
- U32 m_shadow;
- F32 m_innerCos; ///< SPOT LIGHTS
- F32 m_outerCos; ///< SPOT LIGHTS
- RVec3 m_direction; ///< SPOT LIGHTS: Light direction.
- F32 m_shadowAtlasTileScale; ///< POINT LIGHTS: UV scale for all tiles.
- /// 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
- /// the rows of the texture matrix
- Vec4 m_spotLightMatrixOrPointLightUvViewports[6u];
- };
- /// Point light.
- /// WARNING: Don't change the layout without looking at LightUnion.
- struct PointLight
- {
- Vec3 m_position; ///< Position in world space.
- RF32 m_radius; ///< Radius
- RVec3 m_diffuseColor;
- U32 m_padding1;
- U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
- F32 m_shadow;
- F32 m_padding2;
- U32 m_padding3;
- RVec3 m_padding4;
- F32 m_shadowAtlasTileScale; ///< UV scale for all tiles.
- Vec4 m_shadowAtlasTileOffsets[6u]; ///< It's a array of Vec2 but because of padding round it up.
- };
- static_assert(sizeof(PointLight) == sizeof(LightUnion));
- /// Spot light.
- /// WARNING: Don't change the layout without looking at LightUnion.
- struct SpotLight
- {
- Vec3 m_position; ///< Position in world space.
- RF32 m_radius; ///< Radius
- RVec3 m_diffuseColor;
- U32 m_padding1;
- U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
- U32 m_shadow;
- F32 m_innerCos;
- F32 m_outerCos;
- RVec3 m_direction;
- F32 m_padding2;
- Mat4 m_textureMatrix;
- Vec4 m_padding3[2];
- };
- static_assert(sizeof(SpotLight) == sizeof(LightUnion));
- /// Directional light (sun).
- struct DirectionalLight
- {
- RVec3 m_diffuseColor;
- U32 m_shadowCascadeCount; ///< If it's zero then it doesn't cast shadow.
- RVec3 m_direction;
- U32 m_active;
- Vec4 m_shadowCascadeDistances;
- Vec3 m_padding0;
- U32 m_shadowLayer; ///< Shadow layer used in RT shadows. Also used to show that it doesn't cast shadow.
- Mat4 m_textureMatrices[kMaxShadowCascades];
- };
- constexpr U32 kSizeof_DirectionalLight = 4u * sizeof(Vec4) + kMaxShadowCascades * sizeof(Mat4);
- static_assert(sizeof(DirectionalLight) == kSizeof_DirectionalLight);
- static_assert(kMaxShadowCascades == 4u); // Because m_shadowCascadeDistances is a Vec4
- /// Representation of a reflection probe.
- typedef GpuSceneReflectionProbe ReflectionProbe;
- /// Decal.
- typedef GpuSceneDecal Decal;
- /// Fog density volume.
- typedef GpuSceneFogDensityVolume FogDensityVolume;
- /// Global illumination probe
- typedef GpuSceneGlobalIlluminationProbe GlobalIlluminationProbe;
- /// Common matrices.
- struct CommonMatrices
- {
- Mat3x4 m_cameraTransform;
- Mat3x4 m_view;
- Mat4 m_projection;
- Mat4 m_viewProjection;
- Mat4 m_jitter;
- Mat4 m_projectionJitter;
- Mat4 m_viewProjectionJitter;
- Mat4 m_invertedViewProjectionJitter; ///< To unproject in world space.
- Mat4 m_invertedViewProjection;
- Mat4 m_invertedProjectionJitter; ///< To unproject in view space.
- /// It's being used to reproject a clip space position of the current frame to the previous frame. Its value should
- /// be m_jitter * m_prevFrame.m_viewProjection * m_invertedViewProjectionJitter. At first it unprojects the current
- /// position to world space, all fine here. Then it projects to the previous frame as if the previous frame was
- /// using the current frame's jitter matrix.
- Mat4 m_reprojection;
- /// To unproject to view space. Jitter not considered.
- /// @code
- /// F32 z = m_unprojectionParameters.z / (m_unprojectionParameters.w + depth);
- /// Vec2 xy = ndc * m_unprojectionParameters.xy * z;
- /// pos = Vec3(xy, z);
- /// @endcode
- Vec4 m_unprojectionParameters;
- };
- constexpr U32 kSizeof_CommonMatrices = 43u * sizeof(Vec4);
- static_assert(sizeof(CommonMatrices) == kSizeof_CommonMatrices);
- /// Common uniforms for light shading passes.
- struct ClusteredShadingUniforms
- {
- Vec2 m_renderingSize;
- F32 m_time;
- U32 m_frame;
- Vec4 m_nearPlaneWSpace;
- Vec3 m_cameraPosition;
- F32 m_reflectionProbesMipCount;
- UVec2 m_tileCounts;
- U32 m_zSplitCount;
- F32 m_zSplitCountOverFrustumLength; ///< m_zSplitCount/(far-near)
- Vec2 m_zSplitMagic; ///< It's the "a" and "b" of computeZSplitClusterIndex(). See there for details.
- U32 m_tileSize;
- U32 m_lightVolumeLastZSplit;
- UVec2 m_padding0;
- F32 m_near;
- F32 m_far;
- DirectionalLight m_directionalLight;
- CommonMatrices m_matrices;
- CommonMatrices m_previousMatrices;
- };
- // Define the type of some cluster object masks
- #if ANKI_GLSL
- # if ANKI_CLUSTERED_SHADING_USE_64BIT
- # define ExtendedClusterObjectMask U64
- # else
- # define ExtendedClusterObjectMask U32
- # endif
- #else
- # if ANKI_CLUSTERED_SHADING_USE_64BIT
- typedef U64 ExtendedClusterObjectMask;
- # else
- typedef U32 ExtendedClusterObjectMask;
- # endif
- #endif
- /// Information that a tile or a Z-split will contain.
- struct Cluster
- {
- ExtendedClusterObjectMask m_pointLightsMask;
- ExtendedClusterObjectMask m_spotLightsMask;
- ExtendedClusterObjectMask m_decalsMask;
- U32 m_fogDensityVolumesMask;
- U32 m_reflectionProbesMask;
- U32 m_giProbesMask;
- // Pad to 16byte
- #if ANKI_CLUSTERED_SHADING_USE_64BIT
- U32 m_padding0;
- U32 m_padding1;
- U32 m_padding2;
- #else
- U32 m_padding0;
- U32 m_padding1;
- #endif
- };
- #if ANKI_CLUSTERED_SHADING_USE_64BIT
- constexpr U32 kSizeof_Cluster = 3u * sizeof(Vec4);
- static_assert(sizeof(Cluster) == kSizeof_Cluster);
- #else
- constexpr U32 kSizeof_Cluster = 2u * sizeof(Vec4);
- static_assert(sizeof(Cluster) == kSizeof_Cluster);
- #endif
- constexpr ANKI_ARRAY(U32, GpuSceneNonRenderableObjectType::kCount, kClusteredObjectSizes2) = {
- sizeof(LightUnion), sizeof(Decal), sizeof(FogDensityVolume), sizeof(ReflectionProbe), sizeof(GlobalIlluminationProbe)};
- constexpr ANKI_ARRAY(U32, GpuSceneNonRenderableObjectType::kCount, kMaxVisibleClusteredObjects2) = {
- kMaxVisibleLights, kMaxVisibleDecals, kMaxVisibleFogDensityVolumes, kMaxVisibleReflectionProbes, kMaxVisibleGlobalIlluminationProbes};
- ANKI_END_NAMESPACE
|