Platform SDK: DirectX

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.

[C++]

To create a depth buffer in a C++ application

  1. Call the IDirect3D7::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 IDirectDrawSurface7::AddAttachedSurface method.

After creating the depth buffer surface and attaching it to the render-target surface, call the IDirect3D7::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, IDirectDraw7::GetDeviceIdentifier to track hardware that imposes this limitation.

[Visual Basic]

To create a depth buffer in a Visual Basic application

  1. Call the Direct3D7.GetEnumZBufferFormats method to retrieve a reference to a Direct3DEnumPixelFormats object that can enumerate the supported depth-buffer formats. Query the enumeration object for the depth-buffer pixel formats that the device supports.
  2. Prepare a DDSURFACEDESC2 type 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 2).
  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 DirectDrawSurface7.AddAttachedSurface method.

After creating the depth buffer surface and attaching it to the render-target surface, call Direct3D7.CreateDevice 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, DirectDraw7.GetDeviceIdentifier and the associated object (DirectDrawIdentifier) to track hardware that imposes this limitation.