Compartilhar via


Depth Buffering

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

Depth buffering is an operation that halts processing on pixels that are obscured by pixels that have already been rendered at the same position and that are nearer to the viewer. The Direct3D Mobile's depth buffer support halts processing on a pixel whose depth value fails a test with the depth value stored in the depth buffer at the given point. Depth buffering support is controlled by a variety of render states and requires the existence of a depth buffer.

Depth buffering operations occur very early in the rasterization phase. Direct3D Mobile performs the depth test as soon as a depth value has been generated for a pixel by interpolation. That way the pixel may be discarded before any time is spent on unnecessarily processing the pixel for effects that could never be seen.

The results of the depth buffer test are temporarily stored per pixel for use in the stencil buffering test. In fact, stencil buffer processing is performed on pixels that fail the depth test, even though no other pixel processing is performed. If the pixel passes the depth buffer test, the pixel's depth value is passed further down the pipeline to the pixel write stage.

Depth Buffer Properties

The depth buffer is a channel in the depth stencil buffer. It is created implicitly as part of the creation of the default swap chain at device creation time. For more information, see Creating a Direct3D Mobile Device. The format of the depth buffer determines how many bits are available for depth buffering. The depth buffer must always have the same x and y pixel dimensions as the render target.

If a depth buffer was not created as part of the device creation, then the depth buffer functionality is off and may not be turned on.

Depth Comparison

The depth comparison occurs after the rasterizer has interpolated the depth value has for the pixel. Microsoft® Direct3D Mobile® fetches the depth value corresponding to that pixel from the depth buffer and then compares it to the interpolated value using a depth comparison function. The depth value from the depth buffer is treated as the reference value in this comparison. If the function returns TRUE, then pixel processing continues. If the function returns FALSE, then processing for the pixel is halted immediately and the next pixel is processed.

The value of these render states can be set by calling with the D3DMRS_SCOPESCALEDEPTHBIAS and D3DMRS_DEPTHBIAS flags containing the appropriate values.

You can specify which depth comparison function to use by calling the IDirect3DMobileDevice::SetRenderState method and setting the D3DMRS_ZFUNC render state (see D3DMRENDERSTATETYPE) to a value from the D3DMCMPFUNC enumeration.

Z and W Buffering

Direct3D Mobile drivers can support either Z buffering or W buffering. You can set a value for the D3DMRS_ZENABLE render state to indicate which type of depth buffering you would prefer to use in your application.

Depth Biasing

When two primitives are coplanar it is neither one has an inherent rendering priority over the other. This results in an effect know as Z-fighting where the two primitives may appear bleed through one another.

To help mitigate Z-fighting, Direct3D Mobile allows you to add a biasing factor pixel's depth value before it is tested against the value in the depth buffer. The depth biasing support in Direct3D Mobile is similar to DirectX 9 implementation for Windows-based desktop systems in that it uses the D3DMRS_DEPTHBIAS and D3DMRS_SLOPESCALEDEPTHBIAS render states.

In Direct3D Mobile there are two biasing values that can be set, D3DMRS_SLOPESCALEDEPTHBIAS and the D3DMRS_DEPTHBIAS render states – both of which can be used to compute the depth offset. The offset is added to the primitive's interpolated depth value to produce a final depth value that is used for depth testing.

The offset used for biasing calculations is defined as the following:

Offset = m * D3DMRS_SLOPESCALEDEPTHBIAS + D3DMRS_DEPTHBIAS

Where m is the maximum depth slope of the triangle being rendered and is defined as:

m = max(abs( delta z / delta x ), abs( delta z / delta y ))

You can enable depth biasing can be enabled by setting values for one or both of these render states. The default value for both render states is zero. Setting both render states to zero disables depth biasing. The units for the D3DMRS_DEPTHBIAS and D3DMRS_SLOPESCALEDEPTHBIAS render states depend on whether the application uses Z-buffering or W-buffering. The application must provide suitable values.

Depth bias is not applied to point or line primitives. However, bias does needs to be applied to triangles drawn in wireframe mode.

See Also

Concepts

Per Pixel Operations