From 7d6429107db783fc382972ed2ae28cad6e589eab Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 10 Mar 2018 10:07:56 -0800 Subject: Fixed performance issues with switching PSO in D3D12 backend --- .../src/DeviceContextD3D12Impl.cpp | 59 +++++++++++++--------- 1 file changed, 34 insertions(+), 25 deletions(-) (limited to 'Graphics/GraphicsEngineD3D12') diff --git a/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp index 38adf957..6c9a41be 100644 --- a/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp @@ -122,6 +122,11 @@ namespace Diligent Flush(true); } + // If no pipeline state is bound, we are working with the fresh command + // list. We have to commit the states set in the context that are not + // committed by the draw command (render targets, viewports, scissor rects, etc.) + bool CommitStates = !m_pPipelineState; + TDeviceContextBase::SetPipelineState( pPipelineState ); auto *pPipelineStateD3D12 = ValidatedCast(pPipelineState); @@ -137,35 +142,33 @@ namespace Diligent auto &GraphicsCtx = pCmdCtx->AsGraphicsContext(); GraphicsCtx.SetPipelineState(pd3d12PSO); - if (Desc.GraphicsPipeline.RasterizerDesc.ScissorEnable) + if (CommitStates) { - // Commit currently set scissor rectangles - D3D12_RECT d3d12ScissorRects[MaxD3D12ScissorRects]; // Do not waste time initializing array with zeroes - for( Uint32 sr = 0; sr < m_NumScissorRects; ++sr ) + if (Desc.GraphicsPipeline.RasterizerDesc.ScissorEnable) { - d3d12ScissorRects[sr].left = m_ScissorRects[sr].left; - d3d12ScissorRects[sr].top = m_ScissorRects[sr].top; - d3d12ScissorRects[sr].right = m_ScissorRects[sr].right; - d3d12ScissorRects[sr].bottom = m_ScissorRects[sr].bottom; + // Commit currently set scissor rectangles + D3D12_RECT d3d12ScissorRects[MaxD3D12ScissorRects]; // Do not waste time initializing array with zeroes + for( Uint32 sr = 0; sr < m_NumScissorRects; ++sr ) + { + d3d12ScissorRects[sr].left = m_ScissorRects[sr].left; + d3d12ScissorRects[sr].top = m_ScissorRects[sr].top; + d3d12ScissorRects[sr].right = m_ScissorRects[sr].right; + d3d12ScissorRects[sr].bottom = m_ScissorRects[sr].bottom; + } + GraphicsCtx.SetScissorRects(m_NumScissorRects, d3d12ScissorRects); + } + else + { + // Disable scissor rectangles + static_assert(_countof(MaxD3D12TexSizeRects) == MaxD3D12ScissorRects, "Unexpected array size"); + GraphicsCtx.SetScissorRects(MaxD3D12ScissorRects, MaxD3D12TexSizeRects); } - GraphicsCtx.SetScissorRects(m_NumScissorRects, d3d12ScissorRects); - } - else - { - // Disable scissor rectangles - static_assert(_countof(MaxD3D12TexSizeRects) == MaxD3D12ScissorRects, "Unexpected array size"); - GraphicsCtx.SetScissorRects(MaxD3D12ScissorRects, MaxD3D12TexSizeRects); - } - GraphicsCtx.SetStencilRef( m_StencilRef ); - GraphicsCtx.SetBlendFactor( m_BlendFactors ); - m_CommittedD3D12IndexBuffer = nullptr; - m_CommittedD3D12IndexDataStartOffset = 0; - m_CommittedIBFormat = VT_UNDEFINED; - m_bCommittedD3D12VBsUpToDate = false; - m_bCommittedD3D12IBUpToDate = false; - CommitRenderTargets(); - CommitViewports(); + GraphicsCtx.SetStencilRef(m_StencilRef); + GraphicsCtx.SetBlendFactor(m_BlendFactors); + CommitRenderTargets(); + CommitViewports(); + } } m_pCommittedResourceCache = nullptr; } @@ -546,6 +549,12 @@ namespace Diligent m_pCurrCmdCtx = RequestNewCmdCtx ? pDeviceD3D12Impl->AllocateCommandContext() : nullptr; m_NumCommandsInCurCtx = 0; + m_CommittedD3D12IndexBuffer = nullptr; + m_CommittedD3D12IndexDataStartOffset = 0; + m_CommittedIBFormat = VT_UNDEFINED; + m_bCommittedD3D12VBsUpToDate = false; + m_bCommittedD3D12IBUpToDate = false; + m_pPipelineState.Release(); } -- cgit v1.2.3