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

Code before:
        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;
        } else if (static_cast<uint32_t>(usage) & (static_cast<uint32_t>(wgpu::TextureUsage::CopySrc) | static_cast<uint32_t>(wgpu::TextureUsage::CopyDst))) {
            return wgpu::TextureAspect::All;
        }
        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;
        } else if (static_cast<uint32_t>(usage) & (static_cast<uint32_t>(wgpu::TextureUsage::CopySrc) | static_cast<uint32_t>(wgpu::TextureUsage::CopyDst))) {
            return wgpu::TextureAspect::All;
        }
        return wgpu::TextureAspect::StencilOnly;
    }

    if (format == wgpu::TextureFormat::Depth24PlusStencil8 ||
        format == wgpu::TextureFormat::Depth32FloatStencil8) {
        if (static_cast<uint32_t>(usage) & static_cast<uint32_t>(wgpu::TextureUsage::TextureBinding)) {
            return wgpu::TextureAspect::DepthOnly;
        }
        return wgpu::TextureAspect::All;
    }

    return wgpu::TextureAspect::All;
}

wgpu::TextureView WGPUTexture::makeTextureView(const uint8_t& baseLevel, const uint8_t& levelCount,
        SamplerType target) {

    wgpu::TextureViewDescriptor textureViewDescriptor{
        .label = getUserTextureViewLabel(target),
        .format = mFormat,
        .baseMipLevel = baseLevel,
        .mipLevelCount = levelCount,
        // TODO: check if this baseArrayLayer assumption is correct
        .baseArrayLayer = 0,
        .arrayLayerCount = mArrayLayerCount,
        .aspect = inferWGPUTextureViewAspect(mFormat, mUsage),
        .usage = mUsage
    };

    switch (target) {
        case SamplerType::SAMPLER_2D:
            textureViewDescriptor.dimension = wgpu::TextureViewDimension::e2D;
            break;
        case SamplerType::SAMPLER_2D_ARRAY:

Review comment (suggestion):
No need for the repeated `return wgpu::TextureAspect::All` statements for most(maybe all) of these, especially the ones with multiple other ifs to get t here. If you don't enter one of the internal `if` blocks returning something else,  you won't enter any of the other `format == wgpu::TextureFormat` blocks and hit the bottom one instead.

Code after:
    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;
    }

    if (fFormat == filament::backend::TextureFormat::DEPTH24_STENCIL8 ||
            fFormat == filament::backend::TextureFormat::DEPTH32F_STENCIL8) {
        if (isSample) {
            return wgpu::TextureAspect::DepthOnly;
        }
    }

    return wgpu::TextureAspect::All;
}

wgpu::TextureView WGPUTexture::makeTextureView(const uint8_t& baseLevel, const uint8_t& levelCount,
        SamplerType target) {

    wgpu::TextureViewDescriptor textureViewDescriptor{
        .label = getUserTextureViewLabel(target),
        .format = mFormat,
        .baseMipLevel = baseLevel,
        .mipLevelCount = levelCount,
        // TODO: check if this baseArrayLayer assumption is correct
        .baseArrayLayer = 0,
        .arrayLayerCount = mArrayLayerCount,
        .aspect = mAspect,
        .usage = mUsage
    };

    switch (target) {
        case SamplerType::SAMPLER_2D:
            textureViewDescriptor.dimension = wgpu::TextureViewDimension::e2D;
            break;
        case SamplerType::SAMPLER_2D_ARRAY:
            textureViewDescriptor.dimension = wgpu::TextureViewDimension::e2DArray;