Skip to content

BufferAttribute

Represents a named stream of per-vertex or per-instance data.

BufferAttribute is the basic unit of mesh data. It owns a flat array of floats along with the metadata needed to interpret it: a name that matches the attribute's name in shader code, a format describing the number of components per element, and a rate that determines whether the data advances per vertex or per instance.

Attributes are added to a Geometry (vertex rate) or an instanced mesh (instance rate) and may be shared by multiple geometries. The renderer uploads attribute data to the GPU on first use and re-uploads it whenever the data changes, using an internal version that SetData and Write increment.

cpp
auto positions = vglx::BufferAttribute::Create({
  .name = vglx::BufferAttribute::kPosition,
  .format = vglx::BufferAttribute::Format::Float32x3,
  .rate = vglx::BufferAttribute::Rate::Vertex
}, {
  0.5f, -0.5f, 0.0f,
  0.0f,  0.5f, 0.0f,
 -0.5f, -0.5f, 0.0f,
});

geometry->AddAttribute(positions);

Note that attributes are non-interleaved: each attribute holds a tightly packed array of its own values and is uploaded to its own GPU buffer rather than sharing an interleaved buffer with other attributes.

Construction

Constructors


BufferAttribute()

Constructs a buffer attribute.

The data size must be divisible by the number of components implied by the format. Invalid parameters are reported and leave the attribute in an invalid state.

cpp
BufferAttribute::BufferAttribute(const Params& params, std::vector<float> data);
ParameterDescription
paramsAttribute parameters.
dataFlat array of floats containing the attribute data.

Factories preferred


BufferAttribute::Create() std::shared_ptr<BufferAttribute>

Creates a shared instance of BufferAttribute.

cpp
static std::shared_ptr<BufferAttribute> BufferAttribute::Create(const Params& params, std::vector<float> data);
ParameterDescription
paramsAttribute parameters.
dataFlat array of floats containing the attribute data.

Types

BufferAttribute::Format enum

Enumerates supported attribute data formats.

Describes the number of 32-bit float components that make up a single element.

ValueDescription
Float32x1One float per element (scalar).
Float32x2Two floats per element (vec2).
Float32x3Three floats per element (vec3).
Float32x4Four floats per element (vec4).
Float32x16Sixteen floats per element (mat4).

BufferAttribute::Rate enum

Determines how the attribute advances during rendering.

ValueDescription
VertexOne element per vertex.
InstanceOne element per instance.

BufferAttribute::Params struct

Parameters for constructing a buffer attribute.

ParameterDescription
name std::string_viewAttribute name matching its name in shader code.
format FormatNumber of components per element.
rate RatePer-vertex or per-instance rate.

Properties

Known attribute names

Attributes created with these names are picked up automatically by the built-in shaders using the formats listed below. Attributes with any other name are custom: they require a matching declaration in the shader code of a ShaderMaterial. Prefer these constants over spelling the strings out to avoid silent name mismatches.

ConstantDescription
kPositionVertex position in object space (Float32x3).
kNormalVertex normal in object space (Float32x3).
kTexCoordTexture coordinates (Float32x2).
kColorPer-vertex color (Float32x3).
kTangentTangent with handedness in w for normal mapping (Float32x4).
kInstanceTransformPer-instance transform matrix (Float32x16, instance rate).
kInstanceColorPer-instance color (Float32x3, instance rate).

format const Format

Number of float components per element.

cpp
const Format format {Format::Float32x1};

name const std::string

Attribute name matching its name in shader code.

cpp
const std::string name {};

rate const Rate

Determines whether the data advances per vertex or per instance.

cpp
const Rate rate {Rate::Vertex};

Functions

Components() uint32_t

Returns the number of float components per element.

cpp
uint32_t BufferAttribute::Components() const;

ElementCount() uint32_t

Returns the number of elements stored in the attribute.

The element count is derived from the data size and the number of components.

cpp
uint32_t BufferAttribute::ElementCount() const;

GetData() const std::vector<float>&

Returns a read-only reference to the attribute data.

cpp
const std::vector<float>& BufferAttribute::GetData() const;

GetVersion() uint32_t

Returns the data version, incremented on every update.

The renderer compares versions to decide when the attribute needs to be re-uploaded.

cpp
uint32_t BufferAttribute::GetVersion() const;

IsValid() bool

Checks whether the attribute has a name and data.

cpp
bool BufferAttribute::IsValid() const;

SetData() void

Replaces the attribute data.

The new data must contain the same number of elements as the existing data. Attributes cannot be resized after construction. To change the element count, create a new attribute instead. Data must also be divisible by the number of components implied by the format, otherwise the update is rejected. A successful update marks the attribute for re-upload.

cpp
void BufferAttribute::SetData(std::vector<float> data);
ParameterDescription
dataFlat array of floats containing the new attribute data.

Write() void

Overwrites a range of the attribute data in place.

The range [offset, offset + values.size()) must fit within the existing data, otherwise the write is rejected. A successful write marks the attribute for re-upload.

cpp
void BufferAttribute::Write(std::size_t offset, std::span<const float> values);
ParameterDescription
offsetPosition of the first float to overwrite.
valuesValues to copy into the attribute data.

Released under the MIT License.