Skip to content

Structs and Arrays

Use struct() and array() when your buffer elements are more than plain numbers or vectors.

import { Buffer, struct } from '@volten/core';
const Particle = struct('Particle', {
position: 'vec3f',
velocity: 'vec3f',
mass: 'f32'
});
const particles = new Buffer(
[
{ position: [0, 0, 0], velocity: [1, 0, 0], mass: 1 },
{ position: [1, 0, 0], velocity: [0, 1, 0], mass: 2 }
],
Particle,
'rw'
);

The struct name is used for generated WGSL type declarations.

Struct fields can reference other struct schemas:

const Aabb = struct('Aabb', {
min: 'vec3f',
max: 'vec3f'
});
const Particle = struct('Particle', {
position: 'vec3f',
velocity: 'vec3f',
bounds: Aabb
});

Use array() for fixed-size arrays inside a schema:

import { array, struct } from '@volten/core';
const Cell = struct('Cell', {
neighbors: array('vec3u', 8),
temperature: 'f32'
});

Arrays can also be nested:

const Tile = struct('Tile', {
samples: array(array('f32', 4), 4)
});

For layouts Volten cannot describe directly yet, use RawBuffer and provide pre-packed bytes.