Microsoft DirectX 8.1 (C++)

Creating a Depth Buffer

A depth buffer is a property of the device. To create a depth buffer that is managed by Microsoft® Direct3D®, set the appropriate members of the D3DPRESENT_PARAMETERS structure as shown in the following code example.

    D3DPRESENT_PARAMETERS d3dpp; 

    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed               = TRUE;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_COPY_VSYNC;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

By setting the EnableAutoDepthStencil member to TRUE, you instruct Direct3D to manage depth buffers for the application. Note that AutoDepthStencilFormat must be set to a valid depth buffer format. The D3DFMT_D16 flag specifies a 16-bit depth buffer, if one is available.

The following call to the IDirect3D8::CreateDevice method creates a device that then creates a depth buffer.

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                  &d3dpp, &d3dDevice ) ) )
    return E_FAIL;

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 recreated in the new size.

To create a new depth buffer surface, use the IDirect3DDevice8::CreateDepthStencilSurface method.

To set a new depth-buffer surface for the device, use the IDirect3DDevice8::SetRenderTarget method.

To use the depth buffer in your application, you need to enable the depth buffer. For details, see Enabling Depth Buffering.