Drawing

Cascaded shadow maps

One shadow map over a big outdoor view has the same texel size everywhere: too coarse next to the camera, and wasted far away. Cascaded shadow maps cut the camera's view into slices by distance and give each slice its own map. This page explains where to cut, how to fit a map around a slice so shadows stay still, and how a pixel picks its map. It builds on Shadow mapping. The last part shows how RASM does it.

In short

  • Split the camera's view depth into 2 to 4 slices. Each slice gets its own shadow map, a cascade, with the same number of texels.
  • Near slices are small, so their texels are small: sharp shadows next to the camera, and still shadows far away.
  • The split depths mix an even split and a logarithmic one, with one number, lambda.
  • Each cascade's box surrounds a sphere around its slice, so its size does not change when the camera turns, and it moves in whole texels, so shadow edges do not crawl.
  • The pixel shader picks the cascade by the pixel's view depth, and fades the shadow out at the end of the last one.

1The problem: one map for a big view

A shadow map texel covers box width / map size world units, the same everywhere in the box. A screen pixel is different: it covers a small area near the camera and a large area far away. Compare the two at both ends of the view.

what the camera sees (from above) the shadow map's box and texels
camera near: 1 texel, many pixels → blocky far: 1 texel, about 1 pixel → fine
With one map fitted to a 60-unit view, a 2048 texel map has texels of about 5 cm. A 720-pixel-high image with a 45° field of view has pixels of 0.6 cm at 5 units: one texel covers about 9 × 9 pixels there.

Making the map bigger helps a little and costs a lot: twice the resolution is four times the memory and the drawing. What helps is spending the texels where the pixels are small.

2The idea: one map per slice of the view

Cut the camera's view into slices by distance. Give each slice its own shadow map with the same number of texels. The near slice is short and narrow, so its map covers a small area and its texels are small.

0 1 2 3
Four cascades, numbered from 0 next to the camera. Each box has the same 4 × 4 texels in this drawing (2048 × 2048 in a real one), so the texels grow with the box. The boxes overlap a little at the splits.
  1. Split the view depth from near to the shadow distance into slices.
  2. Fit a box along the light around each slice, and snap it to whole texels.
  3. Draw the scene's depth once per box, into that cascade's map.
  4. Pick the cascade per pixel by its view depth, and test it like one shadow map.

3Where to split

The split depths decide how big each slice is. Two simple rules, and a mix of them:

15 30 45 60

Even

Same length for every slice. The first slice is long, so near texels stay big.

12 60 first two end at 0.5 and 2.4

Logarithmic

Each slice is the same ratio longer: texels on screen stay the same size. The first slice is tiny.

4.9 11 22 60

Mix (lambda 0.7)

Mostly logarithmic, with some even split so the first slice is long enough to matter.

spliti = λ · logi + (1 − λ) · eveni, i = 1 … N

logi = n · (f / n)i / N

eveni = n + (f − n) · i / N

n
the camera's near plane
f
the shadow distance: the last split. Past it there is no shadow.
N
the number of cascades
λ
the mix: 0 is even, 1 is logarithmic. 0.5 to 0.8 is common.
Change the number of cascades, the mix and the distance. Find the mix where cascade 0 is short but not wasted: with lambda 1 it ends half a unit from the camera. The camera has a 45° field of view and a 16:9 image; each map has 2048 texels per side, and a unit is one meter.

4Fitting a box around a slice

A slice is a frustum piece with 8 corners. Its map needs a box along the light that holds all 8. The tightest box changes shape whenever the camera turns, and so does the texel size. Shadows then swim. A sphere around the slice does not change when the camera turns, so a box around the sphere keeps its size.

90 × 80 110 × 70

Tight box

The camera turns, the box changes size. Texels grow and shrink: edges shimmer.

96 × 96 96 × 96

Box around a sphere

The sphere does not turn, so the box keeps its size. It wastes some texels in the corners.

