Skip to content

v.read()

Use v.read() when JavaScript needs GPU results.

const result = await v.read(buffer);

v.read() accepts:

| Target | Result | | -------------- | -------------------------------------------------- | | Buffer | Typed array when possible, otherwise ArrayBuffer | | RawBuffer | ArrayBuffer | | Node | Record of declared outputs | | Handle | Result from the concrete buffer behind the handle | | ReadTarget[] | Array of results |

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

Primitive Buffer types return matching typed arrays when Volten can infer one.

Structured buffers return ArrayBuffer; use unpack() to decode them.

Reading a node returns only declared outputs.

const computeResult = kernel({
shader: `...`,
outputs: ['result']
});
const node = computeResult({ input, result });
v.run(node);
const outputs = await v.read(node);
// { result: Float32Array [...] }

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

const data = new Buffer([1, 2, 3, 4], 'f32', 'rw');
const scale = kernel({
shader: `
fn main(gid: vec3u) {
data[gid.x] = data[gid.x] * 2.0;
}
`
});
const node = scale({ data });
v.run(node);
const values = await v.read(node.data);
// Float32Array [2, 4, 6, 8]
const [a, b] = await v.read([bufferA, nodeB.output]);

Volten deduplicates concrete buffers internally, so reading the same backing buffer through multiple handles does not copy it more than once.