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.
Access Mode
Section titled “Access Mode”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().
Element Count and Byte Length
Section titled “Element Count and Byte Length”Buffers have a fixed element count.
const values = new Buffer([1, 2, 3, 4], 'f32');
values.count; // 4values.stride; // bytes per elementvalues.byteLength; // count * strideUpdating Data
Section titled “Updating Data”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
Section titled “Labels”Labels help when inspecting GPU resources:
const positions = new Buffer([[0, 0, 0]], 'vec3f', 'rw', { label: 'particle positions'});