13. Screen-Space Ambient Occlusion

Darkening creases and crevices using only the depth buffer

In the global illumination article we saw that indirect light bounces around a scene, filling in shadows and coloring surfaces that no direct light can reach. Computing that full light transport is expensive. But one specific aspect of indirect lighting — the way corners, crevices, and tight spaces receive less ambient light — can be approximated cheaply in screen space.

Screen-Space Ambient Occlusion (SSAO) is that approximation. It uses the depth buffer (and optionally normals) from a single camera render to estimate how "occluded" each pixel is — how much of the hemisphere above it is blocked by nearby geometry. Occluded pixels get darkened, giving the scene subtle contact shadows and a sense of depth that flat ambient lighting completely lacks.

Why Ambient Occlusion Matters

Without any ambient occlusion, a scene lit with a constant ambient term looks flat. Every surface receives the same base illumination regardless of its surroundings. A ball resting on a floor looks like it's floating — there's no darkening at the contact point. Interior corners of a room are the same brightness as the center of a wall.

Ambient occlusion fixes this by answering a simple question for each surface point: how much of the sky (or surrounding environment) can this point see? A point on an open plain sees the entire hemisphere — full ambient light. A point wedged into a corner sees very little — it gets darkened.

The ground-truth way to compute this is to cast rays into the hemisphere above each point and check how many are blocked. That's exactly what a path tracer does. SSAO fakes it using only information already available after rasterization.

The Depth Buffer as Geometry

After the geometry pass, the depth buffer contains the distance from the camera to the closest surface at every pixel. This is an implicit representation of the scene's visible geometry — not the full 3D world, but a 2.5D snapshot of what the camera sees.

SSAO exploits this: for each pixel, it samples nearby points in the depth buffer to estimate local occlusion. If many nearby samples are closer to the camera than the current pixel's depth, the pixel is probably recessed or surrounded by geometry — it should be darker.

Depth buffer (side view):

Camera
  |
  |    ____
  |   |    |      <- object A (closer)
  |   |    |____
  |   |         |
  |   |    P    | <- point P in a crevice
  |   |_________|
  |
  z-axis (depth) ->

Point P is surrounded by closer geometry on multiple sides.
Many hemisphere samples will hit something -> high occlusion.

The Classic SSAO Algorithm

The original SSAO technique (Crytek, 2007) works in three steps:

Step 1 — Generate a Sample Kernel

Create a set of sample points distributed within a hemisphere oriented along the surface normal. Each sample is a 3D offset vector with length between 0 and some radius parameter.

The samples should be weighted toward the center — occlusion from nearby geometry matters more than distant geometry. A common approach is to scale sample distances with an accelerating curve:

for i in 0..kernelSize:
    sample = random point in unit hemisphere
    scale = i / kernelSize
    scale = lerp(0.1, 1.0, scale * scale)   // quadratic falloff
    sample *= scale
    kernel.push(sample)

A typical kernel size is 32 to 64 samples. More samples = less noise, but more texture reads per pixel.

Step 2 — Sample the Depth Buffer

For each pixel:

  1. Reconstruct its view-space position from the depth buffer
  2. Look up its view-space normal (from a normal buffer, or reconstruct from depth derivatives)
  3. Build a TBN matrix (tangent, bitangent, normal) to orient the hemisphere along the surface normal
  4. For each sample in the kernel:
    • Transform the sample offset by the TBN matrix and add it to the pixel's view-space position — this gives a candidate point in the hemisphere above the surface
    • Project that point back to screen space and read the depth buffer at that location
    • Compare: if the depth buffer value at that screen position is closer to the camera than the sample point, the sample is occluded (something is blocking it)
occlusion = 0
for each sample in kernel:
    // Offset from surface point along hemisphere
    samplePos = pixelViewPos + TBN * sample * radius

    // Project to screen space
    screenPos = project(samplePos)

    // Read actual depth at that screen position
    sceneDepth = depthBuffer[screenPos.xy]

    // Is the sample behind existing geometry?
    if sceneDepth < samplePos.z:
        occlusion += 1

