| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3DCommon/SwapChain.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstdint> |
| 8 | |
| 9 | #include "Common/Assert.h" |
| 10 | #include "Common/CommonFuncs.h" |
| 11 | #include "Common/HRWrap.h" |
| 12 | #include "Common/Logging/Log.h" |
| 13 | #include "Common/MsgHandler.h" |
| 14 | |
| 15 | #include "VideoCommon/VideoConfig.h" |
| 16 | |
| 17 | static bool IsTearingSupported(IDXGIFactory2* dxgi_factory) |
| 18 | { |
| 19 | Microsoft::WRL::ComPtr<IDXGIFactory5> factory5; |
| 20 | if (FAILED(dxgi_factory->QueryInterface(IID_PPV_ARGS(&factory5)))) |
| 21 | return false; |
| 22 | |
| 23 | UINT allow_tearing = 0; |
| 24 | return SUCCEEDED(factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allow_tearing, |
| 25 | sizeof(allow_tearing))) && |
| 26 | allow_tearing != 0; |
| 27 | } |
| 28 | |
| 29 | static bool GetFullscreenState(IDXGISwapChain* swap_chain) |
| 30 | { |
| 31 | BOOL fs = FALSE; |
| 32 | return SUCCEEDED(swap_chain->GetFullscreenState(&fs, nullptr)) && fs; |
| 33 | } |
| 34 | |
| 35 | namespace D3DCommon |
| 36 | { |
| 37 | SwapChain::SwapChain(const WindowSystemInfo& wsi, IDXGIFactory* dxgi_factory, IUnknown* d3d_device) |
| 38 | : m_wsi(wsi), m_dxgi_factory(dxgi_factory), m_d3d_device(d3d_device) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | SwapChain::~SwapChain() |
| 43 | { |
| 44 | // Can't destroy swap chain while it's fullscreen. |
| 45 | if (m_swap_chain && GetFullscreenState(m_swap_chain.Get())) |
| 46 | m_swap_chain->SetFullscreenState(FALSE, nullptr); |
| 47 | } |
| 48 | |
| 49 | bool SwapChain::WantsStereo() |
| 50 | { |
| 51 | return g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer; |
| 52 | } |
| 53 | |
| 54 | bool SwapChain::WantsHDR() |
| 55 | { |
| 56 | return g_ActiveConfig.bHDR; |
| 57 | } |
| 58 | |
| 59 | u32 SwapChain::GetSwapChainFlags() const |
| 60 | { |
| 61 | // This flag is necessary if we want to use a flip-model swapchain without locking the framerate |
| 62 | return m_allow_tearing_supported ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; |
| 63 | } |
| 64 | |
| 65 | bool SwapChain::CreateSwapChain(bool stereo, bool hdr) |
| 66 | { |
| 67 | RECT client_rc; |
| 68 | if (GetClientRect(static_cast<HWND>(m_wsi.render_surface), &client_rc)) |
| 69 | { |
| 70 | m_width = client_rc.right - client_rc.left; |
| 71 | m_height = client_rc.bottom - client_rc.top; |
| 72 | } |
| 73 | |
| 74 | m_stereo = false; |
| 75 | m_hdr = false; |
| 76 | |
| 77 | // Try using the Win8 version if available. |
| 78 | Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory2; |
| 79 | HRESULT hr = m_dxgi_factory.As(&dxgi_factory2); |
| 80 | if (SUCCEEDED(hr)) |
| 81 | { |
| 82 | m_allow_tearing_supported = IsTearingSupported(dxgi_factory2.Get()); |
| 83 | |
| 84 | DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {}; |
| 85 | swap_chain_desc.Width = m_width; |
| 86 | swap_chain_desc.Height = m_height; |
| 87 | swap_chain_desc.BufferCount = SWAP_CHAIN_BUFFER_COUNT; |
| 88 | swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 89 | swap_chain_desc.SampleDesc.Count = 1; |
| 90 | swap_chain_desc.SampleDesc.Quality = 0; |
| 91 | swap_chain_desc.Format = GetDXGIFormatForAbstractFormat(m_texture_format, false); |
| 92 | |
| 93 | swap_chain_desc.Scaling = DXGI_SCALING_STRETCH; |
| 94 | swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; |
| 95 | swap_chain_desc.Stereo = stereo; |
| 96 | swap_chain_desc.Flags = GetSwapChainFlags(); |
| 97 | |
| 98 | Microsoft::WRL::ComPtr<IDXGISwapChain1> swap_chain1; |
| 99 | hr = dxgi_factory2->CreateSwapChainForHwnd(m_d3d_device.Get(), |
| 100 | static_cast<HWND>(m_wsi.render_surface), |
| 101 | &swap_chain_desc, nullptr, nullptr, &swap_chain1); |
| 102 | if (FAILED(hr)) |
| 103 | { |
| 104 | // Flip-model discard swapchains aren't supported on Windows 8, so here we fall back to |
| 105 | // a sequential swapchain |
| 106 | swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; |
| 107 | hr = dxgi_factory2->CreateSwapChainForHwnd(m_d3d_device.Get(), |
| 108 | static_cast<HWND>(m_wsi.render_surface), |
| 109 | &swap_chain_desc, nullptr, nullptr, &swap_chain1); |
| 110 | } |
| 111 | |
| 112 | m_swap_chain = swap_chain1; |
| 113 | } |
| 114 | |
| 115 | // Flip-model swapchains aren't supported on Windows 7, so here we fall back to a legacy |
| 116 | // BitBlt-model swapchain. Note that this won't work for DX12, but systems which don't |
| 117 | // support the newer DXGI interface aren't going to support DX12 anyway. |
| 118 | if (FAILED(hr)) |
| 119 | { |
| 120 | hdr = false; |
| 121 | |
| 122 | DXGI_SWAP_CHAIN_DESC desc = {}; |
| 123 | desc.BufferDesc.Width = m_width; |
| 124 | desc.BufferDesc.Height = m_height; |
| 125 | desc.BufferDesc.Format = GetDXGIFormatForAbstractFormat(m_texture_format, false); |
| 126 | desc.SampleDesc.Count = 1; |
| 127 | desc.SampleDesc.Quality = 0; |
| 128 | desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 129 | desc.BufferCount = SWAP_CHAIN_BUFFER_COUNT; |
| 130 | desc.OutputWindow = static_cast<HWND>(m_wsi.render_surface); |
| 131 | desc.Windowed = TRUE; |
| 132 | desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; |
| 133 | desc.Flags = 0; |
| 134 | |
| 135 | m_allow_tearing_supported = false; |
| 136 | hr = m_dxgi_factory->CreateSwapChain(m_d3d_device.Get(), &desc, &m_swap_chain); |
| 137 | } |
| 138 | |
| 139 | if (FAILED(hr)) |
| 140 | { |
| 141 | PanicAlertFmt("Failed to create swap chain: {}", Common::HRWrap(hr)); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // We handle fullscreen ourselves. |
| 146 | hr = m_dxgi_factory->MakeWindowAssociation(static_cast<HWND>(m_wsi.render_surface), |
| 147 | DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_ALT_ENTER); |
| 148 | if (FAILED(hr)) |
| 149 | WARN_LOG_FMT(VIDEO, "MakeWindowAssociation() failed: {}", Common::HRWrap(hr)); |
| 150 | |
| 151 | m_stereo = stereo; |
| 152 | |
| 153 | if (hdr) |
| 154 | { |
| 155 | // Only try to activate HDR here, to avoid failing when creating the swapchain |
| 156 | // (we can't know if the format is supported upfront) |
| 157 | Microsoft::WRL::ComPtr<IDXGISwapChain4> swap_chain4; |
| 158 | hr = m_swap_chain.As(&swap_chain4); |
| 159 | if (SUCCEEDED(hr)) |
| 160 | { |
| 161 | UINT color_space_support = 0; |
| 162 | // Note that this should succeed even if HDR is not currently engaged on the monitor, |
| 163 | // but it should display fine nonetheless. |
| 164 | // We need to check for DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020 as checking for |
| 165 | // scRGB always returns false (DX bug). |
| 166 | hr = swap_chain4->CheckColorSpaceSupport(DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020, |
| 167 | &color_space_support); |
| 168 | if (SUCCEEDED(hr) && (color_space_support & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) |
| 169 | { |
| 170 | hr = swap_chain4->ResizeBuffers(SWAP_CHAIN_BUFFER_COUNT, 0, 0, |
| 171 | GetDXGIFormatForAbstractFormat(m_texture_format_hdr, false), |
| 172 | GetSwapChainFlags()); |
| 173 | if (SUCCEEDED(hr)) |
| 174 | { |
| 175 | hr = swap_chain4->SetColorSpace1(DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709); |
| 176 | if (SUCCEEDED(hr)) |
| 177 | m_hdr = hdr; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (!CreateSwapChainBuffers()) |
| 184 | { |
| 185 | PanicAlertFmt("Failed to create swap chain buffers"); |
| 186 | DestroySwapChainBuffers(); |
| 187 | m_swap_chain.Reset(); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | void SwapChain::DestroySwapChain() |
| 195 | { |
| 196 | DestroySwapChainBuffers(); |
| 197 | |
| 198 | // Can't destroy swap chain while it's fullscreen. |
| 199 | if (m_swap_chain && GetFullscreenState(m_swap_chain.Get())) |
| 200 | m_swap_chain->SetFullscreenState(FALSE, nullptr); |
| 201 | |
| 202 | m_swap_chain.Reset(); |
| 203 | } |
| 204 | |
| 205 | bool SwapChain::ResizeSwapChain() |
| 206 | { |
| 207 | DestroySwapChainBuffers(); |
| 208 | |
| 209 | // The swap chain fills up the size of the window if no size is specified |
| 210 | HRESULT hr = m_swap_chain->ResizeBuffers(SWAP_CHAIN_BUFFER_COUNT, 0, 0, DXGI_FORMAT_UNKNOWN, |
| 211 | GetSwapChainFlags()); |
| 212 | |
| 213 | if (FAILED(hr)) |
| 214 | WARN_LOG_FMT(VIDEO, "ResizeBuffers() failed: {}", Common::HRWrap(hr)); |
| 215 | |
| 216 | Microsoft::WRL::ComPtr<IDXGISwapChain4> swap_chain4; |
| 217 | hr = m_swap_chain.As(&swap_chain4); |
| 218 | if (SUCCEEDED(hr)) |
| 219 | hr = swap_chain4->SetColorSpace1(m_hdr ? DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709 : |
| 220 | DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709); |
| 221 | |
| 222 | DXGI_SWAP_CHAIN_DESC desc; |
| 223 | if (SUCCEEDED(m_swap_chain->GetDesc(&desc))) |
| 224 | { |
| 225 | m_width = desc.BufferDesc.Width; |
| 226 | m_height = desc.BufferDesc.Height; |
| 227 | } |
| 228 | |
| 229 | return CreateSwapChainBuffers(); |
| 230 | } |
| 231 | |
| 232 | void SwapChain::SetStereo(bool stereo) |
| 233 | { |
| 234 | if (m_stereo == stereo) |
| 235 | return; |
| 236 | |
| 237 | DestroySwapChain(); |
| 238 | // Do not try to re-activate HDR here if it had already failed |
| 239 | if (!CreateSwapChain(stereo, m_hdr)) |
| 240 | { |
| 241 | PanicAlertFmt("Failed to switch swap chain stereo mode"); |
| 242 | CreateSwapChain(false, false); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void SwapChain::SetHDR(bool hdr) |
| 247 | { |
| 248 | if (m_hdr == hdr) |
| 249 | return; |
| 250 | |
| 251 | // NOTE: as an optimization here we could just call "ResizeSwapChain()" |
| 252 | // by adding some code to check if we could change the format to HDR. |
| 253 | |
| 254 | DestroySwapChain(); |
| 255 | // Do not try to re-activate stereo mode here if it had already failed |
| 256 | if (!CreateSwapChain(m_stereo, hdr)) |
| 257 | { |
| 258 | PanicAlertFmt("Failed to switch swap chain SDR/HDR mode"); |
| 259 | CreateSwapChain(false, false); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | bool SwapChain::GetFullscreen() const |
| 264 | { |
| 265 | return GetFullscreenState(m_swap_chain.Get()); |
| 266 | } |
| 267 | |
| 268 | void SwapChain::SetFullscreen(bool request) |
| 269 | { |
| 270 | m_swap_chain->SetFullscreenState(request, nullptr); |
| 271 | } |
| 272 | |
| 273 | bool SwapChain::CheckForFullscreenChange() |
| 274 | { |
| 275 | if (m_fullscreen_request != m_has_fullscreen) |
| 276 | { |
| 277 | HRESULT hr = m_swap_chain->SetFullscreenState(m_fullscreen_request, nullptr); |
| 278 | if (SUCCEEDED(hr)) |
| 279 | { |
| 280 | m_has_fullscreen = m_fullscreen_request; |
| 281 | return true; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | const bool new_fullscreen_state = GetFullscreenState(m_swap_chain.Get()); |
| 286 | if (new_fullscreen_state != m_has_fullscreen) |
| 287 | { |
| 288 | m_has_fullscreen = new_fullscreen_state; |
| 289 | m_fullscreen_request = new_fullscreen_state; |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | bool SwapChain::Present() |
| 297 | { |
| 298 | // When using sync interval 0, it is recommended to always pass the tearing flag when it is |
| 299 | // supported, even when presenting in windowed mode. However, this flag cannot be used if the app |
| 300 | // is in fullscreen mode as a result of calling SetFullscreenState. |
| 301 | UINT present_flags = 0; |
| 302 | if (m_allow_tearing_supported && !g_ActiveConfig.bVSyncActive && !m_has_fullscreen) |
| 303 | present_flags |= DXGI_PRESENT_ALLOW_TEARING; |
| 304 | |
| 305 | HRESULT hr = m_swap_chain->Present(static_cast<UINT>(g_ActiveConfig.bVSyncActive), present_flags); |
| 306 | if (FAILED(hr)) |
| 307 | { |
| 308 | WARN_LOG_FMT(VIDEO, "Swap chain present failed: {}", Common::HRWrap(hr)); |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | bool SwapChain::ChangeSurface(void* native_handle) |
| 316 | { |
| 317 | DestroySwapChain(); |
| 318 | m_wsi.render_surface = native_handle; |
| 319 | // We only keep the swap chain settings (HDR/Stereo) that had successfully applied before |
| 320 | return CreateSwapChain(m_stereo, m_hdr); |
| 321 | } |
| 322 | |
| 323 | } // namespace D3DCommon |