Directx11 tutorial (48) Roles of depth/stencel Buffer

Source: Internet
Author: User

In d3d11, there are depth/stencel buffer, which correspond to framebuffer, as shown in. one pixel in framebuffer has the corresponding depth buffer and stencel buffer values:

 

In d3d11, depth buffer is defined with stencel buffer. For example, dxgi_format_d24_unorm_s8_uint uses an unsigned 24-bit value as the depth buffer value of the pixel and maps it to [0, 1]. use an 8-bit value to represent the pixel stencel value and map it to [0,255]. Note: In hardware, detph buffer and stencel buffer are actually two independent buffers.

Let's take a look at the functions of depth buffer and stencel Buffer:

In the raster phase, when raster-based primitive (such as a triangle) is implemented, the raster hardware accesses stencel buffer and depth buffer for comparison, this comparison is generally referred to as stencel test and depth test. Only pixel (or sample or fragment is more suitable) of stencel test and depth test can be written into framebuffer.

Is the diagram of stencel test and depth test:

First, stecil test will be performed, that is, the current pixel stencer value is compared with the stencer value corresponding to the stencer buffer. Only the pixel that passes the comparison test will perform the depth test, otherwise, it will be discarded by the raster hardware. The depth test and depth test will be performed to ensure the relationship between the front and back positions of objects in the scene. If both test passes, the depth buffer value will be updated (of course, stencel pass can be used to update or maintain the stencel buffer value based on the stencel function ).

By using stencel test, we can implement the mask effect to achieve some rendering effects, such as mirrors and volume shadows.

For example:

In a frame, we first render a quadrilateral and act as a mirror. Before rendering, clear the depth/stencel buffer value (depth is set to 1.0, stencel is set to 0 ), in this scenario, the stencel buffer is

Next, we set stencel to always pass, set stencel buffer value to 1, and render a mirror (quadrilateral ). This is the value of stencel buffer.

Next, we will render other primitive values. For example, if a triangle is used, first set the stenpencil value to 1 and set the stenpencil comparison function to equal. In this way, the stenpencil value will be compared during triangle rendering, for pixel after triangle grating, if it is within the mirror range, 1 = 1, so stencel pass will keep this pixel, if it is outside the mirror, because the value of stencel buffer is 0, stencel test fail is used to discard this pixel. For example, if the triangle is used, the actual reserved pixels are only the Blue pass stenric test part.

In the following chapter, we will use stencel in d3d11 to implement a mirror effect.

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.