DepthStencilBuffer Class
Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)
A depth stencil buffer is where depth and stencil data are kept for a render target. The depth buffer is used to determine which 3D objects appear behind other 3D objects; the stencil buffer is used for special effects. Depth stencil buffers are often created at the same time render targets are created. Depth stencil buffers must be recreated when the device is reset. The DepthStencilBuffer property on GraphicsDevice points to the current DepthStencilBuffer.
To use a DepthStencilBuffer, you must:
- Create the DepthStencilBuffer.
shadowDepthBuffer = GfxComponent.CreateDepthStencil(shadowRenderTarget, DepthFormat.Depth24Stencil8Single);public static DepthStencilBuffer CreateDepthStencil(RenderTarget2D target) { return new DepthStencilBuffer(target.GraphicsDevice, target.Width, target.Height, target.GraphicsDevice.DepthStencilBuffer.Format, target.MultiSampleType, target.MultiSampleQuality); } public static DepthStencilBuffer CreateDepthStencil(RenderTarget2D target, DepthFormat depth) { if (GraphicsAdapter.DefaultAdapter.CheckDepthStencilMatch(DeviceType.Hardware, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Format, target.Format, depth)) { return new DepthStencilBuffer(target.GraphicsDevice, target.Width, target.Height, depth, target.MultiSampleType, target.MultiSampleQuality); } else return CreateDepthStencil(target); }
- Set the DepthStencilBuffer using DepthStencilBuffer. It is a good idea to cache the current DepthStencilBuffer beforehand.
// Cache the current depth buffer DepthStencilBuffer old = GraphicsDevice.DepthStencilBuffer; // Set our custom depth buffer GraphicsDevice.DepthStencilBuffer = shadowDepthBuffer;
- Draw into your DepthStencilBuffer.
// Render the shadow map GraphicsDevice.Clear(Color.Black); DrawScene(MyEffect.shadowMap); - Reset the original DepthStencilBuffer.
// Reset the depth buffer GraphicsDevice.DepthStencilBuffer = old;
Custom depth stencil buffers are normally used in conjunction with a custom RenderTarget. How To: Create a Depth Texture has an example of this.
Concepts
Tasks
How To: Draw a Shadow
How To: Create a Depth Texture
How To: Implement Shadow Mapping