From 466dff5b223bd3f4af7963fec546ff179a745556 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 2 Dec 2018 09:39:49 -0800 Subject: Added explicit state transition control to UpdateBuffer and UpdateTexture commands --- .../GraphicsEngine/include/DeviceContextBase.h | 170 +++++++++++++-------- Graphics/GraphicsEngine/interface/DeviceContext.h | 55 +++++-- 2 files changed, 148 insertions(+), 77 deletions(-) (limited to 'Graphics/GraphicsEngine') diff --git a/Graphics/GraphicsEngine/include/DeviceContextBase.h b/Graphics/GraphicsEngine/include/DeviceContextBase.h index 0bbdd843..d8dfd94b 100644 --- a/Graphics/GraphicsEngine/include/DeviceContextBase.h +++ b/Graphics/GraphicsEngine/include/DeviceContextBase.h @@ -55,21 +55,22 @@ struct VertexStreamInfo /// Base implementation of the device context. /// \tparam BaseInterface - base interface that this class will inheret. -/// \tparam BufferImplType - buffer implemenation type (BufferD3D11Impl, BufferD3D12Impl, etc.) -/// \tparam TextureViewImplType - texture view implemenation type (TextureViewD3D11Impl, TextureViewD3D12Impl, etc.) -/// \tparam PipelineStateImplType - pipeline state implementation type (PipelineStateD3D11Impl, PipelineStateD3D12Impl, etc.) +/// \tparam BufferImplType - buffer implemenation type (BufferVkImpl, BufferD3D12Impl, etc.) +/// \tparam TextureImplType - texture implemenation type (TextureVkImpl, TextureD3D12Impl, etc.) +/// \tparam PipelineStateImplType - pipeline state implementation type (PipelineStateVkImpl, PipelineStateD3D12Impl, etc.) /// \remarks Device context keeps strong references to all objects currently bound to /// the pipeline: buffers, states, samplers, shaders, etc. /// The context also keeps strong references to the device and /// the swap chain. template class DeviceContextBase : public ObjectBase { public: - using TObjectBase = ObjectBase; + using TObjectBase = ObjectBase; + using TextureViewImplType = typename TextureImplType::ViewImplType; /// \param pRefCounters - reference counters object that controls the lifetime of this device context. /// \param pRenderDevice - render device. @@ -110,7 +111,7 @@ public: inline bool SetRenderTargets( Uint32 NumRenderTargets, ITextureView* ppRenderTargets[], ITextureView* pDepthStencil); /// Base implementation of IDeviceContext::UpdateBuffer(); validates input parameters. - virtual void UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData)override = 0; + virtual void UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData, RESOURCE_STATE_TRANSITION_MODE StateTransitionMode)override = 0; /// Base implementation of IDeviceContext::CopyBuffer(); validates input parameters. virtual void CopyBuffer(IBuffer *pSrcBuffer, Uint32 SrcOffset, IBuffer *pDstBuffer, Uint32 DstOffset, Uint32 Size)override = 0; @@ -122,7 +123,8 @@ public: virtual void UnmapBuffer(IBuffer* pBuffer)override = 0; /// Base implementaiton of IDeviceContext::UpdateData(); validates input parameters - virtual void UpdateTexture( ITexture* pTexture, Uint32 MipLevel, Uint32 Slice, const Box& DstBox, const TextureSubResData& SubresData )override = 0; + virtual void UpdateTexture( ITexture* pTexture, Uint32 MipLevel, Uint32 Slice, const Box& DstBox, const TextureSubResData& SubresData, + RESOURCE_STATE_TRANSITION_MODE SrcBufferTransitionMode, RESOURCE_STATE_TRANSITION_MODE TextureTransitionMode )override = 0; /// Base implementaiton of IDeviceContext::CopyTexture(); validates input parameters virtual void CopyTexture( ITexture* pSrcTexture, @@ -189,6 +191,14 @@ protected: bool DvpVerifyDrawArguments(const DrawAttribs& drawAttribs); bool DvpVerifyDispatchArguments(const DispatchComputeAttribs &DispatchAttrs); void DvpVerifyStateTransitionDesc(const StateTransitionDesc& Barrier); + bool DvpVerifyTextureState(const TextureImplType& Texture, RESOURCE_STATE RequiredState, const char* OperationName); + bool DvpVerifyBufferState (const BufferImplType& Buffer, RESOURCE_STATE RequiredState, const char* OperationName); +#else +# define DvpVerifyDrawArguments (...)[](){return true;}() +# define DvpVerifyDispatchArguments (...)[](){return true;}() +# define DvpVerifyStateTransitionDesc(...)do{}while(false) +# define DvpVerifyTextureState (...)[](){return true;}() +# define DvpVerifyBufferState (...)[](){return true;}() #endif /// Strong reference to the device. @@ -256,8 +266,8 @@ protected: }; -template -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer** ppBuffers, Uint32* pOffsets, SET_VERTEX_BUFFERS_FLAGS Flags ) { #ifdef DEVELOPMENT @@ -308,14 +318,14 @@ inline void DeviceContextBase{}; } -template -inline void DeviceContextBase :: SetPipelineState(PipelineStateImplType* pPipelineState, int /*Dummy*/) +template +inline void DeviceContextBase :: SetPipelineState(PipelineStateImplType* pPipelineState, int /*Dummy*/) { m_pPipelineState = pPipelineState; } -template -inline bool DeviceContextBase :: +template +inline bool DeviceContextBase :: CommitShaderResources(IShaderResourceBinding* pShaderResourceBinding, COMMIT_SHADER_RESOURCES_FLAGS Flags, int) { #ifdef DEVELOPMENT @@ -337,15 +347,15 @@ inline bool DeviceContextBase -inline void DeviceContextBase :: InvalidateState() +template +inline void DeviceContextBase :: InvalidateState() { - DeviceContextBase :: ClearStateCache(); + DeviceContextBase :: ClearStateCache(); m_IsDefaultFramebufferBound = false; } -template -inline void DeviceContextBase :: SetIndexBuffer( IBuffer* pIndexBuffer, Uint32 ByteOffset ) +template +inline void DeviceContextBase :: SetIndexBuffer( IBuffer* pIndexBuffer, Uint32 ByteOffset ) { m_pIndexBuffer = ValidatedCast(pIndexBuffer); m_IndexDataStartOffset = ByteOffset; @@ -359,8 +369,8 @@ inline void DeviceContextBase -inline void DeviceContextBase :: GetPipelineState(IPipelineState** ppPSO, float* BlendFactors, Uint32& StencilRef) +template +inline void DeviceContextBase :: GetPipelineState(IPipelineState** ppPSO, float* BlendFactors, Uint32& StencilRef) { VERIFY( ppPSO != nullptr, "Null pointer provided null" ); VERIFY(* ppPSO == nullptr, "Memory address contains a pointer to a non-null blend state" ); @@ -378,8 +388,8 @@ inline void DeviceContextBase -inline bool DeviceContextBase ::SetBlendFactors(const float* BlendFactors, int) +template +inline bool DeviceContextBase ::SetBlendFactors(const float* BlendFactors, int) { bool FactorsDiffer = false; for( Uint32 f = 0; f < 4; ++f ) @@ -391,8 +401,8 @@ inline bool DeviceContextBase -inline bool DeviceContextBase :: SetStencilRef(Uint32 StencilRef, int) +template +inline bool DeviceContextBase :: SetStencilRef(Uint32 StencilRef, int) { if (m_StencilRef != StencilRef) { @@ -402,8 +412,8 @@ inline bool DeviceContextBase -inline void DeviceContextBase :: SetViewports( Uint32 NumViewports, const Viewport* pViewports, Uint32& RTWidth, Uint32& RTHeight ) +template +inline void DeviceContextBase :: SetViewports( Uint32 NumViewports, const Viewport* pViewports, Uint32& RTWidth, Uint32& RTHeight ) { if ( RTWidth == 0 || RTHeight == 0 ) { @@ -430,8 +440,8 @@ inline void DeviceContextBase -inline void DeviceContextBase :: GetViewports( Uint32 &NumViewports, Viewport* pViewports ) +template +inline void DeviceContextBase :: GetViewports( Uint32 &NumViewports, Viewport* pViewports ) { NumViewports = m_NumViewports; if ( pViewports ) @@ -441,8 +451,8 @@ inline void DeviceContextBase -inline void DeviceContextBase :: SetScissorRects( Uint32 NumRects, const Rect* pRects, Uint32& RTWidth, Uint32& RTHeight ) +template +inline void DeviceContextBase :: SetScissorRects( Uint32 NumRects, const Rect* pRects, Uint32& RTWidth, Uint32& RTHeight ) { if ( RTWidth == 0 || RTHeight == 0 ) { @@ -461,8 +471,8 @@ inline void DeviceContextBase -inline bool DeviceContextBase :: +template +inline bool DeviceContextBase :: SetRenderTargets( Uint32 NumRenderTargets, ITextureView* ppRenderTargets[], ITextureView* pDepthStencil ) { bool bBindRenderTargets = false; @@ -584,8 +594,8 @@ inline bool DeviceContextBase -inline void DeviceContextBase :: GetRenderTargets( Uint32 &NumRenderTargets, ITextureView** ppRTVs, ITextureView** ppDSV ) +template +inline void DeviceContextBase :: GetRenderTargets( Uint32 &NumRenderTargets, ITextureView** ppRTVs, ITextureView** ppDSV ) { NumRenderTargets = m_NumBoundRenderTargets; @@ -617,8 +627,8 @@ inline void DeviceContextBase -inline void DeviceContextBase :: ClearStateCache() +template +inline void DeviceContextBase :: ClearStateCache() { for(Uint32 stream=0; stream < m_NumVertexStreams; ++stream) m_VertexStreams[stream] = VertexStreamInfo{}; @@ -652,8 +662,8 @@ inline void DeviceContextBase -inline void DeviceContextBase :: ResetRenderTargets() +template +inline void DeviceContextBase :: ResetRenderTargets() { for (Uint32 rt = 0; rt < m_NumBoundRenderTargets; ++rt) m_pBoundRenderTargets[rt].Release(); @@ -673,9 +683,9 @@ inline void DeviceContextBase -inline void DeviceContextBase :: - UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData) +template +inline void DeviceContextBase :: + UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData, RESOURCE_STATE_TRANSITION_MODE StateTransitionMode) { VERIFY(pBuffer != nullptr, "Buffer must not be null"); const auto& BuffDesc = ValidatedCast(pBuffer)->GetDesc(); @@ -684,8 +694,8 @@ inline void DeviceContextBase -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: CopyBuffer(IBuffer *pSrcBuffer, Uint32 SrcOffset, IBuffer *pDstBuffer, Uint32 DstOffset, Uint32 Size) { VERIFY(pSrcBuffer != nullptr, "Source buffer must not be null"); @@ -696,8 +706,8 @@ inline void DeviceContextBase -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: MapBuffer( IBuffer* pBuffer, MAP_TYPE MapType, MAP_FLAGS MapFlags, PVoid& pMappedData ) { VERIFY(pBuffer, "pBuffer must not be null"); @@ -741,24 +751,25 @@ inline void DeviceContextBase -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: UnmapBuffer(IBuffer* pBuffer) { VERIFY(pBuffer, "pBuffer must not be null"); } -template -inline void DeviceContextBase :: - UpdateTexture(ITexture* pTexture, Uint32 MipLevel, Uint32 Slice, const Box& DstBox, const TextureSubResData& SubresData) +template +inline void DeviceContextBase :: + UpdateTexture(ITexture* pTexture, Uint32 MipLevel, Uint32 Slice, const Box& DstBox, const TextureSubResData& SubresData, + RESOURCE_STATE_TRANSITION_MODE SrcBufferTransitionMode, RESOURCE_STATE_TRANSITION_MODE TextureTransitionMode) { VERIFY( pTexture != nullptr, "pTexture must not be null" ); ValidateUpdateTextureParams( pTexture->GetDesc(), MipLevel, Slice, DstBox, SubresData ); } -template -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: CopyTexture( ITexture* pSrcTexture, Uint32 SrcMipLevel, Uint32 SrcSlice, @@ -776,8 +787,8 @@ inline void DeviceContextBaseGetDesc(), DstMipLevel, DstSlice, DstX, DstY, DstZ ); } -template -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: MapTextureSubresource(ITexture* pTexture, Uint32 MipLevel, Uint32 ArraySlice, @@ -790,8 +801,8 @@ inline void DeviceContextBaseGetDesc(), MipLevel, ArraySlice, MapType, MapFlags, pMapRegion); } -template -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: UnmapTextureSubresource(ITexture* pTexture, Uint32 MipLevel, Uint32 ArraySlice) { VERIFY(pTexture, "pTexture must not be null"); @@ -799,8 +810,8 @@ inline void DeviceContextBaseGetDesc().ArraySize, "Array slice is out of range"); } -template -inline void DeviceContextBase :: +template +inline void DeviceContextBase :: GenerateMips(ITextureView* pTexView) { VERIFY(pTexView != nullptr, "pTexView must not be null"); @@ -811,8 +822,8 @@ inline void DeviceContextBase -inline bool DeviceContextBase :: +template +inline bool DeviceContextBase :: DvpVerifyDrawArguments(const DrawAttribs& drawAttribs) { if (!m_pPipelineState) @@ -848,8 +859,8 @@ inline bool DeviceContextBase -inline bool DeviceContextBase :: +template +inline bool DeviceContextBase :: DvpVerifyDispatchArguments(const DispatchComputeAttribs& DispatchAttrs) { if (!m_pPipelineState) @@ -879,8 +890,8 @@ inline bool DeviceContextBase -void DeviceContextBase :: +template +void DeviceContextBase :: DvpVerifyStateTransitionDesc(const StateTransitionDesc& Barrier) { DEV_CHECK_ERR((Barrier.pTexture != nullptr) ^ (Barrier.pBuffer != nullptr), "Exactly one of pTexture or pBuffer members of StateTransitionDesc must not be null"); @@ -925,7 +936,36 @@ void DeviceContextBase +bool DeviceContextBase :: + DvpVerifyTextureState(const TextureImplType& Texture, RESOURCE_STATE RequiredState, const char* OperationName) +{ + if (Texture.IsInKnownState() && !Texture.CheckState(RequiredState)) + { + LOG_ERROR_MESSAGE(OperationName, " requires texture '", Texture.GetDesc().Name, "' to be transitioned to ", GetResourceStateString(RequiredState), " state. " + "Actual texture state: ", GetResourceStateString(Texture.GetState()), ". " + "Use appropriate state transiton flags or explicitly transition the texture using IDeviceContext::TransitionResourceStates() method."); + return false; + } + return true; +} + +template +bool DeviceContextBase :: + DvpVerifyBufferState (const BufferImplType& Buffer, RESOURCE_STATE RequiredState, const char* OperationName) +{ + if (Buffer.IsInKnownState() && !Buffer.CheckState(RequiredState)) + { + LOG_ERROR_MESSAGE(OperationName, " requires buffer '", Buffer.GetDesc().Name, "' to be transitioned to ", GetResourceStateString(RequiredState), " state. " + "Actual buffer state: ", GetResourceStateString(Buffer.GetState()), ". " + "Use appropriate state transiton flags or explicitly transition the buffer using IDeviceContext::TransitionResourceStates() method."); + return false; + } + + return true; +} + +#endif // DEVELOPMENT } diff --git a/Graphics/GraphicsEngine/interface/DeviceContext.h b/Graphics/GraphicsEngine/interface/DeviceContext.h index 3389425f..d1235dbe 100644 --- a/Graphics/GraphicsEngine/interface/DeviceContext.h +++ b/Graphics/GraphicsEngine/interface/DeviceContext.h @@ -312,11 +312,27 @@ enum CLEAR_RENDER_TARGET_STATE_TRANSITION_MODE CLEAR_RENDER_TARGET_TRANSITION_STATE, /// Do not perform transition, but verify that the state is correct. - /// This flag only has effect in debug and development builds. No validation + /// This mode only has effect in debug and development builds. No validation /// is performed in release build. CLEAR_RENDER_TARGET_VERIFY_STATE }; +/// Defines resource state transitions performed by various commands +enum RESOURCE_STATE_TRANSITION_MODE +{ + /// Perform no state transitions + RESOURCE_STATE_TRANSITION_MODE_NONE = 0, + + /// Transition resource to the state required by the command + RESOURCE_STATE_TRANSITION_MODE_TRANSITION, + + /// Do not transition, but verify that the state is correct. + /// No validation is performed if the state is unknown to the engine. + /// This mode only has effect in debug and development builds. No validation + /// is performed in release build. + RESOURCE_STATE_TRANSITION_MODE_VERIFY +}; + /// Describes the viewport. /// This structure is used by IDeviceContext::SetViewports(). @@ -655,11 +671,16 @@ public: /// Updates the data in the buffer - /// \param [in] pBuffer - Pointer to the buffer to updates. - /// \param [in] Offset - Offset in bytes from the beginning of the buffer to the update region. - /// \param [in] Size - Size in bytes of the data region to update. - /// \param [in] pData - Pointer to the data to write to the buffer. - virtual void UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData) = 0; + /// \param [in] pBuffer - Pointer to the buffer to updates. + /// \param [in] Offset - Offset in bytes from the beginning of the buffer to the update region. + /// \param [in] Size - Size in bytes of the data region to update. + /// \param [in] pData - Pointer to the data to write to the buffer. + /// \param [in] StateTransitionMode - Buffer state transition mode (see Diligent::RESOURCE_STATE_TRANSITION_MODE) + virtual void UpdateBuffer(IBuffer* pBuffer, + Uint32 Offset, + Uint32 Size, + const PVoid pData, + RESOURCE_STATE_TRANSITION_MODE StateTransitionMode) = 0; /// Copies the data from one buffer to another @@ -692,12 +713,22 @@ public: /// Updates the data in the texture - /// \param [in] pTexture - Pointer to the device context interface to be used to perform the operation. - /// \param [in] MipLevel - Mip level of the texture subresource to update. - /// \param [in] Slice - Array slice. Should be 0 for non-array textures. - /// \param [in] DstBox - Destination region on the texture to update. - /// \param [in] SubresData - Source data to copy to the texture. - virtual void UpdateTexture(ITexture* pTexture, Uint32 MipLevel, Uint32 Slice, const Box& DstBox, const TextureSubResData& SubresData) = 0; + /// \param [in] pTexture - Pointer to the device context interface to be used to perform the operation. + /// \param [in] MipLevel - Mip level of the texture subresource to update. + /// \param [in] Slice - Array slice. Should be 0 for non-array textures. + /// \param [in] DstBox - Destination region on the texture to update. + /// \param [in] SubresData - Source data to copy to the texture. + /// \param [in] SrcBufferTransitionMode - If pSrcBuffer member of TextureSubResData structure is not null, this + /// parameter defines state transition mode of the source buffer. + /// If pSrcBuffer is null, this parameter is ignored. + /// \param [in] TextureTransitionMode - Texture state transition mode (see Diligent::RESOURCE_STATE_TRANSITION_MODE) + virtual void UpdateTexture(ITexture* pTexture, + Uint32 MipLevel, + Uint32 Slice, + const Box& DstBox, + const TextureSubResData& SubresData, + RESOURCE_STATE_TRANSITION_MODE SrcBufferTransitionMode, + RESOURCE_STATE_TRANSITION_MODE TextureTransitionMode) = 0; /// Copies data from one texture to another -- cgit v1.2.3