DirectX SDK

Step 1: Enumerate Depth-Buffer Formats

[C++]

This section pertains only to application development in Visual Basic. See Direct3D Immediate Mode C/C++ Tutorials.

[Visual Basic]

Before you can create a depth buffer, you must determine what depth-buffer formats, if any, are supported by the rendering device. Call the Direct3D7.GetEnumZBufferFormats method to enumerate the depth-buffer formats that the device supports. The ZBuffer application uses the following code to enumerate depth-buffer formats:

    ' Create the z-buffer after creating the backbuffer and before creating the
    ' Direct3D device. Note: before creating the z-buffer, applications may want to check
    ' the device capabilities for the D3DPRASTERCAPS_ZBUFFERLESSHSR flag. This flag is true 
    ' for certain hardware that can do HSR (hidden-surface removal) without a z-buffer.
    ' For those devices, there is no need to create a z-buffer.
    '
    Dim ddpfZBuffer As DDPIXELFORMAT
    Dim d3dEnumPFs As Direct3DEnumPixelFormats
        
    Set g_d3d = g_dd.GetDirect3D
    Set d3dEnumPFs = g_d3d.GetEnumZBufferFormats("IID_IDirect3DRGBDevice")

The GetEnumZBufferFormats method accepts the globally unique identifier (GUID) of the device for which the formats will be enumerated. The ZBuffer application uses a hard-coded GUID; however, a real-world application would implement a testing mechanism to determine the GUID of the rendering device, either IID_IDirect3DRGBDevice or IID_IDirect3DHALDevice.

Now that the formats for the device have been enumerated, you can cycle through each of the supported depth-buffer formats:

    Dim I As Long
          
    For I = 1 To d3dEnumPFs.GetCount()
        Call d3dEnumPFs.GetItem(I, ddpfZBuffer)
        If ddpfZBuffer.lFlags = DDPF_ZBUFFER Then
            Exit For
        End If
    Next I

When Direct3DEnumPixelFormats.GetItem is called, it passes as its second argument a DDPIXELFORMAT type describing the pixel format of the depth buffer. The lFlags member will contain DDPF_ZBUFFER for any pixel formats that include depth-buffer bits. As its first argument, GetItem takes an index into the array of the enumerated formats. If a suitable format is found, GetItem saves the pixel format into the ddpfZBuffer parameter, a DDPIXELFORMAT type, and the loop is terminated.

For simplicity, this tutorial only uses z-buffers, which are the most common type of depth buffer. It ignores any other formats (such as DDPF_STENCILBUFFER) that the system enumerates. Applications could also check the bit depth of the z-buffer (8-, 16-, 24-, 32-bit) and make a choice based on that as well.

After you determine the format of the depth buffer, you can create a DirectDrawSurface to use that format, which is the topic of Step 2: Create the Depth Buffer.