---
id: "reverse-z-depth-buffer"
title: "Reverse-Z and floating-point depth buffers"
aliases: [reversed-Z, reverse Z, infinite far plane depth]
domain: "performance"
stage: "performance"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot]
year_introduced: 2008
inputs: [mesh]
outputs: [depth]
style: "both"
dynamic: "both"
cost: "cheap"
authority: "Reed"
url: "https://texelatlas.vercel.app/t/reverse-z-depth-buffer/"
sources_verified_at: "2026-11-29"
---
# Reverse-Z and floating-point depth buffers (Reed)

> Map the near plane to depth 1 and the far plane to 0 and store depth in a float format, so floating-point density cancels the hyperbolic skew of perspective depth and precision becomes almost uniform.

## Reach for it when

- distant surfaces flicker and z-fight
- depth precision is poor with large far plane
- stripes where two coplanar surfaces overlap in the distance
- need very small near plane without artifacts
- large open world has depth buffer artifacts

## How it works

A standard perspective projection stores depth that is 1/z-like, packing most representable values near the camera. A float format does the opposite: it has most of its precision near zero. Using the usual convention (near=0, far=1) stacks both biases at the same end and wastes the float's strength.

Reverse-Z flips the mapping: the projection matrix is altered so the near plane lands at 1 and far at 0 (often with the far plane at infinity, which simplifies the matrix), the depth clear value becomes 0, and the depth test becomes greater-or-equal. Now the 1/z crowding near the camera lines up with the float's dense region near 0 only at far distances, and the two nonlinearities roughly cancel.

On APIs with clip space z in [-1,1] (OpenGL) you must also change the clip control or use a [0,1] range, otherwise the gain is lost.

## When to use it

Choose it whenever you render large view distances, a tiny near plane, or see distant z-fighting; it is a near-free change that pairs with a D32F or D24-float depth target. Prefer a logarithmic depth write only on hardware where you cannot change depth format or projection and can accept losing early-Z. It also matters for [Cascaded Shadow Maps](https://texelatlas.vercel.app/t/cascaded-shadow-maps.md) and [Virtual Shadow Maps](https://texelatlas.vercel.app/t/virtual-shadow-maps.md), where light-space depth range is large.

## Cost

cheap — No extra passes; a float depth format may cost more memory than D24 but runs at the same speed on modern GPUs.
Measured GPU Weight: 3× baseline (light) — 0.0943 ms median / 0.0964 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 2.68× baseline (light) — 0.0423 ms median (2.23× faster on L4).
Ampere A100 scaling: 1.89× baseline (feather) — 0.0543 ms median (1.74× faster on A100).

## Failure modes

- OpenGL's [-1,1] clip range cancels the benefit unless glClipControl/ZERO_TO_ONE is used
- Forgetting to flip the clear value, depth func or depth-bias sign causes everything to vanish or render inverted
- Integer D24/D16 targets gain nothing; the float format is required
- Shaders that reconstruct view position from depth, or Hi-Z and SSAO passes, silently break if still written for forward Z
- Depth bias and slope-scale values tuned for forward Z need retuning

## Engine support

- babylonjs: builtin — `engine.useReverseDepthBuffer = true` (evidence: https://github.com/BabylonJS/Babylon.js/blob/9.38.0/packages/dev/core/src/Engines/abstractEngine.pure.ts, checked 2026-12-01)
- godot: builtin — `Always on in the RenderingDevice renderers (Forward+, Mobile)` (evidence: https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/ProjectSettings.xml, checked 2026-12-01) — Reversed Z since 4.3.
- metal: builtin — `MTLPixelFormat.depth32Float + MTLDepthStencilDescriptor.depthCompareFunction = .greater` (evidence: https://developer.apple.com/tutorials/data/documentation/metal/mtlpixelformat.json, checked 2026-12-01) — Primitives only; the reversed projection matrix is yours.
- threejs: builtin — `new THREE.WebGLRenderer({ reversedDepthBuffer: true })` (evidence: https://github.com/mrdoob/three.js/blob/r187/src/renderers/WebGLRenderer.js, checked 2026-12-01)
- unity: builtin — `Automatic on supported platforms; shader macro UNITY_REVERSED_Z` (evidence: https://docs.unity3d.com/6000.0/Documentation/Manual/SL-PlatformDifferences.html, checked 2026-12-01)
- unreal: builtin — `Reversed-Z floating-point depth buffer (FReversedZPerspectiveMatrix / SceneDepth across UE render paths)` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/depth-material-expressions-in-unreal-engine, checked 2026-12-01)
- webgpu: builtin — `format: 'depth32float' with depthStencil.depthCompare: 'greater' and depth clear value 0` (evidence: https://www.w3.org/TR/webgpu/, checked 2026-12-01) — Primitives only; the reversed projection matrix is yours.

## Dataflow

- Inputs: mesh
- Outputs: depth
- Platforms: webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot

## Presets

### Reverse-Z depth buffer — forward [0,1] distant z-fighting (left) vs float32 reverse-Z [1,0] (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_DEPTH_BITS": 12,
  "P_PLANE_GAP": 0.04,
  "P_SPLIT": 0.5,
  "P_CAM_SPEED": 1
}
```

```glsl
const float P_DEPTH_BITS = 12.0; // P Mantissa Bits [8.0, 16.0, 1.0]
const float P_PLANE_GAP = 0.04; // P Coplanar Gap [0.005, 0.12, 0.005]
const float P_SPLIT = 0.5; // P Fwd / Rev-Z Split [0.0, 1.0, 0.02]
const float P_CAM_SPEED = 1.0; // P Glide Speed [0.0, 2.5, 0.05]

// Quantize a value x in [0,1] to a simulated floating-point representation with mBits mantissa bits
float quantFloat(float x, float mBits) {
  float ax = max(x, 1e-6);
  float e = floor(log2(ax));
  float stepSz = exp2(e - mBits);
  return floor(ax / stepSz + 0.5) * stepSz;
}

// Forward-Z NDC depth: z_ndc = (f / (f - n)) * (1 - n / z) -> crowds near 1.0 where float step is huge!
float forwardZ(float z, float nearP, float farP, float mBits) {
  float ndc = clamp((farP / (farP - nearP)) * (1.0 - nearP / max(z, nearP)), 0.0, 1.0);
  return quantFloat(ndc, mBits);
}

// Reverse-Z NDC depth: z_ndc = (n / (f - n)) * (f / z - 1) -> approaches 0.0 where float exponents pack densely!
float reverseZ(float z, float nearP, float farP, float mBits) {
  float ndc = clamp((nearP / (farP - nearP)) * (farP / max(z, nearP) - 1.0), 0.0, 1.0);
  return quantFloat(ndc, mBits);
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  float screenX = fragCoord.x / max(iResolution.x, 1.0);
  float screenY = fragCoord.y / max(iResolution.y, 1.0);
  float split = clamp(P_SPLIT, 0.0, 1.0);
  float mBits = clamp(P_DEPTH_BITS, 7.0, 17.0);
  float gap = clamp(P_PLANE_GAP, 0.004, 0.15);
  float tAnim = iTime * max(P_CAM_SPEED, 0.0);

  float nearP = 0.25;
  float farP = 180.0;

  // Perspective runway looking down a long architectural corridor with closely layered decals
  vec3 ro = vec3(0.0, 1.85, tAnim * 2.2);
  vec3 rd = normalize(vec3(uv.x, uv.y - 0.18, -1.35));

  vec3 col = mix(vec3(0.04, 0.06, 0.11), vec3(0.11, 0.16, 0.26), smoothstep(-0.1, 0.65, rd.y));

  // Intersect primary floor (y = 0) and slightly raised coplanar marking plane (y = gap)
  if (rd.y < -0.004) {
    float tFloor = -ro.y / rd.y;
    float tDecal = -(ro.y - gap) / rd.y;
    if (tFloor > nearP && tFloor < farP) {
      vec3 pF = ro + rd * tFloor;
      vec3 pD = ro + rd * tDecal;

      // View-space depth Z along camera forward axis (-z)
      float zF = tFloor * (-rd.z);
      float zD = tDecal * (-rd.z);

      // Floor checkerboard pattern & bright amber/cyan runway chevrons on the raised decal plane
      vec2 fGrid = floor(pF.xz * 0.45);
      float chk = mod(fGrid.x + fGrid.y, 2.0);
      vec3 floorCol = mix(vec3(0.16, 0.20, 0.27), vec3(0.25, 0.30, 0.38), chk);

      float laneX = abs(pD.x);
      float chevron = sin(pD.z * 1.15 - laneX * 1.4);
      float isDecal = step(laneX, 3.6) * step(0.15, chevron);
      vec3 decalCol = mix(vec3(0.98, 0.62, 0.14), vec3(0.14, 0.88, 0.94), step(1.6, laneX));

      // Tiny sub-pixel perspective jitter amplifies hardware triangle-interpolation rounding ties
      float jitter = sin(fragCoord.x * 0.71 + fragCoord.y * 1.13 + iTime * 2.4) * 0.5;
      float zF_j = zF + jitter * gap * 0.35;
      float zD_j = zD - jitter * gap * 0.35;

      bool useReverse = screenX >= split;
      bool decalWins;
      if (!useReverse) {
        // Standard Forward-Z: smaller depth wins, but quantization ties cause severe z-fighting!
        float qF = forwardZ(zF_j, nearP, farP, mBits);
        float qD = forwardZ(zD_j, nearP, farP, mBits);
        decalWins = (qD < qF) || (qD == qF && fract(sin(dot(floor(fragCoord * 0.5), vec2(12.98, 78.23))) * 43758.5) > 0.5);
      } else {
        // Reverse-Z: larger depth wins, and float32 exponent scaling resolves tiny gaps cleanly!
        float qF = reverseZ(zF_j, nearP, farP, mBits);
        float qD = reverseZ(zD_j, nearP, farP, mBits);
        decalWins = (qD > qF);
      }

      vec3 surf = (isDecal > 0.5 && decalWins) ? decalCol : floorCol;
      // Highlight z-fighting quantization tie zones on the Forward-Z side in hot crimson
      if (!useReverse && isDecal > 0.5 && forwardZ(zF, nearP, farP, mBits) == forwardZ(zD, nearP, farP, mBits)) {
        surf = mix(surf, vec3(1.0, 0.18, 0.32), 0.35);
      }

      // Distance fog & runway rim guide lines
      float rail = smoothstep(0.12, 0.03, abs(abs(pF.x) - 3.85));
      surf = mix(surf, vec3(0.85, 0.92, 1.0), rail * 0.65);
      float fog = exp(-tFloor * 0.018);
      col = mix(vec3(0.06, 0.09, 0.15), surf, fog);
    }
  }

  // Bottom precision-graph HUD strip comparing ULP resolution vs log-distance for Forward-Z vs Reverse-Z
  if (screenY < 0.17) {
    float hy = screenY / 0.17;
    col = mix(col, vec3(0.03, 0.04, 0.07), 0.86);
    float zTest = mix(nearP, farP, pow(screenX, 2.2));
    // Relative precision (log10 of resolvable delta-z): Forward-Z collapses, Reverse-Z stays flat!
    float fwdPrec = clamp(1.0 - log2(max(zTest * zTest / (nearP * farP * 0.25), 1.0)) / 10.0, 0.06, 0.92);
    float revPrec = clamp(0.86 - 0.08 * log2(max(zTest / nearP, 1.0)) / 9.0, 0.12, 0.92);
    float lineF = smoothstep(0.055, 0.012, abs(hy - fwdPrec));
    float lineR = smoothstep(0.055, 0.012, abs(hy - revPrec));
    col = mix(col, vec3(1.0, 0.30, 0.36), lineF);
    col = mix(col, vec3(0.18, 0.94, 0.72), lineR);
    float topBorder = smoothstep(0.95, 0.99, hy);
    col = mix(col, vec3(0.35, 0.45, 0.60), topBorder);
  }

  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
  col = mix(col, vec3(0.95, 0.97, 1.0), sep);

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).

## Combines with

- [Hierarchical-Z occlusion culling](https://texelatlas.vercel.app/t/hierarchical-z-occlusion-culling.md): The depth pyramid built from a reverse-Z buffer keeps distant occluders precise, so conservative max/min reductions stay valid.

## Sources

1. [primary] Nathan Reed Visualizing Depth Precision. developer.nvidia.com. https://developer.nvidia.com/blog/visualizing-depth-precision (verified via http on 2026-11-29)
2. [tutorial] Nathan Reed Depth Precision Visualized. reedbeta.com. https://www.reedbeta.com/blog/depth-precision-visualized (verified via http on 2026-11-29)

## Cite this entry

Texel. "Reverse-Z and floating-point depth buffers." https://texelatlas.vercel.app/t/reverse-z-depth-buffer/ (accessed 2026-12-01).
