Step 3: Attach the Depth Buffer

Once the depth buffer is created, you need to attach it to the surface that will be used as the render target. Do this by calling the IDirectDrawSurface4::AddAttachedSurface method of the render-target surface. The ZBuffer sample performs this with the following code:

    // Attach the z-buffer to the back buffer.
    if( FAILED( hr = g_pddsBackBuffer->AddAttachedSurface( g_pddsZBuffer ) ) )
        return hr;
 

Once the depth buffer is attached to the render-target surface, the system will automatically use the depth buffer whenever depth buffering is enabled, as discussed in Step 4: Enable Depth Buffering.