Partager via


Alpha Testing State

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

Applications can use alpha testing to control when pixels are written to the render-target surface. By using the D3DMRS_ALPHATESTENABLE render state (see D3DMRENDERSTATETYPE), your application sets the current Microsoft® Direct3D® Mobile device so that it tests each pixel according to an alpha test function. If the test succeeds, the pixel is written to the surface. If it does not, Direct3D Mobile ignores the pixel. Select the alpha test function with the D3DMRS_ALPHAFUNC render state. Your application can set a reference alpha value for all pixels to compare against by using the D3DMRS_ALPHAREF render state.

The most common use for alpha testing is to improve performance when rasterizing objects that are nearly transparent. If the color data being rasterized is more opaque than the color at a given pixel (D3DMPCMPCAPS_GREATEREQUAL, see D3DMPCMPCAPS Values), then the pixel is written. Otherwise, the rasterizer ignores the pixel altogether, saving the processing required to blend the two colors. The following code example checks if a given comparison function is supported and, if so, it sets the comparison function parameters required to improve performance during rendering.

// This code example assumes that pCaps is a
// D3DMCAPS structure that was filled with a 
// previous call to IDirect3DMobile::GetDeviceCaps.
if (pCaps.AlphaCmpCaps & D3DMPCMPCAPS_GREATEREQUAL)
{
    dev->SetRenderState(D3DMRS_ALPHAREF, (DWORD)0x00000001);
    dev->SetRenderState(D3DMRS_ALPHATESTENABLE, TRUE); 
    dev->SetRenderState(D3DMRS_ALPHAFUNC, D3DMCMP_GREATEREQUAL);
}
 
// If the comparison is not supported, render anyway. 
// The only drawback is no performance gain.

Not all hardware supports all alpha-testing features. You can check the device capabilities by calling the IDirect3DMobile::GetDeviceCaps method. After retrieving the device capabilities, check the AlphaCmpCaps member of the associated D3DMCAPS structure for the desired comparison function. If the AlphaCmpCaps member contains only the D3DMPCMPCAPS_ALWAYS capability or only the D3DMPCMPCAPS_NEVER capability, the driver does not support alpha tests.

See Also

Reference

D3DMCMPFUNC

Concepts

Render States