An Interpretation of Depth Value

 

Recently when I am working on Screen Space Reflection, I noticed there are some subtleties in the computation of depth value.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

In this article I will explain the deeper meaning in the computation of depth value.

 

Perspective Projection Matrix

 

First, it is well known that the DirectX perspective projection matrix is:

An interpretation of depth value 随笔 第1张

In this matrix:

An interpretation of depth value 随笔 第2张 is width of the screen

An interpretation of depth value 随笔 第3张 is height of the screen

An interpretation of depth value 随笔 第4张 is near clipping plane

An interpretation of depth value 随笔 第5张 is far clipping plane

All these values are measured in camera space.

 

Since DirectX uses row vector, the z and w component are:

An interpretation of depth value 随笔 第6张

An interpretation of depth value 随笔 第7张

After perspective division, the actual depth value is:

An interpretation of depth value 随笔 第8张

But what does it mean? Is there any deeper meaning in it? Why not use a simpler one, like linear interpolation?

 

The answer to the second question is yes.

To dive into the deeper meaning, first we express An interpretation of depth value 随笔 第9张 as an expression of An interpretation of depth value 随笔 第10张, we get:

 

An interpretation of depth value 随笔 第11张

Something interesting happens! The depth value is linear interpolation weight for the reciprocal of near and far value!

 

Sounds great, but who cares? Yes, in most cases, you don’t need to care about that. However, in some specific case, it will bring you a lot of convenience. Screen space reflection is an example.

 

Ray Marching in Screen Space

 

In screen space reflection, one need to perform ray marching and find the intersection of a ray and depth buffer. Of course, ray marching can be performed in view space, but it’s difficult to choose a proper step size because the same step size in view space appear smaller and smaller when the ray is moving away from the camera. Alternatively, performing ray marching in screen space can avoid this problem because you can choose the step size based on the pixel size. However, as the depth value is a non-linear function of z value in view space, the step size for depth value is not constant. If you use the projection matrix to calculate the depth value based on the x and y values in every step, that will cause a lot of computation. Is there a fast way to calculate the depth value? Yes! And the linear interpolation nature of the reciprocal of depth value is the heart of this method.

 

To explain this, I would like to formulate the problem first. Suppose you know the view space coordinates of the both ends of a line segment AB, denoted as An interpretation of depth value 随笔 第12张 and An interpretation of depth value 随笔 第13张. The projection matrix is also known, so you can calculate their screen space coordinates An interpretation of depth value 随笔 第14张 and An interpretation of depth value 随笔 第15张. You want to perform linear interpolation on image plane with An interpretation of depth value 随笔 第16张 the interpolation weight. The xy coordinate of point C is easy:

An interpretation of depth value 随笔 第17张

An interpretation of depth value 随笔 第18张

 

An interpretation of depth value 随笔 第19张

But how can I get An interpretation of depth value 随笔 第20张 conveniently, given An interpretation of depth value 随笔 第21张 ? I can calculate An interpretation of depth value 随笔 第22张 given An interpretation of depth value 随笔 第23张, it’s

An interpretation of depth value 随笔 第24张

So, the next step is to know the relationship between s and t.

Now let’s look at the picture below, it’s the equivalent version of the last picture.

An interpretation of depth value 随笔 第25张

Let

An interpretation of depth value 随笔 第26张,

An interpretation of depth value 随笔 第27张.

Then we have:

An interpretation of depth value 随笔 第28张

An interpretation of depth value 随笔 第29张

Also we know

An interpretation of depth value 随笔 第30张

An interpretation of depth value 随笔 第31张

and

An interpretation of depth value 随笔 第32张

Put them together, we have

An interpretation of depth value 随笔 第33张

An interpretation of depth value 随笔 第34张

An interpretation of depth value 随笔 第35张

 

Put it into An interpretation of depth value 随笔 第36张 , we have:

An interpretation of depth value 随笔 第37张

 

An interpretation of depth value 随笔 第38张

An interpretation of depth value 随笔 第39张

Finally, we get

An interpretation of depth value 随笔 第40张

That is to say, instead of using An interpretation of depth value 随笔 第41张 to linear interpolate An interpretation of depth value 随笔 第42张 ,we can use An interpretation of depth value 随笔 第43张 to linear interpolate An interpretation of depth value 随笔 第44张! We’ve already use An interpretation of depth value 随笔 第45张 to linear interpolate xy coordinate. Furthermore, we can consider An interpretation of depth value 随笔 第46张 as a special coordinate axis. So, in the (x’, y’, 1/z) coordinate space, every axis can be linear interpolated!

 

Why is the depth value like that?

From the first section, we know the depth value is the linear interpolation weight for the reciprocal of near and far value.

An interpretation of depth value 随笔 第47张

An interpretation of depth value 随笔 第48张

An interpretation of depth value 随笔 第49张

From the second section, we know (x’, y’, 1/z) can be linear interpolated.

An interpretation of depth value 随笔 第50张

Then we have

An interpretation of depth value 随笔 第51张

An interpretation of depth value 随笔 第52张

That means An interpretation of depth value 随笔 第53张 linearly interpolates depth value, and in the (x’, y’, depth) coordinate space, every axis can be linear interpolated.

An interpretation of depth value 随笔 第54张

To show the benefit of this, let’s see the picture above, suppose there is a line segment in view space, if we choose a naïve depth (for example, linear interpolating near and far value), the line segment will become a curve in (x’, y’, depth) space. However, by using DirectX depth (OpenGL depth is similar), the line segment will be still a line segment!

 

Things that appear straight/planar in view space, will also appear straight/planar in clip space. That is the idea behind the depth computation formula.

 

 



来自为知笔记(Wiz)

附件列表

     

    扫码关注我们
    微信号:SRE实战
    拒绝背锅 运筹帷幄