WasmSimd

clay.simd.wasm.WasmSimd (Class)

Loader and memory manager for the wasm emission kernels (web target).

The kernels are embedded as base64 (see kernel.c / build.sh) and instantiated asynchronously at startup: until ready is true, callers fall back to the plain js batched path. Feature detection picks the SIMD build when the engine supports wasm SIMD (all evergreen browsers since 2021, Safari 16.4+), the scalar wasm build otherwise.

Memory model: this class owns the module's linear memory and hands out regions through a simple bump allocator (alloc). The GL batcher allocates its staging buffers as views over this memory, so the kernels write in place and gl.bufferData(view) uploads with zero extra copies. Per-part source copies (vertices/colors/uvs/indices into the scratch regions) are done with typed-array set where possible, which is nearly free browser-side.

Growing the memory detaches every existing view: memoryGeneration is bumped on growth and view holders (the batcher, the scratch views here) must re-derive their views from their stored offsets when it changes.

Static Members

ready: Bool

True once the module is instantiated and usable.


usingSimd: Bool

True when the SIMD build of the kernels is in use.


memoryGeneration: Int

Bumped every time the wasm memory grows (views must be re-derived).


memoryBuffer: js.lib.ArrayBuffer

The wasm memory's ArrayBuffer (compare with a view's buffer to know if it is wasm-backed).


vertsScratch: js.lib.Float32Array

Scratch views (valid after requireScratch, until the next memory growth).


colorsScratch: js.lib.Float32Array

packedScratch: js.lib.Uint32Array


indicesScratch: js.lib.Int32Array

vertsScratchByteOffset: Int

Scratch byte offsets (stable across growth).


colorsScratchByteOffset: Int

uvsScratchByteOffset: Int

indicesScratchByteOffset: Int

init(): Void

Starts loading the kernels. Safe to call multiple times.


alloc(bytes: Int): Int

Allocates bytes from the module memory (16-byte aligned). Returns the byte offset, or -1 if the memory limit was reached (in which case the whole wasm path is permanently disabled and callers keep using the plain js path). Regions are never freed.

Name Type
bytes Int
Returns
Int

requireScratch(numVertFloats: Int, numColorFloats: Int, numUvFloats: Int, numIndices: Int): Bool

Scratch source regions for one mesh part, sized for at least numVertFloats position floats, numColorFloats color floats, numUvFloats uv floats and numIndices indices. Views are reused across parts within a frame and re-derived after memory growth.

Name Type
numVertFloats Int
numColorFloats Int
numUvFloats Int
numIndices Int
Returns
Bool

meshPartF32(posByteOffset: Int, posStride: Int, colByteOffset: Int, uvByteOffset: Int, idxByteOffset: Int, idxBase: Int, vertsByteOffset: Int, vertStride: Int, floatColorsByteOffset: Int, packedColorsByteOffset: Int, uvsByteOffset: Int, indicesByteOffset: Int, start: Int, end: Int, a: Float, b: Float, c: Float, d: Float, tx: Float, ty: Float, z: Float, textureSlot: Float, writeSlot: Bool, colorMode: Int, singleR: Float, singleG: Float, singleB: Float, singleA: Float, globalAlpha: Float, premultiply: Bool, zeroAlpha: Bool, hasUvs: Bool, ufx: Float, ufy: Float, srcAttrCount: Int, dstAttrCount: Int): Void

Runs the fused mesh-part kernel. All offsets are byte offsets into the module memory; see kernel.c for parameter semantics.

Name Type
posByteOffset Int
posStride Int
colByteOffset Int
uvByteOffset Int
idxByteOffset Int
idxBase Int
vertsByteOffset Int
vertStride Int
floatColorsByteOffset Int
packedColorsByteOffset Int
uvsByteOffset Int
indicesByteOffset Int
start Int
end Int
a Float
b Float
c Float
d Float
tx Float
ty Float
z Float
textureSlot Float
writeSlot Bool
colorMode Int
singleR Float
singleG Float
singleB Float
singleA Float
globalAlpha Float
premultiply Bool
zeroAlpha Bool
hasUvs Bool
ufx Float
ufy Float
srcAttrCount Int
dstAttrCount Int

Private Members

memory: Dynamic

meshPartF32Fn: Dynamic

bumpOffset: Int

memoryBytes: Int

vertsScratchOffset: Int

vertsScratchFloats: Int

colorsScratchOffset: Int

colorsScratchFloats: Int

uvsScratchOffset: Int

uvsScratchFloats: Int

indicesScratchOffset: Int

indicesScratchInts: Int

vertsScratchView: js.lib.Float32Array

colorsScratchView: js.lib.Float32Array

packedScratchView: js.lib.Uint32Array

uvsScratchView: js.lib.Float32Array

indicesScratchView: js.lib.Int32Array

scratchGeneration: Int

ensureCapacity(needed: Int): Bool
Name Type
needed Int
Returns
Bool

nextPow2(v: Int): Int
Name Type
v Int
Returns
Int