Vulkan View Depth from Faramebuffer Depth

Matthew Wellings 18-Nov-2016

This note gives the mathematical derivation of the equation that recovers view space depth in the fragment shaders of the blog post Depth Peeling Pseudo-Volumetric Rendering.

10000-1000012120001 2nr-l0r+lr-l002nt-bt+bt-b000-(f+n)f-n-2nff-n00-10 = 2nr-l0r+lr-l00-2nt-b-t+bt-b000-2nf2(f-n)-nff-n00-10

This resulting matrix is then effectively pre-multiplied by the view space coordinates to give clip space:
xcyczc wc = 2nr-l0r+lr-l00-2nt-b-t+bt-b000-2nf2(f-n)-nff-n00-10 xvxvzv1

This leads to the following equations for the view space to homogeneous clip space conversion:
xc=xv2nr-l+xvr+lr-l
yc=yv-2nt-b+yvt+bt-b
zc=zv-f+n2(f-n)-12-nff-n
wc=-zv

After the conversion to Euclidean NDC space the equation for zc becomes:
zd=zv-zv-f+n2(f-n)-12-1-zvnff-n
or
zd=f+n2(f-n)+12+1zvnff-n

Then after applying the conversion to window coordinates the transformation remains the same (if you have your minDepth and maxDepth to 0 and 1 respectively):
zf=f+n2(f-n)+12+1zvnff-n

Rearranging for zv this becomes:
zv=zf-f+n2(f-n)+12nff-n-1

The terms f+n2(f-n)+12 and nff-n are both constant within a shader with fixed projection matrix allowing us to simplify the equation to be resolved in the shader by taking a=nff-n and b=f+n2(f-n)+12 to give zv=a(zf-b)

Matthew Wellings - Blog

Depth Peeling Pseudo-Volumetric Rendering 25-Sept-2016
Depth Peeling Order Independent Transparency in Vulkan 27-Jul-2016
The new Vulkan Coordinate System 20-Mar-2016
Improving VR Video Quality with Alternative Projections 10-Feb-2016
Playing VR Videos in Cardboard Apps 6-Feb-2016
Creating VR Video Trailers for Cardboard games 2-Feb-2016
Playing Stereo 3D Video in Cardboard Apps 19-Jan-2015
Adding Ray Traced Explosions to the Bullet Physics Engine 8-Oct-2015