Culling and the frame
Many lights: clustered lighting
A street with a hundred lamps, a building with a thousand lights: each pixel is reached by only a few of them. Clustered lighting cuts the camera's view into small boxes, lists the lights that reach each box once per frame, and lets every pixel shade only its box's list. This page explains the idea step by step. The last part shows how RASM does it.
In short
- Shading every light at every pixel costs pixels × lights. Most of that work adds 0.
- A light with a range reaches only a sphere. A light without one (the sun, or a lamp with no range) reaches everything and must be shaded everywhere.
- Tiles split the screen. A tile's column still runs from near to far, so it collects every light along the way.
- Clusters cut each column into depth slices: small boxes of the view. Slices that grow with distance keep the boxes about as deep as they are wide.
- Each frame the GPU lists the lights of each cluster. Each pixel finds its cluster from its position on screen and its depth, and shades that list.
- The lists have a fixed size. Fill them in a fixed order, so the image does not change from run to run.
1The problem: every light at every pixel
The simple shader loops over all lights for each pixel. Look at one point on the floor below: 40 lights are in the scene, and 3 of them reach it.
The fix is to find, for each pixel, the short list of lights that can reach it, and to find it without testing every light at every pixel.
2How far a light reaches
A point light's brightness falls with the square of the distance, 1 / d², and never reaches 0. So the shader multiplies it by a window that goes smoothly to 0 at a chosen range r. Past the range, the light adds exactly 0. Now the light has a shape: a sphere of radius r.
light(d) = I / d² · ( max(1 − (d / r)⁴, 0) )²
- I
- the light's intensity
- d
- the distance from the light to the point
- r
- the range: the window is 1 near the light and 0 from r on
A spot light lights only a cone. Its sphere can be smaller than the range's: the smallest sphere around the cone of length r. Which sphere is smallest depends on the cone's angle θ:
Point light
A sphere of radius r, centered on the light.
Narrow spot
Center r / (2 cos θ) along the axis, the same radius.
Wide spot
Center r cos θ along the axis, radius r sin θ.
3Tiles: split the screen
Cut the screen into tiles, for example 16 × 9. Each tile is a column of the view: the part of the frustum behind those pixels. List, once per frame, the lights whose sphere touches each column. A pixel then shades only its tile's list. This is tiled shading (Forward+).
Seen from the side, a column runs from the near plane to the far plane. Every light along it, near or far, is in the list:
One tile: a whole column
Where the column crosses near and far objects, or a long street, the list is long.
Clusters: the column in slices
Each slice keeps only the lights near its own depth.
Some renderers fix tiles by first drawing the scene's depth, then shrinking each column to the depths it really holds. That needs a depth pass, and it does not help see-through surfaces, which sit in front of that depth.
4Clusters: cut the columns into slices
A cluster is one tile × one depth slice: a small box of the view. A common grid is 16 × 9 tiles × 24 slices = 3456 clusters. The grid comes from the camera alone: it needs no depth buffer, so see-through surfaces and multisampled targets use it like opaque ones.
Even slices are a bad fit for a perspective camera. A tile is narrow near the camera and wide far away, so even slices make near clusters long thin needles and far clusters flat plates. Slices that each grow by the same factor keep the clusters about as deep as they are wide:
start of slice k = near · (far / near)^(k / S)
slice of depth z = ⌊ S · log(z / near) / log(far / near) ⌋
- S
- the number of slices, for example 24
- near, far
- the depths the slices cover: the camera's near plane, and the farthest light
- z
- a pixel's view depth: its distance along the camera's forward axis
With near = 0.1 and far = 100, each slice is 1.33 times as deep as the one before. An orthographic camera's tiles do not widen with distance, so its slices are even.
5Building the lists on the GPU
The lights move, and the camera moves, so the lists are built again every frame. Testing every light against every cluster costs clusters × lights: 3456 × 4096 = 14 million tests. Two levels cut that down: first the lights of each tile's column, then, inside a tile, the lights of each cluster.
- Visible lightseach light's sphere in view space, tested against the frustum
- Tile listsa group per tile: the spheres that touch the column's 4 side planes
- Cluster listsa thread per cluster: its tile's spheres that touch its box
- Shadea pixel finds its cluster and loops over that list
The box test is the sphere–box test: the point of the box closest to the sphere's center, then its distance. The box is the cluster's 8 corners in view space; it is a little bigger than the cluster, which only adds a light that adds 0.
A fixed order
Thousands of GPU threads add lights to lists at the same time. If each thread takes the next free place with an atomic counter, the order of a list changes from run to run. The sum of the lights then changes in its last bits, and two runs of the same frame give different images, which breaks image tests. Instead, pack the hits with a prefix sum, in light order. The lists, and the image, are then the same every time.
6A cap per cluster, and memory
The lists live in one buffer with a fixed place per cluster. That needs a cap: the most lights one cluster can hold. A light past the cap is left out of that cluster, and its pixels miss it, so count those and tell the user.
Far clusters are big. With thousands of short-range lights, one far cluster can cover hundreds of them, and every pixel in it loops over all of them. Finer tiles far away, or culling the empty depth of each cluster, help there.
7Variants
Tiled (Forward+)
Screen tiles only, often bounded by the depth buffer's min and max per tile.
Needs a depth pass
Clustered forward
Tiles × depth slices. Works for see-through surfaces and with MSAA.
No depth needed
Clustered deferred
The same lists, read by a lighting pass over a G-buffer.
Memory for the G-buffer
Z-binning
Sort lights by depth; a depth bin keeps a range of light indices, a tile a bit mask.
Little memory, a sort per frame
Bit masks
A bit per light per cluster instead of a list: no cap, but its size grows with the lights.
Good for hundreds, not thousands
Lists built on the CPU
Fine for a few hundred lights; the CPU's cost grows with the lights.
Simple, but not GPU-driven
8Pitfalls
- Light pools end in a hard edge, or blocks of light flicker on and off
- The shader's falloff does not reach 0 at the range the lists use. The window must make the light exactly 0 past its sphere.
- A spot light's lit area is cut along a straight line
- Its sphere does not hold the whole cone: the cone's round end reaches farther than r cos θ along the axis.
- Dark seams along tile or slice edges
- The pixel picks its cluster with other math than the list builder. Share one function, and give each box a small margin.
- The image changes a little between two runs
- Lists filled with atomic counters come out in a different order each run. Pack them in light order.
- Far away, the frame gets slow
- Big far clusters hold many lights each. Shorter ranges, fewer far lights, or finer far clusters.
- Some lights are missing in a dense spot
- A cluster reached its cap. Count the lights left out and warn.
9In RASM
RASM keeps every light in a GPU table and sorts the local lights into 16 × 9 × 24 clusters for each shaded view, every frame, on the GPU. Point and spot lights with a range are local; directional lights and lights with range 0 are global. EngineConfig::clusteredLighting turns it off (every pixel then shades every light), and examples/11_many_lights shows it with 1024 moving lights.
Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Grid | tiles × slices | 16 × 9 × 24 (CLUSTER_TILES_X, CLUSTER_TILES_Y, CLUSTER_SLICES), the same at any resolution. |
| Slice range | near to far | From the nearest visible light's sphere (no nearer than the camera's near plane) to the farthest one's, found by lightsMain each frame. A pixel outside it has no local light. |
| Slices | growing, even for orthographic | The same (getClusterSliceDepth); an orthographic camera's may start behind it. |
| Build | two levels | tilesMain, then clustersMain, each packed in light order with no global atomics. |
| Cap | a fixed list per cluster | MAX_CLUSTER_LIGHTS (256) per cluster and 24 × 256 per tile. The lights left out are FrameStats::lightsDropped, with a warning in the log. |
| Global lights | a separate list | The front of the scene's light list; FrameStats::lightsGlobal counts them. |
| Shadowed lights | — | A view's shadowed lights (up to MAX_VIEW_SHADOWS) are shaded in their own loop with their shadows, and left out of the clusters. Every other light has no shadow. |
| Seeing it | – | The light count debug view (DebugView::LIGHT_COUNT, Debug views) shows, as a heat color, how many lights each pixel shades: the shadowed ones, the other global ones and its cluster's. The RASM window's Buffers section (Inspecting GPU resources, InspectKind::LIGHT_CLUSTERS) shows the lists themselves: each cluster's count for one depth slice, or each screen tile's (INSPECT_TILES). examples/11_many_lights opens it with --inspect <view> clusters <slice>. |
| Memory | the lists | About 6.8 MiB per shaded view and frame slot: cluster lists, tile lists, a sphere and a bit per light. |
Where it lives in code
| What | Where |
|---|---|
| The light API | Renderer::createLight, getLight, updateLight in src/scene/light.cpp; addLightToScene in src/scene/scene.cpp |
| The light table and the scene lists | writeLightRow, syncGpuLights, buildSceneLightList in src/renderer/lightClusters.cpp |
A view's lights in SceneData | writeSceneLights, called by prepareSceneView |
| The three dispatches | recordLightClusters; lightsMain, tilesMain, clustersMain in shaders/passes/lightClusters.slang |
| Layout, slices, a light's sphere | shaders/lib/lightClusters.slang (getCluster, getClusterSlice, getLightSphere) |
| The three light loops | shadePBR and shadeLight in shaders/lib/pbr.slang |
| Tests of the slices and the scene list | tests/lightClustersTests.cpp |
- The shader's falloff must be exactly 0 past a light's sphere, or a cluster that leaves the light out changes the image.
getClusterSliceandgetClusterSliceDepthexist in Slang and in C++ (for the tests): change both, and the lab on this page.- The cluster buffer's layout is in
shaders/lib/lightClusters.slangand ingetClusterBufferSize: change both. - Fill the lists in light order, never with an atomic counter, so the image stays the same from run to run.
SceneDatais a CPU↔GPU struct: a change to it follows the struct layout rules.
Further reading
- Ola Olsson, Markus Billeter and Ulf Assarsson, Clustered Deferred and Forward Shading, HPG 2012.
- Takahiro Harada, Jay McKee and Jason C. Yang, Forward+: Bringing Deferred Lighting to the Next Level, Eurographics 2012.
- Emil Persson, Practical Clustered Shading, SIGGRAPH 2015 (Just Cause 3).
- Michal Drobot, Improved Culling for Tiled and Clustered Rendering, SIGGRAPH 2017 (z-binning).
- Tiago Sousa and Jean Geffroy, The devil is in the details: idTech 666, SIGGRAPH 2016 (DOOM's clustered forward).
- Bart Wronski, Cull that cone! (2017): bounding spheres of spot light cones.