WebGPU Renderer
5,000 dynamic lights in a browser, three ways
WebGPU, TypeScript, WGSL · Sep – Oct 2025
- 53× faster than naive forward rendering
- 497 → 9.3 ms per frame at 5,000 lights
- 3.5× faster than Forward+
Three real-time renderers for the Sponza atrium, lit by thousands of moving point lights, in WebGPU, TypeScript and WGSL: naive forward, Forward+ and clustered deferred. It runs in the browser, live at webgpu.tonyxtian.com.
Naive forward
The baseline. Every fragment loops over every light, with inverse-square falloff and Lambert's cosine, so its cost is fragments × lights: at 5,000 lights it draws 2 frames a second.
Clustering the lights
Forward+ and clustered deferred share a compute pass that sorts the lights into clusters. The view frustum is cut into a 16×9×24 grid: 16×9 tiles across the screen, and 24 slices in depth spaced exponentially, near · (far / near)^(z / 24), so the slices are thin near the camera, where the detail is, and deep far away.
Each cluster gets a view-space bounding box, and every light whose sphere of influence meets the box joins the cluster's list through an atomicAdd, up to 256 lights a cluster. When shading, a fragment finds its cluster from its screen position and depth, and loops over only that cluster's lights. Every buffer follows WGSL's alignment rules, 16 bytes for a vec3, checked against an offset calculator.
Forward+ and clustered deferred
Forward+ shades in one pass, geometry and lighting together, with each fragment reading its cluster's lights. Clustered deferred splits it in two. A G-buffer pass writes position and normal (rgba16float) and albedo (rgba8unorm) to three render targets; then a fullscreen pass, a quad drawn with no vertex buffer, lights each pixel once from its cluster.
| Forward+ | Clustered deferred | |
|---|---|---|
| Passes | One: geometry and lighting | Two: G-buffer, then lighting |
| Overdraw | Lights fragments that are later hidden | Lights each visible pixel once |
| Memory | No G-buffer | Three G-buffer targets |
| Bandwidth | Lower | The G-buffer is written, then read |
| Transparency | Supported | Hard: one surface per pixel |
Benchmarking
One button runs the whole matrix unattended: 3 renderers × 10 light counts from 500 to 5,000, 30 configurations. For each it switches renderer, waits 200 ms, warms up for 30 frames, records 120, and keeps the average, minimum and maximum; the results download as a CSV. It times whole frames, from one requestAnimationFrame to the next, so CPU work, GPU work and the two waiting on each other all count.
| Renderer | 500 lights | 2,000 lights | 5,000 lights | vs naive |
|---|---|---|---|---|
| Naive forward | 52.3 ms | 199.3 ms | 497.2 ms | 1× |
| Forward+ | 8.3 ms | 16.0 ms | 32.8 ms | 15.1× |
| Clustered deferred | 8.3 ms | 8.3 ms | 9.3 ms | 53.4× |
Naive forward slows almost in proportion to the lights; Forward+ slows 4× for 10× the lights; clustered deferred barely moves. It was also the steadiest: across the run, naive frames took from 8 to 1,108 ms, Forward+ from 8 to 50, and clustered deferred from 8 to 17.
With the same clusters, deferred is 3.5× faster than Forward+ at 5,000 lights. Forward+ lights fragments that are later drawn over, so its lighting cost rises with the geometry; deferred lights each visible pixel exactly once, in a fullscreen pass with coherent memory access.