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

Code before:
        case filament::backend::TextureFormat::RGB32UI:
            // No direct mapping in wgpu without alpha.
            return wgpu::TextureFormat::RGBA32Uint;
        case filament::backend::TextureFormat::RGB32I:
            // No direct mapping in wgpu without alpha.
            return wgpu::TextureFormat::RGBA32Sint;
        case filament::backend::TextureFormat::DXT1_RGB:
            return wgpu::TextureFormat::BC1RGBAUnorm;
        case filament::backend::TextureFormat::DXT1_RGBA:
            return wgpu::TextureFormat::BC1RGBAUnorm;
        case filament::backend::TextureFormat::DXT3_RGBA:
            return wgpu::TextureFormat::BC2RGBAUnorm;
        case filament::backend::TextureFormat::DXT5_RGBA:
            return wgpu::TextureFormat::BC3RGBAUnorm;
        case filament::backend::TextureFormat::DXT1_SRGB:
            return wgpu::TextureFormat::BC1RGBAUnormSrgb;
        case filament::backend::TextureFormat::DXT1_SRGBA:
            return wgpu::TextureFormat::BC1RGBAUnormSrgb;
        case filament::backend::TextureFormat::DXT3_SRGBA:
            return wgpu::TextureFormat::BC2RGBAUnormSrgb;
        case filament::backend::TextureFormat::DXT5_SRGBA:
            return wgpu::TextureFormat::BC3RGBAUnormSrgb;
    }
}

wgpu::TextureAspect WGPUTexture::inferWGPUTextureViewAspect(const wgpu::TextureFormat& format,
        const wgpu::TextureUsage& usage) {

    if (format == wgpu::TextureFormat::Depth32Float || format == wgpu::TextureFormat::Depth24Plus ||
            format == wgpu::TextureFormat::Depth16Unorm) {
        if (static_cast<uint32_t>(usage) &
                        static_cast<uint32_t>(wgpu::TextureUsage::RenderAttachment) ||
                static_cast<uint32_t>(usage) &
                        static_cast<uint32_t>(wgpu::TextureUsage::TextureBinding) ||
                static_cast<uint32_t>(usage) &
                        static_cast<uint32_t>(wgpu::TextureUsage::StorageBinding)) {
            return wgpu::TextureAspect::DepthOnly;
        }
    }

    if (format == wgpu::TextureFormat::Stencil8) {
        if (static_cast<uint32_t>(usage) &
                        static_cast<uint32_t>(wgpu::TextureUsage::RenderAttachment) ||
                static_cast<uint32_t>(usage) &
                        static_cast<uint32_t>(wgpu::TextureUsage::StorageBinding)) {
            return wgpu::TextureAspect::StencilOnly;
        }
    }

    if (format == wgpu::TextureFormat::Depth24PlusStencil8 ||
            format == wgpu::TextureFormat::Depth32FloatStencil8) {

Review comment (suggestion):
Could we not use the `TextureUsage` from Filament + format for this directly _(`filament::backend::TextureUsage`)_?

What about something like _(where `usage` is type `filament::backend::TextureUsage`)_?:

```cpp
if (any(usage & TextureUsage::DEPTH_ATTACHMENT) &&
        (none(usage & (TextureUsage::COLOR_ATTACHMENT | TextureUsage::STENCIL_ATTACHMENT)))) {
    ... maybe assert format is consistent with depth data...    
    return wgpu::TextureAspect::DepthOnly;
}
if (any(usage & TextureUsage::STENCIL_ATTACHMENT) &&
        (none(usage & (TextureUsage::COLOR_ATTACHMENT | TextureUsage::DEPTH_ATTACHMENT)))) {
    ... maybe assert format is consistent with stencil data...        
    return wgpu::TextureAspect::StencilOnly;
}
if ... format is clearly depth only (Depth16Unorm, Depth24Plus, Depth32Float)... then return wgpu::TextureAspect::DepthOnly
If ... format is clearly stencil only (Stencil8)... then return wgpu::TextureAspect::StencilOnly
return wgpu::TextureAspect::All;
```

i.e. if the usage is DEPTH_ATTACHMENT and not any other attachment (color or stencil), then it should be DepthOnly right? If the usage is STENCIL_ATTACHMENT and not any other attachment (color or depth), then it should be StencilOnly right? Otherwise, we could infer the aspect from the format IFF it is clearly depth only or stencil only. Otherwise, we should not declare a restriction (All).

Sanity checking the format for DepthOnly and StencilOnly might be a good idea, but probably as an assert, e.g. `assert_invariant(...)`. For instance, if we find that it should be `DepthOnly` we could assert the format is one of the few WebGPU depth formats. If we find it should be `StencilOnly` we should assert the format is one of the few stencil formats.

Code after:
        case filament::backend::TextureFormat::RGB32UI:
            // No direct mapping in wgpu without alpha.
            return wgpu::TextureFormat::RGBA32Uint;
        case filament::backend::TextureFormat::RGB32I:
            // No direct mapping in wgpu without alpha.
            return wgpu::TextureFormat::RGBA32Sint;
        case filament::backend::TextureFormat::DXT1_RGB:
            return wgpu::TextureFormat::BC1RGBAUnorm;
        case filament::backend::TextureFormat::DXT1_RGBA:
            return wgpu::TextureFormat::BC1RGBAUnorm;
        case filament::backend::TextureFormat::DXT3_RGBA:
            return wgpu::TextureFormat::BC2RGBAUnorm;
        case filament::backend::TextureFormat::DXT5_RGBA:
            return wgpu::TextureFormat::BC3RGBAUnorm;
        case filament::backend::TextureFormat::DXT1_SRGB:
            return wgpu::TextureFormat::BC1RGBAUnormSrgb;
        case filament::backend::TextureFormat::DXT1_SRGBA:
            return wgpu::TextureFormat::BC1RGBAUnormSrgb;
        case filament::backend::TextureFormat::DXT3_SRGBA:
            return wgpu::TextureFormat::BC2RGBAUnormSrgb;
        case filament::backend::TextureFormat::DXT5_SRGBA:
            return wgpu::TextureFormat::BC3RGBAUnormSrgb;
    }
}

wgpu::TextureAspect WGPUTexture::fToWGPUTextureViewAspect(TextureUsage const& fUsage,
        TextureFormat const& fFormat) {

    const bool isDepth = any(fUsage & TextureUsage::DEPTH_ATTACHMENT);
    const bool isStencil = any(fUsage & TextureUsage::STENCIL_ATTACHMENT);
    const bool isColor = any(fUsage & TextureUsage::COLOR_ATTACHMENT);
    const bool isSample = (fUsage == TextureUsage::SAMPLEABLE);

    if (isDepth && !isColor && !isStencil) {
        return wgpu::TextureAspect::DepthOnly;
    }

    if (isStencil && !isColor && !isDepth) {
        return wgpu::TextureAspect::StencilOnly;
    }

    if (fFormat == filament::backend::TextureFormat::DEPTH32F ||
            fFormat == filament::backend::TextureFormat::DEPTH24 ||
            fFormat == filament::backend::TextureFormat::DEPTH16) {
        return wgpu::TextureAspect::DepthOnly;
    }

    if (fFormat == filament::backend::TextureFormat::STENCIL8) {
        return wgpu::TextureAspect::StencilOnly;
    }