Skip to content

fix: Make an explicit VertexFormat stride the vertex size - #10127

Open
AlexAPPi wants to merge 6 commits into
playcanvas:mainfrom
AlexAPPi:al-docs-o-s-vf-a-d
Open

AlexAPPi wants to merge 6 commits into
playcanvas:mainfrom
AlexAPPi:al-docs-o-s-vf-a-d

Conversation

@AlexAPPi

@AlexAPPi AlexAPPi commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

An interleaved VertexFormat now has one stride, and that stride is the vertex size the buffer is allocated from. Previously an explicit stride was stored on the attribute but ignored by VertexFormat.size, so the vertex buffer was too small for the layout the GPU read. WebGPU also used only the last attribute's stride, while WebGL used each attribute's own.

Changes

  • A stride set on the attributes becomes VertexFormat.size, including for attributes that omit one. Every interleaved attribute stores that same stride. A VertexBuffer holds size bytes per vertex.
  • When no stride is set, the size stays the packed sum: each attribute is rounded up to a multiple of 4.
  • When the explicit strides differ, or a stride is not a positive whole number, the packed size is used instead. A stride of 0 is not used. WebGL would treat it as tightly packed, and WebGPU would repeat one element for every vertex.
  • null and undefined for offset and stride are omitted. An omitted offset stays on the packed 4-byte boundary, so it can overlap an earlier explicit offset. An attribute must fit in the stride (offset + size).
  • vertexCount, including 0, still selects a non-interleaved format. Offset and stride on the description are ignored. Each attribute is stored in its own array.
  • Debug builds report a layout the backends would reject: component alignment, WebGPU offset alignment of min(4, format size) for real vertex formats, a WebGL stride above 255, and a WebGPU stride that is not a multiple of 4 or is above 2048.
  • The rendering hash separates offset, stride and size, so layouts such as offset 2 / stride 224 and offset 22 / stride 24 no longer share a WebGPU pipeline.

Public API

AttributeDescription now documents offset and stride. VertexFormat.size and the resolved elements[].offset / elements[].stride describe both interleaved and non-interleaved layouts.

Before, two float3 attributes with stride 32 still produced a 24-byte vertex:

const vertexFormat = new VertexFormat(graphicsDevice, [
    { semantic: SEMANTIC_POSITION, components: 3, type: TYPE_FLOAT32, stride: 32 },
    { semantic: SEMANTIC_NORMAL, components: 3, type: TYPE_FLOAT32, stride: 32 }
]);
// vertexFormat.size === 24

After, the shared stride is the vertex size:

const vertexFormat = new VertexFormat(graphicsDevice, [
    { semantic: SEMANTIC_POSITION, components: 3, type: TYPE_FLOAT32, stride: 32 },
    { semantic: SEMANTIC_NORMAL, components: 3, type: TYPE_FLOAT32, stride: 32 }
]);
// vertexFormat.size === 32
// the vertex buffer holds 32 bytes per vertex

@mvaligursky mvaligursky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The new text matches what the constructor does, but making offset and stride public documents a path that only partly works, so a few things need addressing first.

A stride larger than the packed vertex size doesn't work. VertexFormat.size ignores explicit strides, and VertexBuffer allocates format.size * numVertices. The example in the description (two float32x3 attributes with stride: 32) gets a size of 24, so the buffer is 24·N bytes while the GPU reads up to 32·N−8 bytes:

  • WebGPU fails draw validation because the vertex buffer is too small.
  • WebGL2 reads zeros or garbage for the trailing vertices.
  • Passing 32·N bytes as options.data is rejected by the size check in setData.

WebGPU uses a single stride per format. For an interleaved format, WebgpuVertexBufferLayout uses the last element's stride as arrayStride for every attribute, while WebGL honours each attribute's own stride. Different strides on different attributes therefore render differently on the two backends.

Smaller points:

  • The constructor uses hasOwnProperty, so offset: undefined and stride: undefined are taken as given rather than defaulted. WebGL treats them as 0, and WebGPU fails pipeline creation. "When unspecified" should say "when omitted", or the code should switch to ??.
  • A default offset is the packed position, whatever offsets earlier attributes set, so mixing explicit and default offsets can make attributes overlap. In the same way, explicit offsets that add padding without a stride run into the next vertex.
  • The limits aren't mentioned. WebGPU needs each offset to be a multiple of min(4, format size), and the stride to be a multiple of 4 and at most 2048. WebGL needs both values to be multiples of the component type size, and the stride to be at most 255. stride: 0 also means "tightly packed" on WebGL but "every vertex reads the same element" on WebGPU.
  • "Ignored when vertexCount is specified": the code checks truthiness, so vertexCount = 0 still uses them.
  • The class-level elements[].offset / elements[].stride descriptions (lines 62–65) now disagree with the new text, and they're wrong for non-interleaved formats.

I'd suggest one of two ways forward. Either state the limits plainly in the docs: the stride must equal the packed vertex size, and offsets only reorder attributes within it. Or make a small constructor change so the docs become true: when explicit strides are given, assert that they're all equal and use that value as size, assert offset + elementSize <= stride and the alignment, and use ?? instead of hasOwnProperty.

@AlexAPPi AlexAPPi changed the title docs: Document offset and stride on VertexFormat attribute descriptions fix: Make an explicit VertexFormat stride the vertex size Nov 23, 2026
@AlexAPPi
AlexAPPi requested a review from mvaligursky November 23, 2026 21:43

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants