diff --git a/src/dawn_native/CommandEncoder.cpp b/src/dawn_native/CommandEncoder.cpp index f9a805e185..f2d42691ba 100644 --- a/src/dawn_native/CommandEncoder.cpp +++ b/src/dawn_native/CommandEncoder.cpp @@ -62,19 +62,22 @@ namespace dawn_native { return {}; } - bool FitsInBuffer(const BufferBase* buffer, uint64_t offset, uint64_t size) { + MaybeError ValidateCopySizeFitsInBuffer(const Ref& buffer, + uint64_t offset, + uint64_t size) { uint64_t bufferSize = buffer->GetSize(); - return offset <= bufferSize && (size <= (bufferSize - offset)); - } - - MaybeError ValidateCopySizeFitsInBuffer(const BufferCopy& bufferCopy, uint64_t dataSize) { - if (!FitsInBuffer(bufferCopy.buffer.Get(), bufferCopy.offset, dataSize)) { + bool fitsInBuffer = offset <= bufferSize && (size <= (bufferSize - offset)); + if (!fitsInBuffer) { return DAWN_VALIDATION_ERROR("Copy would overflow the buffer"); } return {}; } + MaybeError ValidateCopySizeFitsInBuffer(const BufferCopy& bufferCopy, uint64_t dataSize) { + return ValidateCopySizeFitsInBuffer(bufferCopy.buffer, bufferCopy.offset, dataSize); + } + MaybeError ValidateB2BCopySizeAlignment(uint64_t dataSize, uint64_t srcOffset, uint64_t dstOffset) { @@ -690,10 +693,10 @@ namespace dawn_native { CopyBufferToBufferCmd* copy = mAllocator.Allocate(Command::CopyBufferToBuffer); - copy->source.buffer = source; - copy->source.offset = sourceOffset; - copy->destination.buffer = destination; - copy->destination.offset = destinationOffset; + copy->source = source; + copy->sourceOffset = sourceOffset; + copy->destination = destination; + copy->destinationOffset = destinationOffset; copy->size = size; } @@ -877,18 +880,20 @@ namespace dawn_native { case Command::CopyBufferToBuffer: { CopyBufferToBufferCmd* copy = mIterator.NextCommand(); - DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->source, copy->size)); - DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, copy->size)); - DAWN_TRY(ValidateB2BCopySizeAlignment(copy->size, copy->source.offset, - copy->destination.offset)); + DAWN_TRY( + ValidateCopySizeFitsInBuffer(copy->source, copy->sourceOffset, copy->size)); + DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, + copy->destinationOffset, copy->size)); + DAWN_TRY(ValidateB2BCopySizeAlignment(copy->size, copy->sourceOffset, + copy->destinationOffset)); - DAWN_TRY(ValidateCanUseAs(copy->source.buffer.Get(), - dawn::BufferUsageBit::TransferSrc)); - DAWN_TRY(ValidateCanUseAs(copy->destination.buffer.Get(), + DAWN_TRY( + ValidateCanUseAs(copy->source.Get(), dawn::BufferUsageBit::TransferSrc)); + DAWN_TRY(ValidateCanUseAs(copy->destination.Get(), dawn::BufferUsageBit::TransferDst)); - mResourceUsages.topLevelBuffers.insert(copy->source.buffer.Get()); - mResourceUsages.topLevelBuffers.insert(copy->destination.buffer.Get()); + mResourceUsages.topLevelBuffers.insert(copy->source.Get()); + mResourceUsages.topLevelBuffers.insert(copy->destination.Get()); } break; case Command::CopyBufferToTexture: { diff --git a/src/dawn_native/Commands.h b/src/dawn_native/Commands.h index e36262299a..53d6047c47 100644 --- a/src/dawn_native/Commands.h +++ b/src/dawn_native/Commands.h @@ -102,8 +102,10 @@ namespace dawn_native { }; struct CopyBufferToBufferCmd { - BufferCopy source; - BufferCopy destination; + Ref source; + uint64_t sourceOffset; + Ref destination; + uint64_t destinationOffset; uint64_t size; }; diff --git a/src/dawn_native/d3d12/CommandBufferD3D12.cpp b/src/dawn_native/d3d12/CommandBufferD3D12.cpp index d9c7d52fb8..6b9f8e631c 100644 --- a/src/dawn_native/d3d12/CommandBufferD3D12.cpp +++ b/src/dawn_native/d3d12/CommandBufferD3D12.cpp @@ -469,15 +469,15 @@ namespace dawn_native { namespace d3d12 { case Command::CopyBufferToBuffer: { CopyBufferToBufferCmd* copy = mCommands.NextCommand(); - Buffer* srcBuffer = ToBackend(copy->source.buffer.Get()); - Buffer* dstBuffer = ToBackend(copy->destination.buffer.Get()); + Buffer* srcBuffer = ToBackend(copy->source.Get()); + Buffer* dstBuffer = ToBackend(copy->destination.Get()); srcBuffer->TransitionUsageNow(commandList, dawn::BufferUsageBit::TransferSrc); dstBuffer->TransitionUsageNow(commandList, dawn::BufferUsageBit::TransferDst); commandList->CopyBufferRegion( - dstBuffer->GetD3D12Resource().Get(), copy->destination.offset, - srcBuffer->GetD3D12Resource().Get(), copy->source.offset, copy->size); + dstBuffer->GetD3D12Resource().Get(), copy->destinationOffset, + srcBuffer->GetD3D12Resource().Get(), copy->sourceOffset, copy->size); } break; case Command::CopyBufferToTexture: { diff --git a/src/dawn_native/metal/CommandBufferMTL.mm b/src/dawn_native/metal/CommandBufferMTL.mm index a0a35491a3..7cc8897d58 100644 --- a/src/dawn_native/metal/CommandBufferMTL.mm +++ b/src/dawn_native/metal/CommandBufferMTL.mm @@ -348,14 +348,12 @@ namespace dawn_native { namespace metal { case Command::CopyBufferToBuffer: { CopyBufferToBufferCmd* copy = mCommands.NextCommand(); - auto& src = copy->source; - auto& dst = copy->destination; encoders.EnsureBlit(commandBuffer); - [encoders.blit copyFromBuffer:ToBackend(src.buffer)->GetMTLBuffer() - sourceOffset:src.offset - toBuffer:ToBackend(dst.buffer)->GetMTLBuffer() - destinationOffset:dst.offset + [encoders.blit copyFromBuffer:ToBackend(copy->source)->GetMTLBuffer() + sourceOffset:copy->sourceOffset + toBuffer:ToBackend(copy->destination)->GetMTLBuffer() + destinationOffset:copy->destinationOffset size:copy->size]; } break; diff --git a/src/dawn_native/opengl/CommandBufferGL.cpp b/src/dawn_native/opengl/CommandBufferGL.cpp index a1b5e547c4..7c5a8dc9de 100644 --- a/src/dawn_native/opengl/CommandBufferGL.cpp +++ b/src/dawn_native/opengl/CommandBufferGL.cpp @@ -360,13 +360,11 @@ namespace dawn_native { namespace opengl { case Command::CopyBufferToBuffer: { CopyBufferToBufferCmd* copy = mCommands.NextCommand(); - auto& src = copy->source; - auto& dst = copy->destination; - glBindBuffer(GL_PIXEL_PACK_BUFFER, ToBackend(src.buffer)->GetHandle()); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ToBackend(dst.buffer)->GetHandle()); - glCopyBufferSubData(GL_PIXEL_PACK_BUFFER, GL_PIXEL_UNPACK_BUFFER, src.offset, - dst.offset, copy->size); + glBindBuffer(GL_PIXEL_PACK_BUFFER, ToBackend(copy->source)->GetHandle()); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ToBackend(copy->destination)->GetHandle()); + glCopyBufferSubData(GL_PIXEL_PACK_BUFFER, GL_PIXEL_UNPACK_BUFFER, + copy->sourceOffset, copy->destinationOffset, copy->size); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); diff --git a/src/dawn_native/vulkan/CommandBufferVk.cpp b/src/dawn_native/vulkan/CommandBufferVk.cpp index 89fbbac4c0..a04bec72e8 100644 --- a/src/dawn_native/vulkan/CommandBufferVk.cpp +++ b/src/dawn_native/vulkan/CommandBufferVk.cpp @@ -301,21 +301,19 @@ namespace dawn_native { namespace vulkan { switch (type) { case Command::CopyBufferToBuffer: { CopyBufferToBufferCmd* copy = mCommands.NextCommand(); - auto& src = copy->source; - auto& dst = copy->destination; + Buffer* srcBuffer = ToBackend(copy->source.Get()); + Buffer* dstBuffer = ToBackend(copy->destination.Get()); - ToBackend(src.buffer) - ->TransitionUsageNow(commands, dawn::BufferUsageBit::TransferSrc); - ToBackend(dst.buffer) - ->TransitionUsageNow(commands, dawn::BufferUsageBit::TransferDst); + srcBuffer->TransitionUsageNow(commands, dawn::BufferUsageBit::TransferSrc); + dstBuffer->TransitionUsageNow(commands, dawn::BufferUsageBit::TransferDst); VkBufferCopy region; - region.srcOffset = src.offset; - region.dstOffset = dst.offset; + region.srcOffset = copy->sourceOffset; + region.dstOffset = copy->destinationOffset; region.size = copy->size; - VkBuffer srcHandle = ToBackend(src.buffer)->GetHandle(); - VkBuffer dstHandle = ToBackend(dst.buffer)->GetHandle(); + VkBuffer srcHandle = srcBuffer->GetHandle(); + VkBuffer dstHandle = dstBuffer->GetHandle(); device->fn.CmdCopyBuffer(commands, srcHandle, dstHandle, 1, ®ion); } break;