From 923bd2fd217c1dc1e75fa92b0284d3817904988b Mon Sep 17 00:00:00 2001 From: David Maas Date: Wed, 21 Jul 2021 16:01:52 -0500 Subject: [PATCH] Examples: DirectX12: Fixed Alt+Enter fullscreen in DirectX12 example. (#4346, #4348) This also removes unnecessary recreation of backend-owned device objects when the window is resized. + amend original PR with a g_pSwapChain->SetFullscreenState(false, NULL); call. --- docs/CHANGELOG.txt | 2 ++ examples/example_win32_directx12/main.cpp | 33 +++-------------------- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 052664f6..5d681c77 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -96,6 +96,8 @@ Other Changes: - Backends: OpenGL3: Use OES_vertex_array extension on Emscripten + backup/restore current state. (#4266, #4267) [@harry75369] - Backends: GLFW: Installing and exposed ImGui_ImplGlfw_MonitorCallback() for forward compatibility with docking branch. - Backends: OSX: Added a fix for shortcuts using CTRL key instead of CMD key. (#4253) [@rokups] +- Examples: DX12: Fixed handling of Alt+Enter in example app (using swapchain's ResizeBuffers). (#4346) [@PathogenDavid] +- Examples: DX12: Removed unecessary recreation of backend-owned device objects when window is resized. (#4347) [@PathogenDavid] - Examples: OSX+OpenGL2: Fix event forwarding (fix key remaining stuck when using shortcuts with Cmd/Super key). Other OSX examples were not affected. (#4253, #1873) [@rokups] - Examples: Updated all .vcxproj to VS2015 (toolset v140) to facilitate usage with vcpkg. diff --git a/examples/example_win32_directx12/main.cpp b/examples/example_win32_directx12/main.cpp index 8476fe7d..74bfef32 100644 --- a/examples/example_win32_directx12/main.cpp +++ b/examples/example_win32_directx12/main.cpp @@ -54,7 +54,6 @@ void CreateRenderTarget(); void CleanupRenderTarget(); void WaitForLastSubmittedFrame(); FrameContext* WaitForNextFrameResources(); -void ResizeSwapChain(HWND hWnd, int width, int height); LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); // Main code @@ -352,7 +351,7 @@ bool CreateDeviceD3D(HWND hWnd) void CleanupDeviceD3D() { CleanupRenderTarget(); - if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; } + if (g_pSwapChain) { g_pSwapChain->SetFullscreenState(false, NULL); g_pSwapChain->Release(); g_pSwapChain = NULL; } if (g_hSwapChainWaitableObject != NULL) { CloseHandle(g_hSwapChainWaitableObject); } for (UINT i = 0; i < NUM_FRAMES_IN_FLIGHT; i++) if (g_frameContext[i].CommandAllocator) { g_frameContext[i].CommandAllocator->Release(); g_frameContext[i].CommandAllocator = NULL; } @@ -432,31 +431,6 @@ FrameContext* WaitForNextFrameResources() return frameCtx; } -void ResizeSwapChain(HWND hWnd, int width, int height) -{ - DXGI_SWAP_CHAIN_DESC1 sd; - g_pSwapChain->GetDesc1(&sd); - sd.Width = width; - sd.Height = height; - - IDXGIFactory4* dxgiFactory = NULL; - g_pSwapChain->GetParent(IID_PPV_ARGS(&dxgiFactory)); - - g_pSwapChain->Release(); - CloseHandle(g_hSwapChainWaitableObject); - - IDXGISwapChain1* swapChain1 = NULL; - dxgiFactory->CreateSwapChainForHwnd(g_pd3dCommandQueue, hWnd, &sd, NULL, NULL, &swapChain1); - swapChain1->QueryInterface(IID_PPV_ARGS(&g_pSwapChain)); - swapChain1->Release(); - dxgiFactory->Release(); - - g_pSwapChain->SetMaximumFrameLatency(NUM_BACK_BUFFERS); - - g_hSwapChainWaitableObject = g_pSwapChain->GetFrameLatencyWaitableObject(); - assert(g_hSwapChainWaitableObject != NULL); -} - // Forward declare message handler from imgui_impl_win32.cpp extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); @@ -472,11 +446,10 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED) { WaitForLastSubmittedFrame(); - ImGui_ImplDX12_InvalidateDeviceObjects(); CleanupRenderTarget(); - ResizeSwapChain(hWnd, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam)); + HRESULT result = g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT); + assert(SUCCEEDED(result) && "Failed to resize swapchain."); CreateRenderTarget(); - ImGui_ImplDX12_CreateDeviceObjects(); } return 0; case WM_SYSCOMMAND: