Logarithmic Depth

中文English

Abstract

What logarithmic depth is and why it is useful.

Contents
  1. Why Use Logarithmic Depth?
  2. Computing Logarithmic Depth
  3. Refining the Logarithmic Function
  4. Open Issue

Why Use Logarithmic Depth?

Under perspective projection, the NDC z coordinate $z_d$ and the negated view-space z coordinate $z_v$ are related by:

$$ \begin{alignedat}{13} z_d & = \frac{f+n}{f-n} + \frac{ \frac{2fn}{n-f} }{ z_v } \\ & = a + \frac{b}{z_v}, z_v \in [n, f] \end{alignedat} $$

Here $n$ and $f$ are the near and far planes, respectively. $z_v$ measures distance along the camera's view-space negative Z axis. For $n=0.1, f=100$, the curve for $z_d$ looks like this:

More than half the range of $z_d$ maps to just the interval $[0.1, 0.2]$ of $z_v$. Mathematically this is fine, but computers store floating-point values at finite precision. A 32-bit IEEE 754 float has roughly seven to eight significant decimal digits.

For example, 8,158,151.1 is stored as 8,158,151.0, preserving only the first seven digits. The unit in the last place (ULP) for the interval $[2^{22}, 2^{23}]$, or $[4194304, 8388608]$, is $(2^{23} - 2^{22}) / 2^{23} = 0.5$. The next representable value after 8,158,151 is 8,158,151.5, so there is no float for 8,158,151.1.

A depth buffer commonly stores depth as a 24- or 32-bit normalized unsigned integer, or as a 32-bit float. For a 24-bit normalized buffer, for example, the shader's floating-point depth is converted to an unsigned integer for storage and converted back to a float when read.

In a small scene, conventional nonlinear depth mapping still gives distinct values of $z_d$ for distances separated by centimeters. Here is an example with $f=100, n=0.1$:

$z_v$$z_d$
0.1-1
......
99.001$\frac{100.1-\frac{20}{\red{99.001}}}{99.9} = 0.9999797981838565 ≈ 0.999979794025421142578125 \text{(same as 99.002)}$
99.002$\frac{100.1-\frac{20}{\red{99.002}}}{99.9} = 0.9999798186097453 ≈ 0.999979794025421142578125$
99.51$\frac{100.1-\frac{20}{\red{99.51}}}{99.9} = 0.9999901418854287 ≈ 0.999990165233612060546875$
99.52$\frac{100.1-\frac{20}{\red{99.52}}}{99.9} = 0.9999903440417908 ≈ 0.99999034404754638671875$
100$\frac{100.1-\frac{20}{100}}{99.9} = 1.0 $

This mapping breaks down when the far plane needs to be tens of kilometers away, and even more so when a planet-scale scene includes the whole Earth, with a radius of roughly 6,600,000 m.

Set the far plane to $f=10^9$ and the near plane to $n = 0.1$:

$z_v$$z_d$
......
1000.01$0.99980000219996 ≈ 0.999800026416778564453$
1001.02$0.9998002000001799 ≈ 0.999800026416778564453$
700,000,000$0.9999999999142857 ≈ 1$
800,000,000$0.99999999995 ≈ 1$
......

All of the depths from 700,000,000 m through 1,000,000,000 m are stored as 1. In practice, finite-precision arithmetic during the depth calculation can make the result worse.

The projection maps view-space z to NDC z through a negative reciprocal. A small portion of $z_v$ consumes most of the range of $z_d$. Many larger $z_v$ values then collapse to the same $z_d$, causing z-fighting in large scenes. Single-precision arithmetic and storage also contribute. A logarithmic function distributes the available depth values more evenly and can reduce the problem.

Computing Logarithmic Depth

The vertex shader computes the desired NDC depth. It can only output clip-space coordinates, and the graphics API divides them by w to obtain NDC. To compensate, multiply the desired depth by clip-space w before writing it.

We use this logarithmic function [1] :

$$ z = \frac{\ln{(z_v \cdot C + 1)}}{\ln{(f \cdot C + 1)}} $$

