Skip to content

Buffer

Raw GPU buffer wrapping a typed scratch region for compute kernels. Use for things meshes + textures don't model: atomic counters, lookup tables (e.g. marching cubes' triangle / edge tables), per-particle state, scratch arrays between kernel passes.

Authors construct via Buffer.float32/uint32/uint8/atomic(...). The returned handle binds via mesh.runCompute(...)'s inputBuffers / outputBuffers maps and supports CPU readback via readSync() for things like "how many triangles did marching cubes emit this frame?".

Properties

byteLength

  • Type: number
  • Total byte length – length * elementSize.

elementType

  • Type: string
  • Element type as a scripting string ("float32", "uint32", "uint8", "atomic_uint").

id

  • Type: string
  • Stable handle id assigned at construction. Surfaces in the scripting manager's live-set; used by destroy() to release the experience-lifetime retention.

length

  • Type: number
  • Number of elements (not bytes).

Static Methods

atomic()

javascript
Buffer.atomic(initialValue?: number): Buffer | any

Allocate a single-element atomic-uint counter – common pattern for marching-cubes' triangle emitter, particle compaction, etc.

Example:

javascript
const triCounter = Buffer.atomic(0);

mesh.runCompute(kernel, {
    outputBuffers: { triCount: triCounter },
    threadGroups: [8, 8, 4]
});

const [emitted] = triCounter.readSync();
mesh.drawCount = emitted * 3;

Parameters:

  • initialValue (number) (optional) - Counter seed.

Returns: Buffer | any

float32()

javascript
Buffer.float32(length: number, initialValue?: number): Buffer | any

Allocate a Float32-typed GPU buffer.

Parameters:

  • length (number) - Element count (not bytes).
  • initialValue (number) (optional) - Optional seed value for all elements.

Returns: Buffer | any

uint32()

javascript
Buffer.uint32(length: number, initialValue?: number): Buffer | any

Allocate a Uint32-typed GPU buffer.

Parameters:

  • length (number) - Element count (not bytes).
  • initialValue (number) (optional) - Optional seed value for all elements.

Returns: Buffer | any

uint8()

javascript
Buffer.uint8(length: number, initialValue?: number): Buffer | any

Allocate a Uint8-typed GPU buffer.

Parameters:

  • length (number) - Element count (not bytes).
  • initialValue (number) (optional) - Optional seed value for all elements.

Returns: Buffer | any

Methods

destroy()

javascript
destroy(): void

Release the underlying GPU buffer immediately.

Buffers clean up automatically when the proxy goes out of scope. Call destroy() only for early termination in hot allocation / teardown loops where JSC's GC delay matters.

Returns: void

readSync()

javascript
readSync(): Float32Array | Uint32Array | Uint8Array

Read the buffer contents back to JS as a TypedArray. Triggers a command-buffer wait if a compute dispatch is in flight against the buffer – use sparingly (typical pattern: read atomic counters after dispatch to size a downstream draw call).

elementType. Nil if the buffer is detached.

Returns: Float32Array | Uint32Array | Uint8Array

writeSync()

javascript
writeSync(data: Float32Array | Uint32Array | Uint8Array): void

Write a TypedArray's contents into the buffer (CPU → GPU). Useful for seeding lookup tables, atomic counter resets, particle initial state. Length must match the buffer's declared element count; shorter writes leave the tail untouched.

elementType.

Parameters:

  • data (Float32Array | Uint32Array | Uint8Array) - matching

Returns: void