Skip to content

Commit 4596428

Browse files
committed
Add global uniform buffer support with vhGlobalUniform struct
1 parent a6f9559 commit 4596428

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

src/vrhi_backend.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,13 +1028,40 @@ void vhCmdBackendState::BE_BlitBuffer( vhBackendBuffer& dst, vhBackendBuffer& sr
10281028
void vhCmdBackendState::init()
10291029
{
10301030
std::lock_guard< std::mutex > lock( backendMutex );
1031+
1032+
if ( !m_globalUniformBuffer )
1033+
{
1034+
// Called from vhInit which already holds g_nvRHIStateMutex lock, and before RHI thread even starts.
1035+
// So we don't need to lock g_nvRHIStateMutex here.
1036+
1037+
nvrhi::BufferDesc desc;
1038+
desc.setByteSize( sizeof( vhGlobalUniform ) );
1039+
desc.setIsConstantBuffer( true );
1040+
desc.setDebugName( "GlobalUniforms" );
1041+
nvrhi::BufferHandle bhandle = g_vhDevice->createBuffer( desc );
1042+
if ( !bhandle )
1043+
{
1044+
VRHI_ERR( "vhCmdBackendState::init() : Failed to create Global Uniform Buffer!\n" );
1045+
assert( !"Failed to create Global Uniform Buffer." );
1046+
return;
1047+
}
1048+
1049+
m_globalUniformBuffer = std::make_unique< vhBackendBuffer >();
1050+
m_globalUniformBuffer->handle = bhandle;
1051+
m_globalUniformBuffer->name = "GlobalUniforms";
1052+
m_globalUniformBuffer->desc = desc;
1053+
m_globalUniformBuffer->stride = sizeof( vhGlobalUniform );
1054+
m_globalUniformBuffer->flags = VRHI_BUFFER_NONE;
1055+
}
10311056
}
10321057

10331058
void vhCmdBackendState::shutdown()
10341059
{
10351060
std::lock_guard< std::mutex > lock( backendMutex );
10361061
std::lock_guard< std::mutex > lock2( g_nvRHIStateMutex );
10371062

1063+
m_globalUniformBuffer.reset();
1064+
10381065
backendTextures.clear();
10391066
backendBuffers.clear();
10401067
backendShaders.clear();

src/vrhi_backend.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ struct vhStateResolveCache
120120
// Main Backend State
121121
// --------------------------------------------------------------------------
122122

123+
struct vhGlobalUniform
124+
{
125+
glm::vec4 u_viewRect;
126+
glm::vec4 u_viewTexel;
127+
glm::mat4 u_view;
128+
glm::mat4 u_invView;
129+
glm::mat4 u_proj;
130+
glm::mat4 u_invProj;
131+
glm::mat4 u_viewProj;
132+
glm::mat4 u_invViewProj;
133+
glm::mat4 u_model[4];
134+
glm::mat4 u_modelView;
135+
glm::mat4 u_modelViewProj;
136+
glm::vec4 u_alphaRef4;
137+
glm::vec4 u_global[32];
138+
};
139+
static_assert( sizeof( vhGlobalUniform ) < 16384, "vhGlobalUniform must be smaller than 16KB" );
140+
static_assert( sizeof( vhGlobalUniform ) == 1328, "vhGlobalUniform packing mismatch" );
141+
123142
class vhCmdBackendState : public VIDLHandler
124143
{
125144
friend class vhCmdBackendStateTest;
@@ -132,6 +151,8 @@ class vhCmdBackendState : public VIDLHandler
132151
std::map< vhStateId, vhState > backendStates;
133152
std::unordered_map< uint64_t, nvrhi::FramebufferHandle > backendFramebuffers;
134153

154+
// Internal Global Uniform Buffer
155+
std::unique_ptr< vhBackendBuffer > m_globalUniformBuffer;
135156
// RAII for vhMem, takes ownership of the pointer and auto-destructs it.
136157
inline std::unique_ptr< vhMem > BE_MemRAII( const vhMem* mem )
137158
{

0 commit comments

Comments
 (0)