Skip to content

Commit

Permalink
Clustered: Added lights BVH generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonJoker committed May 29, 2023
1 parent cdc75fe commit 55a66d2
Show file tree
Hide file tree
Showing 36 changed files with 2,183 additions and 241 deletions.
2 changes: 1 addition & 1 deletion external/RenderGraph
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ namespace castor3d
C3D_API crg::FramePass const & createAssignLightsToClustersPass( crg::FramePassGroup & graph
, crg::FramePass const * previousPass
, RenderDevice const & device
, LightCache const & lights
, CameraUbo const & cameraUbo
, FrustumClusters const & clusters );
, FrustumClusters & clusters );
}

#endif
18 changes: 18 additions & 0 deletions include/Core/Castor3D/Render/Clustered/BuildLightsBVH.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
See LICENSE file in root folder
*/
#ifndef ___C3D_BuildLightsBVH_H___
#define ___C3D_BuildLightsBVH_H___

#include "ClusteredModule.hpp"

namespace castor3d
{
C3D_API crg::FramePass const & createBuildLightsBVHPass( crg::FramePassGroup & graph
, crg::FramePass const * previousPass
, RenderDevice const & device
, CameraUbo const & cameraUbo
, FrustumClusters & clusters );
}

#endif
6 changes: 6 additions & 0 deletions include/Core/Castor3D/Render/Clustered/ClusteredModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ See LICENSE file in root folder

#include "Castor3D/Render/RenderModule.hpp"

#include <CastorUtils/Design/DesignModule.hpp>

namespace castor3d
{
/**@name Render */
Expand Down Expand Up @@ -34,6 +36,10 @@ namespace castor3d

CU_DeclareSmartPtr( castor3d, FrustumClusters, C3D_API );

using ClustersBuffersChangedFunction = std::function< void( FrustumClusters const & ) >;
using OnClustersBuffersChanged = castor::SignalT< ClustersBuffersChangedFunction >;
using OnClustersBuffersChangedConnection = castor::ConnectionT< ClustersBuffersChangedFunction >;

//@}
//@}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace castor3d
, crg::FramePass const * previousPass
, RenderDevice const & device
, CameraUbo const & cameraUbo
, FrustumClusters const & clusters );
, FrustumClusters & clusters );
}

#endif
18 changes: 18 additions & 0 deletions include/Core/Castor3D/Render/Clustered/ComputeLightsMortonCode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
See LICENSE file in root folder
*/
#ifndef ___C3D_ComputeLightsMortonCode_H___
#define ___C3D_ComputeLightsMortonCode_H___

#include "ClusteredModule.hpp"

namespace castor3d
{
C3D_API crg::FramePass const & createComputeLightsMortonCodePass( crg::FramePassGroup & graph
, crg::FramePass const * previousPass
, RenderDevice const & device
, CameraUbo const & cameraUbo
, FrustumClusters & clusters );
}

#endif
169 changes: 155 additions & 14 deletions include/Core/Castor3D/Render/Clustered/FrustumClusters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ See LICENSE file in root folder
#include "Castor3D/Shader/Ubos/ClustersUbo.hpp"

#include <CastorUtils/Design/GroupChangeTracked.hpp>
#include <CastorUtils/Design/Signal.hpp>

#include <ashespp/Buffer/Buffer.hpp>

Expand All @@ -34,6 +35,42 @@ namespace castor3d
*\param[in, out] updater Les données d'update.
*/
C3D_API void update( CpuUpdater & updater );
/**
*\~english
*\brief Registers the clusters related frame passes.
*\~french
*\brief Enregistre les passes liées aux clusters.
*/
C3D_API crg::FramePass const & createFramePasses( crg::FramePassGroup & graph
, crg::FramePass const * previousPass
, CameraUbo const & cameraUbo );
/**
*\~english
*\brief Compute the number of nodes for given BVH level.
*\param[in] numLeaves The number of leaf nodes.
*\~french
*\brief Calcule le nombre de noeuds du niveau donné d'un BVH.
*\param[in] numLeaves Le nombre de noeuds feuilles.
*/
C3D_API static uint32_t getNumLevelNodes( uint32_t level );
/**
*\~english
*\brief Compute the number of levels needed for a BVH that consists of a number of leaf nodes.
*\param[in] numLeaves The number of leaf nodes.
*\~french
*\brief Calcule le nombre de niveaux nécessaire pour un BVH contenant le nombre de noeuds feuille donné.
*\param[in] numLeaves Le nombre de noeuds feuilles.
*/
C3D_API static uint32_t getNumLevels( uint32_t numLeaves );
/**
*\~english
*\brief Compute the number of (child) nodes needed to represent a BVH that consists of a number of leaf nodes.
*\param[in] numLeaves The number of leaf nodes.
*\~french
*\brief Calcule le nombre de noeuds (enfants) nécessaire pour un représenter un BVH contenant le nombre de noeuds feuille donné.
*\param[in] numLeaves Le nombre de noeuds feuilles.
*/
C3D_API static uint32_t getNumNodes( uint32_t numLeaves );

