glTF Byte Alignment for Accessors and BufferViews
In glTF 2.0, precise byte alignment constraints govern how binary
data is organized within bufferView and
accessor structures to ensure efficient GPU memory mapping
and prevent CPU memory alignment faults. This article details the
specific alignment rules required for
bufferView.byteOffset, bufferView.byteStride,
and accessor.byteOffset, explaining how these offsets
correlate with the underlying component data types.
BufferView Alignment Constraints
A bufferView represents a contiguous segment of a raw
binary buffer. glTF enforces two primary alignment
restrictions on bufferView properties:
- byteOffset: The starting point of a
bufferViewwithin its parentbuffermust be aligned to a multiple of 4 bytes. This requirement guarantees that all nested accessors begin at a memory address that satisfies the basic 4-byte boundaries standard across 32-bit and 64-bit hardware architectures. - byteStride: When interleaving vertex attributes
within a single
bufferView, thebyteStridedefines the distance in bytes between consecutive vertex elements. ThebyteStridemust be a multiple of 4 and must fall within the range of 4 to 252 bytes inclusive. If defined, it can only be applied to bufferViews referencing vertex attributes, never to index buffers or animation samplers.
Accessor Alignment Constraints
An accessor defines the typed view (data type, component
type, count) into a bufferView. The alignment of an
accessor is strictly tied to its componentType:
- byteOffset: The
byteOffsetof anaccessorspecifies the offset relative to the start of thebufferView. This value must be an exact multiple of the component type's size in bytes:- 1-byte alignment:
BYTE(5120) andUNSIGNED_BYTE(5121). - 2-byte alignment:
SHORT(5122) andUNSIGNED_SHORT(5123). - 4-byte alignment:
UNSIGNED_INT(5125) andFLOAT(5126).
- 1-byte alignment:
Because bufferView.byteOffset is guaranteed to be a
multiple of 4, aligning accessor.byteOffset to its
component type's size ensures that the absolute offset within the binary
buffer (bufferView.byteOffset + accessor.byteOffset) is
always aligned to the component type's natural boundary.
GLB Container Alignment
When distributing models in the binary container format (.glb), additional padding constraints apply to support overall alignment:
- The binary chunk (
BIN) must be padded with trailing zeroes (0x00) so that its total length is a multiple of 4. - The JSON chunk must be padded with trailing space characters
(
0x20) so that its total length is also a multiple of 4.
These global 4-byte boundaries ensure that the binary data chunk always begins at a 4-byte aligned position relative to the start of the file, allowing direct pointer casting and zero-copy loading in client applications.