Skip to content

compute.readback.readIntoWasmMemory

Summary

compute.readback.readIntoWasmMemory copies a GPU byte range into an allocation in WebAssembly linear memory. The staging copy is submitted at invocation. The destination is resolved against the current memory buffer when the copy completes.

Syntax

readIntoWasmMemory(mem: WebAssembly.Memory, dstPtrBytes: number, src: ReadbackSource, srcOffsetBytes?: number, sizeBytes?: number, opts?: { label?: string }): Promise<void>
await wgpu.compute.readback.readIntoWasmMemory(memory, 16, storage);

Parameters

Name Type Required Description
mem WebAssembly.Memory Yes Destination linear memory.
dstPtrBytes number Yes Non-negative integer destination byte address.
src GPUBuffer | StorageBuffer Yes Source with COPY_SRC usage.
srcOffsetBytes number No Four-byte-aligned offset relative to the source range. Defaults to 0.
sizeBytes number No Requested bytes. Defaults to the bytes remaining in the source.
opts { label?: string } No Optional GPU debug label.

Returns

Promise<void> - Resolves after the bytes are written into memory. Zero bytes perform no write. A non-empty destination must fit in the current memory, and the source follows the same range checks as read.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const memory = new WebAssembly.Memory({ initial: 1 });
const storage = wgpu.compute.createStorageBuffer({ data: new Uint32Array([12, 34]), copySrc: true });

await wgpu.compute.readback.readIntoWasmMemory(memory, 16, storage, 0, 8);
console.log(new Uint32Array(memory.buffer, 16, 2)); // [12, 34]
storage.destroy();

Notes

Submit any source-writing GPU commands before calling this method. Keep the destination allocation live until the promise resolves. Build views from mem.buffer after the await so they refer to the current buffer if memory grew.

See Also