Depth-stencil testing in OpenGL and D3D11

Source: Internet
Author: User

In OpenGL and D3d11 pipeline, the operation after pixel shader is the depth template test, the depth template test is in sample units, that is, a pixel can have multiple sample points, each sample point has depth information. The depth-stencil test is performed once for each sample point, and if it is MSAA, the final result is to resolve the pixel results of each sample. The following link has an introduction to MSAA.

Http://www.cnblogs.com/mikewolf2002/archive/2012/11/22/2783235.html

The process of deep template testing is as follows:

Using deep-template testing in OpenGL, you first have to enable depth testing and template testing, which can be accomplished by the following two functions:

Glenable (gl_depth_test);

Glenable (gl_stencil_test);

The following function initializes the depth buffer and the stencil buffer value.

Glclearstencil (0); Start at 0

Glcleardepth (1.0f); Start at 1.0

Glclear (Gl_color_buffer_bit | Gl_stencil_buffer_bit); The function resets the depth and stencil values, respectively, 1.0 and 0, that is, the depth value of each pixel is initially 1.0, and the corresponding template value is 0.

In the process of template testing, you can use a comparison mask (comparison mask) with the values in the template buffer to bitwise AND operation, and then compare with the reference value, so as to achieve the value of the template buffer in the position of a certain position on the status of the judgment. In this way, the values in the template buffers can be used not only as a separate whole, but also as a set of bits.

In OpenGL, you can set a comparison condition (comparison function), a reference value (reference value), and a mask for comparison (comparison mask) by calling the Glstencilfunc () function.

Glstencilfunc (gl_equal,//comparison conditions

0x1,//reference value

0xFF); Comparison with masks

The comparison criteria can be set to the following values

Gl_never can never pass

Gl_always can always be passed (default value)

Gl_less less than the reference value can be

Gl_lequal is less than or equal to the

Gl_equal is equal to passing

Gl_gequal is greater than or equal to passing

Gl_greater greater than through

Gl_notequal not equal to passing through

In addition to comparing reference values with template values, we also need to update the values in the template buffers using some actions called template Operations (stencil operation). The template buffer update is closely related to the results of the template test and the results of the depth test. As shown in the flowchart above: the template operation can specify the appropriate update method for the following three cases, respectively.

1. The template test failed.

2. The template test passed, but the depth test failed.

3. The template test passes and the depth test passes.

When one of these cases occurs, a pre-set update operation is performed. In OpenGL, you can use the Glstencilop () function to set the Update method for each of the three cases described above. For example

Glstencilop (Gl_keep,//First case Update method

GL_DECR,//Update method for the second case

GL_INCR); Update method for the third case

However, the following update methods are set:

Gl_keep keeps the current template value unchanged

Gl_zero to set the current template value to 0

Gl_replac to set the current template value to a reference value

GL_INCR adds 1 to the current template value

GL_DECR minus 1 on the current template value

Gl_invert A bitwise inverse of the current template value

We can update the template value by writing a mask (write mask) to specify the position status on the bit bit. OpenGL, the Glstencilmask () function is provided to set the write mask. For example, Glstencilmask (0xFF) can also be turned on or off by Gldepthmask (Gl_false) to turn on or off the deep write function, Gl_true is on, Gl_false is forbidden to write. In the new version of OpenGL, the Glstencilfuncseparate function and the Glstencilopseparate function are allowed by using different template test conditions and template value changes for the front and back sides of the polygon. These two functions are similar to Glstencilfunc and Glstencilop, with only one parameter face at the front, which specifies which faces are currently set. You can choose Gl_front, Gl_back, Gl_front_and_back.

In D3d11, the depth template is set to test various conditions with the depth template state.

Sets the depth template status description.
Depthstencildesc.depthenable = true;
Depthstencildesc.depthwritemask = D3d11_depth_write_mask_all;//d3d11_depth_write_mask_zero Prohibit write depth buffer
Depthstencildesc.depthfunc = d3d11_comparison_less;

Depthstencildesc.stencilenable = true;
Depthstencildesc.stencilreadmask = 0xFF; Same as the third parameter of function Glstencilfunc in OpenGL
Depthstencildesc.stencilwritemask = 0xFF; Just like glstencilmask means.

For front face pixels using the template operation operation, the stencil operation in three different situations
DepthStencilDesc.FrontFace.StencilFailOp = D3d11_stencil_op_keep;
DepthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
DepthStencilDesc.FrontFace.StencilPassOp = D3d11_stencil_op_keep;
DepthStencilDesc.FrontFace.StencilFunc = D3d11_comparison_always;

The template operation mode used for back face pixels.
DepthStencilDesc.BackFace.StencilFailOp = D3d11_stencil_op_keep;
DepthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
DepthStencilDesc.BackFace.StencilPassOp = D3d11_stencil_op_keep;
DepthStencilDesc.BackFace.StencilFunc = D3d11_comparison_always;

Create a depth template state so that it takes effect
result = M_device->createdepthstencilstate (&depthstencildesc, &m_depthstencilstate);
if (FAILED (Result))
{
return false;

}

Sets the depth template state.
M_devicecontext->omsetdepthstencilstate (m_depthstencilstate, 1);

Depth-stencil testing in OpenGL and D3D11

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.