diff --git a/editor/src/Application.cpp b/editor/src/Application.cpp index cdc04b63..18beaeea 100644 --- a/editor/src/Application.cpp +++ b/editor/src/Application.cpp @@ -96,6 +96,10 @@ void Application::ShutdownEditorContext() { void Application::RenderEditorFrame() { static constexpr float kClearColor[4] = { 0.22f, 0.22f, 0.22f, 1.0f }; + if (!m_windowRenderer.BeginFrame()) { + return; + } + m_imguiBackend.BeginFrame(); m_viewportHostService.BeginFrame(); m_layerStack.onImGuiRender(); diff --git a/editor/src/Platform/D3D12WindowRenderer.h b/editor/src/Platform/D3D12WindowRenderer.h index 3ce7b935..04176cfa 100644 --- a/editor/src/Platform/D3D12WindowRenderer.h +++ b/editor/src/Platform/D3D12WindowRenderer.h @@ -155,6 +155,23 @@ public: RecreateBackBufferViews(); } + bool BeginFrame() { + auto* d3d12Queue = GetD3D12CommandQueue(); + auto* d3d12CommandList = GetD3D12CommandList(); + if (m_swapChain == nullptr || + d3d12Queue == nullptr || + d3d12CommandList == nullptr || + m_srvHeap == nullptr) { + return false; + } + + // Viewport panels can resize and recreate render targets while building ImGui. + // Make sure the previous frame is fully retired before any CPU-side resource destruction. + d3d12Queue->WaitForPreviousFrame(); + m_commandList->Reset(); + return true; + } + void Render( UI::ImGuiBackendBridge& imguiBackend, const float clearColor[4], @@ -168,9 +185,6 @@ public: return; } - d3d12Queue->WaitForPreviousFrame(); - m_commandList->Reset(); - if (beforeUiRender) { beforeUiRender(GetRenderContext()); }