brendan-duncan / wgsl_reflect

A WebGPU Shading Language parser and reflection library for Javascript.
MIT License
184 stars 20 forks source link

1.0.13 crashes on some shaders #62

Closed robey closed 1 day ago

robey commented 1 day ago

I don't actually know what's wrong, but my shader can be parsed with versions 1.0.7 to 1.0.12 (inclusive), but crashes when trying to parse with 1.0.13. I'm including the shader (from a webgpu tutorial) and the stack trace.

shader:

  const shader = `
    struct Triangle {
      color: vec4f,
      offset: vec2f,
    };

    struct View {
      scale: vec2f,
    };

    struct VertexData {
      @builtin(position) position: vec4f,
      @location(0) color: vec4f,
    }

    @group(0) @binding(0) var<storage, read> triangles: array<Triangle>;
    @group(0) @binding(1) var<storage, read> views: array<View>;

    @vertex fn vs(
      @builtin(vertex_index) vertexIndex : u32,
      @builtin(instance_index) instanceIndex: u32,
    ) -> VertexData {
      let pos = array(
        vec2f( 0.0,  0.366),  // top center: (√3/2)-0.5
        vec2f(-0.5, -0.5),  // bottom left
        vec2f( 0.5, -0.5)   // bottom right
      );

      let triangle = triangles[instanceIndex];
      let view = views[instanceIndex];

      var ret: VertexData;
      ret.position = vec4f(
        pos[vertexIndex] * view.scale + triangle.offset, 0.0, 1.0);
      ret.color = triangle.color;
      return ret;
    }

    @fragment fn fs(vertex: VertexData) -> @location(0) vec4f {
      return vertex.color;
    }
  `;

stack trace:

TypeError: can't access property "name", e is null
    _getTypeInfo wgsl_reflect.module.js:3861
    _getTypeInfo wgsl_reflect.module.js:3819
    _markStructsFromAST wgsl_reflect.module.js:3583
    _findResources wgsl_reflect.module.js:3609
    search wgsl_reflect.module.js:676
    search wgsl_reflect.module.js:224
    searchBlock wgsl_reflect.module.js:36
    search wgsl_reflect.module.js:81
    _findResources wgsl_reflect.module.js:3590
    update wgsl_reflect.module.js:3465
    ze wgsl_reflect.module.js:3372
    parseShaderVariables struct2.ts:13

and here's the shortest code that crashes:

export function parseShaderVariables(shader: string) {
  const reflect = new WgslReflect(shader);
  console.log(reflect);
}

Sorry this isn't more helpful, but if you can think of other things you'd like me to try, I'm happy to.

This library looks extremely cool. :)

brendan-duncan commented 1 day ago

Sorry about that. Wasn't handling arrays with implicit format correctly. Fixed in 1.0.14.

robey commented 1 day ago

Can confirm it's fixed! Thanks for the speedy reply! :)