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

Code before:

    inline wgpu::VertexAttribute const* getVertexAttributeForIndex(uint32_t index) const {
        return mAttributes[index].data();
    }
    inline uint32_t getVertexAttributeSize(uint32_t index) const {
        return mAttributes[index].size();
    }
private:
    std::vector<wgpu::VertexBufferLayout> mVertexBufferLayout {};
    std::vector<std::vector<wgpu::VertexAttribute>> mAttributes {};
};

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

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

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

    wgpu::Buffer buffer;
    wgpu::IndexFormat indexFormat;
};

struct WGPUBufferObject : HwBufferObject {
    WGPUBufferObject(wgpu::Device const &device, BufferObjectBinding bindingType, uint32_t byteCount);

    wgpu::Buffer buffer;
    const BufferObjectBinding bufferObjectBinding;
};

class WebGPUDescriptorSetLayout : public HwDescriptorSetLayout {
public:
    WebGPUDescriptorSetLayout(DescriptorSetLayout const& layout, wgpu::Device const* device);
    ~WebGPUDescriptorSetLayout();

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);

    wgpu::BindGroupLayout mLayout;
};

// TODO: Currently WGPUTexture is not used by WebGPU for useful task.
// Update the struct when used by WebGPU driver.
struct WGPUTexture : public HwTexture {

Review comment (suggestion):
If we are going to use a vector we should use `FixedCapacityVector` to avoid any extra potential heap allocations.

Code after:
// of vertex buffer is implicitly calculated by the position of VertexBufferLayout in an array.
class WGPUVertexBufferInfo : public HwVertexBufferInfo {
public:
    WGPUVertexBufferInfo(uint8_t bufferCount, uint8_t attributeCount,
            AttributeArray const& attributes);
    inline  wgpu::VertexBufferLayout const* getVertexBufferLayout() const {
        return mVertexBufferLayout.data();
    }

    inline uint32_t getVertexBufferLayoutSize() const {
        return mVertexBufferLayout.size();
    }

    inline wgpu::VertexAttribute const* getVertexAttributeForIndex(uint32_t index) const {
        assert_invariant(index < mAttributes.size());
        return mAttributes[index].data();
    }

    inline uint32_t getVertexAttributeSize(uint32_t index) const {
        assert_invariant(index < mAttributes.size());
        return mAttributes[index].size();
    }

private:
    // TODO: can we do better in terms on heap management.
    std::vector<wgpu::VertexBufferLayout> mVertexBufferLayout {};
    std::vector<std::vector<wgpu::VertexAttribute>> mAttributes {};
};

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

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

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

    wgpu::Buffer buffer;
    wgpu::IndexFormat indexFormat;
};

struct WGPUBufferObject : HwBufferObject {
    WGPUBufferObject(wgpu::Device const &device, BufferObjectBinding bindingType, uint32_t byteCount);

    wgpu::Buffer buffer = nullptr;
    const BufferObjectBinding bufferObjectBinding;
};