DirectX - Stenciling



Applications which use gaming programming usually use the stencil buffer to mask the respective pixels. Some of the common effects of stenciling which are implemented in DirectX are mentioned below −

  • Compositing
  • Decaling
  • Dissolves, Fades, and Swipes
  • Outlines and Silhouettes
  • Two-Sided Stencil

The stencil buffer in DirectX helps user to enable or disable the drawing patterns to the rendering target surface on pixel-by-pixel basis. The most fundamental level included is enabling the applications to mask sections of the required rendered image which is clearly not depicted. Applications usually use stencil buffers with required special effects and activities such as dissolves, decaling, and outlining.

Stencil buffer information in DirectX is embedded in z-buffer data which is called z coordinate area. The application can use the IDirect3D9::CheckDeviceFormat method to check for hardware stencil support, as mentioned in the code snippet below −

if( FAILED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
   pCaps->DeviceType,
   Format,
   D3DUSAGE_DEPTHSTENCIL,
   D3DRTYPE_SURFACE,
   D3DFMT_D24S8 ) ) )
return E_FAIL;

IDirect3D9::CheckDeviceFormat interface of DirectX allows user to choose a device to create the capabilities of the device. In this situation, devices that do not support 8-bit stencil buffers are rejected.

Working of the Stencil Buffer

Direct3D performs a particular test with the contents of the required stencil buffer on a pixel-by-pixel basis. Every pixel is considered strong with respect to the target surface, includes performing the test for corresponding value in the stencil buffer, a stencil reference value, and a stencil mask value. Direct3D performs an action. The test is performed using the following steps −

  • Perform a bitwise AND operation of the stencil reference value with the stencil mask of DirectX.

  • Perform the same operation with current pixel with the stencil mask.

  • Compare the results generated, using the comparison function.

The current pixel is always written to the target surface if the particular test is successful, and is completely ignored if the test fails. The default comparison behavior is usually written in the pixel, no matter how the bitwise operation turns out is called as D3DCMP_ALWAYS.

The application can usually customize the operation of the stencil buffer which is great functionality of stencil buffer. It can be set as the comparison function, the stencil mask, and the stencil reference value. It usually controls the action that Direct3D or DirectX takes when a particular stencil test passes or fails.

Illustrations

The following code examples are usually considered to demonstrate setting up the stencil buffer −

// Enable stencil testing
pDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);

// Specify the stencil comparison function
pDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);

// Set the comparison reference value
pDevice->SetRenderState(D3DRS_STENCILREF, 0);

// Specify a stencil mask
pDevice->SetRenderState(D3DRS_STENCILMASK, 0);

// A write mask controls what is written
pDevice->SetRenderState(D3DRS_STENCILWRITEMASK, D3DSTENCILOP_KEEP);

// Specify when to write stencil data
pDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);

By default, the stencil reference value is zero. Any integer value is valid. DirectX performs a bitwise AND of the stencil reference value and a stencil mask value before the stencil test.

The user can control what pixel information is written out depending on the stencil comparison.

Advertisements