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.
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.
- Split the view depth from near to the shadow distance into slices.
- Fit a box along the light around each slice, and snap it to whole texels.
- Draw the scene's depth once per box, into that cascade's map.
- 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:
Even
Same length for every slice. The first slice is long, so near texels stay big.
Logarithmic
Each slice is the same ratio longer: texels on screen stay the same size. The first slice is tiny.
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.
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.
Tight box
The camera turns, the box changes size. Texels grow and shrink: edges shimmer.
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:
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.
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.
Free move
The grid slides over the world. Edge texels change every frame: crawling edges.
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
6Picking the cascade in the shader
The main pass knows every cascade's matrix and split depth. For each pixel:
- Depth the pixel's distance along the camera's axis.
- Choose the first cascade whose split is beyond it.
- Test in that map: normal offset, bias, PCF.
- Off the edge? Use the next, bigger cascade.
- Fade out near the shadow distance.
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.
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;
Where RASM follows this page, and where it differs
| Part | This page | RASM |
|---|---|---|
| Lights | a directional light | One 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. |
| Cascades | 2 to 4 | 1 to MAX_SHADOW_CASCADES (4), cascadeCount; each a size × size depth-only target, shadow_cascade_0 and up, made in the graph. |
| Splits | the lambda mix | The 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. |
| Fit | a sphere on the view axis | fitShadowCascade: 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 light | extend the box | The box starts distance before the sphere, toward the light. |
| Snap | whole texels | Yes: 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 fits | once per frame | prepareSceneView of every view of the shadow calls getShadowMaps with the same inputs, so the drawing and the reading views agree. |
| Picking | by view depth | sampleShadow: 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, bias | as for one map | Per 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. |
| Seams | blend over a band | No blend: the cascade changes at the split. A fade over the last tenth of distance. |
Where it lives in code
| What | Where |
|---|---|
| The API | ShadowDesc, SceneShadow, MAX_SHADOW_CASCADES in include/rasm/scene.h; Renderer::addShadowPasses, Renderer::addScenePasses in src/renderer/gpuScene.cpp |
| Split and fit math | getShadowSplits, fitShadowCascade, ShadowCascade in src/renderer/shadowCascades.cpp; tested in tests/shadowCascadesTests.cpp |
| Fitting every frame | getShadowMaps and Renderer::prepareSceneView in src/renderer/gpuScene.cpp |
| GPU fields | shadowMaps 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 lookup | sampleShadow and sampleShadowMap in shaders/lib/shadow.slang; applied by shadePBR in shaders/lib/pbr.slang |
| An example | examples/06_shadows/main.cpp: four cascades, and an inset with the four maps |
- 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.addScenePassesrejects a shadow thataddShadowPassesdid 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_CASCADESininclude/rasm/scene.hand inshaders/lib/core.slangmust match.- The cascade loop in
sampleShadowstays 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
- Rouslan Dimitrov, "Cascaded Shadow Maps", NVIDIA, 2007.
- Fan Zhang and others, "Parallel-Split Shadow Maps for Large-scale Virtual Environments", 2006: the lambda split.
- Microsoft, "Cascaded Shadow Maps" (Windows DirectX documentation): fitting, and snapping to texels.
- Michal Valient, "Stable Rendering of Cascaded Shadow Maps", ShaderX6, 2008.
- Elmar Eisemann and others, Real-Time Shadows (2011).