From ebc3aa118ce3601096fad3301d4ab1f1be87460b Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 11 Apr 2020 00:00:15 +1000 Subject: [PATCH] SDL: Fix DXGI intercepting ALT+ENTER --- src/duckstation-sdl/d3d11_host_display.cpp | 56 ++++++++++++++-------- src/duckstation-sdl/d3d11_host_display.h | 3 +- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/duckstation-sdl/d3d11_host_display.cpp b/src/duckstation-sdl/d3d11_host_display.cpp index a1765389d..8a61216d7 100644 --- a/src/duckstation-sdl/d3d11_host_display.cpp +++ b/src/duckstation-sdl/d3d11_host_display.cpp @@ -200,17 +200,46 @@ bool D3D11HostDisplay::CreateD3DDevice(bool debug_device) return false; } - ComPtr dxgi_factory; - HRESULT hr = CreateDXGIFactory(IID_PPV_ARGS(dxgi_factory.GetAddressOf())); + UINT create_flags = 0; + if (debug_device) + create_flags |= D3D11_CREATE_DEVICE_DEBUG; + + HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, create_flags, nullptr, 0, + D3D11_SDK_VERSION, m_device.GetAddressOf(), nullptr, m_context.GetAddressOf()); + if (FAILED(hr)) { - Log_ErrorPrintf("Failed to create DXGI factory: 0x%08X", hr); + Log_ErrorPrintf("Failed to create D3D device: 0x%08X", hr); return false; } + // we need the specific factory for the device, otherwise MakeWindowAssociation() is flaky. + ComPtr dxgi_device; + ComPtr dxgi_adapter; + if (FAILED(m_device.As(&dxgi_device)) || FAILED(dxgi_device->GetParent(IID_PPV_ARGS(dxgi_adapter.GetAddressOf()))) || + FAILED(dxgi_adapter->GetParent(IID_PPV_ARGS(m_dxgi_factory.GetAddressOf())))) + { + Log_WarningPrint("Failed to get parent adapter/device/factory"); + return false; + } + + DXGI_ADAPTER_DESC adapter_desc; + if (SUCCEEDED(dxgi_adapter->GetDesc(&adapter_desc))) + { + char adapter_name_buffer[128]; + const int name_length = + WideCharToMultiByte(CP_UTF8, 0, adapter_desc.Description, static_cast(std::wcslen(adapter_desc.Description)), + adapter_name_buffer, countof(adapter_name_buffer), 0, nullptr); + if (name_length >= 0) + { + adapter_name_buffer[name_length] = 0; + Log_InfoPrintf("D3D Adapter: %s", adapter_name_buffer); + } + } + m_allow_tearing_supported = false; ComPtr dxgi_factory5; - hr = dxgi_factory.As(&dxgi_factory5); + hr = m_dxgi_factory.As(&dxgi_factory5); if (SUCCEEDED(hr)) { BOOL allow_tearing_supported = false; @@ -220,19 +249,6 @@ bool D3D11HostDisplay::CreateD3DDevice(bool debug_device) m_allow_tearing_supported = (allow_tearing_supported == TRUE); } - UINT create_flags = 0; - if (debug_device) - create_flags |= D3D11_CREATE_DEVICE_DEBUG; - - hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, create_flags, nullptr, 0, D3D11_SDK_VERSION, - m_device.GetAddressOf(), nullptr, m_context.GetAddressOf()); - - if (FAILED(hr)) - { - Log_ErrorPrintf("Failed to create D3D device: 0x%08X", hr); - return false; - } - DXGI_SWAP_CHAIN_DESC swap_chain_desc = {}; swap_chain_desc.BufferDesc.Width = m_window_width; swap_chain_desc.BufferDesc.Height = m_window_height; @@ -247,7 +263,7 @@ bool D3D11HostDisplay::CreateD3DDevice(bool debug_device) if (m_allow_tearing_supported) swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; - hr = dxgi_factory->CreateSwapChain(m_device.Get(), &swap_chain_desc, m_swap_chain.GetAddressOf()); + hr = m_dxgi_factory->CreateSwapChain(m_device.Get(), &swap_chain_desc, m_swap_chain.GetAddressOf()); if (FAILED(hr)) { Log_WarningPrintf("Failed to create a flip-discard swap chain, trying discard."); @@ -255,7 +271,7 @@ bool D3D11HostDisplay::CreateD3DDevice(bool debug_device) swap_chain_desc.Flags = 0; m_allow_tearing_supported = false; - hr = dxgi_factory->CreateSwapChain(m_device.Get(), &swap_chain_desc, m_swap_chain.GetAddressOf()); + hr = m_dxgi_factory->CreateSwapChain(m_device.Get(), &swap_chain_desc, m_swap_chain.GetAddressOf()); if (FAILED(hr)) { Log_ErrorPrintf("CreateSwapChain failed: 0x%08X", hr); @@ -263,7 +279,7 @@ bool D3D11HostDisplay::CreateD3DDevice(bool debug_device) } } - hr = dxgi_factory->MakeWindowAssociation(swap_chain_desc.OutputWindow, DXGI_MWA_NO_WINDOW_CHANGES); + hr = m_dxgi_factory->MakeWindowAssociation(swap_chain_desc.OutputWindow, DXGI_MWA_NO_WINDOW_CHANGES); if (FAILED(hr)) Log_WarningPrintf("MakeWindowAssociation() to disable ALT+ENTER failed"); diff --git a/src/duckstation-sdl/d3d11_host_display.h b/src/duckstation-sdl/d3d11_host_display.h index 820b19b87..7f39d9c40 100644 --- a/src/duckstation-sdl/d3d11_host_display.h +++ b/src/duckstation-sdl/d3d11_host_display.h @@ -49,7 +49,8 @@ private: void RenderDisplay(); SDL_Window* m_window = nullptr; - SDL_GLContext m_gl_context = nullptr; + + ComPtr m_dxgi_factory; ComPtr m_device; ComPtr m_context;