| // Copyright 2026 The Dawn & Tint Authors | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // 1. Redistributions of source code must retain the above copyright notice, this | |
| // list of conditions and the following disclaimer. | |
| // | |
| // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // | |
| // 3. Neither the name of the copyright holder nor the names of its | |
| // contributors may be used to endorse or promote products derived from | |
| // this software without specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| #include "src/dawn/native/BufferTextureCopyOversizedRow.h" | |
| #include <cstdint> | |
| #include "src/dawn/common/Constants.h" | |
| #include "src/dawn/common/Math.h" | |
| #include "src/dawn/native/Buffer.h" | |
| #include "src/dawn/native/CommandAllocator.h" | |
| #include "src/dawn/native/CommandEncoder.h" | |
| #include "src/dawn/native/Device.h" | |
| #include "src/dawn/native/Texture.h" | |
| namespace dawn::native { | |
| namespace { | |
| // Intel GPUs encode a buffer<->texture copy's bytes per row and texels per row as (value - 1) in | |
| // 18-bit and 14-bit hardware register fields, so up to 2^18 bytes (256 KiB) and 2^14 (16384) | |
| // texels inclusive fit without wrapping. The two must be checked independently: | |
| // bytesPerRow == texelsPerRow * bytesPerBlock, so a format with a small bytesPerBlock (e.g. | |
| // R8Unorm, 1 byte/texel) can push texelsPerRow past 2^14 while bytesPerRow is still nowhere near | |
| // 2^18. See crbug.com/481934465. | |
| // These thresholds also cover the larger row pitch overflow / wrap-around limits on ARM Mali | |
| // (2^28 bytes, crbug.com/569161611), ImgTec PowerVR (2^28 bytes, crbug.com/529867799), | |
| // Qualcomm Adreno (2^28 bytes, crbug.com/380487895), and NVIDIA (2^31 bytes, crbug.com/583083283). | |
| // TODO(crbug.com/590457108): Move these thresholds to backend/adapter-specific internal limits so | |
| // backends that don't need splitting (or have higher limits) don't unnecessarily split copies. | |
| constexpr uint64_t kMaxBytesPerRow = 1u << 18; | |
| constexpr uint64_t kMaxTexelsPerRow = 1u << 14; | |
| static_assert(kMaxTextureDimension1D <= kMaxTexelsPerRow); | |
| static_assert(kMaxTextureDimension2D <= kMaxTexelsPerRow); | |
| static_assert(uint64_t{kMaxTextureDimension2D} * kMaxTexelBlockByteSize <= kMaxBytesPerRow); | |
| // Multi-slice copies also step across images by `bytesPerImage = bytesPerRow * rowsPerImage`. | |
| // ARM Mali GLES wraps signed 32-bit bit strides at 2^28 bytes (256 MiB), and drivers that compute | |
| // byte strides in signed 32-bit integers (e.g. NVIDIA Vulkan, crbug.com/583083283) wrap at 2 GiB. | |
| constexpr uint64_t kMaxBytesPerImage = 1ull << 28; | |
| // TODO(crbug.com/590159189): Consider caching a single 3x1 R8Uint scratch texture on the device | |
| // rather than creating one per oversized copy if this workaround remains long-term. | |
| ResultOrError<Ref<TextureBase>> CreateUnalignedTailScratchTexture(DeviceBase* device, | |
| uint32_t remBytes) { | |
| TextureDescriptor scratchDesc = {}; | |
| scratchDesc.dimension = wgpu::TextureDimension::e2D; | |
| scratchDesc.format = wgpu::TextureFormat::R8Uint; | |
| scratchDesc.size = {remBytes, 1, 1}; | |
| scratchDesc.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::CopySrc; | |
| return device->CreateTexture(&scratchDesc); | |
| } | |
| // Copies `size` bytes from `srcBuffer[srcOffset]` to `dstBuffer[dstOffset]` (with 4-byte-aligned | |
| // offsets) without reading or writing past `size`. Copies the 4-byte-aligned prefix via | |
| // CopyBufferToBuffer and transfers any trailing 1-3 bytes through `*scratchTexture` (lazily | |
| // created as a `{remBytes, 1, 1}` R8Uint texture so callers can reuse it across rows). | |
| // TODO(crbug.com/590159189): Gate the scratch-texture tail behind a device toggle enabled only on | |
| // non-Apple Macs and WGPU, and remove it once unaligned CopyBufferToBuffer sizes are supported | |
| // (https://github.com/gpuweb/gpuweb/issues/5203). | |
| MaybeError CopyBufferToBufferWithUnalignedTail(CommandEncoder* commandEncoder, | |
| CommandAllocator* allocator, | |
| BufferBase* srcBuffer, | |
| uint64_t srcOffset, | |
| BufferBase* dstBuffer, | |
| uint64_t dstOffset, | |
| uint64_t size, | |
| Ref<TextureBase>* scratchTexture) { | |
| DAWN_ASSERT(srcOffset % kCopyBufferToBufferOffsetAlignment == 0); | |
| DAWN_ASSERT(dstOffset % kCopyBufferToBufferOffsetAlignment == 0); | |
| DAWN_ASSERT(srcOffset + size <= srcBuffer->GetSize()); | |
| DAWN_ASSERT(dstOffset + size <= dstBuffer->GetSize()); | |
| const uint64_t alignedBytes = AlignDown(size, kCopyBufferToBufferOffsetAlignment); | |
| if (alignedBytes > 0) { | |
| CopyBufferToBufferCmd* b2b = | |
| allocator->Allocate<CopyBufferToBufferCmd>(Command::CopyBufferToBuffer); | |
| b2b->source = srcBuffer; | |
| b2b->sourceOffset = srcOffset; | |
| b2b->destination = dstBuffer; | |
| b2b->destinationOffset = dstOffset; | |
| b2b->size = alignedBytes; | |
| } | |
| const uint32_t remBytes = checked_cast<uint32_t>(size - alignedBytes); | |
| if (remBytes == 0) { | |
| return {}; | |
| } | |
| if (*scratchTexture == nullptr) { | |
| DAWN_TRY_ASSIGN(*scratchTexture, | |
| CreateUnalignedTailScratchTexture(commandEncoder->GetDevice(), remBytes)); | |
| } | |
| DAWN_ASSERT((*scratchTexture)->GetBaseSize().width >= remBytes); | |
| TexelCopyBufferInfo tailSrc = {}; | |
| tailSrc.buffer = srcBuffer; | |
| tailSrc.layout.offset = srcOffset + alignedBytes; | |
| TexelCopyTextureInfo scratchInfo = {}; | |
| scratchInfo.texture = scratchTexture->Get(); | |
| TexelCopyBufferInfo tailDst = {}; | |
| tailDst.buffer = dstBuffer; | |
| tailDst.layout.offset = dstOffset + alignedBytes; | |
| const Extent3D tailExtent = {remBytes, 1, 1}; | |
| commandEncoder->APICopyBufferToTexture(&tailSrc, &scratchInfo, &tailExtent); | |
| commandEncoder->APICopyTextureToBuffer(&scratchInfo, &tailDst, &tailExtent); | |
| return {}; | |
| } | |
| } // namespace | |
| bool NeedsBufferTextureCopySplitForOversizedRow(const BufferCopy& bufferCopy, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo) { | |
| DAWN_CHECK(static_cast<uint64_t>(blockInfo.ToTexelWidth(copySize.width)) <= kMaxTexelsPerRow); | |
| DAWN_CHECK(Align(blockInfo.ToBytes(copySize.width), uint64_t{kTextureBytesPerRowAlignment}) <= | |
| kMaxBytesPerRow); | |
| const uint64_t bytesPerRow = blockInfo.ToBytes(bufferCopy.blocksPerRow); | |
| const bool exceedsMaxBytesPerRow = bytesPerRow > kMaxBytesPerRow; | |
| const bool exceedsMaxTexelsPerRow = | |
| static_cast<uint64_t>(blockInfo.ToTexelWidth(bufferCopy.blocksPerRow)) > kMaxTexelsPerRow; | |
| const bool exceedsMaxBytesPerImage = | |
| copySize.depthOrArrayLayers > BlockCount{1u} && | |
| blockInfo.ToBytes(bufferCopy.blocksPerRow * bufferCopy.rowsPerImage) >= kMaxBytesPerImage; | |
| return exceedsMaxBytesPerRow || exceedsMaxTexelsPerRow || exceedsMaxBytesPerImage; | |
| } | |
| bool NeedsBufferTextureCopySplitForOversizedRow(const TexelCopyBufferLayout& layout, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo) { | |
| // `layout.bytesPerRow` is not required to be a multiple of `blockInfo.byteSize` (e.g. in | |
| // `Queue::WriteTexture`), so round up to a whole block count before `BytesToBlocks` (which | |
| // asserts divisibility). | |
| BufferCopy bufferCopy; | |
| bufferCopy.offset = layout.offset; | |
| bufferCopy.blocksPerRow = | |
| blockInfo.BytesToBlocks(RoundUp(uint64_t{layout.bytesPerRow}, blockInfo.byteSize)); | |
| bufferCopy.rowsPerImage = BlockCount{layout.rowsPerImage}; | |
| return NeedsBufferTextureCopySplitForOversizedRow(bufferCopy, copySize, blockInfo); | |
| } | |
| bool NeedsTempBufferForOversizedRowCopy(const BufferCopy& bufferCopy, | |
| const TextureCopy& textureCopy, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo) { | |
| DAWN_ASSERT(textureCopy.texture != nullptr); | |
| if (!textureCopy.texture->GetFormat().HasDepthOrStencil() || | |
| copySize.height <= BlockCount{1u}) { | |
| return false; | |
| } | |
| const BlockExtent3D sliceSize{copySize.width, copySize.height, BlockCount{1u}}; | |
| return NeedsBufferTextureCopySplitForOversizedRow(bufferCopy, sliceSize, blockInfo); | |
| } | |
| MaybeError ForEachSplitBufferTextureCopyForOversizedRow( | |
| const BufferCopy& bufferCopy, | |
| const TextureCopy& textureCopy, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo, | |
| absl::FunctionRef<MaybeError(const BufferCopy&, const TextureCopy&, const BlockExtent3D&)> | |
| func) { | |
| DAWN_CHECK(!copySize.IsEmpty()); | |
| const BlockOrigin3D origin = blockInfo.ToBlock(textureCopy.origin); | |
| // If the copy already fits within kMaxTexelsPerRow, kMaxBytesPerRow, and kMaxBytesPerImage, | |
| // emit it as a single copy. | |
| if (!NeedsBufferTextureCopySplitForOversizedRow(bufferCopy, copySize, blockInfo)) { | |
| return func(bufferCopy, textureCopy, copySize); | |
| } | |
| // If a single slice already fits under kMaxTexelsPerRow and kMaxBytesPerRow (only multi-slice | |
| // kMaxBytesPerImage was exceeded), emit one sub-copy per slice. | |
| const BlockExtent3D sliceSize{copySize.width, copySize.height, BlockCount{1u}}; | |
| if (!NeedsBufferTextureCopySplitForOversizedRow(bufferCopy, sliceSize, blockInfo)) { | |
| BufferCopy subBufferCopy; | |
| subBufferCopy.buffer = bufferCopy.buffer; | |
| subBufferCopy.blocksPerRow = bufferCopy.blocksPerRow; | |
| subBufferCopy.rowsPerImage = copySize.height; | |
| DAWN_ASSERT(!sliceSize.IsEmpty()); | |
| DAWN_ASSERT( | |
| !NeedsBufferTextureCopySplitForOversizedRow(subBufferCopy, sliceSize, blockInfo)); | |
| TextureCopy subTextureCopy = textureCopy; | |
| for (BlockCount z{0u}; z < copySize.depthOrArrayLayers; z += BlockCount{1u}) { | |
| subBufferCopy.offset = | |
| bufferCopy.offset + | |
| blockInfo.ToBytes(z * bufferCopy.rowsPerImage * bufferCopy.blocksPerRow); | |
| subTextureCopy.origin = | |
| blockInfo.ToTexel(BlockOrigin3D{origin.x, origin.y, origin.z + z}); | |
| DAWN_TRY(func(subBufferCopy, subTextureCopy, sliceSize)); | |
| } | |
| return {}; | |
| } | |
| // Otherwise, split per unpadded row. Since maxTextureDimension1D/2D <= 16384 and | |
| // block.byteSize <= 16, we never need to split further into sub-rows. | |
| const BlockExtent3D rowSize{copySize.width, BlockCount{1u}, BlockCount{1u}}; | |
| BufferCopy subBufferCopy; | |
| subBufferCopy.buffer = bufferCopy.buffer; | |
| subBufferCopy.blocksPerRow = blockInfo.BytesToBlocks( | |
| Align(blockInfo.ToBytes(copySize.width), uint64_t{kTextureBytesPerRowAlignment})); | |
| subBufferCopy.rowsPerImage = BlockCount{1u}; | |
| DAWN_ASSERT(!rowSize.IsEmpty()); | |
| DAWN_ASSERT(!IsPowerOfTwo(static_cast<uint64_t>(blockInfo.width)) || | |
| !NeedsBufferTextureCopySplitForOversizedRow(subBufferCopy, rowSize, blockInfo)); | |
| TextureCopy subTextureCopy = textureCopy; | |
| for (BlockCount z{0u}; z < copySize.depthOrArrayLayers; z += BlockCount{1u}) { | |
| // Buffer offset of the first row of this layer. | |
| const uint64_t layerOffset = | |
| bufferCopy.offset + | |
| blockInfo.ToBytes(z * bufferCopy.rowsPerImage * bufferCopy.blocksPerRow); | |
| for (BlockCount y{0u}; y < copySize.height; y += BlockCount{1u}) { | |
| // Advance from the layer's first row by y rows of (possibly padded) blocks per row. | |
| subBufferCopy.offset = layerOffset + blockInfo.ToBytes(y * bufferCopy.blocksPerRow); | |
| subTextureCopy.origin = | |
| blockInfo.ToTexel(BlockOrigin3D{origin.x, origin.y + y, origin.z + z}); | |
| DAWN_TRY(func(subBufferCopy, subTextureCopy, rowSize)); | |
| } | |
| } | |
| return {}; | |
| } | |
| void SplitCopyTextureToBufferForOversizedRow(CommandAllocator* allocator, | |
| const BufferCopy& bufferCopy, | |
| const TextureCopy& textureCopy, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo) { | |
| DAWN_ASSERT(!NeedsTempBufferForOversizedRowCopy(bufferCopy, textureCopy, copySize, blockInfo)); | |
| MaybeError result = ForEachSplitBufferTextureCopyForOversizedRow( | |
| bufferCopy, textureCopy, copySize, blockInfo, | |
| [&](const BufferCopy& subBufferCopy, const TextureCopy& subTextureCopy, | |
| const BlockExtent3D& subCopySize) -> MaybeError { | |
| CopyTextureToBufferCmd* cmd = | |
| allocator->Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer); | |
| cmd->source = subTextureCopy; | |
| cmd->destination.buffer = subBufferCopy.buffer; | |
| cmd->destination.offset = subBufferCopy.offset; | |
| cmd->destination.blocksPerRow = subBufferCopy.blocksPerRow; | |
| cmd->destination.rowsPerImage = subBufferCopy.rowsPerImage; | |
| cmd->copySize = blockInfo.ToTexel(subCopySize); | |
| return {}; | |
| }); | |
| // Callback never returns an error. | |
| DAWN_ASSERT(!result.IsError()); | |
| } | |
| void SplitCopyBufferToTextureForOversizedRow(CommandAllocator* allocator, | |
| const BufferCopy& bufferCopy, | |
| const TextureCopy& textureCopy, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo) { | |
| DAWN_ASSERT(!NeedsTempBufferForOversizedRowCopy(bufferCopy, textureCopy, copySize, blockInfo)); | |
| MaybeError result = ForEachSplitBufferTextureCopyForOversizedRow( | |
| bufferCopy, textureCopy, copySize, blockInfo, | |
| [&](const BufferCopy& subBufferCopy, const TextureCopy& subTextureCopy, | |
| const BlockExtent3D& subCopySize) -> MaybeError { | |
| CopyBufferToTextureCmd* cmd = | |
| allocator->Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture); | |
| cmd->source.buffer = subBufferCopy.buffer; | |
| cmd->source.offset = subBufferCopy.offset; | |
| cmd->source.blocksPerRow = subBufferCopy.blocksPerRow; | |
| cmd->source.rowsPerImage = subBufferCopy.rowsPerImage; | |
| cmd->destination = subTextureCopy; | |
| cmd->copySize = blockInfo.ToTexel(subCopySize); | |
| return {}; | |
| }); | |
| // Callback never returns an error. | |
| DAWN_ASSERT(!result.IsError()); | |
| } | |
| MaybeError CopyBufferToTextureViaTempBufferForOversizedRow(CommandEncoder* commandEncoder, | |
| CommandAllocator* allocator, | |
| const BufferCopy& bufferCopy, | |
| const TextureCopy& textureCopy, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo) { | |
| DeviceBase* device = commandEncoder->GetDevice(); | |
| DAWN_ASSERT(device->IsLockedByCurrentThreadIfNeeded()); | |
| DAWN_CHECK(!copySize.IsEmpty()); | |
| DAWN_ASSERT(NeedsTempBufferForOversizedRowCopy(bufferCopy, textureCopy, copySize, blockInfo)); | |
| const uint64_t rowBytes = blockInfo.ToBytes(copySize.width); | |
| const uint64_t copyRowBytes = Align(rowBytes, kCopyBufferToBufferOffsetAlignment); | |
| const uint64_t compactBytesPerRow = Align(rowBytes, uint64_t{kTextureBytesPerRowAlignment}); | |
| const uint64_t sliceHeight = static_cast<uint64_t>(copySize.height); | |
| const uint64_t compactSliceBytes = compactBytesPerRow * (sliceHeight - 1) + copyRowBytes; | |
| BufferDescriptor tempBufferDesc = {}; | |
| tempBufferDesc.size = compactSliceBytes; | |
| tempBufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::CopySrc; | |
| Ref<BufferBase> tempBuffer; | |
| DAWN_TRY_ASSIGN(tempBuffer, device->CreateBuffer(&tempBufferDesc)); | |
| commandEncoder->TrackTopLevelBuffer(tempBuffer.Get()); | |
| // Every row read by CopyBufferToTexture is written before each slice copy, and row padding | |
| // bytes are never read. | |
| tempBuffer->SetInitialized(true); | |
| const uint64_t srcBytesPerRow = blockInfo.ToBytes(bufferCopy.blocksPerRow); | |
| const uint64_t srcBytesPerImage = | |
| blockInfo.ToBytes(bufferCopy.blocksPerRow * bufferCopy.rowsPerImage); | |
| const BlockOrigin3D dstOrigin = blockInfo.ToBlock(textureCopy.origin); | |
| const BlockExtent3D sliceSize{copySize.width, copySize.height, BlockCount{1u}}; | |
| Ref<TextureBase> scratchTexture; | |
| for (BlockCount z{0u}; z < copySize.depthOrArrayLayers; z += BlockCount{1u}) { | |
| const uint64_t layerOffset = | |
| bufferCopy.offset + static_cast<uint64_t>(z) * srcBytesPerImage; | |
| for (BlockCount y{0u}; y < copySize.height; y += BlockCount{1u}) { | |
| const uint64_t rowOffset = layerOffset + static_cast<uint64_t>(y) * srcBytesPerRow; | |
| const uint64_t dstRowOffset = static_cast<uint64_t>(y) * compactBytesPerRow; | |
| // Round up to 4 bytes when trailing source bytes are available so only a tightly sized | |
| // final row needs the R8Uint tail copy. | |
| const uint64_t bytesToCopy = (rowOffset + copyRowBytes <= bufferCopy.buffer->GetSize()) | |
| ? copyRowBytes | |
| : rowBytes; | |
| DAWN_TRY(CopyBufferToBufferWithUnalignedTail( | |
| commandEncoder, allocator, bufferCopy.buffer.Get(), rowOffset, tempBuffer.Get(), | |
| dstRowOffset, bytesToCopy, &scratchTexture)); | |
| } | |
| CopyBufferToTextureCmd* b2t = | |
| allocator->Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture); | |
| b2t->source.buffer = tempBuffer; | |
| b2t->source.offset = 0; | |
| b2t->source.blocksPerRow = blockInfo.BytesToBlocks(compactBytesPerRow); | |
| b2t->source.rowsPerImage = copySize.height; | |
| b2t->destination = textureCopy; | |
| b2t->destination.origin = | |
| blockInfo.ToTexel(BlockOrigin3D{dstOrigin.x, dstOrigin.y, dstOrigin.z + z}); | |
| b2t->copySize = blockInfo.ToTexel(sliceSize); | |
| DAWN_ASSERT(!NeedsBufferTextureCopySplitForOversizedRow(b2t->source, sliceSize, blockInfo)); | |
| } | |
| return {}; | |
| } | |
| MaybeError CopyTextureToBufferViaTempBufferForOversizedRow(CommandEncoder* commandEncoder, | |
| CommandAllocator* allocator, | |
| const TextureCopy& textureCopy, | |
| const BufferCopy& bufferCopy, | |
| const BlockExtent3D& copySize, | |
| const TypedTexelBlockInfo& blockInfo) { | |
| DeviceBase* device = commandEncoder->GetDevice(); | |
| DAWN_ASSERT(device->IsLockedByCurrentThreadIfNeeded()); | |
| DAWN_CHECK(!copySize.IsEmpty()); | |
| DAWN_ASSERT(NeedsTempBufferForOversizedRowCopy(bufferCopy, textureCopy, copySize, blockInfo)); | |
| const uint64_t rowBytes = blockInfo.ToBytes(copySize.width); | |
| const uint64_t copyRowBytes = Align(rowBytes, kCopyBufferToBufferOffsetAlignment); | |
| const uint64_t compactBytesPerRow = Align(rowBytes, uint64_t{kTextureBytesPerRowAlignment}); | |
| const uint64_t sliceHeight = static_cast<uint64_t>(copySize.height); | |
| const uint64_t compactSliceBytes = compactBytesPerRow * (sliceHeight - 1) + copyRowBytes; | |
| BufferDescriptor tempBufferDesc = {}; | |
| tempBufferDesc.size = compactSliceBytes; | |
| tempBufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::CopySrc; | |
| Ref<BufferBase> tempBuffer; | |
| DAWN_TRY_ASSIGN(tempBuffer, device->CreateBuffer(&tempBufferDesc)); | |
| commandEncoder->TrackTopLevelBuffer(tempBuffer.Get()); | |
| // Every byte read from tempBuffer is written by the preceding CopyTextureToBuffer. | |
| tempBuffer->SetInitialized(true); | |
| Ref<TextureBase> scratchTexture; | |
| const uint64_t dstBytesPerRow = blockInfo.ToBytes(bufferCopy.blocksPerRow); | |
| const uint64_t dstBytesPerImage = | |
| blockInfo.ToBytes(bufferCopy.blocksPerRow * bufferCopy.rowsPerImage); | |
| const BlockOrigin3D srcOrigin = blockInfo.ToBlock(textureCopy.origin); | |
| const BlockExtent3D sliceSize{copySize.width, copySize.height, BlockCount{1u}}; | |
| for (BlockCount z{0u}; z < copySize.depthOrArrayLayers; z += BlockCount{1u}) { | |
| CopyTextureToBufferCmd* t2b = | |
| allocator->Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer); | |
| t2b->source = textureCopy; | |
| t2b->source.origin = | |
| blockInfo.ToTexel(BlockOrigin3D{srcOrigin.x, srcOrigin.y, srcOrigin.z + z}); | |
| t2b->destination.buffer = tempBuffer; | |
| t2b->destination.offset = 0; | |
| t2b->destination.blocksPerRow = blockInfo.BytesToBlocks(compactBytesPerRow); | |
| t2b->destination.rowsPerImage = copySize.height; | |
| t2b->copySize = blockInfo.ToTexel(sliceSize); | |
| DAWN_ASSERT( | |
| !NeedsBufferTextureCopySplitForOversizedRow(t2b->destination, sliceSize, blockInfo)); | |
| const uint64_t layerOffset = | |
| bufferCopy.offset + static_cast<uint64_t>(z) * dstBytesPerImage; | |
| for (BlockCount y{0u}; y < copySize.height; y += BlockCount{1u}) { | |
| const uint64_t srcRowOffset = static_cast<uint64_t>(y) * compactBytesPerRow; | |
| const uint64_t dstRowOffset = layerOffset + static_cast<uint64_t>(y) * dstBytesPerRow; | |
| DAWN_TRY(CopyBufferToBufferWithUnalignedTail( | |
| commandEncoder, allocator, tempBuffer.Get(), srcRowOffset, bufferCopy.buffer.Get(), | |
| dstRowOffset, rowBytes, &scratchTexture)); | |
| } | |
| } | |
| return {}; | |
| } | |
| } // namespace dawn::native |