Concepts and working principles of templates

Source: Internet
Author: User

The template buffer is used to control whether a pixel is used to draw a scene to the rendering target surface in one pixel. At the most basic level of template buffering, it can make the program mask part of the rendered image so that it cannot be displayed.

The template buffer is an additional buffer that records the information of each pixel, much like a Z buffer. In fact, the buffer will reside in some bits of the Z buffer. Common templates/Z buffer formats are 15-bit Z and 1-bit templates, or 24-bit Z and 8-bit templates, as shown in 7-1. When plotting a polygon, you can perform a simple mathematical operation on the template content for each pixel. For example, you can increase or decrease the template buffer, or reject pixel rendering when the template value fails a simple comparison test. You can mark an area of the frame buffer, and then depict only the marked (or unmarked) area. The above operations are very useful for such effects, and various volume effects are good examples, such as the shadow volume.


Figure 7-1 storage format of the template

The working principle of template buffering is similar to that of deep buffering. The time when template buffering works is after all 3D models have been projected into the target buffer zone. The reason for this is that the template will perform a template test on each pixel to be drawn. The template test uses the template reference value, template mask, and template comparison function, and the template value corresponding to the current pixel as the parameter. After the test, the values in the template buffer are processed according to the test results. Finally, when rendering the final effect, the system will make the appropriate rendering Effect Based on the template buffer value. It can be seen that the nature of the template buffering technology is similar to that of the Depth buffering technology. It provides a standard for comparison to determine whether to display a certain pixel. See Figure 7-2.

Direct3d performs a pixel-by-pixel-based test on the template buffer. For each pixel on the target surface, it uses the corresponding value in the template buffer-the template reference value and the template mask value to perform the test. If the test passes, direct3d executes an action. Perform the following steps for testing:
(1) perform bitwise AND operations on the template reference value and template mask;
(2) Place the template buffer of the current pixel in the template mask for bitwise AND operation;
(3) Use the comparison function to compare the results obtained in the first two steps.

To write pseudo-code, follow these steps:
(Stencilref & stencilmask) compfunc
(Stencilbuffervalue & stencilmask)

In the preceding formula, stencilbuffervalue indicates the template buffer content of the current pixel. The pseudocode uses the & symbol to represent the bitwise and operation. stencilmask indicates the template mask value; and stencilref indicates the template reference value; compfunc is a comparison function.


Figure 7-2 template Workflow

If the template test passes, the current pixel is written to the target surface. If the template fails, the current pixel is ignored. By default, the write pixel operation (d3dcmp_always flag) is performed no matter what result is obtained by each bit-by-bit operation ). You can change the rendering status of d3drenderstate_stencilfunc to declare the required comparison function by passing a member of The d3dcmpfunc Enumeration type.

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.