From 3531a7d892a01cff3449d371df2784b75fa9a534 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Tue, 17 Apr 2018 20:02:38 -0700 Subject: Working on Device context Vulkan implementation: implemented setting viewports & scissor rects --- .../include/DeviceContextVkImpl.h | 12 +- .../include/PipelineStateVkImpl.h | 6 +- .../include/VulkanUtilities/VulkanCommandBuffer.h | 62 +++--- .../interface/PipelineStateVk.h | 16 +- .../src/DeviceContextVkImpl.cpp | 222 +++++++++------------ .../src/RenderDeviceFactoryVk.cpp | 2 +- .../src/RenderDeviceVkImpl.cpp | 30 +-- .../GraphicsEngineVulkan/src/RootSignature.cpp | 22 +- .../src/ShaderResourceLayoutVk.cpp | 6 +- .../GraphicsEngineVulkan/src/SwapChainVkImpl.cpp | 12 +- 10 files changed, 170 insertions(+), 220 deletions(-) (limited to 'Graphics/GraphicsEngineVulkan') diff --git a/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h b/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h index 32861f69..4564b331 100644 --- a/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h @@ -116,24 +116,20 @@ public: size_t GetNumCommandsInCtx()const { return m_NumCommandsInCurCtx; } private: + void CommitRenderPassAndFramebuffer(); void CommitVkIndexBuffer(VALUE_TYPE IndexType); void CommitVkVertexBuffers(class GraphicsContext &GraphCtx); void TransitionVkVertexBuffers(class GraphicsContext &GraphCtx); void CommitRenderTargets(); void CommitViewports(); - void CommitScissorRects(class GraphicsContext &GraphCtx, bool ScissorEnable); + void CommitScissorRects(); void Flush(bool RequestNewCmdCtx); #if 0 friend class SwapChainVkImpl; - inline class CommandContext* RequestCmdContext() - { - // Make sure that the number of commands in the context is at least one, - // so that the context cannot be disposed by Flush() - m_NumCommandsInCurCtx = m_NumCommandsInCurCtx != 0 ? m_NumCommandsInCurCtx : 1; - return m_pCurrCmdCtx; - } #endif + inline void EnsureVkCmdBuffer(); + inline void DisposeVkCmdBuffer(); VulkanUtilities::VulkanCommandBuffer m_CommandBuffer; diff --git a/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h b/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h index 8cf7a13e..e90cd69a 100644 --- a/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h @@ -50,15 +50,15 @@ public: virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface ); - //virtual IVkPipelineState *GetVkPipelineState()const override final{return m_pVkPSO;} - virtual void BindShaderResources( IResourceMapping *pResourceMapping, Uint32 Flags )override; virtual void CreateShaderResourceBinding( IShaderResourceBinding **ppShaderResourceBinding )override; virtual bool IsCompatibleWith(const IPipelineState *pPSO)const override final; - //virtual IVkRootSignature *GetVkRootSignature()const override final{return m_RootSig.GetVkRootSignature(); } + virtual VkRenderPass GetVkRenderPass()const override final{return m_RenderPass;} + + virtual VkPipeline GetVkPipeline()const override final { return m_Pipeline; } //ShaderResourceCacheVk* CommitAndTransitionShaderResources(IShaderResourceBinding *pShaderResourceBinding, // class CommandContext &Ctx, diff --git a/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanCommandBuffer.h b/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanCommandBuffer.h index e7ea80f2..9af47e44 100644 --- a/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanCommandBuffer.h +++ b/Graphics/GraphicsEngineVulkan/include/VulkanUtilities/VulkanCommandBuffer.h @@ -144,29 +144,31 @@ namespace VulkanUtilities VERIFY_EXPR(m_VkCmdBuffer != VK_NULL_HANDLE); VERIFY(m_State.RenderPass == VK_NULL_HANDLE, "Current pass has not been ended"); - VkRenderPassBeginInfo BeginInfo; - BeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; - BeginInfo.pNext = nullptr; - BeginInfo.renderPass = RenderPass; - BeginInfo.framebuffer = Framebuffer; - // The render area MUST be contained within the framebuffer dimensions (7.4) - BeginInfo.renderArea = {{0,0}, { FramebufferWidth, FramebufferHeight }}; - BeginInfo.clearValueCount = 0; - BeginInfo.pClearValues = nullptr; // an array of VkClearValue structures that contains clear values for - // each attachment, if the attachment uses a loadOp value of VK_ATTACHMENT_LOAD_OP_CLEAR - // or if the attachment has a depth/stencil format and uses a stencilLoadOp value of - // VK_ATTACHMENT_LOAD_OP_CLEAR. The array is indexed by attachment number. Only elements - // corresponding to cleared attachments are used. Other elements of pClearValues are - // ignored (7.4) - - vkCmdBeginRenderPass(m_VkCmdBuffer, &BeginInfo, - VK_SUBPASS_CONTENTS_INLINE // the contents of the subpass will be recorded inline in the - // primary command buffer, and secondary command buffers must not - // be executed within the subpass - ); - - m_State.RenderPass = RenderPass; - m_State.Framebuffer = Framebuffer; + if(m_State.RenderPass != RenderPass || m_State.Framebuffer != Framebuffer) + { + VkRenderPassBeginInfo BeginInfo; + BeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; + BeginInfo.pNext = nullptr; + BeginInfo.renderPass = RenderPass; + BeginInfo.framebuffer = Framebuffer; + // The render area MUST be contained within the framebuffer dimensions (7.4) + BeginInfo.renderArea = {{0,0}, { FramebufferWidth, FramebufferHeight }}; + BeginInfo.clearValueCount = 0; + BeginInfo.pClearValues = nullptr; // an array of VkClearValue structures that contains clear values for + // each attachment, if the attachment uses a loadOp value of VK_ATTACHMENT_LOAD_OP_CLEAR + // or if the attachment has a depth/stencil format and uses a stencilLoadOp value of + // VK_ATTACHMENT_LOAD_OP_CLEAR. The array is indexed by attachment number. Only elements + // corresponding to cleared attachments are used. Other elements of pClearValues are + // ignored (7.4) + + vkCmdBeginRenderPass(m_VkCmdBuffer, &BeginInfo, + VK_SUBPASS_CONTENTS_INLINE // the contents of the subpass will be recorded inline in the + // primary command buffer, and secondary command buffers must not + // be executed within the subpass + ); + m_State.RenderPass = RenderPass; + m_State.Framebuffer = Framebuffer; + } } void EndRenderPass() @@ -186,16 +188,22 @@ namespace VulkanUtilities { // 9.8 VERIFY_EXPR(m_VkCmdBuffer != VK_NULL_HANDLE); - vkCmdBindPipeline(m_VkCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, ComputePipeline); - m_State.ComputePipeline = ComputePipeline; + if(m_State.ComputePipeline != ComputePipeline) + { + vkCmdBindPipeline(m_VkCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, ComputePipeline); + m_State.ComputePipeline = ComputePipeline; + } } void BindGraphicsPipeline(VkPipeline GraphicsPipeline) { // 9.8 VERIFY_EXPR(m_VkCmdBuffer != VK_NULL_HANDLE); - vkCmdBindPipeline(m_VkCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, GraphicsPipeline); - m_State.GraphicsPipeline = GraphicsPipeline; + if(m_State.GraphicsPipeline != GraphicsPipeline) + { + vkCmdBindPipeline(m_VkCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, GraphicsPipeline); + m_State.GraphicsPipeline = GraphicsPipeline; + } } void SetViewports(uint32_t firstViewport, uint32_t viewportCount, const VkViewport* pViewports) diff --git a/Graphics/GraphicsEngineVulkan/interface/PipelineStateVk.h b/Graphics/GraphicsEngineVulkan/interface/PipelineStateVk.h index 828593d7..e7df6722 100644 --- a/Graphics/GraphicsEngineVulkan/interface/PipelineStateVk.h +++ b/Graphics/GraphicsEngineVulkan/interface/PipelineStateVk.h @@ -41,17 +41,11 @@ class IPipelineStateVk : public IPipelineState { public: - /// Returns ID3D12PipelineState interface of the internal D3D12 pipeline state object object. - - /// The method does *NOT* call AddRef() on the returned interface, - /// so Release() must not be called. - //virtual ID3D12PipelineState* GetD3D12PipelineState()const = 0; - - /// Returns a pointer to the root signature object associated with this pipeline state. - - /// The method does *NOT* call AddRef() on the returned interface, - /// so Release() must not be called. - //virtual ID3D12RootSignature* GetD3D12RootSignature()const = 0; + /// Returns handle to a vulkan render pass object. + virtual VkRenderPass GetVkRenderPass()const = 0; + + /// Returns handle to a vulkan pipeline pass object. + virtual VkPipeline GetVkPipeline()const = 0; }; } diff --git a/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp index 0dc4c85a..d144f115 100644 --- a/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp @@ -77,23 +77,54 @@ namespace Diligent DeviceContextVkImpl::~DeviceContextVkImpl() { -#if 0 if(m_bIsDeferred) - ValidatedCast(m_pDevice.RawPtr())->DisposeCommandContext(m_pCurrCmdCtx); + { + DisposeVkCmdBuffer(); + } else { if (m_NumCommandsInCurCtx != 0) - LOG_WARNING_MESSAGE("Flusing outstanding commands from the device context being destroyed. This may result in Vk synchronization errors"); + LOG_WARNING_MESSAGE("Flusing outstanding commands from the device context being destroyed. This may result in synchronization errors"); Flush(false); } -#endif } IMPLEMENT_QUERY_INTERFACE( DeviceContextVkImpl, IID_DeviceContextVk, TDeviceContextBase ) + inline void DeviceContextVkImpl::EnsureVkCmdBuffer() + { + // Make sure that the number of commands in the context is at least one, + // so that the context cannot be disposed by Flush() + m_NumCommandsInCurCtx = m_NumCommandsInCurCtx != 0 ? m_NumCommandsInCurCtx : 1; + if (m_CommandBuffer.GetVkCmdBuffer() == VK_NULL_HANDLE) + { + auto pDeviceVkImpl = m_pDevice.RawPtr(); + auto vkCmdBuff = m_CmdPool.GetCommandBuffer(pDeviceVkImpl->GetCompletedFenceValue()); + m_CommandBuffer.SetVkCmdBuffer(vkCmdBuff); + } + } + + inline void DeviceContextVkImpl::DisposeVkCmdBuffer() + { + VERIFY(m_CommandBuffer.GetState().RenderPass == VK_NULL_HANDLE, "Disposing command buffer with unifinished render pass"); + auto vkCmdBuff = m_CommandBuffer.GetVkCmdBuffer(); + if(vkCmdBuff != VK_NULL_HANDLE) + { + auto pDeviceVkImpl = m_pDevice.RawPtr(); + m_CmdPool.DisposeCommandBuffer(vkCmdBuff, pDeviceVkImpl->GetNextFenceValue()); + m_CommandBuffer.Reset(); + } + } + + void DeviceContextVkImpl::SetPipelineState(IPipelineState *pPipelineState) { + if (m_CommandBuffer.GetState().RenderPass) + { + m_CommandBuffer.EndRenderPass(); + } + // Never flush deferred context! if (!m_bIsDeferred && m_NumCommandsInCurCtx >= m_NumCommandsToFlush) { @@ -120,37 +151,35 @@ namespace Diligent // and the first PSO set on the command list was a compute pipeline, // the states would otherwise never be committed (since m_pPipelineState != nullptr) CommitStates = OldPSODesc.IsComputePipeline; - // We also need to update scissor rect if ScissorEnable state has changed - CommitScissor = OldPSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable != PSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable; + // We also need to update scissor rect if ScissorEnable state was disabled in previous pipeline + CommitScissor = !OldPSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable; } TDeviceContextBase::SetPipelineState( pPipelineState ); -#if 0 - auto *pCmdCtx = RequestCmdContext(); - - auto *pVkPSO = pPipelineStateVk->GetVkPipelineState(); + EnsureVkCmdBuffer(); + if (PSODesc.IsComputePipeline) { - pCmdCtx->AsComputeContext().SetPipelineState(pVkPSO); + auto vkPipeline = pPipelineStateVk->GetVkPipeline(); + m_CommandBuffer.BindComputePipeline(vkPipeline); } else { - auto &GraphicsCtx = pCmdCtx->AsGraphicsContext(); - GraphicsCtx.SetPipelineState(pVkPSO); - if(CommitStates) { - GraphicsCtx.SetStencilRef(m_StencilRef); - GraphicsCtx.SetBlendFactor(m_BlendFactors); + m_CommandBuffer.SetStencilReference(m_StencilRef); + m_CommandBuffer.SetBlendConstants(m_BlendFactors); CommitRenderTargets(); CommitViewports(); } - if(CommitStates || CommitScissor) + if(PSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable && (CommitStates || CommitScissor)) { - CommitScissorRects(GraphicsCtx, PSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable); + CommitScissorRects(); } } + +#if 0 m_pCommittedResourceCache = nullptr; #endif } @@ -172,7 +201,7 @@ namespace Diligent return; #if 0 auto *pCtx = RequestCmdContext(); - auto *pPipelineStateVk = ValidatedCast(m_pPipelineState.RawPtr()); + auto *pPipelineStateVk = m_pPipelineState.RawPtr(); m_pCommittedResourceCache = pPipelineStateVk->CommitAndTransitionShaderResources(pShaderResourceBinding, *pCtx, true, (Flags & COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES)!=0); #endif @@ -182,6 +211,7 @@ namespace Diligent { if (TDeviceContextBase::SetStencilRef(StencilRef, 0)) { + EnsureVkCmdBuffer(); m_CommandBuffer.SetStencilReference(m_StencilRef); } } @@ -190,10 +220,17 @@ namespace Diligent { if (TDeviceContextBase::SetBlendFactors(m_BlendFactors, 0)) { + EnsureVkCmdBuffer(); m_CommandBuffer.SetBlendConstants(m_BlendFactors); } } + void DeviceContextVkImpl::CommitRenderPassAndFramebuffer() + { + auto *pPipelineStateVk = m_pPipelineState.RawPtr(); + + } + void DeviceContextVkImpl::CommitVkIndexBuffer(VALUE_TYPE IndexType) { VERIFY( m_pIndexBuffer != nullptr, "Index buffer is not set up for indexed draw command" ); @@ -264,7 +301,7 @@ namespace Diligent void DeviceContextVkImpl::CommitVkVertexBuffers(GraphicsContext &GraphCtx) { - auto *pPipelineStateVk = ValidatedCast(m_pPipelineState.RawPtr()); + auto *pPipelineStateVk = m_pPipelineState.RawPtr(); #if 0 // Do not initialize array with zeroes for performance reasons Vk_VERTEX_BUFFER_VIEW VBViews[MaxBufferSlots];// = {} @@ -324,6 +361,9 @@ namespace Diligent } #endif + if(m_CommandBuffer.GetState().RenderPass == VK_NULL_HANDLE) + CommitRenderPassAndFramebuffer(); + #if 0 auto &GraphCtx = RequestCmdContext()->AsGraphicsContext(); if( DrawAttribs.IsIndexed ) @@ -341,7 +381,7 @@ namespace Diligent CommitVkIndexBuffer(DrawAttribs.IndexType); } - auto *pPipelineStateVk = ValidatedCast(m_pPipelineState.RawPtr()); + auto *pPipelineStateVk = m_pPipelineState.RawPtr(); auto VkTopology = TopologyToVkTopology( DrawAttribs.Topology ); GraphCtx.SetPrimitiveTopology(VkTopology); @@ -412,7 +452,7 @@ namespace Diligent #endif #if 0 - auto *pPipelineStateVk = ValidatedCast(m_pPipelineState.RawPtr()); + auto *pPipelineStateVk = m_pPipelineState.RawPtr(); auto &ComputeCtx = RequestCmdContext()->AsComputeContext(); ComputeCtx.SetRootSignature( pPipelineStateVk->GetVkRootSignature() ); @@ -470,7 +510,7 @@ namespace Diligent { if (m_pSwapChain) { - pDSVVk = ValidatedCast(m_pSwapChain.RawPtr())->GetDepthBufferDSV(); + pDSVVk = m_pSwapChain.RawPtr()->GetDepthBufferDSV(); } else { @@ -504,7 +544,7 @@ namespace Diligent { if (m_pSwapChain) { - pVkRTV = ValidatedCast(m_pSwapChain.RawPtr())->GetCurrentBackBufferRTV(); + pVkRTV = m_pSwapChain.RawPtr()->GetCurrentBackBufferRTV(); } else { @@ -526,7 +566,7 @@ namespace Diligent void DeviceContextVkImpl::Flush(bool RequestNewCmdCtx) { - auto pDeviceVkImpl = ValidatedCast(m_pDevice.RawPtr()); + auto pDeviceVkImpl = m_pDevice.RawPtr(); auto vkCmdBuff = m_CommandBuffer.GetVkCmdBuffer(); if(vkCmdBuff != VK_NULL_HANDLE ) { @@ -536,17 +576,13 @@ namespace Diligent //m_pCurrCmdCtx->FlushResourceBarriers(); pDeviceVkImpl->ExecuteCommandBuffer(vkCmdBuff, true); } - m_CmdPool.DisposeCommandBuffer(vkCmdBuff, pDeviceVkImpl->GetNextFenceValue()); + DisposeVkCmdBuffer(); } m_NumCommandsInCurCtx = 0; - m_CommandBuffer.Reset(); if(RequestNewCmdCtx) - { - vkCmdBuff = m_CmdPool.GetCommandBuffer(pDeviceVkImpl->GetCompletedFenceValue()); - m_CommandBuffer.SetVkCmdBuffer(vkCmdBuff); - } + EnsureVkCmdBuffer(); m_pPipelineState.Release(); } @@ -560,9 +596,7 @@ namespace Diligent void DeviceContextVkImpl::SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags ) { TDeviceContextBase::SetVertexBuffers( StartSlot, NumBuffersSet, ppBuffers, pStrides, pOffsets, Flags ); -#if 0 - m_bCommittedVkVBsUpToDate = false; -#endif + m_State.CommittedVBsUpToDate = false; } void DeviceContextVkImpl::InvalidateState() @@ -571,119 +605,63 @@ namespace Diligent LOG_WARNING_MESSAGE("Invalidating context that has outstanding commands in it. Call Flush() to submit commands for execution"); TDeviceContextBase::InvalidateState(); -#if 0 - m_CommittedVkIndexBuffer = nullptr; - m_CommittedVkIndexDataStartOffset = 0; - m_CommittedIBFormat = VT_UNDEFINED; - m_bCommittedVkVBsUpToDate = false; - m_bCommittedVkIBUpToDate = false; -#endif + m_State = ContextState{}; + VERIFY(m_CommandBuffer.GetState().RenderPass == VK_NULL_HANDLE, "Invalidating context with unifinished render pass"); + m_CommandBuffer.Reset(); } void DeviceContextVkImpl::SetIndexBuffer( IBuffer *pIndexBuffer, Uint32 ByteOffset ) { TDeviceContextBase::SetIndexBuffer( pIndexBuffer, ByteOffset ); -#if 0 - m_bCommittedVkIBUpToDate = false; -#endif + m_State.CommittedIBUpToDate = false; } void DeviceContextVkImpl::CommitViewports() { -#if 0 - constexpr Uint32 MaxViewports = Vk_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; - Vk_VIEWPORT VkViewports[MaxViewports]; // Do not waste time initializing array to zero - + VkViewport VkViewports[MaxViewports]; // Do not waste time initializing array to zero for( Uint32 vp = 0; vp < m_NumViewports; ++vp ) { - VkViewports[vp].TopLeftX = m_Viewports[vp].TopLeftX; - VkViewports[vp].TopLeftY = m_Viewports[vp].TopLeftY; - VkViewports[vp].Width = m_Viewports[vp].Width; - VkViewports[vp].Height = m_Viewports[vp].Height; - VkViewports[vp].MinDepth = m_Viewports[vp].MinDepth; - VkViewports[vp].MaxDepth = m_Viewports[vp].MaxDepth; + VkViewports[vp].x = m_Viewports[vp].TopLeftX; + VkViewports[vp].y = m_Viewports[vp].TopLeftY; + VkViewports[vp].width = m_Viewports[vp].Width; + VkViewports[vp].height = m_Viewports[vp].Height; + VkViewports[vp].minDepth = m_Viewports[vp].MinDepth; + VkViewports[vp].maxDepth = m_Viewports[vp].MaxDepth; } - // All viewports must be set atomically as one operation. - // Any viewports not defined by the call are disabled. - RequestCmdContext()->AsGraphicsContext().SetViewports( m_NumViewports, VkViewports ); -#endif + EnsureVkCmdBuffer(); + // TODO: reinterpret_cast m_Viewports to m_Viewports? + m_CommandBuffer.SetViewports(0, m_NumViewports, VkViewports); } void DeviceContextVkImpl::SetViewports( Uint32 NumViewports, const Viewport *pViewports, Uint32 RTWidth, Uint32 RTHeight ) { -#if 0 - constexpr Uint32 MaxViewports = Vk_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; - VERIFY( NumViewports < MaxViewports, "Too many viewports are being set" ); - NumViewports = std::min( NumViewports, MaxViewports ); - TDeviceContextBase::SetViewports( NumViewports, pViewports, RTWidth, RTHeight ); VERIFY( NumViewports == m_NumViewports, "Unexpected number of viewports" ); CommitViewports(); -#endif } -/* - constexpr LONG MaxVkTexDim = Vk_REQ_TEXTURE2D_U_OR_V_DIMENSION; - constexpr Uint32 MaxVkScissorRects = Vk_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; - static constexpr RECT MaxVkTexSizeRects[Vk_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE] = + void DeviceContextVkImpl::CommitScissorRects() { - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim }, - { 0,0, MaxVkTexDim,MaxVkTexDim } - }; -*/ + VERIFY(m_pPipelineState && m_pPipelineState->GetDesc().GraphicsPipeline.RasterizerDesc.ScissorEnable, "Scissor test must be enabled in the graphics pipeline"); - void DeviceContextVkImpl::CommitScissorRects(GraphicsContext &GraphCtx, bool ScissorEnable) - { -#if 0 - if (ScissorEnable) - { - // Commit currently set scissor rectangles - Vk_RECT VkScissorRects[MaxVkScissorRects]; // Do not waste time initializing array with zeroes - for (Uint32 sr = 0; sr < m_NumScissorRects; ++sr) - { - VkScissorRects[sr].left = m_ScissorRects[sr].left; - VkScissorRects[sr].top = m_ScissorRects[sr].top; - VkScissorRects[sr].right = m_ScissorRects[sr].right; - VkScissorRects[sr].bottom = m_ScissorRects[sr].bottom; - } - GraphCtx.SetScissorRects(m_NumScissorRects, VkScissorRects); - } - else + VkRect2D VkScissorRects[MaxViewports]; // Do not waste time initializing array with zeroes + for (Uint32 sr = 0; sr < m_NumScissorRects; ++sr) { - // Disable scissor rectangles - static_assert(_countof(MaxVkTexSizeRects) == MaxVkScissorRects, "Unexpected array size"); - GraphCtx.SetScissorRects(MaxVkScissorRects, MaxVkTexSizeRects); + const auto &SrcRect = m_ScissorRects[sr]; + VkScissorRects[sr].offset = {SrcRect.left, SrcRect.top}; + VkScissorRects[sr].extent = {static_cast(SrcRect.right - SrcRect.left), static_cast(SrcRect.bottom - SrcRect.top)}; } -#endif + + EnsureVkCmdBuffer(); + // TODO: reinterpret_cast m_Viewports to m_Viewports? + m_CommandBuffer.SetScissorRects(0, m_NumScissorRects, VkScissorRects); } void DeviceContextVkImpl::SetScissorRects( Uint32 NumRects, const Rect *pRects, Uint32 RTWidth, Uint32 RTHeight ) { -#if 0 - const Uint32 MaxScissorRects = Vk_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; - VERIFY( NumRects < MaxScissorRects, "Too many scissor rects are being set" ); - NumRects = std::min( NumRects, MaxScissorRects ); - TDeviceContextBase::SetScissorRects(NumRects, pRects, RTWidth, RTHeight); // Only commit scissor rects if scissor test is enabled in the rasterizer state. @@ -695,11 +673,9 @@ namespace Diligent if(!PSODesc.IsComputePipeline && PSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable) { VERIFY(NumRects == m_NumScissorRects, "Unexpected number of scissor rects"); - auto &Ctx = RequestCmdContext()->AsGraphicsContext(); - CommitScissorRects(Ctx, true); + CommitScissorRects(); } } -#endif } @@ -718,7 +694,7 @@ namespace Diligent if (m_pSwapChain) { NumRenderTargets = 1; - auto *pSwapChainVk = ValidatedCast(m_pSwapChain.RawPtr()); + auto *pSwapChainVk = m_pSwapChain.RawPtr(); ppRTVs[0] = pSwapChainVk->GetCurrentBackBufferRTV(); pDSV = pSwapChainVk->GetDepthBufferDSV(); } @@ -731,8 +707,8 @@ namespace Diligent else { for( Uint32 rt = 0; rt < NumRenderTargets; ++rt ) - ppRTVs[rt] = ValidatedCast(m_pBoundRenderTargets[rt].RawPtr()); - pDSV = ValidatedCast(m_pBoundDepthStencil.RawPtr()); + ppRTVs[rt] = m_pBoundRenderTargets[rt].RawPtr(); + pDSV = m_pBoundDepthStencil.RawPtr(); } RequestCmdContext()->AsGraphicsContext().SetRenderTargets(NumRenderTargets, ppRTVs, pDSV); #endif @@ -885,7 +861,7 @@ namespace Diligent { #if 0 auto *pCtx = RequestCmdContext(); - m_MipsGenerator.GenerateMips(ValidatedCast(m_pDevice.RawPtr()), pTexView, *pCtx); + m_MipsGenerator.GenerateMips(m_pDevice.RawPtr(), pTexView, *pCtx); ++m_NumCommandsInCurCtx; #endif } @@ -917,7 +893,7 @@ namespace Diligent InvalidateState(); CommandListVkImpl* pCmdListVk = ValidatedCast(pCommandList); - ValidatedCast(m_pDevice.RawPtr())->CloseAndExecuteCommandContext(pCmdListVk->Close(), true); + m_pDevice.RawPtr()->CloseAndExecuteCommandContext(pCmdListVk->Close(), true); #endif } diff --git a/Graphics/GraphicsEngineVulkan/src/RenderDeviceFactoryVk.cpp b/Graphics/GraphicsEngineVulkan/src/RenderDeviceFactoryVk.cpp index 7d0c2cea..2c2f1bcd 100644 --- a/Graphics/GraphicsEngineVulkan/src/RenderDeviceFactoryVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/RenderDeviceFactoryVk.cpp @@ -308,7 +308,7 @@ void EngineFactoryVkImpl::CreateSwapChainVk( IRenderDevice *pDevice, { if (auto pDeferredCtx = pDeviceVk->GetDeferredContext(ctx)) { - auto *pDeferredCtxVk = ValidatedCast(pDeferredCtx.RawPtr()); + auto *pDeferredCtxVk = pDeferredCtx.RawPtr(); pDeferredCtxVk->SetSwapChain(pSwapChainVk); // We cannot bind default render target here because // there is no guarantee that deferred context will be used diff --git a/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp index f6dcdf51..43b115c3 100644 --- a/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp @@ -235,11 +235,10 @@ Uint64 RenderDeviceVkImpl::GetCompletedFenceValue() void RenderDeviceVkImpl::FinishFrame(bool ReleaseAllResources) { -#if 0 { if (auto pImmediateCtx = m_wpImmediateContext.Lock()) { - auto pImmediateCtxVk = ValidatedCast(pImmediateCtx.RawPtr()); + auto pImmediateCtxVk = pImmediateCtx.RawPtr(); if(pImmediateCtxVk->GetNumCommandsInCtx() != 0) LOG_ERROR_MESSAGE("There are outstanding commands in the immediate device context when finishing the frame. This is an error and may cause unpredicted behaviour. Call Flush() to submit all commands for execution before finishing the frame"); } @@ -248,13 +247,13 @@ void RenderDeviceVkImpl::FinishFrame(bool ReleaseAllResources) { if (auto pDeferredCtx = wpDeferredCtx.Lock()) { - auto pDeferredCtxVk = ValidatedCast(pDeferredCtx.RawPtr()); + auto pDeferredCtxVk = pDeferredCtx.RawPtr(); if(pDeferredCtxVk->GetNumCommandsInCtx() != 0) LOG_ERROR_MESSAGE("There are outstanding commands in the deferred device context when finishing the frame. This is an error and may cause unpredicted behaviour. Close all deferred contexts and execute them before finishing the frame"); } } } -#endif + auto CompletedFenceValue = ReleaseAllResources ? std::numeric_limits::max() : GetCompletedFenceValue(); // We must use NextFenceValue here, NOT current value, because the @@ -353,29 +352,6 @@ VkCommandBuffer RenderDeviceVkImpl::AllocateCommandBuffer(const Char *DebugName) std::lock_guard LockGuard(m_CmdPoolMutex); auto CmdBuffer = m_CmdBufferPool.GetCommandBuffer(GetCompletedFenceValue(), DebugName); return CmdBuffer; - -#if 0 - CommandContext* ret = nullptr; - if (m_AvailableContexts.empty()) - { - auto &CmdCtxAllocator = GetRawAllocator(); - auto *pRawMem = ALLOCATE(CmdCtxAllocator, "CommandContext instance", sizeof(CommandContext)); - ret = new (pRawMem) CommandContext( GetRawAllocator(), m_CmdListManager, m_GPUDescriptorHeaps, m_DynamicDescriptorAllocationChunkSize); - m_ContextPool.emplace_back(ret, STDDeleterRawMem(CmdCtxAllocator) ); - } - else - { - ret = m_AvailableContexts.front(); - m_AvailableContexts.pop_front(); - ret->Reset(m_CmdListManager); - } - VERIFY_EXPR(ret != nullptr); - ret->SetID(ID); - //if ( ID != nullptr && *ID != 0 ) - // EngineProfiling::BeginBlock(ID, NewContext); - - return ret; -#endif } diff --git a/Graphics/GraphicsEngineVulkan/src/RootSignature.cpp b/Graphics/GraphicsEngineVulkan/src/RootSignature.cpp index 6a72824c..f357a860 100644 --- a/Graphics/GraphicsEngineVulkan/src/RootSignature.cpp +++ b/Graphics/GraphicsEngineVulkan/src/RootSignature.cpp @@ -643,7 +643,7 @@ void TransitionResource(CommandContext &Ctx, { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_CBV, "Unexpected descriptor range type"); // Not using QueryInterface() for the sake of efficiency - auto *pBuffToTransition = ValidatedCast(Res.pObject.RawPtr()); + auto *pBuffToTransition = Res.pObject.RawPtr(); if( !pBuffToTransition->CheckAllStates(Vk_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER) ) Ctx.TransitionResource(pBuffToTransition, Vk_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER ); } @@ -652,7 +652,7 @@ void TransitionResource(CommandContext &Ctx, case CachedResourceType::BufSRV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_SRV, "Unexpected descriptor range type"); - auto *pBuffViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pBuffViewVk = Res.pObject.RawPtr(); auto *pBuffToTransition = ValidatedCast(pBuffViewVk->GetBuffer()); if( !pBuffToTransition->CheckAllStates(Vk_RESOURCE_STATE_SHADER_RESOURCE) ) Ctx.TransitionResource(pBuffToTransition, Vk_RESOURCE_STATE_SHADER_RESOURCE ); @@ -662,7 +662,7 @@ void TransitionResource(CommandContext &Ctx, case CachedResourceType::BufUAV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_UAV, "Unexpected descriptor range type"); - auto *pBuffViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pBuffViewVk = Res.pObject.RawPtr(); auto *pBuffToTransition = ValidatedCast(pBuffViewVk->GetBuffer()); if( !pBuffToTransition->CheckAllStates(Vk_RESOURCE_STATE_UNORDERED_ACCESS) ) Ctx.TransitionResource(pBuffToTransition, Vk_RESOURCE_STATE_UNORDERED_ACCESS ); @@ -672,7 +672,7 @@ void TransitionResource(CommandContext &Ctx, case CachedResourceType::TexSRV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_SRV, "Unexpected descriptor range type"); - auto *pTexViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pTexViewVk = Res.pObject.RawPtr(); auto *pTexToTransition = ValidatedCast(pTexViewVk->GetTexture()); if( !pTexToTransition->CheckAllStates(Vk_RESOURCE_STATE_SHADER_RESOURCE) ) Ctx.TransitionResource(pTexToTransition, Vk_RESOURCE_STATE_SHADER_RESOURCE ); @@ -682,7 +682,7 @@ void TransitionResource(CommandContext &Ctx, case CachedResourceType::TexUAV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_UAV, "Unexpected descriptor range type"); - auto *pTexViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pTexViewVk = Res.pObject.RawPtr(); auto *pTexToTransition = ValidatedCast(pTexViewVk->GetTexture()); if( !pTexToTransition->CheckAllStates(Vk_RESOURCE_STATE_UNORDERED_ACCESS) ) Ctx.TransitionResource(pTexToTransition, Vk_RESOURCE_STATE_UNORDERED_ACCESS ); @@ -711,7 +711,7 @@ void DbgVerifyResourceState(ShaderResourceCacheVk::Resource &Res, { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_CBV, "Unexpected descriptor range type"); // Not using QueryInterface() for the sake of efficiency - auto *pBuffToTransition = ValidatedCast(Res.pObject.RawPtr()); + auto *pBuffToTransition = Res.pObject.RawPtr(); auto State = pBuffToTransition->GetState(); if( (State & Vk_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER) != Vk_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER ) LOG_ERROR_MESSAGE("Resource \"", pBuffToTransition->GetDesc().Name, "\" is not in Vk_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER state. Did you forget to call TransitionShaderResources() or specify COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES flag in a call to CommitShaderResources()?" ); @@ -721,7 +721,7 @@ void DbgVerifyResourceState(ShaderResourceCacheVk::Resource &Res, case CachedResourceType::BufSRV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_SRV, "Unexpected descriptor range type"); - auto *pBuffViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pBuffViewVk = Res.pObject.RawPtr(); auto *pBuffToTransition = ValidatedCast(pBuffViewVk->GetBuffer()); auto State = pBuffToTransition->GetState(); if( (State & Vk_RESOURCE_STATE_SHADER_RESOURCE) != Vk_RESOURCE_STATE_SHADER_RESOURCE ) @@ -732,7 +732,7 @@ void DbgVerifyResourceState(ShaderResourceCacheVk::Resource &Res, case CachedResourceType::BufUAV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_UAV, "Unexpected descriptor range type"); - auto *pBuffViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pBuffViewVk = Res.pObject.RawPtr(); auto *pBuffToTransition = ValidatedCast(pBuffViewVk->GetBuffer()); auto State = pBuffToTransition->GetState(); if( (State & Vk_RESOURCE_STATE_UNORDERED_ACCESS) != Vk_RESOURCE_STATE_UNORDERED_ACCESS ) @@ -743,7 +743,7 @@ void DbgVerifyResourceState(ShaderResourceCacheVk::Resource &Res, case CachedResourceType::TexSRV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_SRV, "Unexpected descriptor range type"); - auto *pTexViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pTexViewVk = Res.pObject.RawPtr(); auto *pTexToTransition = ValidatedCast(pTexViewVk->GetTexture()); auto State = pTexToTransition->GetState(); if( (State & Vk_RESOURCE_STATE_SHADER_RESOURCE) != Vk_RESOURCE_STATE_SHADER_RESOURCE ) @@ -754,7 +754,7 @@ void DbgVerifyResourceState(ShaderResourceCacheVk::Resource &Res, case CachedResourceType::TexUAV: { VERIFY(RangeType == Vk_DESCRIPTOR_RANGE_TYPE_UAV, "Unexpected descriptor range type"); - auto *pTexViewVk = ValidatedCast(Res.pObject.RawPtr()); + auto *pTexViewVk = Res.pObject.RawPtr(); auto *pTexToTransition = ValidatedCast(pTexViewVk->GetTexture()); auto State = pTexToTransition->GetState(); if( (State & Vk_RESOURCE_STATE_UNORDERED_ACCESS) != Vk_RESOURCE_STATE_UNORDERED_ACCESS ) @@ -1018,7 +1018,7 @@ void RootSignature::CommitRootViews(ShaderResourceCacheVk& ResourceCache, #endif auto& Res = ResourceCache.GetRootTable(RootInd).GetResource(0, Vk_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, dbgShaderType); - auto *pBuffToTransition = ValidatedCast(Res.pObject.RawPtr()); + auto *pBuffToTransition = Res.pObject.RawPtr(); if( !pBuffToTransition->CheckAllStates(Vk_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER) ) Ctx.TransitionResource(pBuffToTransition, Vk_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER); diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp index f0cb17e2..4b6dd174 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp @@ -734,7 +734,7 @@ bool ShaderResourceLayoutVk::SRV_CBV_UAV::IsBound(Uint32 ArrayIndex) auto &CachedRes = RootTable.GetResource(OffsetFromTableStart + ArrayIndex, Vk_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, m_ParentResLayout.m_pResources->GetShaderType()); if( CachedRes.pObject != nullptr ) { - VERIFY(CachedRes.CPUDescriptorHandle.ptr != 0 || ValidatedCast(CachedRes.pObject.RawPtr())->GetDesc().Usage == USAGE_DYNAMIC, "No relevant descriptor handle"); + VERIFY(CachedRes.CPUDescriptorHandle.ptr != 0 || CachedRes.pObject.RawPtr()->GetDesc().Usage == USAGE_DYNAMIC, "No relevant descriptor handle"); return true; } } @@ -948,7 +948,7 @@ void ShaderResourceLayoutVk::dbgVerifyBindings()const if( !CachedRes.pObject || // Dynamic buffers do not have CPU descriptor handle as they do not keep Vk buffer, and space is allocated from the GPU ring buffer - CachedRes.CPUDescriptorHandle.ptr == 0 && !(CachedRes.Type==CachedResourceType::CBV && ValidatedCast(CachedRes.pObject.RawPtr())->GetDesc().Usage == USAGE_DYNAMIC) ) + CachedRes.CPUDescriptorHandle.ptr == 0 && !(CachedRes.Type==CachedResourceType::CBV && CachedRes.pObject.RawPtr()->GetDesc().Usage == USAGE_DYNAMIC) ) LOG_ERROR_MESSAGE( "No resource is bound to ", GetShaderVariableTypeLiteralName(res.Attribs.GetVariableType()), " variable \"", res.Attribs.GetPrintName(ArrInd), "\" in shader \"", GetShaderName(), "\"" ); if (res.Attribs.BindCount > 1 && res.IsValidSampler()) @@ -958,7 +958,7 @@ void ShaderResourceLayoutVk::dbgVerifyBindings()const if(SamInfo.Attribs.BindCount == 1) { const auto &CachedSampler = m_pResourceCache->GetRootTable(SamInfo.RootIndex).GetResource(SamInfo.OffsetFromTableStart, Vk_DESCRIPTOR_HEAP_TYPE_SAMPLER, m_pResources->GetShaderType()); - if( auto *pTexView = ValidatedCast(CachedRes.pObject.RawPtr()) ) + if( auto *pTexView = CachedRes.pObject.RawPtr() ) { auto *pSampler = const_cast(pTexView)->GetSampler(); if (pSampler != nullptr && CachedSampler.pObject != pSampler) diff --git a/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp index f36fd208..0dfe082b 100644 --- a/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp @@ -98,7 +98,7 @@ SwapChainVkImpl::SwapChainVkImpl(IReferenceCounters *pRefCounters, void SwapChainVkImpl::CreateVulkanSwapChain() { - auto *pRenderDeviceVk = ValidatedCast(m_pRenderDevice.RawPtr()); + auto *pRenderDeviceVk = m_pRenderDevice.RawPtr(); const auto& PhysicalDevice = pRenderDeviceVk->GetPhysicalDevice(); auto vkDeviceHandle = PhysicalDevice.GetVkDeviceHandle(); // Get the list of VkFormats that are supported: @@ -302,14 +302,14 @@ SwapChainVkImpl::~SwapChainVkImpl() { if(m_VkSwapChain != VK_NULL_HANDLE) { - auto *pDeviceVkImpl = ValidatedCast(m_pRenderDevice.RawPtr()); + auto *pDeviceVkImpl = m_pRenderDevice.RawPtr(); vkDestroySwapchainKHR(pDeviceVkImpl->GetVkDevice(), m_VkSwapChain, NULL); } } void SwapChainVkImpl::InitBuffersAndViews() { - auto *pDeviceVkImpl = ValidatedCast(m_pRenderDevice.RawPtr()); + auto *pDeviceVkImpl = m_pRenderDevice.RawPtr(); auto LogicalVkDevice = pDeviceVkImpl->GetVkDevice(); #ifdef _DEBUG @@ -345,7 +345,7 @@ void SwapChainVkImpl::InitBuffersAndViews() BackBufferDesc.MipLevels = 1; RefCntAutoPtr pBackBufferTex; - ValidatedCast(m_pRenderDevice.RawPtr())->CreateTexture(BackBufferDesc, swapchainImages[i], &pBackBufferTex); + m_pRenderDevice.RawPtr()->CreateTexture(BackBufferDesc, swapchainImages[i], &pBackBufferTex); TextureViewDesc RTVDesc; RTVDesc.ViewType = TEXTURE_VIEW_RENDER_TARGET; @@ -422,12 +422,12 @@ void SwapChainVkImpl::Resize( Uint32 NewWidth, Uint32 NewHeight ) VERIFY( pDeviceContext, "Immediate context has been released" ); if( pDeviceContext ) { - RenderDeviceVkImpl *pDeviceVk = ValidatedCast(m_pRenderDevice.RawPtr()); + RenderDeviceVkImpl *pDeviceVk = m_pRenderDevice.RawPtr(); pDeviceContext->Flush(); try { - auto *pImmediateCtxVk = ValidatedCast(pDeviceContext.RawPtr()); + auto *pImmediateCtxVk = pDeviceContext.RawPtr(); bool bIsDefaultFBBound = pImmediateCtxVk->IsDefaultFBBound(); // All references to the swap chain must be released before it can be resized -- cgit v1.2.3