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

Code before:

private:
    wgpu::BindGroupLayout mLayout = nullptr;
    static constexpr uint8_t INVALID_INDEX = MAX_DESCRIPTOR_COUNT + 1;
    std::array<uint8_t, MAX_DESCRIPTOR_COUNT> mEntryIndexByBinding{};
    std::vector<wgpu::BindGroupEntry> mEntries;
    const size_t mEntriesWithDynamicOffsetsCount;
    wgpu::BindGroup mBindGroup = nullptr;
};

class WGPUTexture : public HwTexture {
public:
    WGPUTexture(SamplerType samplerType, uint8_t levels, TextureFormat format,
            uint8_t samples, uint32_t width, uint32_t height, uint32_t depth, TextureUsage usage,
            wgpu::Device const& device) noexcept;

    WGPUTexture(WGPUTexture* src, uint8_t baseLevel, uint8_t levelCount) noexcept;
    wgpu::TextureAspect getAspect() const { return mAspect; }
    size_t getBlockWidth() const { return mBlockWidth; }
    size_t getBlockHeight() const { return mBlockHeight; }


    [[nodiscard]] const wgpu::Texture& getTexture() const { return mTexture; }

    [[nodiscard]] wgpu::TextureView getTextureView() const {return mTextureView;}
    [[nodiscard]] wgpu::TextureView getTextureView(uint8_t mipLevel, uint32_t arrayLayer) const;

    [[nodiscard]] wgpu::TextureFormat getFormat() const { return mFormat; }
    [[nodiscard]] uint32_t getArrayLayerCount() const { return mArrayLayerCount; }
    [[nodiscard]] filament::backend::SamplerType getSamplerType() const { return mSamplerType; }


    static wgpu::TextureFormat fToWGPUTextureFormat(
            filament::backend::TextureFormat const& fFormat);
    static wgpu::TextureAspect fToWGPUTextureViewAspect(
            filament::backend::TextureUsage const& fUsage,
            filament::backend::TextureFormat const& fFormat);

private:
    // CreateTextureR has info for a texture and sampler. Texture Views are needed for binding,
    // along with a sampler Current plan: Inherit the sampler and Texture to always exist (It is a
    // ref counted pointer) when making views. View is optional
    wgpu::Texture mTexture = nullptr;
    wgpu::TextureView mTextureView = nullptr;
    wgpu::TextureFormat mFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureAspect mAspect = wgpu::TextureAspect::Undefined;
    wgpu::TextureUsage mUsage = wgpu::TextureUsage::None;
    uint32_t mArrayLayerCount = 1;
    size_t mBlockWidth;
    size_t mBlockHeight;
    SamplerType mSamplerType;

Review comment (suggestion):
nit: I suggest naming the function to indicate it may be more than a getter, e.g. `getOrMakeTextureView`.

Code after:

private:
    wgpu::BindGroupLayout mLayout = nullptr;
    static constexpr uint8_t INVALID_INDEX = MAX_DESCRIPTOR_COUNT + 1;
    std::array<uint8_t, MAX_DESCRIPTOR_COUNT> mEntryIndexByBinding{};
    std::vector<wgpu::BindGroupEntry> mEntries;
    const size_t mEntriesWithDynamicOffsetsCount;
    wgpu::BindGroup mBindGroup = nullptr;
};

class WGPUTexture : public HwTexture {
public:
    WGPUTexture(SamplerType samplerType, uint8_t levels, TextureFormat format,
            uint8_t samples, uint32_t width, uint32_t height, uint32_t depth, TextureUsage usage,
            wgpu::Device const& device) noexcept;

    WGPUTexture(WGPUTexture* src, uint8_t baseLevel, uint8_t levelCount) noexcept;
    wgpu::TextureAspect getAspect() const { return mAspect; }
    size_t getBlockWidth() const { return mBlockWidth; }
    size_t getBlockHeight() const { return mBlockHeight; }

    [[nodiscard]] const wgpu::Texture& getTexture() const { return mTexture; }

    [[nodiscard]] wgpu::TextureView getDefaultTextureView() const {return mDefaultTextureView;}
    [[nodiscard]] wgpu::TextureView getOrMakeTextureView(uint8_t mipLevel, uint32_t arrayLayer) const;

    [[nodiscard]] wgpu::TextureFormat getFormat() const { return mFormat; }
    [[nodiscard]] uint32_t getArrayLayerCount() const { return mArrayLayerCount; }

    static wgpu::TextureFormat fToWGPUTextureFormat(
            filament::backend::TextureFormat const& fFormat);
    static wgpu::TextureAspect fToWGPUTextureViewAspect(
            filament::backend::TextureUsage const& fUsage,
            filament::backend::TextureFormat const& fFormat);

private:
    // CreateTextureR has info for a texture and sampler. Texture Views are needed for binding,
    // along with a sampler Current plan: Inherit the sampler and Texture to always exist (It is a
    // ref counted pointer) when making views. View is optional
    wgpu::Texture mTexture = nullptr;
    wgpu::TextureFormat mFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureAspect mAspect = wgpu::TextureAspect::Undefined;
    wgpu::TextureUsage mUsage = wgpu::TextureUsage::None;
    uint32_t mArrayLayerCount = 1;
    size_t mBlockWidth;
    size_t mBlockHeight;
    SamplerType mSamplerType;
    wgpu::TextureView mDefaultTextureView = nullptr;
    uint32_t mDefaultMipLevel = 0;
    uint32_t mDefaultBaseArrayLayer = 0;