Repository navigation
Conversation
mvaligursky
left a comment
There was a problem hiding this comment.
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.datais rejected by the size check insetData.
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, sooffset: undefinedandstride: undefinedare 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: 0also means "tightly packed" on WebGL but "every vertex reads the same element" on WebGPU. - "Ignored when
vertexCountis specified": the code checks truthiness, sovertexCount = 0still uses them. - The class-level
elements[].offset/elements[].stridedescriptions (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.
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
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:
After, the shared stride is the vertex size: