Cleanup: Make TexelBlockInfo a member of Format, not superclass

Bug: dawn:439
Change-Id: I1255086d29e8b85045776f183b3fb563eec0b090
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/27940
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Austin Eng 2020-09-02 18:50:09 +00:00 committed by Commit Bot service account
parent 8ec8f31e3b
commit ea82272fd6
17 changed files with 121 additions and 105 deletions

View File

@ -162,7 +162,6 @@ namespace dawn_native {
} }
} }
// TODO(jiawei.shao@intel.com): support copying with depth stencil textures
bool IsFullBufferOverwrittenInTextureToBufferCopy(const CopyTextureToBufferCmd* copy) { bool IsFullBufferOverwrittenInTextureToBufferCopy(const CopyTextureToBufferCmd* copy) {
ASSERT(copy != nullptr); ASSERT(copy != nullptr);
@ -175,15 +174,16 @@ namespace dawn_native {
} }
const TextureBase* texture = copy->source.texture.Get(); const TextureBase* texture = copy->source.texture.Get();
const uint64_t copyTextureDataSizePerRow = copy->copySize.width / const TexelBlockInfo& blockInfo =
texture->GetFormat().blockWidth * texture->GetFormat().GetTexelBlockInfo(copy->source.aspect);
texture->GetFormat().blockByteSize; const uint64_t copyTextureDataSizePerRow =
copy->copySize.width / blockInfo.blockWidth * blockInfo.blockByteSize;
if (copy->destination.bytesPerRow > copyTextureDataSizePerRow) { if (copy->destination.bytesPerRow > copyTextureDataSizePerRow) {
return false; return false;
} }
const uint64_t overwrittenRangeSize = const uint64_t overwrittenRangeSize = copyTextureDataSizePerRow *
copyTextureDataSizePerRow * (copy->copySize.height / texture->GetFormat().blockHeight) * (copy->copySize.height / blockInfo.blockHeight) *
copy->copySize.depth; copy->copySize.depth;
if (copy->destination.buffer->GetSize() > overwrittenRangeSize) { if (copy->destination.buffer->GetSize() > overwrittenRangeSize) {
return false; return false;

View File

@ -622,12 +622,12 @@ namespace dawn_native {
DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst)); DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));
DAWN_TRY(ValidateTextureSampleCountInBufferCopyCommands(destination->texture)); DAWN_TRY(ValidateTextureSampleCountInBufferCopyCommands(destination->texture));
DAWN_TRY(ValidateBufferToTextureCopyRestrictions(*destination));
// We validate texture copy range before validating linear texture data, // We validate texture copy range before validating linear texture data,
// because in the latter we divide copyExtent.width by blockWidth and // because in the latter we divide copyExtent.width by blockWidth and
// copyExtent.height by blockHeight while the divisibility conditions are // copyExtent.height by blockHeight while the divisibility conditions are
// checked in validating texture copy range. // checked in validating texture copy range.
DAWN_TRY(ValidateTextureCopyRange(*destination, *copySize)); DAWN_TRY(ValidateTextureCopyRange(*destination, *copySize));
DAWN_TRY(ValidateBufferToTextureCopyRestrictions(*destination));
DAWN_TRY(ValidateLinearTextureData( DAWN_TRY(ValidateLinearTextureData(
source->layout, source->buffer->GetSize(), source->layout, source->buffer->GetSize(),
destination->texture->GetFormat().GetTexelBlockInfo(destination->aspect), destination->texture->GetFormat().GetTexelBlockInfo(destination->aspect),
@ -644,11 +644,12 @@ namespace dawn_native {
} }
// In the case of one row copy bytesPerRow might not contain enough bytes // In the case of one row copy bytesPerRow might not contain enough bytes
const TexelBlockInfo& blockInfo =
destination->texture->GetFormat().GetTexelBlockInfo(destination->aspect);
uint32_t bytesPerRow = source->layout.bytesPerRow; uint32_t bytesPerRow = source->layout.bytesPerRow;
if (copySize->height <= 1 && copySize->depth <= 1) { if (copySize->height <= 1 && copySize->depth <= 1) {
bytesPerRow = bytesPerRow =
Align(copySize->width * destination->texture->GetFormat().blockByteSize, Align(copySize->width * blockInfo.blockByteSize, kTextureBytesPerRowAlignment);
kTextureBytesPerRowAlignment);
} }
// Record the copy command. // Record the copy command.
@ -681,12 +682,12 @@ namespace dawn_native {
DAWN_TRY(ValidateBufferCopyView(GetDevice(), *destination)); DAWN_TRY(ValidateBufferCopyView(GetDevice(), *destination));
DAWN_TRY(ValidateCanUseAs(destination->buffer, wgpu::BufferUsage::CopyDst)); DAWN_TRY(ValidateCanUseAs(destination->buffer, wgpu::BufferUsage::CopyDst));
DAWN_TRY(ValidateTextureToBufferCopyRestrictions(*source));
// We validate texture copy range before validating linear texture data, // We validate texture copy range before validating linear texture data,
// because in the latter we divide copyExtent.width by blockWidth and // because in the latter we divide copyExtent.width by blockWidth and
// copyExtent.height by blockHeight while the divisibility conditions are // copyExtent.height by blockHeight while the divisibility conditions are
// checked in validating texture copy range. // checked in validating texture copy range.
DAWN_TRY(ValidateTextureCopyRange(*source, *copySize)); DAWN_TRY(ValidateTextureCopyRange(*source, *copySize));
DAWN_TRY(ValidateTextureToBufferCopyRestrictions(*source));
DAWN_TRY(ValidateLinearTextureData( DAWN_TRY(ValidateLinearTextureData(
destination->layout, destination->buffer->GetSize(), destination->layout, destination->buffer->GetSize(),
source->texture->GetFormat().GetTexelBlockInfo(source->aspect), *copySize)); source->texture->GetFormat().GetTexelBlockInfo(source->aspect), *copySize));
@ -702,10 +703,12 @@ namespace dawn_native {
} }
// In the case of one row copy bytesPerRow might not contain enough bytes // In the case of one row copy bytesPerRow might not contain enough bytes
const TexelBlockInfo& blockInfo =
source->texture->GetFormat().GetTexelBlockInfo(source->aspect);
uint32_t bytesPerRow = destination->layout.bytesPerRow; uint32_t bytesPerRow = destination->layout.bytesPerRow;
if (copySize->height <= 1 && copySize->depth <= 1) { if (copySize->height <= 1 && copySize->depth <= 1) {
bytesPerRow = Align(copySize->width * source->texture->GetFormat().blockByteSize, bytesPerRow =
kTextureBytesPerRowAlignment); Align(copySize->width * blockInfo.blockByteSize, kTextureBytesPerRowAlignment);
} }
// Record the copy command. // Record the copy command.
@ -733,15 +736,15 @@ namespace dawn_native {
DAWN_TRY(GetDevice()->ValidateObject(source->texture)); DAWN_TRY(GetDevice()->ValidateObject(source->texture));
DAWN_TRY(GetDevice()->ValidateObject(destination->texture)); DAWN_TRY(GetDevice()->ValidateObject(destination->texture));
DAWN_TRY(ValidateTextureCopyView(GetDevice(), *source, *copySize));
DAWN_TRY(ValidateTextureCopyView(GetDevice(), *destination, *copySize));
DAWN_TRY( DAWN_TRY(
ValidateTextureToTextureCopyRestrictions(*source, *destination, *copySize)); ValidateTextureToTextureCopyRestrictions(*source, *destination, *copySize));
DAWN_TRY(ValidateTextureCopyRange(*source, *copySize)); DAWN_TRY(ValidateTextureCopyRange(*source, *copySize));
DAWN_TRY(ValidateTextureCopyRange(*destination, *copySize)); DAWN_TRY(ValidateTextureCopyRange(*destination, *copySize));
DAWN_TRY(ValidateTextureCopyView(GetDevice(), *source, *copySize));
DAWN_TRY(ValidateTextureCopyView(GetDevice(), *destination, *copySize));
DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc)); DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst)); DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));

View File

@ -490,16 +490,6 @@ namespace dawn_native {
return DAWN_VALIDATION_ERROR("mipLevel out of range"); return DAWN_VALIDATION_ERROR("mipLevel out of range");
} }
if (textureCopy.origin.x % texture->GetFormat().blockWidth != 0) {
return DAWN_VALIDATION_ERROR(
"Offset.x must be a multiple of compressed texture format block width");
}
if (textureCopy.origin.y % texture->GetFormat().blockHeight != 0) {
return DAWN_VALIDATION_ERROR(
"Offset.y must be a multiple of compressed texture format block height");
}
switch (textureCopy.aspect) { switch (textureCopy.aspect) {
case wgpu::TextureAspect::All: case wgpu::TextureAspect::All:
break; break;
@ -560,12 +550,22 @@ namespace dawn_native {
} }
// Validation for the texel block alignments: // Validation for the texel block alignments:
if (copySize.width % textureCopy.texture->GetFormat().blockWidth != 0) { const TexelBlockInfo& blockInfo =
textureCopy.texture->GetFormat().GetTexelBlockInfo(textureCopy.aspect);
if (textureCopy.origin.x % blockInfo.blockWidth != 0) {
return DAWN_VALIDATION_ERROR(
"Offset.x must be a multiple of compressed texture format block width");
}
if (textureCopy.origin.y % blockInfo.blockHeight != 0) {
return DAWN_VALIDATION_ERROR(
"Offset.y must be a multiple of compressed texture format block height");
}
if (copySize.width % blockInfo.blockWidth != 0) {
return DAWN_VALIDATION_ERROR( return DAWN_VALIDATION_ERROR(
"copySize.width must be a multiple of compressed texture format block width"); "copySize.width must be a multiple of compressed texture format block width");
} }
if (copySize.height % textureCopy.texture->GetFormat().blockHeight != 0) { if (copySize.height % blockInfo.blockHeight != 0) {
return DAWN_VALIDATION_ERROR( return DAWN_VALIDATION_ERROR(
"copySize.height must be a multiple of compressed texture format block height"); "copySize.height must be a multiple of compressed texture format block height");
} }

View File

@ -82,21 +82,13 @@ namespace dawn_native {
TexelBlockInfo Format::GetTexelBlockInfo(wgpu::TextureAspect aspect) const { TexelBlockInfo Format::GetTexelBlockInfo(wgpu::TextureAspect aspect) const {
switch (aspect) { switch (aspect) {
case wgpu::TextureAspect::All: case wgpu::TextureAspect::All:
switch (aspects) { return blockInfo;
case Aspect::Color:
case Aspect::Depth:
case Aspect::Stencil:
break;
default:
UNREACHABLE();
}
return *this;
case wgpu::TextureAspect::DepthOnly: case wgpu::TextureAspect::DepthOnly:
ASSERT(HasDepth()); ASSERT(HasDepth());
switch (format) { switch (format) {
case wgpu::TextureFormat::Depth32Float: case wgpu::TextureFormat::Depth32Float:
return *this; return blockInfo;
default: default:
UNREACHABLE(); UNREACHABLE();
break; break;
@ -126,11 +118,11 @@ namespace dawn_native {
switch (aspect) { switch (aspect) {
case Aspect::Color: case Aspect::Color:
ASSERT(aspects == aspect); ASSERT(aspects == aspect);
return *this; return blockInfo;
case Aspect::Depth: case Aspect::Depth:
switch (format) { switch (format) {
case wgpu::TextureFormat::Depth32Float: case wgpu::TextureFormat::Depth32Float:
return *this; return blockInfo;
default: default:
UNREACHABLE(); UNREACHABLE();
break; break;
@ -195,9 +187,9 @@ namespace dawn_native {
internalFormat.supportsStorageUsage = supportsStorageUsage; internalFormat.supportsStorageUsage = supportsStorageUsage;
internalFormat.aspects = Aspect::Color; internalFormat.aspects = Aspect::Color;
internalFormat.type = type; internalFormat.type = type;
internalFormat.blockByteSize = byteSize; internalFormat.blockInfo.blockByteSize = byteSize;
internalFormat.blockWidth = 1; internalFormat.blockInfo.blockWidth = 1;
internalFormat.blockHeight = 1; internalFormat.blockInfo.blockHeight = 1;
AddFormat(internalFormat); AddFormat(internalFormat);
}; };
@ -211,9 +203,9 @@ namespace dawn_native {
internalFormat.supportsStorageUsage = false; internalFormat.supportsStorageUsage = false;
internalFormat.aspects = aspects; internalFormat.aspects = aspects;
internalFormat.type = Type::Other; internalFormat.type = Type::Other;
internalFormat.blockByteSize = byteSize; internalFormat.blockInfo.blockByteSize = byteSize;
internalFormat.blockWidth = 1; internalFormat.blockInfo.blockWidth = 1;
internalFormat.blockHeight = 1; internalFormat.blockInfo.blockHeight = 1;
AddFormat(internalFormat); AddFormat(internalFormat);
}; };
@ -227,9 +219,9 @@ namespace dawn_native {
internalFormat.supportsStorageUsage = false; internalFormat.supportsStorageUsage = false;
internalFormat.aspects = Aspect::Depth; internalFormat.aspects = Aspect::Depth;
internalFormat.type = type; internalFormat.type = type;
internalFormat.blockByteSize = byteSize; internalFormat.blockInfo.blockByteSize = byteSize;
internalFormat.blockWidth = 1; internalFormat.blockInfo.blockWidth = 1;
internalFormat.blockHeight = 1; internalFormat.blockInfo.blockHeight = 1;
AddFormat(internalFormat); AddFormat(internalFormat);
}; };
@ -243,9 +235,9 @@ namespace dawn_native {
internalFormat.supportsStorageUsage = false; internalFormat.supportsStorageUsage = false;
internalFormat.aspects = Aspect::Color; internalFormat.aspects = Aspect::Color;
internalFormat.type = Type::Float; internalFormat.type = Type::Float;
internalFormat.blockByteSize = byteSize; internalFormat.blockInfo.blockByteSize = byteSize;
internalFormat.blockWidth = width; internalFormat.blockInfo.blockWidth = width;
internalFormat.blockHeight = height; internalFormat.blockInfo.blockHeight = height;
AddFormat(internalFormat); AddFormat(internalFormat);
}; };

View File

@ -39,8 +39,11 @@ namespace dawn_native {
// exact number of known format. // exact number of known format.
static constexpr size_t kKnownFormatCount = 53; static constexpr size_t kKnownFormatCount = 53;
struct Format;
using FormatTable = std::array<Format, kKnownFormatCount>;
// A wgpu::TextureFormat along with all the information about it necessary for validation. // A wgpu::TextureFormat along with all the information about it necessary for validation.
struct Format : TexelBlockInfo { struct Format {
enum class Type { enum class Type {
Float, Float,
Sint, Sint,
@ -72,12 +75,15 @@ namespace dawn_native {
// The index of the format in the list of all known formats: a unique number for each format // The index of the format in the list of all known formats: a unique number for each format
// in [0, kKnownFormatCount) // in [0, kKnownFormatCount)
size_t GetIndex() const; size_t GetIndex() const;
private:
TexelBlockInfo blockInfo;
friend FormatTable BuildFormatTable(const DeviceBase* device);
}; };
// Implementation details of the format table in the device. // Implementation details of the format table in the device.
using FormatTable = std::array<Format, kKnownFormatCount>;
// Returns the index of a format in the FormatTable. // Returns the index of a format in the FormatTable.
size_t ComputeFormatIndex(wgpu::TextureFormat format); size_t ComputeFormatIndex(wgpu::TextureFormat format);
// Builds the format table with the extensions enabled on the device. // Builds the format table with the extensions enabled on the device.

View File

@ -378,12 +378,12 @@ namespace dawn_native {
return DAWN_VALIDATION_ERROR("The sample count of textures must be 1"); return DAWN_VALIDATION_ERROR("The sample count of textures must be 1");
} }
DAWN_TRY(ValidateBufferToTextureCopyRestrictions(*destination));
// We validate texture copy range before validating linear texture data, // We validate texture copy range before validating linear texture data,
// because in the latter we divide copyExtent.width by blockWidth and // because in the latter we divide copyExtent.width by blockWidth and
// copyExtent.height by blockHeight while the divisibility conditions are // copyExtent.height by blockHeight while the divisibility conditions are
// checked in validating texture copy range. // checked in validating texture copy range.
DAWN_TRY(ValidateTextureCopyRange(*destination, *writeSize)); DAWN_TRY(ValidateTextureCopyRange(*destination, *writeSize));
DAWN_TRY(ValidateBufferToTextureCopyRestrictions(*destination));
DAWN_TRY(ValidateLinearTextureData( DAWN_TRY(ValidateLinearTextureData(
*dataLayout, dataSize, *dataLayout, dataSize,
destination->texture->GetFormat().GetTexelBlockInfo(destination->aspect), *writeSize)); destination->texture->GetFormat().GetTexelBlockInfo(destination->aspect), *writeSize));

View File

@ -159,8 +159,9 @@ namespace dawn_native {
return DAWN_VALIDATION_ERROR("Texture has too many mip levels"); return DAWN_VALIDATION_ERROR("Texture has too many mip levels");
} }
if (format->isCompressed && (descriptor->size.width % format->blockWidth != 0 || const TexelBlockInfo& blockInfo = format->GetTexelBlockInfo(wgpu::TextureAspect::All);
descriptor->size.height % format->blockHeight != 0)) { if (format->isCompressed && (descriptor->size.width % blockInfo.blockWidth != 0 ||
descriptor->size.height % blockInfo.blockHeight != 0)) {
return DAWN_VALIDATION_ERROR( return DAWN_VALIDATION_ERROR(
"The size of the texture is incompatible with the texture format"); "The size of the texture is incompatible with the texture format");
} }
@ -557,8 +558,9 @@ namespace dawn_native {
// 4 at non-zero mipmap levels. // 4 at non-zero mipmap levels.
if (mFormat.isCompressed) { if (mFormat.isCompressed) {
// TODO(jiawei.shao@intel.com): check if there are any overflows. // TODO(jiawei.shao@intel.com): check if there are any overflows.
uint32_t blockWidth = mFormat.blockWidth; const TexelBlockInfo& blockInfo = mFormat.GetTexelBlockInfo(wgpu::TextureAspect::All);
uint32_t blockHeight = mFormat.blockHeight; uint32_t blockWidth = blockInfo.blockWidth;
uint32_t blockHeight = blockInfo.blockHeight;
extent.width = (extent.width + blockWidth - 1) / blockWidth * blockWidth; extent.width = (extent.width + blockWidth - 1) / blockWidth * blockWidth;
extent.height = (extent.height + blockHeight - 1) / blockHeight * blockHeight; extent.height = (extent.height + blockHeight - 1) / blockHeight * blockHeight;
} }

View File

@ -952,7 +952,7 @@ namespace dawn_native { namespace d3d12 {
UploadHandle uploadHandle; UploadHandle uploadHandle;
DAWN_TRY_ASSIGN(uploadHandle, DAWN_TRY_ASSIGN(uploadHandle,
uploader->Allocate(bufferSize, device->GetPendingCommandSerial(), uploader->Allocate(bufferSize, device->GetPendingCommandSerial(),
GetFormat().blockByteSize)); blockInfo.blockByteSize));
memset(uploadHandle.mappedBuffer, clearColor, bufferSize); memset(uploadHandle.mappedBuffer, clearColor, bufferSize);
for (uint32_t level = range.baseMipLevel; for (uint32_t level = range.baseMipLevel;

View File

@ -643,7 +643,7 @@ namespace dawn_native { namespace metal {
TextureBufferCopySplit splitCopies = ComputeTextureBufferCopySplit( TextureBufferCopySplit splitCopies = ComputeTextureBufferCopySplit(
texture, dst.mipLevel, dst.origin, copySize, buffer->GetSize(), src.offset, texture, dst.mipLevel, dst.origin, copySize, buffer->GetSize(), src.offset,
src.bytesPerRow, src.rowsPerImage); src.bytesPerRow, src.rowsPerImage, dst.aspect);
for (uint32_t i = 0; i < splitCopies.count; ++i) { for (uint32_t i = 0; i < splitCopies.count; ++i) {
const TextureBufferCopySplit::CopyInfo& copyInfo = splitCopies.copies[i]; const TextureBufferCopySplit::CopyInfo& copyInfo = splitCopies.copies[i];
@ -693,7 +693,7 @@ namespace dawn_native { namespace metal {
TextureBufferCopySplit splitCopies = ComputeTextureBufferCopySplit( TextureBufferCopySplit splitCopies = ComputeTextureBufferCopySplit(
texture, src.mipLevel, src.origin, copySize, buffer->GetSize(), dst.offset, texture, src.mipLevel, src.origin, copySize, buffer->GetSize(), dst.offset,
dst.bytesPerRow, dst.rowsPerImage); dst.bytesPerRow, dst.rowsPerImage, src.aspect);
for (uint32_t i = 0; i < splitCopies.count; ++i) { for (uint32_t i = 0; i < splitCopies.count; ++i) {
const TextureBufferCopySplit::CopyInfo& copyInfo = splitCopies.copies[i]; const TextureBufferCopySplit::CopyInfo& copyInfo = splitCopies.copies[i];

View File

@ -488,15 +488,17 @@ namespace dawn_native { namespace metal {
} else { } else {
// Compute the buffer size big enough to fill the largest mip. // Compute the buffer size big enough to fill the largest mip.
Extent3D largestMipSize = GetMipLevelVirtualSize(range.baseMipLevel); Extent3D largestMipSize = GetMipLevelVirtualSize(range.baseMipLevel);
const TexelBlockInfo& blockInfo =
GetFormat().GetTexelBlockInfo(wgpu::TextureAspect::All);
// Metal validation layers: sourceBytesPerRow must be at least 64. // Metal validation layers: sourceBytesPerRow must be at least 64.
uint32_t largestMipBytesPerRow = std::max( uint32_t largestMipBytesPerRow = std::max(
(largestMipSize.width / GetFormat().blockWidth) * GetFormat().blockByteSize, 64u); (largestMipSize.width / blockInfo.blockWidth) * blockInfo.blockByteSize, 64u);
// Metal validation layers: sourceBytesPerImage must be at least 512. // Metal validation layers: sourceBytesPerImage must be at least 512.
uint64_t largestMipBytesPerImage = uint64_t largestMipBytesPerImage =
std::max(static_cast<uint64_t>(largestMipBytesPerRow) * std::max(static_cast<uint64_t>(largestMipBytesPerRow) *
(largestMipSize.height / GetFormat().blockHeight), (largestMipSize.height / blockInfo.blockHeight),
512llu); 512llu);
// TODO(enga): Multiply by largestMipSize.depth and do a larger 3D copy to clear a whole // TODO(enga): Multiply by largestMipSize.depth and do a larger 3D copy to clear a whole
@ -511,7 +513,7 @@ namespace dawn_native { namespace metal {
UploadHandle uploadHandle; UploadHandle uploadHandle;
DAWN_TRY_ASSIGN(uploadHandle, DAWN_TRY_ASSIGN(uploadHandle,
uploader->Allocate(bufferSize, device->GetPendingCommandSerial(), uploader->Allocate(bufferSize, device->GetPendingCommandSerial(),
GetFormat().blockByteSize)); blockInfo.blockByteSize));
memset(uploadHandle.mappedBuffer, clearColor, bufferSize); memset(uploadHandle.mappedBuffer, clearColor, bufferSize);
id<MTLBlitCommandEncoder> encoder = commandContext->EnsureBlit(); id<MTLBlitCommandEncoder> encoder = commandContext->EnsureBlit();

View File

@ -47,7 +47,8 @@ namespace dawn_native { namespace metal {
uint64_t bufferSize, uint64_t bufferSize,
uint64_t bufferOffset, uint64_t bufferOffset,
uint32_t bytesPerRow, uint32_t bytesPerRow,
uint32_t rowsPerImage); uint32_t rowsPerImage,
Aspect aspect);
void EnsureDestinationTextureInitialized(Texture* texture, void EnsureDestinationTextureInitialized(Texture* texture,
const TextureCopy& dst, const TextureCopy& dst,

View File

@ -49,9 +49,11 @@ namespace dawn_native { namespace metal {
uint64_t bufferSize, uint64_t bufferSize,
uint64_t bufferOffset, uint64_t bufferOffset,
uint32_t bytesPerRow, uint32_t bytesPerRow,
uint32_t rowsPerImage) { uint32_t rowsPerImage,
Aspect aspect) {
TextureBufferCopySplit copy; TextureBufferCopySplit copy;
const Format textureFormat = texture->GetFormat(); const Format textureFormat = texture->GetFormat();
const TexelBlockInfo& blockInfo = textureFormat.GetTexelBlockInfo(aspect);
// When copying textures from/to an unpacked buffer, the Metal validation layer doesn't // When copying textures from/to an unpacked buffer, the Metal validation layer doesn't
// compute the correct range when checking if the buffer is big enough to contain the // compute the correct range when checking if the buffer is big enough to contain the
@ -70,7 +72,7 @@ namespace dawn_native { namespace metal {
// We work around this limitation by detecting when Metal would complain and copy the // We work around this limitation by detecting when Metal would complain and copy the
// last image and row separately using tight sourceBytesPerRow or sourceBytesPerImage. // last image and row separately using tight sourceBytesPerRow or sourceBytesPerImage.
uint32_t dataRowsPerImage = rowsPerImage / textureFormat.blockHeight; uint32_t dataRowsPerImage = rowsPerImage / blockInfo.blockHeight;
uint32_t bytesPerImage = bytesPerRow * dataRowsPerImage; uint32_t bytesPerImage = bytesPerRow * dataRowsPerImage;
// Metal validation layer requires that if the texture's pixel format is a compressed // Metal validation layer requires that if the texture's pixel format is a compressed
@ -113,7 +115,7 @@ namespace dawn_native { namespace metal {
} }
// Doing all the copy in last image except the last row. // Doing all the copy in last image except the last row.
uint32_t copyBlockRowCount = copyExtent.height / textureFormat.blockHeight; uint32_t copyBlockRowCount = copyExtent.height / blockInfo.blockHeight;
if (copyBlockRowCount > 1) { if (copyBlockRowCount > 1) {
copy.copies[copy.count].bufferOffset = currentOffset; copy.copies[copy.count].bufferOffset = currentOffset;
copy.copies[copy.count].bytesPerRow = bytesPerRow; copy.copies[copy.count].bytesPerRow = bytesPerRow;
@ -121,10 +123,10 @@ namespace dawn_native { namespace metal {
copy.copies[copy.count].textureOrigin = {origin.x, origin.y, copy.copies[copy.count].textureOrigin = {origin.x, origin.y,
origin.z + copyExtent.depth - 1}; origin.z + copyExtent.depth - 1};
ASSERT(copyExtent.height - textureFormat.blockHeight < ASSERT(copyExtent.height - blockInfo.blockHeight <
texture->GetMipLevelVirtualSize(mipLevel).height); texture->GetMipLevelVirtualSize(mipLevel).height);
copy.copies[copy.count].copyExtent = {clampedCopyExtent.width, copy.copies[copy.count].copyExtent = {clampedCopyExtent.width,
copyExtent.height - textureFormat.blockHeight, 1}; copyExtent.height - blockInfo.blockHeight, 1};
++copy.count; ++copy.count;
@ -135,16 +137,16 @@ namespace dawn_native { namespace metal {
// Doing the last row copy with the exact number of bytes in last row. // Doing the last row copy with the exact number of bytes in last row.
// Workaround this issue in a way just like the copy to a 1D texture. // Workaround this issue in a way just like the copy to a 1D texture.
uint32_t lastRowDataSize = uint32_t lastRowDataSize =
(copyExtent.width / textureFormat.blockWidth) * textureFormat.blockByteSize; (copyExtent.width / blockInfo.blockWidth) * blockInfo.blockByteSize;
uint32_t lastRowCopyExtentHeight = uint32_t lastRowCopyExtentHeight =
textureFormat.blockHeight + clampedCopyExtent.height - copyExtent.height; blockInfo.blockHeight + clampedCopyExtent.height - copyExtent.height;
ASSERT(lastRowCopyExtentHeight <= textureFormat.blockHeight); ASSERT(lastRowCopyExtentHeight <= blockInfo.blockHeight);
copy.copies[copy.count].bufferOffset = currentOffset; copy.copies[copy.count].bufferOffset = currentOffset;
copy.copies[copy.count].bytesPerRow = lastRowDataSize; copy.copies[copy.count].bytesPerRow = lastRowDataSize;
copy.copies[copy.count].bytesPerImage = lastRowDataSize; copy.copies[copy.count].bytesPerImage = lastRowDataSize;
copy.copies[copy.count].textureOrigin = { copy.copies[copy.count].textureOrigin = {
origin.x, origin.y + copyExtent.height - textureFormat.blockHeight, origin.x, origin.y + copyExtent.height - blockInfo.blockHeight,
origin.z + copyExtent.depth - 1}; origin.z + copyExtent.depth - 1};
copy.copies[copy.count].copyExtent = {clampedCopyExtent.width, lastRowCopyExtentHeight, 1}; copy.copies[copy.count].copyExtent = {clampedCopyExtent.width, lastRowCopyExtentHeight, 1};
++copy.count; ++copy.count;

View File

@ -540,21 +540,21 @@ namespace dawn_native { namespace opengl {
gl.BindTexture(target, texture->GetHandle()); gl.BindTexture(target, texture->GetHandle());
const Format& formatInfo = texture->GetFormat(); const Format& formatInfo = texture->GetFormat();
gl.PixelStorei( const TexelBlockInfo& blockInfo = formatInfo.GetTexelBlockInfo(dst.aspect);
GL_UNPACK_ROW_LENGTH, gl.PixelStorei(GL_UNPACK_ROW_LENGTH, src.bytesPerRow / blockInfo.blockByteSize *
src.bytesPerRow / formatInfo.blockByteSize * formatInfo.blockWidth); blockInfo.blockWidth);
gl.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, src.rowsPerImage); gl.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, src.rowsPerImage);
if (formatInfo.isCompressed) { if (formatInfo.isCompressed) {
gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_SIZE, formatInfo.blockByteSize); gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_SIZE, blockInfo.blockByteSize);
gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_WIDTH, formatInfo.blockWidth); gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_WIDTH, blockInfo.blockWidth);
gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_HEIGHT, formatInfo.blockHeight); gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_HEIGHT, blockInfo.blockHeight);
gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_DEPTH, 1); gl.PixelStorei(GL_UNPACK_COMPRESSED_BLOCK_DEPTH, 1);
ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D); ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D);
uint64_t copyDataSize = (copySize.width / formatInfo.blockWidth) * uint64_t copyDataSize = (copySize.width / blockInfo.blockWidth) *
(copySize.height / formatInfo.blockHeight) * (copySize.height / blockInfo.blockHeight) *
formatInfo.blockByteSize * copySize.depth; blockInfo.blockByteSize * copySize.depth;
Extent3D copyExtent = ComputeTextureCopyExtent(dst, copySize); Extent3D copyExtent = ComputeTextureCopyExtent(dst, copySize);
if (texture->GetArrayLayers() > 1) { if (texture->GetArrayLayers() > 1) {

View File

@ -289,7 +289,9 @@ namespace dawn_native { namespace opengl {
ASSERT(range.aspects == Aspect::Color); ASSERT(range.aspects == Aspect::Color);
static constexpr uint32_t MAX_TEXEL_SIZE = 16; static constexpr uint32_t MAX_TEXEL_SIZE = 16;
ASSERT(GetFormat().blockByteSize <= MAX_TEXEL_SIZE); const TexelBlockInfo& blockInfo = GetFormat().GetTexelBlockInfo(Aspect::Color);
ASSERT(blockInfo.blockByteSize <= MAX_TEXEL_SIZE);
std::array<GLbyte, MAX_TEXEL_SIZE> clearColorData; std::array<GLbyte, MAX_TEXEL_SIZE> clearColorData;
clearColor = (clearValue == TextureBase::ClearValue::Zero) ? 0 : 255; clearColor = (clearValue == TextureBase::ClearValue::Zero) ? 0 : 255;
clearColorData.fill(clearColor); clearColorData.fill(clearColor);
@ -317,19 +319,20 @@ namespace dawn_native { namespace opengl {
ASSERT(range.aspects == Aspect::Color); ASSERT(range.aspects == Aspect::Color);
// create temp buffer with clear color to copy to the texture image // create temp buffer with clear color to copy to the texture image
ASSERT(kTextureBytesPerRowAlignment % GetFormat().blockByteSize == 0); const TexelBlockInfo& blockInfo = GetFormat().GetTexelBlockInfo(Aspect::Color);
ASSERT(kTextureBytesPerRowAlignment % blockInfo.blockByteSize == 0);
uint32_t bytesPerRow = uint32_t bytesPerRow =
Align((GetWidth() / GetFormat().blockWidth) * GetFormat().blockByteSize, Align((GetWidth() / blockInfo.blockWidth) * blockInfo.blockByteSize,
kTextureBytesPerRowAlignment); kTextureBytesPerRowAlignment);
// Make sure that we are not rounding // Make sure that we are not rounding
ASSERT(bytesPerRow % GetFormat().blockByteSize == 0); ASSERT(bytesPerRow % blockInfo.blockByteSize == 0);
ASSERT(GetHeight() % GetFormat().blockHeight == 0); ASSERT(GetHeight() % blockInfo.blockHeight == 0);
dawn_native::BufferDescriptor descriptor = {}; dawn_native::BufferDescriptor descriptor = {};
descriptor.mappedAtCreation = true; descriptor.mappedAtCreation = true;
descriptor.usage = wgpu::BufferUsage::CopySrc; descriptor.usage = wgpu::BufferUsage::CopySrc;
descriptor.size = bytesPerRow * (GetHeight() / GetFormat().blockHeight); descriptor.size = bytesPerRow * (GetHeight() / blockInfo.blockHeight);
if (descriptor.size > std::numeric_limits<uint32_t>::max()) { if (descriptor.size > std::numeric_limits<uint32_t>::max()) {
return DAWN_OUT_OF_MEMORY_ERROR("Unable to allocate buffer."); return DAWN_OUT_OF_MEMORY_ERROR("Unable to allocate buffer.");
} }
@ -345,7 +348,7 @@ namespace dawn_native { namespace opengl {
// Bind buffer and texture, and make the buffer to texture copy // Bind buffer and texture, and make the buffer to texture copy
gl.PixelStorei(GL_UNPACK_ROW_LENGTH, gl.PixelStorei(GL_UNPACK_ROW_LENGTH,
(bytesPerRow / GetFormat().blockByteSize) * GetFormat().blockWidth); (bytesPerRow / blockInfo.blockByteSize) * blockInfo.blockWidth);
gl.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0); gl.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
for (uint32_t level = range.baseMipLevel; level < range.baseMipLevel + range.levelCount; for (uint32_t level = range.baseMipLevel; level < range.baseMipLevel + range.levelCount;
++level) { ++level) {

View File

@ -376,15 +376,17 @@ namespace dawn_native { namespace vulkan {
const TextureCopy& dstCopy, const TextureCopy& dstCopy,
const Extent3D& copySize) { const Extent3D& copySize) {
ASSERT(srcCopy.texture->GetFormat().format == dstCopy.texture->GetFormat().format); ASSERT(srcCopy.texture->GetFormat().format == dstCopy.texture->GetFormat().format);
ASSERT(srcCopy.aspect == dstCopy.aspect);
dawn_native::Format format = srcCopy.texture->GetFormat(); dawn_native::Format format = srcCopy.texture->GetFormat();
ASSERT(copySize.width % format.blockWidth == 0); const TexelBlockInfo& blockInfo = format.GetTexelBlockInfo(srcCopy.aspect);
ASSERT(copySize.height % format.blockHeight == 0); ASSERT(copySize.width % blockInfo.blockWidth == 0);
ASSERT(copySize.height % blockInfo.blockHeight == 0);
// Create the temporary buffer. Note that We don't need to respect WebGPU's 256 alignment // Create the temporary buffer. Note that We don't need to respect WebGPU's 256 alignment
// because it isn't a hard constraint in Vulkan. // because it isn't a hard constraint in Vulkan.
uint64_t tempBufferSize = uint64_t tempBufferSize =
(copySize.width / format.blockWidth * copySize.height / format.blockHeight) * (copySize.width / blockInfo.blockWidth * copySize.height / blockInfo.blockHeight) *
format.blockByteSize; blockInfo.blockByteSize;
BufferDescriptor tempBufferDescriptor; BufferDescriptor tempBufferDescriptor;
tempBufferDescriptor.size = tempBufferSize; tempBufferDescriptor.size = tempBufferSize;
tempBufferDescriptor.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst; tempBufferDescriptor.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst;
@ -396,7 +398,8 @@ namespace dawn_native { namespace vulkan {
tempBufferCopy.buffer = tempBuffer.Get(); tempBufferCopy.buffer = tempBuffer.Get();
tempBufferCopy.rowsPerImage = copySize.height; tempBufferCopy.rowsPerImage = copySize.height;
tempBufferCopy.offset = 0; tempBufferCopy.offset = 0;
tempBufferCopy.bytesPerRow = copySize.width / format.blockWidth * format.blockByteSize; tempBufferCopy.bytesPerRow =
copySize.width / blockInfo.blockWidth * blockInfo.blockByteSize;
VkCommandBuffer commands = recordingContext->commandBuffer; VkCommandBuffer commands = recordingContext->commandBuffer;
VkImage srcImage = ToBackend(srcCopy.texture)->GetHandle(); VkImage srcImage = ToBackend(srcCopy.texture)->GetHandle();

View File

@ -945,10 +945,12 @@ namespace dawn_native { namespace vulkan {
} }
} else { } else {
// create temp buffer with clear color to copy to the texture image // create temp buffer with clear color to copy to the texture image
const TexelBlockInfo& blockInfo =
GetFormat().GetTexelBlockInfo(wgpu::TextureAspect::All);
uint32_t bytesPerRow = uint32_t bytesPerRow =
Align((GetWidth() / GetFormat().blockWidth) * GetFormat().blockByteSize, Align((GetWidth() / blockInfo.blockWidth) * blockInfo.blockByteSize,
kTextureBytesPerRowAlignment); kTextureBytesPerRowAlignment);
uint64_t bufferSize64 = bytesPerRow * (GetHeight() / GetFormat().blockHeight); uint64_t bufferSize64 = bytesPerRow * (GetHeight() / blockInfo.blockHeight);
if (bufferSize64 > std::numeric_limits<uint32_t>::max()) { if (bufferSize64 > std::numeric_limits<uint32_t>::max()) {
return DAWN_OUT_OF_MEMORY_ERROR("Unable to allocate buffer."); return DAWN_OUT_OF_MEMORY_ERROR("Unable to allocate buffer.");
} }
@ -957,7 +959,7 @@ namespace dawn_native { namespace vulkan {
UploadHandle uploadHandle; UploadHandle uploadHandle;
DAWN_TRY_ASSIGN(uploadHandle, DAWN_TRY_ASSIGN(uploadHandle,
uploader->Allocate(bufferSize, device->GetPendingCommandSerial(), uploader->Allocate(bufferSize, device->GetPendingCommandSerial(),
GetFormat().blockByteSize)); blockInfo.blockByteSize));
memset(uploadHandle.mappedBuffer, clearColor, bufferSize); memset(uploadHandle.mappedBuffer, clearColor, bufferSize);
// compute the buffer image copy to set the clear region of entire texture // compute the buffer image copy to set the clear region of entire texture

View File

@ -290,13 +290,13 @@ class CopySplitTest : public testing::Test {
Texture2DCopySplit DoTest(const TextureSpec& textureSpec, const BufferSpec& bufferSpec) { Texture2DCopySplit DoTest(const TextureSpec& textureSpec, const BufferSpec& bufferSpec) {
ASSERT(textureSpec.width % textureSpec.blockWidth == 0 && ASSERT(textureSpec.width % textureSpec.blockWidth == 0 &&
textureSpec.height % textureSpec.blockHeight == 0); textureSpec.height % textureSpec.blockHeight == 0);
dawn_native::Format fakeFormat = {}; dawn_native::TexelBlockInfo blockInfo = {};
fakeFormat.blockWidth = textureSpec.blockWidth; blockInfo.blockWidth = textureSpec.blockWidth;
fakeFormat.blockHeight = textureSpec.blockHeight; blockInfo.blockHeight = textureSpec.blockHeight;
fakeFormat.blockByteSize = textureSpec.texelBlockSizeInBytes; blockInfo.blockByteSize = textureSpec.texelBlockSizeInBytes;
Texture2DCopySplit copySplit = ComputeTextureCopySplit( Texture2DCopySplit copySplit = ComputeTextureCopySplit(
{textureSpec.x, textureSpec.y, textureSpec.z}, {textureSpec.x, textureSpec.y, textureSpec.z},
{textureSpec.width, textureSpec.height, textureSpec.depth}, fakeFormat, {textureSpec.width, textureSpec.height, textureSpec.depth}, blockInfo,
bufferSpec.offset, bufferSpec.bytesPerRow, bufferSpec.rowsPerImage); bufferSpec.offset, bufferSpec.bytesPerRow, bufferSpec.rowsPerImage);
ValidateCopySplit(textureSpec, bufferSpec, copySplit); ValidateCopySplit(textureSpec, bufferSpec, copySplit);
return copySplit; return copySplit;