From 9ff40a6835a7d518edccb193fc8fa35aefd453a6 Mon Sep 17 00:00:00 2001 From: Maxwell Geng <33229754+MaxwellGengYF@users.noreply.github.com> Date: Thu, 19 Dec 2024 12:26:26 +0800 Subject: [PATCH] fallback dx swapchain interface --- src/backends/dx/DXApi/LCCmdBuffer.cpp | 4 +++- src/backends/dx/DXApi/LCSwapChain.cpp | 13 +++++++------ src/backends/dx/DXApi/LCSwapChain.h | 5 +++-- src/backends/dx/DXApi/ext.cpp | 2 +- src/backends/dx/DXRuntime/CommandAllocator.cpp | 2 +- src/backends/dx/DXRuntime/CommandAllocator.h | 2 +- src/backends/dx/DXRuntime/CommandQueue.cpp | 2 +- src/backends/dx/DXRuntime/CommandQueue.h | 2 +- src/ext/imgui | 2 +- 9 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/backends/dx/DXApi/LCCmdBuffer.cpp b/src/backends/dx/DXApi/LCCmdBuffer.cpp index b863c0cee..5263c9459 100644 --- a/src/backends/dx/DXApi/LCCmdBuffer.cpp +++ b/src/backends/dx/DXApi/LCCmdBuffer.cpp @@ -1123,8 +1123,10 @@ void LCCmdBuffer::Present( { std::lock_guard lck{mtx}; tracker.listType = alloc->Type(); - swapchain->frameIndex = swapchain->swapChain->GetCurrentBackBufferIndex(); + // swapchain->frameIndex = swapchain->swapChain->GetCurrentBackBufferIndex(); auto &&rt = &swapchain->m_renderTargets[swapchain->frameIndex]; + swapchain->frameIndex += 1; + swapchain->frameIndex %= swapchain->frameCount; auto cb = alloc->GetBuffer(); auto bd = cb->Build(); auto cmdList = cb->CmdList(); diff --git a/src/backends/dx/DXApi/LCSwapChain.cpp b/src/backends/dx/DXApi/LCSwapChain.cpp index 1a47fd6e2..262f05a6c 100644 --- a/src/backends/dx/DXApi/LCSwapChain.cpp +++ b/src/backends/dx/DXApi/LCSwapChain.cpp @@ -15,7 +15,7 @@ LCSwapChain::LCSwapChain( uint backBufferCount) : Resource(device), vsync(vsync) { this->format = format; - auto frameCount = backBufferCount + 1; + frameCount = backBufferCount + 1; vstd::push_back_func( m_renderTargets, frameCount, @@ -40,19 +40,19 @@ LCSwapChain::LCSwapChain( nullptr, nullptr, &localSwap)); - - swapChain = DxPtr(static_cast(localSwap), true); + + swapChain = DxPtr(localSwap, true); } for (uint32_t n = 0; n < frameCount; n++) { ThrowIfFailed(swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n].rt))); } - if (!vsync) - swapChain->SetMaximumFrameLatency(backBufferCount * 2); + // if (!vsync) + // swapChain->SetMaximumFrameLatency(backBufferCount * 2); } LCSwapChain::LCSwapChain( PixelStorage &storage, Device *device, - IDXGISwapChain4 *swapChain, + IDXGISwapChain1 *swapChain, bool vsync) : Resource(device), swapChain(swapChain, false), @@ -63,6 +63,7 @@ LCSwapChain::LCSwapChain( m_renderTargets, swapChainDesc.BufferCount, [&] { return device; }); + frameCount = swapChainDesc.BufferCount; for (uint32_t n = 0; n < swapChainDesc.BufferCount; n++) { ThrowIfFailed(swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n].rt))); } diff --git a/src/backends/dx/DXApi/LCSwapChain.h b/src/backends/dx/DXApi/LCSwapChain.h index 1896264ff..1b684c79a 100644 --- a/src/backends/dx/DXApi/LCSwapChain.h +++ b/src/backends/dx/DXApi/LCSwapChain.h @@ -9,8 +9,9 @@ namespace lc::dx { class LCSwapChain : public Resource { public: vstd::vector m_renderTargets; - DxPtr swapChain; + DxPtr swapChain; uint64 frameIndex = 0; + uint64 frameCount = 0; DXGI_FORMAT format; bool vsync; Tag GetTag() const override { return Tag::SwapChain; } @@ -27,7 +28,7 @@ class LCSwapChain : public Resource { LCSwapChain( PixelStorage& storage, Device* device, - IDXGISwapChain4* swapChain, + IDXGISwapChain1* swapChain, bool vsync); }; }// namespace lc::dx diff --git a/src/backends/dx/DXApi/ext.cpp b/src/backends/dx/DXApi/ext.cpp index c58b983dc..c072077c7 100644 --- a/src/backends/dx/DXApi/ext.cpp +++ b/src/backends/dx/DXApi/ext.cpp @@ -149,7 +149,7 @@ SwapchainCreationInfo DxNativeResourceExt::register_external_swapchain( auto res = new LCSwapChain( info.storage, dx_device, - reinterpret_cast(swapchain_ptr), + reinterpret_cast(swapchain_ptr), vsync); info.handle = reinterpret_cast(res); info.native_handle = swapchain_ptr; diff --git a/src/backends/dx/DXRuntime/CommandAllocator.cpp b/src/backends/dx/DXRuntime/CommandAllocator.cpp index e9f8f2759..9375c7373 100644 --- a/src/backends/dx/DXRuntime/CommandAllocator.cpp +++ b/src/backends/dx/DXRuntime/CommandAllocator.cpp @@ -82,7 +82,7 @@ void CommandAllocator::Execute( } } } -void CommandAllocator::ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain1 *swapchain, bool vsync) { +void CommandAllocator::ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain *swapchain, bool vsync) { auto present = [&]() { if (vsync) { ThrowIfFailed(swapchain->Present(1, 0)); diff --git a/src/backends/dx/DXRuntime/CommandAllocator.h b/src/backends/dx/DXRuntime/CommandAllocator.h index 68d58ee69..9788e1a8c 100644 --- a/src/backends/dx/DXRuntime/CommandAllocator.h +++ b/src/backends/dx/DXRuntime/CommandAllocator.h @@ -81,7 +81,7 @@ class CommandAllocator final : public vstd::IOperatorNewBase { ~CommandAllocator(); CommandBuffer *GetBuffer() const; void Execute(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex); - void ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain1 *swapchain, bool vsync); + void ExecuteAndPresent(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex, IDXGISwapChain *swapchain, bool vsync); void Complete(CommandQueue *queue, ID3D12Fence *fence, uint64 fenceIndex); DefaultBuffer const *AllocateScratchBuffer(size_t targetSize); BufferView GetTempReadbackBuffer(uint64 size, size_t align = 0); diff --git a/src/backends/dx/DXRuntime/CommandQueue.cpp b/src/backends/dx/DXRuntime/CommandQueue.cpp index adaa1c9ed..fc998f4af 100644 --- a/src/backends/dx/DXRuntime/CommandQueue.cpp +++ b/src/backends/dx/DXRuntime/CommandQueue.cpp @@ -187,7 +187,7 @@ void CommandQueue::ExecuteEmptyCallbacks(AllocatorPtr &&alloc, vstd::vectorExecuteAndPresent(this, cmdFence.Get(), curFrame, swapChain, vsync); mtx.lock(); diff --git a/src/backends/dx/DXRuntime/CommandQueue.h b/src/backends/dx/DXRuntime/CommandQueue.h index e2de32473..d4f8c6cb1 100644 --- a/src/backends/dx/DXRuntime/CommandQueue.h +++ b/src/backends/dx/DXRuntime/CommandQueue.h @@ -61,7 +61,7 @@ class CommandQueue : vstd::IOperatorNewBase { void ExecuteCallbacks(AllocatorPtr &&alloc, vstd::vector> &&callbacks); void ExecuteEmpty(AllocatorPtr &&alloc); void ExecuteEmptyCallbacks(AllocatorPtr &&alloc, vstd::vector> &&callbacks); - void ExecuteAndPresent(AllocatorPtr &&alloc, IDXGISwapChain1 *swapChain, bool vsync); + void ExecuteAndPresent(AllocatorPtr &&alloc, IDXGISwapChain *swapChain, bool vsync); void Complete(uint64 fence); void Complete(); void ForceSync( diff --git a/src/ext/imgui b/src/ext/imgui index 947aa9c97..edef72d49 160000 --- a/src/ext/imgui +++ b/src/ext/imgui @@ -1 +1 @@ -Subproject commit 947aa9c9722bd6ff740dd757da609ff41f4d3ba3 +Subproject commit edef72d49783ca8907f1343593b3a865feda74a2