Visualization of linear depth

If you write some screen-space shaders, often you have to get the position in world space or position in view space.
And they are reconstructed from the depth buffer in most cases.

They are many ways to do it, and this good article explain it well.

This is a very simple result of visualizing linear depth buffer in view-space. To do this, I used the approach (a0).


I picked up some methods to reconstruct positions from the depth value below.

(a0) Get position in view-space by using normalized view-space z as depth.

  • VS for prepare
    • Compute depth in view-space.
  • PS for prepare
    • Output ( depth_view_CS / cFar );
  • PS for shading
    • float3 pos_view_CS is a ray direction in view-space.
    • Fetch "depth = ( depth_view_CS / cFar )" from a texture.
    • pos_view_CS = ( ray_view_CS depth * cFar );

(a1) Get position in world-space by using normalized view-space z as depth.

  • In (a0), you have to transfer position in view coordinate to position in world coordinate in the shading pass.

(b0) To get position in view-space, use HW depth buffer

  • In the shading pass, fetch the HW depth buffer and convert it to get the linear depth value in view-space.
  • Get the position in view-space.

(b1) To get position in world-space, use HW depth buffer

  • In (b0), you have to transfer position in view-space to position in world-space.