Skip to content

Backends — WebGPU & WebGL2

Every shader definition ships both a WGSL and a GLSL ES 3.00 fragment. The engine picks the backend at mount time.

Value Behaviour
'auto' (default) Try WebGPU first; fall back to WebGL2
'webgpu' WebGPU only — mount() rejects if unavailable
'webgl2' WebGL2 only — useful for testing the fallback
new ShaderInstance(aurora, { backend: 'auto' });

For <sk-shader> and <Shader>, pass the option where the instance is created — defineShaderElement(defs, { backend: 'webgl2' }) or <Shader backend="webgl2" ... />.

All instances on a page share one GPUDevice, created lazily by getSharedDevice(). Mounting a second canvas costs a pipeline compile — pipelines are also cached per shader name, so swapping presets on the same engine reuses the compiled pipeline.

  • If navigator.gpu is missing or requestAdapter() fails under 'auto', the instance mounts the WebGL2 backend instead — no code change needed.
  • Uniform packing is identical on both backends (one vec4 slot per field), so a shader looks and animates the same either way.
  • Errors surface through the onError option:
new ShaderInstance(aurora, {
onError: (err) => console.error('shader failed', err),
});