Buffers and memory
Choose a buffer’s usage flags for its entire lifetime. Give the GPU enough space for the data you will reuse, upload only the ranges that change, and keep intermediate results on the device until the CPU needs them.
Allocate for the durable contract​
Choose a resource from its long-lived requirements:
- byte length or texture dimensions;
- format and element layout;
- every binding, copy, render, or storage usage it will need;
- update frequency and expected lifetime;
- device size and binding limits.
Reuse the allocation for contents-only changes. Recreate it when the structural contract changes, such as a larger capacity, different format, or missing usage.
Upload data​
const positions = device.createBuffer({
data: new Float32Array([0, 0, 1, 0, 0, 1]),
usage: Buffer.VERTEX | Buffer.COPY_DST
});
positions.write(nextPositions);
Declare every later use at creation. The owner eventually calls positions.destroy().
Upload without stalling the frame​
Small or infrequent updates can use resource write helpers. When an upload must be ordered with other GPU operations, encode it with the same command sequence. Large streaming workloads should use bounded batches so JavaScript preparation, temporary allocations, and queue work do not arrive as one large spike.
Avoid keeping unnecessary full-size CPU and GPU copies indefinitely. Preserve CPU data when the application needs recovery, editing, or another consumer; otherwise release staging data after a safe upload boundary.
Keep intermediate results GPU-resident​
If one GPU operation produces data for another, bind or copy that result directly on the GPU. Reading the value into JavaScript merely to decide a following dispatch or draw introduces an avoidable round trip. WebGPU indirect dispatch and drawing are particularly useful for consuming GPU-produced counts without CPU synchronization.
Storage and copies​
A WebGPU storage buffer can hold data for many shader invocations. Declare Buffer.STORAGE at creation and include any copy or vertex usage required by later passes. A compute pass can write a result that a subsequent pass reads on the same device. For its WGSL declaration and element stride, see Layouts and shader bindings.
Use a command encoder when a buffer copy must follow an earlier GPU write in the same ordered workload. Both source and destination need the appropriate copy usage. Check the device limits before allocating large buffers or binding a subrange.
Read back deliberately​
Readback has both latency and ordering cost. The CPU cannot receive a valid result until all GPU work that writes the requested range has completed.
- Prefer asynchronous reads.
- Read compact summaries rather than source-sized buffers.
- Allow only a bounded number of readbacks in flight.
- Associate each result with a generation so stale asynchronous output cannot overwrite newer state.
- Do not block a render path merely to update optional telemetry.
WebGL 2 readback is more constrained and may perform a synchronous GPU read internally even when luma.gl exposes an asynchronous-compatible surface. Design performance-sensitive WebGL paths with that limitation in mind.
const bytes = await resultBuffer.readAsync(0, Uint32Array.BYTES_PER_ELEMENT);
const result = new Uint32Array(bytes.buffer, bytes.byteOffset, 1)[0];
Common mistakes​
- Assuming integrated GPU memory can be read by JavaScript without synchronization.
- Reallocating whole resources for small updates.
- Reading back source-sized data every frame.
- Allowing stale asynchronous readbacks to publish after the view or dataset changed.
- Ignoring
maxBufferSize, binding-size, row-alignment, or texture-dimension limits.
See the Buffer API for individual update, copy, mapping, and readback methods.