What is template buffering (stencil buffer)

Source: Internet
Author: User

Link http://blog.csdn.net/xuxinhua/article/details/6853244 What is template buffering (stencil buffer)

A template buffer is similar to a depth buffer. In fact, it uses part of the depth buffer (hence, depth buffers are often called depth-stencil buffers). Depth buffering lets programmers set up a template function to test the "reference (Reference)" Template value-a global value – the pixels are drawn each time the value is still in the template buffer.

The result of the template test determines whether the pixel's color value is to be written to the render target, and whether the depth value of the pixel is written to the depth buffer.

For example, when a reference template value of 0 o'clock draws some objects into the scene, and the template buffer is cleared to 1, the template buffer will be 0 when the object is drawn. If you then set the reference value to 1, and the stencilfunction is set to comparefunction.lessequal, only those pixels of the corresponding region whose template value is not 0 are drawn. This is the basic usage of creating a limit on the current drawing area using template buffering.

There are many more advanced uses of template buffering. Specifying Stenciloperations, in addition to being replaceable or discarded, can also reduce or increase the template buffer after each template test, which can be combined with the Stencilmask value to ensure that the template test only works on a portion of the template buffer area.

To use template buffering, Depthformat must retain some bytes for template buffering. The DEPTHFORMAT.DEPTH24STENCIL8 depth format uses 8 bits for a template buffer. When you combine this format with the Renderstate.stencilmask attribute, it can provide 8 different template buffers. The DEPTH24STENCIL4 depth format uses 4 bits for template buffering, while the DEPTHFORMAT.DEPTH15STENCIL1 format allows only 1 bits. You can pass Clearoptions.stencilbuffer to the Graphicsdevice.clear method to clear the template buffer individually.

You can use the Depthstencilbuffer class to create your own depth-stencil buffers. When creating a custom render target, you need to create your own depth-stencil buffer.

The depth buffer is a buffer of the same size as your render target, which records the depth of each pixel.

When a pixel is drawn for the second time-for example, when an object is drawn after another object-the depth buffer either retains the previous depth value or replaces the current depth value with the depth value of the second pixel. Which depth is left behind depends on the depth function you choose. For example, if the current depth function is comparefunction.lessequal, only values less than or equal to the current depth value will be preserved, and values greater than the current depth value will be discarded. This is called a depth test and is tested in depth each time the pixel is drawn. When a pixel is deeply tested, its color is written to the render target, and the depth is written to the depth buffer.

The depth value of a pixel is determined by the view matrix and projection matrix. The pixel depth value on the near-cut plane is 0, and the depth value of the pixel on the far-cut plane is 1. Each object in the scene needs to be drawn, usually the pixels closest to the camera are preserved, which blocks the visibility of the objects behind them.

The depth buffer usually also contains the stencil bits– so the depth buffer is called the depth-stencil buffer. The depth format (depth format) represents the construction of a depth buffer. Depth buffers are always the same as bits, but can be combined in different ways, similar to texture formatting. The common depth format is DEPTH32, which is used to store depth information in this format. Another common format is DEPTHFORMAT.DEPTH24STENCIL8, which uses bits for depth calculations and 8 bits for template buffering (stencil buffer). Depthformat.depth24stencil8single is not commonly used, and this format uses the number of bits to calculate depth buffers in floating-point numbers. Use the Autodepthstencilformat property in Presentationparameters to set the default depth format.

The

uses renderstate.depthbufferenable to turn depth buffering on or off. Use Renderstate.depthbufferfunction to change the comparison function for deep testing. Passing Clearoptions.depthbuffer to the Graphicsdevice.clear method provides a clear depth buffer. Use the Depthstencilbuffer class to create your own depth buffers. See example how to:create a Depth Texture Learn how to create a custom depthstencilbuffer.

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.