From f451b41981e8ccb7491af44dd0d683b1cd48116f Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Mon, 15 Aug 2022 14:28:10 +0200 Subject: [PATCH] SP: Update debug drawing so that it's only allocating vertex/index buffer once. --- neo/renderer/ImmediateMode.cpp | 50 ++++++++++------------ neo/renderer/ImmediateMode.h | 9 ++-- neo/renderer/NVRHI/BufferObject_NVRHI.cpp | 19 ++++---- neo/renderer/NVRHI/RenderBackend_NVRHI.cpp | 2 +- 4 files changed, 38 insertions(+), 42 deletions(-) diff --git a/neo/renderer/ImmediateMode.cpp b/neo/renderer/ImmediateMode.cpp index cbf955b4..b4e86a9d 100644 --- a/neo/renderer/ImmediateMode.cpp +++ b/neo/renderer/ImmediateMode.cpp @@ -40,12 +40,12 @@ extern DeviceManager* deviceManager; namespace { -const int c_drawVertsCapacity = ( 20000 * 4 ); -//const int c_drawIndexesCapacity = ( 20000 * 6 ); +const int c_drawVertsCapacity = ( 40000 * 4 ); +const int c_drawIndexesCapacity = ( c_drawVertsCapacity * 2 ); idDrawVert drawVerts[c_drawVertsCapacity]; -triIndex_t lineIndices[c_drawVertsCapacity * 2]; -triIndex_t sphereIndices[c_drawVertsCapacity * 2]; +triIndex_t lineIndices[c_drawIndexesCapacity]; +triIndex_t sphereIndices[c_drawIndexesCapacity]; bool active = false; } @@ -53,7 +53,10 @@ bool active = false; int fhImmediateMode::drawCallCount = 0; int fhImmediateMode::drawCallVertexSize = 0; -void fhImmediateMode::Init() +idVertexBuffer fhImmediateMode::vertexBuffer; +idIndexBuffer fhImmediateMode::indexBuffer; + +void fhImmediateMode::Init( nvrhi::ICommandList* commandList ) { for( int i = 0; i < c_drawVertsCapacity * 2; ++i ) { @@ -61,6 +64,7 @@ void fhImmediateMode::Init() } ResetStats(); + InitBuffers( commandList ); } void fhImmediateMode::ResetStats() @@ -68,6 +72,7 @@ void fhImmediateMode::ResetStats() drawCallCount = 0; drawCallVertexSize = 0; } + int fhImmediateMode::DrawCallCount() { return drawCallCount; @@ -78,6 +83,14 @@ int fhImmediateMode::DrawCallVertexSize() return drawCallVertexSize; } +void fhImmediateMode::InitBuffers( nvrhi::ICommandList* commandList ) +{ + commandList->open(); + vertexBuffer.AllocBufferObject( nullptr, c_drawVertsCapacity * sizeof( idDrawVert ), bufferUsageType_t::BU_STATIC, commandList ); + indexBuffer.AllocBufferObject( nullptr, c_drawIndexesCapacity * sizeof( triIndex_t ), bufferUsageType_t::BU_STATIC, commandList ); + commandList->close(); + deviceManager->GetDevice()->executeCommandList( commandList ); +} fhImmediateMode::fhImmediateMode( nvrhi::ICommandList* _commandList, bool geometryOnly ) : commandList( _commandList ), @@ -116,27 +129,8 @@ void fhImmediateMode::End() return; } - nvrhi::BufferDesc vertexBufferDesc; - vertexBufferDesc.byteSize = drawVertsUsed * sizeof( idDrawVert ); - vertexBufferDesc.isVertexBuffer = true; - vertexBufferDesc.debugName = "VertexBuffer"; - vertexBufferDesc.initialState = nvrhi::ResourceStates::CopyDest; - vertexBuffer = deviceManager->GetDevice()->createBuffer( vertexBufferDesc ); - - commandList->beginTrackingBufferState( vertexBuffer, nvrhi::ResourceStates::CopyDest ); - commandList->writeBuffer( vertexBuffer, drawVerts, drawVertsUsed * sizeof( idDrawVert ) ); - commandList->setPermanentBufferState( vertexBuffer, nvrhi::ResourceStates::VertexBuffer ); - - nvrhi::BufferDesc indexBufferDesc; - indexBufferDesc.byteSize = drawVertsUsed * sizeof( triIndex_t ); - indexBufferDesc.isIndexBuffer = true; - indexBufferDesc.debugName = "IndexBuffer"; - indexBufferDesc.initialState = nvrhi::ResourceStates::CopyDest; - indexBuffer = deviceManager->GetDevice()->createBuffer( indexBufferDesc ); - - commandList->beginTrackingBufferState( indexBuffer, nvrhi::ResourceStates::CopyDest ); - commandList->writeBuffer( indexBuffer, lineIndices, drawVertsUsed * sizeof( triIndex_t ) ); - commandList->setPermanentBufferState( indexBuffer, nvrhi::ResourceStates::IndexBuffer ); + vertexBuffer.Update( drawVerts, drawVertsUsed * sizeof( idDrawVert ), 0, false, commandList ); + indexBuffer.Update( lineIndices, drawVertsUsed * sizeof( triIndex_t ), 0, false, commandList ); renderProgManager.CommitConstantBuffer( commandList ); @@ -167,8 +161,8 @@ void fhImmediateMode::End() state.bindings.push_back( tr.backend.currentBindingSets[i] ); } - state.indexBuffer = { indexBuffer, nvrhi::Format::R16_UINT, 0 }; - state.vertexBuffers = { { vertexBuffer, 0, 0 } }; + state.indexBuffer = { indexBuffer.GetAPIObject(), nvrhi::Format::R16_UINT, 0}; + state.vertexBuffers = { { vertexBuffer.GetAPIObject(), 0, 0 } }; state.pipeline = pipeline; state.framebuffer = tr.backend.currentFrameBuffer->GetApiObject(); diff --git a/neo/renderer/ImmediateMode.h b/neo/renderer/ImmediateMode.h index b18400eb..9a743ba9 100644 --- a/neo/renderer/ImmediateMode.h +++ b/neo/renderer/ImmediateMode.h @@ -72,14 +72,17 @@ public: static void AddTrianglesFromPolygon( fhImmediateMode& im, const idVec3* xyz, int num ); - static void Init(); + static void Init( nvrhi::ICommandList* commandList ); static void ResetStats(); static int DrawCallCount(); static int DrawCallVertexSize(); private: + + static void InitBuffers( nvrhi::ICommandList* commandList ); + nvrhi::CommandListHandle commandList; - nvrhi::BufferHandle vertexBuffer; - nvrhi::BufferHandle indexBuffer; + static idVertexBuffer vertexBuffer; + static idIndexBuffer indexBuffer; bool geometryOnly; float currentTexCoord[2]; diff --git a/neo/renderer/NVRHI/BufferObject_NVRHI.cpp b/neo/renderer/NVRHI/BufferObject_NVRHI.cpp index be662ce5..f7838f21 100644 --- a/neo/renderer/NVRHI/BufferObject_NVRHI.cpp +++ b/neo/renderer/NVRHI/BufferObject_NVRHI.cpp @@ -115,7 +115,7 @@ bool idVertexBuffer::AllocBufferObject( const void* data, int allocSize, bufferU } else { - vertexBufferDesc.initialState = nvrhi::ResourceStates::Common; + vertexBufferDesc.initialState = nvrhi::ResourceStates::CopyDest; vertexBufferDesc.keepInitialState = true; vertexBufferDesc.debugName = "Static idDrawVert vertex buffer"; } @@ -128,7 +128,7 @@ bool idVertexBuffer::AllocBufferObject( const void* data, int allocSize, bufferU } // copy the data - if( data != NULL ) + if( data ) { Update( data, allocSize, 0, true, commandList ); } @@ -196,9 +196,9 @@ void idVertexBuffer::Update( const void* data, int updateSize, int offset, bool { if( initialUpdate ) { - commandList->beginTrackingBufferState( bufferHandle, nvrhi::ResourceStates::Common ); + commandList->beginTrackingBufferState( bufferHandle, nvrhi::ResourceStates::CopyDest ); commandList->writeBuffer( bufferHandle, data, numBytes, GetOffset() + offset ); - commandList->setPermanentBufferState( bufferHandle, nvrhi::ResourceStates::ShaderResource | nvrhi::ResourceStates::VertexBuffer ); + commandList->setPermanentBufferState( bufferHandle, nvrhi::ResourceStates::VertexBuffer ); } else { @@ -309,21 +309,20 @@ bool idIndexBuffer::AllocBufferObject( const void* data, int allocSize, bufferUs nvrhi::BufferDesc indexBufferDesc; indexBufferDesc.byteSize = numBytes; indexBufferDesc.isIndexBuffer = true; - indexBufferDesc.initialState = nvrhi::ResourceStates::Common; + indexBufferDesc.initialState = nvrhi::ResourceStates::CopyDest; indexBufferDesc.canHaveRawViews = true; indexBufferDesc.canHaveTypedViews = true; indexBufferDesc.format = nvrhi::Format::R16_UINT; if( _usage == BU_STATIC ) { - indexBufferDesc.debugName = "VertexCache Static Index Buffer"; indexBufferDesc.keepInitialState = true; + indexBufferDesc.debugName = "VertexCache Static Index Buffer"; } else if( _usage == BU_DYNAMIC ) { - indexBufferDesc.debugName = "VertexCache Mapped Index Buffer"; - indexBufferDesc.initialState = nvrhi::ResourceStates::CopyDest; indexBufferDesc.cpuAccess = nvrhi::CpuAccessMode::Write; + indexBufferDesc.debugName = "VertexCache Mapped Index Buffer"; } bufferHandle = deviceManager->GetDevice()->createBuffer( indexBufferDesc ); @@ -396,9 +395,9 @@ void idIndexBuffer::Update( const void* data, int updateSize, int offset, bool i { if( initialUpdate ) { - commandList->beginTrackingBufferState( bufferHandle, nvrhi::ResourceStates::Common ); + commandList->beginTrackingBufferState( bufferHandle, nvrhi::ResourceStates::CopyDest ); commandList->writeBuffer( bufferHandle, data, numBytes, GetOffset() + offset ); - commandList->setPermanentBufferState( bufferHandle, nvrhi::ResourceStates::IndexBuffer | nvrhi::ResourceStates::ShaderResource ); + commandList->setPermanentBufferState( bufferHandle, nvrhi::ResourceStates::IndexBuffer ); commandList->commitBarriers(); } else diff --git a/neo/renderer/NVRHI/RenderBackend_NVRHI.cpp b/neo/renderer/NVRHI/RenderBackend_NVRHI.cpp index 54bbaffd..b2965e80 100644 --- a/neo/renderer/NVRHI/RenderBackend_NVRHI.cpp +++ b/neo/renderer/NVRHI/RenderBackend_NVRHI.cpp @@ -160,7 +160,7 @@ void idRenderBackend::Init() commandList->close(); deviceManager->GetDevice()->executeCommandList( commandList ); - fhImmediateMode::Init(); + fhImmediateMode::Init( commandList ); // allocate the frame data, which may be more if smp is enabled R_InitFrameData();