From 35c318c44920ed670cd975a7848019b5d0b6eef3 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 6 Oct 2018 22:45:34 -0700 Subject: Reworked D3D11 swap chain to properly return current RTV and DSV through GetCurrentBackBufferRTV() and GetDepthBufferDSV() --- .../include/SwapChainD3D11Impl.h | 13 ++--- .../GraphicsEngineD3D11/interface/SwapChainD3D11.h | 18 +++---- .../src/DeviceContextD3D11Impl.cpp | 57 ++++++++++------------ .../GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp | 57 +++++++++++----------- .../GraphicsEngineD3D11/src/Texture1D_D3D11.cpp | 36 +++++++++----- .../GraphicsEngineD3D11/src/Texture2D_D3D11.cpp | 33 ++++++++----- .../GraphicsEngineD3D11/src/Texture3D_D3D11.cpp | 33 ++++++++----- 7 files changed, 133 insertions(+), 114 deletions(-) (limited to 'Graphics') diff --git a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h index f72128cc..3551b5d3 100644 --- a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h @@ -54,20 +54,15 @@ public: virtual IDXGISwapChain* GetDXGISwapChain()override final{ return m_pSwapChain; } - virtual ID3D11RenderTargetView* GetRTV()override final{ return m_pRenderTargetView; } - virtual ID3D11DepthStencilView* GetDSV()override final{ return m_pDepthStencilView; } - - virtual ITextureView* GetCurrentBackBufferRTV()override final{return nullptr;} - virtual ITextureView* GetDepthBufferDSV()override final{return nullptr;} + virtual ITextureViewD3D11* GetCurrentBackBufferRTV()override final{return m_pRenderTargetView;} + virtual ITextureViewD3D11* GetDepthBufferDSV() override final{return m_pDepthStencilView;} private: virtual void UpdateSwapChain(bool CreateNew)override final; void CreateRTVandDSV(); - /// Back buffer render target view - CComPtr m_pRenderTargetView; - /// Back buffer depth-stencil view - CComPtr m_pDepthStencilView; + RefCntAutoPtr m_pRenderTargetView; + RefCntAutoPtr m_pDepthStencilView; }; } diff --git a/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h b/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h index f6ee357e..07ea799d 100644 --- a/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h +++ b/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h @@ -27,6 +27,7 @@ /// Definition of the Diligent::ISwapChainD3D11 interface #include "../../GraphicsEngine/interface/SwapChain.h" +#include "TextureViewD3D11.h" namespace Diligent { @@ -39,24 +40,17 @@ static constexpr INTERFACE_ID IID_SwapChainD3D11 = class ISwapChainD3D11 : public ISwapChain { public: + /// Returns render target view of the back buffer in the swap chain + virtual ITextureViewD3D11* GetCurrentBackBufferRTV() = 0; + + /// Returns depth-stencil view of the depth buffer + virtual ITextureViewD3D11* GetDepthBufferDSV() = 0; /// Returns a pointer to the IDXGISwapChain interface of the internal DXGI object. /// The method does *NOT* call AddRef() on the returned interface, /// so Release() must not be called. virtual IDXGISwapChain* GetDXGISwapChain() = 0; - - /// Returns d3d11 render target view of the swap chain's back buffer - - /// The method does *NOT* call AddRef() on the returned interface, - /// so Release() must not be called. - virtual ID3D11RenderTargetView* GetRTV() = 0; - - /// Returns d3d11 depth stencil view of the internal depth buffer object - - /// The method does *NOT* call AddRef() on the returned interface, - /// so Release() must not be called. - virtual ID3D11DepthStencilView* GetDSV() = 0; }; } diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 369b632e..d848c5b0 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -845,22 +845,11 @@ namespace Diligent void DeviceContextD3D11Impl::ClearDepthStencil( ITextureView* pView, Uint32 ClearFlags, float fDepth, Uint8 Stencil ) { - ID3D11DepthStencilView* pd3d11DSV = nullptr; - if( pView != nullptr ) - { -#ifdef _DEBUG - const auto& ViewDesc = pView->GetDesc(); - VERIFY( ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL, "Incorrect view type: depth stencil is expected" ); -#endif - auto* pViewD3D11 = ValidatedCast(pView); - pd3d11DSV = static_cast(pViewD3D11->GetD3D11View()); - } - else + if (pView == nullptr) { if (m_pSwapChain) { - pd3d11DSV = m_pSwapChain.RawPtr()->GetDSV(); - VERIFY_EXPR(pd3d11DSV != nullptr); + pView = m_pSwapChain->GetDepthBufferDSV(); } else { @@ -868,6 +857,14 @@ namespace Diligent return; } } + +#ifdef DEVELOPMENT + const auto& ViewDesc = pView->GetDesc(); + VERIFY( ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL, "Incorrect view type: depth stencil is expected" ); +#endif + auto* pViewD3D11 = ValidatedCast(pView); + auto* pd3d11DSV = static_cast(pViewD3D11->GetD3D11View()); + UINT32 d3d11ClearFlags = 0; if( ClearFlags & CLEAR_DEPTH_FLAG ) d3d11ClearFlags |= D3D11_CLEAR_DEPTH; if( ClearFlags & CLEAR_STENCIL_FLAG ) d3d11ClearFlags |= D3D11_CLEAR_STENCIL; @@ -878,22 +875,11 @@ namespace Diligent void DeviceContextD3D11Impl::ClearRenderTarget( ITextureView* pView, const float *RGBA ) { - ID3D11RenderTargetView* pd3d11RTV = nullptr; - if( pView != nullptr ) + if (pView == nullptr) { -#ifdef _DEBUG - const auto& ViewDesc = pView->GetDesc(); - VERIFY( ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET, "Incorrect view type: render target is expected" ); -#endif - auto* pViewD3D11 = ValidatedCast(pView); - pd3d11RTV = static_cast(pViewD3D11->GetD3D11View()); - } - else - { - if (m_pSwapChain) + if (m_pSwapChain != nullptr) { - pd3d11RTV = m_pSwapChain.RawPtr()->GetRTV(); - VERIFY_EXPR(pd3d11RTV != nullptr); + pView = m_pSwapChain->GetCurrentBackBufferRTV(); } else { @@ -902,6 +888,13 @@ namespace Diligent } } +#ifdef DEVELOPMENT + const auto& ViewDesc = pView->GetDesc(); + VERIFY( ViewDesc.ViewType == TEXTURE_VIEW_RENDER_TARGET, "Incorrect view type: render target is expected" ); +#endif + auto* pViewD3D11 = ValidatedCast(pView); + auto* pd3d11RTV = static_cast(pViewD3D11->GetD3D11View()); + static const float Zero[4] = { 0.f, 0.f, 0.f, 0.f }; if( RGBA == nullptr ) RGBA = Zero; @@ -990,8 +983,10 @@ namespace Diligent { NumRenderTargets = 1; auto* pSwapChainD3D11 = m_pSwapChain.RawPtr(); - pd3d11RTs[0] = pSwapChainD3D11->GetRTV(); - pd3d11DSV = pSwapChainD3D11->GetDSV(); + auto* pBackBufferViewD3D11 = pSwapChainD3D11->GetCurrentBackBufferRTV(); + pd3d11RTs[0] = static_cast(pBackBufferViewD3D11->GetD3D11View()); + auto* pDepthBufferViewD3D11 = pSwapChainD3D11->GetDepthBufferDSV(); + pd3d11DSV = static_cast(pDepthBufferViewD3D11->GetD3D11View()); VERIFY_EXPR(pd3d11RTs[0] != nullptr && pd3d11DSV != nullptr); } else @@ -1007,7 +1002,7 @@ namespace Diligent auto* pView = m_pBoundRenderTargets[rt].RawPtr(); if( pView ) { - auto* pViewD3D11 = static_cast(pView); + auto* pViewD3D11 = ValidatedCast(pView); pd3d11RTs[rt] = static_cast(pViewD3D11->GetD3D11View()); } else @@ -1017,7 +1012,7 @@ namespace Diligent auto* pDepthStencil = m_pBoundDepthStencil.RawPtr(); if( pDepthStencil != nullptr ) { - auto* pViewD3D11 = static_cast(pDepthStencil); + auto* pViewD3D11 = ValidatedCast(pDepthStencil); pd3d11DSV = static_cast(pViewD3D11->GetD3D11View()); } } diff --git a/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp index cdc8e744..1bb2267d 100644 --- a/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp @@ -51,46 +51,47 @@ SwapChainD3D11Impl::~SwapChainD3D11Impl() void SwapChainD3D11Impl::CreateRTVandDSV() { - auto *pDevice = m_pRenderDevice.RawPtr()->GetD3D11Device(); - + auto* pRenderDeviceD3D11Impl = m_pRenderDevice.RawPtr(); + m_pRenderTargetView.Release(); m_pDepthStencilView.Release(); // Create a render target view - CComPtr pBackBuffer; - CHECK_D3D_RESULT_THROW( m_pSwapChain->GetBuffer( 0, __uuidof(ID3D11Texture2D), reinterpret_cast( static_cast(&pBackBuffer) ) ), + CComPtr pd3dBackBuffer; + CHECK_D3D_RESULT_THROW( m_pSwapChain->GetBuffer( 0, __uuidof(ID3D11Texture2D), reinterpret_cast( static_cast(&pd3dBackBuffer) ) ), "Failed to get back buffer from swap chain" ); - - D3D11_RENDER_TARGET_VIEW_DESC RTVDesc = {}; - RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; - // We need to explicitly specify RTV format, as we may need to create RGBA8_UNORM_SRGB RTV for - // a RGBA8_UNORM swap chain - RTVDesc.Format = TexFormatToDXGI_Format(m_SwapChainDesc.ColorBufferFormat); - RTVDesc.Texture2D.MipSlice = 0; - CHECK_D3D_RESULT_THROW( pDevice->CreateRenderTargetView( pBackBuffer, &RTVDesc, &m_pRenderTargetView ), - "Failed to get RTV for the back buffer" ); - + static const char BackBufferName[] = "Main back buffer"; + auto hr = pd3dBackBuffer->SetPrivateData(WKPDID_D3DDebugObjectName, _countof(BackBufferName)-1, BackBufferName); + VERIFY(SUCCEEDED(hr)); + + RefCntAutoPtr pBackBuffer; + pRenderDeviceD3D11Impl->CreateTextureFromD3DResource(pd3dBackBuffer, &pBackBuffer); + + TextureViewDesc RTVDesc; + RTVDesc.ViewType = TEXTURE_VIEW_RENDER_TARGET; + RTVDesc.Format = m_SwapChainDesc.ColorBufferFormat; + RefCntAutoPtr pRTV; + pBackBuffer->CreateView(RTVDesc, &pRTV); + m_pRenderTargetView = RefCntAutoPtr(pRTV, IID_TextureViewD3D11); + // Create depth buffer - D3D11_TEXTURE2D_DESC DepthBufferDesc; + TextureDesc DepthBufferDesc; + DepthBufferDesc.Name = "Main depth buffer"; + DepthBufferDesc.Type = RESOURCE_DIM_TEX_2D; DepthBufferDesc.Width = m_SwapChainDesc.Width; DepthBufferDesc.Height = m_SwapChainDesc.Height; DepthBufferDesc.MipLevels = 1; DepthBufferDesc.ArraySize = 1; - auto DepthFormat = TexFormatToDXGI_Format( m_SwapChainDesc.DepthBufferFormat ); - DepthBufferDesc.Format = DepthFormat; - DepthBufferDesc.SampleDesc.Count = m_SwapChainDesc.SamplesCount; - DepthBufferDesc.SampleDesc.Quality = 0; - DepthBufferDesc.Usage = D3D11_USAGE_DEFAULT; - DepthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; + DepthBufferDesc.Format = m_SwapChainDesc.DepthBufferFormat; + DepthBufferDesc.SampleCount = m_SwapChainDesc.SamplesCount; + DepthBufferDesc.Usage = USAGE_DEFAULT; + DepthBufferDesc.BindFlags = BIND_DEPTH_STENCIL; DepthBufferDesc.CPUAccessFlags = 0; DepthBufferDesc.MiscFlags = 0; - CComPtr ptex2DDepthBuffer; - CHECK_D3D_RESULT_THROW( pDevice->CreateTexture2D( &DepthBufferDesc, NULL, &ptex2DDepthBuffer ), - "Failed to create the depth buffer" ); - - // Create DSV - CHECK_D3D_RESULT_THROW( pDevice->CreateDepthStencilView( ptex2DDepthBuffer, NULL, &m_pDepthStencilView ), - "Failed to create the DSV for the depth buffer" ); + RefCntAutoPtr ptex2DDepthBuffer; + m_pRenderDevice->CreateTexture(DepthBufferDesc, TextureData{}, &ptex2DDepthBuffer); + auto pDSV = ptex2DDepthBuffer->GetDefaultView(TEXTURE_VIEW_DEPTH_STENCIL); + m_pDepthStencilView = RefCntAutoPtr(pDSV, IID_TextureViewD3D11); } IMPLEMENT_QUERY_INTERFACE( SwapChainD3D11Impl, IID_SwapChainD3D11, TSwapChainBase ) diff --git a/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp b/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp index 4d26ba9a..5e600ca9 100644 --- a/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp @@ -74,20 +74,32 @@ static TextureDesc TexDescFromD3D11Texture1D(ID3D11Texture1D *pd3d11Texture) { D3D11_TEXTURE1D_DESC D3D11TexDesc; pd3d11Texture->GetDesc(&D3D11TexDesc); - + TextureDesc TexDesc; - TexDesc.Name = "Texture1D_D3D11 from native d3d11 texture"; - TexDesc.Type = D3D11TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_1D_ARRAY : RESOURCE_DIM_TEX_1D; - TexDesc.Width = Uint32{D3D11TexDesc.Width}; - TexDesc.Height = 1; - TexDesc.ArraySize = Uint32{D3D11TexDesc.ArraySize}; - TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); - TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; - TexDesc.SampleCount = 1; - TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); - TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); + + UINT DataSize = 0; + pd3d11Texture->GetPrivateData(WKPDID_D3DDebugObjectName, &DataSize, nullptr); + std::vector ObjectName; + if (DataSize > 0) + { + ObjectName.resize(DataSize+1); // Null terminator is not reported in DataSize + pd3d11Texture->GetPrivateData(WKPDID_D3DDebugObjectName, &DataSize, ObjectName.data()); + TexDesc.Name = ObjectName.data(); + } + else + TexDesc.Name = "Texture1D_D3D11 from native d3d11 texture"; + + TexDesc.Type = D3D11TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_1D_ARRAY : RESOURCE_DIM_TEX_1D; + TexDesc.Width = Uint32{D3D11TexDesc.Width}; + TexDesc.Height = 1; + TexDesc.ArraySize = Uint32{D3D11TexDesc.ArraySize}; + TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); + TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; + TexDesc.SampleCount = 1; + TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); + TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); TexDesc.CPUAccessFlags = D3D11CPUAccessFlagsToCPUAccessFlags(D3D11TexDesc.CPUAccessFlags); - TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); + TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); return TexDesc; } diff --git a/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp b/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp index 1e338e84..dbe780d9 100644 --- a/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp @@ -81,18 +81,29 @@ static TextureDesc TexDescFromD3D11Texture2D(ID3D11Texture2D *pd3d11Texture) pd3d11Texture->GetDesc(&D3D11TexDesc); TextureDesc TexDesc; - TexDesc.Name = "Texture2D_D3D11 from native d3d11 texture"; - TexDesc.Type = D3D11TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_2D_ARRAY : RESOURCE_DIM_TEX_2D; - TexDesc.Width = Uint32{D3D11TexDesc.Width}; - TexDesc.Height = Uint32{D3D11TexDesc.Height}; - TexDesc.ArraySize = Uint32{D3D11TexDesc.ArraySize}; - TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); - TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; - TexDesc.SampleCount = Uint32{ D3D11TexDesc.SampleDesc.Count }; - TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); - TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); + UINT DataSize = 0; + pd3d11Texture->GetPrivateData(WKPDID_D3DDebugObjectName, &DataSize, nullptr); + std::vector ObjectName; + if (DataSize > 0) + { + ObjectName.resize(DataSize+1); // Null terminator is not reported in DataSize + pd3d11Texture->GetPrivateData(WKPDID_D3DDebugObjectName, &DataSize, ObjectName.data()); + TexDesc.Name = ObjectName.data(); + } + else + TexDesc.Name = "Texture2D_D3D11 from native d3d11 texture"; + + TexDesc.Type = D3D11TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_2D_ARRAY : RESOURCE_DIM_TEX_2D; + TexDesc.Width = Uint32{D3D11TexDesc.Width}; + TexDesc.Height = Uint32{D3D11TexDesc.Height}; + TexDesc.ArraySize = Uint32{D3D11TexDesc.ArraySize}; + TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); + TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; + TexDesc.SampleCount = Uint32{ D3D11TexDesc.SampleDesc.Count }; + TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); + TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); TexDesc.CPUAccessFlags = D3D11CPUAccessFlagsToCPUAccessFlags(D3D11TexDesc.CPUAccessFlags); - TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); + TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); return TexDesc; } diff --git a/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp b/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp index 960720dd..bf1e7a4a 100644 --- a/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp @@ -77,18 +77,29 @@ static TextureDesc TexDescFromD3D11Texture3D(ID3D11Texture3D* pd3d11Texture) pd3d11Texture->GetDesc(&D3D11TexDesc); TextureDesc TexDesc; - TexDesc.Name = "Texture3D_D3D11 from native d3d11 texture"; - TexDesc.Type = RESOURCE_DIM_TEX_3D; - TexDesc.Width = Uint32{D3D11TexDesc.Width}; - TexDesc.Height = Uint32{D3D11TexDesc.Height}; - TexDesc.Depth = Uint32{D3D11TexDesc.Depth}; - TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); - TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; - TexDesc.SampleCount = 1; - TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); - TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); + UINT DataSize = 0; + pd3d11Texture->GetPrivateData(WKPDID_D3DDebugObjectName, &DataSize, nullptr); + std::vector ObjectName; + if (DataSize > 0) + { + ObjectName.resize(DataSize+1); // Null terminator is not reported in + pd3d11Texture->GetPrivateData(WKPDID_D3DDebugObjectName, &DataSize, ObjectName.data()); + TexDesc.Name = ObjectName.data(); + } + else + TexDesc.Name = "Texture3D_D3D11 from native d3d11 texture"; + + TexDesc.Type = RESOURCE_DIM_TEX_3D; + TexDesc.Width = Uint32{D3D11TexDesc.Width}; + TexDesc.Height = Uint32{D3D11TexDesc.Height}; + TexDesc.Depth = Uint32{D3D11TexDesc.Depth}; + TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); + TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; + TexDesc.SampleCount = 1; + TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); + TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); TexDesc.CPUAccessFlags = D3D11CPUAccessFlagsToCPUAccessFlags(D3D11TexDesc.CPUAccessFlags); - TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); + TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); return TexDesc; } -- cgit v1.2.3