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.
Reading a Buffer
Section titled “Reading a Buffer”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);// Float32ArrayStructured 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 }// ]Reading a RawBuffer
Section titled “Reading a RawBuffer”RawBuffer returns an ArrayBuffer.
const bytes = await v.read(rawBuffer);Reading a Node
Section titled “Reading a Node”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.
Reading Multiple Targets
Section titled “Reading Multiple Targets”Pass an array to read multiple targets at once:
const [a, b] = await v.read([bufferA, bufferB]);