The smallest sphere whose centre is on the view axis has a simple formula. In view space, the slice's near corners are at depth zn, at distance rn from the axis; its far corners are at zf, at rf. The centre at depth c is as far from the near corners as from the far ones:

r r c camera zn zf rn rf h = √(tan²x + tan²y): half the diagonal of the view at depth 1 The sphere does not depend on where the camera looks.
Drawn from the side. A perspective slice widens with depth, so rn = zn · h and rf = zf · h. For an orthographic camera r is the same at both ends.

c = (zf² + rf² − zn² − rn²) / (2 (zf − zn))

c = clamp(c, zn, zf)

r = max( √((c − zn)² + rn²), √((zf − c)² + rf²) )

c
the centre's depth on the view axis. It moves to the far end when the far corners are wide: then the sphere is just the far corners' circle.
r
the radius. It depends only on the projection and the two depths, never on where the camera looks.

The box is an orthographic camera that looks along the light, with a square side of 2r. Its depth range must also hold the casters: a tree or a wall outside the slice, between the slice and the sun, still throws its shadow into the slice. So the box starts well before the sphere, toward the light.

slice caster the box reaches back toward the light the sphere
The caster is outside the slice and outside its sphere, but inside the box, so it is drawn into the map. A box that started at the sphere would miss it, and the shadow would be cut off.

5Stable shadows: move in whole texels

The box follows the camera every frame. If it moves by part of a texel, the texel grid slides over the world, and every shadow edge is sampled at new places: edges crawl and flicker while the camera walks. Moving the box only in whole texels, along the light's own x and y, keeps the grid fixed in the world.

frame 1 frame 2: moved 0.5 texel

Free move

The grid slides over the world. Edge texels change every frame: crawling edges.

frame 1 frame 2: moved 1 texel

Whole texels

The grid stays on the same world lines. The same texels are dark: still edges.

t = 2 r / size

x′ = ⌊x / t⌋ · t, y′ = ⌊y / t⌋ · t

t
one texel in world units
x, y
the sphere's centre in the light's frame (its right and up axes)
x′, y′
the box's centre: on the texel grid of a frame that never moves
Both halves are needed The snap keeps the grid's position fixed; the sphere keeps the texel's size fixed. A snapped tight box still shimmers when the camera turns, because its texels change size.

6Picking the cascade in the shader

The main pass knows every cascade's matrix and split depth. For each pixel:

  1. Depth the pixel's distance along the camera's axis.
  2. Choose the first cascade whose split is beyond it.
  3. Test in that map: normal offset, bias, PCF.
  4. Off the edge? Use the next, bigger cascade.
  5. Fade out near the shadow distance.
full none 0.9 f f 0 1 2 3 depth the cascades under the graph end at their splits (4.9, 10.7, 22 and 60 for lambda 0.7)
The shadow's strength against the pixel's view depth. Past the distance f there is no map, so the point is lit.
Neighbouring pixels can pick different cascades So the shader cannot read "the cascade's texture" through one index that differs per pixel, unless it marks the index as non-uniform (NonUniformResourceIndex in HLSL and Slang), which some compilers turn into extra requirements. A loop over the cascades with a constant count, unrolled, avoids it: each copy of the loop body reads one fixed cascade, whose texture index is the same for every pixel of the draw.

7Worked example

A camera with a 45° vertical field of view, a 16:9 image 720 pixels high and a near plane at 0.1. The shadow reaches 60 units, with 4 cascades of 2048 × 2048, and lambda 0.7. Then h = √(0.414² + 0.736²) = 0.845.