castor::Point3ui const & getDimensions()const noexcept
{
Expand All @@ -55,31 +92,126 @@ namespace castor3d
return m_clustersUbo;
}

auto & getAabbBuffer()const noexcept
auto & getClustersAABBBuffer()const noexcept
{
CU_Require( m_aabbBuffer );
return *m_aabbBuffer;
}

auto & getPointLightIndexBuffer()const noexcept
auto & getPointLightClusterGridBuffer()const noexcept
{
CU_Require( m_pointLightClusterGridBuffer );
return *m_pointLightClusterGridBuffer;
}

auto & getSpotLightClusterGridBuffer()const noexcept
{
CU_Require( m_spotLightClusterGridBuffer );
return *m_spotLightClusterGridBuffer;
}

auto & getPointLightClusterIndexBuffer()const noexcept
{
CU_Require( m_pointLightClusterIndexBuffer );
return *m_pointLightClusterIndexBuffer;
}

auto & getSpotLightClusterIndexBuffer()const noexcept
{
CU_Require( m_spotLightClusterIndexBuffer );
return *m_spotLightClusterIndexBuffer;
}

auto & getLightsAABBBuffer()const noexcept
{
CU_Require( m_lightsAABBBuffer );
return *m_lightsAABBBuffer;
}

auto & getPointLightBVHBuffer()const noexcept
{
CU_Require( m_pointBVHBuffer );
return *m_pointBVHBuffer;
}

auto & getSpotLightBVHBuffer()const noexcept
{
return *m_pointIndexBuffer;
CU_Require( m_spotBVHBuffer );
return *m_spotBVHBuffer;
}

auto & getPointLightClusterBuffer()const noexcept
void swapLightMortonIndicesIO()
{
return *m_pointClusterBuffer;
m_lightMortonIndicesInput = 1u - m_lightMortonIndicesInput;
}

auto & getSpotLightIndexBuffer()const noexcept
ashes::BufferBase & getPointLightIndicesBuffer( uint32_t index )const noexcept
{
return *m_spotIndexBuffer;
return *m_pointIndicesBuffers[index];
}

auto & getSpotLightClusterBuffer()const noexcept
ashes::BufferBase & getSpotLightIndicesBuffer( uint32_t index )const noexcept
{
return *m_spotClusterBuffer;
return *m_spotIndicesBuffers[index];
}

ashes::BufferBase & getPointLightMortonCodesBuffer( uint32_t index )const noexcept
{
return *m_pointMortonCodesBuffers[index];
}

ashes::BufferBase & getSpotLightMortonCodesBuffer( uint32_t index )const noexcept
{
return *m_spotMortonCodesBuffers[index];
}

ashes::BufferBase & getInputPointLightIndicesBuffer()const noexcept
{
return getPointLightIndicesBuffer( m_lightMortonIndicesInput );
}

ashes::BufferBase & getInputSpotLightIndicesBuffer()const noexcept
{
return getSpotLightIndicesBuffer( m_lightMortonIndicesInput );
}

ashes::BufferBase & getOutputPointLightIndicesBuffer()const noexcept
{
return getPointLightIndicesBuffer( 1u - m_lightMortonIndicesInput );
}

ashes::BufferBase & getOutputSpotLightIndicesBuffer()const noexcept
{
return getSpotLightIndicesBuffer( 1u - m_lightMortonIndicesInput );
}

ashes::BufferBase & getInputPointLightMortonCodesBuffer()const noexcept
{
return getPointLightMortonCodesBuffer( m_lightMortonIndicesInput );
}

