Use of occlusion query in d3d10/11

Source: Internet
Author: User

In d3d10/11, there is the d3d10_query/d3d11_query interface. Through the query interface, we can query Some GPU statuses, such as timestamp) [This is often used for performance analysis], or to block query information. The DirectX SDK example shows how to use the occlusion query in d3d10/11. Source file directory:SDK Root\ Samples \ c ++ \ direct3d10 \ drawpredicated

First, let's look at what objects are included in the scenario: 6 telescopes (the telescope is a high-mode object with a large number of mesh objects). There is a beacon-shaped object in the center of the telescope, and then the city model represented by cubes, in addition, there is another undisplayed object, that is, the telescope surround box (Low Mode). When we select render occluders, we can see the surround box.

From the d3d11 tutorial pipeline introduction, we know that when d3d transmits draw to the GPU, it usually goes through command processor-vertex shader-PA (Body assembly)-Rs (grating) -pixel shader-color buffer, these stages (GS and tessllation are not considered ).

A triangle, after a long rendering pipeline, will eventually be displayed on the screen. If it is like a telescope, it will be in our field of vision. If it is like a telescope, there is a wall in front of us, and the objects behind us cannot be seen at all. But these high-mode telescopes, Vs and PS, still need to be done, will consume a lot of GPU time, if we use the occlusion query and prediction function to configure a close-fitting low-mode surround box for the High-mode telescope, We can query the occlusion of the surround box, if the box is in the current frame, all samples (in the deep test, the unit is sample, and you can see the msaa tutorial) are all Z fail, then query

D3d10_query_occlusion_predicate returns false. If any sample z pass exists, true is returned. If false is returned, the surrounding box is completely invisible in the current frame, the telescope in the box is completely invisible. When rendering the telescope, we can start the prediction function to render it based on the occlusion Query Result of the box, at the GPU hardware level, in command processor, it will discard the draw and will not do Vs and PS, which can effectively improve FPS. For example, in the figure below, when prediction is enabled, the FPS is 460 frames. If prediction is not enabled, only 261 frames are used.

 

 

Code implementation is also very simple:

First, define the prediction variable.

Id3d10predicate * g_ppredicate [num_microscope_instances] = {null };

Because there are 6 telescopes in the scenario, an array is used to represent all the prediction variables,

During program initialization, these occlusion query predictions will be created:

// Create a prediction

D3d10_query_desc qdesc;
Qdesc. miscflags = d3d10_query_misc_predicatehint;
Qdesc. query = d3d10_query_occlusion_predicate;
For (INT I = 0; I <num_microscope_instances; I ++)
{
V_return (pd3ddevice-> createpredicate (& qdesc, & g_ppredicate [I]);
}

Maintain the order of rendering before and after the box and telescope are rendered.

// Render the surround box of the telescope

For (INT I = 0; I <num_microscope_instances; I ++)
{
D3dxmatrix mmatrot;
D3dxmatrix mwvp;
D3dxmatrixrotationy (& mmatrot, I * (d3dx_pi/3.0f ));
Mwvp = mmatrot * mworldviewproj;
G_pmworldviewproj-> setmatrix (float *) & mwvp );

// Render the box mesh. Note that a prediction variable is in the middle.

// When g_prenderoccluder is used for rendering, the box is not displayed on the screen, but the rendering result is lost through alpha blending, but the depth information is saved in memory, so that the following uses its z pass information for prediction when rendering the telescope.

If (g_busepredication)
{
G_ppredicate [I]-> begin ();
G_occludermesh.render (pd3ddevice, g_prenderoccluder );
G_ppredicate [I]-> end ();
}
}

// Render the surround box before rendering the telescope. Pay attention to the Code for enabling and disabling the prediction function.

For (INT I = 0; I <num_microscope_instances; I ++)
{
D3dxmatrix mmatrot;
D3dxmatrix mwvp;
D3dxmatrixrotationy (& mmatrot, I * (d3dx_pi/3.0f ));
Mwvp = mmatrot * mworldviewproj;
G_pmworldviewproj-> setmatrix (float *) & mwvp );

If (g_busepredication)
{
Pd3ddevice-> setpredication (g_ppredicate [I], false );
}

G_heavymesh.render (pd3ddevice, g_prendertextured, g_pdiffusetex );

If (g_busepredication)
{
Pd3ddevice-> setpredication (null, false );
}

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.