Skip to content

Kernel beta ​

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 read-only ​

  • Type: string
  • Display name of the kernel's entry-point function.

id read-only ​

  • 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.

A compute shader with a header names its own entry point, and the options of kernel.run and mesh.runCompute bind by the names in that header.

Example:

javascript
const ripple = await Kernel.fromAsset({ assetId: 'ripple' });
const heights = await Texture.compute({ pixelFormat: 'rgba16Float', width: 256, height: 256 });
scene.on('render', function () {
    ripple.run({ parameters: { damping: 0.98 }, outputTextures: { output: heights.write } });
});

Parameters:

  • options (Object)
  • options.assetId (string) - Shader-library asset id.
  • options.functionName (string) (optional) - Kernel entry-point name. Required unless the shader has a header.

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(): void

Release 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? }): void

Dispatch 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.

A kernel from an authored compute shader takes no slots: the maps bind by the names in the shader's header and the shader says how it dispatches. Pass parameters: { name: value } for its parameters, which keep their defaults otherwise, outputTextures: { output: texture.write } for a paint, and outputBuffers for the fields of an update. A texture left out reads its empty value. count overrides the number of elements, time and deltaTime the engine's values, and target names the object a surface input buffer is local to.

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? }) - , or for an authored kernel { settings?, inputBuffers?, outputBuffers?, inputTextures?, outputTextures?, count?, time?, deltaTime?, target?, threadGroups? }

Returns: void