Skip to content

WebGPU in the web SDK, with WebGL2 behind it

The web SDK draws with WebGPU where the browser has it on real hardware, and with WebGL2 everywhere else. The WebGPU renderer runs the same passes, targets, states and order as the WebGL2 one, and both are held to the reference pictures the native backends are matched to, by the same rule.

// renderer: 'auto'
const sona = await SonaRenderer.create({ canvas, appId: 'your-app' });
// 'webgpu' or 'webgl2'
sona.renderer;
// ask for one
await SonaRenderer.create({ canvas, renderer: 'webgl2' });
renderer What it does
'auto' (default) WebGPU on a hardware adapter; WebGL2 when there is no WebGPU, the adapter is a software one (SwiftShader, llvmpipe, a fallback adapter), or WebGPU fails to start
'webgpu' WebGPU, software adapters included. SonaError('unsupported') when the browser has none
'webgl2' WebGL2 only

The pipelines are compiled before the canvas is claimed, so a WebGPU failure in 'auto' still leaves the canvas free for WebGL2 (a canvas that has had a WebGPU context can never get a WebGL2 one). A lost WebGPU device is replaced by a new one from the same kind of adapter and every avatar is uploaded again, as a restored WebGL2 context is. SonaRenderer.supported() is true with either API.

How it is built

What differs between the two APIs, and how the picture stays the same:

Difference WebGL2 WebGPU
Clip depth 0..w turned into -w..w in the vertex stage 0..w as written
Render targets bottom row first top row first: every lookup into a target the frame drew is flipped back (the shadow taps' v offsets too), so each texel holds what the WebGL2 target holds at that place
SSAO in GL's frame evaluated at GL's pixel, uv and poisson cell
Mips generateMipmap a render pass per level through an sRGB view (linear-light average); cut-out textures take the same coverage-keeping levels
MSAA ANGLE draws upside down under GL, so its samples sit mirrored the main pass draws upside down too (front face flipped), and resolves by hand the right way up, averaging linear light

Measured

Both renderers are scored against the same reference pictures as the native backends, by the same rule: the mean difference and the difference away from silhouettes stay within twice what two runs of one renderer differ by. The WebGL2 renderer reproduces the reference pictures exactly; WebGPU differs from them only at single pixels on edges.

WebGPU against WebGL2 through the SDK, with and without 4x MSAA, on avatars with see-through parts and with the shadow and occlusion passes on: the two cover the same pixels, and what differs is single pixels on edges. On a hardware GPU 'auto' picks WebGPU.

This page as Markdown · All the docs as one file (llms-full.txt)