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

Code before:
    // Recreate some of the info (Sampler in slot X with the actual sampler) so letting Dawn confirm
    // there isn't a mismatch may be easiest.
    // Also storing the wgpu ObjectBase takes care of ownership challenges in theory
    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> mEntriesSortedByBinding;
    std::bitset<MAX_DESCRIPTOR_COUNT> mEntriesByBindingWithDynamicOffsets {};
    std::bitset<MAX_DESCRIPTOR_COUNT> mEntriesByBindingAdded {};
    std::vector<uint32_t> mDynamicOffsets;
    wgpu::BindGroup mBindGroup = nullptr;
};

class WGPUTexture : public HwTexture {
public:
    WGPUTexture(SamplerType target, 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;

    const wgpu::Texture& getTexture() const { return mTexture; }
    const wgpu::TextureView& getTexView() const { return mTexView; }

    static wgpu::TextureFormat fToWGPUTextureFormat(const filament::backend::TextureFormat& fUsage);
    static wgpu::TextureAspect inferWGPUTextureViewAspect(const wgpu::TextureFormat& format, const wgpu::TextureUsage& usage);

private:
    wgpu::TextureView makeTextureView(const uint8_t& baseLevel, const uint8_t& levelCount,
            SamplerType target);
    // 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::TextureUsage mUsage = wgpu::TextureUsage::None;
    wgpu::TextureFormat mFormat = wgpu::TextureFormat::Undefined;
    uint32_t mArrayLayerCount = 1;
    wgpu::TextureView mTexView = nullptr;
    wgpu::TextureUsage fToWGPUTextureUsage(const filament::backend::TextureUsage& fUsage);

};

struct WGPURenderPrimitive : public HwRenderPrimitive {

    WGPUVertexBuffer* vertexBuffer = nullptr;
    WGPUIndexBuffer* indexBuffer = nullptr;
};

class WGPURenderTarget : public HwRenderTarget {
public:
    class Attachment {

Review comment (bug):
nit: technically `const wgpu::TextureFormat& format` and `wgpu::TextureFormat const& format` have the same meaning to the compiler, which is a reference to something, where the something cannot be changed/mutated in this scope. Thus, there is nothing technically wrong here. However, I would recommend adopting the habit of preferring `const&`, that is `wgpu::TextureFormat const& format`, because it is generally more consistent with syntax elsewhere in the language, notably when dealing with pointers _(the `*` around a type)_. This is sometimes referred to as "East const". Often confusion arises, especially with folks that don't normally deal with pointers, about whether the `const` applies to the pointer itself or the data it points to. More commonly we care about applying the `const` to the data, as making the pointer immutable itself is typically less of risk. In a lot of cases C/C++ doesn't care which side the `const` is, but there are cases where it does; if you follow this convention you will be less likely to be tripped up by the exceptions _(this is more applicable to the use of pointers, `*`, than references, but doing the same thing consistently is nice IMO)_.

Code after:
    // Recreate some of the info (Sampler in slot X with the actual sampler) so letting Dawn confirm
    // there isn't a mismatch may be easiest.
    // Also storing the wgpu ObjectBase takes care of ownership challenges in theory
    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> mEntriesSortedByBinding;
    std::bitset<MAX_DESCRIPTOR_COUNT> mEntriesByBindingWithDynamicOffsets{};
    std::bitset<MAX_DESCRIPTOR_COUNT> mEntriesByBindingAdded{};
    std::vector<uint32_t> mDynamicOffsets;
    wgpu::BindGroup mBindGroup = nullptr;
};

class WGPUTexture : public HwTexture {
public:
    WGPUTexture(SamplerType target, 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;

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

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

private:
    wgpu::TextureView makeTextureView(const uint8_t& baseLevel, const uint8_t& levelCount,
            SamplerType target);
    // 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::TextureUsage mUsage = wgpu::TextureUsage::None;
    wgpu::TextureFormat mFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureAspect mAspect = wgpu::TextureAspect::Undefined;
    uint32_t mArrayLayerCount = 1;
    wgpu::TextureView mTexView = nullptr;
    wgpu::TextureUsage fToWGPUTextureUsage(filament::backend::TextureUsage const& fUsage);
};

struct WGPURenderPrimitive : public HwRenderPrimitive {

    WGPUVertexBuffer* vertexBuffer = nullptr;
    WGPUIndexBuffer* indexBuffer = nullptr;
};