Skip to content

Commit

Permalink
transfer barriers in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Nov 9, 2021
1 parent 2feb372 commit b0ab1ee
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Engine/gapi/abstractgraphicsapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 20 additions & 9 deletions Engine/gapi/directx12/dxcommandbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<DxBuffer&>(dstBuf);
auto& src = reinterpret_cast<const DxTexture&>(srcTex);
auto& src = reinterpret_cast<DxTexture&>(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;
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/gapi/directx12/dxcommandbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Engine/gapi/resourcestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions Engine/gapi/vulkan/vcommandbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<mipLevels; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion Engine/gapi/vulkan/vcommandbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class VCommandBuffer:public AbstractGraphicsApi::CommandBuffer {

void changeLayout(AbstractGraphicsApi::Texture& tex, ResourceAccess prev, ResourceAccess next, uint32_t mipId);

void copy(AbstractGraphicsApi::Buffer& dest, 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 copy(AbstractGraphicsApi::Texture& dest, size_t width, size_t height, size_t mip, const AbstractGraphicsApi::Buffer& src, size_t offset);
Expand Down
6 changes: 2 additions & 4 deletions Engine/graphics/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,15 @@ void Encoder<CommandBuffer>::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<CommandBuffer>::copy(const Texture2d& src, uint32_t mip, StorageBuffer& dest, size_t offset) {
if(offset%4!=0)
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<CommandBuffer>::generateMipmaps(Attachment& tex) {
Expand Down

0 comments on commit b0ab1ee

Please sign in to comment.