ashes::BufferBase & getInputSpotLightMortonCodesBuffer()const noexcept
{
return getSpotLightMortonCodesBuffer( m_lightMortonIndicesInput );
}

ashes::BufferBase & getOutputPointLightMortonCodesBuffer()const noexcept
{
return getPointLightMortonCodesBuffer( 1u - m_lightMortonIndicesInput );
}

ashes::BufferBase & getOutputSpotLightMortonCodesBuffer()const noexcept
{
return getSpotLightMortonCodesBuffer( 1u - m_lightMortonIndicesInput );
}

auto & getCamera()const noexcept
{
return m_camera;
}

OnClustersBuffersChanged onClusterBuffersChanged;

private:
struct AABB
{
Expand All @@ -97,17 +229,26 @@ namespace castor3d
bool m_clustersDirty{ true };
bool m_lightsDirty{ true };
bool m_first{ true };
uint32_t m_lightMortonIndicesInput{ 1u };
castor::GroupChangeTracked< castor::Point3ui > m_dimensions;
castor::GroupChangeTracked< uint32_t > m_clusterSize;
castor::GroupChangeTracked< castor::Matrix4x4f > m_cameraProjection;
castor::GroupChangeTracked< castor::Matrix4x4f > m_cameraView;
castor::GroupChangeTracked< float > m_nearK;
ClustersUbo m_clustersUbo;
ashes::BufferPtr< AABB > m_aabbBuffer;
ashes::BufferPtr< u32 > m_pointIndexBuffer;
ashes::BufferPtr< castor::Point2ui > m_pointClusterBuffer;
ashes::BufferPtr< u32 > m_spotIndexBuffer;
ashes::BufferPtr< castor::Point2ui > m_spotClusterBuffer;
ashes::BufferBasePtr m_aabbBuffer;
ashes::BufferBasePtr m_pointLightClusterGridBuffer;
ashes::BufferBasePtr m_spotLightClusterGridBuffer;
ashes::BufferBasePtr m_pointLightClusterIndexBuffer;
ashes::BufferBasePtr m_spotLightClusterIndexBuffer;
ashes::BufferBasePtr m_lightsAABBBuffer;
std::array< ashes::BufferBasePtr, 2u > m_pointMortonCodesBuffers;
std::array< ashes::BufferBasePtr, 2u > m_spotMortonCodesBuffers;
std::array< ashes::BufferBasePtr, 2u > m_pointIndicesBuffers;
std::array< ashes::BufferBasePtr, 2u > m_spotIndicesBuffers;
ashes::BufferBasePtr m_pointBVHBuffer;
ashes::BufferBasePtr m_spotBVHBuffer;
std::vector< ashes::BufferBasePtr > m_toDelete;
};
}

Expand Down
18 changes: 18 additions & 0 deletions include/Core/Castor3D/Render/Clustered/ReduceLightsAABB.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
See LICENSE file in root folder
*/
#ifndef ___C3D_ReduceLightsAABB_H___
#define ___C3D_ReduceLightsAABB_H___

#include "ClusteredModule.hpp"

namespace castor3d
{
C3D_API crg::FramePass const & createReduceLightsAABBPass( crg::FramePassGroup & graph
, crg::FramePass const * previousPass
, RenderDevice const & device
, CameraUbo const & cameraUbo
, FrustumClusters & clusters );
}

#endif
2 changes: 1 addition & 1 deletion include/Core/Castor3D/Render/RenderTarget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ namespace castor3d
return m_debugConfig;
}

FrustumClusters const & getFrustumClusters()const noexcept
FrustumClusters & getFrustumClusters()const noexcept
{
CU_Require( m_frustumClusters );
return *m_frustumClusters;
Expand Down
3 changes: 1 addition & 2 deletions include/Core/Castor3D/Render/RenderTechnique.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,7 @@ namespace castor3d
PrepassRendering m_prepass;
crg::FramePass const * m_lastDepthPass{};
crg::FramePass const * m_depthRangePass{};
crg::FramePass const* m_computeClustersAABB{};
crg::FramePass const* m_dispatchLightInClusters{};
crg::FramePass const * m_clustersLastPass{};
BackgroundRendererUPtr m_background{};
OpaqueRendering m_opaque;
crg::FramePass const * m_lastOpaquePass{};
Expand Down
Loading

0 comments on commit 55a66d2

Please sign in to comment.