Skip to content
FoundationSource checked

Z-Buffering

Keeps the closest visible fragment for each pixel.

Quick Facts

Compact metadata for implementation and discovery.

Category
Rendering
Difficulty
Beginner
Family
Visibility
Tier
Foundation
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
7
Code example
Not ready
Visualization
Ready

Overview

Z-Buffering stores a depth value per pixel and only replaces a pixel when a new fragment is closer.

Core Concepts

per-pixel depthvisibility testdepth comparisonfragment updaterendered imagevisibility orderingvisibilityscene representation

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(f)

Average

O(f)

Worst

O(f)

Space

O(p)

When to Use

  • Use it to resolve visible surfaces in a rasterization pipeline.
  • Use it when each fragment has a comparable depth value.
  • Use it for standard opaque 3D rendering.

When Not to Use

  • Avoid relying on it alone for transparent surfaces that need ordering or blending.
  • Avoid it when depth precision is insufficient for the near and far plane range.
  • Avoid inconsistent depth conventions between projection and comparison.

What It Does

  • Tracks depth per pixel
  • Rejects hidden fragments

Use Cases

  • Real-time 3D rendering
  • Visibility testing
  • Rasterization pipelines

Inputs

  • Fragments with depth values
  • Depth buffer

Outputs

  • Visible color buffer
  • Updated depth buffer

Key Formulas (1)

Depth test

write fragment if depthnew < depthstored
write fragment if depth_new < depth_stored

Keeps the closest visible fragment for each pixel.

Pseudocode

  1. 1.Initialize each pixel depth to far away
  2. 2.For each fragment:
  3. 3.Compare fragment depth with stored pixel depth
  4. 4.Write the color if the fragment is closer
  5. 5.Update the stored depth

Canonical Example

Input
two fragments for one pixel with depths 0.3 and 0.8
Output
the color of the depth 0.3 fragment
Explanation
With a standard smaller-is-nearer convention, the nearer fragment replaces the farther one.

Implementation Notes

  • Initialize the depth buffer to the farthest representable value.
  • For each fragment, compare its depth with the stored depth before writing color.
  • Keep projection depth mapping and depth-test comparison consistent.

Common Pitfalls

  • Setting near and far planes too far apart, which reduces depth precision.
  • Using the wrong comparison direction for the depth convention.
  • Expecting z-buffering to handle transparency automatically.

Interactive Mechanism Walkthrough

Spatial Primitive & Sweep-Line Canvas

Show overlapping fragments competing by depth for the same pixel.

Z-Buffering mechanism walkthrough step 1 of 5
Step 1 of 5: Initialize each pixel depth to far away
depth-buffer

Notes

Depth-buffer precision issues often appear as z-fighting between nearly coplanar surfaces.

Sources (1)

What each reference supports for this entry.

Real-Time Rendering

Book

Computer graphics reference covering depth buffering and visibility in raster pipelines.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Rasterization commonly uses the z-buffer to resolve visibility.

Alternative toShadow Mapping

Both compare stored depths, but for different visibility problems.

Related Algorithms

Tags

GraphicsVisibilityClassic