A Z-buffer, also called a depth buffer, is a per-pixel memory array that stores the distance of the nearest surface drawn so far, allowing the graphics hardware to decide which fragment is visible and to discard fragments hidden behind it. It is the standard solution to the hidden surface problem in rasterization-based rendering.
In 3D graphics & animation, and especially in Real-time 3D applications such as games, simulators and WebGL scenes, the Z-buffer lets objects be drawn in almost any order while still producing correct occlusion. Its precision, format and configuration directly affect visual artifacts such as Z-fighting and the cost of rendering techniques such as shadow mapping and deferred shading.
How the Z-buffer resolves visibility
The Z-buffer works through a per-fragment depth test. Before a frame is drawn, every entry is cleared to the farthest possible value, usually 1.0 in normalized depth, or 0.0 when reversed depth is used.
When the rasterizer produces a fragment, the GPU compares its interpolated depth with the value already stored at that pixel. If the new fragment is closer according to the depth function, its color is written and the stored depth is updated; otherwise the fragment is discarded. Because the test happens per pixel, intersecting triangles are resolved correctly without any sorting.
The configurable comparison operator controls what counts as visible. The table below lists the common depth functions and where each is used.
| Depth function | Passes when | Typical use |
|---|---|---|
| LESS | New depth is smaller than stored depth | Default opaque rendering |
| LEQUAL | New depth is smaller or equal | Multi-pass rendering on the same geometry |
| GREATER | New depth is larger than stored depth | Reversed-Z depth buffers |
| EQUAL | New depth matches stored depth | Shading pass after a depth prepass |
| ALWAYS | Every fragment passes | Overlays, user interface and debugging |
Depth precision and buffer formats
Depth values are not stored linearly. After perspective projection, depth is proportional to the reciprocal of view distance, so most of the available precision is concentrated close to the near clipping plane, while distant surfaces share very few distinct values.
This non-linear distribution is the main cause of Z-fighting, the flickering that appears when two nearly coplanar surfaces receive the same stored depth. What counts most is the proportion of far distance to near distance, not the far value by itself: moving the near plane outward improves precision much more than pulling the far plane inward.
The format chosen for the buffer sets the absolute precision limit. The following table compares the formats most often found in modern graphics APIs.
| Format | Bits per pixel | Precision characteristics |
|---|---|---|
| D16 unorm | 16 | Low precision; suits small scenes and shadow maps |
| D24 unorm with S8 stencil | 32 combined | Long-standing default with stencil support |
| D32 float | 32 | High precision, especially with reversed-Z |
| D32 float with S8 stencil | 40 to 64 depending on hardware | Float depth plus stencil operations |
Reversed-Z inverts the usual convention, so surfaces at the near clip receive the largest stored value and the far clip receives zero. Combined with a 32-bit floating-point format, it pairs the dense float precision near zero with the dense perspective precision far away, producing nearly uniform accuracy across the view range. Many current engines use this as the default.
Z-buffer compared with other visibility techniques
The Z-buffer is one of several ways to decide what the camera sees. It is simple, hardware-accelerated and order-independent for opaque geometry, but other methods remain useful for transparency or for reducing work before rasterization.
| Technique | Works at | Strengths and limits |
|---|---|---|
| Z-buffer | Per pixel | Exact for opaque surfaces; cannot blend transparency by itself |
| Painter’s algorithm | Per object or triangle | Simple sorting; fails on intersecting or cyclic overlaps |
| Backface culling | Per triangle | Cheap removal of faces pointing away; not full visibility |
| Frustum culling | Per object | Skips objects outside the view; no occlusion handling |
| Occlusion culling | Per object or cluster | Skips hidden objects early; often reads a depth pyramid |
These techniques are complementary rather than competing. A typical frame uses frustum and occlusion culling to reduce draw calls, backface culling to discard half of the triangles, and the Z-buffer to resolve the final per-pixel visibility.
Performance features built on the depth buffer
Modern GPUs perform the depth test before the fragment shader whenever possible. This early-Z rejection avoids shading fragments that would be discarded anyway, which is a major saving in scenes with high overdraw.
Early testing is disabled automatically when a shader writes its own depth value or discards fragments in ways the hardware cannot predict, so alpha-tested foliage and custom depth output carry a hidden performance cost. Hierarchical depth structures and lossless depth compression further reduce memory bandwidth.
- Depth prepass: draw opaque geometry once with depth only, then shade with an EQUAL test so each pixel is shaded once.
- Shadow mapping: render a depth buffer from the light, then compare scene depth against it.
- Deferred shading and SSAO: reconstruct position and occlusion from the stored depth.
- Soft particles and fog: fade effects based on the distance to opaque geometry.
Common Z-buffer problems and fixes
Most depth-related defects come from precision limits, wrong state or transparency handling. The table below links typical symptoms to their causes and practical remedies.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Flickering coplanar surfaces | Insufficient depth precision | Raise near plane, use reversed-Z or offset geometry |
| Transparent objects hide others | Transparent pass writes depth | Sort back to front and disable depth writes |
| Decals flicker on walls | Identical depth to the base surface | Apply polygon offset or depth bias |
| Shadow acne on lit surfaces | Self-comparison in shadow map | Add slope-scaled depth bias |
| Unexpectedly slow pixel shading | Early-Z disabled by shader depth writes | Avoid depth output and limit discard use |
A frequent misconception is that a larger far plane is the main problem. In practice, a near plane set too close, such as 0.01 units in a scene measured in meters, causes most visible Z-fighting.
The short version
Imagine painting a scene where every object is drawn one after another in random order. To avoid painting a far tree over a nearby house, each pixel keeps a note saying how far away the nearest thing painted there is.
When a new object arrives, it is painted on a pixel only if it is closer than what that note says. The Z-buffer is simply this collection of distance notes. The key takeaway is that closer surfaces always win, so the picture looks correct no matter which object is drawn first.
Where it makes a difference
A small team is building an open-world flight simulator in which the camera sees cockpit instruments 0.3 meters away and mountains up to 40 kilometers away. With a conventional 24-bit depth buffer and the near plane at 0.1 meters, distant runways and road markings flicker badly as the aircraft moves.
The rendering programmer switches to a 32-bit floating-point depth buffer with reversed-Z, changes the depth function to GREATER, and clears depth to 0.0 instead of 1.0. The cockpit is rendered in the same pass, so no geometry splitting is needed.
After the change, Z-fighting on distant terrain disappears while cockpit detail remains sharp. A depth prepass added in the same update also reduces fragment shading cost over dense forests, because early-Z rejection now skips most hidden tree fragments.