Skip to content

GPUndarray.wrap

Summary

GPUndarray.wrap creates ndarray metadata over an existing StorageBuffer. No allocation or copy happens; the wrapped buffer is treated as backing storage. Use this to reinterpret existing buffers as typed multidimensional arrays. baseOffsetBytes lets you place the ndarray view inside a larger shared buffer. It defaults to zero and must be a non-negative safe integer and a multiple of four. The ndarray's backing extent must fit within the wrapper. Direct binding also requires a device-aligned offset and a valid rounded size.

Syntax

GPUndarray.wrap(buffer: StorageBuffer, dtype: DType, layout: NdLayoutDescriptor, baseOffsetBytes?: number): GPUndarray
const g = GPUndarray.wrap(buffer, dtype, layout, baseOffsetBytes);

Parameters

Name Type Required Description
buffer StorageBuffer Yes Existing storage buffer to wrap.
dtype DType Yes Element data type for interpretation.
layout NdLayoutDescriptor Yes Shape plus optional strides/offset metadata.
baseOffsetBytes number No Byte offset into buffer where ndarray backing begins.

Returns

GPUndarray - GPU array over the provided storage. Destroy the array and the borrowed storage separately.

Example

import WasmGPU, { GPUndarray } from "@zushah/wasmgpu";

const canvas = document.querySelector("canvas");
const wgpu = await WasmGPU.create(canvas);

const buffer = wgpu.compute.createStorageBuffer({ byteLength: 4096, copySrc: true, copyDst: true });
const g = GPUndarray.wrap(buffer, "u32", { shape: [256] }, 0);

console.log(g.bindingResource());
g.destroy();
buffer.destroy();

See Also