skip to content

How do stencil shadow volumes mark shadowed pixels, and why did shadow mapping replace them in most real-time renderers?

level: middleimportance: nice to knowfreq 33%

answer

  1. extrude the silhouette away from the light
  2. count entries and exits
  3. depth pass versus depth fail
  4. exact edges, heavy fill

basics

~20 s

Each caster's silhouette is extruded away from the light into a volume; drawing its faces into the stencil buffer counts the volumes each visible point lies inside, and a nonzero count means shadow. Edges are exact, but fill rate is heavy.

solid answer

~50 s

A shadow volume is the region a caster hides from the light. Find the **silhouette edges**, where a light-facing triangle meets one facing away, and extrude them away from the light, capping the ends. After the scene's depth is laid down, draw the volumes with depth writes off. With **depth pass**, front faces in front of the visible surface increment the stencil and back faces in front of it decrement, so a surface inside a volume ends at a nonzero count. That breaks when the camera is inside a volume, so **depth fail** counts faces behind the surface instead: back faces increment, front faces decrement, which needs capped volumes and a far plane at infinity or depth clamping. Shadows are pixel-exact with no bias. Shadow maps won because volumes need closed meshes and silhouette extraction, burn fill rate per light, give only hard edges and ignore alpha-tested holes.

go deeper

for a junior

Recall that shadow volumes extrude silhouettes away from the light and use the stencil buffer to mark pixels inside a volume.

for a middle

Explain the increment and decrement rules for depth pass and depth fail, why the camera-inside case breaks depth pass, and what caps and an infinite far plane are for.

for a senior

Compare costs on real content: fill rate per light, closed-mesh and connectivity requirements, alpha-tested casters, and why soft shadows push toward shadow maps.

for a principal

Judge when exact hard shadows on simple closed geometry would justify volumes today, against keeping one shadow-map pipeline for all content.

## The idea: shadow as a region of space Shadow mapping samples the light's view into a texture. **Shadow volumes** describe the shadow geometrically instead: for each caster and light, build the closed region of space the caster hides from the light, then ask, per pixel, whether the visible surface lies inside any such region. The answer comes from counting, and the **stencil buffer**, a per-pixel integer that draws can increment and decrement, does the counting. ## Building a volume 1. **Classify triangles** of the caster as facing the light or facing away. 2. **Find the silhouette**: edges shared by one light-facing and one away-facing triangle. This needs connectivity, so the mesh must be **closed** (every edge shared by exactly two triangles). 3. **Extrude** each silhouette edge into a quad stretching away from the light, usually to infinity using homogeneous points with `w = 0`. 4. **Cap** the volume when the method needs it: the light-facing triangles form the near cap, and the away-facing triangles pushed to infinity form the far cap. Silhouettes change whenever the caster or light moves, so the extraction runs every frame, on the CPU or in a geometry stage on the GPU. ## Counting with the stencil buffer First render the scene's depth (and usually ambient light). Then draw each volume's faces with **depth writes off** and colour writes off, updating only the stencil: | Method | Faces counted | Front faces | Back faces | Weakness | |---|---|---|---|---| | Depth pass | those **in front of** the visible surface | increment | decrement | wrong when the camera is inside a volume | | Depth fail | those **behind** the visible surface | decrement | increment | needs both caps and an infinite far plane | With **depth pass**, a ray from the eye enters a volume through a front face and leaves through a back face. If the visible surface lies inside, the ray has entered but not yet left, so the count is nonzero. When the camera itself is inside a volume, the entering face is behind the near plane and gets clipped, and every count is off by one. **Depth fail** counts from the other end of the ray, from infinity back to the surface, which the near plane never clips. The far cap must not be clipped either, so the projection's far plane is pushed to infinity or depth is clamped. Two-sided stencil lets one draw update front and back faces with different operations. Finally, the light's contribution is added only where the stencil count is zero, one light at a time. ## Why shadow maps replaced them - **Fill rate.** Extruded quads cover huge screen areas, and every pixel of every volume face is rasterized, per light. Long volumes from a low sun can cost more than the scene. - **Geometry demands.** Closed meshes, connectivity data and per-frame silhouette extraction rule out much real content, and the cost scales with the triangle count of casters. - **Hard edges only.** The count is an integer, so penumbrae need extra techniques; shadow maps filter naturally. - **Alpha-tested casters.** Foliage cut out by a texture casts the shadow of its whole card, because the volume follows triangles, not texels. - **Scaling.** Shadow-map cost follows resolution and caster count, and a map can be reused across frames or cascaded; volumes have no such dials. What volumes keep is **exactness**: no resolution, no aliasing, no bias, no acne or peter-panning, at any distance. That made them attractive for hard-shadowed interiors with few lights and simple closed geometry. | Property | Shadow volumes | Shadow maps | |---|---|---| | Edge quality | pixel-exact, hard | resolution-limited, filterable | | Bias needed | no | yes | | Caster requirements | closed meshes, no alpha cutouts | anything the rasterizer can draw | | Main cost | fill rate per light | casters re-rendered per light, map memory | | Soft shadows | difficult | PCF, PCSS, prefiltering | ## Where the counting idea survives - **Light-volume masks.** Some deferred renderers use the same front-face and back-face stencil test to mark the pixels inside a light's bounding volume before shading them. - **Screen-space contact shadows** solve a related problem, small exact occlusion near contacts, but by ray marching the depth buffer rather than counting volumes.

  • Why does depth-pass counting fail when the camera is inside a shadow volume?
    Depth pass counts the volume faces a view ray crosses before it reaches the visible surface. If the camera already sits inside a volume, the face the ray should have entered through lies behind the camera or the near plane and is clipped, so the count misses one entry. Shadowed surfaces read zero and lit ones read minus one, inverting the shadow.
  • Why must the volumes be built from closed meshes?
    Counting works only if every view ray that enters a volume also has a matching exit face. Silhouette extraction needs every edge shared by two triangles to decide where the light-facing side ends. A hole or an open edge leaves a gap in the volume, so counts go wrong along rays that pass through it and shadows streak across the screen.

  • Shadow volumes suffer from acne and need a depth bias, just like shadow maps.
  • Shadow volumes cannot give pixel-exact hard shadow edges.
  • Depth-pass counting works correctly wherever the camera is placed.
  • Alpha-tested foliage casts correct cutout shadows with shadow volumes.
  • Shadow volumes cost nothing extra in fill rate because they write no colour.