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

Code before:
}

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));
}

void WebGPUDriver::createTextureExternalImage2R(Handle<HwTexture> textureHandle,
        const backend::SamplerType target, const backend::TextureFormat format,

Review comment (suggestion):
designated initializer?:
```cpp
const wgpu::TextureComponentSwizzle swizzle{
    .r = toWGPUComponentSwizzle(r),
    .g = toWGPUComponentSwizzle(g),
    .b = toWGPUComponentSwizzle(b),
    .a = toWGPUComponentSwizzle(a),
};
```

Code after:
}

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,
        .mipLevelCount = sourceTexture->getTexture().GetMipLevelCount(),
        .baseArrayLayer = 0,
        .arrayLayerCount = sourceTexture->getTexture().GetDepthOrArrayLayers(),
    };