From c32095993f3e5734b3b6a112aeef0a9536db8f7d Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 19 Dec 2019 10:11:08 -0800 Subject: Added ClearRenderTarget test; fixed issue with bound render targets in D3D12 and Vk backends --- .../GraphicsEngine/include/DeviceContextBase.h | 80 +++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) (limited to 'Graphics/GraphicsEngine') diff --git a/Graphics/GraphicsEngine/include/DeviceContextBase.h b/Graphics/GraphicsEngine/include/DeviceContextBase.h index c822dacf..d357c4f6 100644 --- a/Graphics/GraphicsEngine/include/DeviceContextBase.h +++ b/Graphics/GraphicsEngine/include/DeviceContextBase.h @@ -190,7 +190,7 @@ public: /// Returns the render device IRenderDevice* GetDevice() { return m_pDevice; } - inline void ResetRenderTargets(); + virtual void ResetRenderTargets(); bool IsDeferred() const { return m_bIsDeferred; } @@ -204,6 +204,17 @@ protected: /// Clears all cached resources inline void ClearStateCache(); + /// Checks if the texture is currently bound as a render target. + bool CheckIfBoundAsRenderTarget(TextureImplType* pTexture); + + /// Checks if the texture is currently bound as depth-stencil buffer. + bool CheckIfBoundAsDepthStencil(TextureImplType* pTexture); + + /// Checks if the texture is bound as a render target or depth-stencil buffer and + /// resets render targets if it is. + bool UnbindTextureFromFramebuffer(TextureImplType* pTexture, bool bShowMessage); + + #ifdef DEVELOPMENT // clang-format off bool DvpVerifyDrawArguments (const DrawAttribs& Attribs)const; @@ -722,7 +733,72 @@ inline void DeviceContextBase::ClearStateCa } template -inline void DeviceContextBase::ResetRenderTargets() +bool DeviceContextBase::CheckIfBoundAsRenderTarget(TextureImplType* pTexture) +{ + if (pTexture == nullptr) + return false; + + for (Uint32 rt = 0; rt < m_NumBoundRenderTargets; ++rt) + { + if (m_pBoundRenderTargets[rt] && m_pBoundRenderTargets[rt]->GetTexture() == pTexture) + { + return true; + } + } + + return false; +} + +template +bool DeviceContextBase::CheckIfBoundAsDepthStencil(TextureImplType* pTexture) +{ + if (pTexture == nullptr) + return false; + + return m_pBoundDepthStencil && m_pBoundDepthStencil->GetTexture() == pTexture; +} + +template +bool DeviceContextBase::UnbindTextureFromFramebuffer(TextureImplType* pTexture, bool bShowMessage) +{ + if (pTexture == nullptr) + return false; + + if (pTexture->IsInKnownState() && !pTexture->CheckState(RESOURCE_STATE_RENDER_TARGET) && !pTexture->CheckState(RESOURCE_STATE_DEPTH_WRITE)) + return false; + + bool bResetRenderTargets = false; + if (CheckIfBoundAsRenderTarget(pTexture)) + { + if (bShowMessage) + { + LOG_INFO_MESSAGE("Texture '", pTexture->GetDesc().Name, + "' is currently bound as render target and will be unset along with all " + "other render targets and depth-stencil buffer. " + "Call SetRenderTargets() to reset the render targets."); + } + + bResetRenderTargets = true; + } + else if (CheckIfBoundAsDepthStencil(pTexture)) + { + LOG_INFO_MESSAGE("Texture '", pTexture->GetDesc().Name, + "' is currently bound as depth buffer and will be unset along with " + "all render targets. Call SetRenderTargets() to reset the render targets."); + + bResetRenderTargets = true; + } + + if (bResetRenderTargets) + { + ResetRenderTargets(); + } + + return bResetRenderTargets; +} + +template +void DeviceContextBase::ResetRenderTargets() { for (Uint32 rt = 0; rt < m_NumBoundRenderTargets; ++rt) m_pBoundRenderTargets[rt].Release(); -- cgit v1.2.3