The GPU executes commands after the CPU records them. A result such as the object under a cursor therefore reaches the CPU in a later . TypeGPU’s buffer.read() creates a , submit and promise per call. A readback reuses a ring of staging buffers, records copies in your frame’s encoder and maps them after submit.
Each copy reaches the CPU when its staging buffer maps
hz
Try Drag map takes and the bars stretch into more staging buffers, then lower staging to see captures skipped while every buffer maps. read() waits for the next copy.
Copies a buffer, or a range of its bytes, to the CPU without stalling, using a ring of staging
buffers. Records one copy per capture. Throws unless staging is an integer above 0, hz, when
set, is finite and above 0, and offset and size are multiples of 4 that fit the buffer.
@owns ― 3 buffers: staging buffers mapped in turn, as many as staging, each the size of the copy
@records ― capture after the pass that writes the buffer; a second capture in a frame does
nothing.
Also captures for latest this many times per second on average, in the frame closest to each
tick. Without a rate, only next() and request cause captures. Default none.
Records a copy when a next() is waiting, a request is pending or the capture rate is due. Maps
the copy after submission. Call after the pass that writes the buffer. A second call in the
same frame does nothing.
capture(
frame:frames.Frame
frame);// after it
constscheduler:coroutines.Coroutines
scheduler.
Coroutines.run(frame: frames.Frame): void
Resumes every coroutine whose wait is complete. Call while a frame is recording. Coroutine and
wait errors go to the coroutine's yield* or to onError, rather than escaping run.
run(
frame:frames.Frame
frame);
const
constpolled:number[]|undefined
polled=
constprobe:readback.Readback<number[]>
probe.
Readback<number[]>.take(): number[]|undefined
Returns latest once per completed copy, then undefined until the next copy completes. Each
value goes to one caller. Callers sharing a readback split the values between them.
take();// polled: each new value once, or undefined
Waits for a promise, such as a readback's next() or pipelines.compile. Resumes at the first
run after it settles, with its value or its error.
@param ― promise the promise to wait for
@returns ― a wait to yield*
until(
constprobe:readback.Readback<number[]>
probe.
Readback<number[]>.next(): Promise<number[]>
Returns a promise for the value from the next capture, and requests that capture. Promises
asked for before one capture share its copy. Rejects if mapping fails or the reader is
destroyed first. Waits indefinitely without a capture. In a coroutine, wait with
yield* coroutines.until(probe.next()).
capture(frame) copies the buffer, or its range, at that point in the command order. Place it after the pass that fills a counter, or before the passes when a picker must read the frame already on screen.
Busy buffers While every staging buffer is mapping, it is skipped and tried again next frame.
Cadence Record the pass that fills the buffer every frame and let hz pick the frames that copy; a pass that counts costs microseconds. Gating that pass with a clock of your own makes two that disagree, so a copy can land in a frame the pass skipped.
Once a frame A frame captures once; a second call does nothing.
Unsubmitted frames A frame that is never submitted hands its reads to the next capture.
Reads decode the whole schema into JavaScript values, one object per struct, so a large array of structs allocates thousands of objects per read. Pass bytes: true to skip decoding. decode then receives the copied bytes, which follow the schema’s WGSL layout with its padding. They are valid only during the call, so read them into arrays of your own. Without decode, reads return a copy as an ArrayBuffer, so a result of any other type needs a decode that makes it.
const
constCell:d.WgslStruct<{
level:d.F32;
source:d.U32;
}>
Cell=
import d
d.
struct<{
level:d.F32;
source:d.U32;
}>(props:{
level:d.F32;
source:d.U32;
}):d.WgslStruct<{
level:d.F32;
source:d.U32;
}>
export struct
Creates a struct schema that can be used to construct GPU buffers.
Ensures proper alignment and padding of properties (as opposed to a d.unstruct schema).
The order of members matches the passed in properties object.
Copies a buffer, or a range of its bytes, to the CPU without stalling, using a ring of staging
buffers. Records one copy per capture. Throws unless staging is an integer above 0, hz, when
set, is finite and above 0, and offset and size are multiples of 4 that fit the buffer.
@owns ― 3 buffers: staging buffers mapped in turn, as many as staging, each the size of the copy
@records ― capture after the pass that writes the buffer; a second capture in a frame does
nothing.
@param ― source the buffer it copies
@returns ― a reader you capture into and destroy
create(
constroot:TgpuRoot
root,
constcells:TgpuBuffer<d.WgslArray<d.WgslStruct<{
level:d.F32;
source:d.U32;
}>>>&StorageFlag
cells,{
BytesOptions<T= ArrayBuffer>.bytes: true
Reads the copied bytes without decoding them through the buffer's schema.
Converts the copied bytes to the value returned by next and latest, such as typed arrays of
your own. The bytes are valid only during the call, so copy what you keep. If it throws, reads
for that copy fail with the error. Required for a result other than an ArrayBuffer. Default a
copy of the bytes.
With bytes: true, offset and size copy only that range of the buffer, such as one level of a packed map or a run of . Both count bytes and are multiples of 4; size defaults to the rest of the buffer. Each staging buffer holds only the range, and decode receives only its bytes. A range needs bytes: true, since part of a buffer is not a value of its schema.
Copies a buffer, or a range of its bytes, to the CPU without stalling, using a ring of staging
buffers. Records one copy per capture. Throws unless staging is an integer above 0, hz, when
set, is finite and above 0, and offset and size are multiples of 4 that fit the buffer.
@owns ― 3 buffers: staging buffers mapped in turn, as many as staging, each the size of the copy
@records ― capture after the pass that writes the buffer; a second capture in a frame does
nothing.
Returns a promise for the value from the next capture, and requests that capture. Promises
asked for before one capture share its copy. Rejects if mapping fails or the reader is
destroyed first. Waits indefinitely without a capture. In a coroutine, wait with
yield* coroutines.until(probe.next()).
Call reset() after clearing a buffer or reloading a scene to discard earlier copies.
To restart counters, clear the buffer in the frame, before the passes that count, and reset(). Otherwise a copy made before the clear can land after it, and counters that take differences run backwards.
A late map never replaces a newer latest.
A promise that already resolved keeps its value, even if its coroutine resumes after a reset. Compare a held result with latest.
A ’s changes reach the GPU at the first submit after its flush. Compare latestFrame with that frame’s index when a value must be newer than a change.
Device loss rejects every map. Promises waiting on a copy reject with the map’s error, later captures also fail, and latest keeps its last value. Create the again on another root to recover.
A capture placed before the pass that fills the buffer reads the previous frame’s value, and nothing catches that.
A reads a fixed buffer. A varying list read once, on demand, needs code of its own.
frameA frame is one turn of the loop, and the object your function gets for it, which holds the encoder, the time and the time since the last frame.You own the frame →
staging bufferA staging buffer is a buffer the CPU can map, which a result is copied into so that it can be read after the frame.Words →
cadenceA cadence tells you on which frames work that runs less often than every frame is due.Cadences →
rowA row is one object's values in a buffer, such as one enemy, one particle or one light.Objects are rows in buffers →
tableA table is a buffer of rows with named fields, some written by the CPU and the others by kernels.Rows →
moduleA module is one piece of GPU work, made by a factory that borrows your root and returns resources, shader functions and methods that record into your frame.A module borrows →
readbackA readback copies a buffer during a frame and hands it to the CPU a frame or two later, without making the frame wait.Results back →