@@ -735,6 +735,49 @@ bool vhCmdBackendState::BE_PreSubmitCommon_FindResource(
735735 return true ;
736736 }
737737
738+ // Check for User Globals ($Globals)
739+ // These aren't explicitly bound in vhState::buffers, so we must find them via reflection.
740+ // TODO: Move this to the cache, to avoid O(N^3) loop here.
741+ for ( const auto * shader : scache.bshaders )
742+ {
743+ for ( const auto & res : shader->reflection )
744+ {
745+ if ( res.slot == item.slot && res.type == nvrhi::ResourceType::ConstantBuffer && ( res.name == " $Globals" || res.name == " _Globals" || res.name == " globalParams" ) )
746+ {
747+ if ( state.debugFlags & VRHI_STATE_DEBUG_LOG_ALL_BINDINGS )
748+ {
749+ VRHI_LOG ( " FindResource: Found User Globals '%s' at slot %u. Size: %u\n " , res.name .c_str (), res.slot , res.sizeInBytes );
750+ for ( auto & m : res.members ) VRHI_LOG ( " Member: %s ( Offset: %u )\n " , m.name .c_str (), m.offset );
751+ }
752+ if ( res.sizeInBytes == 0 ) continue ;
753+
754+ static std::vector< uint8_t > s_globalPackBuffer;
755+ uint32_t packSize = res.sizeInBytes ;
756+ if ( ( state.debugFlags & VRHI_STATE_DEBUG_LOG_ALL_BINDINGS ) && packSize > 65536 )
757+ {
758+ VRHI_LOG ( " FindResource: Large Globals CB size %u\n " , packSize );
759+ }
760+
761+ uint32_t alignedSize = ( uint32_t ) VRHI_ROUND_UP ( packSize, VRHI_CBUF_ALIGN );
762+ if ( s_globalPackBuffer.size () < alignedSize ) s_globalPackBuffer.resize ( alignedSize, 0 );
763+ vhPackUserGlobals ( state.uniforms , res.members , s_globalPackBuffer.data (), packSize );
764+
765+ int64_t offset = m_userUniformBuffer.Write ( s_globalPackBuffer.data (), alignedSize );
766+ if ( offset < 0 )
767+ {
768+ if ( state.debugFlags & VRHI_STATE_DEBUG_LOG_BINDING_MISMATCH ) VRHI_ERR ( " FindResource: Failed to write UserGlobals ($Globals)\n " );
769+ return false ;
770+ }
771+
772+ nvrhi::BufferRange range ( offset, alignedSize );
773+ outItem = nvrhi::BindingSetItem::ConstantBuffer ( item.slot , m_userUniformBuffer.handle [ m_userUniformBuffer.frameIdx ], range );
774+ outItem.type = item.type ;
775+ if ( state.debugFlags & VRHI_STATE_DEBUG_LOG_ALL_BINDINGS ) VRHI_LOG ( " FindResource: UserGlobals bound to slot %d\n " , item.slot );
776+ return true ;
777+ }
778+ }
779+ }
780+
738781 auto it = stageTable.bufferTable .find ( item.slot );
739782 if ( it == stageTable.bufferTable .end () )
740783 {
@@ -1363,6 +1406,14 @@ void vhCmdBackendState::init()
13631406 descWorld.setCpuAccess ( nvrhi::CpuAccessMode::Write );
13641407 descWorld.setDebugName ( " WorldUniforms" );
13651408 m_worldUniformBuffer.Init_DeviceStateLocked ( descWorld );
1409+
1410+ nvrhi::BufferDesc descUser;
1411+ descUser.setByteSize ( g_vhInit.maxUserGlobals );
1412+ descUser.setIsConstantBuffer ( true );
1413+ descUser.setCpuAccess ( nvrhi::CpuAccessMode::Write );
1414+ descUser.setDebugName ( " UserUniforms" );
1415+ m_userUniformBuffer.Init_DeviceStateLocked ( descUser );
1416+ assert ( g_vhInit.maxUserGlobals % VRHI_CBUF_ALIGN == 0 );
13661417 }
13671418}
13681419
@@ -1373,6 +1424,7 @@ void vhCmdBackendState::shutdown()
13731424
13741425 m_globalUniformBuffer.Shutdown_DeviceStateLocked ();
13751426 m_worldUniformBuffer.Shutdown_DeviceStateLocked ();
1427+ m_userUniformBuffer.Shutdown_DeviceStateLocked ();
13761428
13771429 backendTextures.clear ();
13781430 backendBuffers.clear ();
@@ -2169,6 +2221,7 @@ void vhCmdBackendState::Handle_vhFlushInternal( VIDL_vhFlushInternal* cmd )
21692221
21702222 // Flush and step transient buffer maps here.
21712223 // This needs to be done *before* we flush the command lists to GPU!!
2224+
21722225 m_globalUniformBuffer.Unmap_DeviceStateLocked ();
21732226 m_globalUniformBuffer.Step ();
21742227 m_globalUniformBufferLastHash = 0 ;
@@ -2177,6 +2230,9 @@ void vhCmdBackendState::Handle_vhFlushInternal( VIDL_vhFlushInternal* cmd )
21772230 m_worldUniformBuffer.Step ();
21782231 m_worldUniformBufferLastHash = 0 ;
21792232
2233+ m_userUniformBuffer.Unmap_DeviceStateLocked ();
2234+ m_userUniformBuffer.Step ();
2235+
21802236 // Send it!!
21812237 vhCmdListFlushAll_DeviceStateLocked ();
21822238
0 commit comments