Appearance
Kernel
Opaque handle to a compiled GPU compute kernel. Returned by Kernel.fromSource(...) / Kernel.fromAsset(...). Two dispatch entry points: kernel.run({...}) for standalone compute (texture / buffer I/O, no mesh involvement) and mesh.runCompute(kernel, options) when the kernel writes mesh-attribute buffers.
Properties
functionName
- Type: string
- Display name of the kernel's entry-point function.
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.
Static Methods
fromAsset()
javascript
Kernel.fromAsset(options: Object): Promise<Kernel>Compile a compute kernel from a shader-library asset declared in the experience's asset library.
Parameters:
options(Object)options.assetId(string) - Shader-library asset id.options.functionName(string) - Kernel entry-point name.
Returns: Promise<Kernel>
fromSource()
javascript
Kernel.fromSource(options: Object): Promise<Kernel>Compile a compute kernel from inline MSL source. Handy for prototypes and tests; production scripts should prefer Kernel.fromAsset(...) so the source lives in the experience's asset library.
Example:
javascript
const kernel = await Kernel.fromSource({
source: `#include <metal_stdlib>
using namespace metal;
kernel void wave(...) { ... }`,
functionName: 'wave'
});Parameters:
options(Object)options.source(string) - MSL source string.options.functionName(string) - Kernel entry-point name.
Returns: Promise<Kernel>
Methods
destroy()
javascript
destroy(): voidRelease the kernel's GPU resources (compiled library + pipeline state) immediately. Use in hot loops where JSC's non-deterministic GC would otherwise stretch the lifetime. Normal scripts don't need to call this – ARC handles cleanup when the kernel goes out of scope. Calling mesh.runCompute(...) or kernel.run(...) against a destroyed kernel warns and aborts the dispatch.
Returns: void
run()
javascript
run(options: { uniforms?, inputBuffers?, outputBuffers?, inputTextures?, outputTextures?, threadGroups, threadsPerThreadgroup? }): voidDispatch the kernel against textures and / or Buffer handles – no mesh involvement. Use for compute passes that don't write mesh attribute buffers (texture generation, particle simulation pre-pass, any "pure compute" kernel that doesn't displace geometry).
MSL slot order: buffer(0) uniforms → buffer(1..) inputBuffers then outputBuffers → texture(0..) inputTextures then outputTextures.
Example:
javascript
kernel.run({
uniforms: new Float32Array([scene.time]),
outputTextures: { heightOut: heightMap.write },
threadGroups: [16, 16, 1]
});Parameters:
options({ uniforms?, inputBuffers?, outputBuffers?, inputTextures?, outputTextures?, threadGroups, threadsPerThreadgroup? })
Returns: void