Backends: DX12: Fixed OBJECT_DELETED_WHILE_STILL_IN_USE on viewport resizing. (#3210)

Tested with detaching/attaching a viewport and resizing it. DX12 debug
layer is clean.
This commit is contained in:
Ivan Zinkevich 2020-05-07 23:50:51 +03:00 committed by omar
parent 8596f2b0fc
commit 1e7672acf4

View File

@ -846,22 +846,26 @@ static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport)
} }
} }
static void ImGui_WaitForPendingOperations(ImGuiViewportDataDx12* data)
{
HRESULT hr = S_FALSE;
if (data && data->CommandQueue && data->Fence && data->FenceEvent)
{
hr = data->CommandQueue->Signal(data->Fence, ++data->FenceSignaledValue);
IM_ASSERT(hr == S_OK);
::WaitForSingleObject(data->FenceEvent, 0); // Reset any forgotten waits
hr = data->Fence->SetEventOnCompletion(data->FenceSignaledValue, data->FenceEvent);
IM_ASSERT(hr == S_OK);
::WaitForSingleObject(data->FenceEvent, INFINITE);
}
}
static void ImGui_ImplDX12_DestroyWindow(ImGuiViewport* viewport) static void ImGui_ImplDX12_DestroyWindow(ImGuiViewport* viewport)
{ {
// The main viewport (owned by the application) will always have RendererUserData == NULL since we didn't create the data for it. // The main viewport (owned by the application) will always have RendererUserData == NULL since we didn't create the data for it.
if (ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData) if (ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData)
{ {
// Wait for pending operations to complete to safely release objects below ImGui_WaitForPendingOperations(data);
HRESULT hr;
if (data->CommandQueue && data->Fence && data->FenceEvent)
{
hr = data->CommandQueue->Signal(data->Fence, ++data->FenceSignaledValue);
IM_ASSERT(hr == S_OK);
::WaitForSingleObject(data->FenceEvent, 0); // Reset any forgotten waits
hr = data->Fence->SetEventOnCompletion(data->FenceSignaledValue, data->FenceEvent);
IM_ASSERT(hr == S_OK);
::WaitForSingleObject(data->FenceEvent, INFINITE);
}
SafeRelease(data->CommandQueue); SafeRelease(data->CommandQueue);
SafeRelease(data->CommandList); SafeRelease(data->CommandList);
@ -887,6 +891,8 @@ static void ImGui_ImplDX12_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
{ {
ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData; ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData;
ImGui_WaitForPendingOperations(data);
for (UINT i = 0; i < g_numFramesInFlight; i++) for (UINT i = 0; i < g_numFramesInFlight; i++)
SafeRelease(data->FrameCtx[i].RenderTarget); SafeRelease(data->FrameCtx[i].RenderTarget);