Layouts and shader bindings
A buffer contains bytes. A vertex layout describes how a draw steps through them, while a shader binding identifies a resource a shader can access. Start with the shader’s input contract, then choose a storage layout that satisfies it.
One buffer, two views
For a particle simulation, store each position as four consecutive 32-bit floats. A WebGPU compute shader can declare array<vec4<f32>> in a storage binding. A subsequent draw can use the same buffer as vertex input with format float32x4 and stride 16 bytes. Create it with both storage and vertex usage. The fourth component keeps the stride explicit; a packed three-float vertex record does not have the stride of a WGSL storage array of three-component vectors.
Choose a memory layout
Packed
A packed buffer stores one column with no unrelated data between rows.
positions buffer
row 0: position
row 1: position
row 2: position
Use packed layout when a column is updated, transferred, or bound independently.
Interleaved
An interleaved buffer stores multiple columns inside each row.
instance buffer
row 0: position, color, radius
row 1: position, color, radius
row 2: position, color, radius
Use interleaved layout when columns are usually consumed together by a render or transform pipeline.
Segmented
A segmented buffer stores multiple packed columns in one buffer.
table buffer
segment 0: position row 0, position row 1, position row 2
segment 1: color row 0, color row 1, color row 2
segment 2: radius row 0, radius row 1, radius row 2
Use segmented layout when a table should have one allocation but columns should remain columnar for storage-buffer access.
Bind shader inputs
- Vertex attributes use a format, offset, and stride. Bind each buffer to the input described by the pipeline’s vertex layout. See Vertex attributes for interleaved inputs and indices.
- Uniform buffers supply values shared across the draw or dispatch. Pack fields according to the shader’s uniform layout, including required padding. See Uniform buffers.
- Storage buffers support indexed reads and writes in WebGPU. A WGSL declaration specifies the element type and access mode; the corresponding binding must agree.
Storage buffers become especially useful when the shader reads a well-structured record array:
struct InstanceRecord {
modelMatrix: mat4x4<f32>,
tint: vec4<f32>,
};
@group(0) @binding(auto)
var<storage, read> instances: array<InstanceRecord>;
This is not the same thing as vertex attribute layout:
- storage shaders read raw WGSL memory layouts;
- vertex fetch decodes
VertexFormatvalues such as normalized colors; - padding rules matter for storage arrays, structs, and matrix columns.
When one logical row may need both interpretations, choose names and row layouts
with the record view in mind. Describe attribute-side views directly with
BufferLayout; keep the corresponding WGSL struct or matrix layout explicit in
the shader.
Match the pipeline
| Layout | Render vertex attributes | WebGL transform feedback | WebGPU compute storage buffers | Buffer copy commands |
|---|---|---|---|---|
| Packed | Direct | Direct | Direct | Direct |
| Interleaved | Direct with BufferLayout.attributes | Direct as input or output, not in-place | Usable with explicit strided reads | Row ranges are not contiguous per column |
| Segmented | Usable as separate attribute bindings into one buffer range | Usable with explicit offsets | Direct for columnar storage reads | Column segments are contiguous |
Render pipelines use BufferLayout and vertex formats to interpret bytes. This supports normalized attributes such as unorm8x4 because vertex fetch converts them to shader-visible values.
The packed mode for an inline GPUData struct format follows WebGPU vertex
layout requirements: field offsets are aligned to
min(4, format byte length) and the row stride is a multiple of four. Small
formats can therefore use one- or two-byte alignment, while the complete row
can still be bound as a vertex buffer. These are WebGPU vertex-fetch rules
rather than a wgsl-vertex memory layout.
Compute pipelines use storage bindings. They read raw storage values, so normalized formats are not decoded unless the compute shader does that work explicitly.
Use the bindings guide for named resources, explicit groups, and backend differences. For columnar Arrow data, see GPU data and Arrow.