Skip to content

Reading Data

Use v.read() when JavaScript needs the result of GPU work.

const result = await v.read(buffer);

v.read() accepts a Buffer, RawBuffer, Handle, Node, or an array of read targets.

Primitive buffers return typed arrays when Volten can infer the right type:

const values = new Buffer([1, 2, 3, 4], 'f32', 'rw');
v.run(node);
const result = await v.read(values);
// Float32Array

Structured buffers return an ArrayBuffer. Decode it with unpack().

import { unpack } from '@volten/core';
const raw = await v.read(particles);
const decoded = unpack(raw, Particle);
console.log(decoded);
// [
// { position: [0, 0, 0], velocity: [1, 0, 0], mass: 1 },
// { position: [1, 0, 0], velocity: [0, 1, 0], mass: 2 }
// ]

RawBuffer returns an ArrayBuffer.

const bytes = await v.read(rawBuffer);

Reading a node returns a record of its declared outputs.

const kernel = new Kernel(
`
fn main(gid: vec3u) {
result[gid.x] = input[gid.x] * 2.0;
}
`,
{
outputs: ['result']
}
);
const node = v.pass(kernel, { input, result });
v.run(node);
const outputs = await v.read(node);
// { result: Float32Array }

If a kernel has no declared outputs, read the concrete buffer directly.

Pass an array to read multiple targets at once:

const [a, b] = await v.read([bufferA, bufferB]);