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.

one point 40 lights in the scene a circle: how far one light reaches 3 reach the point the other 37 add exactly 0, but the loop still runs them
Only the lights whose circle covers a point light it. The loop over all lights spends most of its time on lights that add nothing.
2.07 Mpixels at 1920 × 1080before overdraw
4096lights in the scenestreet lamps, windows, sparks
8.5 Glight evaluations per frame, all lights at all pixels2.07 M × 4096
~30lights that reach a pixel, in a lit streetthe work that matters

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 θ:

r

Point light

A sphere of radius r, centered on the light.

θ ≤ 45°: through the apex and the rim

Narrow spot

Center r / (2 cos θ) along the axis, the same radius.

θ > 45°: around the rim's circle

Wide spot

Center r cos θ along the axis, radius r sin θ.

A light without a range reaches everything The sun, and a lamp left with an unbounded 1 / d² falloff, have no sphere. No box can leave them out, so they go into a short global list that every pixel shades. Hundreds of global lights cost what hundreds of lights cost without clusters.

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:

a near pixel's list: all 5 lights

One tile: a whole column

Where the column crosses near and far objects, or a long street, the list is long.

the near pixel's cluster: 1 light

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.

A side view of a camera's view, cut into tiles (rows) and slices (columns). Drag the bright light, change the range, and switch between even and growing slices. Each cluster shows how many lights touch it.

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.

  1. Visible lightseach light's sphere in view space, tested against the frustum
  2. Tile listsa group per tile: the spheres that touch the column's 4 side planes
  3. Cluster listsa thread per cluster: its tile's spheres that touch its box
  4. 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.

the cluster's box: its corners at the slice's 2 depths A: the closest point is inside the sphere: kept B: farther than r: left out
The closest point of the box to the center is the center clamped to the box. The light is in the cluster when that point is within its radius.

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.

3456clusters16 × 9 tiles × 24 slices
256lights per cluster, at mostthe cap
3.4 MiBfor the cluster lists3456 × 256 × 4 bytes
6144lights per tile, at most24 slices × 256: more cannot fit its clusters

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

lights

Clustered deferred

The same lists, read by a lighting pass over a G-buffer.

Memory for the G-buffer

a bin: lights 2 to 3

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

a bit per light

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

CPU lists

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.

CPU, only on a change buffers compute, each frame fragment shader
createLight / updateLight one row, GpuTable::setRow addLightToScene rebuild that scene's list light table 56 B per light, 2 frame slots scene light list [ global | local ] rows Cull 1 pass, timer "Light Clusters" lightsMain 1 group: visible tilesMain 144 groups: tiles clustersMain 3456 threads clusters ≤ 256 rows each fragMain → shadePBR 1. shadowed lights (≤ 4) 2. global lights 3. its cluster's lights same sums with or without
The CPU touches a light only when it changes. The GPU builds the clusters in the view's first cull pass, and every draw pass of the view, the Transparent one too, reads them.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Gridtiles × slices16 × 9 × 24 (CLUSTER_TILES_X, CLUSTER_TILES_Y, CLUSTER_SLICES), the same at any resolution.
Slice rangenear to farFrom 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.
Slicesgrowing, even for orthographicThe same (getClusterSliceDepth); an orthographic camera's may start behind it.
Buildtwo levelstilesMain, then clustersMain, each packed in light order with no global atomics.
Capa fixed list per clusterMAX_CLUSTER_LIGHTS (256) per cluster and 24 × 256 per tile. The lights left out are FrameStats::lightsDropped, with a warning in the log.
Global lightsa separate listThe 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>.
Memorythe listsAbout 6.8 MiB per shaded view and frame slot: cluster lists, tile lists, a sphere and a bit per light.
0.31 ms"Light Clusters", 4096 lights (3813 visible)1920 × 1080, RTX 3060, Release
4.5 msthe draw passes, clusteredScene 1 + 2 + Transparent
231 msthe same passes, every light at every pixel--all-lights
0pixels that differ between the two11_many_lights, several frames and cameras

Where it lives in code

WhatWhere
The light APIRenderer::createLight, getLight, updateLight in src/scene/light.cpp; addLightToScene in src/scene/scene.cpp
The light table and the scene listswriteLightRow, syncGpuLights, buildSceneLightList in src/renderer/lightClusters.cpp
A view's lights in SceneDatawriteSceneLights, called by prepareSceneView
The three dispatchesrecordLightClusters; lightsMain, tilesMain, clustersMain in shaders/passes/lightClusters.slang
Layout, slices, a light's sphereshaders/lib/lightClusters.slang (getCluster, getClusterSlice, getLightSphere)
The three light loopsshadePBR and shadeLight in shaders/lib/pbr.slang
Tests of the slices and the scene listtests/lightClustersTests.cpp
Keep these true
  • The shader's falloff must be exactly 0 past a light's sphere, or a cluster that leaves the light out changes the image.
  • getClusterSlice and getClusterSliceDepth exist 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.slang and in getClusterBufferSize: change both.
  • Fill the lists in light order, never with an atomic counter, so the image stays the same from run to run.
  • SceneData is a CPU↔GPU struct: a change to it follows the struct layout rules.

Further reading