Creating a Depth Buffer

You usually create a depth buffer during your application's startup sequence, before creating a Direct3D device object for rendering. Use the following steps to create and attach a depth buffer surface to the render target surface:

To create a depth buffer

  1. Call the IDirect3D3::EnumZBufferFormats method to determine the depth-buffer pixel formats that the device supports.
  2. Prepare a DDSURFACEDESC2 structure that describes a DirectDraw surface that matches the render target surface's dimensions, includes the DDSCAPS_ZBUFFER capability flag, and uses a supported depth-buffer pixel format (retrieved in Step 1).
  3. Create the surface in video memory or system memory, depending on what type of rendering device your application will use. (See note.)
  4. Attach the depth buffer surface to the rendering surface using the IDirectDrawSurface4::AddAttachedSurface method.

After creating the depth buffer surface and attaching it to the render target surface, call the IDirect3D3::CreateDevice method to create a rendering device that uses the render target surface and its depth buffer.

Create the depth buffer in video memory–with the DDSCAPS_VIDEOMEMORY surface capability–when your application uses a hardware driver (HAL device), and in system memory–the DDSCAPS_SYSTEMMEMORY surface capability–when using software emulation drivers (the MMX or RGB devices). Failing to create a depth buffer in the appropriate type of memory will cause the CreateDevice method to fail.

Note  Some popular hardware devices require that the render target and depth buffer surfaces use the same bit depth. On such hardware, if your application uses a 16-bit render target surface, the attached depth buffer must also be 16-bits. For a 32-bit render target surface, the depth buffer must be 32-bits, of which 8-bits can be used for stencil buffering (if needed).

If the hardware upon which your application is running has this requirement, and your application fails to meet it, any attempts to create a rendering device that uses the non-compliant surfaces will fail. You can use the DirectDraw method, IDirectDraw4::GetDeviceIdentifier to track hardware that imposes this limitation.