compute.ndarray.wrap¶
Summary¶
compute.ndarray.wrap creates a GPU ndarray over an existing StorageBuffer on the compute device. The returned array borrows the storage, so changes through aliases can affect its values.
Syntax¶
wrap(buffer: StorageBuffer, dtype: DType, layout: NdLayoutDescriptor, baseOffsetBytes?: number): GPUndarray
const a = wgpu.compute.ndarray.wrap(storage, "f32", { shape: [4] });
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
buffer |
StorageBuffer |
Yes | Live storage wrapper on the same device. Use compute.wrapStorageBuffer first for a raw GPUBuffer. |
dtype |
DType |
Yes | Element type matching the stored bytes. |
layout |
NdLayoutDescriptor |
Yes | Shape, optional strides and optional offset. The entire backing extent must fit the buffer range. |
baseOffsetBytes |
number |
No | Four-byte-aligned non-negative safe integer offset within the wrapper, default 0. |
Returns¶
GPUndarray - Borrowed storage view. Destroying it invalidates the array. The caller keeps the storage live and destroys it separately.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const compute = wgpu.compute;
const storage = compute.createStorageBuffer({ data: new Float32Array([4, 8]), copySrc: true });
const gpu = compute.ndarray.wrap(storage, "f32", { shape: [2] });
const cpu = await compute.ndarray.toCPU(gpu);
console.log(cpu.data());
cpu.destroy();
gpu.destroy();
storage.destroy();
Notes¶
The first logical element starts at buffer.offsetBytes + baseOffsetBytes + layout.offsetBytes in the raw GPU buffer, using zero for an omitted layout offset. Direct bindings impose additional device alignment on the backing range's start.