From 6e6f2859ab8874825452ee852f1b1c6923aead09 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 26 Mar 2022 16:48:02 +0200 Subject: [PATCH] Fix switching to fullscreen mode in D3D11/D3D12 flip presentation modes --- .../DirectX/DX11/GPUDeviceDX11.cpp | 19 ++++++++----------- .../DirectX/DX11/GPUSwapChainDX11.cpp | 12 +++++++++++- .../DirectX/DX12/GPUSwapChainDX12.cpp | 12 +++++++++++- .../Engine/Platform/Windows/WindowsWindow.cpp | 10 ++++++++++ 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp index 35d2253a2..aadac4abf 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp @@ -265,18 +265,15 @@ bool GPUDeviceDX11::Init() return true; } UpdateOutputs(adapter); + + ComPtr factory5; + _factoryDXGI->QueryInterface(IID_PPV_ARGS(&factory5)); + if (factory5) { - ComPtr factory5; - _factoryDXGI->QueryInterface(IID_PPV_ARGS(&factory5)); - if (factory5) - { - BOOL allowTearing; - if (SUCCEEDED(factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allowTearing, sizeof(allowTearing))) && allowTearing) - { - AllowTearing = true; - } - factory5->Release(); - } + BOOL allowTearing; + if (SUCCEEDED(factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allowTearing, sizeof(allowTearing))) && allowTearing) + AllowTearing = true; + factory5->Release(); } diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUSwapChainDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUSwapChainDX11.cpp index 79e76a2a5..c7b884c59 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUSwapChainDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUSwapChainDX11.cpp @@ -112,6 +112,16 @@ void GPUSwapChainDX11::SetFullscreen(bool isFullscreen) } _isFullscreen = isFullscreen; + + // Buffers must be resized in flip presentation model + if (swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL || + swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_DISCARD) + { + const int32 width = _width; + const int32 height = _height; + _width = _height = 0; + Resize(width, height); + } } #else LOG(Info, "Cannot change fullscreen mode on this platform"); @@ -218,7 +228,7 @@ bool GPUSwapChainDX11::Resize(int32 width, int32 height) ASSERT(_swapChain); // Disable DXGI changes to the window - VALIDATE_DIRECTX_RESULT(dxgi->MakeWindowAssociation(_windowHandle, 0)); + VALIDATE_DIRECTX_RESULT(dxgi->MakeWindowAssociation(_windowHandle, DXGI_MWA_NO_ALT_ENTER)); #else auto dxgiFactory = (IDXGIFactory2*)_device->GetDXGIFactory(); VALIDATE_DIRECTX_RESULT(dxgiFactory->CreateSwapChainForCoreWindow(_device->GetDevice(), static_cast(_windowHandle), &swapChainDesc, nullptr, &_swapChain)); diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUSwapChainDX12.cpp b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUSwapChainDX12.cpp index 18b1af930..e7e976987 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUSwapChainDX12.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUSwapChainDX12.cpp @@ -137,6 +137,16 @@ void GPUSwapChainDX12::SetFullscreen(bool isFullscreen) } _isFullscreen = isFullscreen; + + // Buffers must be resized in flip presentation model + if (swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL || + swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_DISCARD) + { + const int32 width = _width; + const int32 height = _height; + _width = _height = 0; + Resize(width, height); + } } #else LOG(Info, "Cannot change fullscreen mode on this platform"); @@ -219,7 +229,7 @@ bool GPUSwapChainDX12::Resize(int32 width, int32 height) _backBuffers.Resize(swapChainDesc.BufferCount); // Disable DXGI changes to the window - dxgiFactory->MakeWindowAssociation(_windowHandle, 0); + VALIDATE_DIRECTX_RESULT(dxgiFactory->MakeWindowAssociation(_windowHandle, DXGI_MWA_NO_ALT_ENTER)); } else { diff --git a/Source/Engine/Platform/Windows/WindowsWindow.cpp b/Source/Engine/Platform/Windows/WindowsWindow.cpp index 50f092407..cb1617219 100644 --- a/Source/Engine/Platform/Windows/WindowsWindow.cpp +++ b/Source/Engine/Platform/Windows/WindowsWindow.cpp @@ -1031,6 +1031,10 @@ LRESULT WindowsWindow::WndProc(UINT msg, WPARAM wParam, LPARAM lParam) // In this case, we don't resize yet -- we wait until the user stops dragging, and a WM_EXITSIZEMOVE message comes. UpdateRegion(); } + else if (_isSwitchingFullScreen) + { + // Ignored + } else { // This WM_SIZE come from resizing the window via an API like SetWindowPos() so resize @@ -1090,6 +1094,12 @@ LRESULT WindowsWindow::WndProc(UINT msg, WPARAM wParam, LPARAM lParam) Close(ClosingReason::User); return 0; } + if (wParam == VK_RETURN) + { + LOG(Info, "Alt+Enter pressed"); + SetIsFullscreen(!IsFullscreen()); + return 0; + } break; case WM_POWERBROADCAST: switch (wParam)