Skip to content

driver.view

Summary

driver.view creates a typed JavaScript alias over bytes in the WasmGPU runtime's current WebAssembly memory. Use it to read or write a known pointer range without copying. Use driver.viewOn when you need to pass the buffer explicitly.

Syntax

view<T extends WasmTypedArray>(ctor: WasmTypedArrayConstructor<T>, ptr: number, len: number): T
viewOn<T extends WasmTypedArray>(ctor: WasmTypedArrayConstructor<T>, buffer: ArrayBufferLike, ptr: number, len: number): T
const values = wgpu.driver.view(Float32Array, ptr, 4);

Parameters

Name Type Required Description
ctor WasmTypedArrayConstructor<T> Yes Typed-array constructor matching the memory's element representation.
buffer ArrayBufferLike For viewOn The buffer that contains the byte range.
ptr number Yes Nonnegative Wasm byte offset aligned to the element size.
len number Yes Number of typed elements.

Returns

T - A live alias. Typed-array construction throws for a misaligned or out-of-buffer range. The caller is responsible for the allocation's lifetime and for refreshing the view after memory growth replaces or detaches its buffer.

Example

const ptr = wgpu.frameArena.allocF32(3);
const origin = wgpu.driver.view(Float32Array, ptr, 3);
origin.set([0, 1, 0]);

See Also