WebGPU compute readback: flags, sync, and a minimal pattern
Reading compute results from WebGPU to the CPU requires exact buffer flags and synchronization. This post shows the minimal pattern to avoid garbage data and frame stalls across browsers.
11 Apr 2026, 22:31 UTC

You just ran a compute dispatch and need the results on the CPU
Without the right buffer flags and synchronization, you'll either get empty data, a thrown exception, or a frame-time stall that makes the rest of your app janky. The pattern that works: create a dedicated readback buffer with MAP_READ | COPY_DST, copy your compute results into it, submit the queue, wait for GPU completion, then call mapAsync—and always unmap when you're done.
Buffer usage flags—what actually gets copied
WebGPU buffers are created with GPUDevice.createBuffer using usage flags. A compute storage buffer that writes results needs GPUBufferUsage.STORAGE on the write side, but the CPU cannot map it directly. You need a separate buffer with GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST that receives a copyBufferToBuffer command from the storage buffer.
Why COPY_SRC and MAP_READ matter
The buffer that stores compute output must have GPUBufferUsage.COPY_SRC so a copy operation can transfer its contents. The readback buffer needs GPUBufferUsage.MAP_READ to allow mapAsync and GPUMapMode.READ. Omitting either flag causes the creation call to fail or the subsequent read to return uninitialized data.
The mapAsync lifecycle and why submit + onSubmittedWorkDone matters
mapAsync resolves only after the GPU finishes pending work touching the buffer. If you call it right after a dispatch without submitting the queue, the promise may never resolve or return data the GPU hasn't written yet. The typical pattern is: encode commands, call queue.submit, then await queue.onSubmittedWorkDone(), then call buffer.mapAsync(GPUMapMode.READ). When the promise resolves, buffer.getMappedRange() gives you an ArrayBuffer that you must release with buffer.unmap().
A minimal readback pattern (illustrative)
// run inside a secure context (https) where navigator.gpu exists
const device = await navigator.gpu.requestAdapter().then(adapter => adapter.requestDevice());
// compute buffer with storage usage
const computeBuffer = device.createBuffer({
size: 4 * 1024, // example: 1024 vec4 values
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
});
// readback buffer with map and copy flags
const readbackBuffer = device.createBuffer({
size: 4 * 1024,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
// after compute pass encoding, copy results
const encoder = device.createCommandEncoder();
encoder.copyBufferToBuffer(computeBuffer, 0, readbackBuffer, 0, computeBuffer.size);
// submit and wait for GPU completion
const commandBuffer = encoder.finish();
queue.submit([commandBuffer]);
await queue.onSubmittedWorkDone();
// now map async – this will resolve after the copy is committed
await readbackBuffer.mapAsync(GPUMapMode.READ);
// read the data once the promise resolves
if (readbackBuffer.getMappedRange().byteLength > 0) {
const data = new Float32Array(readbackBuffer.getMappedRange());
// ... use data ...
readbackBuffer.unmap();
}
- Expected check: after the above sequence, the
Float32Arrayshould contain the pattern your compute shader wrote; if it doesn't, verify that@workgroup_sizein WGSL matchesdispatchWorkgroupsdimensions - Relevant risk: mapping before
onSubmittedWorkDonemay return empty or garbage data, and forgettingunmap()keeps the buffer mapped, blocking later GPU operations or remapping attempts
Trade-offs, limitations, and browser reality
Mapping every frame can stall the GPU and CPU, causing frame-time jitter. A common mitigation is double-buffering readback buffers: create two readback buffers and swap which one you copy into and map each frame. This lets the GPU work on one while the CPU reads from the other, reducing synchronization overhead.
Cross-browser support varies. WebGPU reached stable status in Chrome 113 and is available in Safari, while Firefox rollout depends on version and platform. Always feature-detect before entering a WebGPU code path:
if (navigator.gpu === undefined) {
console.error('WebGPU not supported in this browser');
// fall back to Canvas2D or report unsupported
Additionally, WGSL compute entry points require @workgroup_size to match the dimensions of dispatchWorkgroups; mismatched sizes cause validation errors at pipeline creation time. Use the WebGPU validation layer in DevTools to catch invalid buffer usage flag combinations and shader mismatches early.
Closing action
Start by feature-detecting, create your storage and readback buffers with the correct usage flags, and only map after you've submitted the queue and waited for onSubmittedWorkDone. If you're reading back every frame, consider double-buffering readback buffers to avoid per-frame stalls. Your CPU—and your users—will thank you.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.