Writing Kernels
A Kernel contains the WGSL code that runs on the GPU.
import { Kernel } from '@volten/core';
const scale = new Kernel(` fn main(gid: vec3u) { output[gid.x] = input[gid.x] * multiplier; }`);You write a main function. Volten adds the compute entry point, workgroup size, bindings, and safety guard around it.
Builtin Shorthands
Section titled “Builtin Shorthands”Volten expands a few parameter names in main:
| Name | Type | Meaning |
|---|---|---|
gid | vec3u or vec3<u32> | Global invocation id |
lid | u32 | Local invocation index |
lid3 | vec3u or vec3<u32> | Local invocation id |
wid | vec3u or vec3<u32> | Workgroup id |
nwg | vec3u or vec3<u32> | Number of workgroups |
const kernel = new Kernel(` fn main(gid: vec3u, lid: u32) { data[gid.x] = f32(lid); }`);You can also write the full WGSL builtin syntax yourself.
fn main(@builtin(global_invocation_id) gid: vec3u) { data[gid.x] = data[gid.x] * 2.0;}Helper Functions
Section titled “Helper Functions”Only the function named main is treated as the kernel entry point. Other functions are left alone.
const kernel = new Kernel(` fn square(x: f32) -> f32 { return x * x; }
fn main(gid: vec3u) { data[gid.x] = square(data[gid.x]); }`);Labels
Section titled “Labels”Add a label when you want clearer debug output and browser GPU tooling names.
const kernel = new Kernel(`...`, { label: 'scale values'});