Stencil Operations
A version of this page is also available for
4/8/2010
Stencil buffers are supported in Microsoft® Direct3D® Mobile as part of the pixel processing pipeline. The stencil operations are programmable via multiple render states that control the various tests and outcomes of the stencil unit. To use a stencil buffer, you must add a stencil channel to your depth buffer when you create your depth buffer. If there is no depth buffer, or if the depth buffer has no stencil channel, then the stencil buffer will not be enabled. This check is not performed in the Direct3D Mobile middleware, but in the driver itself.
All stencil operations are tied to the D3DMRS_STENCILENABLE render state (see D3DMRENDERSTATETYPE). This render state controls whether the stencil buffer and stencil buffer operations are enabled. You can set the value for this render state by calling the IDirect3DMobileDevice::SetRenderState method.
Stencil Inputs
The inputs into the stencil test are the stencil value for this pixel from the stencil buffer, the stencil reference value, and the results of the depth buffering test.
The stencil value for every pixel is read from the stencil buffer. Before that value is advanced to the stencil comparison it, is combined in a bit-wise AND operation with the stencil read mask. The stencil read mask is specified by the value for the D3DMRS_STENCILMASK render state. Bits in the render state value that have no corresponding bits in the stencil buffer value are ignored. For related information see Writing Pixels.
The stencil reference value is controlled by the D3DMRS_STENCILREF render state. Bits in the render state value that have no corresponding bits in the stencil buffer value are ignored.
The results from the depth buffering test are forwarded from the depth processing unit.
Stencil Comparisons
The stencil comparison function is specified by the D3DMRS_STENCILFUNC render state. The value for this render state must come form the D3DMCMPFUNC enumeration.
Stencil Actions
Direct3D Mobile uses both the results from the stencil comparison and the results from the depth comparison to determine which stencil action execute. The possible actions are controlled by D3DMSTENCILOP enumeration values set for the D3DMRS_STENCILFAIL, D3DMRS_STENCILZFAIL, and D3DMRS_STENCILPASS render states. In addition to the specific conditions corresponding to these render states, if either the depth test or the stencil test fail for a pixel then processing on this pixel is halted after the stencil action is complete.