Repository: google/filament
File: filament/backend/src/webgpu/WebGPUDriver.cpp (C++)

Code before:

void WebGPUDriver::createBufferObjectR(Handle<HwBufferObject> bufferObjectHandle,
        const uint32_t byteCount, const BufferObjectBinding bindingType, const BufferUsage usage) {
    constructHandle<WebGPUBufferObject>(bufferObjectHandle, mDevice, bindingType, byteCount);
}

void WebGPUDriver::createTextureR(Handle<HwTexture> textureHandle, const SamplerType target,
        const uint8_t levels, const TextureFormat format, const uint8_t samples,
        const uint32_t width, const uint32_t height, const uint32_t depth,
        const TextureUsage usage) {
    constructHandle<WebGPUTexture>(textureHandle, target, levels, format, samples, width, height,
            depth, usage, mDevice);
}

void WebGPUDriver::createTextureViewR(Handle<HwTexture> textureHandle,
        Handle<HwTexture> sourceTextureHandle, const uint8_t baseLevel, const uint8_t levelCount) {
    auto source = handleCast<WebGPUTexture>(sourceTextureHandle);

    constructHandle<WebGPUTexture>(textureHandle, source, baseLevel, levelCount);
}

void WebGPUDriver::createTextureViewSwizzleR(Handle<HwTexture> textureHandle,
        Handle<HwTexture> sourceTextureHandle, const backend::TextureSwizzle r,
        const backend::TextureSwizzle g, const backend::TextureSwizzle b,
        const backend::TextureSwizzle a) {

    auto sourceTexture = handleCast<WebGPUTexture>(sourceTextureHandle);
    assert_invariant(sourceTexture);

    wgpu::TextureComponentSwizzle swizzle;
    swizzle.r = toWGPUComponentSwizzle(r);
    swizzle.g = toWGPUComponentSwizzle(g);
    swizzle.b = toWGPUComponentSwizzle(b);
    swizzle.a = toWGPUComponentSwizzle(a);

    wgpu::TextureComponentSwizzleDescriptor swizzleDesc {};
    swizzleDesc.swizzle = swizzle;

    wgpu::TextureViewDescriptor viewDesc = {
        .nextInChain = &swizzleDesc,
        .label = "swizzled_texture_view",
        .format = sourceTexture->getTexture().GetFormat(),
        .dimension = sourceTexture->getViewDimension(),
        .baseMipLevel = 0,
        .mipLevelCount = sourceTexture->getTexture().GetMipLevelCount(),
        .baseArrayLayer = 0,
        .arrayLayerCount = sourceTexture->getTexture().GetDepthOrArrayLayers(),
    };

    wgpu::TextureView swizzledView = sourceTexture->getTexture().CreateView(&viewDesc);
    constructHandle<WebGPUTexture>(textureHandle, sourceTexture, (swizzledView));

Review comment (suggestion):
I would add a guard here, where this should not be getting called if texture swizzling is not supported, e.g.:
```cpp
FILAMENT_CHECK_PRECONDITION(isTextureSwizzleSupported()) << "Unexpected call to swizzle a texture when the backend/driver does not support it";
```

Code after:

void WebGPUDriver::createBufferObjectR(Handle<HwBufferObject> bufferObjectHandle,
        const uint32_t byteCount, const BufferObjectBinding bindingType, const BufferUsage usage) {
    constructHandle<WebGPUBufferObject>(bufferObjectHandle, mDevice, bindingType, byteCount);
}

void WebGPUDriver::createTextureR(Handle<HwTexture> textureHandle, const SamplerType target,
        const uint8_t levels, const TextureFormat format, const uint8_t samples,
        const uint32_t width, const uint32_t height, const uint32_t depth,
        const TextureUsage usage) {
    constructHandle<WebGPUTexture>(textureHandle, target, levels, format, samples, width, height,
            depth, usage, mDevice);
}

void WebGPUDriver::createTextureViewR(Handle<HwTexture> textureHandle,
        Handle<HwTexture> sourceTextureHandle, const uint8_t baseLevel, const uint8_t levelCount) {
    auto source = handleCast<WebGPUTexture>(sourceTextureHandle);

    constructHandle<WebGPUTexture>(textureHandle, source, baseLevel, levelCount);
}

void WebGPUDriver::createTextureViewSwizzleR(Handle<HwTexture> textureHandle,
        Handle<HwTexture> sourceTextureHandle, const backend::TextureSwizzle r,
        const backend::TextureSwizzle g, const backend::TextureSwizzle b,
        const backend::TextureSwizzle a) {

    if (!isTextureSwizzleSupported()) {
        FWGPU_LOGW << "WebGPUDriver::createTextureViewSwizzleR called while texture swizzling is "
                      "not supported by the device/driver";
    }

    auto sourceTexture{ handleCast<WebGPUTexture>(sourceTextureHandle) };
    assert_invariant(sourceTexture);

    wgpu::TextureComponentSwizzle swizzle
    {
        .r = toWGPUComponentSwizzle(r),
        .g = toWGPUComponentSwizzle(g),
        .b = toWGPUComponentSwizzle(b),
        .a = toWGPUComponentSwizzle(a),
    };

    wgpu::TextureComponentSwizzleDescriptor swizzleDesc {};
    swizzleDesc.swizzle = swizzle;

    const wgpu::TextureViewDescriptor viewDesc {
        .nextInChain = &swizzleDesc,
        .label = "swizzled_texture_view",
        .format = sourceTexture->getTexture().GetFormat(),
        .dimension = sourceTexture->getViewDimension(),
        .baseMipLevel = 0,