4.87end of cascade 00.7 · 0.1 · 600¼ + 0.3 · 15.08
10.73end of cascade 10.7 · 2.45 + 0.3 · 30.05
21.99end of cascade 2 (cascade 3 ends at the distance, 60)0.7 · 12.12 + 0.3 · 45.03
0.41 cmtexel of cascade 0 (r = 4.16)2 · 4.16 / 2048
0.89 · 1.81 · 4.95 cmtexels of cascades 1, 2, 3 (r = 9.07, 18.6, 50.7)2 r / 2048
5.0 cmtexel of one map over all 60 units (r = 51.4)12 times cascade 0's
1.4 texelsper pixel at the end of each cascadepixel = 2 z · 0.414 / 720
64 MiBmap memory, 32-bit depth4 · 2048 · 2048 · 4 bytes

Cascades 1 to 3 have their centre at their far end (c = zf): their far corners are wider than the slice is long. Cascade 0 is the only one where the sphere's centre sits inside the slice (c = 4.26).

8Pitfalls

Shadow edges crawl when the camera moves
The box is not snapped to whole texels.
Shadow edges shimmer when the camera turns
The box is fitted tightly to the slice, so its size changes. Fit it around a sphere.
A shadow is cut off, or a caster's shadow is missing
The box starts at the sphere, so casters between the slice and the light are clipped. Extend it toward the light.
A visible line where the shadow changes sharpness
The seam between two cascades. A higher resolution, a lambda closer to even, or blending the two cascades over a short band hides it.
The shadow ends in a hard line far away
No fade at the shadow distance.
Blocky shadows next to the camera even with cascades
Lambda too low (cascade 0 is too long), or too high (cascade 0 ends before anything visible, and cascade 1 does the work).
Acne in the far cascades only
A bias in world units. Bias in texels of the cascade that is read: its texels are bigger.
Shadows flicker between frames for no reason
The pass that draws a cascade and the pass that reads it use different matrices, for example one frame apart. Compute both from the same camera in the same frame.

9In RASM

RASM has cascaded shadows for directional lights; a shaded view reads up to MAX_VIEW_SHADOWS (4) shadows. Renderer::addShadowPasses makes the cascades: one depth-only render target and one set of scene passes per cascade, all drawn from the application's camera. It returns a SceneShadow, which addScenePasses takes for the view that is shaded with it. No light camera exists: every view of the shadow fits the cascades again each frame, with the same function, so the maps are drawn and read with the same matrices. examples/06_shadows shows it. In a list of views, View::shadows fits each view's cascades to its own camera; views of different cameras draw one set of maps in turn (Multiple views), and examples/20_views shows four of them.

const rasm::SceneShadow shadow = engine.addShadowPasses(graph, scene, camera, rasm::ShadowDesc{.light = sun, .cascadeCount = CASCADE_COUNT, .size = 2048, .distance = 60.0f, .splitLambda = 0.7f});
if (shadow.mapCount == 0 || !engine.addScenePasses(graph, scene, camera, sceneTarget, &shadow, 1))
    return 1;
the application and the CPU side graph passes textures shader steps
application · once, when building the graph addShadowPasses(graph, scene, camera, ShadowDesc{light, cascadeCount, ...}) addScenePasses(graph, scene, camera, sceneTarget, &shadow, 1) every frame, each view's prepareSceneView getShadowMaps: getShadowSplits, then fitShadowCascade per cascade (sphere, box along the light, snap) GPU · in graph order Cascade 0 … 3 cull + depth-only draws shadow_cascade_0…3 readDepth Cull, Scene (rasm.slang) SceneData::shadowMaps: matrix, texture, split fragMain → sampleShadow → sampleShadowMap (shaders/lib/shadow.slang) view depth → first split beyond it offset, PCF; off the edge → next fade over the last tenth of the distance lit 0..1 → shadePBR × that light (shadows[i]) Four cascades and the shaded view use 5 of the 32 scene views (MAX_SCENE_VIEWS).
Each cascade is an ordinary scene view of the application's camera, culled on the GPU with the cascade's own matrices. Its passes are named Cascade 0 Cull 1, Cascade 0 1 and so on in the GPU timers and the graph dump. A cascade takes each caster's LOD from the camera and from the shaded view's target size, so a surface is shadowed by the same triangles it is drawn with (LOD selection).

