Skip to content

fix: Ensure VertexBufferLayout matches parsed WGSL shader @location layout #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Kaapp
Copy link
Collaborator

@Kaapp Kaapp commented Apr 12, 2025

For deck.gl tracker, and to fix issue raised in visgl/deck.gl#9531

Background

When I originally ported the pointcloud layer, the attribute struct had been set up like the following:

struct Attributes {
  @builtin(instance_index) instanceIndex : u32,
  @builtin(vertex_index) vertexIndex : u32,
  @location(0) positions: vec3<f32>,
  @location(1) instanceNormals: vec3<f32>,
  @location(2) instanceColors: vec4<f32>,
  @location(3) instancePositions: vec3<f32>,
  @location(4) instancePositions64Low: vec3<f32>,
  @location(5) instancePickingColors: vec3<f32>
};

which results, perhaps confusingly, in the following error, as instanceColors are provided as a unorm8x4 and should have stride 4, not 24.
image

If, instead, that struct is ordered in the following way (note: instancePositions have been moved down), it works as expected:

struct Attributes {
  @builtin(instance_index) instanceIndex : u32,
  @builtin(vertex_index) vertexIndex : u32,
  @location(0) positions: vec3<f32>,
  @location(1) instancePositions: vec3<f32>,
  @location(2) instancePositions64Low: vec3<f32>,
  @location(3) instanceNormals: vec3<f32>,
  @location(4) instanceColors: vec4<f32>,
  @location(5) instancePickingColors: vec3<f32>
};

After some digging, it turns out that error is being thrown because the VertexBufferLayout we pass to the WebGPURenderPipeline contained a different order of attributes than those being bound to the shader by setVertexBuffer. In some cases that generates this error. In other cases, where the buffer sizes all match it subtly binds the buffers with data meant for other buffers. In my case, I got 'lucky' that I re-ordered the buffers into the same order as they were specified by deck's AttributeManager.

Instead, ensure that we produce a VertexBufferLayout that matches the order of the attributes from the WGSL source code by sorting the bufferLayout. This means that both WGSL samples work as expected.

@Kaapp Kaapp requested review from felixpalmer and ibgreen April 12, 2025 14:11
@Kaapp Kaapp self-assigned this Apr 12, 2025
@Kaapp Kaapp mentioned this pull request Apr 12, 2025
20 tasks
@Kaapp Kaapp marked this pull request as draft April 12, 2025 15:46
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