From 5677bdafc48e6231a67c01e5d894ed657aa0ecf6 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Fri, 30 Mar 2018 09:52:13 -0700 Subject: Implemented initialization in fullscreen mode on Win32 --- .../include/SwapChainD3D11Impl.h | 3 +- .../interface/RenderDeviceFactoryD3D11.h | 1 + .../src/RenderDeviceFactoryD3D11.cpp | 13 ++++++-- .../GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp | 37 +++++++++++++++++++--- 4 files changed, 45 insertions(+), 9 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h index e9bd6899..2289f20d 100644 --- a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h @@ -39,7 +39,8 @@ class SwapChainD3D11Impl : public SwapChainBase public: typedef SwapChainBase TSwapChainBase; SwapChainD3D11Impl(IReferenceCounters *pRefCounters, - const SwapChainDesc& SwapChainDesc, + const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, class RenderDeviceD3D11Impl* pRenderDeviceD3D11, class DeviceContextD3D11Impl* pDeviceContextD3D11, void* pNativeWndHandle); diff --git a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h b/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h index 84154472..69029ce9 100644 --- a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h +++ b/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h @@ -52,6 +52,7 @@ public: virtual void CreateSwapChainD3D11( IRenderDevice *pDevice, IDeviceContext *pImmediateContext, const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, void* pNativeWndHandle, ISwapChain **ppSwapChain ) = 0; diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp index fc33ddb7..3084b881 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp @@ -56,6 +56,7 @@ public: void CreateSwapChainD3D11( IRenderDevice *pDevice, IDeviceContext *pImmediateContext, const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, void* pNativeWndHandle, ISwapChain **ppSwapChain )override final; @@ -166,7 +167,7 @@ void EngineFactoryD3D11Impl::CreateDeviceAndContextsD3D11( const EngineD3D11Attr D3D_FEATURE_LEVEL d3dFeatureLevel = D3D_FEATURE_LEVEL_11_0; HRESULT hr = D3D11CreateDevice( hardwareAdapter, // Specify nullptr to use the default adapter. - hardwareAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, // Create a device using the hardware graphics driver. + hardwareAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, // If no adapter specified, request hardware graphics driver. 0, // Should be 0 unless the driver is D3D_DRIVER_TYPE_SOFTWARE. creationFlags, // Set debug and Direct2D compatibility flags. featureLevels, // List of feature levels this app can support. @@ -289,6 +290,7 @@ void EngineFactoryD3D11Impl::AttachToD3D11Device(void *pd3d11NativeDevice, /// \param [in] pDevice - Pointer to the render device /// \param [in] pImmediateContext - Pointer to the immediate device context /// \param [in] SCDesc - Swap chain description +/// \param [in] FSDesc - Fullscreen mode description /// \param [in] pNativeWndHandle - Platform-specific native handle of the window /// the swap chain will be associated with: /// * On Win32 platform, this should be window handle (HWND) @@ -297,7 +299,12 @@ void EngineFactoryD3D11Impl::AttachToD3D11Device(void *pd3d11NativeDevice, /// /// \param [out] ppSwapChain - Address of the memory location where pointer to the new /// swap chain will be written -void EngineFactoryD3D11Impl::CreateSwapChainD3D11( IRenderDevice *pDevice, Diligent::IDeviceContext *pImmediateContext, const SwapChainDesc& SCDesc, void* pNativeWndHandle, ISwapChain **ppSwapChain ) +void EngineFactoryD3D11Impl::CreateSwapChainD3D11( IRenderDevice *pDevice, + IDeviceContext *pImmediateContext, + const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, + void* pNativeWndHandle, + ISwapChain **ppSwapChain ) { VERIFY( ppSwapChain, "Null pointer provided" ); if( !ppSwapChain ) @@ -312,7 +319,7 @@ void EngineFactoryD3D11Impl::CreateSwapChainD3D11( IRenderDevice *pDevice, Dilig auto &RawMemAllocator = GetRawAllocator(); auto *pSwapChainD3D11 = NEW_RC_OBJ(RawMemAllocator, "SwapChainD3D11Impl instance", SwapChainD3D11Impl) - (SCDesc, pDeviceD3D11, pDeviceContextD3D11, pNativeWndHandle); + (SCDesc, FSDesc, pDeviceD3D11, pDeviceContextD3D11, pNativeWndHandle); pSwapChainD3D11->QueryInterface( IID_SwapChain, reinterpret_cast(ppSwapChain) ); pDeviceContextD3D11->SetSwapChain(pSwapChainD3D11); diff --git a/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp index 01f4623d..34bb54d0 100644 --- a/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp @@ -34,6 +34,7 @@ namespace Diligent SwapChainD3D11Impl::SwapChainD3D11Impl(IReferenceCounters *pRefCounters, const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, RenderDeviceD3D11Impl* pRenderDeviceD3D11, DeviceContextD3D11Impl* pDeviceContextD3D11, void* pNativeWndHandle) : @@ -46,7 +47,15 @@ SwapChainD3D11Impl::SwapChainD3D11Impl(IReferenceCounters *pRefCounters, if( m_SwapChainDesc.Width == 0 || m_SwapChainDesc.Height == 0 ) { RECT rc; - GetClientRect( hWnd, &rc ); + if(FSDesc.Fullscreen) + { + const HWND hDesktop = GetDesktopWindow(); + GetWindowRect(hDesktop, &rc); + } + else + { + GetClientRect(hWnd, &rc); + } m_SwapChainDesc.Width = rc.right - rc.left; m_SwapChainDesc.Height = rc.bottom - rc.top; } @@ -73,13 +82,15 @@ SwapChainD3D11Impl::SwapChainD3D11Impl(IReferenceCounters *pRefCounters, swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.BufferCount = m_SwapChainDesc.BufferCount; swapChainDesc.Scaling = DXGI_SCALING_NONE; - swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; + // Windows Store apps must use DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL or DXGI_SWAP_EFFECT_FLIP_DISCARD. + swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED; // Not used - swapChainDesc.Flags = 0; + swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; CComPtr pSwapChain1; #if PLATFORM_WIN32 + // This sequence obtains the DXGI factory that was used to create the Direct3D device above. CComPtr pDXGIDevice; pDevice->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast( static_cast(&pDXGIDevice) ) ); @@ -88,11 +99,20 @@ SwapChainD3D11Impl::SwapChainD3D11Impl(IReferenceCounters *pRefCounters, CComPtr pDXGIFactory; pDXGIAdapter->GetParent(__uuidof(pDXGIFactory), reinterpret_cast( static_cast(&pDXGIFactory) )); - CHECK_D3D_RESULT_THROW( pDXGIFactory->CreateSwapChainForHwnd(pDevice, hWnd, &swapChainDesc, nullptr, nullptr, &pSwapChain1), + DXGI_SWAP_CHAIN_FULLSCREEN_DESC FullScreenDesc = {}; + FullScreenDesc.Windowed = FSDesc.Fullscreen ? FALSE : TRUE; + FullScreenDesc.RefreshRate.Numerator = FSDesc.RefreshRateNumerator; + FullScreenDesc.RefreshRate.Denominator = FSDesc.RefreshRateDenominator; + FullScreenDesc.Scaling = static_cast(FSDesc.Scaling); + FullScreenDesc.ScanlineOrdering = static_cast(FSDesc.ScanlineOrder); + CHECK_D3D_RESULT_THROW( pDXGIFactory->CreateSwapChainForHwnd(pDevice, hWnd, &swapChainDesc, &FullScreenDesc, nullptr, &pSwapChain1), "Failed to create DXGI swap chain" ); #elif PLATFORM_UNIVERSAL_WINDOWS + if (FSDesc.Fullscreen) + LOG_WARNING_MESSAGE("UWP applications do not support fullscreen mode"); + CComPtr pDXGIDevice; pDevice->QueryInterface(__uuidof(IDXGIDevice3), reinterpret_cast(static_cast(&pDXGIDevice))); CComPtr pDXGIAdapter; @@ -121,7 +141,14 @@ SwapChainD3D11Impl::SwapChainD3D11Impl(IReferenceCounters *pRefCounters, SwapChainD3D11Impl::~SwapChainD3D11Impl() { - + // Swap chain must be in windowed mode when terminating the app + if(m_pSwapChain) + { + BOOL IsFullScreen = FALSE; + m_pSwapChain->GetFullscreenState(&IsFullScreen, nullptr); + if(IsFullScreen) + m_pSwapChain->SetFullscreenState(FALSE, nullptr); + } } void SwapChainD3D11Impl::CreateRTVandDSV() -- cgit v1.2.3