Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

What Is a Depth Buffer?

A depth buffer is a buffer that is the same width and height as your render target. This buffer records the depth of each pixel that is rendered.

When a pixel is rendered a second time - such as when one object is rendered behind another - the depth buffer will either keep the previous depth value, or replace it with the depth value for the second pixel. Which depth is preserved and which depth is discarded depends on the depth function selected. For example, if CompareFunction.LessEqual is the current depth function, depth values that are less than or equal to the current value are preserved. Any value greater than the current depth value is discarded. This is called the depth test. The depth test occurs every time a pixel is rendered. When a pixel passes the depth test, its color is written to the render target and its depth is written to the depth buffer.

The depth of a pixel is determined based on the size of the view and projection matrix selected for rendering. A pixel that touches the near plane of the projection has depth 0. A pixel that touches the far plane of the projection has depth 1. As each object in the scene is rendered, normally the pixels that are closest to the camera are kept, as those objects block the view of the objects behind them.

The depth buffer may also contain stencil bits - for this reason it's often called the depth-stencil buffer. The depth format describes the composition of the depth buffer. The depth buffer is always 32 bits, but those bits can be arranged in different ways, similar to how texture formats can vary. A common depth format is Depth32, where all 32 bits are reserved for depth information. Another common format is DepthFormat.Depth24Stencil8, where 24 bits are used for depth calcuation and 8 bits are used by the stencil buffer. DepthFormat.Depth24Stencil8Single is a more unusual format where the 24 bits for the depth buffer are arranged as a floating point value. Use the AutoDepthStencilFormat property on PresentationParameters to set the default depth format.

Use RenderState.DepthBufferEnable to enable or disable depth buffering. Use RenderState.DepthBufferFunction to change the comparison function used for the depth test. The depth buffer can be cleared seperately by passing ClearOptions.DepthBuffer to the GraphicsDevice.Clear method. Use the DepthStencilBuffer class to create your own depth buffer. How To: Create a Depth Texture provides an example of how to create a custom DepthStencilBuffer.

Concepts

Tasks

Reference

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.