skip to content

A distant mountain range z-fights with the terrain in front of it; why does perspective depth lose precision with distance, and how do you fix it?

level: seniorimportance: should knowfreq 50%

answer

  1. hyperbolic, not linear, in distance
  2. error grows with distance squared
  3. near plane matters, far barely
  4. float depth, reversed range

basics

~20 s

Perspective depth stores roughly 1/z, so representable values crowd near the camera and the gap between them grows with distance squared. Push the near plane out, or store 32-bit float depth with reversed depth (near = 1, far = 0).

solid answer

~50 s

With a 0-to-1 depth range mapping near `n` to 0 and far `f` to 1, stored depth is `d = f/(f - n) * (1 - n/z)`, affine in `1/z`. Its slope is `f n / ((f - n) z^2)`, so one depth step `Δd` spans `Δz ≈ Δd * z^2 / n` when `f >> n`. In this scenario, 24-bit depth (`Δd ≈ 6e-8`), `n = 0.1 m` and mountains at 2,000 m give `Δz ≈ 2.4 m`, so nearby ridges collapse to one value and fight. Fixes: raise the near plane, the cheapest lever (1 m gives about 0.24 m); move to 32-bit float depth with **reversed depth**, where far geometry sits near 0 and the float exponent cancels the `1/z` crowding, giving about 0.1 mm at 2 km; or split near and far scenery into separate depth ranges. Pulling the far plane in barely helps, and reversing a fixed-point buffer does nothing.

go deeper

for a junior

Know what z-fighting looks like and that the depth buffer has limited precision, worse far from the camera.

for a middle

Derive why stored depth is affine in 1/z, why the near plane dominates, and what reversed float depth changes.

for a senior

Work the error at a given distance, tell precision from coplanar fighting, and plan a move to reversed depth, including every consumer of the depth buffer.

for a principal

Weigh a project-wide depth convention: reversed float depth as the default, its memory cost per target, and what it forces on tools, shadows and post-processing code.

## Where the precision goes After the perspective divide and viewport transform, the depth stored for a point at view distance `z`, with near plane `n`, far plane `f` and a 0-to-1 range that maps near to 0, is `d(z) = f / (f - n) * (1 - n / z)` Building the projection that produces this belongs to the coordinate-space maths; what matters here is its shape. `d` is affine in `1/z`, not in `z`. Solving `d = 0.5` gives `z = 2 f n / (f + n)`, about `2n` when `f >> n`: half of all depth values are spent between the near plane and twice its distance. ## Deriving the error Differentiate: `dd/dz = f n / ((f - n) z^2)`. A depth buffer that can only represent steps of `Δd` separates two surfaces only when they are at least `Δz ≈ Δd * z^2 * (f - n) / (f n)`, about `Δd * z^2 / n` when `f >> n` apart. Three conclusions follow: - The error grows with the **square** of distance. - It is inversely proportional to the **near** distance: doubling `n` roughly halves the error at every distance. - The **far** plane hardly matters once it is much larger than `n`; pulling it in from 20 km to 10 km changes `(f - n) / f` by a fraction of a percent. ## The mountain range, worked The scenario's figures: 24-bit fixed-point depth, so `Δd = 2^-24`, about `6e-8`; a near plane at 0.1 m; mountains 2,000 m away. `Δz ≈ 6e-8 * 2000^2 / 0.1 = 6e-8 * 4e6 / 0.1 ≈ 2.4 m` Any terrain ridge less than about 2.4 m in front of the mountain's slope rounds to the same depth value, and which one wins flips from pixel to pixel and frame to frame. With the near plane at 1 m the step drops to about 0.24 m. ## Reversed depth with floating point A **32-bit float** depth buffer does not help on its own. Floats are densest near 0 and sparsest near 1, and in the standard mapping distant geometry lives just below 1, where the float spacing is `2^-24`: no better than 24-bit fixed point. **Reversed depth** maps near to 1 and far to 0. Distant geometry now sits near 0, where the float exponent gives dense values. With the far plane at infinity the mapping is `d = n / z`; a float's precision is relative to its value, so `Δd ≈ d * 2^-24`, and substituting gives `Δz ≈ z * 2^-24`, about 0.1 mm at 2 km. That is comparable to the rounding of the vertex positions themselves. Reversing a fixed-point buffer gains nothing, because fixed-point steps are uniform. | Depth format | Standard (near = 0) | Reversed (near = 1) | |---|---|---| | 24-bit fixed point | about 2.4 m at 2 km with n = 0.1 m | unchanged | | 32-bit float | about the same as 24-bit at distance | about 0.1 mm at 2 km | ## Moving to reversed depth 1. Make the projection map near to 1 and far, or infinity, to 0. 2. Flip the depth comparison to greater, or greater-or-equal. 3. Clear depth to 0 instead of 1. 4. Use a 0-to-1 clip-space depth convention. With a -1-to-1 convention the hardware computes `0.5 * z_ndc + 0.5`, and that addition rounds away the tiny values that carry the precision. 5. Fix every consumer that assumes 1 means far: a sky drawn at the far plane, position reconstruction from depth, depth-based fog, occlusion tests built from a depth pyramid, and any shadow comparison that shares the convention. ## Other fixes, and telling the cases apart - **Push the near plane out** as far as the camera allows; it is the cheapest lever. - **Split the depth range**: draw distant scenery in its own pass with its own near and far planes, clear depth, then draw the near world. Objects that straddle the split need care. - **Logarithmic depth** written from the fragment program spreads precision evenly, but program-written depth stops early depth testing for everything that uses it. - **Depth bias** helps only truly coplanar surfaces such as decals; it does not fix precision. To tell precision from coplanar geometry: precision fighting gets worse with distance and improves when the near plane moves out, while two coplanar triangles fight at any distance and ignore the near plane.

  • What has to change in a renderer when it moves to reversed depth?
    The projection maps near to 1 and far to 0, the depth comparison flips to greater, and depth clears to 0. The clip-space depth convention must be 0 to 1. Everything that reads depth with the old meaning must flip too: sky at the far plane, position reconstruction, fog, depth-pyramid occlusion and shadow comparisons.
  • How do you tell precision z-fighting from coplanar geometry fighting?
    Precision fighting grows with distance and improves when the near plane is pushed out or reversed float depth is enabled. Coplanar surfaces fight at every distance and do not respond to either, because their depths are equal in any format. Coplanar cases are fixed in the content or with a small depth bias.
  • Why not simply write logarithmic depth from the fragment program?
    It spreads precision evenly, but a program that writes depth stops the GPU testing depth before shading and often disables coarse depth rejection, so every surface using it shades its hidden fragments. Writing it per vertex instead is wrong for large triangles, since log depth is not linear across the screen. Reversed float depth gets similar precision for free.

  • Moving the far plane closer is the main way to fix distant z-fighting.
  • Depth is stored linearly in view distance, so precision is equal everywhere.
  • A 32-bit float depth buffer alone fixes distant z-fighting.
  • Reversed depth also improves a 24-bit fixed-point depth buffer.
  • Z-fighting is always coplanar geometry and must be fixed in the art.