R2 Shaders
R2LogDepth.h
Go to the documentation of this file.
1 #ifndef R2_LOG_DEPTH_H
2 #define R2_LOG_DEPTH_H
3 
4 /// \file R2LogDepth.h
5 /// \brief Logarithmic depth functions.
6 
7 ///
8 /// Prepare an eye-space Z value for encoding. See R2_logDepthEncodePartial.
9 ///
10 /// @param z An eye-space Z value
11 /// @return The prepared value
12 ///
13 
14 float
16  const float z)
17 {
18  return 1.0 + (-z);
19 }
20 
21 ///
22 /// Partially encode the given _positive_ eye-space Z value. This partial encoding
23 /// can be used when performing part of the encoding in a vertex shader
24 /// and the rest in a fragment shader (for efficiency reasons) - See R2_logDepthPrepareEyeZ.
25 ///
26 /// @param z An eye-space Z value
27 /// @param depth_coefficient The depth coefficient used to encode \a z
28 ///
29 /// @return The encoded depth
30 ///
31 
32 float
34  const float z,
35  const float depth_coefficient)
36 {
37  float half_co = depth_coefficient * 0.5;
38  float clamp_z = max (0.000001, z);
39  return log2 (clamp_z) * half_co;
40 }
41 
42 ///
43 /// Fully encode the given eye-space Z value.
44 ///
45 /// @param z An eye-space Z value
46 /// @param depth_coefficient The depth coefficient used to encode \a z
47 /// @return The fully encoded depth
48 ///
49 
50 float
52  const float z,
53  const float depth_coefficient)
54 {
55  float half_co = depth_coefficient * 0.5;
56  float clamp_z = max (0.000001, z + 1.0);
57  return log2 (clamp_z) * half_co;
58 }
59 
60 ///
61 /// Decode a depth value that was encoded with the given depth coefficient.
62 /// Note that in most cases, this will yield a _positive_ eye-space Z value,
63 /// and must be negated to yield a conventional negative eye-space Z value.
64 ///
65 /// @param z The depth value
66 /// @param depth_coefficient The coefficient used during encoding
67 ///
68 /// @return The original (positive) eye-space Z value
69 ///
70 
71 float
73  const float z,
74  const float depth_coefficient)
75 {
76  float half_co = depth_coefficient * 0.5;
77  float exponent = z / half_co;
78  return pow (2.0, exponent) - 1.0;
79 }
80 
81 #endif // R2_LOG_DEPTH_H
float R2_logDepthEncodePartial(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:33
float R2_logDepthEncodeFull(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:51
float R2_logDepthDecode(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:72
float R2_logDepthPrepareEyeZ(const float z)
Definition: R2LogDepth.h:15