Repository: google/filament
File: filament/backend/src/webgpu/WebGPUTexture.h (C/C++)

Code before:
     * (this view will only have one mip level)
     * @param arrayLayer The layer into the underyling texture for which this view will reference
     * (this view will only have one layer)
     * @return A texture view for the MSAA sidecar texture
     */
    wgpu::TextureView makeMsaaSidecarTextureViewIfTextureSidecarExists(uint8_t samples,
            uint8_t mipLevel, uint32_t arrayLayer) const;

    /**
     * @return nullptr if a MSAA sidecar texture is not appliable, otherwise a view to one
     */
    [[nodiscard]] wgpu::TextureView makeMsaaSidecarTextureView(wgpu::Texture const&, uint8_t mipLevel, uint32_t arrayLayer) const;

    [[nodiscard]] static wgpu::TextureFormat fToWGPUTextureFormat(
            filament::backend::TextureFormat const& fFormat);

    /**
     * @param format a required texture format (can be a view to a different underlying texture
     * format)
     * @return true if multiple mip levels can be generated via a compute shader + storage binding
     * on the texture, false otherwise.
     */
    [[nodiscard]] static bool supportsMultipleMipLevelsViaStorageBinding(
            wgpu::TextureFormat format);

    [[nodiscard]] static size_t getWGPUTextureFormatPixelSize(wgpu::TextureFormat format);

private:
    // the texture view's format, which may differ from the underlying texture's format itself,
    // an example of when this is needed is when certain underlying shaders don't support
    // certain formats (e.g. compute shaders), but we need a view to that format elsewhere
    // (e.g. another shader or render target)
    wgpu::TextureFormat mViewFormat = wgpu::TextureFormat::Undefined;
    MipmapGenerationStrategy mMipmapGenerationStrategy = MipmapGenerationStrategy::NONE;
    // format is inherited from HwTexture. This naming is to distinguish it from Filament's format
    // this is the underlying texture's format, not necessarily the "view" of it... see mViewFormat
    wgpu::TextureFormat mWebGPUFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureAspect mAspect = wgpu::TextureAspect::Undefined;
    // usage is inherited from HwTexture. This naming is to distinguish it from Filament's usage
    wgpu::TextureUsage mWebGPUUsage = wgpu::TextureUsage::None;
    wgpu::TextureUsage mViewUsage = wgpu::TextureUsage::None;
    wgpu::TextureViewDimension mDimension = wgpu::TextureViewDimension::Undefined;
    size_t mBlockWidth = 0;
    size_t mBlockHeight = 0;
    uint32_t mArrayLayerCount = 1;
    wgpu::Texture mTexture = nullptr;
    uint32_t mDefaultMipLevel = 0;
    uint32_t mDefaultBaseArrayLayer = 0;
    wgpu::TextureView mDefaultTextureView = nullptr;
    // At the time of writing this, WebGPU only supported 4 samples in a multi-sampled texture.
    // If that has changed, then consider updating the implementation to have a map of msaa textures

Review comment (refactor):
Not that you should change this; this is fine. But,  I just wanted to point out that likely this function could be improved by being `constexpr`. That said, the benefits of that would not be realized (yet) by how it is being used in this PR (it would only be nice in a future refactor that may or many never happen), and, of course, the downside of having it constexpr is that it would have to be implemented here in the header vs. the corresponding implementation .cpp file (since the function would need to be shared outside the WebGPUTexture translation unit). I just bring it up as food for thought if you have not already thought through that.

Code after:
     * (this view will only have one mip level)
     * @param arrayLayer The layer into the underyling texture for which this view will reference
     * (this view will only have one layer)
     * @return A texture view for the MSAA sidecar texture
     */
    wgpu::TextureView makeMsaaSidecarTextureViewIfTextureSidecarExists(uint8_t samples,
            uint8_t mipLevel, uint32_t arrayLayer) const;

    /**
     * @return nullptr if a MSAA sidecar texture is not appliable, otherwise a view to one
     */
    [[nodiscard]] wgpu::TextureView makeMsaaSidecarTextureView(wgpu::Texture const&, uint8_t mipLevel, uint32_t arrayLayer) const;

    [[nodiscard]] static wgpu::TextureFormat fToWGPUTextureFormat(
            filament::backend::TextureFormat const& fFormat);

    /**
     * @param format a required texture format (can be a view to a different underlying texture
     * format)
     * @return true if multiple mip levels can be generated via a compute shader + storage binding
     * on the texture, false otherwise.
     */
    [[nodiscard]] static bool supportsMultipleMipLevelsViaStorageBinding(
            wgpu::TextureFormat format);

    [[nodiscard]] static size_t getWGPUTextureFormatPixelSize(const wgpu::TextureFormat format);

private:
    // the texture view's format, which may differ from the underlying texture's format itself,
    // an example of when this is needed is when certain underlying shaders don't support
    // certain formats (e.g. compute shaders), but we need a view to that format elsewhere
    // (e.g. another shader or render target)
    wgpu::TextureFormat mViewFormat = wgpu::TextureFormat::Undefined;
    MipmapGenerationStrategy mMipmapGenerationStrategy = MipmapGenerationStrategy::NONE;
    // format is inherited from HwTexture. This naming is to distinguish it from Filament's format
    // this is the underlying texture's format, not necessarily the "view" of it... see mViewFormat
    wgpu::TextureFormat mWebGPUFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureAspect mAspect = wgpu::TextureAspect::Undefined;
    // usage is inherited from HwTexture. This naming is to distinguish it from Filament's usage
    wgpu::TextureUsage mWebGPUUsage = wgpu::TextureUsage::None;
    wgpu::TextureUsage mViewUsage = wgpu::TextureUsage::None;
    wgpu::TextureViewDimension mDimension = wgpu::TextureViewDimension::Undefined;
    size_t mBlockWidth = 0;
    size_t mBlockHeight = 0;
    uint32_t mArrayLayerCount = 1;
    wgpu::Texture mTexture = nullptr;
    uint32_t mDefaultMipLevel = 0;
    uint32_t mDefaultBaseArrayLayer = 0;
    wgpu::TextureView mDefaultTextureView = nullptr;
    // At the time of writing this, WebGPU only supported 4 samples in a multi-sampled texture.
    // If that has changed, then consider updating the implementation to have a map of msaa textures