Refactor Internal Command Buffer Copy APIs

Modify internal command buffer types to reflect the front end APIs for
copyTextureToBuffer and copyBufferToTexture.

Bug: dawn:17

Change-Id: I088a167ee7145d741e70ed28c1df7a12d24b72fc
Reviewed-on: https://dawn-review.googlesource.com/c/2740
Commit-Queue: Brandon1 Jones <brandon1.jones@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Brandon Jones 2018-11-30 17:39:24 +00:00 committed by Commit Bot service account
parent 5acd60c929
commit 179db44c25
9 changed files with 155 additions and 189 deletions

View File

@ -35,29 +35,30 @@ namespace dawn_native {
namespace { namespace {
MaybeError ValidateCopyLocationFitsInTexture(const TextureCopyLocation& location) { MaybeError ValidateCopySizeFitsInTexture(const TextureCopy& textureCopy,
const TextureBase* texture = location.texture.Get(); const Extent3D& copySize) {
if (location.level >= texture->GetNumMipLevels()) { const TextureBase* texture = textureCopy.texture.Get();
if (textureCopy.level >= texture->GetNumMipLevels()) {
return DAWN_VALIDATION_ERROR("Copy mip-level out of range"); return DAWN_VALIDATION_ERROR("Copy mip-level out of range");
} }
if (location.slice >= texture->GetArrayLayers()) { if (textureCopy.slice >= texture->GetArrayLayers()) {
return DAWN_VALIDATION_ERROR("Copy array-layer out of range"); return DAWN_VALIDATION_ERROR("Copy array-layer out of range");
} }
// All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid // All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid
// overflows. // overflows.
uint64_t level = location.level; uint64_t level = textureCopy.level;
if (uint64_t(location.x) + uint64_t(location.width) > if (uint64_t(textureCopy.origin.x) + uint64_t(copySize.width) >
(static_cast<uint64_t>(texture->GetSize().width) >> level) || (static_cast<uint64_t>(texture->GetSize().width) >> level) ||
uint64_t(location.y) + uint64_t(location.height) > uint64_t(textureCopy.origin.y) + uint64_t(copySize.height) >
(static_cast<uint64_t>(texture->GetSize().height) >> level)) { (static_cast<uint64_t>(texture->GetSize().height) >> level)) {
return DAWN_VALIDATION_ERROR("Copy would touch outside of the texture"); return DAWN_VALIDATION_ERROR("Copy would touch outside of the texture");
} }
// TODO(cwallez@chromium.org): Check the depth bound differently for 2D arrays and 3D // TODO(cwallez@chromium.org): Check the depth bound differently for 2D arrays and 3D
// textures // textures
if (location.z != 0 || location.depth != 1) { if (textureCopy.origin.z != 0 || copySize.depth != 1) {
return DAWN_VALIDATION_ERROR("No support for z != 0 and depth != 1 for now"); return DAWN_VALIDATION_ERROR("No support for z != 0 and depth != 1 for now");
} }
@ -69,31 +70,29 @@ namespace dawn_native {
return offset <= bufferSize && (size <= (bufferSize - offset)); return offset <= bufferSize && (size <= (bufferSize - offset));
} }
MaybeError ValidateCopySizeFitsInBuffer(const BufferCopyLocation& location, MaybeError ValidateCopySizeFitsInBuffer(const BufferCopy& bufferCopy, uint32_t dataSize) {
uint32_t dataSize) { if (!FitsInBuffer(bufferCopy.buffer.Get(), bufferCopy.offset, dataSize)) {
if (!FitsInBuffer(location.buffer.Get(), location.offset, dataSize)) {
return DAWN_VALIDATION_ERROR("Copy would overflow the buffer"); return DAWN_VALIDATION_ERROR("Copy would overflow the buffer");
} }
return {}; return {};
} }
MaybeError ValidateTexelBufferOffset(TextureBase* texture, MaybeError ValidateTexelBufferOffset(TextureBase* texture, const BufferCopy& bufferCopy) {
const BufferCopyLocation& location) {
uint32_t texelSize = uint32_t texelSize =
static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())); static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat()));
if (location.offset % texelSize != 0) { if (bufferCopy.offset % texelSize != 0) {
return DAWN_VALIDATION_ERROR("Buffer offset must be a multiple of the texel size"); return DAWN_VALIDATION_ERROR("Buffer offset must be a multiple of the texel size");
} }
return {}; return {};
} }
MaybeError ComputeTextureCopyBufferSize(const TextureCopyLocation& location, MaybeError ComputeTextureCopyBufferSize(const Extent3D& copySize,
uint32_t rowPitch, uint32_t rowPitch,
uint32_t* bufferSize) { uint32_t* bufferSize) {
// TODO(cwallez@chromium.org): check for overflows // TODO(cwallez@chromium.org): check for overflows
*bufferSize = (rowPitch * (location.height - 1) + location.width) * location.depth; *bufferSize = (rowPitch * (copySize.height - 1) + copySize.width) * copySize.depth;
return {}; return {};
} }
@ -103,13 +102,15 @@ namespace dawn_native {
return texelSize * width; return texelSize * width;
} }
MaybeError ValidateRowPitch(const TextureCopyLocation& location, uint32_t rowPitch) { MaybeError ValidateRowPitch(dawn::TextureFormat format,
const Extent3D& copySize,
uint32_t rowPitch) {
if (rowPitch % kTextureRowPitchAlignment != 0) { if (rowPitch % kTextureRowPitchAlignment != 0) {
return DAWN_VALIDATION_ERROR("Row pitch must be a multiple of 256"); return DAWN_VALIDATION_ERROR("Row pitch must be a multiple of 256");
} }
uint32_t texelSize = TextureFormatPixelSize(location.texture.Get()->GetFormat()); uint32_t texelSize = TextureFormatPixelSize(format);
if (rowPitch < location.width * texelSize) { if (rowPitch < copySize.width * texelSize) {
return DAWN_VALIDATION_ERROR( return DAWN_VALIDATION_ERROR(
"Row pitch must not be less than the number of bytes per row"); "Row pitch must not be less than the number of bytes per row");
} }
@ -385,11 +386,12 @@ namespace dawn_native {
CopyBufferToTextureCmd* copy = mIterator.NextCommand<CopyBufferToTextureCmd>(); CopyBufferToTextureCmd* copy = mIterator.NextCommand<CopyBufferToTextureCmd>();
uint32_t bufferCopySize = 0; uint32_t bufferCopySize = 0;
DAWN_TRY(ValidateRowPitch(copy->destination, copy->rowPitch)); DAWN_TRY(ValidateRowPitch(copy->destination.texture->GetFormat(),
DAWN_TRY(ComputeTextureCopyBufferSize(copy->destination, copy->rowPitch, copy->copySize, copy->source.rowPitch));
DAWN_TRY(ComputeTextureCopyBufferSize(copy->copySize, copy->source.rowPitch,
&bufferCopySize)); &bufferCopySize));
DAWN_TRY(ValidateCopyLocationFitsInTexture(copy->destination)); DAWN_TRY(ValidateCopySizeFitsInTexture(copy->destination, copy->copySize));
DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->source, bufferCopySize)); DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->source, bufferCopySize));
DAWN_TRY( DAWN_TRY(
ValidateTexelBufferOffset(copy->destination.texture.Get(), copy->source)); ValidateTexelBufferOffset(copy->destination.texture.Get(), copy->source));
@ -407,11 +409,12 @@ namespace dawn_native {
CopyTextureToBufferCmd* copy = mIterator.NextCommand<CopyTextureToBufferCmd>(); CopyTextureToBufferCmd* copy = mIterator.NextCommand<CopyTextureToBufferCmd>();
uint32_t bufferCopySize = 0; uint32_t bufferCopySize = 0;
DAWN_TRY(ValidateRowPitch(copy->source, copy->rowPitch)); DAWN_TRY(ValidateRowPitch(copy->source.texture->GetFormat(), copy->copySize,
DAWN_TRY(ComputeTextureCopyBufferSize(copy->source, copy->rowPitch, copy->destination.rowPitch));
&bufferCopySize)); DAWN_TRY(ComputeTextureCopyBufferSize(
copy->copySize, copy->destination.rowPitch, &bufferCopySize));
DAWN_TRY(ValidateCopyLocationFitsInTexture(copy->source)); DAWN_TRY(ValidateCopySizeFitsInTexture(copy->source, copy->copySize));
DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, bufferCopySize)); DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, bufferCopySize));
DAWN_TRY( DAWN_TRY(
ValidateTexelBufferOffset(copy->source.texture.Get(), copy->destination)); ValidateTexelBufferOffset(copy->source.texture.Get(), copy->destination));
@ -657,18 +660,14 @@ namespace dawn_native {
copy->source.buffer = source->buffer; copy->source.buffer = source->buffer;
copy->source.offset = source->offset; copy->source.offset = source->offset;
copy->destination.texture = destination->texture; copy->destination.texture = destination->texture;
copy->destination.x = destination->origin.x; copy->destination.origin = destination->origin;
copy->destination.y = destination->origin.y; copy->copySize = *copySize;
copy->destination.z = destination->origin.z;
copy->destination.width = copySize->width;
copy->destination.height = copySize->height;
copy->destination.depth = copySize->depth;
copy->destination.level = destination->level; copy->destination.level = destination->level;
copy->destination.slice = destination->slice; copy->destination.slice = destination->slice;
if (source->rowPitch == 0) { if (source->rowPitch == 0) {
copy->rowPitch = ComputeDefaultRowPitch(destination->texture, copySize->width); copy->source.rowPitch = ComputeDefaultRowPitch(destination->texture, copySize->width);
} else { } else {
copy->rowPitch = source->rowPitch; copy->source.rowPitch = source->rowPitch;
} }
} }
@ -682,20 +681,16 @@ namespace dawn_native {
mAllocator.Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer); mAllocator.Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer);
new (copy) CopyTextureToBufferCmd; new (copy) CopyTextureToBufferCmd;
copy->source.texture = source->texture; copy->source.texture = source->texture;
copy->source.x = source->origin.x; copy->source.origin = source->origin;
copy->source.y = source->origin.y; copy->copySize = *copySize;
copy->source.z = source->origin.z;
copy->source.width = copySize->width;
copy->source.height = copySize->height;
copy->source.depth = copySize->depth;
copy->source.level = source->level; copy->source.level = source->level;
copy->source.slice = source->slice; copy->source.slice = source->slice;
copy->destination.buffer = destination->buffer; copy->destination.buffer = destination->buffer;
copy->destination.offset = destination->offset; copy->destination.offset = destination->offset;
if (destination->rowPitch == 0) { if (destination->rowPitch == 0) {
copy->rowPitch = ComputeDefaultRowPitch(source->texture, copySize->width); copy->destination.rowPitch = ComputeDefaultRowPitch(source->texture, copySize->width);
} else { } else {
copy->rowPitch = destination->rowPitch; copy->destination.rowPitch = destination->rowPitch;
} }
} }

