Depth buffer

From Valve Developer Community
Revision as of 17:41, 15 January 2011 by Biohazard 90 (talk | contribs)
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 is rendered to the alpha channel of the backbuffer and then copied to a rendertarget which has its dimensions sized to the next lower power of two resolution of the framebuffer. Thus it does not have proper anti-aliasing applied; by default it scales the scene depth to a maximum range of 192 units. 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).

Note.pngNote:By default your output is compressed to 8-bit precision, this will cause heavy colour banding when a long range is used.