diff --git a/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.cpp b/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.cpp index 6ee11af7..e785bbf0 100644 --- a/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.cpp +++ b/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.cpp @@ -42,6 +42,8 @@ void D3D12WindowRenderer::Shutdown() { m_viewportTextureAllocator.Shutdown(); m_presenter.Shutdown(); m_hostDevice.Shutdown(); + m_activeFrameSlot = 0u; + m_nextFrameSlot = 0u; m_activeBackBufferIndex = 0u; m_lastError.clear(); } @@ -52,6 +54,8 @@ bool D3D12WindowRenderer::Resize(int width, int height) { return false; } + m_activeFrameSlot = 0u; + m_nextFrameSlot = 0u; m_activeBackBufferIndex = m_presenter.GetCurrentBackBufferIndex(); m_lastError = m_presenter.GetLastError(); if (!m_lastError.empty()) { @@ -68,11 +72,14 @@ bool D3D12WindowRenderer::BeginFrame() { } m_activeBackBufferIndex = m_presenter.GetCurrentBackBufferIndex(); - if (!m_hostDevice.BeginFrame(m_activeBackBufferIndex)) { + const std::uint32_t frameSlot = m_nextFrameSlot; + if (!m_hostDevice.BeginFrame(frameSlot)) { m_lastError = m_hostDevice.GetLastError(); return false; } + m_activeFrameSlot = frameSlot; + m_nextFrameSlot = (frameSlot + 1u) % kFrameContextCount; m_lastError.clear(); return true; } @@ -95,7 +102,7 @@ bool D3D12WindowRenderer::SubmitFrame(bool presentSwapChain) { return false; } - if (!m_hostDevice.SubmitFrame(m_activeBackBufferIndex)) { + if (!m_hostDevice.SubmitFrame(m_activeFrameSlot)) { m_lastError = m_hostDevice.GetLastError(); return false; } @@ -111,7 +118,7 @@ bool D3D12WindowRenderer::SubmitFrame(bool presentSwapChain) { } bool D3D12WindowRenderer::SignalFrameCompletion() { - if (!m_hostDevice.SignalFrameCompletion(m_activeBackBufferIndex)) { + if (!m_hostDevice.SignalFrameCompletion(m_activeFrameSlot)) { m_lastError = m_hostDevice.GetLastError(); return false; } @@ -213,7 +220,7 @@ std::uint32_t D3D12WindowRenderer::GetBackBufferCount() const { } ::XCEngine::Rendering::RenderContext D3D12WindowRenderer::GetRenderContext() const { - return m_hostDevice.GetRenderContext(m_activeBackBufferIndex); + return m_hostDevice.GetRenderContext(m_activeFrameSlot); } } // namespace XCEngine::UI::Editor::Host diff --git a/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.h b/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.h index ad9f8ab2..6fc7997a 100644 --- a/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.h +++ b/new_editor/app/Rendering/D3D12/D3D12WindowRenderer.h @@ -23,6 +23,8 @@ namespace XCEngine::UI::Editor::Host { class D3D12WindowRenderer : public Ports::ViewportRenderPort { public: + static constexpr std::uint32_t kFrameContextCount = + D3D12HostDevice::kFrameContextCount; static constexpr std::uint32_t kSwapChainBufferCount = D3D12WindowSwapChainPresenter::kSwapChainBufferCount; @@ -57,6 +59,8 @@ private: D3D12HostDevice m_hostDevice = {}; D3D12WindowSwapChainPresenter m_presenter = {}; D3D12ShaderResourceDescriptorAllocator m_viewportTextureAllocator = {}; + std::uint32_t m_activeFrameSlot = 0u; + std::uint32_t m_nextFrameSlot = 0u; std::uint32_t m_activeBackBufferIndex = 0u; std::unordered_map m_viewportTextureCpuHandles = {}; std::string m_lastError = {}; diff --git a/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.cpp b/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.cpp index c07a73ff..cad56538 100644 --- a/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.cpp +++ b/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.cpp @@ -89,7 +89,7 @@ void D3D12WindowSwapChainPresenter::ConfigureFrameLatency() { return; } - nativeSwapChain->SetMaximumFrameLatency(1u); + nativeSwapChain->SetMaximumFrameLatency(kSwapChainBufferCount); } void D3D12WindowSwapChainPresenter::DestroySwapChain() { diff --git a/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.h b/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.h index 49ced76c..e1588f7e 100644 --- a/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.h +++ b/new_editor/app/Rendering/D3D12/D3D12WindowSwapChainPresenter.h @@ -23,7 +23,7 @@ namespace XCEngine::UI::Editor::Host { class D3D12WindowSwapChainPresenter { public: - static constexpr std::uint32_t kSwapChainBufferCount = 2u; + static constexpr std::uint32_t kSwapChainBufferCount = D3D12HostDevice::kFrameContextCount; bool Initialize(D3D12HostDevice& hostDevice, HWND hwnd, int width, int height); void Shutdown();