View File

@ -54,35 +54,37 @@ namespace dawn_native {
Ref<RenderPassDescriptorBase> info; Ref<RenderPassDescriptorBase> info;
}; };
struct BufferCopyLocation { struct BufferCopy {
Ref<BufferBase> buffer; Ref<BufferBase> buffer;
uint32_t offset; uint32_t offset; // Bytes
uint32_t rowPitch; // Bytes
uint32_t imageHeight; // Texels
}; };
struct TextureCopyLocation { struct TextureCopy {
Ref<TextureBase> texture; Ref<TextureBase> texture;
uint32_t x, y, z;
uint32_t width, height, depth;
uint32_t level; uint32_t level;
uint32_t slice; uint32_t slice;
Origin3D origin; // Texels
dawn::TextureAspect aspect;
}; };
struct CopyBufferToBufferCmd { struct CopyBufferToBufferCmd {
BufferCopyLocation source; BufferCopy source;
BufferCopyLocation destination; BufferCopy destination;
uint32_t size; uint32_t size;
}; };
struct CopyBufferToTextureCmd { struct CopyBufferToTextureCmd {
BufferCopyLocation source; BufferCopy source;
TextureCopyLocation destination; TextureCopy destination;
uint32_t rowPitch; Extent3D copySize; // Texels
}; };
struct CopyTextureToBufferCmd { struct CopyTextureToBufferCmd {
TextureCopyLocation source; TextureCopy source;
BufferCopyLocation destination; BufferCopy destination;
uint32_t rowPitch; Extent3D copySize; // Texels
}; };
struct DispatchCmd { struct DispatchCmd {

View File

@ -329,10 +329,9 @@ namespace dawn_native { namespace d3d12 {
texture->TransitionUsageNow(commandList, dawn::TextureUsageBit::TransferDst); texture->TransitionUsageNow(commandList, dawn::TextureUsageBit::TransferDst);
auto copySplit = ComputeTextureCopySplit( auto copySplit = ComputeTextureCopySplit(
copy->destination.x, copy->destination.y, copy->destination.z, copy->destination.origin, copy->copySize,
copy->destination.width, copy->destination.height, copy->destination.depth,
static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())), static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())),
copy->source.offset, copy->rowPitch); copy->source.offset, copy->source.rowPitch);
D3D12_TEXTURE_COPY_LOCATION textureLocation; D3D12_TEXTURE_COPY_LOCATION textureLocation;
textureLocation.pResource = texture->GetD3D12Resource(); textureLocation.pResource = texture->GetD3D12Resource();
@ -352,7 +351,7 @@ namespace dawn_native { namespace d3d12 {
bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width; bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width;
bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height; bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height;
bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth; bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth;
bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch; bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->source.rowPitch;
D3D12_BOX sourceRegion; D3D12_BOX sourceRegion;
sourceRegion.left = info.bufferOffset.x; sourceRegion.left = info.bufferOffset.x;
@ -377,10 +376,9 @@ namespace dawn_native { namespace d3d12 {
buffer->TransitionUsageNow(commandList, dawn::BufferUsageBit::TransferDst); buffer->TransitionUsageNow(commandList, dawn::BufferUsageBit::TransferDst);
auto copySplit = ComputeTextureCopySplit( auto copySplit = ComputeTextureCopySplit(
copy->source.x, copy->source.y, copy->source.z, copy->source.width, copy->source.origin, copy->copySize,
copy->source.height, copy->source.depth,
static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())), static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())),
copy->destination.offset, copy->rowPitch); copy->destination.offset, copy->destination.rowPitch);
D3D12_TEXTURE_COPY_LOCATION textureLocation; D3D12_TEXTURE_COPY_LOCATION textureLocation;
textureLocation.pResource = texture->GetD3D12Resource(); textureLocation.pResource = texture->GetD3D12Resource();
@ -399,7 +397,8 @@ namespace dawn_native { namespace d3d12 {
bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width; bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width;
bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height; bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height;
bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth; bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth;
bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch; bufferLocation.PlacedFootprint.Footprint.RowPitch =
copy->destination.rowPitch;
D3D12_BOX sourceRegion; D3D12_BOX sourceRegion;
sourceRegion.left = info.textureOffset.x; sourceRegion.left = info.textureOffset.x;

View File

@ -24,32 +24,26 @@ namespace dawn_native { namespace d3d12 {
uint32_t rowPitch, uint32_t rowPitch,
uint32_t slicePitch, uint32_t slicePitch,
uint32_t texelSize, uint32_t texelSize,
uint32_t* texelOffsetX, Origin3D* texelOffset) {
uint32_t* texelOffsetY,
uint32_t* texelOffsetZ) {
uint32_t byteOffsetX = offset % rowPitch; uint32_t byteOffsetX = offset % rowPitch;
offset -= byteOffsetX; offset -= byteOffsetX;
uint32_t byteOffsetY = offset % slicePitch; uint32_t byteOffsetY = offset % slicePitch;
uint32_t byteOffsetZ = offset - byteOffsetY; uint32_t byteOffsetZ = offset - byteOffsetY;
*texelOffsetX = byteOffsetX / texelSize; texelOffset->x = byteOffsetX / texelSize;
*texelOffsetY = byteOffsetY / rowPitch; texelOffset->y = byteOffsetY / rowPitch;
*texelOffsetZ = byteOffsetZ / slicePitch; texelOffset->z = byteOffsetZ / slicePitch;
} }
} // namespace } // namespace
TextureCopySplit ComputeTextureCopySplit(uint32_t x, TextureCopySplit ComputeTextureCopySplit(Origin3D origin,
uint32_t y, Extent3D copySize,
uint32_t z,
uint32_t width,
uint32_t height,
uint32_t depth,
uint32_t texelSize, uint32_t texelSize,
uint32_t offset, uint32_t offset,
uint32_t rowPitch) { uint32_t rowPitch) {
TextureCopySplit copy; TextureCopySplit copy;
if (z != 0 || depth > 1) { if (origin.z != 0 || copySize.depth > 1) {
// TODO(enga@google.com): Handle 3D // TODO(enga@google.com): Handle 3D
ASSERT(false); ASSERT(false);
return copy; return copy;
@ -63,20 +57,14 @@ namespace dawn_native { namespace d3d12 {
if (offset == alignedOffset) { if (offset == alignedOffset) {
copy.count = 1; copy.count = 1;
copy.copies[0].textureOffset.x = x; copy.copies[0].textureOffset = origin;
copy.copies[0].textureOffset.y = y;
copy.copies[0].textureOffset.z = z;
copy.copies[0].copySize.width = width; copy.copies[0].copySize = copySize;
copy.copies[0].copySize.height = height;
copy.copies[0].copySize.depth = depth;
copy.copies[0].bufferOffset.x = 0; copy.copies[0].bufferOffset.x = 0;
copy.copies[0].bufferOffset.y = 0; copy.copies[0].bufferOffset.y = 0;
copy.copies[0].bufferOffset.z = 0; copy.copies[0].bufferOffset.z = 0;
copy.copies[0].bufferSize.width = width; copy.copies[0].bufferSize = copySize;
copy.copies[0].bufferSize.height = height;
copy.copies[0].bufferSize.depth = depth;
// Return early. There is only one copy needed because the offset is already 512-byte // Return early. There is only one copy needed because the offset is already 512-byte
// aligned // aligned
@ -85,13 +73,13 @@ namespace dawn_native { namespace d3d12 {
ASSERT(alignedOffset < offset); ASSERT(alignedOffset < offset);
uint32_t texelOffsetX, texelOffsetY, texelOffsetZ; Origin3D texelOffset;
ComputeTexelOffsets(offset - alignedOffset, rowPitch, rowPitch * height, texelSize, ComputeTexelOffsets(offset - alignedOffset, rowPitch, rowPitch * copySize.height, texelSize,
&texelOffsetX, &texelOffsetY, &texelOffsetZ); &texelOffset);
uint32_t rowPitchInTexels = rowPitch / texelSize; uint32_t rowPitchInTexels = rowPitch / texelSize;
if (width + texelOffsetX <= rowPitchInTexels) { if (copySize.width + texelOffset.x <= rowPitchInTexels) {
// The region's rows fit inside the row pitch. In this case, extend the width of the // The region's rows fit inside the row pitch. In this case, extend the width of the
// PlacedFootprint and copy the buffer with an offset location // PlacedFootprint and copy the buffer with an offset location
// |<--------------- row pitch --------------->| // |<--------------- row pitch --------------->|
@ -117,20 +105,14 @@ namespace dawn_native { namespace d3d12 {
copy.count = 1; copy.count = 1;
copy.copies[0].textureOffset.x = x; copy.copies[0].textureOffset = origin;
copy.copies[0].textureOffset.y = y;
copy.copies[0].textureOffset.z = z;
copy.copies[0].copySize.width = width; copy.copies[0].copySize = copySize;
copy.copies[0].copySize.height = height;
copy.copies[0].copySize.depth = depth;
copy.copies[0].bufferOffset.x = texelOffsetX; copy.copies[0].bufferOffset = texelOffset;
copy.copies[0].bufferOffset.y = texelOffsetY; copy.copies[0].bufferSize.width = copySize.width + texelOffset.x;
copy.copies[0].bufferOffset.z = texelOffsetZ; copy.copies[0].bufferSize.height = copySize.height + texelOffset.y;
copy.copies[0].bufferSize.width = width + texelOffsetX; copy.copies[0].bufferSize.depth = copySize.depth + texelOffset.z;
copy.copies[0].bufferSize.height = height + texelOffsetY;
copy.copies[0].bufferSize.depth = depth + texelOffsetZ;
return copy; return copy;
} }
@ -171,37 +153,33 @@ namespace dawn_native { namespace d3d12 {
copy.count = 2; copy.count = 2;
copy.copies[0].textureOffset.x = x; copy.copies[0].textureOffset = origin;
copy.copies[0].textureOffset.y = y;
copy.copies[0].textureOffset.z = z;
ASSERT(rowPitchInTexels > texelOffsetX); ASSERT(rowPitchInTexels > texelOffset.x);
copy.copies[0].copySize.width = rowPitchInTexels - texelOffsetX; copy.copies[0].copySize.width = rowPitchInTexels - texelOffset.x;
copy.copies[0].copySize.height = height; copy.copies[0].copySize.height = copySize.height;
copy.copies[0].copySize.depth = depth; copy.copies[0].copySize.depth = copySize.depth;
copy.copies[0].bufferOffset.x = texelOffsetX; copy.copies[0].bufferOffset = texelOffset;
copy.copies[0].bufferOffset.y = texelOffsetY;
copy.copies[0].bufferOffset.z = texelOffsetZ;
copy.copies[0].bufferSize.width = rowPitchInTexels; copy.copies[0].bufferSize.width = rowPitchInTexels;
copy.copies[0].bufferSize.height = height + texelOffsetY; copy.copies[0].bufferSize.height = copySize.height + texelOffset.y;
copy.copies[0].bufferSize.depth = depth + texelOffsetZ; copy.copies[0].bufferSize.depth = copySize.depth + texelOffset.z;
copy.copies[1].textureOffset.x = x + copy.copies[0].copySize.width; copy.copies[1].textureOffset.x = origin.x + copy.copies[0].copySize.width;
copy.copies[1].textureOffset.y = y; copy.copies[1].textureOffset.y = origin.y;
copy.copies[1].textureOffset.z = z; copy.copies[1].textureOffset.z = origin.z;
ASSERT(width > copy.copies[0].copySize.width); ASSERT(copySize.width > copy.copies[0].copySize.width);
copy.copies[1].copySize.width = width - copy.copies[0].copySize.width; copy.copies[1].copySize.width = copySize.width - copy.copies[0].copySize.width;
copy.copies[1].copySize.height = height; copy.copies[1].copySize.height = copySize.height;
copy.copies[1].copySize.depth = depth; copy.copies[1].copySize.depth = copySize.depth;
copy.copies[1].bufferOffset.x = 0; copy.copies[1].bufferOffset.x = 0;
copy.copies[1].bufferOffset.y = texelOffsetY + 1; copy.copies[1].bufferOffset.y = texelOffset.y + 1;
copy.copies[1].bufferOffset.z = texelOffsetZ; copy.copies[1].bufferOffset.z = texelOffset.z;
copy.copies[1].bufferSize.width = copy.copies[1].copySize.width; copy.copies[1].bufferSize.width = copy.copies[1].copySize.width;
copy.copies[1].bufferSize.height = height + texelOffsetY + 1; copy.copies[1].bufferSize.height = copySize.height + texelOffset.y + 1;
copy.copies[1].bufferSize.depth = depth + texelOffsetZ; copy.copies[1].bufferSize.depth = copySize.depth + texelOffset.z;
return copy; return copy;
} }

View File

@ -24,24 +24,12 @@ namespace dawn_native { namespace d3d12 {
struct TextureCopySplit { struct TextureCopySplit {
static constexpr unsigned int kMaxTextureCopyRegions = 2; static constexpr unsigned int kMaxTextureCopyRegions = 2;
struct Extent {
uint32_t width = 0;
uint32_t height = 0;
uint32_t depth = 0;
};
struct Origin {
uint32_t x = 0;
uint32_t y = 0;
uint32_t z = 0;
};
struct CopyInfo { struct CopyInfo {
Origin textureOffset; Origin3D textureOffset;
Origin bufferOffset; Origin3D bufferOffset;
Extent bufferSize; Extent3D bufferSize;
Extent copySize; Extent3D copySize;
}; };
uint32_t offset = 0; uint32_t offset = 0;
@ -49,12 +37,8 @@ namespace dawn_native { namespace d3d12 {
std::array<CopyInfo, kMaxTextureCopyRegions> copies; std::array<CopyInfo, kMaxTextureCopyRegions> copies;
}; };
TextureCopySplit ComputeTextureCopySplit(uint32_t x, TextureCopySplit ComputeTextureCopySplit(Origin3D origin,
uint32_t y, Extent3D copySize,
uint32_t z,
uint32_t width,
uint32_t height,
uint32_t depth,
uint32_t texelSize, uint32_t texelSize,
uint32_t offset, uint32_t offset,
uint32_t rowPitch); uint32_t rowPitch);

View File

@ -253,24 +253,25 @@ namespace dawn_native { namespace metal {
CopyBufferToTextureCmd* copy = mCommands.NextCommand<CopyBufferToTextureCmd>(); CopyBufferToTextureCmd* copy = mCommands.NextCommand<CopyBufferToTextureCmd>();
auto& src = copy->source; auto& src = copy->source;
auto& dst = copy->destination; auto& dst = copy->destination;
auto& copySize = copy->copySize;
Buffer* buffer = ToBackend(src.buffer.Get()); Buffer* buffer = ToBackend(src.buffer.Get());
Texture* texture = ToBackend(dst.texture.Get()); Texture* texture = ToBackend(dst.texture.Get());
MTLOrigin origin; MTLOrigin origin;
origin.x = dst.x; origin.x = dst.origin.x;
origin.y = dst.y; origin.y = dst.origin.y;
origin.z = dst.z; origin.z = dst.origin.z;
MTLSize size; MTLSize size;
size.width = dst.width; size.width = copySize.width;
size.height = dst.height; size.height = copySize.height;
size.depth = dst.depth; size.depth = copySize.depth;
encoders.EnsureBlit(commandBuffer); encoders.EnsureBlit(commandBuffer);
[encoders.blit copyFromBuffer:buffer->GetMTLBuffer() [encoders.blit copyFromBuffer:buffer->GetMTLBuffer()
sourceOffset:src.offset sourceOffset:src.offset
sourceBytesPerRow:copy->rowPitch sourceBytesPerRow:src.rowPitch
sourceBytesPerImage:(copy->rowPitch * dst.height) sourceBytesPerImage:(src.rowPitch * copySize.height)
sourceSize:size sourceSize:size
toTexture:texture->GetMTLTexture() toTexture:texture->GetMTLTexture()
destinationSlice:dst.slice destinationSlice:dst.slice
@ -282,18 +283,19 @@ namespace dawn_native { namespace metal {
CopyTextureToBufferCmd* copy = mCommands.NextCommand<CopyTextureToBufferCmd>(); CopyTextureToBufferCmd* copy = mCommands.NextCommand<CopyTextureToBufferCmd>();
auto& src = copy->source; auto& src = copy->source;
auto& dst = copy->destination; auto& dst = copy->destination;
auto& copySize = copy->copySize;
Texture* texture = ToBackend(src.texture.Get()); Texture* texture = ToBackend(src.texture.Get());
Buffer* buffer = ToBackend(dst.buffer.Get()); Buffer* buffer = ToBackend(dst.buffer.Get());
MTLOrigin origin; MTLOrigin origin;
origin.x = src.x; origin.x = src.origin.x;
origin.y = src.y; origin.y = src.origin.y;
origin.z = src.z; origin.z = src.origin.z;
MTLSize size; MTLSize size;
size.width = src.width; size.width = copySize.width;
size.height = src.height; size.height = copySize.height;
size.depth = src.depth; size.depth = copySize.depth;
encoders.EnsureBlit(commandBuffer); encoders.EnsureBlit(commandBuffer);
[encoders.blit copyFromTexture:texture->GetMTLTexture() [encoders.blit copyFromTexture:texture->GetMTLTexture()
@ -303,8 +305,8 @@ namespace dawn_native { namespace metal {
sourceSize:size sourceSize:size
toBuffer:buffer->GetMTLBuffer() toBuffer:buffer->GetMTLBuffer()
destinationOffset:dst.offset destinationOffset:dst.offset
destinationBytesPerRow:copy->rowPitch destinationBytesPerRow:dst.rowPitch
destinationBytesPerImage:copy->rowPitch * src.height]; destinationBytesPerImage:dst.rowPitch * copySize.height];
} break; } break;
default: { UNREACHABLE(); } break; default: { UNREACHABLE(); } break;

View File

@ -321,6 +321,7 @@ namespace dawn_native { namespace opengl {
CopyBufferToTextureCmd* copy = mCommands.NextCommand<CopyBufferToTextureCmd>(); CopyBufferToTextureCmd* copy = mCommands.NextCommand<CopyBufferToTextureCmd>();
auto& src = copy->source; auto& src = copy->source;
auto& dst = copy->destination; auto& dst = copy->destination;
auto& copySize = copy->copySize;
Buffer* buffer = ToBackend(src.buffer.Get()); Buffer* buffer = ToBackend(src.buffer.Get());
Texture* texture = ToBackend(dst.texture.Get()); Texture* texture = ToBackend(dst.texture.Get());
GLenum target = texture->GetGLTarget(); GLenum target = texture->GetGLTarget();
@ -331,18 +332,18 @@ namespace dawn_native { namespace opengl {
glBindTexture(target, texture->GetHandle()); glBindTexture(target, texture->GetHandle());
glPixelStorei(GL_UNPACK_ROW_LENGTH, glPixelStorei(GL_UNPACK_ROW_LENGTH,
copy->rowPitch / TextureFormatPixelSize(texture->GetFormat())); src.rowPitch / TextureFormatPixelSize(texture->GetFormat()));
switch (texture->GetDimension()) { switch (texture->GetDimension()) {
case dawn::TextureDimension::e2D: case dawn::TextureDimension::e2D:
if (texture->GetArrayLayers() > 1) { if (texture->GetArrayLayers() > 1) {
glTexSubImage3D( glTexSubImage3D(
target, dst.level, dst.x, dst.y, dst.slice, dst.width, target, dst.level, dst.origin.x, dst.origin.y, dst.slice,
dst.height, 1, format.format, format.type, copySize.width, copySize.height, 1, format.format, format.type,
reinterpret_cast<void*>(static_cast<uintptr_t>(src.offset))); reinterpret_cast<void*>(static_cast<uintptr_t>(src.offset)));
} else { } else {
glTexSubImage2D( glTexSubImage2D(
target, dst.level, dst.x, dst.y, dst.width, dst.height, target, dst.level, dst.origin.x, dst.origin.y, copySize.width,
format.format, format.type, copySize.height, format.format, format.type,
reinterpret_cast<void*>(static_cast<uintptr_t>(src.offset))); reinterpret_cast<void*>(static_cast<uintptr_t>(src.offset)));
} }
break; break;
@ -359,6 +360,7 @@ namespace dawn_native { namespace opengl {
CopyTextureToBufferCmd* copy = mCommands.NextCommand<CopyTextureToBufferCmd>(); CopyTextureToBufferCmd* copy = mCommands.NextCommand<CopyTextureToBufferCmd>();
auto& src = copy->source; auto& src = copy->source;
auto& dst = copy->destination; auto& dst = copy->destination;
auto& copySize = copy->copySize;
Texture* texture = ToBackend(src.texture.Get()); Texture* texture = ToBackend(src.texture.Get());
Buffer* buffer = ToBackend(dst.buffer.Get()); Buffer* buffer = ToBackend(dst.buffer.Get());
auto format = texture->GetGLFormat(); auto format = texture->GetGLFormat();
@ -390,11 +392,11 @@ namespace dawn_native { namespace opengl {
glBindBuffer(GL_PIXEL_PACK_BUFFER, buffer->GetHandle()); glBindBuffer(GL_PIXEL_PACK_BUFFER, buffer->GetHandle());
glPixelStorei(GL_PACK_ROW_LENGTH, glPixelStorei(GL_PACK_ROW_LENGTH,
copy->rowPitch / TextureFormatPixelSize(texture->GetFormat())); dst.rowPitch / TextureFormatPixelSize(texture->GetFormat()));
ASSERT(src.depth == 1 && src.z == 0); ASSERT(copySize.depth == 1 && src.origin.z == 0);
void* offset = reinterpret_cast<void*>(static_cast<uintptr_t>(dst.offset)); void* offset = reinterpret_cast<void*>(static_cast<uintptr_t>(dst.offset));
glReadPixels(src.x, src.y, src.width, src.height, format.format, format.type, glReadPixels(src.origin.x, src.origin.y, copySize.width, copySize.height,
offset); format.format, format.type, offset);
glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);

View File

@ -39,30 +39,31 @@ namespace dawn_native { namespace vulkan {
} }
} }
VkBufferImageCopy ComputeBufferImageCopyRegion(uint32_t rowPitch, VkBufferImageCopy ComputeBufferImageCopyRegion(const BufferCopy& bufferCopy,
const BufferCopyLocation& bufferLocation, const TextureCopy& textureCopy,
const TextureCopyLocation& textureLocation) { const Extent3D& copySize) {
const Texture* texture = ToBackend(textureLocation.texture).Get(); const Texture* texture = ToBackend(textureCopy.texture.Get());
VkBufferImageCopy region; VkBufferImageCopy region;
region.bufferOffset = bufferLocation.offset; region.bufferOffset = bufferCopy.offset;
// In Vulkan the row length is in texels while it is in bytes for Dawn // In Vulkan the row length is in texels while it is in bytes for Dawn
region.bufferRowLength = rowPitch / TextureFormatPixelSize(texture->GetFormat()); region.bufferRowLength =
region.bufferImageHeight = rowPitch * textureLocation.height; bufferCopy.rowPitch / TextureFormatPixelSize(texture->GetFormat());
region.bufferImageHeight = bufferCopy.rowPitch * copySize.height;
region.imageSubresource.aspectMask = texture->GetVkAspectMask(); region.imageSubresource.aspectMask = texture->GetVkAspectMask();
region.imageSubresource.mipLevel = textureLocation.level; region.imageSubresource.mipLevel = textureCopy.level;
region.imageSubresource.baseArrayLayer = textureLocation.slice; region.imageSubresource.baseArrayLayer = textureCopy.slice;
region.imageSubresource.layerCount = 1; region.imageSubresource.layerCount = 1;
region.imageOffset.x = textureLocation.x; region.imageOffset.x = textureCopy.origin.x;
region.imageOffset.y = textureLocation.y; region.imageOffset.y = textureCopy.origin.y;
region.imageOffset.z = textureLocation.z; region.imageOffset.z = textureCopy.origin.z;
region.imageExtent.width = textureLocation.width; region.imageExtent.width = copySize.width;
region.imageExtent.height = textureLocation.height; region.imageExtent.height = copySize.height;
region.imageExtent.depth = textureLocation.depth; region.imageExtent.depth = copySize.depth;
return region; return region;
} }
@ -173,7 +174,7 @@ namespace dawn_native { namespace vulkan {
VkImage dstImage = ToBackend(dst.texture)->GetHandle(); VkImage dstImage = ToBackend(dst.texture)->GetHandle();
VkBufferImageCopy region = VkBufferImageCopy region =
ComputeBufferImageCopyRegion(copy->rowPitch, src, dst); ComputeBufferImageCopyRegion(src, dst, copy->copySize);
// The image is written to so the Dawn guarantees make sure it is in the // The image is written to so the Dawn guarantees make sure it is in the
// TRANSFER_DST_OPTIMAL layout // TRANSFER_DST_OPTIMAL layout
@ -196,7 +197,7 @@ namespace dawn_native { namespace vulkan {
VkBuffer dstBuffer = ToBackend(dst.buffer)->GetHandle(); VkBuffer dstBuffer = ToBackend(dst.buffer)->GetHandle();
VkBufferImageCopy region = VkBufferImageCopy region =
ComputeBufferImageCopyRegion(copy->rowPitch, dst, src); ComputeBufferImageCopyRegion(dst, src, copy->copySize);
// The Dawn TransferSrc usage is always mapped to GENERAL // The Dawn TransferSrc usage is always mapped to GENERAL
device->fn.CmdCopyImageToBuffer(commands, srcImage, VK_IMAGE_LAYOUT_GENERAL, device->fn.CmdCopyImageToBuffer(commands, srcImage, VK_IMAGE_LAYOUT_GENERAL,

View File

@ -220,7 +220,10 @@ namespace {
class CopySplitTest : public testing::Test { class CopySplitTest : public testing::Test {
protected: protected:
TextureCopySplit DoTest(const TextureSpec& textureSpec, const BufferSpec& bufferSpec) { TextureCopySplit DoTest(const TextureSpec& textureSpec, const BufferSpec& bufferSpec) {
TextureCopySplit copySplit = ComputeTextureCopySplit(textureSpec.x, textureSpec.y, textureSpec.z, textureSpec.width, textureSpec.height, textureSpec.depth, textureSpec.texelSize, bufferSpec.offset, bufferSpec.rowPitch); TextureCopySplit copySplit = ComputeTextureCopySplit(
{textureSpec.x, textureSpec.y, textureSpec.z},
{textureSpec.width, textureSpec.height, textureSpec.depth}, textureSpec.texelSize,
bufferSpec.offset, bufferSpec.rowPitch);
ValidateCopySplit(textureSpec, bufferSpec, copySplit); ValidateCopySplit(textureSpec, bufferSpec, copySplit);
return copySplit; return copySplit;
} }