Skip to content

@shadekit/core

The engine behind every wrapper. Use it directly when you drive a canvas yourself or build a wrapper for a framework shadekit doesn’t ship.

import { ShaderInstance } from '@shadekit/core';
import { aurora } from '@shadekit/shaders/aurora';
const instance = new ShaderInstance(aurora, { pointer: true });
await instance.mount(canvas); // also resolves via instance.ready
instance.updateUniforms({ speed: 0.4 });
instance.pause();
instance.play();
instance.destroy();
Member Description
constructor(shader, options?) Takes a ShaderDefinition + ShaderOptions
mount(canvas) Async — acquires the shared device, builds the pipeline, starts rendering. Returns a promise of the instance
ready Promise that resolves once the instance is mounted — safe hook for initial updateUniforms
updateUniforms(props) Merge uniform values; only the keys you pass are rewritten
play() / pause() Resume or stop the render loop
destroy() Remove listeners, unobserve, release the ticker slot

All instances share one GPUDevice (getSharedDevice()) and one requestAnimationFrame ticker. Pipelines are cached per shader name — mounting the same engine twice compiles once.

Option Type Default Description
backend 'auto' | 'webgpu' | 'webgl2' 'auto' Prefer WebGPU, fall back to WebGL2 — see Backends
maxDpr number 2 Upper bound for devicePixelRatio
pointer boolean false Track the pointer and feed mouse, pointer, pulse builtins
paused boolean false Start paused — renders one frame
respectReducedMotion boolean true Under prefers-reduced-motion, render one frame then pause
frameBudget number 17 Frame time (ms) that triggers adaptive DPR reduction
onError (error: unknown) => void — Called when device acquisition or rendering fails
interface ShaderDefinition {
name: string; // unique — used for the pipeline cache
wgsl: string; // @fragment fn fs_main(...) -> @location(0) vec4f
glsl: string; // void main() writing o_color
uniforms?: Record<string, UniformDef>;
}

UniformDef is { size: 1|2|3|4, default: UniformValue } — size is the component count actually used; the slot is always vec4. A UniformValue is number | readonly number[].

Both fragment bodies get the same built-in slots (time, resolution, mouse, pointer, pulse, scroll) plus one uni.<name> / u_<name> per uniforms key. See Uniform reference.

Exported for wrapper authors and codegen:

  • layoutUniforms(def) — compute the packed slot layout for a shader.
  • writeUniform(target, layout, key, value) — write one field into the packed buffer.
  • defaultUniformValues(def) — the resolved defaults record.
  • buildWgslFragment(def) / buildGlslFragment(def) — assemble the full fragment source with the uniform block injected.
import { getSharedDevice } from '@shadekit/core';
const device = await getSharedDevice(); // GPUDevice | null

Lazily acquires the page’s one GPUDevice. Returns null when WebGPU is unavailable — under backend: 'auto' the instance then mounts the WebGL2 backend instead.