Backend scaling: 2D, WebGL2, WebGPU and a shared-GL copy bridge
I wanted to know where the crossover points sit when you scale up live HTML elements in a canvas across all four drawing routes:
2D drawElementImage(), WebGL2 texElementSubImage2D(), WebGPU drawElementImageToTexture(),
and a 2D-to-shared-WebGL copy bridge. Pick a configuration or run a sweep below to measure frame times, upload cost, GPU timers
and Long Animation Frames on your device.
Initialising benchmark...
Show the code the four backend loops and timing hooks
- 2D route: draws every tile with 5-argument
drawElementImage(el, 0, 0, w, h)under a per-tile affine transform insidepaint. - WebGL2 route: uploads dirty tiles through
ElementGL.upload()(texElementSubImage2D()), shades all quads with one context and callsupdateElementGeometry(). - WebGPU route: uses the shared device from
/shared/backend.js, uploads dirty tiles withGPUElementTexture.update()(drawElementImageToTexture()) and draws every quad in one render pass. - Shared-GL copy bridge: draws dirty tiles on a drawable 2D canvas, copies each tile into a shared
ElementGLtexture viatexSubImage2D(), shades all quads and composites back withdrawImage(). - Timers: Long Animation Frames breakdown plus optional
EXT_disjoint_timer_query_webgl2polling.
| Backend | Count | Tile | Dirty | FPS | p50 / p95 / p99 (ms) | Upload (ms) | Draw (ms) | GPU timer | LoAFs (script/style/post) |
|---|---|---|---|---|---|---|---|---|---|
| No measurements yet. | |||||||||
After a backend sweep or full matrix completes, the page queues a compact summary to /api/reports (nothing is sent from localhost) so results from phones and desktop GPUs can be compared later.
LoAF columns show frames over 50 ms split into script, style/layout/paint and post-main-thread presentation delay.
How it works
Every route draws the same grid of live HTML cards at native devicePixelRatio, applying a per-tile wave, rotation and shear transform so the GPU routes and the 2D route do matching geometric work.
Tiles that mutated on a frame also flash a warm orange tint, so you can tell at a glance whether 0%, 10% or 100% of elements are re-uploading.
- 2D (
drawElementImage): there is no retained GPU texture per element, so every frame callsrequestPaint()and draws all N children with 5-argumentdrawElementImage(el, 0, 0, w, h). - WebGL2 (
texElementSubImage2D): oneElementGLcontext holds one texture per tile. At 0% mutation it skipsrequestPaint()and texture uploads after frame 0, redrawing the cached textures straight fromrequestAnimationFrame. At 10% and 100% it only re-uploads the mutated tiles and updates hit-test boxes withupdateElementGeometry(). - WebGPU (
drawElementImageToTexture): uses the single shared device from/shared/backend.js. Each tile owns aGPUTextureand cached bind group, and all 128 transform matrices pack into one 32 KB dynamic uniform buffer so the draw loop allocates no GPU objects per frame. On a software WebGPU adapter, on-screen WebGPU is skipped to avoid taking down the tab, and the live check verifies WGSL and upload off-screen instead. - Shared-GL copy bridge: mirrors how
/lib/shader-fx.jshandles more elements than a tab's WebGL context limit. Dirty tiles draw onto a drawable 2D canvas, copy into one sharedElementGLtexture viagl.texSubImage2D(), shade all N quads and composite the result back onto the 2D canvas withdrawImage().
JS upload and total draw times come from performance.now() around the upload calls and frame callback.
Where available, WebGL2 queries GPU elapsed time with EXT_disjoint_timer_query_webgl2, and WebGPU uses timestamp-query when the shared device enables it, falling back to queue.onSubmittedWorkDone() latency otherwise.
Main-thread stalls over 50 ms are broken down with a long-animation-frame observer.
Live checks
These checks create WebGL2 and WebGPU fixtures and read pixels back to verify rendering, so they wait for a tap instead of running on load.
| Status | Check | Detail |
|---|
Findings
- Hardware GPU and Android phone curves: not verified yet. Run the 20-cell backend sweep or 180-cell full matrix on a desktop GPU and on Chrome for Android to record the real crossover points between 2D, WebGL2, WebGPU and the shared-GL copy bridge.
- GPU timer availability: not verified across hardware yet. Chrome on desktop and Android may withhold
EXT_disjoint_timer_query_webgl2ortimestamp-querydepending on flags and privacy settings; the shared WebGPU device from/shared/backend.jsopens with default features, so WebGPU falls back toqueue.onSubmittedWorkDone()unless the device exposes timestamps. - Context budget across sweeps: grouping the matrix by backend and retiring each WebGL canvas via
window.__gfx.retire()keeps peak active WebGL contexts at 1 across all 180 cells, staying inside Chrome's Android limit.