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

Code before:
    void setBuffer(WGPUBufferObject *bufferObject, uint32_t index);

    Handle<WGPUVertexBufferInfo> vbih;
    utils::FixedCapacityVector<wgpu::Buffer> buffers;
};

struct WGPUIndexBuffer : public HwIndexBuffer {
    WGPUIndexBuffer(wgpu::Device const &device, uint8_t elementSize,
                    uint32_t indexCount);

    wgpu::Buffer buffer;
};

// TODO: Currently WGPUVertexBufferInfo is not used by WebGPU for useful task.
// Update the struct when used by WebGPU driver.
struct WGPUBufferObject : HwBufferObject {
    WGPUBufferObject(BufferObjectBinding bindingType, uint32_t byteCount);

    wgpu::Buffer buffer = nullptr;
    const BufferObjectBinding bufferObjectBinding;
};
class WebGPUDescriptorSetLayout : public HwDescriptorSetLayout {
public:
    WebGPUDescriptorSetLayout(DescriptorSetLayout const& layout, wgpu::Device const& device);
    ~WebGPUDescriptorSetLayout();
    const wgpu::BindGroupLayout& getLayout() const { return mLayout; }
    uint getLayoutSize() const { return mLayoutSize; }

private:
    // TODO: If this is useful elsewhere, remove it from this class
    // Convert Filament Shader Stage Flags bitmask to webgpu equivilant
    static wgpu::ShaderStage filamentStageToWGPUStage(ShaderStageFlags fFlags);
    uint mLayoutSize;
    wgpu::BindGroupLayout mLayout;
};

class WebGPUDescriptorSet : public HwDescriptorSet {
public:
    WebGPUDescriptorSet(const wgpu::BindGroupLayout& layout, uint layoutSize);
    ~WebGPUDescriptorSet();

    wgpu::BindGroup lockAndReturn(wgpu::Device const& device);
    void addEntry(uint index, wgpu::BindGroupEntry entry);
    bool getIsLocked() const { return mIsLocked; }

private:
    // TODO: Consider storing what we used to make the layout. However we need to essentially
    // 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;

Review comment (suggestion):
IMO I would recommend we use `[[nodiscard]]` for getters, and, really, just functions that return something. Pro: it adds some safety. Con: it adds verbosity. I think the Pro outweighs it and there are precedents in this code base.

Code after:
    WGPUVertexBufferInfo(uint8_t bufferCount, uint8_t attributeCount,
            AttributeArray const& attributes)
        : HwVertexBufferInfo(bufferCount, attributeCount),
          attributes(attributes) {}
    AttributeArray attributes;
};

struct WGPUVertexBuffer : public HwVertexBuffer {
    WGPUVertexBuffer(wgpu::Device const &device, uint32_t vextexCount, uint32_t bufferCount,
                     Handle<WGPUVertexBufferInfo> vbih);

    void setBuffer(WGPUBufferObject *bufferObject, uint32_t index);

    Handle<WGPUVertexBufferInfo> vbih;
    utils::FixedCapacityVector<wgpu::Buffer> buffers;
};

struct WGPUIndexBuffer : public HwIndexBuffer {
    WGPUIndexBuffer(wgpu::Device const &device, uint8_t elementSize,
                    uint32_t indexCount);

    wgpu::Buffer buffer;
};

// TODO: Currently WGPUVertexBufferInfo is not used by WebGPU for useful task.
// Update the struct when used by WebGPU driver.
struct WGPUBufferObject : HwBufferObject {
    WGPUBufferObject(BufferObjectBinding bindingType, uint32_t byteCount);

    wgpu::Buffer buffer = nullptr;
    const BufferObjectBinding bufferObjectBinding;
};
class WebGPUDescriptorSetLayout final : public HwDescriptorSetLayout {
public:
    WebGPUDescriptorSetLayout(DescriptorSetLayout const& layout, wgpu::Device const& device);
    ~WebGPUDescriptorSetLayout();
    [[nodiscard]] const wgpu::BindGroupLayout& getLayout() const { return mLayout; }
    [[nodiscard]] uint getLayoutSize() const { return mLayoutSize; }

private:
    // TODO: If this is useful elsewhere, remove it from this class
    // Convert Filament Shader Stage Flags bitmask to webgpu equivilant
    static wgpu::ShaderStage filamentStageToWGPUStage(ShaderStageFlags fFlags);
    uint mLayoutSize;
    wgpu::BindGroupLayout mLayout;
};

class WebGPUDescriptorSet final : public HwDescriptorSet {
public:
    WebGPUDescriptorSet(const wgpu::BindGroupLayout& layout, uint layoutSize);
    ~WebGPUDescriptorSet();