DOM radiosity
I wanted to know if a WebGPU compute pipeline could take a live HTML texture straight from drawElementImageToTexture(), build a Jump-Flood distance field and run 2D Radiance Cascades global illumination with zero CPU readback. Type into the glowing word, toggle the lamps or drag the dark pillars to sweep coloured shadows across the stage.
Show the code upload, Jump-Flood SDF, 4-cascade Radiance Cascades and hit-testing
- One drawable HTML gallery inside the canvas. Bright glyphs and lit buttons act as emitters; dark opaque pillars act as occluders.
- Seed compute pass (WGSL). Reads the live HTML texture with
textureLoad(), unpremultiplies alpha and writes emitter radiance and JFA seeds. - Jump-Flood Algorithm (WGSL). Runs
log2(N)passes to compute exact Euclidean distance to the nearest live glyph or occluder. - 2D Radiance Cascades (WGSL). Marches 4 angular-spatial cascades (4, 16, 64 and 256 rays) against the JFA distance field and merges upper cascades bilinearly.
- Composite and hit-testing. Composites the native-resolution DOM texture over the lit floor and calls
updateElementGeometry()so clicks and typing stay aligned. - Live checks. Probes
drawElementImageToTexture()usage flags,textureLoad()premultiplied alpha, compute shadow contrast andelementFromPoint().
How it works
Every interactive element in the gallery lives inside a single <div id="scene" drawable> child of the canvas. When you type a letter, toggle a lamp or drag a pillar, Chrome fires a paint event on the canvas and the page uploads the element's current picture to an rgba8unorm texture with GPUQueue.drawElementImageToTexture() (or gl.texElementSubImage2D() on the WebGL 2 route through ElementGL).
The GPU classifies every pixel directly from that live texture, with no DOM geometry readback in JavaScript. Transparent pixels (alpha < 0.14) are air. Bright pixels (max(r, g, b) > 0.38 after unpremultiplying) are emissive light sources. Dark opaque pixels (alpha > 0.45 and max(r, g, b) <= 0.38) are solid occluders. That means typed glyph strokes float in open air as light emitters, while the dark pillar boxes block rays and cast coloured penumbra shadows.
On WebGPU, four @compute stages run on a downscaled grid (up to 384 px on the long side, rounded to multiples of 16) before the full-resolution composite pass:
- Seed pass: reads
domTexwithtextureLoad(), writes emitter colour and solidity to anrgba16floatscene texture, and seeds the Jump-Flood coordinate buffer. - Jump-Flood SDF: runs 9 ping-pong passes (step 256 down to 1) so every pixel stores the exact Euclidean distance to the nearest solid glyph or pillar.
- Radiance Cascades: runs 4 cascades from coarse to fine (256, 64, 16 and 4 angular bins at probe spacings of 16, 8, 4 and 2 grid cells), sphere-tracing each cascade's ray interval against the JFA distance field and bilinearly merging unoccluded rays from the cascade above.
- Resolve and composite: resolves cascade 0 into a smooth irradiance texture, feeds it back as indirect bounce on the next frame, and composites the crisp native-resolution DOM texture over the lit gallery floor.
After each draw, updateElementGeometry() keeps the hit boxes of the text input, lamp buttons and movable pillars aligned with their drawn positions, and toggling a data attribute clears Chromium's hit-test cache whenever a pillar moves. On browsers without a usable WebGPU device (including headless test browsers with software adapters), the page runs the same JFA and 4-cascade Radiance Cascades algorithm in WebGL 2 fragment shaders inside one ElementGL context, and falls back to 2D canvas or plain HTML if WebGL is unavailable.
Live checks
These checks run on isolated fixtures when you press the button, using the shared device from checkDevice(). They never touch or scroll the gallery above.
| Result | Experiment | Detail |
|---|
Findings
- Zero-readback DOM classification works from a single texture. Separating transparent air, dark opaque occluders and bright emitters purely by unpremultiplied RGB and alpha lets typed letters, toggled buttons and dragged boxes drive global illumination without querying
getBoundingClientRect()per element. - The upload texture needs
RENDER_ATTACHMENTin Chromium 157. Earlier measurements on/webgpu-features/showeddrawElementImageToTexture()expectsCOPY_DST | RENDER_ATTACHMENT | TEXTURE_BINDINGon anrgba8unormtarget. The first two live checks above test whetherTEXTURE_BINDING | COPY_DSTalone or an addedSTORAGE_BINDINGflag are accepted on this browser. - Downscaled compute plus native DOM composite keeps text crisp on phones. Running JFA and 4-cascade Radiance Cascades at a maximum of 384 cells keeps the compute workload small and produces soft penumbras, while compositing
domTexat full device pixel resolution keeps the live input and buttons readable. - Hit-testing inside a full-stage drawable container stays aligned. Putting the input, lamps and draggable pillars inside one
drawablecontainer means one texture upload and oneupdateElementGeometry()call per paint, with a data-attribute toggle to clear the hit-test cache when a pillar moves. - Real GPU performance and on-screen WebGPU presentation are not verified here. Run the live checks on Chrome with hardware WebGPU to confirm direct
STORAGE_BINDINGbehaviour, compute readback values and frame rate on a phone.