Querying for Depth Buffer Support
As with any feature, the driver that an application uses might not support all types of depth buffering. Always check the driver's capabilities. Although most drivers support z-based depth buffering, not all support w-based depth buffering (w-buffering). Drivers do not fail if you attempt to enable an unsupported scheme. Instead, they fall back on another depth buffering method, or sometimes disable depth buffering altogether, which can result in scenes rendered with extreme depth-sorting artifacts.
To check for general support for depth buffers, query Microsoft Direct3D for the display device that your application will use before you create a Direct3D device. If the Direct3D object reports that it supports depth buffering, any hardware devices created from it will support z-buffering.
To query for depth buffering support, use the Manager.CheckDeviceFormat method, as shown in the following C# code example.
[C#]
AdapterInformation ai = Manager.Adapters.Default;
// Verify that the depth format exists.
if (Manager.CheckDeviceFormat(ai.Adapter,
DeviceType.Hardware,
adapterFmt,
Usage.DepthStencil,
ResourceType.Surface,
DepthFormat.D16,
out result))
{
return true;
}
return false; // If the call fails, the error code is passed back in the result.
The Manager.CheckDeviceFormat method allows you to choose a device to create based on the capabilities of that device. In this case, devices that do not support 16-bit depth buffers are rejected.
Using Manager.CheckDepthStencilMatch to determine depth-stencil compatibility with a render target is illustrated in the following C# code example.
[C#]
// Reject devices that cannot create a render target of RTFormat while
// the back buffer is of RTFormat and the depth-stencil buffer is
// at least 8 bits of stencil.
AdapterInformation ai = Manager.Adapters.Default;
if (Manager.CheckDepthStencilMatch(ai.Adapter,
DeviceType.Hardware,
adapterFmt,
RTFormat,
DepthFormat.D24S8))
{
return true; // Device can create the render target.
}
return false; // Reject the device; it cannot create the render target.
When you know that the driver supports depth buffers, you can verify w-buffer support. Although depth buffers are supported for all software rasterizers, w-buffers are supported only by the reference rasterizer, which is not suited for use by real-world applications. Regardless of the type of device an application uses, support for w-buffers should be verified before any attempt is made to enable w-based depth buffering.
The following procedure demonstrates how to verify support for w-buffers.
- After creating the device, retrieve its Caps structure from the Device.DeviceCaps property.
- The Caps.LineCaps property contains a reference to a LineCaps structure that contains information about the driver's support for rendering primitives.
- If the driver supports w-based depth buffering for that primitive type, check the RasterCaps.SupportsWBuffer property of the RasterCaps structure contained in the Caps.RasterCaps property.