Skip to content

feat: Expose lower-level schema read and write APIs #2410

@iwoplaza

Description

@iwoplaza

Defining schemas is core to TypeGPU, and so far, we can do the following with schemas:

  • Query their size (d.sizeOf(...))
  • Query their alignment (d.alignmentOf(...))
  • Plug them into a buffer or shorthand (root.createBuffer(...))
  • Plug them into shader definitions like variables and constants (tgpu.privateVar, tgpu.const)
  • Use them in shells to constrict input types
  • (among many more)

There is one functionality that I feel like is needlessly hiding beneath our buffer.write and buffer.read APIs, and that is using a schema to write our data instance objects (d.v3f, number, etc...) into an ArrayBuffer provided by the user.

import { d, writeToArrayBuffer, readFromArrayBuffer } from 'typegpu';

const Boid = d.struct({
  pos: d.vec3f,
  vel: d.vec3f,
});

const buffer = new ArrayBuffer(d.sizeOf(Boid));
writeToArrayBuffer(buffer, Boid, { pos: [1, 2, 3], vel: [0, 1, 0] }, /* { offset: ... } */);
const boid = readFromArrayBuffer(buffer, Boid);

My main use case right now adding the same functionality we have for our buffers to the WebGL fallback, but I can imagine this API being useful beyond that of course.

A lower-level patch utility would also be pretty useful:

import { d, patchArrayBuffer } from 'typegpu';

const Boid = d.struct({
  pos: d.vec3f,
  vel: d.vec3f,
});

const BoidsArray = d.arrayOf(Boid, 10);
const buffer = new ArrayBuffer(d.sizeOf(BoidsArray));
patchArrayBuffer(buffer, BoidsArray, { 5: { pos: [1, 2, 3], vel: [0, 1, 0] } });

Metadata

Metadata

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