Its range is $[0,1]$. Here f is the far plane, and C is a tunable constant that controls depth resolution close to the near plane. OpenGL's NDC depth range is $[-1,1]$, so remap the function to $[-1, 1]$:

$$ z = 2 \frac{\ln{(z_v \cdot C + 1)}}{\ln{(f \cdot C + 1)}} - 1 $$

In addition to the constants $n$, $f$, and $C$, we need the view-space depth $z_v$. Clip-space w gives the distance along the negative view-space z axis. Multiply the vertex by the MVP matrix, then replace its clip-space z:

void main ()
{
  gl_Position = uMVP * vec4(aPosition, 1.0);

  float z_v = gl_Position.w;
  gl_Position.z = log(z_v*C + 1.0) / log(f*C + 1.0);
  gl_Position.z = 2.0 * gl_Position.z - 1.0;  // 把 [0, 1] 映射到 [-1, 1]
  gl_Position.z *= gl_Position.w;             // 补偿图形 API 的透视除法
}

This produces logarithmically distributed depth values.

The plot compares conventional and logarithmic depth:

Here $n=0.1,f=100,C=0.5$. The blue curve for conventional depth is very steep and becomes steeper as the far plane $f$ moves away.

For a given $C$, a far plane $f$, and an $n$-bit depth buffer, the resolution at distance $x$ is:

$$ \frac{\ln{(C \cdot f + 1)}}{(2^{n} - 1) \cdot \frac{C}{C \cdot x + 1}} $$

The vertex shader computes logarithmic depth only at the vertices. Interpolation across a primitive can deviate from the desired value, especially for nearby objects. The rasterizer interpolates depth linearly. Varying values use perspective-correct interpolation. Pass a varying and write the correct depth using gl_FragDepth in the fragment shader. Using gl_FragDepth can increase bandwidth and disable depth optimizations such as early Z.

Fragment depth is interpolated linearly. Perspective-correct interpolation of varyings accounts for clip-space w.

Use a varying in the vertex shader for perspective-correct interpolation:

out float vDepthPlusOne;

void main ()
{
  gl_Position = uMVP * vec4(aPosition, 1.0);

  vDepthPlusOne = gl_Position.w*C + 1.0;
}

Use that interpolated varying to set the depth in the fragment shader:

in float vDepthPlusOne;

void main()
{
  outColor = xxx;

  gl_FragDepth = log(vDepthPlusOne) / log(f*C + 1);
}

Refining the Logarithmic Function

To improve shader performance and address clipping, use this logarithmic function instead [2] :

$$ z = 2 \frac{\log_{2}{(z_v + 1)}}{\log_{2}{(f + 1)}} - 1 $$

There are three reasons for the change:

  1. The shader implements log in terms of log2. Using log2 directly saves an extra multiplication.
  2. The constant C controlled the precision distribution, but the available precision is usually more than sufficient. Setting $C=1$ produces good results.
  3. log is undefined for values less than or equal to zero. If a triangle vertex lies far enough behind the camera, at $\leq -1$, the entire triangle may be rejected before clipping.

Set C to 1.0 in the vertex shader:

out float vDepthPlusOne;

void main ()
{
  gl_Position = uMVP * vec4(aPosition, 1.0);

  vDepthPlusOne = gl_Position.w + 1.0;
}

Update the fragment shader as follows:

in float vDepthPlusOne;
uniform float uOneOverLog2FarPlusOne;

void main()
{
  outColor = xxx;

  gl_FragDepth = log2(max(1e-6, vDepthPlusOne)) * uOneOverLog2FarPlusOne;
}

uOneOverLog2FarPlusOne is $1.0 / \log_2(far + 1.0)$. Supplying it as a uniform avoids repeating the same computation for each fragment. Fewer operations can also improve precision.

Open Issue

Recover the corresponding conventional depth from a stored logarithmic depth value.

[1] Logarithmic Depth Buffer, Outerra, August 12, 2009, https://outerra.blogspot.com/2009/08/logarithmic-z-buffer.html. ↩
[2] Logarithmic depth buffer optimizations & fixes, Outerra, July 18, 2013, https://outerra.blogspot.com/2013/07/logarithmic-depth-buffer-optimizations.html. ↩
Loading Disqus comments…