Skip to content

bug: Interaction of different structs with the same fields #2450

@iwoplaza

Description

@iwoplaza

I always knew interactions between different structs with the same fields wasn't looked at till now, but I didn't expect the behavior to be this strange, haha.

  it('copies?', () => {
    const Boid = d.struct({
      pos: d.vec3f,
      vel: d.vec3f,
    });

    // A different struct, but with the same properties
    const Bird = d.struct({
      pos: d.vec3f,
      vel: d.vec3f,
    });

    function main() {
      'use gpu';
      const boid = Boid();
      const bird = Bird(boid); // doing an explicit copy, works on the type level
      return bird;
    }

    expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
      "struct Boid {
        pos: vec3f,
        vel: vec3f,
      }

      struct Bird {
        pos: vec3f,
        vel: vec3f,
      }

      fn main() -> Bird {
        var boid = Boid();
        var bird = boid;
        return bird;
      }"
    `);
  });

It seems like when we try to explicitly copy the object boid of type Boid as a new object of type Bird that happens to have the same properties, we just don't handle it and emit boid with the type Bird, where in WGSL it's still going to be of type Boid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions