{
 "id": "reverse-z-depth-buffer",
 "title": "Reverse-Z and floating-point depth buffers",
 "aliases": [
  "reversed-Z",
  "reverse Z",
  "infinite far plane depth"
 ],
 "domain": "performance",
 "subdomain": "depth precision",
 "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",
 "summary": "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.",
 "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.\n\nReverse-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.\n\nOn 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": "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` and `virtual-shadow-maps`, where light-space depth range is large.",
 "cost": {
  "class": "cheap",
  "notes": "No extra passes; a float depth format may cost more memory than D24 but runs at the same speed on modern GPUs."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://developer.nvidia.com/blog/visualizing-depth-precision",
   "type": "web",
   "title": "Visualizing Depth Precision",
   "authors": [
    "Nathan Reed"
   ],
   "year": null,
   "venue": "developer.nvidia.com",
   "url": "https://developer.nvidia.com/blog/visualizing-depth-precision",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20260918035639/https://developer.nvidia.com/blog/visualizing-depth-precision/",
     "timestamp": "20260918035639"
    },
    "page_title": "Visualizing Depth Precision | NVIDIA Technical Blog",
    "host": "developer.nvidia.com",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://developer.nvidia.com/blog/visualizing-depth-precision → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://developer.nvidia.com/blog/visualizing-depth-precision → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://developer.nvidia.com/blog/visualizing-depth-precision → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.95,
   "__bib": "reedvisualizing"
  },
  {
   "role": "tutorial",
   "key": "url:https://www.reedbeta.com/blog/depth-precision-visualized",
   "type": "tutorial",
   "title": "Depth Precision Visualized",
   "authors": [
    "Nathan Reed"
   ],
   "year": null,
   "venue": "reedbeta.com",
   "url": "https://www.reedbeta.com/blog/depth-precision-visualized",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": null,
    "page_title": "Depth Precision Visualized – Nathan Reed’s coding blog",
    "host": "reedbeta.com",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://www.reedbeta.com/blog/depth-precision-visualized → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://www.reedbeta.com/blog/depth-precision-visualized → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://www.reedbeta.com/blog/depth-precision-visualized → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.95,
   "__bib": "reeddepth"
  }
 ],
 "presets": [
  {
   "id": "reverse-z-depth-buffer-live",
   "title": "Reverse-Z depth buffer — forward [0,1] distant z-fighting (left) vs float32 reverse-Z [1,0] (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_DEPTH_BITS": 12,
    "P_PLANE_GAP": 0.04,
    "P_SPLIT": 0.5,
    "P_CAM_SPEED": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_DEPTH_BITS = 12.0; // P Mantissa Bits [8.0, 16.0, 1.0]\nconst float P_PLANE_GAP = 0.04; // P Coplanar Gap [0.005, 0.12, 0.005]\nconst float P_SPLIT = 0.5; // P Fwd / Rev-Z Split [0.0, 1.0, 0.02]\nconst float P_CAM_SPEED = 1.0; // P Glide Speed [0.0, 2.5, 0.05]\n\n// Quantize a value x in [0,1] to a simulated floating-point representation with mBits mantissa bits\nfloat quantFloat(float x, float mBits) {\n  float ax = max(x, 1e-6);\n  float e = floor(log2(ax));\n  float stepSz = exp2(e - mBits);\n  return floor(ax / stepSz + 0.5) * stepSz;\n}\n\n// Forward-Z NDC depth: z_ndc = (f / (f - n)) * (1 - n / z) -> crowds near 1.0 where float step is huge!\nfloat forwardZ(float z, float nearP, float farP, float mBits) {\n  float ndc = clamp((farP / (farP - nearP)) * (1.0 - nearP / max(z, nearP)), 0.0, 1.0);\n  return quantFloat(ndc, mBits);\n}\n\n// Reverse-Z NDC depth: z_ndc = (n / (f - n)) * (f / z - 1) -> approaches 0.0 where float exponents pack densely!\nfloat reverseZ(float z, float nearP, float farP, float mBits) {\n  float ndc = clamp((nearP / (farP - nearP)) * (farP / max(z, nearP) - 1.0), 0.0, 1.0);\n  return quantFloat(ndc, mBits);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float screenY = fragCoord.y / max(iResolution.y, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  float mBits = clamp(P_DEPTH_BITS, 7.0, 17.0);\n  float gap = clamp(P_PLANE_GAP, 0.004, 0.15);\n  float tAnim = iTime * max(P_CAM_SPEED, 0.0);\n\n  float nearP = 0.25;\n  float farP = 180.0;\n\n  // Perspective runway looking down a long architectural corridor with closely layered decals\n  vec3 ro = vec3(0.0, 1.85, tAnim * 2.2);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.18, -1.35));\n\n  vec3 col = mix(vec3(0.04, 0.06, 0.11), vec3(0.11, 0.16, 0.26), smoothstep(-0.1, 0.65, rd.y));\n\n  // Intersect primary floor (y = 0) and slightly raised coplanar marking plane (y = gap)\n  if (rd.y < -0.004) {\n    float tFloor = -ro.y / rd.y;\n    float tDecal = -(ro.y - gap) / rd.y;\n    if (tFloor > nearP && tFloor < farP) {\n      vec3 pF = ro + rd * tFloor;\n      vec3 pD = ro + rd * tDecal;\n\n      // View-space depth Z along camera forward axis (-z)\n      float zF = tFloor * (-rd.z);\n      float zD = tDecal * (-rd.z);\n\n      // Floor checkerboard pattern & bright amber/cyan runway chevrons on the raised decal plane\n      vec2 fGrid = floor(pF.xz * 0.45);\n      float chk = mod(fGrid.x + fGrid.y, 2.0);\n      vec3 floorCol = mix(vec3(0.16, 0.20, 0.27), vec3(0.25, 0.30, 0.38), chk);\n\n      float laneX = abs(pD.x);\n      float chevron = sin(pD.z * 1.15 - laneX * 1.4);\n      float isDecal = step(laneX, 3.6) * step(0.15, chevron);\n      vec3 decalCol = mix(vec3(0.98, 0.62, 0.14), vec3(0.14, 0.88, 0.94), step(1.6, laneX));\n\n      // Tiny sub-pixel perspective jitter amplifies hardware triangle-interpolation rounding ties\n      float jitter = sin(fragCoord.x * 0.71 + fragCoord.y * 1.13 + iTime * 2.4) * 0.5;\n      float zF_j = zF + jitter * gap * 0.35;\n      float zD_j = zD - jitter * gap * 0.35;\n\n      bool useReverse = screenX >= split;\n      bool decalWins;\n      if (!useReverse) {\n        // Standard Forward-Z: smaller depth wins, but quantization ties cause severe z-fighting!\n        float qF = forwardZ(zF_j, nearP, farP, mBits);\n        float qD = forwardZ(zD_j, nearP, farP, mBits);\n        decalWins = (qD < qF) || (qD == qF && fract(sin(dot(floor(fragCoord * 0.5), vec2(12.98, 78.23))) * 43758.5) > 0.5);\n      } else {\n        // Reverse-Z: larger depth wins, and float32 exponent scaling resolves tiny gaps cleanly!\n        float qF = reverseZ(zF_j, nearP, farP, mBits);\n        float qD = reverseZ(zD_j, nearP, farP, mBits);\n        decalWins = (qD > qF);\n      }\n\n      vec3 surf = (isDecal > 0.5 && decalWins) ? decalCol : floorCol;\n      // Highlight z-fighting quantization tie zones on the Forward-Z side in hot crimson\n      if (!useReverse && isDecal > 0.5 && forwardZ(zF, nearP, farP, mBits) == forwardZ(zD, nearP, farP, mBits)) {\n        surf = mix(surf, vec3(1.0, 0.18, 0.32), 0.35);\n      }\n\n      // Distance fog & runway rim guide lines\n      float rail = smoothstep(0.12, 0.03, abs(abs(pF.x) - 3.85));\n      surf = mix(surf, vec3(0.85, 0.92, 1.0), rail * 0.65);\n      float fog = exp(-tFloor * 0.018);\n      col = mix(vec3(0.06, 0.09, 0.15), surf, fog);\n    }\n  }\n\n  // Bottom precision-graph HUD strip comparing ULP resolution vs log-distance for Forward-Z vs Reverse-Z\n  if (screenY < 0.17) {\n    float hy = screenY / 0.17;\n    col = mix(col, vec3(0.03, 0.04, 0.07), 0.86);\n    float zTest = mix(nearP, farP, pow(screenX, 2.2));\n    // Relative precision (log10 of resolvable delta-z): Forward-Z collapses, Reverse-Z stays flat!\n    float fwdPrec = clamp(1.0 - log2(max(zTest * zTest / (nearP * farP * 0.25), 1.0)) / 10.0, 0.06, 0.92);\n    float revPrec = clamp(0.86 - 0.08 * log2(max(zTest / nearP, 1.0)) / 9.0, 0.12, 0.92);\n    float lineF = smoothstep(0.055, 0.012, abs(hy - fwdPrec));\n    float lineR = smoothstep(0.055, 0.012, abs(hy - revPrec));\n    col = mix(col, vec3(1.0, 0.30, 0.36), lineF);\n    col = mix(col, vec3(0.18, 0.94, 0.72), lineR);\n    float topBorder = smoothstep(0.95, 0.99, hy);\n    col = mix(col, vec3(0.35, 0.45, 0.60), topBorder);\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "reverse-z-depth-buffer",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:04:37.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5064,
      "std": 0.1969,
      "min": 0.2758,
      "max": 0.9014
     },
     "image": "/specimens/reverse-z-depth-buffer-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "hierarchical-z-occlusion-culling",
  "cascaded-shadow-maps",
  "temporal-anti-aliasing"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "hierarchical-z-occlusion-culling",
   "why": "The depth pyramid built from a reverse-Z buffer keeps distant occluders precise, so conservative max/min reductions stay valid."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/performance/reverse-z-depth-buffer.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Reed",
 "url": "https://texelatlas.vercel.app/t/reverse-z-depth-buffer/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 3,
  "tier": "light",
  "p50_ms": 0.0943,
  "p95_ms": 0.0964,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "reverse-z-depth-buffer-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 2.68,
   "tier": "light",
   "p50_ms": 0.0423,
   "p95_ms": 0.0427,
   "speedup_x": 2.23,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 1.89,
   "tier": "feather",
   "p50_ms": 0.0543,
   "p95_ms": 0.0546,
   "speedup_x": 1.74,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}