From 61bfee05db60a67fb8be6e72b27ba6724f0dd277 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 24 Nov 2018 14:11:02 -0800 Subject: Moved/renamed `IBuffer::UpdateData()` to `IDeviceContext::UpdateBuffer()` Moved/renamed `IBuffer::CopyData()` to `IDeviceContext::CopyBuffer()` --- Graphics/GraphicsEngine/include/BufferBase.h | 21 ------------- .../GraphicsEngine/include/DeviceContextBase.h | 31 ++++++++++++++++++- Graphics/GraphicsEngine/interface/Buffer.h | 20 +------------ Graphics/GraphicsEngine/interface/DeviceContext.h | 20 +++++++++++++ .../GraphicsEngineD3D11/include/BufferD3D11Impl.h | 2 -- .../include/DeviceContextD3D11Impl.h | 4 +++ .../GraphicsEngineD3D11/src/BufferD3D11Impl.cpp | 34 --------------------- .../src/DeviceContextD3D11Impl.cpp | 35 ++++++++++++++++++++++ .../GraphicsEngineD3D12/include/BufferD3D12Impl.h | 2 -- .../include/DeviceContextD3D12Impl.h | 4 +++ .../GraphicsEngineD3D12/src/BufferD3D12Impl.cpp | 16 ---------- .../src/DeviceContextD3D12Impl.cpp | 23 +++++++++----- .../GraphicsEngineOpenGL/include/BufferGLImpl.h | 5 ++-- .../include/DeviceContextGLImpl.h | 4 +++ Graphics/GraphicsEngineOpenGL/src/BufferGLImpl.cpp | 22 +++++--------- .../src/DeviceContextGLImpl.cpp | 17 +++++++++++ .../GraphicsEngineVulkan/include/BufferVkImpl.h | 2 -- .../include/DeviceContextVkImpl.h | 4 +++ Graphics/GraphicsEngineVulkan/src/BufferVkImpl.cpp | 16 ---------- .../src/DeviceContextVkImpl.cpp | 24 ++++++++++----- 20 files changed, 162 insertions(+), 144 deletions(-) (limited to 'Graphics') diff --git a/Graphics/GraphicsEngine/include/BufferBase.h b/Graphics/GraphicsEngine/include/BufferBase.h index 93f752ef..f2414c82 100644 --- a/Graphics/GraphicsEngine/include/BufferBase.h +++ b/Graphics/GraphicsEngine/include/BufferBase.h @@ -113,12 +113,6 @@ public: IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_Buffer, TDeviceObjectBase ) - /// Base implementation of IBuffer::UpdateData(); validates input parameters. - virtual void UpdateData( IDeviceContext* pContext, Uint32 Offset, Uint32 Size, const PVoid pData )override = 0; - - /// Base implementation of IBuffer::CopyData(); validates input parameters. - virtual void CopyData( IDeviceContext* pContext, IBuffer* pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size )override = 0; - /// Base implementation of IBuffer::Map(); validates input parameters. virtual void Map( IDeviceContext* pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid& pMappedData )override; @@ -184,21 +178,6 @@ protected: std::unique_ptr > m_pDefaultSRV; }; -template -void BufferBase :: UpdateData( IDeviceContext* pContext, Uint32 Offset, Uint32 Size, const PVoid pData ) -{ - VERIFY_BUFFER( this->m_Desc.Usage == USAGE_DEFAULT, "Only default usage buffers can be updated with UpdateData()" ); - VERIFY_BUFFER( Offset < this->m_Desc.uiSizeInBytes, "Offset (", Offset, ") exceeds the buffer size (", this->m_Desc.uiSizeInBytes, ")" ); - VERIFY_BUFFER( Size + Offset <= this->m_Desc.uiSizeInBytes, "Update region [", Offset, ",", Size + Offset, ") is out of buffer bounds [0,",this->m_Desc.uiSizeInBytes,")" ); -} - -template -void BufferBase :: CopyData( IDeviceContext* pContext, IBuffer* pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size ) -{ - VERIFY_BUFFER( DstOffset + Size <= this->m_Desc.uiSizeInBytes, "Destination range [", DstOffset, ",", DstOffset + Size, ") is out of buffer bounds [0,",this->m_Desc.uiSizeInBytes,")" ); - VERIFY_BUFFER( SrcOffset + Size <= pSrcBuffer->GetDesc().uiSizeInBytes, "Source range [", SrcOffset, ",", SrcOffset + Size, ") is out of buffer bounds [0,",this->m_Desc.uiSizeInBytes,")" ); -} - template void BufferBase :: Map( IDeviceContext* pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid& pMappedData ) diff --git a/Graphics/GraphicsEngine/include/DeviceContextBase.h b/Graphics/GraphicsEngine/include/DeviceContextBase.h index 23861605..d3afa0f3 100644 --- a/Graphics/GraphicsEngine/include/DeviceContextBase.h +++ b/Graphics/GraphicsEngine/include/DeviceContextBase.h @@ -108,6 +108,12 @@ public: /// from the cached value and false otherwise. inline bool SetRenderTargets( Uint32 NumRenderTargets, ITextureView* ppRenderTargets[], ITextureView* pDepthStencil, Uint32 Dummy = 0 ); + /// Base implementation of IDeviceContext::UpdateBuffer(); validates input parameters. + virtual void UpdateBuffer(IBuffer *pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData)override = 0; + + /// Base implementation of IDeviceContext::CopyBuffer(); validates input parameters. + virtual void CopyBuffer(IBuffer *pSrcBuffer, IBuffer *pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size)override = 0; + /// Sets the strong pointer to the swap chain virtual void SetSwapChain( ISwapChain* pSwapChain )override final { m_pSwapChain = pSwapChain; } @@ -627,6 +633,29 @@ inline void DeviceContextBase +inline void DeviceContextBase :: UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData) +{ + VERIFY(pBuffer != nullptr, "Buffer must not be null"); + const auto& BuffDesc = ValidatedCast(pBuffer)->GetDesc(); + DEV_CHECK_ERR(BuffDesc.Usage == USAGE_DEFAULT, "Unable to update buffer '", BuffDesc.Name, "': only USAGE_DEFAULT buffers can be updated with UpdateData()"); + DEV_CHECK_ERR(Offset < BuffDesc.uiSizeInBytes, "Unable to update buffer '", BuffDesc.Name, "': offset (", Offset, ") exceeds the buffer size (", BuffDesc.uiSizeInBytes, ")" ); + DEV_CHECK_ERR(Size + Offset <= BuffDesc.uiSizeInBytes, "Unable to update buffer '", BuffDesc.Name, "': Update region [", Offset, ",", Size + Offset, ") is out of buffer bounds [0,", BuffDesc.uiSizeInBytes, ")" ); +} + +template +inline void DeviceContextBase :: CopyBuffer(IBuffer *pSrcBuffer, IBuffer *pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) +{ + VERIFY(pSrcBuffer != nullptr, "Source buffer must not be null"); + VERIFY(pDstBuffer != nullptr, "Destination buffer must not be null"); + const auto& SrcBufferDesc = ValidatedCast(pSrcBuffer)->GetDesc(); + const auto& DstBufferDesc = ValidatedCast(pDstBuffer)->GetDesc(); + DEV_CHECK_ERR( DstOffset + Size <= DstBufferDesc.uiSizeInBytes, "Failed to copy buffer '", SrcBufferDesc.Name, "' to '", DstBufferDesc.Name, "': Destination range [", DstOffset, ",", DstOffset + Size, ") is out of buffer bounds [0,", DstBufferDesc.uiSizeInBytes, ")" ); + DEV_CHECK_ERR( SrcOffset + Size <= SrcBufferDesc.uiSizeInBytes, "Failed to copy buffer '", SrcBufferDesc.Name, "' to '", DstBufferDesc.Name, "': Source range [", SrcOffset, ",", SrcOffset + Size, ") is out of buffer bounds [0,", SrcBufferDesc.uiSizeInBytes, ")" ); +} + + #ifdef DEVELOPMENT template inline bool DeviceContextBase :: DvpVerifyDrawArguments(const DrawAttribs& drawAttribs) @@ -739,7 +768,7 @@ void DeviceContextBase(pContext)->GetD3D11DeviceContext(); - - D3D11_BOX DstBox; - DstBox.left = Offset; - DstBox.right = Offset + Size; - DstBox.top = 0; - DstBox.bottom = 1; - DstBox.front = 0; - DstBox.back = 1; - auto *pDstBox = (Offset == 0 && Size == m_Desc.uiSizeInBytes) ? nullptr : &DstBox; - pd3d11DeviceContext->UpdateSubresource(m_pd3d11Buffer, 0, pDstBox, pData, 0, 0); -} - -void BufferD3D11Impl :: CopyData(IDeviceContext *pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) -{ - TBufferBase::CopyData( pContext, pSrcBuffer, SrcOffset, DstOffset, Size ); - - auto *pd3d11DeviceContext = static_cast(pContext)->GetD3D11DeviceContext(); - auto *pSrBufferD3D11Impl = static_cast( pSrcBuffer ); - - D3D11_BOX SrcBox; - SrcBox.left = SrcOffset; - SrcBox.right = SrcOffset + Size; - SrcBox.top = 0; - SrcBox.bottom = 1; - SrcBox.front = 0; - SrcBox.back = 1; - pd3d11DeviceContext->CopySubresourceRegion(m_pd3d11Buffer, 0, DstOffset, 0, 0, pSrBufferD3D11Impl->m_pd3d11Buffer, 0, &SrcBox); -} - void BufferD3D11Impl :: Map(IDeviceContext* pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid& pMappedData) { TBufferBase::Map( pContext, MapType, MapFlags, pMappedData ); diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 241c7b20..a3fabea1 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -938,6 +938,41 @@ namespace Diligent m_pd3d11DeviceContext->Flush(); } + void DeviceContextD3D11Impl::UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData) + { + TDeviceContextBase::UpdateBuffer(pBuffer, Offset, Size, pData); + + auto* pBufferD3D11Impl = ValidatedCast( pBuffer ); + + D3D11_BOX DstBox; + DstBox.left = Offset; + DstBox.right = Offset + Size; + DstBox.top = 0; + DstBox.bottom = 1; + DstBox.front = 0; + DstBox.back = 1; + auto* pDstBox = (Offset == 0 && Size == pBufferD3D11Impl->GetDesc().uiSizeInBytes) ? nullptr : &DstBox; + m_pd3d11DeviceContext->UpdateSubresource(pBufferD3D11Impl->m_pd3d11Buffer, 0, pDstBox, pData, 0, 0); + } + + void DeviceContextD3D11Impl::CopyBuffer(IBuffer *pSrcBuffer, IBuffer *pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) + { + TDeviceContextBase::CopyBuffer(pSrcBuffer, pDstBuffer, SrcOffset, DstOffset, Size); + + auto* pSrcBufferD3D11Impl = ValidatedCast( pSrcBuffer ); + auto* pDstBufferD3D11Impl = ValidatedCast( pDstBuffer ); + + D3D11_BOX SrcBox; + SrcBox.left = SrcOffset; + SrcBox.right = SrcOffset + Size; + SrcBox.top = 0; + SrcBox.bottom = 1; + SrcBox.front = 0; + SrcBox.back = 1; + m_pd3d11DeviceContext->CopySubresourceRegion(pDstBufferD3D11Impl->m_pd3d11Buffer, 0, DstOffset, 0, 0, pSrcBufferD3D11Impl->m_pd3d11Buffer, 0, &SrcBox); + } + + void DeviceContextD3D11Impl::FinishFrame() { } diff --git a/Graphics/GraphicsEngineD3D12/include/BufferD3D12Impl.h b/Graphics/GraphicsEngineD3D12/include/BufferD3D12Impl.h index de757995..60bb6907 100644 --- a/Graphics/GraphicsEngineD3D12/include/BufferD3D12Impl.h +++ b/Graphics/GraphicsEngineD3D12/include/BufferD3D12Impl.h @@ -60,8 +60,6 @@ public: virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override; - virtual void UpdateData( IDeviceContext *pContext, Uint32 Offset, Uint32 Size, const PVoid pData )override; - virtual void CopyData( IDeviceContext *pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size )override; virtual void Map( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData )override; virtual void Unmap( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags )override; diff --git a/Graphics/GraphicsEngineD3D12/include/DeviceContextD3D12Impl.h b/Graphics/GraphicsEngineD3D12/include/DeviceContextD3D12Impl.h index f0bc93b5..eb3f4112 100644 --- a/Graphics/GraphicsEngineD3D12/include/DeviceContextD3D12Impl.h +++ b/Graphics/GraphicsEngineD3D12/include/DeviceContextD3D12Impl.h @@ -88,6 +88,10 @@ public: virtual void Flush()override final; + virtual void UpdateBuffer(IBuffer *pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData)override final; + + virtual void CopyBuffer(IBuffer *pSrcBuffer, IBuffer *pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size)override final; + virtual void FinishFrame()override final; virtual void TransitionResourceStates(Uint32 BarrierCount, StateTransitionDesc* pResourceBarriers)override final; diff --git a/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp index 53bb1b36..82a94489 100644 --- a/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/BufferD3D12Impl.cpp @@ -304,22 +304,6 @@ BufferD3D12Impl :: ~BufferD3D12Impl() IMPLEMENT_QUERY_INTERFACE( BufferD3D12Impl, IID_BufferD3D12, TBufferBase ) -void BufferD3D12Impl::UpdateData( IDeviceContext* pContext, Uint32 Offset, Uint32 Size, const PVoid pData ) -{ - TBufferBase::UpdateData( pContext, Offset, Size, pData ); - - // We must use cmd context from the device context provided, otherwise there will - // be resource barrier issues in the cmd list in the device context - auto *pDeviceContextD3D12 = ValidatedCast(pContext); - pDeviceContextD3D12->UpdateBufferRegion(this, pData, Offset, Size); -} - -void BufferD3D12Impl :: CopyData(IDeviceContext* pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) -{ - TBufferBase::CopyData( pContext, pSrcBuffer, SrcOffset, DstOffset, Size ); - auto *pDeviceContextD3D12 = ValidatedCast(pContext); - pDeviceContextD3D12->CopyBufferRegion(ValidatedCast(pSrcBuffer), this, SrcOffset, DstOffset, Size); -} void BufferD3D12Impl :: Map(IDeviceContext* pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData) { diff --git a/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp index ef8bf180..2afce274 100644 --- a/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp @@ -898,18 +898,27 @@ namespace Diligent ++m_State.NumCommands; } - void DeviceContextD3D12Impl::UpdateBufferRegion(BufferD3D12Impl* pBuffD3D12, const void* pData, Uint64 DstOffset, Uint64 NumBytes) + void DeviceContextD3D12Impl::UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData) { + TDeviceContextBase::UpdateBuffer(pBuffer, Offset, Size, pData); + + // We must use cmd context from the device context provided, otherwise there will + // be resource barrier issues in the cmd list in the device context + auto* pBuffD3D12 = ValidatedCast(pBuffer); VERIFY(pBuffD3D12->GetDesc().Usage != USAGE_DYNAMIC, "Dynamic buffers must be updated via Map()"); - VERIFY_EXPR( static_cast(NumBytes) == NumBytes ); constexpr size_t DefaultAlginment = 16; - auto TmpSpace = m_DynamicHeap.Allocate(static_cast(NumBytes), DefaultAlginment, m_ContextFrameNumber); - memcpy(TmpSpace.CPUAddress, pData, static_cast(NumBytes)); - UpdateBufferRegion(pBuffD3D12, TmpSpace, DstOffset, NumBytes); + auto TmpSpace = m_DynamicHeap.Allocate(Size, DefaultAlginment, m_ContextFrameNumber); + memcpy(TmpSpace.CPUAddress, pData, Size); + UpdateBufferRegion(pBuffD3D12, TmpSpace, Offset, Size); } - void DeviceContextD3D12Impl::CopyBufferRegion(BufferD3D12Impl* pSrcBuffD3D12, BufferD3D12Impl* pDstBuffD3D12, Uint64 SrcOffset, Uint64 DstOffset, Uint64 NumBytes) + void DeviceContextD3D12Impl::CopyBuffer(IBuffer* pSrcBuffer, IBuffer* pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) { + TDeviceContextBase::CopyBuffer(pSrcBuffer, pDstBuffer, SrcOffset, DstOffset, Size); + + auto* pSrcBuffD3D12 = ValidatedCast(pSrcBuffer); + auto* pDstBuffD3D12 = ValidatedCast(pDstBuffer); + VERIFY(pDstBuffD3D12->GetDesc().Usage != USAGE_DYNAMIC, "Dynamic buffers cannot be copy destinations"); auto& CmdCtx = GetCmdContext(); @@ -924,7 +933,7 @@ namespace Diligent size_t SrcDataStartByteOffset; auto* pd3d12SrcBuff = pSrcBuffD3D12->GetD3D12Buffer(SrcDataStartByteOffset, this); CmdCtx.FlushResourceBarriers(); - CmdCtx.GetCommandList()->CopyBufferRegion( pd3d12DstBuff, DstOffset + DstDataStartByteOffset, pd3d12SrcBuff, SrcOffset+SrcDataStartByteOffset, NumBytes); + CmdCtx.GetCommandList()->CopyBufferRegion( pd3d12DstBuff, DstOffset + DstDataStartByteOffset, pd3d12SrcBuff, SrcOffset+SrcDataStartByteOffset, Size); ++m_State.NumCommands; } diff --git a/Graphics/GraphicsEngineOpenGL/include/BufferGLImpl.h b/Graphics/GraphicsEngineOpenGL/include/BufferGLImpl.h index 1d990493..9609eadc 100644 --- a/Graphics/GraphicsEngineOpenGL/include/BufferGLImpl.h +++ b/Graphics/GraphicsEngineOpenGL/include/BufferGLImpl.h @@ -30,6 +30,7 @@ #include "BaseInterfacesGL.h" #include "BufferViewGLImpl.h" #include "RenderDeviceGLImpl.h" +#include "GLContextState.h" namespace Diligent { @@ -59,8 +60,8 @@ public: /// Queries the specific interface, see IObject::QueryInterface() for details virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override; - virtual void UpdateData(IDeviceContext *pContext, Uint32 Offset, Uint32 Size, const PVoid pData)override; - virtual void CopyData( IDeviceContext *pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size )override; + void UpdateData(GLContextState& CtxState, Uint32 Offset, Uint32 Size, const PVoid pData); + void CopyData(GLContextState& CtxState, BufferGLImpl& SrcBufferGL, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size); virtual void Map( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData )override; virtual void Unmap( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags )override; diff --git a/Graphics/GraphicsEngineOpenGL/include/DeviceContextGLImpl.h b/Graphics/GraphicsEngineOpenGL/include/DeviceContextGLImpl.h index 6727bfb2..c88e8410 100644 --- a/Graphics/GraphicsEngineOpenGL/include/DeviceContextGLImpl.h +++ b/Graphics/GraphicsEngineOpenGL/include/DeviceContextGLImpl.h @@ -78,6 +78,10 @@ public: virtual void Flush()override final; + virtual void UpdateBuffer(IBuffer *pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData)override final; + + virtual void CopyBuffer(IBuffer *pSrcBuffer, IBuffer *pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size)override final; + virtual void FinishFrame()override final; virtual void TransitionResourceStates(Uint32 BarrierCount, StateTransitionDesc* pResourceBarriers)override final; diff --git a/Graphics/GraphicsEngineOpenGL/src/BufferGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/BufferGLImpl.cpp index ff2d46de..ed9cfed5 100644 --- a/Graphics/GraphicsEngineOpenGL/src/BufferGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/BufferGLImpl.cpp @@ -184,18 +184,14 @@ BufferGLImpl::~BufferGLImpl() IMPLEMENT_QUERY_INTERFACE( BufferGLImpl, IID_BufferGL, TBufferBase ) -void BufferGLImpl :: UpdateData(IDeviceContext *pContext, Uint32 Offset, Uint32 Size, const PVoid pData) +void BufferGLImpl :: UpdateData(GLContextState& CtxState, Uint32 Offset, Uint32 Size, const PVoid pData) { - TBufferBase::UpdateData( pContext, Offset, Size, pData ); - - auto *pDeviceContextGL = ValidatedCast(pContext); - BufferMemoryBarrier( GL_BUFFER_UPDATE_BARRIER_BIT,// Reads or writes to buffer objects via any OpenGL API functions that allow // modifying their contents will reflect data written by shaders prior to the barrier. // Additionally, writes via these commands issued after the barrier will wait on // the completion of any shader writes to the same memory initiated prior to the barrier. - pDeviceContextGL->GetContextState()); + CtxState); glBindBuffer(GL_ARRAY_BUFFER, m_GlBuffer); // All buffer bind targets (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER etc.) relate to the same @@ -206,21 +202,17 @@ void BufferGLImpl :: UpdateData(IDeviceContext *pContext, Uint32 Offset, Uint32 } -void BufferGLImpl :: CopyData(IDeviceContext *pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) +void BufferGLImpl :: CopyData(GLContextState& CtxState, BufferGLImpl& SrcBufferGL, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) { - TBufferBase::CopyData( pContext, pSrcBuffer, SrcOffset, DstOffset, Size ); - - auto *pDeviceContextGL = ValidatedCast(pContext); - auto *pSrcBufferGL = static_cast( pSrcBuffer ); BufferMemoryBarrier( GL_BUFFER_UPDATE_BARRIER_BIT,// Reads or writes to buffer objects via any OpenGL API functions that allow // modifying their contents will reflect data written by shaders prior to the barrier. // Additionally, writes via these commands issued after the barrier will wait on // the completion of any shader writes to the same memory initiated prior to the barrier. - pDeviceContextGL->GetContextState()); - pSrcBufferGL->BufferMemoryBarrier( + CtxState); + SrcBufferGL.BufferMemoryBarrier( GL_BUFFER_UPDATE_BARRIER_BIT, - pDeviceContextGL->GetContextState() ); + CtxState); // Whilst glCopyBufferSubData() can be used to copy data between buffers bound to any two targets, // the targets GL_COPY_READ_BUFFER and GL_COPY_WRITE_BUFFER are provided specifically for this purpose. @@ -228,7 +220,7 @@ void BufferGLImpl :: CopyData(IDeviceContext *pContext, IBuffer *pSrcBuffer, Uin // the purposes of copying or staging data without disturbing OpenGL state or needing to keep track of // what was bound to the target before your copy. glBindBuffer(GL_COPY_WRITE_BUFFER, m_GlBuffer); - glBindBuffer(GL_COPY_READ_BUFFER, pSrcBufferGL->m_GlBuffer); + glBindBuffer(GL_COPY_READ_BUFFER, SrcBufferGL.m_GlBuffer); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, SrcOffset, DstOffset, Size); CHECK_GL_ERROR("glCopyBufferSubData() failed"); glBindBuffer(GL_COPY_READ_BUFFER, 0); diff --git a/Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp index 946f8ee9..57c684ff 100644 --- a/Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp @@ -1011,6 +1011,23 @@ namespace Diligent return true; } + void DeviceContextGLImpl::UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData) + { + TDeviceContextBase::UpdateBuffer(pBuffer, Offset, Size, pData); + + auto* pBufferGL = ValidatedCast(pBuffer); + pBufferGL->UpdateData(m_ContextState, Offset, Size, pData); + } + + void DeviceContextGLImpl::CopyBuffer(IBuffer *pSrcBuffer, IBuffer *pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) + { + TDeviceContextBase::CopyBuffer(pSrcBuffer, pDstBuffer, SrcOffset, DstOffset, Size); + + auto* pSrcBufferGL = ValidatedCast(pSrcBuffer); + auto* pDstBufferGL = ValidatedCast(pDstBuffer); + pDstBufferGL->CopyData(m_ContextState, *pSrcBufferGL, SrcOffset, DstOffset, Size); + } + void DeviceContextGLImpl::TransitionResourceStates(Uint32 BarrierCount, StateTransitionDesc* pResourceBarriers) { diff --git a/Graphics/GraphicsEngineVulkan/include/BufferVkImpl.h b/Graphics/GraphicsEngineVulkan/include/BufferVkImpl.h index 9e888bec..ef93d90c 100644 --- a/Graphics/GraphicsEngineVulkan/include/BufferVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/BufferVkImpl.h @@ -65,8 +65,6 @@ public: virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject** ppInterface )override; - virtual void UpdateData( IDeviceContext* pContext, Uint32 Offset, Uint32 Size, const PVoid pData )override; - virtual void CopyData( IDeviceContext* pContext, IBuffer* pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size )override; virtual void Map( IDeviceContext* pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid& pMappedData )override; virtual void Unmap( IDeviceContext* pContext, MAP_TYPE MapType, Uint32 MapFlags )override; diff --git a/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h b/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h index 23d10485..c2176442 100644 --- a/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/DeviceContextVkImpl.h @@ -95,6 +95,10 @@ public: virtual void Flush()override final; + virtual void UpdateBuffer(IBuffer *pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData)override final; + + virtual void CopyBuffer(IBuffer *pSrcBuffer, IBuffer *pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size)override final; + virtual void FinishCommandList(class ICommandList** ppCommandList)override final; virtual void ExecuteCommandList(class ICommandList* pCommandList)override final; diff --git a/Graphics/GraphicsEngineVulkan/src/BufferVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/BufferVkImpl.cpp index c31ac7c3..cb0fc958 100644 --- a/Graphics/GraphicsEngineVulkan/src/BufferVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/BufferVkImpl.cpp @@ -303,22 +303,6 @@ BufferVkImpl :: ~BufferVkImpl() IMPLEMENT_QUERY_INTERFACE( BufferVkImpl, IID_BufferVk, TBufferBase ) -void BufferVkImpl::UpdateData( IDeviceContext *pContext, Uint32 Offset, Uint32 Size, const PVoid pData ) -{ - TBufferBase::UpdateData( pContext, Offset, Size, pData ); - - // We must use cmd context from the device context provided, otherwise there will - // be resource barrier issues in the cmd list in the device context - auto *pDeviceContextVk = ValidatedCast(pContext); - pDeviceContextVk->UpdateBufferRegion(this, pData, Offset, Size); -} - -void BufferVkImpl :: CopyData(IDeviceContext* pContext, IBuffer* pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) -{ - TBufferBase::CopyData( pContext, pSrcBuffer, SrcOffset, DstOffset, Size ); - auto *pDeviceContextVk = ValidatedCast(pContext); - pDeviceContextVk->CopyBufferRegion(ValidatedCast(pSrcBuffer), this, SrcOffset, DstOffset, Size); -} void BufferVkImpl :: Map(IDeviceContext* pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid& pMappedData) { diff --git a/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp index e8316daf..3b2d961a 100644 --- a/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp @@ -1212,8 +1212,14 @@ namespace Diligent ++m_State.NumCommands; } - void DeviceContextVkImpl::UpdateBufferRegion(BufferVkImpl *pBuffVk, const void *pData, Uint64 DstOffset, Uint64 NumBytes) + void DeviceContextVkImpl::UpdateBuffer(IBuffer* pBuffer, Uint32 Offset, Uint32 Size, const PVoid pData) { + TDeviceContextBase::UpdateBuffer(pBuffer, Offset, Size, pData); + + // We must use cmd context from the device context provided, otherwise there will + // be resource barrier issues in the cmd list in the device context + auto* pBuffVk = ValidatedCast(pBuffer); + #ifdef DEVELOPMENT if (pBuffVk->GetDesc().Usage == USAGE_DYNAMIC) { @@ -1222,18 +1228,22 @@ namespace Diligent } #endif - VERIFY_EXPR( static_cast(NumBytes) == NumBytes ); constexpr size_t Alignment = 4; // Source buffer offset must be multiple of 4 (18.4) - auto TmpSpace = m_UploadHeap.Allocate(static_cast(NumBytes), Alignment); - memcpy(TmpSpace.CPUAddress, pData, static_cast(NumBytes)); - UpdateBufferRegion(pBuffVk, DstOffset, NumBytes, TmpSpace.vkBuffer, TmpSpace.AlignedOffset); + auto TmpSpace = m_UploadHeap.Allocate(Size, Alignment); + memcpy(TmpSpace.CPUAddress, pData, Size); + UpdateBufferRegion(pBuffVk, Offset, Size, TmpSpace.vkBuffer, TmpSpace.AlignedOffset); // The allocation will stay in the upload heap until the end of the frame at which point all upload // pages will be discarded } - void DeviceContextVkImpl::CopyBufferRegion(BufferVkImpl *pSrcBuffVk, BufferVkImpl *pDstBuffVk, Uint64 SrcOffset, Uint64 DstOffset, Uint64 NumBytes) + void DeviceContextVkImpl::CopyBuffer(IBuffer* pSrcBuffer, IBuffer* pDstBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) { + TDeviceContextBase::CopyBuffer(pSrcBuffer, pDstBuffer, SrcOffset, DstOffset, Size); + + auto *pSrcBuffVk = ValidatedCast(pSrcBuffer); + auto *pDstBuffVk = ValidatedCast(pDstBuffer); + #ifdef DEVELOPMENT if (pDstBuffVk->GetDesc().Usage == USAGE_DYNAMIC) { @@ -1263,7 +1273,7 @@ namespace Diligent VkBufferCopy CopyRegion; CopyRegion.srcOffset = SrcOffset + pSrcBuffVk->GetDynamicOffset(m_ContextId, this); CopyRegion.dstOffset = DstOffset; - CopyRegion.size = NumBytes; + CopyRegion.size = Size; VERIFY(pDstBuffVk->m_VulkanBuffer != VK_NULL_HANDLE, "Copy destination buffer must not be suballocated"); VERIFY_EXPR(pDstBuffVk->GetDynamicOffset(m_ContextId, this) == 0); m_CommandBuffer.CopyBuffer(pSrcBuffVk->GetVkBuffer(), pDstBuffVk->GetVkBuffer(), 1, &CopyRegion); -- cgit v1.2.3