wgpu-native icon indicating copy to clipboard operation
wgpu-native copied to clipboard

Handle WGPUVertexStepMode_Undefined

Open AlbinBernhardssonARM opened this issue 8 months ago • 2 comments

stepMode defaults to "vertex". https://www.w3.org/TR/webgpu/#dom-gpuvertexbufferlayout-stepmode

AlbinBernhardssonARM avatar Jun 10 '25 11:06 AlbinBernhardssonARM

@cwfitzgerald good to take that into account!

However, it's not clear to me how these different cases of "no attributes" should be mapped to the API in wgpu-core. And it looks like null is not allowed in the JS API?

almarklein avatar Jun 30 '25 07:06 almarklein

@cwfitzgerald good to take that into account!

However, it's not clear to me how these different cases of "no attributes" should be mapped to the API in wgpu-core. And it looks like null is not allowed in the JS API?

null is allowed in GPUVertexState.buffers. However, Firefox doesn't appear to handle this quite correctly. null entries are supposed to be skipped during vertex processing and validating draw calls. But the following fails in Firefox (works in Chrome) due to no vertex buffer being bound in slot 1:

let pipeline = device.createRenderPipeline({
  vertex: {
    buffers: [
      {
        // Vertex buffer slot 0...
      },
      null
    ],
    ...
  }
  ...
});

...

renderPassEncoder.setPipeline(pipeline);
renderPassEncoder.setVertexBuffer(0, vertexBuffer);
renderPassEncoder.draw();

Can be worked around by binding some dummy vertex buffer to slot 1.

The reason is wgpu-core doesn't handle null entries in VertexState.buffers. Firefox handles it by simply putting default values on null entries. However a null entry is not quite the same as an entry with attributes = []. An entry with attributes = [] still has to have a bound vertex buffer, based on the outcome of https://github.com/gpuweb/gpuweb/issues/4999 .

So I think the proper, long-term solution is to handle null entries in wgpu-core. Short term solution for wgpu-native is either

  1. Match Firefox behavior and map WGPUVertexStepMode_Undefined, empty attributes or WGPUVertexStepMode_VertexBufferNotUsed as default values { array_stride: 0, step_mode: Vertex, attributes: []}.
  2. Raise an error. Which we already do since we don't handle WGPUVertexStepMode_Undefined or WGPUVertexStepMode_VertexBufferNotUsed. But we could handle WGPUVertexStepMode_Undefined for the non-empty attributes case. And i.m.o. current error message is not too helpful as it implies the wrong vertex step mode was used, when in fact the user did nothing wrong.

I've updated my commit to do 2 with an error message telling developers how they can work around the lack of support, but I can change it to 1 if we prefer.

Note: WGPUVertexStepMode_VertexBufferNotUsed will be removed next time we update webgpu-headers.

AlbinBernhardssonARM avatar Jul 02 '25 15:07 AlbinBernhardssonARM