kernel

Your own compute pass.

kernel(
  body: WgslBody | string,
  opts: {over: string | readonly [number, number]; iterations?: number; when?: 'frame' | 'init'},
): KernelInput

Import kernel from shaders/std.

Over a buffer (over: 'stars') it runs once per live element with index: u32; over a grid size (over: [64, 64]) once per cell with cell: vec2i. It reads and writes every buffer it names, reads other inputs by name (soft(p)), and sees props, built-ins (time, dt, pointer…), library functions and helpers. iterations repeats it within a frame; when: 'init' runs it once, to seed a buffer (and again after a resize).

Parameters

NameTypeNotes
bodyWgslBody | string
opts{over: string | readonly [number, number]; iterations?: number; when?: 'frame' | 'init'}

Example

move: kernel(wgsl`
  var s = stars[index];
  s.vel += (pointer - s.pos) * dt;
  s.pos += s.vel * dt;
  stars[index] = s;
`, {over: 'stars'}),

Tip. A kernel has no return value: write your results into a buffer.

See also

  • buffer — A storage buffer of count records of a struct type, zero-filled.
  • struct — Declare a record type.
  • simulate