Platform SDK: DirectX |
C++ applications can use alpha testing to control when pixels are written to the render-target surface. By using the D3DRENDERSTATE_ALPHATESTENABLE enumerated value, your application sets the current Direct3D 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 doesn't, Direct3D ignores it. Select the alpha test function with the D3DRENDERSTATE_ALPHAFUNC enumerated value. Your application can set a reference alpha value for all pixels to be compared against by using the D3DRENDERSTATE_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 already at a given pixel (D3DPCMPCAPS_GREATEREQUAL) then the pixel is written, otherwise the rasterizer ignores the pixel altogether, saving the processing required to blend the two colors. The following example checks to see if a given comparison function is supported and, if so, it sets the comparison function parameters required to improve performance during rendering.
// This example assumes that pd3dDeviceDesc is a // D3DDEVICEDESC7 structure that was filled with a // previous call to IDirect3DDevice7::GetCaps. if (pd3dDeviceDesc.dpcTriCaps.dwAlphaCmpCaps & D3DPCMPCAPS_GREATEREQUAL) { dev->SetRenderState( D3DRENDERSTATE_ALPHAREF, (DWORD)0x00000001); dev->SetRenderState( D3DRENDERSTATE_ALPHATESTENABLE, TRUE ); dev->SetRenderState( D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATEREQUAL ); } // If the comparison isn't 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 IDirect3DDevice7::GetCaps method. After retrieving the device capabilities, check the dwAlphaCmpCaps member of the D3DPRIMCAPS structure (contained by the associated D3DDEVICEDESC7 structure) for the desired comparison function. If the dwAlphaCmpCaps member contains only the D3DPCMPCAPS_ALWAYS capability or only the D3DPCMPCAPS_NEVER capability, the driver does not support alpha tests at all.
Applications written in Visual Basic use alpha testing to control when pixels are written to the render-target surface. By using the D3DRENDERSTATE_ALPHATESTENABLE enumerated value, your application sets the current Direct3D 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 doesn't, Direct3D ignores it. Select the alpha test function with the D3DRENDERSTATE_ALPHAFUNC render state. Your application can set a reference alpha value for all pixels to be compared against by using the D3DRENDERSTATE_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 already at a given pixel (D3DPCMPCAPS_GREATEREQUAL) then the pixel is written, otherwise the rasterizer ignores the pixel altogether, saving the processing required to blend the two colors. The following example checks to see if a given comparison function is supported and, if so, it sets the comparison function parameters required to improve performance during rendering.
' This example assumes that d3dDeviceDesc is a ' D3DDEVICEDESC7 type that was filled with a ' previous call to Direct3DDevice7.GetCaps. If (d3dDeviceDesc.dpcTriCaps.lAlphaCmpCaps And D3DPCMPCAPS_GREATEREQUAL) Then Call dev.SetRenderState(D3DRENDERSTATE_ALPHAREF, &H1) Call dev.SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, True) Call dev.SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATEREQUAL) End If ' If the comparison isn't 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 Direct3DDevice7.GetCaps method. After retrieving the device capabilities, check the lAlphaCmpCaps member of the D3DPRIMCAPS type (contained by the associated D3DDEVICEDESC7 type) for the desired comparison function. If the lAlphaCmpCaps member contains only the D3DPCMPCAPS_ALWAYS capability or only the D3DPCMPCAPS_NEVER capability, the driver does not support alpha tests at all.