Reverse-Z and floating-point depth buffers
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.
Also known as reversed-Z, reverse Z, infinite far plane depth
In the family Performance, LOD & compression, depth precision
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | Built in | new THREE.WebGLRenderer({ reversedDepthBuffer: true }) docs |
| Babylon.js 9.38.0 | Built in | engine.useReverseDepthBuffer = true docs |
| Godot 4.7 | Built in | Always on in the RenderingDevice renderers (Forward+, Mobile) docs Reversed Z since 4.3. |
| Unity | Built in | Automatic on supported platforms; shader macro UNITY_REVERSED_Z docs |
| Unreal 5.7 | Built in | Reversed-Z floating-point depth buffer (FReversedZPerspectiveMatrix / SceneDepth across UE render paths) docs |
| Blender 5.2 | None found | |
| WebGPU W3C | Built in | format: 'depth32float' with depthStencil.depthCompare: 'greater' and depth clear value 0 docs Primitives only; the reversed projection matrix is yours. |
| Metal | Built in | MTLPixelFormat.depth32Float + MTLDepthStencilDescriptor.depthCompareFunction = .greater docs Primitives only; the reversed projection matrix is yours. |
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 choose it
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
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.
Field marks
- 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
Presets
Reverse-Z depth buffer — forward [0,1] distant z-fighting (left) vs float32 reverse-Z [1,0] (right)
{
"P_DEPTH_BITS": 12,
"P_PLANE_GAP": 0.04,
"P_SPLIT": 0.5,
"P_CAM_SPEED": 1
}Show GLSL (124 lines)
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).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| reverse-z-depth-buffer-live | Tesla T4 1920x1080 | 3.00× baseline (light) · 0.094 ms / 0.096 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| reverse-z-depth-buffer-live | NVIDIA L4 1920x1080 | 2.68× baseline (light) · 0.042 ms / 0.043 ms · 2.2× faster on L4 | opengl-timer-query · Cross-architecture validation |
| reverse-z-depth-buffer-live | NVIDIA A100-SXM4-40GB 1920x1080 | 1.89× baseline (feather) · 0.054 ms / 0.055 ms · 1.7× faster on A100 | opengl-timer-query · Cross-architecture validation |
Crosses
- Hierarchical-Z occlusion culling: The depth pyramid built from a reverse-Z buffer keeps distant occluders precise, so conservative max/min reductions stay valid.
Sources
- primaryNathan Reed. Visualizing Depth Precision. developer.nvidia.com.Verified via live page · 2026-11-29