compute.readback.snapshot¶
Summary¶
For a non-empty range, compute.readback.snapshot submits a staging copy when called and passes the mapped bytes to a callback. Use it when you can decode or copy the data synchronously before the staging buffer is unmapped.
Syntax¶
snapshot(src: ReadbackSource, srcOffsetBytes: number, sizeBytes: number, consume: (bytes: Uint8Array) => void, opts?: { label?: string }): Promise<void>
await wgpu.compute.readback.snapshot(storage, 0, 4, bytes => console.log(bytes[0]));
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
src |
GPUBuffer | StorageBuffer |
Yes | Source buffer with COPY_SRC usage. |
srcOffsetBytes |
number |
Yes | Four-byte-aligned offset relative to the source's logical start. |
sizeBytes |
number |
Yes | Non-negative integer byte count within the logical source range. The copy length rounded up to four bytes must fit the GPU allocation. |
consume |
(bytes: Uint8Array) => void |
Yes | Synchronous consumer of a view with exactly sizeBytes bytes. |
opts |
{ label?: string } |
No | Optional GPU debug label. |
Returns¶
Promise<void> - Resolves after the consumer runs and the mapping is released. A zero-length snapshot skips the consumer. Invalid input can throw before a promise is returned. A mapping or callback error rejects the promise.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const storage = wgpu.compute.createStorageBuffer({ data: new Float32Array([2.5]), copySrc: true });
await wgpu.compute.readback.snapshot(storage, 0, 4, bytes => {
console.log(new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getFloat32(0, true));
});
storage.destroy();
Notes¶
Submit any encoder that writes the source before calling snapshot. Later writes are ordered after the staging copy, even when the ring is busy. Copy the bytes inside consume if you need to keep them.