Buffer
Interface in Runtime
An array on the device of length elements T: a Buffer of structs, one range of the region laid out as a storage array with no size (Rule 6.8), made by zeros or array with a class; or a Buffer of scalars or vectors, made by array(xs) of a typed array, array or a kernel called on a resource.
import type { Buffer } from 'typeshade/runtime'
The signature, the description and the examples come from the compiler's own source at commit af54ddad5.
Syntax
interface Buffer<T> extends Disposable { readonly length: number; resize(length: number): void; set(data: BufferData<T>): void; at(index: number): BufferElement<T>; read(): Promise<BufferRead<T>>; needsUpdate: boolean; dispose(): void;}Description
Every Buffer has the same members (A11, Q1). ps.at(i).pos.set(x, y, z) writes one field of a
struct element, and xs.at(i).set(1.5) one scalar; await b.read() gives every element: each
an instance of the class for a struct, and one typed array of the scalar for a scalar or a
vector, the components one after another (A11, Q2 and Q5). From a call that writes it until
read() brings its values back, set, resize, needsUpdate and the accessors’ get and set
throw a TypeError. Freed by dispose() or when the garbage collector reclaims it, and by
[Symbol.dispose](), which the type declares as Disposable where the host’s lib declares
Symbol.dispose (Amendment A5, D4). A binding a shader module exports is a Buffer of structs
too (Rule 6.1): a storage<array<T, N>> keeps its N elements, and a storage<array<T>>, which
states no size, has no length until the host calls resize or set; until then length is 0,
and at and read throw a TypeError that names the binding. The view types an exported
binding without dispose() and [Symbol.dispose]() (D7).
Instance properties
lengthread onlynumberHow many elements it holds; a vector is one element (Amendment A11, Q5). An exported storage array with no size holds 0 until the host sets its length with
resizeorset.needsUpdatebooleanWhether the array holds a change not yet uploaded. Set it to
trueafter writing through a view thatgetreturned. A new array is dirty.
Instance methods
resize(length: number): voidGive the array
lengthelements (Amendment A3): the elements that fit keep their values, a new element is zero. An accessor thatatreturned before reads the array’s new range, and throws aTypeErrorfor an element that is gone.set(data: BufferData<T>): voidGive the array the length of
dataand its values (Amendment A3): for a struct, each element gives some of its fields, and the others are zero, asarray(Class, data)reads them; for a scalar or a vector, the numbers, the components one after another. Adatathat does not fit changes nothing.at(index: number): BufferElement<T>Element
index’s accessor: a struct’s fields, or a scalar’s or a vector’s element view withgetandset(Amendment A11, Q1). @throwsRangeErrorfor an index outside the array.read(): Promise<BufferRead<T>>Every element: each an instance of the class, built without its constructor, for a struct; one typed array of the scalar for a scalar or a vector (Amendment A11, Q2 and Q5).
dispose(): voidFree the array. A use of an accessor after it throws a
TypeError.
In the guide
See also
Source
src/runtime/resource.ts, line 617, at commit af54ddad5