How do percentage-closer filtering (PCF) and PCSS soften shadow-map edges, and why are a shadow map's comparisons filtered rather than its depths?
answer
- the test is a step function
- compare first, average second
- fixed width versus contact-hardening
- blocker search, then similar triangles
basics
~20 sPCF compares the receiver against several nearby shadow-map texels and averages the pass-or-fail results, giving a fixed-width soft edge. PCSS first averages nearby blocker depths to size the penumbra, then scales the PCF kernel to match. Averaged depths would mean nothing.
solid answer
~50 sA shadow test is a step function of depth, so blurring the stored depths before comparing produces averaged depths that no surface has, and the single comparison still returns 0 or 1. **Percentage-closer filtering** reverses the order: compare the receiver's depth against each of N texels around its lookup, then average the outcomes into a lit fraction. Many GPUs do a bilinear-weighted 2x2 version in one comparison fetch; wider kernels take several, often a Poisson disc rotated per pixel. PCF's penumbra has the same width everywhere. **Percentage-closer soft shadows** make it vary: a blocker search averages the depths of texels nearer the light than the receiver, similar triangles give a penumbra width of `(dReceiver - dBlocker) * lightSize / dBlocker`, and PCF runs with a kernel that wide. Shadows stay sharp at contact and soften with distance, at the price of two sample loops.
code
pseudocode · 22 lines// directional light, orthographic shadow map, depth grows away from the light (world units from its near plane)
// lightWidthPerDepth = 2 * tan(angularRadius); mapWorldWidth = world width the map covers; offsets lie in the unit disc
function pcssLitFraction(uv, receiverDepth):
searchRadius = min(0.5 * receiverDepth * lightWidthPerDepth / mapWorldWidth, maxSearchUv)
sum = 0
count = 0
for each offset in searchDisc:
d = shadowMap.read(uv + offset * searchRadius)
if d < receiverDepth - bias: // nearer the light than the receiver: a blocker
sum = sum + d
count = count + 1
if count == 0:
return 1.0 // no blocker in reach: fully lit
blockerDepth = sum / count
filterRadius = 0.5 * (receiverDepth - blockerDepth) * lightWidthPerDepth / mapWorldWidth
filterRadius = clamp(filterRadius, oneTexelUv, maxFilterUv)
lit = 0
for each offset in filterDisc:
d = shadowMap.read(uv + offset * filterRadius)
if receiverDepth - bias <= d: // this sample does not block
lit = lit + 1
return lit / size(filterDisc)go deeper
Recall that PCF means comparing against several texels and averaging the results, and that PCSS adds a step that makes shadows sharper near contact.
Explain why filtering depths fails, walk the PCF loop, and derive the PCSS penumbra width from similar triangles between light, blocker and receiver.
Discuss what each costs per pixel per light, where PCSS's averaged blocker misleads, and how wide kernels bring acne back on slopes.
Choose a soft-shadow tier per light and cascade under a frame budget, and decide when prefiltered maps beat per-pixel kernels.
## Why depths cannot be filtered like colours This answer assumes **depth grows away from the light**. A colour texture can be blurred because the blurred colour is what you want to see. A shadow map is different: what you want is the *result of a comparison*, and the comparison is a step function of depth. Filtering its input does not filter its output. Take a receiver at depth 0.6 near a shadow edge. Two neighbouring texels hold 0.3 (the caster) and 0.9 (the ground beyond it): - **Filter the depths, then compare**: the average is 0.6, the comparison `0.6 > 0.6` is false, and the result is fully lit. The averaged depth belongs to no real surface, and the answer is still a hard 0 or 1. - **Compare, then filter**: `0.6 > 0.3` is shadowed, `0.6 > 0.9` is lit, and the average is 50 percent lit, which is what a pixel straddling the edge should get. That second order is **percentage-closer filtering (PCF)**, introduced in 1987 by Reeves, Salesin and Cook. ## Percentage-closer filtering For each shaded point: 1. Project it into the light's view to get a lookup position and a receiver depth. 2. Read N texels in a kernel around the lookup. 3. Compare the receiver depth (minus bias) against each one. 4. Average the 0-or-1 results, optionally weighted, into a lit fraction. Practical points: - Many GPUs perform a **bilinear-weighted 2x2 PCF** in a single comparison fetch, so a 3x3 grid of such fetches covers a 4x4 texel footprint with smooth weights. - Cost grows with kernel area: a 5x5 grid of single comparisons is 25 tests per pixel per light. Sparse **Poisson-disc** patterns rotated by a per-pixel angle cover a wide area with fewer samples, trading banding for noise that temporal filtering later removes. - A wide kernel compares the receiver's single depth against texels far from the lookup. On a sloped receiver those texels belong to the same surface at different depths, so acne returns unless bias grows with the kernel, or the receiver depth is adjusted per sample from its depth gradient. PCF gives every shadow the **same penumbra width** in texels, which looks wrong: real penumbrae are sharp where a caster touches the receiver and widen with distance. ## Percentage-closer soft shadows PCSS estimates how wide the penumbra should be, then runs PCF with that width: 1. **Blocker search.** Read texels in a search region around the lookup and average the depths of those nearer the light than the receiver. If none are, the point is fully lit and the work stops. 2. **Penumbra estimate.** Treat the light as an area of width `wLight`, the blockers as a plane at `dBlocker` and the receiver as a plane at `dReceiver`, both measured from the light. Similar triangles from the light's edges past the blocker edge give `wPenumbra = (dReceiver - dBlocker) * wLight / dBlocker`. 3. **Filtering.** Run PCF with a kernel scaled to that width, projected into shadow-map space. For a directional light such as the sun, rays are parallel and the light is described by its angular size instead. The penumbra then grows linearly with the gap: `wPenumbra = (dReceiver - dBlocker) * 2 * tan(angularRadius)`, all distances in world units. ## Where PCSS breaks down - **Averaged blockers.** When the search region holds blockers at very different depths, their average describes no real occluder, so the penumbra is too wide for one and too narrow for the other. - **One depth layer.** The map keeps only the nearest blocker, so blockers hidden behind it never enter the estimate. - **Cost.** Both loops run per pixel per light, and a large penumbra needs a large kernel. Renderers cap the search and filter radii, accept noise and rely on temporal accumulation, or use PCSS only on the nearest cascade. ## Comparing the options | Technique | Penumbra | Cost per pixel | Typical use | |---|---|---|---| | Single comparison | none, hard and aliased | one fetch | rarely shipped alone | | PCF | fixed width | one fetch per sample | default soft edge | | PCSS | grows with caster-receiver gap | search plus filter | hero lights, near cascades | | Prefiltered maps (variance, exponential) | blurred once per texel | constant lookup | wide soft shadows, with leaking | The last row is the alternative route: store a statistic of depth that *can* be filtered, so the blur happens once per texel instead of once per pixel per sample.
- Why can a wide PCF kernel bring shadow acne back on sloped receivers?Every sample in the kernel is compared against the receiver's depth at the centre. On a sloped receiver, texels far from the centre store the same surface at a different depth, and those on the far side of the slope test as blockers. Fixes are more slope-scaled bias for wide kernels, normal offset, or a receiver-plane depth bias that adjusts the receiver depth per sample from its gradient in shadow-map space.
- Why do renderers often run PCSS only on the nearest cascade or on a few key lights?PCSS pays for a blocker search and a filter loop per pixel per light, and wide penumbrae need wide kernels to avoid noise. Far cascades have texels so large that a fixed PCF kernel already looks soft, and minor lights rarely show contact-hardening, so the extra loops buy little there.
- Blurring the shadow map's depths gives soft shadows just as blurring a colour texture does.
- PCF produces physically correct penumbrae that widen with distance from the caster.
- PCSS finds the blocker by tracing rays against the scene's triangles.
- A wider PCF kernel never needs more bias than a single comparison.
- Hardware comparison sampling returns a filtered depth, which the shader then compares.