How does screen-space ambient occlusion estimate occlusion from the depth buffer, and why does it need per-pixel noise and a blur?
answer
- only what the camera saw
- hemisphere around the normal
- sample depth versus scene depth
- few samples, rotated, then blurred
basics
~20 sSSAO places a few points in a hemisphere around each pixel's surface, projects them and counts how many lie behind the depth buffer. Few samples band, so tiled noise rotates the kernel per pixel and a depth-aware blur removes the noise.
solid answer
~50 sFor each pixel SSAO reconstructs the **view-space position** from depth and takes the surface **normal**. It places a handful of points, often 8 to 16, in a **hemisphere** of a chosen radius around that normal, projects each to the screen and reads the depth buffer there. If the stored surface is **closer to the camera** than the sample point by more than a small bias, the sample is occluded; a **range check** fades occluders far in front of the pixel so a foreground character does not draw a halo on a distant wall. AO is one minus the occluded fraction and darkens ambient and indirect light. With so few samples every pixel would show the same banding, so a small tiled **noise texture** rotates the kernel per pixel, turning banding into high-frequency noise, and a **depth-aware blur**, or temporal accumulation, removes it without bleeding across edges.
code
pseudocode · 16 lines// View space looks down +z, so z is linear depth: larger z = farther from the camera.
P = viewPositionFromDepth(p)
N = viewNormal(p)
r = noiseVectors[p.x mod 4][p.y mod 4] // random rotation vector, tiled 4x4
T = normalize(r - N * dot(r, N)) // tangent perpendicular to N
B = cross(N, T)
occluded = 0
for i in 0 .. K - 1:
k = kernel[i] // point in a unit hemisphere, k.z >= 0
s = P + (T * k.x + B * k.y + N * k.z) * radius
q = projectToScreen(s)
sceneZ = linearDepthAt(q) // nearest visible surface at that pixel
if sceneZ < s.z - bias: // a visible surface lies in front of s
fade = clamp(radius / max(abs(P.z - sceneZ), 0.0001), 0, 1)
occluded = occluded + fade // occluders far in front of P count less
ao = 1 - occluded / Kgo deeper
Know that ambient occlusion darkens creases and contact areas, and that SSAO estimates it every frame from the depth buffer alone.
Walk through the hemisphere kernel, projecting samples, the depth comparison with bias and range check, and why per-pixel rotation and a depth-aware blur are needed.
Diagnose haloes, speckle, banding and view-dependent flicker from their causes, and choose resolution, radius and temporal accumulation against the frame's time budget.
Decide whether screen-space occlusion is enough for the project's look or must be combined with baked or world-space occlusion, given its view dependence.
## What ambient occlusion approximates **Ambient occlusion (AO)** is the fraction of the hemisphere above a surface point that nearby geometry blocks. Creases, corners, the gap under a crate and the contact between a foot and the floor receive less of the light arriving from all around, so they should be darker under sky light and bounced light. Offline renderers trace rays to measure it. **Screen-space ambient occlusion (SSAO)** estimates it every frame from what a full-screen pass has to hand: the **depth buffer** and usually a **normal buffer**. Throughout, depth means linear view-space distance, larger meaning farther from the camera; a non-linear or reversed depth buffer is converted to that first. ## The SSAO pass, step by step 1. **Reconstruct** the pixel's view-space position `P` from its depth and screen coordinates, and fetch its normal `N`. 2. **Orient a kernel.** A precomputed set of K points inside a unit hemisphere (K is typically 8 to 16), placed more densely near the centre so close occluders count more, is rotated into a basis around `N` and scaled by a world-space **radius**. 3. **Project** each sample point to the screen and read the scene depth stored at that position. 4. **Test.** If the stored surface is closer to the camera than the sample point by more than a small **bias**, the sample lies behind visible geometry and counts as occluded. 5. **Range check.** If that occluding surface is much nearer than `P`, by far more than the radius, fade its contribution so a foreground object does not darken a distant wall. 6. **Average**: `ao = 1 - occluded / K`. 7. **Denoise** with a blur, then **apply** `ao` to ambient and indirect lighting. ## Why noise and a blur - With 8 to 16 samples the estimate is coarse. If every pixel used the same kernel, the error would form the same pattern everywhere and show as **banding** and contour rings on flat surfaces. - A small **noise texture**, often 4x4 random vectors tiled across the screen, rotates the kernel around `N` differently for each pixel of a 4x4 block. Neighbouring pixels now sample different directions, and structured banding becomes high-frequency **noise**. - A **blur** the size of the noise tile averages 16 different rotations, approaching the quality of many more samples per pixel. A **bilateral** blur, weighted by depth and normal differences, stops occlusion smearing across object edges. - Many renderers instead accumulate SSAO over frames, reusing the jitter and history of temporal anti-aliasing to average rotations over time. ## Artefacts and their causes | Artefact | Cause | Usual fix | |---|---|---| | Dark halo on a wall around a character in front of it | Wall samples hit the character's much nearer depth | Range check that fades distant occluders | | Grey speckle on flat floors | Samples just above the surface self-occlude through depth error | Bias, and a hemisphere around an accurate normal | | Banding or contour rings | Same kernel on every pixel | Per-pixel rotation plus blur | | Occlusion appears and vanishes as the camera turns | Occluders off screen or hidden behind the front layer | Guard band, temporal smoothing or a world-space source | | Occlusion bleeding across silhouettes | Plain blur ignores depth | Depth-aware blur | ## Cost and variants - SSAO often runs at **half resolution** and is upsampled with a depth-aware filter, shading a quarter of the pixels. - **Horizon-based** variants march along a few screen directions, find the highest occluding horizon angle in each and integrate the open sky between horizons, which extracts more from each depth read than scattered points. Cosine-weighted formulations get closer to a ray-traced reference. - AO is meant to multiply **ambient and indirect** light. Direct light already has shadows, and darkening it again makes contact areas look dirty in sunlight; some productions apply a little to direct light for style, knowing it is not physically motivated. ## What screen space cannot know The depth buffer holds one layer, the nearest surface per pixel. SSAO therefore cannot see geometry behind that layer, so a thin pole in front of a wall behaves as if it were infinitely thick, and it cannot see geometry outside the screen, so occlusion fades at the edges and changes as the camera turns. Those limits are why SSAO works best as a **contact** effect at small radii, combined with baked or probe-based indirect lighting at larger scales, which is a separate topic.
- Why is SSAO applied to ambient and indirect light rather than to direct light?AO estimates how much of the surrounding hemisphere is blocked, which matters for light arriving from many directions. Direct light comes from one direction and already has its own shadow test, so darkening it again double-counts occlusion and makes creases look dirty under sunlight.
- Why does SSAO change as the camera moves even when nothing in the scene moves?It only knows the nearest surface per pixel on screen. When an occluder slides off screen or behind a nearer object, its occlusion vanishes, and a thin object is treated as solid all the way back. That view dependence is inherent to screen-space methods; a guard band, temporal smoothing or a world-space occlusion source reduce it.
- SSAO traces rays through the scene's triangles to find occluders.
- SSAO should darken direct sunlight the same way shadows do.
- A wider blur can replace the range check that prevents haloes.
- The SSAO noise texture stores precomputed occlusion values.
- SSAO sees occluders that are off screen or behind other objects.