Tony Yiding Tian

WebGPU Renderer

5,000 dynamic lights in a browser, three ways

WebGPU, TypeScript, WGSL · Sep – Oct 2025

The Sponza atrium lit by thousands of coloured point lights

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
PassesOne: geometry and lightingTwo: G-buffer, then lighting
OverdrawLights fragments that are later hiddenLights each visible pixel once
MemoryNo G-bufferThree G-buffer targets
BandwidthLowerThe G-buffer is written, then read
TransparencySupportedHard: 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.

Renderer500 lights2,000 lights5,000 lightsvs naive
Naive forward52.3 ms199.3 ms497.2 ms1×
Forward+8.3 ms16.0 ms32.8 ms15.1×
Clustered deferred8.3 ms8.3 ms9.3 ms53.4×
Average frame time in Chrome on an RTX 5080, and the speedup at 5,000 lights. The display refreshes at 120 Hz, so 8.3 ms is the floor.

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.

Live demo · GitHub

[email protected] · LinkedIn · GitHub · Resume