SwapChain.cpp 10 KB

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