diff --git a/Engine/gapi/abstractgraphicsapi.h b/Engine/gapi/abstractgraphicsapi.h index 09571b2c..d647b1cc 100644 --- a/Engine/gapi/abstractgraphicsapi.h +++ b/Engine/gapi/abstractgraphicsapi.h @@ -426,7 +426,7 @@ namespace Tempest { virtual void barrier(const BarrierDesc* desc, size_t cnt) = 0; virtual void generateMipmap(Texture& image, uint32_t texWidth, uint32_t texHeight, uint32_t mipLevels) = 0; - virtual void copy(Buffer& dest, size_t offset, ResourceAccess defLayout, Texture& src, uint32_t width, uint32_t height, uint32_t mip)=0; + virtual void copy(Buffer& dest, size_t offset, Texture& src, uint32_t width, uint32_t height, uint32_t mip) = 0; virtual bool isRecording() const = 0; virtual void begin()=0; diff --git a/Engine/gapi/directx12/dxcommandbuffer.cpp b/Engine/gapi/directx12/dxcommandbuffer.cpp index 99460185..4cae7fe9 100644 --- a/Engine/gapi/directx12/dxcommandbuffer.cpp +++ b/Engine/gapi/directx12/dxcommandbuffer.cpp @@ -248,7 +248,7 @@ struct DxCommandBuffer::MipMaps : Stage { struct DxCommandBuffer::CopyBuf : Stage { CopyBuf(DxDevice& dev, DxBuffer& dst, size_t offset, - const DxTexture& src, size_t width, size_t height, size_t mip) + DxTexture& src, size_t width, size_t height, size_t mip) :dst(dst), offset(offset), src(src), width(width), height(height), mip(int32_t(mip)), desc(*dev.copyLayout.handler) { } @@ -271,10 +271,18 @@ struct DxCommandBuffer::CopyBuf : Stage { impl.SetPipelineState(prog.impl.get()); impl.SetComputeRootSignature(prog.sign.get()); cmd.implSetUniforms(desc,true); - impl.SetComputeRoot32BitConstants(UINT(prog.pushConstantId),UINT(sizeof(push)/4),&push,0); const size_t maxWG = 65535; + + auto& resState = cmd.resState; + resState.setLayout(dst,ResourceAccess::ComputeWrite); + resState.setLayout(src,ResourceAccess::TransferSrc); + resState.flush(cmd); + impl.Dispatch(UINT(std::min(outSize,maxWG)),UINT((outSize+maxWG-1)%maxWG),1u); + + resState.setLayout(dst,ResourceAccess::ComputeRead); + resState.setLayout(src,ResourceAccess::Sampler); // TODO: storage images } DxCompPipeline& shader(DxCommandBuffer& cmd, int32_t& bitCnt, int32_t& compCnt) { @@ -336,7 +344,7 @@ struct DxCommandBuffer::CopyBuf : Stage { DxBuffer& dst; const size_t offset = 0; - const DxTexture& src; + DxTexture& src; const size_t width = 0; const size_t height = 0; const int32_t mip = 0; @@ -589,21 +597,24 @@ void DxCommandBuffer::changeLayout(AbstractGraphicsApi::Texture& t, barrier(&b,1); } -void DxCommandBuffer::copy(AbstractGraphicsApi::Buffer& dstBuf, size_t offset, ResourceAccess defLayout, +void DxCommandBuffer::copy(AbstractGraphicsApi::Buffer& dstBuf, size_t offset, AbstractGraphicsApi::Texture& srcTex, uint32_t width, uint32_t height, uint32_t mip) { - resState.flush(*this); - auto& dst = reinterpret_cast(dstBuf); - auto& src = reinterpret_cast(srcTex); + auto& src = reinterpret_cast(srcTex); const UINT bpp = src.bitCount()/8; const UINT pitchBase = UINT(width)*bpp; const UINT pitch = ((pitchBase+D3D12_TEXTURE_DATA_PITCH_ALIGNMENT-1)/D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)*D3D12_TEXTURE_DATA_PITCH_ALIGNMENT; if(pitch==pitchBase && (offset%D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT)==0) { - changeLayout(srcTex,defLayout,ResourceAccess::TransferSrc,mip); + resState.setLayout(dst,ResourceAccess::TransferDst); + resState.setLayout(src,ResourceAccess::TransferSrc); + resState.flush(*this); + copyNative(dstBuf,offset, srcTex,width,height,mip); - changeLayout(srcTex,ResourceAccess::TransferSrc,defLayout,mip); + + resState.setLayout(dst,ResourceAccess::ComputeRead); + resState.setLayout(src,ResourceAccess::Sampler); // TODO: storage images return; } diff --git a/Engine/gapi/directx12/dxcommandbuffer.h b/Engine/gapi/directx12/dxcommandbuffer.h index 247aa40e..16922234 100644 --- a/Engine/gapi/directx12/dxcommandbuffer.h +++ b/Engine/gapi/directx12/dxcommandbuffer.h @@ -62,7 +62,7 @@ class DxCommandBuffer:public AbstractGraphicsApi::CommandBuffer { void barrier (const AbstractGraphicsApi::BarrierDesc* desc, size_t cnt) override; void changeLayout(AbstractGraphicsApi::Texture& tex, ResourceAccess prev, ResourceAccess next, uint32_t mipId); - void copy(AbstractGraphicsApi::Buffer& dst, size_t offset, ResourceAccess defLayout, AbstractGraphicsApi::Texture& src, uint32_t width, uint32_t height, uint32_t mip) override; + void copy(AbstractGraphicsApi::Buffer& dst, size_t offset, AbstractGraphicsApi::Texture& src, uint32_t width, uint32_t height, uint32_t mip) override; void generateMipmap(AbstractGraphicsApi::Texture& image, uint32_t texWidth, uint32_t texHeight, uint32_t mipLevels) override; void copyNative(AbstractGraphicsApi::Buffer& dest, size_t offset, const AbstractGraphicsApi::Texture& src, uint32_t width, uint32_t height, uint32_t mip); diff --git a/Engine/gapi/resourcestate.h b/Engine/gapi/resourcestate.h index 5d67e8a7..eec42010 100644 --- a/Engine/gapi/resourcestate.h +++ b/Engine/gapi/resourcestate.h @@ -16,7 +16,7 @@ class ResourceState { AbstractGraphicsApi::Texture** att, AbstractGraphicsApi::Swapchain** sw, const uint32_t* imgId); void setLayout (AbstractGraphicsApi::Swapchain& s, uint32_t id, ResourceAccess lay, bool discard); - void setLayout (AbstractGraphicsApi::Texture& a, ResourceAccess lay, bool discard); + void setLayout (AbstractGraphicsApi::Texture& a, ResourceAccess lay, bool discard = false); void setLayout (AbstractGraphicsApi::Buffer& b, ResourceAccess lay); void flush (AbstractGraphicsApi::CommandBuffer& cmd); diff --git a/Engine/gapi/vulkan/vcommandbuffer.cpp b/Engine/gapi/vulkan/vcommandbuffer.cpp index a0af320d..38368005 100644 --- a/Engine/gapi/vulkan/vcommandbuffer.cpp +++ b/Engine/gapi/vulkan/vcommandbuffer.cpp @@ -435,15 +435,16 @@ void VCommandBuffer::blit(AbstractGraphicsApi::Texture& srcTex, uint32_t srcW, u filter); } -void VCommandBuffer::copy(AbstractGraphicsApi::Buffer& dest, size_t offset, - ResourceAccess defLayout, +void VCommandBuffer::copy(AbstractGraphicsApi::Buffer& dst, size_t offset, AbstractGraphicsApi::Texture& src, uint32_t width, uint32_t height, uint32_t mip) { - // resState.setLayout(src,TextureLayout::TransferSrc,true); // TODO: more advanced layout tracker + resState.setLayout(dst,ResourceAccess::TransferDst); + resState.setLayout(src,ResourceAccess::TransferSrc); resState.flush(*this); - changeLayout(src,defLayout,ResourceAccess::TransferSrc,mip); - copyNative(dest,offset, src,width,height,mip); - changeLayout(src,ResourceAccess::TransferSrc,defLayout,mip); + copyNative(dst,offset, src,width,height,mip); + + resState.setLayout(dst,ResourceAccess::ComputeRead); + resState.setLayout(src,ResourceAccess::Sampler); // TODO: storage images } void VCommandBuffer::changeLayout(AbstractGraphicsApi::Texture& t, @@ -474,7 +475,7 @@ void VCommandBuffer::generateMipmap(AbstractGraphicsApi::Texture& img, int32_t w = int32_t(texWidth); int32_t h = int32_t(texHeight); - resState.setLayout(img,ResourceAccess::TransferDst,false); + resState.setLayout(img,ResourceAccess::TransferDst); resState.flush(*this); for(uint32_t i=1; i::copy(const Attachment& src, uint32_t mip, StorageBu throw std::system_error(Tempest::GraphicsErrc::InvalidStorageBuffer); uint32_t w = src.w(), h = src.h(); auto& tx = *textureCast(src).impl.handler; - impl->copy(*dest.impl.impl.handler,offset,ResourceAccess::Sampler, - tx,w,h,mip); + impl->copy(*dest.impl.impl.handler,offset,tx,w,h,mip); } void Encoder::copy(const Texture2d& src, uint32_t mip, StorageBuffer& dest, size_t offset) { @@ -214,8 +213,7 @@ void Encoder::copy(const Texture2d& src, uint32_t mip, StorageBuf throw std::system_error(Tempest::GraphicsErrc::InvalidStorageBuffer); uint32_t w = src.w(), h = src.h(); auto& tx = *src.impl.handler; - impl->copy(*dest.impl.impl.handler,offset,ResourceAccess::Sampler, - tx,w,h,mip); + impl->copy(*dest.impl.impl.handler,offset,tx,w,h,mip); } void Encoder::generateMipmaps(Attachment& tex) {