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.
backend option
Section titled “backend option”| 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" ... />.
Shared device
Section titled “Shared device”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.
Fallback in practice
Section titled “Fallback in practice”- If
navigator.gpuis missing orrequestAdapter()fails under'auto', the instance mounts the WebGL2 backend instead — no code change needed. - Uniform packing is identical on both backends (one
vec4slot per field), so a shader looks and animates the same either way. - Errors surface through the
onErroroption:
new ShaderInstance(aurora, { onError: (err) => console.error('shader failed', err),});