WebGL
How a canvas with a WebGL context is drawn without a browser, what works, what is not drawn, and how three.js, React Three Fiber and MapLibre GL run.
getContext('webgl'), getContext('webgl2') and getContext('experimental-webgl') on a canvas return Kinetograph's own context. It records your calls and draws nothing itself. No browser is involved.
This page describes the next release, not 0.3.0. The three.js, React Three Fiber and MapLibre sections, the Chrome comparison and the lists of what is drawn are not in a published version yet.
How it works
- Drawing. The calls of one frame become one layer of that frame's scene. ANGLE, Google's OpenGL ES library, draws the layer from your GLSL ES, and the engine places the result in the frame. If ANGLE cannot load or start, a page with a WebGL canvas fails to render, with an error saying why.
- ANGLE devices. Metal on macOS, Vulkan on Linux, Direct3D 11 on Windows. A machine with no usable GPU device uses SwiftShader, a CPU rasteriser shipped in the Linux and Windows packages.
KINETOGRAPH_ANGLE_BACKEND=swiftshaderforces it.doctorwith no entry prints the device in use and why, or says that WebGL will not work and what failed. - One kind of context. A canvas keeps the first kind it was given.
getContext('2d')aftergetContext('webgl')returnsnull, and so does the reverse. ForgetContext('webgpu')see WebGPU. - Shaders.
getShaderParameter(shader, gl.COMPILE_STATUS)is true for any shader that has source. A shader that does not compile fails the frame with ANGLE's message, which names the shader and the line. - A frame starts empty. Before each frame the engine resets every canvas context. The drawing buffer is empty, also with
preserveDrawingBuffer. The colour textures of a framebuffer start transparent black, a depth buffer at 1 and a stencil buffer at 0. A frame that draws nothing draws nothing. - A frame is a function of its number. It comes out the same drawn alone, out of order or by another worker. A pass that reads what an earlier frame left reads black.
- A texture a framebuffer drew in keeps what was drawn, from frame to frame (an environment map Three.js prefilters once and samples in every frame). Draws are carried until a clear, a covering blit, or a draw that provably fills the texture overwrites it. A render that skips frames (
still, a chunk,--range) renders the frames before it as needed, and on a page with CSS transitions such a frame is reported as not drawn right. - Time.
Date.now(),new Date(),performance.now(),Math.random(), timers andrequestAnimationFramefollow the frame number, not the wall clock. A shader has no clock either: compute its time uniform from the frame. A library that reads the wall clock is not frame-pure. - Unsupported calls. A call the stand-in cannot draw throws a
WebGLUnsupportedError. Its message starts withunsupported:and says what to use instead. A call is never accepted and then ignored where it would change the picture. - Refused draws and failed loads. A draw that WebGL refuses with a GL error draws nothing in a browser and says nothing. Here it is a warning with the frame it happened in. A texture, model or other file the page starts loading and that fails (a missing file, a URL that is not allowed) is a warning naming the URL. Under
--strictboth are errors.
unsupported: readPixels; the picture is drawn by the renderer, and the context cannot be read back (...)If the page lets the error escape, as an uncaught exception, an unhandled rejection or a console.error, it is listed as a page script item in the render warnings and in doctor, and --strict fails on it. A page that catches the error hides it from both.
What works
ANGLE draws what GL specifies, so WebGL 1 and WebGL 2 calls the recorder accepts are drawn as a browser draws them: framebuffers and multiple render targets, multisampled renderbuffers, depth and stencil tests, blending, instancing, uniform buffer objects, 3D, array, cube, integer, compressed and float textures, blitFramebuffer, copyTexImage2D, shadow samplers and a group's children as textures. The cases in crates/dom/tests/webgl.rs are compared with Chrome for Testing.
What is not drawn
The recorder refuses these with an unsupported: error that says what to use instead. Most are limits of the recording, not of ANGLE.
| Feature | Status | Note |
|---|---|---|
readPixels | Not drawn | The picture is drawn by the renderer. |
| Instanced points | Not drawn | |
| Multisampled renderbuffers of 32-bit floats | Not drawn | |
| Pixel buffer objects, transform feedback | Not drawn | |
| Cube faces from a layer picture | Not drawn | |
| Drawing into a texture that holds uploaded pixels, or attachments of different sizes in one framebuffer | Not drawn | |
framebufferTextureLayer above level 0, or into a texture that holds uploaded texels | Not drawn |
What differs from a browser
| Feature | Status | Note |
|---|---|---|
| Points | Differs | Squares of at most 511 px, as on Apple silicon. |
| Lines | Differs | One pixel wide. lineWidth above 1 is ignored, as in Chrome. |
| Trigonometry of large arguments | Differs | The GPU's own, so the last digits can differ from Chrome's. |
Libraries
| Library | Status | Note |
|---|---|---|
| Raw WebGL and GLSL shaders | Works (tested) | Cases are compared with Chrome for Testing in crates/dom/tests/webgl.rs, on ANGLE Metal and ANGLE SwiftShader. |
| p5.brush, standalone build | Works (tested) | Draws as in Chrome. A test renders 12 frames at concurrency 1 and 4, and frames 0, 5 and 11 alone, and the frames match. See @kinetographdev/p5-brush. |
| Three.js in an HTML page | Works in part | See Tested against Chrome. |
three and @react-three/fiber in a React project | Works in part | Through ThreeCanvas. See Three.js and React Three Fiber. |
| MapLibre GL JS | Runs | With recorded tiles. See Maps. Not part of the Chrome comparison. |
Other libraries run only as far as the calls they make are in the tables above. None are tested.
Three.js and React Three Fiber
three and @react-three/fiber stay your project's own dependencies. A project without them gets a build error naming the missing one.
import { ThreeCanvas } from '@kinetographdev/core/three';
// or: import { ThreeCanvas } from '@remotion/three';
export const Scene = () => (
<ThreeCanvas width={1280} height={720}>
<ambientLight />
<mesh><boxGeometry /><meshStandardMaterial color="orange" /></mesh>
</ThreeCanvas>
);ThreeCanvastakeswidth,heightand the props of React Three Fiber's<Canvas>. It draws once per frame, with no render loop of its own.useFrameruns once per frame drawn, so derive motion fromuseCurrentFrame().useCurrentFrame(),useVideoConfig()and<Sequence layout="none">work inside it.useTexture,useGLTFand other loads that suspend hold the frame until they settle. A failed load is a warning (an error under--strict).- Shadow maps are drawn. Spot-light and VSM shadows match Chrome; see the table below for the rest. Point-light shadows do not yet work.
- Workers run in the same thread, so a library that starts a
Workerstill works. A worker module that imports other files is bundled into one script first. This is how MapLibre's worker runs. A decoder that needs WebAssembly in a worker (Draco) is not covered. useVideoTextureanduseOffthreadVideoTextureare not available.
Maps
MapLibre GL JS builds tile and style URLs while it runs, so fetch-deps cannot see them in advance. Record them once:
npx kinetograph fetch-deps src/index.tsx --record --allow-host demotiles.maplibre.orgThe pass runs every frame with the network open for the hosts you name, and keeps each response in .kinetograph/net. render, still and doctor answer only from that store. A request that was not recorded is an error naming the URL and the frame.
- Pass
--allow-hostfor every host the map uses. A redirect to a host not on the list is refused. - Secret query parameters (
access_token,key,token,sig, signed-URLx-amz-*and any--secret-param) are left out of the store and the lock. A value in any other parameter that looks like a credential fails the pass unless you name it with--keep-param.
Tested against Chrome
A sample of 61 webgl_* pages from the three.js 0.187.0 examples (MIT), each drawn at 640x360 by Chrome for Testing and by Kinetograph with a frozen clock and compared at frame 12 (frame 0 for pages that draw once). The number is the mean, over all pixels, of the largest channel difference (0 to 255): under 1.5 is a match, under 6 a small difference.
Result of the 61 pages: 13 match, 12 small difference, 4 small difference on pages that draw with Math.random, 24 differ (13 of them draw with Math.random), 7 blank, 1 error. A page that draws with Math.random after its files load puts things in other places in the two engines, so its number says little and it was judged by eye. The sweep was run before the latest fixes (float cube faces among them) and has not been repeated.
| Area | Status | What the pages showed |
|---|---|---|
| Clipping, stencil | Works | Three pages: two match, one draws with Math.random. |
| Texture arrays, 3D textures | Works | Both match. |
| Spot-light and VSM shadows | Works | Both match. |
| Other shadow maps (PCF, PCSS, CSM, contact) | Partly | Pages cannot be compared, or the meshes are in another animation phase. |
| Point-light shadows | Not yet | Blank: needs depth cube textures. |
| Lights and materials | Partly | Physical transmission matches; hemisphere light, dispersion and blending differ a little; clearcoat, iridescence and alpha transmission differ. |
| Post-processing | Partly | Bloom and pixel match; depth of field, SSAO and afterimage differ a little; GTAO needs Draco and SAO draws nothing (cause not found). |
| glTF models | Partly | Sheen matches. Pages that load models from a remote list or with Draco differ or are blank. |
| Render targets | Partly | Multiple render targets match; render-to-texture differs a little; multisampled feedback differs; drawing into a layer of an array texture is an error. |
| Lines, points, instancing | Partly | Dashed lines match; fat lines and wave points differ a little; the instancing pages draw with Math.random. |
| Cube maps, environments | Partly | PMREM test matches; cube-map material differs a little. HDR cube-map pages were blank in the sweep, before float cube faces were added. |
| Morph targets, skinning | Partly | Both differ a little; the keyframe page needs Draco. |
Not supported yet
- Point-light shadows (depth cube textures).
- Drawing into one layer of an array texture (
framebufferTextureLayer). - Loaders that need WebAssembly: Draco, KTX2, Basis.
readPixels, and reading a WebGL canvas withtoDataURL,toBlobordrawImage.- The SAO post-processing pass, which draws nothing, and multisampled render targets that feed back over more than 1024 draws.
useVideoTextureanduseOffthreadVideoTexture.
Check your own page
npx kinetograph doctor src/index.tsx --range 0..120
npx kinetograph render src/index.tsx --strict
npx kinetograph still src/index.tsx 45npx kinetograph doctor html/index.html --range 0..120
npx kinetograph render html/index.html --strict
npx kinetograph still html/index.html 45doctorruns every frame in the range without the GPU and lists what the engine cannot draw, with the frames. See CLI.render --strictstops at the first frame with unsupported content: error codeUNSUPPORTED_CONTENT, no output file.stilldraws one frame alone. Compare it with the same frame of a render to see that the frame does not depend on the ones before it.
Without --strict, render and still print the problems on stderr and draw the frame anyway. A line for a page error looks like this:
warning: 1 kind of unsupported content in 1 frame, drawn without it or approximated (--strict fails instead):
page script { uncaught exception: WebGLUnsupportedError: unsupported: readPixels; ... }: the page raised an error and went on; whatever it was drawing may be missing (frame 0)Writing for determinism
- Derive everything from the frame number:
useCurrentFrame()in React, or the time the engine gives the page. Pass time to a shader as a uniform you compute. - Draw the whole picture in every frame, clear included. Do not rely on the previous frame's buffer or on
preserveDrawingBuffer. - Do not call
readPixels, and do not read or draw a WebGL canvas withtoDataURL,toBlobordrawImage. Render into a framebuffer texture and sample it. - For a value that must stay the same from frame to frame, use
random('seed')fromremotion.Math.random()is reseeded from the frame number. - Load images and other assets before the first frame is drawn. Hold the frame with
delayRender(); it fails withDELAY_RENDER_TIMEOUTafter 30 seconds unless you set--timeout.