Export (0) Print
Expand All

DepthStencilBuffer Class

Queries and prepares depth stencil buffers.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

public class DepthStencilBuffer : IDisposable

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:

  1. 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);
    }
    
  2. 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;
    
  3. Draw into your DepthStencilBuffer.
    // Render the shadow map
    GraphicsDevice.Clear(Color.Black);
    DrawScene(MyEffect.shadowMap);
    
  4. 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.

Xbox 360, Windows XP SP2, Windows Vista

Community Additions

ADD
Show:
© 2014 Microsoft