diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-03-31 18:55:55 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-03-31 18:55:55 +0000 |
| commit | f88358621a3a2ffa1a9b7a87baa84695075337bd (patch) | |
| tree | bb555200a0d97d473744faf83e63834c742d0286 /Graphics/GraphicsEngineD3DBase | |
| parent | Implemented initialization in fullscreen mode on Win32 (diff) | |
| download | DiligentCore-f88358621a3a2ffa1a9b7a87baa84695075337bd.tar.gz DiligentCore-f88358621a3a2ffa1a9b7a87baa84695075337bd.zip | |
Implemented switching to a fullscreen mode in d3d on Win32
Diffstat (limited to 'Graphics/GraphicsEngineD3DBase')
| -rw-r--r-- | Graphics/GraphicsEngineD3DBase/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Graphics/GraphicsEngineD3DBase/include/D3DErrors.h | 18 | ||||
| -rw-r--r-- | Graphics/GraphicsEngineD3DBase/include/SwapChainD3DBase.h | 186 |
3 files changed, 201 insertions, 4 deletions
diff --git a/Graphics/GraphicsEngineD3DBase/CMakeLists.txt b/Graphics/GraphicsEngineD3DBase/CMakeLists.txt index cb683b09..7b85ad3a 100644 --- a/Graphics/GraphicsEngineD3DBase/CMakeLists.txt +++ b/Graphics/GraphicsEngineD3DBase/CMakeLists.txt @@ -14,6 +14,7 @@ set(INCLUDE include/ShaderD3DBase.h include/ShaderResources.h include/ShaderVariableD3DBase.h + include/SwapChainD3DBase.h ) set(SOURCE diff --git a/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h b/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h index 0a9233e4..b1d1e068 100644 --- a/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h +++ b/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h @@ -67,17 +67,17 @@ private: #define CHECK_D3D_RESULT_THROW(Expr, Message)\ -{ \ +do{ \ HRESULT _hr_ = Expr; \ if(FAILED(_hr_)) \ { \ ComErrorDesc ErrDesc( _hr_ ); \ LOG_ERROR_AND_THROW( Message, "\nHRESULT Desc: ", ErrDesc.Get());\ } \ -} +}while(false) #define CHECK_D3D_RESULT_THROW_EX(Expr, ...)\ -{ \ +do{ \ HRESULT _hr_ = Expr; \ if(FAILED(_hr_)) \ { \ @@ -86,4 +86,14 @@ private: ComErrorDesc ErrDesc( _hr_ ); \ LOG_ERROR_AND_THROW( ms.str(), "\nHRESULT Desc: ", ErrDesc.Get());\ } \ -} +}while(false) + +#define LOG_D3D_ERROR(Expr, Message)\ +do{ \ + HRESULT _hr_ = Expr; \ + if(FAILED(_hr_)) \ + { \ + ComErrorDesc ErrDesc( _hr_ ); \ + LOG_ERROR_MESSAGE( Message, "\nHRESULT Desc: ", ErrDesc.Get());\ + } \ +}while(false) diff --git a/Graphics/GraphicsEngineD3DBase/include/SwapChainD3DBase.h b/Graphics/GraphicsEngineD3DBase/include/SwapChainD3DBase.h new file mode 100644 index 00000000..0dc8bb0d --- /dev/null +++ b/Graphics/GraphicsEngineD3DBase/include/SwapChainD3DBase.h @@ -0,0 +1,186 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +#include "SwapChainBase.h" + +/// \file +/// Base implementation of a D3D swap chain + +namespace Diligent +{ + /// Base implementation of a D3D swap chain + template<class BaseInterface, typename DXGISwapChainType> + class SwapChainD3DBase : public SwapChainBase<BaseInterface> + { + public: + using TBase = SwapChainBase<BaseInterface>; + SwapChainD3DBase(IReferenceCounters *pRefCounters, + IRenderDevice *pDevice, + IDeviceContext *pDeviceContext, + const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, + void* pNativeWndHandle) : + TBase(pRefCounters, pDevice, pDeviceContext, SCDesc), + m_FSDesc(FSDesc), + m_pNativeWndHandle(pNativeWndHandle) + {} + + ~SwapChainD3DBase() + { + // 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); + } + } + + protected: + virtual void UpdateSwapChain(bool CreateNew) = 0; + + void CreateDXGISwapChain(IUnknown *pD3D11DeviceOrD3D12CmdQueue) + { +#if PLATFORM_WIN32 + auto hWnd = reinterpret_cast<HWND>(m_pNativeWndHandle); + if (m_SwapChainDesc.Width == 0 || m_SwapChainDesc.Height == 0) + { + RECT rc; + if (m_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; + } +#endif + + auto DXGIColorBuffFmt = TexFormatToDXGI_Format(m_SwapChainDesc.ColorBufferFormat); + + DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {}; + swapChainDesc.Width = m_SwapChainDesc.Width; + swapChainDesc.Height = m_SwapChainDesc.Height; + // Flip model swapchains (DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL and DXGI_SWAP_EFFECT_FLIP_DISCARD) only support the following Formats: + // - DXGI_FORMAT_R16G16B16A16_FLOAT + // - DXGI_FORMAT_B8G8R8A8_UNORM + // - DXGI_FORMAT_R8G8B8A8_UNORM + // - DXGI_FORMAT_R10G10B10A2_UNORM + // If RGBA8_UNORM_SRGB swap chain is required, we will create RGBA8_UNORM swap chain, but + // create RGBA8_UNORM_SRGB render target view + swapChainDesc.Format = DXGIColorBuffFmt == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB ? DXGI_FORMAT_R8G8B8A8_UNORM : DXGIColorBuffFmt; + swapChainDesc.Stereo = FALSE; + swapChainDesc.SampleDesc.Count = 1; + swapChainDesc.SampleDesc.Quality = 0; + swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + swapChainDesc.BufferCount = m_SwapChainDesc.BufferCount; + swapChainDesc.Scaling = DXGI_SCALING_NONE; + // 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 = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; + + CComPtr<IDXGISwapChain1> pSwapChain1; + CComPtr<IDXGIFactory2> factory; + HRESULT hr = CreateDXGIFactory1(__uuidof(factory), reinterpret_cast<void**>(static_cast<IDXGIFactory2**>(&factory))); + CHECK_D3D_RESULT_THROW(hr, "Failed to create DXGI factory"); + +#if PLATFORM_WIN32 + + DXGI_SWAP_CHAIN_FULLSCREEN_DESC FullScreenDesc = {}; + FullScreenDesc.Windowed = m_FSDesc.Fullscreen ? FALSE : TRUE; + FullScreenDesc.RefreshRate.Numerator = m_FSDesc.RefreshRateNumerator; + FullScreenDesc.RefreshRate.Denominator = m_FSDesc.RefreshRateDenominator; + FullScreenDesc.Scaling = static_cast<DXGI_MODE_SCALING>(m_FSDesc.Scaling); + FullScreenDesc.ScanlineOrdering = static_cast<DXGI_MODE_SCANLINE_ORDER>(m_FSDesc.ScanlineOrder); + hr = factory->CreateSwapChainForHwnd(pD3D11DeviceOrD3D12CmdQueue, hWnd, &swapChainDesc, &FullScreenDesc, nullptr, &pSwapChain1); + CHECK_D3D_RESULT_THROW(hr, "Failed to create Swap Chain"); + + // Do not allow the swap chain handle Alt+Enter + hr = factory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_ALT_ENTER); + +#elif PLATFORM_UNIVERSAL_WINDOWS + + if (m_FSDesc.Fullscreen) + LOG_WARNING_MESSAGE("UWP applications do not support fullscreen mode"); + + hr = factory->CreateSwapChainForCoreWindow( + pD3D11DeviceOrD3D12CmdQueue, + reinterpret_cast<IUnknown*>(m_pNativeWndHandle), + &swapChainDesc, + nullptr, + &pSwapChain1); + CHECK_D3D_RESULT_THROW(hr, "Failed to create DXGI swap chain"); + + // Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and + // ensures that the application will only render after each VSync, minimizing power consumption. + //pDXGIDevice->SetMaximumFrameLatency( 1 ); + +#endif + + pSwapChain1->QueryInterface(__uuidof(m_pSwapChain), reinterpret_cast<void**>(static_cast<DXGISwapChainType**>(&m_pSwapChain))); + + } + + virtual void SetFullscreenMode(const DisplayModeAttribs &DisplayMode)override final + { + if (m_pSwapChain) + { + // If we are already in fullscreen mode, we need to switch to windowed mode first, + // otherwise swap chain creation will fail + if (m_FSDesc.Fullscreen) + m_pSwapChain->SetFullscreenState(FALSE, nullptr); + m_FSDesc.Fullscreen = True; + m_FSDesc.RefreshRateNumerator = DisplayMode.RefreshRateNumerator; + m_FSDesc.RefreshRateDenominator = DisplayMode.RefreshRateDenominator; + m_FSDesc.Scaling = DisplayMode.Scaling; + m_FSDesc.ScanlineOrder = DisplayMode.ScanlineOrder; + m_SwapChainDesc.Width = DisplayMode.Width; + m_SwapChainDesc.Height = DisplayMode.Height; + if (DisplayMode.Format != TEX_FORMAT_UNKNOWN) + m_SwapChainDesc.ColorBufferFormat = DisplayMode.Format; + UpdateSwapChain(true); + } + } + + virtual void SetWindowedMode()override final + { + if (m_FSDesc.Fullscreen) + { + m_FSDesc.Fullscreen = False; + m_pSwapChain->SetFullscreenState(FALSE, nullptr); + } + } + + FullScreenModeDesc m_FSDesc; + CComPtr<DXGISwapChainType> m_pSwapChain; + void* m_pNativeWndHandle; + }; +} |
