|
|
|
|
|
|
|
|
|
|
// Z buffer to linear 0..1 depth (0 at near plane, 1 at far plane). |
|
|
|
// Does not correctly handle oblique view frustums. |
|
|
|
// zBufferParam = { (f-n)/n, 1, (f-n)/n*f, 1/f } |
|
|
|
float Linear01DepthFromNear(float depth, float4 zBufferParam) |
|
|
|
{ |
|
|
|
return 1.0 / (zBufferParam.x + zBufferParam.y / depth); |
|
|
|
|
|
|
// Does not correctly handle oblique view frustums. |
|
|
|
// zBufferParam = { (f-n)/n, 1, (f-n)/n*f, 1/f } |
|
|
|
float Linear01Depth(float depth, float4 zBufferParam) |
|
|
|
{ |
|
|
|
return 1.0 / (zBufferParam.x * depth + zBufferParam.y); |
|
|
|
|
|
|
// Does not correctly handle oblique view frustums. |
|
|
|
// zBufferParam = { (f-n)/n, 1, (f-n)/n*f, 1/f } |
|
|
|
float LinearEyeDepth(float depth, float4 zBufferParam) |
|
|
|
{ |
|
|
|
return 1.0 / (zBufferParam.z * depth + zBufferParam.w); |
|
|
|
|
|
|
|
|
|
|
// 'z' is the view-space Z position (linear depth). |
|
|
|
// saturate() the output of the function to clamp them to the [0, 1] range. |
|
|
|
// encodingParams = { n, log2(f/n), 1/n, 1/log2(f/n) } |
|
|
|
float EncodeLogarithmicDepth(float z, float4 encodingParams) |
|
|
|
{ |
|
|
|
return log2(max(0, z * encodingParams.z)) * encodingParams.w; |
|
|
|
|
|
|
// saturate(d) to clamp the output of the function to the [n, f] range. |
|
|
|
// encodingParams = { n, log2(f/n), 1/n, 1/log2(f/n) } |
|
|
|
float DecodeLogarithmicDepth(float d, float4 encodingParams) |
|
|
|
{ |
|
|
|
return encodingParams.x * exp2(d * encodingParams.y); |
|
|
|