Where RASM follows this page, and where it differs

PartThis pageRASM
Lightsa directional lightOne directional light per shadow, ShadowDesc::light; a view reads up to MAX_VIEW_SHADOWS (4) shadows. Spot and point lights have shadows of their own kind (Spot and point light shadows). The ambient term is not shadowed.
Cascades2 to 41 to MAX_SHADOW_CASCADES (4), cascadeCount; each a size × size depth-only target, shadow_cascade_0 and up, made in the graph.
Splitsthe lambda mixThe same formula in getShadowSplits, from the camera's near plane to distance, with splitLambda. An orthographic camera whose near plane is at or behind it gets the even split only. When the near plane has moved to distance or beyond, there is no shadow and the cascades draw nothing.
Fita sphere on the view axisfitShadowCascade: the formula above, from the camera's projection fitted to the shaded view's target. The light's up axis is world +y, or +z when the light is almost vertical.
Casters toward the lightextend the boxThe box starts distance before the sphere, toward the light.
Snapwhole texelsYes: the sphere's centre, rounded down to whole texels in the light's frame, in world space and in doubles. The views draw in their camera's render space, whose origin moves with the camera; a grid snapped there would move with it (large worlds). The cascade's view is a world-space glm::dmat4 that each view moves into its render space.
Who fitsonce per frameprepareSceneView of every view of the shadow calls getShadowMaps with the same inputs, so the drawing and the reading views agree.
Pickingby view depthsampleShadow: the first cascade whose split (in SceneData::shadowMaps) is at least the view depth; a lookup that sampleShadowMap finds off the map goes on to the next. Unrolled over MAX_SHADOW_CASCADES.
Filtering, biasas for one mapPer cascade, from Shadow mapping: 1 to 3 texels along the normal and one toward the light, 3x3 PCF with bilinear weights. Both offsets in the texels of the cascade that is read.
Seamsblend over a bandNo blend: the cascade changes at the split. A fade over the last tenth of distance.

Where it lives in code

WhatWhere
The APIShadowDesc, SceneShadow, MAX_SHADOW_CASCADES in include/rasm/scene.h; Renderer::addShadowPasses, Renderer::addScenePasses in src/renderer/gpuScene.cpp
Split and fit mathgetShadowSplits, fitShadowCascade, ShadowCascade in src/renderer/shadowCascades.cpp; tested in tests/shadowCascadesTests.cpp
Fitting every framegetShadowMaps and Renderer::prepareSceneView in src/renderer/gpuScene.cpp
GPU fieldsshadowMaps in SceneData (a cascade's matrix, texture and split) and the light's ShadowedLight in shadowedLights (firstMap, mapCount): src/renderer/uberMaterialTypes.h and shaders/lib/core.slang
The lookupsampleShadow and sampleShadowMap in shaders/lib/shadow.slang; applied by shadePBR in shaders/lib/pbr.slang
An exampleexamples/06_shadows/main.cpp: four cascades, and an inset with the four maps
Keep these true
  • The views that draw the cascades and the view that reads them must fit with the same inputs in the same frame: the same camera, the same aspect ratio, the same SceneShadow. addScenePasses rejects a shadow that addShadowPasses did not make for the same scene and camera in the same graph, and a second view of the same camera that reads the same shadow. A view of another camera may read the same maps only after it drew them again from its own camera: the graph orders those draws after the earlier readers.
  • The sphere's radius must not depend on the camera's direction, and the box must move in whole texels, or shadows shimmer and crawl.
  • MAX_SHADOW_CASCADES in include/rasm/scene.h and in shaders/lib/core.slang must match.
  • The cascade loop in sampleShadow stays unrolled, so each texture index is uniform over the draw.
  • The offsets read a texel's size from row 0 of a cascade's matrix and the point's clip w, which is 1 in the orthographic box.

Further reading