Diff
Modified: trunk/Source/WebGPU/ChangeLog (292146 => 292147)
--- trunk/Source/WebGPU/ChangeLog 2022-03-31 08:40:01 UTC (rev 292146)
+++ trunk/Source/WebGPU/ChangeLog 2022-03-31 08:42:32 UTC (rev 292147)
@@ -1,5 +1,34 @@
2022-03-31 Myles C. Maxfield <[email protected]>
+ [WebGPU] Implement CommandEncoder::copyBufferToTexture() according to the spec
+ https://bugs.webkit.org/show_bug.cgi?id=238428
+
+ Reviewed by Kimmo Kinnunen.
+
+ This patch implements CommandEncoder::copyBufferToTexture() according to the spec.
+ There are a bunch of helper validation functions added as public static members of Texture
+ because they will be needed for Queue::writeTexture() later. There are links and quotes
+ from the spec where appropriate.
+
+ * WebGPU/CommandEncoder.mm:
+ (WebGPU::validateImageCopyBuffer):
+ (WebGPU::validateCopyBufferToTexture):
+ (WebGPU::CommandEncoder::copyBufferToTexture):
+ * WebGPU/Texture.h:
+ * WebGPU/Texture.mm:
+ (WebGPU::Texture::texelBlockSize):
+ (WebGPU::Texture::aspectSpecificFormat):
+ (WebGPU::Texture::logicalTextureSubresourceExtent):
+ (WebGPU::Texture::physicalTextureSubresourceExtent):
+ (WebGPU::imageCopyTextureSubresourceSize):
+ (WebGPU::Texture::validateImageCopyTexture):
+ (WebGPU::Texture::refersToSingleAspect):
+ (WebGPU::Texture::isValidImageCopyDestination):
+ (WebGPU::Texture::validateTextureCopyRange):
+ (WebGPU::Texture::validateLinearTextureData):
+
+2022-03-31 Myles C. Maxfield <[email protected]>
+
[WebGPU] Implement Texture::createView() according to the spec
https://bugs.webkit.org/show_bug.cgi?id=238311
Modified: trunk/Source/WebGPU/WebGPU/CommandEncoder.mm (292146 => 292147)
--- trunk/Source/WebGPU/WebGPU/CommandEncoder.mm 2022-03-31 08:40:01 UTC (rev 292146)
+++ trunk/Source/WebGPU/WebGPU/CommandEncoder.mm 2022-03-31 08:42:32 UTC (rev 292147)
@@ -158,11 +158,200 @@
[m_blitCommandEncoder copyFromBuffer:source.buffer() sourceOffset:static_cast<NSUInteger>(sourceOffset) toBuffer:destination.buffer() destinationOffset:static_cast<NSUInteger>(destinationOffset) size:static_cast<NSUInteger>(size)];
}
+static bool validateImageCopyBuffer(const WGPUImageCopyBuffer& imageCopyBuffer)
+{
+ // https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-gpuimagecopybuffer
+
+ // FIXME: "imageCopyBuffer.buffer must be a valid GPUBuffer."
+
+ // "imageCopyBuffer.bytesPerRow must be a multiple of 256."
+ if (imageCopyBuffer.layout.bytesPerRow % 256)
+ return false;
+
+ return false;
+}
+
+static bool validateCopyBufferToTexture(const WGPUImageCopyBuffer& source, const WGPUImageCopyTexture& destination, const WGPUExtent3D& copySize)
+{
+ // "Let dstTextureDesc be destination.texture.[[descriptor]]."
+ const auto& dstTextureDesc = fromAPI(destination.texture).descriptor();
+
+ // "validating GPUImageCopyBuffer(source) returns true."
+ if (!validateImageCopyBuffer(source))
+ return false;
+
+ // "source.buffer.[[usage]] contains COPY_SRC."
+ if (!(fromAPI(source.buffer).usage() & WGPUBufferUsage_CopySrc))
+ return false;
+
+ // "validating GPUImageCopyTexture(destination, copySize) returns true."
+ if (!Texture::validateImageCopyTexture(destination, copySize))
+ return false;
+
+ // "dstTextureDesc.usage contains COPY_DST."
+ if (!(dstTextureDesc.usage & WGPUTextureUsage_CopyDst))
+ return false;
+
+ // "dstTextureDesc.sampleCount is 1."
+ if (dstTextureDesc.sampleCount != 1)
+ return false;
+
+ // "Let aspectSpecificFormat = dstTextureDesc.format."
+ WGPUTextureFormat aspectSpecificFormat = dstTextureDesc.format;
+
+ // "If dstTextureDesc.format is a depth-or-stencil format:"
+ if (Texture::isDepthOrStencilFormat(dstTextureDesc.format)) {
+ // "destination.aspect must refer to a single aspect of dstTextureDesc.format"
+ if (!Texture::refersToSingleAspect(dstTextureDesc.format, destination.aspect))
+ return false;
+
+ // "That aspect must be a valid image copy destination according to § 25.1.2 Depth-stencil formats."
+ if (!Texture::isValidImageCopyDestination(dstTextureDesc.format, destination.aspect))
+ return false;
+
+ // "Set aspectSpecificFormat to the aspect-specific format according to § 25.1.2 Depth-stencil formats."
+ aspectSpecificFormat = Texture::aspectSpecificFormat(dstTextureDesc.format, destination.aspect);
+ }
+
+ // "validating texture copy range(destination, copySize) return true."
+ if (!Texture::validateTextureCopyRange(destination, copySize))
+ return false;
+
+ // "If dstTextureDesc.format is not a depth-or-stencil format:"
+ if (!Texture::isDepthOrStencilFormat(dstTextureDesc.format)) {
+ // "source.offset is a multiple of the texel block size of dstTextureDesc.format."
+ auto texelBlockSize = Texture::texelBlockSize(dstTextureDesc.format);
+ if (source.layout.offset % texelBlockSize)
+ return false;
+ }
+
+ // "If dstTextureDesc.format is a depth-or-stencil format:"
+ if (Texture::isDepthOrStencilFormat(dstTextureDesc.format)) {
+ // "source.offset is a multiple of 4."
+ if (source.layout.offset % 4)
+ return false;
+ }
+
+ // "validating linear texture data(source, source.buffer.[[size]], aspectSpecificFormat, copySize) succeeds."
+ if (!Texture::validateLinearTextureData(source.layout, fromAPI(source.buffer).size(), aspectSpecificFormat, copySize))
+ return false;
+
+ return true;
+}
+
void CommandEncoder::copyBufferToTexture(const WGPUImageCopyBuffer& source, const WGPUImageCopyTexture& destination, const WGPUExtent3D& copySize)
{
- UNUSED_PARAM(source);
- UNUSED_PARAM(destination);
- UNUSED_PARAM(copySize);
+ if (source.nextInChain || source.layout.nextInChain || destination.nextInChain)
+ return;
+
+ // https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-copybuffertotexture
+
+ // "Prepare the encoder state of this. If it returns false, stop."
+ if (!prepareTheEncoderState())
+ return;
+
+ // "If any of the following conditions are unsatisfied"
+ if (!validateCopyBufferToTexture(source, destination, copySize)) {
+ // "generate a validation error and stop."
+ m_device->generateAValidationError("Validation failure.");
+ return;
+ }
+
+ ensureBlitCommandEncoder();
+
+ NSUInteger sourceBytesPerRow = source.layout.bytesPerRow;
+
+ // FIXME: Use checked arithmetic
+ NSUInteger sourceBytesPerImage = source.layout.rowsPerImage * source.layout.bytesPerRow;
+
+ MTLBlitOption options = MTLBlitOptionNone;
+ switch (destination.aspect) {
+ case WGPUTextureAspect_All:
+ options = MTLBlitOptionNone;
+ break;
+ case WGPUTextureAspect_StencilOnly:
+ options = MTLBlitOptionStencilFromDepthStencil;
+ break;
+ case WGPUTextureAspect_DepthOnly:
+ options = MTLBlitOptionDepthFromDepthStencil;
+ break;
+ case WGPUTextureAspect_Force32:
+ return;
+ }
+
+ auto logicalSize = fromAPI(destination.texture).logicalMiplevelSpecificTextureExtent(destination.mipLevel);
+ auto widthForMetal = std::min(copySize.width, logicalSize.width);
+ auto heightForMetal = std::min(copySize.height, logicalSize.height);
+ auto depthForMetal = std::min(copySize.depthOrArrayLayers, logicalSize.depthOrArrayLayers);
+
+ auto& destinationDescriptor = fromAPI(destination.texture).descriptor();
+ switch (destinationDescriptor.dimension) {
+ case WGPUTextureDimension_1D: {
+ // https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400771-copyfrombuffer?language=objc
+ // "When you copy to a 1D texture, height and depth must be 1."
+ auto sourceSize = MTLSizeMake(widthForMetal, 1, 1);
+ auto destinationOrigin = MTLOriginMake(destination.origin.x, 1, 1);
+ for (uint32_t layer = 0; layer < copySize.depthOrArrayLayers; ++layer) {
+ // FIXME: Use checked arithmetic.
+ auto sourceOffset = static_cast<NSUInteger>(source.layout.offset + layer * sourceBytesPerImage);
+ NSUInteger destinationSlice = destination.origin.z + layer;
+ [m_blitCommandEncoder
+ copyFromBuffer:fromAPI(source.buffer).buffer()
+ sourceOffset:sourceOffset
+ sourceBytesPerRow:sourceBytesPerRow
+ sourceBytesPerImage:sourceBytesPerImage
+ sourceSize:sourceSize
+ toTexture:fromAPI(destination.texture).texture()
+ destinationSlice:destinationSlice
+ destinationLevel:destination.mipLevel
+ destinationOrigin:destinationOrigin
+ options:options];
+ }
+ break;
+ }
+ case WGPUTextureDimension_2D: {
+ // https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400771-copyfrombuffer?language=objc
+ // "When you copy to a 2D texture, depth must be 1."
+ auto sourceSize = MTLSizeMake(widthForMetal, heightForMetal, 1);
+ auto destinationOrigin = MTLOriginMake(destination.origin.x, destination.origin.y, 1);
+ for (uint32_t layer = 0; layer < copySize.depthOrArrayLayers; ++layer) {
+ // FIXME: Use checked arithmetic.
+ auto sourceOffset = static_cast<NSUInteger>(source.layout.offset + layer * sourceBytesPerImage);
+ NSUInteger destinationSlice = destination.origin.z + layer;
+ [m_blitCommandEncoder
+ copyFromBuffer:fromAPI(source.buffer).buffer()
+ sourceOffset:sourceOffset
+ sourceBytesPerRow:sourceBytesPerRow
+ sourceBytesPerImage:sourceBytesPerImage
+ sourceSize:sourceSize
+ toTexture:fromAPI(destination.texture).texture()
+ destinationSlice:destinationSlice
+ destinationLevel:destination.mipLevel
+ destinationOrigin:destinationOrigin
+ options:options];
+ }
+ break;
+ }
+ case WGPUTextureDimension_3D: {
+ auto sourceSize = MTLSizeMake(widthForMetal, heightForMetal, depthForMetal);
+ auto destinationOrigin = MTLOriginMake(destination.origin.x, destination.origin.y, destination.origin.z);
+ auto sourceOffset = static_cast<NSUInteger>(source.layout.offset);
+ [m_blitCommandEncoder
+ copyFromBuffer:fromAPI(source.buffer).buffer()
+ sourceOffset:sourceOffset
+ sourceBytesPerRow:sourceBytesPerRow
+ sourceBytesPerImage:sourceBytesPerImage
+ sourceSize:sourceSize
+ toTexture:fromAPI(destination.texture).texture()
+ destinationSlice:0
+ destinationLevel:destination.mipLevel
+ destinationOrigin:destinationOrigin
+ options:options];
+ break;
+ }
+ case WGPUTextureDimension_Force32:
+ return;
+ }
}
void CommandEncoder::copyTextureToBuffer(const WGPUImageCopyTexture& source, const WGPUImageCopyBuffer& destination, const WGPUExtent3D& copySize)
Modified: trunk/Source/WebGPU/WebGPU/Texture.h (292146 => 292147)
--- trunk/Source/WebGPU/WebGPU/Texture.h 2022-03-31 08:40:01 UTC (rev 292146)
+++ trunk/Source/WebGPU/WebGPU/Texture.h 2022-03-31 08:42:32 UTC (rev 292147)
@@ -54,10 +54,22 @@
static uint32_t texelBlockWidth(WGPUTextureFormat); // Texels
static uint32_t texelBlockHeight(WGPUTextureFormat); // Texels
+ // For depth-stencil textures, the input value to texelBlockSize()
+ // needs to be the output of aspectSpecificFormat().
+ static uint32_t texelBlockSize(WGPUTextureFormat); // Bytes
static bool containsDepthAspect(WGPUTextureFormat);
static bool containsStencilAspect(WGPUTextureFormat);
static bool isDepthOrStencilFormat(WGPUTextureFormat);
+ static WGPUTextureFormat aspectSpecificFormat(WGPUTextureFormat, WGPUTextureAspect);
+ static bool validateImageCopyTexture(const WGPUImageCopyTexture&, const WGPUExtent3D&);
+ static bool validateTextureCopyRange(const WGPUImageCopyTexture&, const WGPUExtent3D&);
+ static bool refersToSingleAspect(WGPUTextureFormat, WGPUTextureAspect);
+ static bool isValidImageCopyDestination(WGPUTextureFormat, WGPUTextureAspect);
+ static bool validateLinearTextureData(const WGPUTextureDataLayout&, uint64_t, WGPUTextureFormat, WGPUExtent3D);
+ WGPUExtent3D logicalMiplevelSpecificTextureExtent(uint32_t mipLevel);
+ WGPUExtent3D physicalMiplevelSpecificTextureExtent(uint32_t mipLevel);
+
id<MTLTexture> texture() const { return m_texture; }
const WGPUTextureDescriptor& descriptor() const { return m_descriptor; }
Modified: trunk/Source/WebGPU/WebGPU/Texture.mm (292146 => 292147)
--- trunk/Source/WebGPU/WebGPU/Texture.mm 2022-03-31 08:40:01 UTC (rev 292146)
+++ trunk/Source/WebGPU/WebGPU/Texture.mm 2022-03-31 08:42:32 UTC (rev 292147)
@@ -1439,6 +1439,164 @@
}
}
+uint32_t Texture::texelBlockSize(WGPUTextureFormat format) // Bytes
+{
+ // For depth-stencil textures, the input value to this function
+ // needs to be the output of aspectSpecificFormat().
+ ASSERT(!containsDepthAspect(format) || !containsStencilAspect(format));
+ switch (format) {
+ case WGPUTextureFormat_R8Unorm:
+ case WGPUTextureFormat_R8Snorm:
+ case WGPUTextureFormat_R8Uint:
+ case WGPUTextureFormat_R8Sint:
+ return 1;
+ case WGPUTextureFormat_R16Uint:
+ case WGPUTextureFormat_R16Sint:
+ case WGPUTextureFormat_R16Float:
+ case WGPUTextureFormat_RG8Unorm:
+ case WGPUTextureFormat_RG8Snorm:
+ case WGPUTextureFormat_RG8Uint:
+ case WGPUTextureFormat_RG8Sint:
+ return 2;
+ case WGPUTextureFormat_R32Float:
+ case WGPUTextureFormat_R32Uint:
+ case WGPUTextureFormat_R32Sint:
+ case WGPUTextureFormat_RG16Uint:
+ case WGPUTextureFormat_RG16Sint:
+ case WGPUTextureFormat_RG16Float:
+ case WGPUTextureFormat_RGBA8Unorm:
+ case WGPUTextureFormat_RGBA8UnormSrgb:
+ case WGPUTextureFormat_RGBA8Snorm:
+ case WGPUTextureFormat_RGBA8Uint:
+ case WGPUTextureFormat_RGBA8Sint:
+ case WGPUTextureFormat_BGRA8Unorm:
+ case WGPUTextureFormat_BGRA8UnormSrgb:
+ case WGPUTextureFormat_RGB10A2Unorm:
+ case WGPUTextureFormat_RG11B10Ufloat:
+ case WGPUTextureFormat_RGB9E5Ufloat:
+ return 4;
+ case WGPUTextureFormat_RG32Float:
+ case WGPUTextureFormat_RG32Uint:
+ case WGPUTextureFormat_RG32Sint:
+ case WGPUTextureFormat_RGBA16Uint:
+ case WGPUTextureFormat_RGBA16Sint:
+ case WGPUTextureFormat_RGBA16Float:
+ return 8;
+ case WGPUTextureFormat_RGBA32Float:
+ case WGPUTextureFormat_RGBA32Uint:
+ case WGPUTextureFormat_RGBA32Sint:
+ return 16;
+ case WGPUTextureFormat_Stencil8:
+ return 1;
+ case WGPUTextureFormat_Depth16Unorm:
+ return 2;
+ case WGPUTextureFormat_Depth24Plus:
+ ASSERT(pixelFormat(format) == MTLPixelFormatDepth32Float);
+ return 4;
+ case WGPUTextureFormat_Depth24PlusStencil8:
+ ASSERT_NOT_REACHED();
+ return 0;
+ case WGPUTextureFormat_Depth24UnormStencil8:
+ ASSERT_NOT_REACHED();
+ return 0;
+ case WGPUTextureFormat_Depth32Float:
+ return 4;
+ case WGPUTextureFormat_Depth32FloatStencil8:
+ ASSERT_NOT_REACHED();
+ return 0;
+ case WGPUTextureFormat_BC1RGBAUnorm:
+ case WGPUTextureFormat_BC1RGBAUnormSrgb:
+ return 8;
+ case WGPUTextureFormat_BC2RGBAUnorm:
+ case WGPUTextureFormat_BC2RGBAUnormSrgb:
+ return 8;
+ case WGPUTextureFormat_BC3RGBAUnorm:
+ case WGPUTextureFormat_BC3RGBAUnormSrgb:
+ return 16;
+ case WGPUTextureFormat_BC4RUnorm:
+ case WGPUTextureFormat_BC4RSnorm:
+ return 8;
+ case WGPUTextureFormat_BC5RGUnorm:
+ case WGPUTextureFormat_BC5RGSnorm:
+ return 16;
+ case WGPUTextureFormat_BC6HRGBUfloat:
+ case WGPUTextureFormat_BC6HRGBFloat:
+ return 16;
+ case WGPUTextureFormat_BC7RGBAUnorm:
+ case WGPUTextureFormat_BC7RGBAUnormSrgb:
+ return 16;
+ case WGPUTextureFormat_ETC2RGB8Unorm:
+ case WGPUTextureFormat_ETC2RGB8UnormSrgb:
+ case WGPUTextureFormat_ETC2RGB8A1Unorm:
+ case WGPUTextureFormat_ETC2RGB8A1UnormSrgb:
+ case WGPUTextureFormat_ETC2RGBA8Unorm:
+ case WGPUTextureFormat_ETC2RGBA8UnormSrgb:
+ return 8;
+ case WGPUTextureFormat_EACR11Unorm:
+ case WGPUTextureFormat_EACR11Snorm:
+ return 8;
+ case WGPUTextureFormat_EACRG11Unorm:
+ case WGPUTextureFormat_EACRG11Snorm:
+ return 16;
+ case WGPUTextureFormat_ASTC4x4Unorm:
+ case WGPUTextureFormat_ASTC4x4UnormSrgb:
+ case WGPUTextureFormat_ASTC5x4Unorm:
+ case WGPUTextureFormat_ASTC5x4UnormSrgb:
+ case WGPUTextureFormat_ASTC5x5Unorm:
+ case WGPUTextureFormat_ASTC5x5UnormSrgb:
+ case WGPUTextureFormat_ASTC6x5Unorm:
+ case WGPUTextureFormat_ASTC6x5UnormSrgb:
+ case WGPUTextureFormat_ASTC6x6Unorm:
+ case WGPUTextureFormat_ASTC6x6UnormSrgb:
+ case WGPUTextureFormat_ASTC8x5Unorm:
+ case WGPUTextureFormat_ASTC8x5UnormSrgb:
+ case WGPUTextureFormat_ASTC8x6Unorm:
+ case WGPUTextureFormat_ASTC8x6UnormSrgb:
+ case WGPUTextureFormat_ASTC8x8Unorm:
+ case WGPUTextureFormat_ASTC8x8UnormSrgb:
+ case WGPUTextureFormat_ASTC10x5Unorm:
+ case WGPUTextureFormat_ASTC10x5UnormSrgb:
+ case WGPUTextureFormat_ASTC10x6Unorm:
+ case WGPUTextureFormat_ASTC10x6UnormSrgb:
+ case WGPUTextureFormat_ASTC10x8Unorm:
+ case WGPUTextureFormat_ASTC10x8UnormSrgb:
+ case WGPUTextureFormat_ASTC10x10Unorm:
+ case WGPUTextureFormat_ASTC10x10UnormSrgb:
+ case WGPUTextureFormat_ASTC12x10Unorm:
+ case WGPUTextureFormat_ASTC12x10UnormSrgb:
+ case WGPUTextureFormat_ASTC12x12Unorm:
+ case WGPUTextureFormat_ASTC12x12UnormSrgb:
+ return 16;
+ case WGPUTextureFormat_Undefined:
+ case WGPUTextureFormat_Force32:
+ ASSERT_NOT_REACHED();
+ return 0;
+ }
+}
+
+WGPUTextureFormat Texture::aspectSpecificFormat(WGPUTextureFormat format, WGPUTextureAspect aspect)
+{
+ // https://gpuweb.github.io/gpuweb/#aspect-specific-format
+
+ switch (aspect) {
+ case WGPUTextureAspect_All:
+ return format;
+ case WGPUTextureAspect_StencilOnly: {
+ auto result = stencilSpecificFormat(format);
+ ASSERT(result);
+ return *result;
+ }
+ case WGPUTextureAspect_DepthOnly: {
+ auto result = depthSpecificFormat(format);
+ ASSERT(result);
+ return *result;
+ }
+ case WGPUTextureAspect_Force32:
+ ASSERT_NOT_REACHED();
+ return WGPUTextureFormat_Undefined;
+ }
+}
+
static std::optional<MTLPixelFormat> depthOnlyAspectMetalFormat(WGPUTextureFormat textureFormat)
{
switch (textureFormat) {
@@ -2123,6 +2281,395 @@
m_texture.label = label;
}
+WGPUExtent3D Texture::logicalMiplevelSpecificTextureExtent(uint32_t mipLevel)
+{
+ // https://gpuweb.github.io/gpuweb/#abstract-opdef-logical-miplevel-specific-texture-extent
+
+ switch (m_descriptor.dimension) {
+ case WGPUTextureDimension_1D:
+ return {
+ // "Set extent.width to max(1, descriptor.size.width ≫ mipLevel)."
+ std::max(static_cast<uint32_t>(1), m_descriptor.size.width >> mipLevel),
+ // "Set extent.height to 1."
+ 1,
+ // "Set extent.depthOrArrayLayers to descriptor.size.depthOrArrayLayers."
+ m_descriptor.size.depthOrArrayLayers };
+ case WGPUTextureDimension_2D:
+ return {
+ // "Set extent.width to max(1, descriptor.size.width ≫ mipLevel)."
+ std::max(static_cast<uint32_t>(1), m_descriptor.size.width >> mipLevel),
+ // "Set extent.height to max(1, descriptor.size.height ≫ mipLevel)."
+ std::max(static_cast<uint32_t>(1), m_descriptor.size.height >> mipLevel),
+ // "Set extent.depthOrArrayLayers to descriptor.size.depthOrArrayLayers."
+ m_descriptor.size.depthOrArrayLayers };
+ case WGPUTextureDimension_3D:
+ return {
+ // "Set extent.width to max(1, descriptor.size.width ≫ mipLevel)."
+ std::max(static_cast<uint32_t>(1), m_descriptor.size.width >> mipLevel),
+ // "Set extent.height to max(1, descriptor.size.height ≫ mipLevel)."
+ std::max(static_cast<uint32_t>(1), m_descriptor.size.height >> mipLevel),
+ // "Set extent.depthOrArrayLayers to max(1, descriptor.size.depthOrArrayLayers ≫ mipLevel)."
+ std::max(static_cast<uint32_t>(1), m_descriptor.size.depthOrArrayLayers >> mipLevel) };
+ case WGPUTextureDimension_Force32:
+ ASSERT_NOT_REACHED();
+ return WGPUExtent3D { };
+ }
+}
+
+WGPUExtent3D Texture::physicalMiplevelSpecificTextureExtent(uint32_t mipLevel)
+{
+ // https://gpuweb.github.io/gpuweb/#abstract-opdef-physical-miplevel-specific-texture-extent
+
+ // "Let logicalExtent be logical miplevel-specific texture extent(descriptor, mipLevel)."
+ auto logicalExtent = logicalMiplevelSpecificTextureExtent(mipLevel);
+
+ switch (m_descriptor.dimension) {
+ case WGPUTextureDimension_1D:
+ return {
+ // "Set extent.width to logicalExtent.width rounded up to the nearest multiple of descriptor’s texel block width."
+ static_cast<uint32_t>(WTF::roundUpToMultipleOf(texelBlockWidth(m_descriptor.format), logicalExtent.width)),
+ // "Set extent.height to 1."
+ 1,
+ // "Set extent.depthOrArrayLayers to logicalExtent.depthOrArrayLayers."
+ logicalExtent.depthOrArrayLayers };
+ case WGPUTextureDimension_2D:
+ return {
+ // "Set extent.width to logicalExtent.width rounded up to the nearest multiple of descriptor’s texel block width."
+ static_cast<uint32_t>(WTF::roundUpToMultipleOf(texelBlockWidth(m_descriptor.format), logicalExtent.width)),
+ // "Set extent.height to logicalExtent.height rounded up to the nearest multiple of descriptor’s texel block height."
+ static_cast<uint32_t>(WTF::roundUpToMultipleOf(texelBlockHeight(m_descriptor.format), logicalExtent.height)),
+ // "Set extent.depthOrArrayLayers to logicalExtent.depthOrArrayLayers."
+ logicalExtent.depthOrArrayLayers };
+ case WGPUTextureDimension_3D:
+ return {
+ // "Set extent.width to logicalExtent.width rounded up to the nearest multiple of descriptor’s texel block width."
+ static_cast<uint32_t>(WTF::roundUpToMultipleOf(texelBlockWidth(m_descriptor.format), logicalExtent.width)),
+ // "Set extent.height to logicalExtent.height rounded up to the nearest multiple of descriptor’s texel block height."
+ static_cast<uint32_t>(WTF::roundUpToMultipleOf(texelBlockHeight(m_descriptor.format), logicalExtent.height)),
+ // "Set extent.depthOrArrayLayers to logicalExtent.depthOrArrayLayers."
+ logicalExtent.depthOrArrayLayers };
+ case WGPUTextureDimension_Force32:
+ ASSERT_NOT_REACHED();
+ return WGPUExtent3D { };
+ }
+}
+
+static WGPUExtent3D imageCopyTextureSubresourceSize(const WGPUImageCopyTexture& imageCopyTexture)
+{
+ // https://gpuweb.github.io/gpuweb/#imagecopytexture-subresource-size
+
+ // "Its width, height and depthOrArrayLayers are the width, height, and depth, respectively, of the physical size of imageCopyTexture.texture subresource at mipmap level imageCopyTexture.mipLevel."
+ return fromAPI(imageCopyTexture.texture).physicalMiplevelSpecificTextureExtent(imageCopyTexture.mipLevel);
+}
+
+bool Texture::validateImageCopyTexture(const WGPUImageCopyTexture& imageCopyTexture, const WGPUExtent3D& copySize)
+{
+ // https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-gpuimagecopytexture
+
+ // "blockWidth be the texel block width of imageCopyTexture.texture.[[descriptor]].format."
+ uint32_t blockWidth = Texture::texelBlockWidth(fromAPI(imageCopyTexture.texture).descriptor().format);
+
+ // "blockHeight be the texel block height of imageCopyTexture.texture.[[descriptor]].format."
+ uint32_t blockHeight = Texture::texelBlockHeight(fromAPI(imageCopyTexture.texture).descriptor().format);
+
+ // FIXME: "imageCopyTexture.texture must be a valid GPUTexture."
+
+ // "imageCopyTexture.mipLevel must be less than the [[descriptor]].mipLevelCount of imageCopyTexture.texture."
+ if (imageCopyTexture.mipLevel >= fromAPI(imageCopyTexture.texture).descriptor().mipLevelCount)
+ return false;
+
+ // "imageCopyTexture.origin.x must be a multiple of blockWidth."
+ if (imageCopyTexture.origin.x % blockWidth)
+ return false;
+
+ // "imageCopyTexture.origin.y must be a multiple of blockHeight."
+ if (imageCopyTexture.origin.y % blockHeight)
+ return false;
+
+ // "imageCopyTexture.texture.[[descriptor]].format is a depth-stencil format."
+ // "imageCopyTexture.texture.[[descriptor]].sampleCount is greater than 1."
+ if (Texture::isDepthOrStencilFormat(fromAPI(imageCopyTexture.texture).descriptor().format)
+ || fromAPI(imageCopyTexture.texture).descriptor().sampleCount > 1) {
+ // "The imageCopyTexture subresource size of imageCopyTexture is equal to copySize"
+ auto subresourceSize = imageCopyTextureSubresourceSize(imageCopyTexture);
+ if (subresourceSize.width != copySize.width
+ || subresourceSize.height != copySize.height
+ || subresourceSize.depthOrArrayLayers != copySize.depthOrArrayLayers)
+ return false;
+ }
+
+ return true;
+}
+
+bool Texture::refersToSingleAspect(WGPUTextureFormat format, WGPUTextureAspect aspect)
+{
+ switch (aspect) {
+ case WGPUTextureAspect_All:
+ ASSERT(Texture::containsDepthAspect(format) || Texture::containsStencilAspect(format));
+ if (Texture::containsDepthAspect(format) && Texture::containsStencilAspect(format))
+ return false;
+ break;
+ case WGPUTextureAspect_StencilOnly:
+ if (!Texture::containsStencilAspect(format))
+ return false;
+ break;
+ case WGPUTextureAspect_DepthOnly:
+ if (!Texture::containsDepthAspect(format))
+ return false;
+ break;
+ case WGPUTextureAspect_Force32:
+ return false;
+ }
+
+ return true;
+}
+
+bool Texture::isValidImageCopyDestination(WGPUTextureFormat format, WGPUTextureAspect aspect)
+{
+ // https://gpuweb.github.io/gpuweb/#depth-formats
+
+ switch (format) {
+ case WGPUTextureFormat_Undefined:
+ case WGPUTextureFormat_R8Unorm:
+ case WGPUTextureFormat_R8Snorm:
+ case WGPUTextureFormat_R8Uint:
+ case WGPUTextureFormat_R8Sint:
+ case WGPUTextureFormat_R16Uint:
+ case WGPUTextureFormat_R16Sint:
+ case WGPUTextureFormat_R16Float:
+ case WGPUTextureFormat_RG8Unorm:
+ case WGPUTextureFormat_RG8Snorm:
+ case WGPUTextureFormat_RG8Uint:
+ case WGPUTextureFormat_RG8Sint:
+ case WGPUTextureFormat_R32Float:
+ case WGPUTextureFormat_R32Uint:
+ case WGPUTextureFormat_R32Sint:
+ case WGPUTextureFormat_RG16Uint:
+ case WGPUTextureFormat_RG16Sint:
+ case WGPUTextureFormat_RG16Float:
+ case WGPUTextureFormat_RGBA8Unorm:
+ case WGPUTextureFormat_RGBA8UnormSrgb:
+ case WGPUTextureFormat_RGBA8Snorm:
+ case WGPUTextureFormat_RGBA8Uint:
+ case WGPUTextureFormat_RGBA8Sint:
+ case WGPUTextureFormat_BGRA8Unorm:
+ case WGPUTextureFormat_BGRA8UnormSrgb:
+ case WGPUTextureFormat_RGB10A2Unorm:
+ case WGPUTextureFormat_RG11B10Ufloat:
+ case WGPUTextureFormat_RGB9E5Ufloat:
+ case WGPUTextureFormat_RG32Float:
+ case WGPUTextureFormat_RG32Uint:
+ case WGPUTextureFormat_RG32Sint:
+ case WGPUTextureFormat_RGBA16Uint:
+ case WGPUTextureFormat_RGBA16Sint:
+ case WGPUTextureFormat_RGBA16Float:
+ case WGPUTextureFormat_RGBA32Float:
+ case WGPUTextureFormat_RGBA32Uint:
+ case WGPUTextureFormat_RGBA32Sint:
+ ASSERT_NOT_REACHED();
+ return false;
+ case WGPUTextureFormat_Stencil8:
+ case WGPUTextureFormat_Depth16Unorm:
+ return true;
+ case WGPUTextureFormat_Depth24Plus:
+ return false;
+ case WGPUTextureFormat_Depth24PlusStencil8:
+ case WGPUTextureFormat_Depth24UnormStencil8:
+ return aspect == WGPUTextureAspect_StencilOnly;
+ case WGPUTextureFormat_Depth32Float:
+ return false;
+ case WGPUTextureFormat_Depth32FloatStencil8:
+ return aspect == WGPUTextureAspect_StencilOnly;
+ case WGPUTextureFormat_BC1RGBAUnorm:
+ case WGPUTextureFormat_BC1RGBAUnormSrgb:
+ case WGPUTextureFormat_BC2RGBAUnorm:
+ case WGPUTextureFormat_BC2RGBAUnormSrgb:
+ case WGPUTextureFormat_BC3RGBAUnorm:
+ case WGPUTextureFormat_BC3RGBAUnormSrgb:
+ case WGPUTextureFormat_BC4RUnorm:
+ case WGPUTextureFormat_BC4RSnorm:
+ case WGPUTextureFormat_BC5RGUnorm:
+ case WGPUTextureFormat_BC5RGSnorm:
+ case WGPUTextureFormat_BC6HRGBUfloat:
+ case WGPUTextureFormat_BC6HRGBFloat:
+ case WGPUTextureFormat_BC7RGBAUnorm:
+ case WGPUTextureFormat_BC7RGBAUnormSrgb:
+ case WGPUTextureFormat_ETC2RGB8Unorm:
+ case WGPUTextureFormat_ETC2RGB8UnormSrgb:
+ case WGPUTextureFormat_ETC2RGB8A1Unorm:
+ case WGPUTextureFormat_ETC2RGB8A1UnormSrgb:
+ case WGPUTextureFormat_ETC2RGBA8Unorm:
+ case WGPUTextureFormat_ETC2RGBA8UnormSrgb:
+ case WGPUTextureFormat_EACR11Unorm:
+ case WGPUTextureFormat_EACR11Snorm:
+ case WGPUTextureFormat_EACRG11Unorm:
+ case WGPUTextureFormat_EACRG11Snorm:
+ case WGPUTextureFormat_ASTC4x4Unorm:
+ case WGPUTextureFormat_ASTC4x4UnormSrgb:
+ case WGPUTextureFormat_ASTC5x4Unorm:
+ case WGPUTextureFormat_ASTC5x4UnormSrgb:
+ case WGPUTextureFormat_ASTC5x5Unorm:
+ case WGPUTextureFormat_ASTC5x5UnormSrgb:
+ case WGPUTextureFormat_ASTC6x5Unorm:
+ case WGPUTextureFormat_ASTC6x5UnormSrgb:
+ case WGPUTextureFormat_ASTC6x6Unorm:
+ case WGPUTextureFormat_ASTC6x6UnormSrgb:
+ case WGPUTextureFormat_ASTC8x5Unorm:
+ case WGPUTextureFormat_ASTC8x5UnormSrgb:
+ case WGPUTextureFormat_ASTC8x6Unorm:
+ case WGPUTextureFormat_ASTC8x6UnormSrgb:
+ case WGPUTextureFormat_ASTC8x8Unorm:
+ case WGPUTextureFormat_ASTC8x8UnormSrgb:
+ case WGPUTextureFormat_ASTC10x5Unorm:
+ case WGPUTextureFormat_ASTC10x5UnormSrgb:
+ case WGPUTextureFormat_ASTC10x6Unorm:
+ case WGPUTextureFormat_ASTC10x6UnormSrgb:
+ case WGPUTextureFormat_ASTC10x8Unorm:
+ case WGPUTextureFormat_ASTC10x8UnormSrgb:
+ case WGPUTextureFormat_ASTC10x10Unorm:
+ case WGPUTextureFormat_ASTC10x10UnormSrgb:
+ case WGPUTextureFormat_ASTC12x10Unorm:
+ case WGPUTextureFormat_ASTC12x10UnormSrgb:
+ case WGPUTextureFormat_ASTC12x12Unorm:
+ case WGPUTextureFormat_ASTC12x12UnormSrgb:
+ case WGPUTextureFormat_Force32:
+ ASSERT_NOT_REACHED();
+ return false;
+ }
+}
+
+bool Texture::validateTextureCopyRange(const WGPUImageCopyTexture& imageCopyTexture, const WGPUExtent3D& copySize)
+{
+ // https://gpuweb.github.io/gpuweb/#validating-texture-copy-range
+
+ // "Let blockWidth be the texel block width of imageCopyTexture.texture.[[descriptor]].format."
+ auto blockWidth = Texture::texelBlockWidth(fromAPI(imageCopyTexture.texture).descriptor().format);
+
+ // "Let blockHeight be the texel block height of imageCopyTexture.texture.[[descriptor]].format."
+ auto blockHeight = Texture::texelBlockHeight(fromAPI(imageCopyTexture.texture).descriptor().format);
+
+ // "Let subresourceSize be the imageCopyTexture subresource size of imageCopyTexture."
+ auto subresourceSize = imageCopyTextureSubresourceSize(imageCopyTexture);
+
+ // "(imageCopyTexture.origin.x + copySize.width) ≤ subresourceSize.width"
+ // FIXME: Used checked arithmetic
+ if (imageCopyTexture.origin.x + copySize.width > subresourceSize.width)
+ return false;
+
+ // "(imageCopyTexture.origin.y + copySize.height) ≤ subresourceSize.height"
+ // FIXME: Used checked arithmetic
+ if ((imageCopyTexture.origin.y + copySize.height) > subresourceSize.height)
+ return false;
+
+ // "(imageCopyTexture.origin.z + copySize.depthOrArrayLayers) ≤ subresourceSize.depthOrArrayLayers"
+ // FIXME: Used checked arithmetic
+ if ((imageCopyTexture.origin.z + copySize.depthOrArrayLayers) > subresourceSize.depthOrArrayLayers)
+ return false;
+
+ // "copySize.width must be a multiple of blockWidth."
+ if (copySize.width % blockWidth)
+ return false;
+
+ // "copySize.height must be a multiple of blockHeight."
+ if (copySize.height % blockHeight)
+ return false;
+
+ return true;
+}
+
+bool Texture::validateLinearTextureData(const WGPUTextureDataLayout& layout, uint64_t byteSize, WGPUTextureFormat format, WGPUExtent3D copyExtent)
+{
+ // https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-linear-texture-data
+
+ // "Let blockWidth, blockHeight, and blockSize be the texel block width, height, and size of format."
+ uint32_t blockWidth = Texture::texelBlockWidth(format);
+ uint32_t blockHeight = Texture::texelBlockHeight(format);
+ uint32_t blockSize = Texture::texelBlockSize(format);
+
+ // "It is assumed that copyExtent.width is a multiple of blockWidth and copyExtent.height is a multiple of blockHeight."
+
+ // "widthInBlocks be copyExtent.width ÷ blockWidth."
+ auto widthInBlocks = copyExtent.width / blockWidth;
+
+ // "heightInBlocks be copyExtent.height ÷ blockHeight."
+ auto heightInBlocks = copyExtent.height / blockHeight;
+
+ // "bytesInLastRow be blockSize × widthInBlocks."
+ // FIXME: Use checked arithmetic.
+ auto bytesInLastRow = blockSize * widthInBlocks;
+
+ // "If heightInBlocks > 1"
+ if (heightInBlocks > 1) {
+ // "layout.bytesPerRow must be specified."
+ if (layout.bytesPerRow == WGPU_COPY_STRIDE_UNDEFINED)
+ return false;
+ }
+
+ // "If copyExtent.depthOrArrayLayers > 1"
+ if (copyExtent.depthOrArrayLayers > 1) {
+ // "layout.bytesPerRow and layout.rowsPerImage must be specified."
+ if (layout.bytesPerRow == WGPU_COPY_STRIDE_UNDEFINED || layout.rowsPerImage == WGPU_COPY_STRIDE_UNDEFINED)
+ return false;
+ }
+
+ // "If specified"
+ if (layout.bytesPerRow != WGPU_COPY_STRIDE_UNDEFINED) {
+ // "layout.bytesPerRow must be greater than or equal to bytesInLastRow."
+ if (layout.bytesPerRow < bytesInLastRow)
+ return false;
+ }
+
+ // "If specified"
+ if (layout.rowsPerImage != WGPU_COPY_STRIDE_UNDEFINED) {
+ // "layout.rowsPerImage must be greater than or equal to heightInBlocks."
+ if (layout.rowsPerImage < heightInBlocks)
+ return false;
+ }
+
+ // "Let requiredBytesInCopy be 0."
+ uint64_t requiredBytesInCopy = 0;
+
+ // "If copyExtent.depthOrArrayLayers > 1:"
+ if (copyExtent.depthOrArrayLayers > 1) {
+ // "Let bytesPerImage be layout.bytesPerRow × layout.rowsPerImage."
+ // FIXME: Use checked arithmetic.
+ auto bytesPerImage = layout.bytesPerRow * layout.rowsPerImage;
+
+ // "Let bytesBeforeLastImage be bytesPerImage × (copyExtent.depthOrArrayLayers − 1)."
+ // FIXME: Use checked arithmetic.
+ auto bytesBeforeLastImage = bytesPerImage * (copyExtent.depthOrArrayLayers - 1);
+
+ // "Add bytesBeforeLastImage to requiredBytesInCopy."
+ // FIXME: Use checked arithmetic.
+ requiredBytesInCopy += bytesBeforeLastImage;
+ }
+
+ // "If copyExtent.depthOrArrayLayers > 0:"
+ if (copyExtent.depthOrArrayLayers > 0) {
+ // "If heightInBlocks > 1"
+ if (heightInBlocks > 1) {
+ // "add layout.bytesPerRow × (heightInBlocks − 1) to requiredBytesInCopy."
+ // FIXME: Use checked arithmetic.
+ requiredBytesInCopy += layout.bytesPerRow * (heightInBlocks - 1);
+ }
+
+ // "If heightInBlocks > 0"
+ if (heightInBlocks > 0) {
+ // "add bytesInLastRow to requiredBytesInCopy."
+ // FIXME: Use checked arithmetic.
+ requiredBytesInCopy += bytesInLastRow;
+ }
+ }
+
+ // "Fail if the following conditions are not satisfied:"
+ // "layout.offset + requiredBytesInCopy ≤ byteSize."
+ // FIXME: Use checked arithmetic.
+ if (layout.offset + requiredBytesInCopy > byteSize)
+ return false;
+
+ return true;
+}
+
} // namespace WebGPU
#pragma mark WGPU Stubs