Share via


Creating a Depth Buffer

A depth buffer is a property of a device. To create a depth buffer that is managed by Microsoft Direct3D, set the appropriate members of the PresentParameters class as shown in the following C# code example.

          [C#]
          

PresentParameters presentParams = new PresentParameters();

presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Copy;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;

Setting the PresentParameters.EnableAutoDepthStencil member to true instructs Direct3D to manage depth buffers for the application. Note that PresentParameters.AutoDepthStencilFormat must be set to a valid depth buffer format. The DepthFormat.D16 value specifies a 16-bit depth buffer, provided one is available.

The following C# call to the Device constructor creates a device that in turn creates a depth buffer.

          [C#]
          

Device device;

// Create a device using the PresentParameters previously set.
device = new Device(0, 
                    DeviceType.Hardware,
                    this,
                    CreateFlags.SoftwareVertexProcessing,
                    presentParams);

The depth buffer is automatically set as the render target of the device. When the device is reset, the depth buffer is automatically destroyed and re-created in the new size. To create a new depth buffer surface, use the Device.CreateDepthStencilSurface method. To set a new depth-buffer surface for the device, use the Device.DepthStencilSurface method.

To use the depth buffer in an application, the depth buffer must be enabled. For more information, see Enabling Depth Buffering.