Template testing
1. Template testing implementation process
As mentioned earlier, we can use template buffers to prevent pixel fragments from rendering to certain areas of the background buffer. The operation of determining whether a specific pixel can be written to the background buffer is called stencil test, and its implementation process is:
if( StencilRef & StencilReadMask ⊴ Value &StencilReadMask)
accept pixel
else
reject pixel
Template testing is conducted during pixel rasterization (i.e. output merging stage). After enabling the template function, each rasterized pixel needs to be template tested with the following two operands:
The left operand (LHS) is obtained by performing a bitwise AND operation on a template reference value (StencilRef) and a template mask (StencilReadMask) specified by the application.
The right operand (RHS) is obtained by performing a bitwise AND operation on the corresponding value (Value) of the current pixel in the template buffer and a template mask (StencilReadMask).
Note that the StencilReadMask in LHS and RHS is the same.
Then, compare LHS and RHS using the comparison function ⊴ specified by the application, and return true or false. When the test result is true, it indicates that the pixel can be written to the background buffer (assuming the depth test also passes). When the test result is false, it indicates that the pixel should be discarded and not written to the background buffer. Of course, if a pixel fails the template test, its depth value will not be written into the depth buffer.
Operator ⊴ can be D3D11_ COMPARISON_ Any function defined by FUNC enumeration type:
typedef enum D3D11_COMPARISON_FUNC
{
D3D11_COMPARISON_NEVER = 1,
D3D11_COMPARISON_LESS = 2,
D3D11_COMPARISON_EQUAL = 3,
D3D11_COMPARISON_LESS_EQUAL = 4,
D3D11_COMPARISON_GREATER = 5,
D3D11_COMPARISON_NOT_EQUAL = 6,
D3D11_COMPARISON_GREATER_EQUAL = 7,
D3D11_COMPARISON_ALWAYS = 8,
} D3D11_COMPARISON_FUNC;
1D3D11_ COMPARISON_ NEVER: Always returns false.
2D3D11_ COMPARISON_ LESS: Use< The operator replaces ⊴.
3D3D11_ COMPARISON_ QUAL: Using== The operator replaces ⊴.
4D3D11_ COMPARISON_ LESS_ Equal: Use the ≤ operator instead of ⊴.
5D3D11_ COMPARISON_ GREATER: Use> The operator replaces ⊴.
6D3D11_ COMPARISON_ NOT_ QUAL: Use it= The operator replaces ⊴.
7D3D11_ COMPARISON_ GREATER_ Equal: Use the ≥ operator instead of ⊴.
8D3D11_ COMPARISON_ ALWAYS: Always returns true.