Skip to content

Buffers

Use Buffer when a kernel needs an array of values.

import { Buffer } from '@volten/core';
const values = new Buffer([1, 2, 3, 4], 'f32');
const output = new Buffer([0, 0, 0, 0], 'f32', 'rw');

The second argument is the element type. Volten uses it to pack the data and generate the shader binding.

The third argument controls shader access:

const input = new Buffer([1, 2, 3, 4], 'f32', 'r');
const inout = new Buffer([1, 2, 3, 4], 'f32', 'rw');

'r' generates a read-only storage binding. 'rw' generates a read-write storage binding.

If omitted, the access mode defaults to 'rw'.

This is shader access, not CPU access. JavaScript can still upload data with set() or update().

Buffers have a fixed element count.

const values = new Buffer([1, 2, 3, 4], 'f32');
values.count; // 4
values.stride; // bytes per element
values.byteLength; // count * stride

Use set() to replace the full contents without changing the element count:

values.set([10, 20, 30, 40]);

Use update() to replace a contiguous range. The offset is measured in elements:

values.update([99, 100], 1);

Labels help when inspecting GPU resources:

const positions = new Buffer([[0, 0, 0]], 'vec3f', 'rw', {
label: 'particle positions'
});