ao = 1.0 - (occlusion / kernelSize)

Step 3 — Blur and Apply

The raw SSAO output is noisy because we use a limited number of samples. To hide this:

  1. Rotate the kernel per pixel using a small random rotation texture (typically 4x4), tiled across the screen. This turns banding artifacts into high-frequency noise
  2. Blur the result with a bilateral or edge-aware blur (usually two-pass separable, 4x4 or larger). The blur smooths the noise but preserves edges by comparing depths — pixels with very different depths don't bleed into each other
  3. Multiply the blurred AO value into the ambient lighting term
Final ambient = ambient_color * ao_value

The result is subtle but immediately noticeable: creases darken, objects feel grounded, and the scene gains depth.

The Random Rotation Trick

With only 32-64 samples per pixel, the hemisphere is undersampled and you get visible banding. The standard fix: create a small noise texture (4x4 texels) containing random rotation vectors, and tile it across the screen. Each pixel's kernel gets rotated by a different angle.

This converts the low-frequency banding into high-frequency noise, which the blur pass then eliminates. The result looks as if you used many more samples than you actually did.

Without rotation noise:       With rotation noise + blur:

 ████████████████              ████████████████
 ██▓▓▓▓▓▓▓▓████   banding ->  ██▓▓▓▒▒▒░░░████   smooth gradient
 ██▒▒▒▒▒▒▒▒████              ██▒▒▒░░░   ████
 ██░░░░░░░░████              ██░░░      ████
 ████████████████              ████████████████

Range Check — Avoiding Halos

A naive implementation produces dark halos around objects that are far in front of a distant background. The samples behind the object hit the background's depth, which is much farther away — but the algorithm counts them as "occluding" because they're at a different depth.

The fix is a range check: only count a sample as occluded if the depth difference is within a reasonable range. If the difference is too large, the occluding geometry is probably unrelated to the current surface.

depthDiff = sceneDepth - samplePos.z

// Only count occlusion if the depth difference is small
rangeCheck = smoothstep(0.0, 1.0, radius / abs(depthDiff))
occlusion += (sceneDepth < samplePos.z ? 1.0 : 0.0) * rangeCheck

Normal-Oriented Hemispheres vs. Spheres

The original Crytek paper sampled a sphere around each pixel, not a hemisphere. Half the samples would be below the surface and always occluded, wasting half the work and producing a baseline gray everywhere.

The improved version (used by almost every engine today) orients the samples into a hemisphere aligned with the surface normal. This eliminates the wasted samples and produces cleaner results:

Sphere sampling:              Hemisphere sampling:

      * * *                         * * *
    * * * * *                     * * * * *
    * * P * *  <- half below      * * P * *  <- all above surface
    * * * * *     always hit      ─────────
      * * *       geometry          surface
    ─────────
      surface

SSAO in the Deferred Pipeline

SSAO fits naturally into a deferred rendering pipeline:

  1. Geometry pass — render to G-buffer (depth, normals, albedo)
  2. SSAO pass — full-screen quad; read depth + normals from G-buffer, write AO to a texture
  3. Blur pass — bilateral blur on the AO texture
  4. Lighting pass — sample the blurred AO texture and multiply it into the ambient term

In a forward renderer, you can still compute SSAO as a post-process using the depth buffer (and reconstruct normals from depth derivatives), but having explicit normals from a G-buffer gives better results.

Improvements and Variants

The basic SSAO algorithm has been refined many times:

HBAO (Horizon-Based Ambient Occlusion)

Instead of sampling random points in a hemisphere, HBAO traces rays along the depth buffer in several directions and finds the horizon angle — the highest angle at which geometry blocks the view. The occlusion is computed analytically from these horizon angles.

