Skip to content

feat: Second argument to .as for reinterpretation #2413

@iwoplaza

Description

@iwoplaza

This is equivalent in function to creating a new typed buffer and plugging in an existing GPUBuffer, but we can validate that the size of the schemas matches. Also, it's pretty cool.

const size = 4;

const array2d = <T extends AnyWgslData>(element: T, w: number, h: number) =>
  d.arrayOf(d.arrayOf(d.vec2h, w), h);

const displacementBuffer = root.createBuffer(array2d(d.vec2h, size, size)).$usage('storage');

const regularView = noiseBuffer.as('mutable');
const packedView = noiseBuffer.as('mutable', array2d(d.u32, size, size));

We could also go for a .reinterpret API, which would nicely extend to fixed usages:

const size = 4;

const array2d = <T extends AnyWgslData>(element: T, w: number, h: number) =>
  d.arrayOf(d.arrayOf(d.vec2h, w), h);

const displacementMutable = root.createMutable(array2d(d.vec2h, size, size));
const displacementPackedMutable = displacementMutable.reinterpret(array2d(d.u32, size, size));

To be discussed before attempting to implement

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions