Depth buffer

From Valve Developer Community
Revision as of 05:54, 16 January 2011 by Biohazard 90 (talk | contribs) (replacement example)
Jump to navigation Jump to search
Source's depth buffer is very limited

The depth buffer is a texture in which each on-screen pixel is assigned a greyscale value depending on its distance from the camera. This allows visual effects to easily alter with distance. It is used for soft particles.

Source's depth buffer has a very short range of just 192 units, and is also squashed down to fit into a power of two rendertarget (after having being generated in the alpha channel of the full backbuffer). It can be viewed with r_depthoverlay.

Todo: Differences between depth buffer and fog.

Scaling the depth output

The available output range of the depth buffer can be adjusted in every mod that is utilizing a custom shader library. A requirement is to solely render all opaque geometry in a scene with modified shaders; this can either be achieved by manually replacing all shaders used in every VMT file (optimal solution), reloading all materials at runtime (slow) or overriding the actual shader files used by a default shader dll (probably unstable).

Todo: Confirm the last option

The depth information drawn to the backbuffer is scaled by this factor ( src\materialsystem\stdshaders\common_ps_fxc.h ):

#define OO_DESTALPHA_DEPTH_RANGE (g_LinearFogColor.w)

Changing the definition of the constant g_LinearFogColor would also result in a different range, but it seems to be inaccessible from the publicly available source code. The value describes how the homogenous depth is transformed into normalized space, so if one was to describe the whole scene in the alpha channel, the value should be 1 / (zFar - zNear) or 1 / projectedPosition.w. Otherwise you should use 1 / desired_range to calculate your constant, keep in mind that your interval starts at zNear (default 7).

To extend the range to maximum distance of 1024 units the definition should be changed to this:

#define OO_DESTALPHA_DEPTH_RANGE 0.00098 // 1 / ( 1024 - 7 )
Note.pngNote:By default the output is compressed to 8-bit precision, this will cause heavy colour banding when a long range is used.