From 0890fd8f2e485bfdd93ff6227be08ff4142e55d1 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 11 Mar 2022 11:57:53 -0800 Subject: gfx: Add `ITransientResourceHeap::finish()` to avoid `Signal` after every queue submit. (#2158) Co-authored-by: Yong He --- tools/gfx/d3d12/render-d3d12.cpp | 43 ++++++++++++++++++---------------- tools/gfx/d3d12/render-d3d12.h | 4 ++++ tools/gfx/debug-layer.cpp | 6 +++++ tools/gfx/debug-layer.h | 1 + tools/gfx/renderer-shared.h | 2 ++ tools/render-test/render-test-main.cpp | 1 + 6 files changed, 37 insertions(+), 20 deletions(-) (limited to 'tools') diff --git a/tools/gfx/d3d12/render-d3d12.cpp b/tools/gfx/d3d12/render-d3d12.cpp index 67c31158b..de3081f40 100644 --- a/tools/gfx/d3d12/render-d3d12.cpp +++ b/tools/gfx/d3d12/render-d3d12.cpp @@ -2378,6 +2378,7 @@ void DeviceImpl::submitResourceCommandsAndWait(const DeviceImpl::ResourceCommand { info.commandBuffer->close(); m_resourceCommandQueue->executeCommandBuffer(info.commandBuffer); + m_resourceCommandTransientHeap->finish(); m_resourceCommandTransientHeap->synchronizeAndReset(); } @@ -2785,18 +2786,9 @@ Result AccelerationStructureImpl::getNativeHandle(InteropHandle* outHandle) Result TransientResourceHeapImpl::synchronizeAndReset() { - Array waitHandles; - for (auto& waitInfo : m_waitInfos) - { - if (waitInfo.waitValue == 0) - continue; - if (waitInfo.fence) - { - waitInfo.fence->SetEventOnCompletion(waitInfo.waitValue, waitInfo.fenceEvent); - waitHandles.add(waitInfo.fenceEvent); - } - } - WaitForMultipleObjects((DWORD)waitHandles.getCount(), waitHandles.getBuffer(), TRUE, INFINITE); + WaitForMultipleObjects( + (DWORD)m_waitHandles.getCount(), m_waitHandles.getBuffer(), TRUE, INFINITE); + m_waitHandles.clear(); m_currentViewHeapIndex = -1; m_currentSamplerHeapIndex = -1; allocateNewViewDescriptorHeap(m_device); @@ -2809,6 +2801,22 @@ Result TransientResourceHeapImpl::synchronizeAndReset() return SLANG_OK; } +Result TransientResourceHeapImpl::finish() +{ + for (auto& waitInfo : m_waitInfos) + { + if (waitInfo.waitValue == 0) + continue; + if (waitInfo.fence) + { + waitInfo.queue->Signal(waitInfo.fence, waitInfo.waitValue); + waitInfo.fence->SetEventOnCompletion(waitInfo.waitValue, waitInfo.fenceEvent); + m_waitHandles.add(waitInfo.fenceEvent); + } + } + return SLANG_OK; +} + TransientResourceHeapImpl::QueueWaitInfo& TransientResourceHeapImpl::getQueueWaitInfo( uint32_t queueIndex) { @@ -3462,10 +3470,8 @@ void RayTracingCommandEncoderImpl::dispatchRays( auto shaderTableImpl = static_cast(shaderTable); - ResourceCommandEncoderImpl resourceCopyEncoder; - resourceCopyEncoder.init(m_commandBuffer); auto shaderTableBuffer = - shaderTableImpl->getOrCreateBuffer(pipelineImpl, m_transientHeap, &resourceCopyEncoder); + shaderTableImpl->getOrCreateBuffer(pipelineImpl, m_transientHeap, static_cast(this)); auto shaderTableAddr = shaderTableBuffer->getDeviceAddress(); D3D12_DISPATCH_RAYS_DESC dispatchDesc = {}; @@ -5015,8 +5021,8 @@ void CommandQueueImpl::executeCommandBuffers( auto& waitInfo = transientHeap->getQueueWaitInfo(m_queueIndex); waitInfo.waitValue = m_fenceValue; waitInfo.fence = m_fence; + waitInfo.queue = m_d3dQueue; } - m_d3dQueue->Signal(m_fence, m_fenceValue); } if (fence) @@ -6556,10 +6562,7 @@ void ResourceCommandEncoderImpl::textureBarrier( void ResourceCommandEncoderImpl::bufferBarrier( size_t count, IBufferResource* const* buffers, ResourceState src, ResourceState dst) { - - List barriers; - barriers.reserve(count); - + ShortList barriers; for (size_t i = 0; i < count; i++) { auto bufferImpl = static_cast(buffers[i]); diff --git a/tools/gfx/d3d12/render-d3d12.h b/tools/gfx/d3d12/render-d3d12.h index 3ef4a84a7..2cb9a3fd3 100644 --- a/tools/gfx/d3d12/render-d3d12.h +++ b/tools/gfx/d3d12/render-d3d12.h @@ -490,9 +490,11 @@ public: { uint64_t waitValue; HANDLE fenceEvent; + ComPtr queue; ComPtr fence = nullptr; }; ShortList m_waitInfos; + Array m_waitHandles; QueueWaitInfo& getQueueWaitInfo(uint32_t queueIndex); // During command submission, we need all the descriptor tables that get @@ -545,6 +547,8 @@ public: createCommandBuffer(ICommandBuffer** outCommandBuffer) override; virtual SLANG_NO_THROW Result SLANG_MCALL synchronizeAndReset() override; + + virtual SLANG_NO_THROW Result SLANG_MCALL finish() override; }; struct Submitter diff --git a/tools/gfx/debug-layer.cpp b/tools/gfx/debug-layer.cpp index 341f0034a..414434da6 100644 --- a/tools/gfx/debug-layer.cpp +++ b/tools/gfx/debug-layer.cpp @@ -1619,6 +1619,12 @@ Result DebugTransientResourceHeap::synchronizeAndReset() return baseObject->synchronizeAndReset(); } +Result DebugTransientResourceHeap::finish() +{ + SLANG_GFX_API_FUNC; + return baseObject->finish(); +} + Result DebugTransientResourceHeap::createCommandBuffer(ICommandBuffer** outCommandBuffer) { SLANG_GFX_API_FUNC; diff --git a/tools/gfx/debug-layer.h b/tools/gfx/debug-layer.h index 84ae33c2a..de8ccfe8e 100644 --- a/tools/gfx/debug-layer.h +++ b/tools/gfx/debug-layer.h @@ -724,6 +724,7 @@ public: public: ITransientResourceHeap* getInterface(const Slang::Guid& guid); virtual SLANG_NO_THROW Result SLANG_MCALL synchronizeAndReset() override; + virtual SLANG_NO_THROW Result SLANG_MCALL finish() override; virtual SLANG_NO_THROW Result SLANG_MCALL createCommandBuffer(ICommandBuffer** outCommandBuffer) override; }; diff --git a/tools/gfx/renderer-shared.h b/tools/gfx/renderer-shared.h index 24d51c87a..a9d9e19c4 100644 --- a/tools/gfx/renderer-shared.h +++ b/tools/gfx/renderer-shared.h @@ -1150,6 +1150,8 @@ public: return static_cast(this); return nullptr; } + + virtual SLANG_NO_THROW Result SLANG_MCALL finish() override { return SLANG_OK; } }; class ShaderTableBase diff --git a/tools/render-test/render-test-main.cpp b/tools/render-test/render-test-main.cpp index 7046e2fda..81e8cd38a 100644 --- a/tools/render-test/render-test-main.cpp +++ b/tools/render-test/render-test-main.cpp @@ -939,6 +939,7 @@ Result RenderTestApp::writeBindingOutput(const String& fileName) commandBuffer->close(); m_queue->executeCommandBuffer(commandBuffer); + m_transientHeap->finish(); m_transientHeap->synchronizeAndReset(); m_device->readBufferResource(stagingBuffer, 0, bufferSize, blob.writeRef()); -- cgit v1.2.3