How does shadow mapping decide whether a pixel is in shadow, from rendering depth from the light to the per-pixel comparison?
answer
- two passes, one from the light
- what the light sees first
- same light view-projection, both passes
- receiver depth versus stored depth
basics
~20 sShadow mapping first renders depth from the light into a texture. When shading, each pixel's point is projected into the light's view and its depth compared with the stored depth; if it is farther, something blocks the light.
solid answer
~50 sIt takes two passes. First, render the shadow casters from the light's point of view into a depth-only target: an orthographic projection for a directional light, a perspective one for a spot light, six cube faces for a point light. Then, in the main pass, transform each shaded point by the same light view-projection, turn the result into shadow-map texture coordinates plus a light-space depth, and read the stored depth there. With depth growing away from the light, the point is in shadow when its depth is greater than the stored one, because a surface nearer the light was recorded first. Many GPUs offer a comparison sampling mode that does the test in the texture unit and filters the 0-or-1 results. The test works for any rasterizable caster, but the map's finite resolution brings aliasing and self-shadowing that bias and filtering must handle.
code
pseudocode · 11 lines// main pass, per shaded point; depth grows away from the light, depth range 0..1
p = lightViewProj * [worldPos, 1]
ndc = p.xyz / p.w // w stays 1 for a directional light's orthographic projection
uv = ndc.xy * 0.5 + 0.5 // some conventions also flip v here
receiverDepth = ndc.z
if uv.x < 0 or uv.x > 1 or uv.y < 0 or uv.y > 1 or receiverDepth > 1:
return LIT // outside the shadow map: no caster was recorded
storedDepth = shadowMap.read(uv)
if receiverDepth - bias > storedDepth:
return SHADOWED // a surface nearer the light was recorded here
return LITgo deeper
Recall the two passes: depth from the light, then a per-pixel comparison in light space. Know which projection each light type uses and which way the comparison goes.
Walk through the transform into the light's space, the 0..1 remap and the bias in the comparison. Explain why off-screen casters must still be drawn.
Tie each artefact to its cause: acne to one depth per texel, jagged edges to texels per screen pixel, crawling to refitting the light frustum, and say what each remedy costs.
Frame shadows as a budget: casters re-rendered per light and face, memory per map, and which lights earn a shadow at all when dozens are visible.
## The idea: record what the light sees A point is in shadow when something sits between it and the light. Testing that directly means a ray query per pixel against the whole scene. **Shadow mapping** replaces the query with a lookup: render the scene once *from the light*, keep only the distance to the first surface along each direction, and later ask whether a shaded point lies farther than that first surface. This answer assumes the usual convention that **depth grows away from the light** (0 at the light's near plane, 1 at its far plane). With reversed depth every comparison below flips. ## Pass 1: rendering depth from the light The shadow pass draws every potential **caster** into a depth-only render target, the **shadow map**. No colour is written and the fragment work is minimal, so the pass is bounded by vertex work and depth writes. The projection depends on the light type: - **Directional light** (the sun): all rays are parallel, so an **orthographic** projection covers a box of the world. - **Spot light**: rays leave one point inside a cone, so a **perspective** projection whose field of view matches the cone. - **Point light**: rays leave one point in every direction. One perspective frustum cannot cover that, so the usual answer is a **cube** of six 90-degree faces, or two hemispheres with a paraboloid mapping. The light's frustum must include casters the camera cannot see. A tree behind the camera can still throw its shadow onto ground in view, so culling the shadow pass with the camera frustum would delete visible shadows. ## Pass 2: the comparison While shading a pixel in the main pass: 1. Take the pixel's world position. 2. Transform it by the **same light view-projection** the shadow pass used and divide by `w` (which stays 1 for an orthographic light). 3. Map the resulting x and y from the `-1..1` range to `0..1` texture coordinates (some conventions also flip v), and take z, in the depth range the shadow pass wrote, as the **receiver depth**. 4. Read the **stored depth** at those coordinates. 5. If the receiver depth is greater than the stored depth (minus a small bias), a surface nearer the light occupies that direction: the point is **shadowed**. Otherwise it is lit. Points that land outside the map's coverage, or beyond its far plane, had no caster recorded and are normally treated as lit. Many GPUs offer a **comparison sampling mode**: the texture unit compares the receiver depth against several neighbouring texels and returns the filtered fraction of passed tests. That is the hardware form of percentage-closer filtering, and it softens the stair-stepped edge a single test gives. ## What the stored depth cannot tell you The map holds **one depth per texel**, the nearest surface to the light. That has consequences: - A semi-transparent caster cannot be partly recorded; it is either written or not (alpha-tested casters discard fragments to cut holes). - Everything behind the first surface is equally shadowed; the map knows nothing about a second occluder. - The texel grid is fixed in light space while screen pixels are fixed in camera space, so one texel can cover many screen pixels near the camera. ## The usual artefacts | Artefact | Cause | Usual remedy | |---|---|---| | Shadow acne (stripes on lit surfaces) | one stored depth per texel versus a sloped receiver | constant and slope-scaled bias, normal offset | | Peter-panning (shadow detached from its caster) | too much bias | less constant bias, normal offset, contact shadows | | Jagged or blocky edges | too few texels per screen pixel | higher resolution, cascades, filtering | | Edges that crawl as the camera moves | the light's frustum is refit each frame | stabilize the fit and snap to texels | ## What it costs - One extra render of the casters per shadowed light (six for a cube map), which often makes shadows one of the largest geometry costs in a frame. - Shadow-map memory: a 2048 by 2048 map with 32-bit depth is 2048 * 2048 * 4 bytes, 16 MiB. - One or more texture reads per shaded pixel per light in the main pass. The method's strength is generality: anything the rasterizer can draw, including alpha-tested foliage and open meshes, can cast a shadow, and the cost scales with resolution and caster count rather than with scene topology.
- Why does a point light usually need six shadow-map renders while a spot light needs one?A perspective projection covers well under 180 degrees, so a light that shines in every direction is covered by a cube of six 90-degree frusta, one per face, each rendered like a spot-light map. The lookup picks the face from the direction between light and point. Two hemispheres with a paraboloid mapping cut it to two renders but bend straight edges, which needs well-tessellated casters.
- Why must a directional light's shadow pass include casters outside the camera's view?Shadows come from whatever sits between the light and the visible receivers. A cliff behind the camera can shadow the beach in front of it, so the light frustum is extended toward the light to cover those casters, or casters in front of its near plane have their depth clamped to it so they still write a value.
- What does a comparison sampling mode return, compared with an ordinary depth read?An ordinary read returns a stored depth, which the shader then compares. A comparison read takes the receiver depth as an input, compares it against the neighbouring texels the filter touches, and returns the filtered fraction of tests passed, a value between 0 and 1. That gives a softened edge for one fetch on many GPUs.
A watchtower guard writes down, for every direction, how far away the first thing they can see is. Later, to know whether a spot is hidden from the tower, you check whether it lies farther along its direction than the distance the guard wrote down.
- The shadow map stores how much light reaches each surface, like a lightmap.
- You compare the pixel's camera-space depth directly with the value in the shadow map.
- Casters outside the camera frustum can be skipped when rendering the shadow map.
- A high enough shadow-map resolution removes the need for any depth bias.
- Shadow mapping needs closed, watertight meshes to cast correct shadows.