HBAO produces more physically accurate results, especially on flat surfaces near walls, and avoids many of the artifacts of point-sampling approaches.

HBAO+

NVIDIA's production-quality version of HBAO. It adds interleaved rendering (computing AO at quarter resolution in a checkerboard pattern, then reconstructing) for better performance, and uses a more sophisticated depth-aware blur.

GTAO (Ground Truth Ambient Occlusion)

GTAO integrates the visible horizon analytically over each pixel's hemisphere, approximating the ground-truth AO integral more closely than HBAO. It's the technique used in many modern engines (Unreal Engine uses a variant) and provides excellent quality at moderate cost.

SSDO (Screen-Space Directional Occlusion)

Extends SSAO to approximate one bounce of indirect lighting — not just darkening occluded areas, but coloring them based on the occluding surfaces. A sample that hits a red wall contributes red indirect light to the occluded point. This bridges the gap between SSAO and full global illumination.

Tuning Parameters

SSAO has several parameters that dramatically affect the result:

Parameter Effect Typical range
Radius How far to search for occluding geometry 0.3 – 2.0 world units
Bias Minimum depth difference to count as occlusion (prevents self-occlusion on flat surfaces) 0.01 – 0.05
Kernel size Number of samples per pixel 32 – 64
Power Exponent applied to final AO value — higher = darker, more contrasty 1.0 – 3.0
Blur size Radius of the bilateral blur 4 – 8 texels

The right values depend on the scene scale and art style. Start with a large radius to see the effect clearly, then dial it back until it's subtle.

SSAO Explorer

Adjust the sliders to see how each parameter affects the ambient occlusion.

Performance Considerations

SSAO is a full-screen effect — it runs for every pixel. The main costs are:

  • Texture reads — each pixel reads kernelSize depth samples plus the noise texture. This is the bottleneck on most GPUs. Reducing kernel size or rendering AO at half resolution helps
  • Blur passes — typically two passes (horizontal + vertical). Cheap individually, but they add bandwidth pressure
  • Resolution — many engines compute SSAO at half or quarter resolution and upscale, trading quality for performance

At 1080p with 32 samples and a 4x4 blur, SSAO typically costs 0.5–1.5ms on modern hardware. At 4K, rendering at half resolution is almost mandatory.

Limitations of Screen Space

Because SSAO only sees what the camera sees, it has inherent blind spots:

  • Off-screen geometry contributes no occlusion — an object just outside the frame casts no AO shadow
  • Occluded geometry (hidden behind other objects) can't occlude anything either — it's not in the depth buffer
  • Thin objects viewed edge-on may vanish from the depth buffer, losing their occlusion contribution
  • View-dependent artifacts — AO changes as the camera moves, because different geometry enters and leaves the depth buffer

These are fundamental to all screen-space techniques. For static scenes, baking AO into lightmaps avoids these issues entirely. For dynamic scenes, SSAO remains the best cost/quality tradeoff.

Putting It All Together

SSAO is one of the highest-impact post-processing effects in real-time rendering. It's cheap, it works with any geometry, and it dramatically improves the visual grounding of a scene. The algorithm is straightforward — sample the depth buffer in a hemisphere, count how many samples are occluded, blur the result, and darken the ambient term.

The key ideas to remember:

  1. Sample a hemisphere of points around each pixel, oriented along the surface normal
  2. Project each sample to screen space and compare against the depth buffer
  3. Rotate the kernel per pixel with a noise texture to convert banding into blur-friendly noise
  4. Range-check depth differences to avoid halos from distant geometry
  5. Bilateral blur the result to smooth noise while preserving edges

From here, you can explore HBAO and GTAO for higher quality, or SSDO to add indirect color bleeding. Each builds on the same core insight: the depth buffer is a cheap proxy for scene geometry, and a few dozen samples per pixel are enough to approximate what a path tracer computes with thousands of rays.