"Unity Ngui game Development Four" Ngui number of Drawcall

Source: Internet
Author: User

Read a lot about Ngui Drawcall article, see more of one point is: An Atlas corresponds to a drawcall.

But in fact, Ngui internal has its own set of rules for drawcall processing. The relevant rules are:

1.Atlas Atlas number about

The call order (drawing order) of the 2.Atlas Atlas is related to

3. Related to the number of UIPanel


One, reduce the number of Drawcall Ngui 3

Upgrade to NGUI3, Drawcall number from 5 to 88, think should not be ngui problem it. After finishing a bit, I found two points:
1) The number of the same atlas,drawcall depends on the number of panel (actually the number of UIPanel this script). For example, I have two sprites, these two sprites belong to the same atlas, but under different panel, at this time the number of Drawcall is 2, and NGUI 2 is 1. The recommended use is to use only one panel.
2) for different Atlas, the sprite under the same panel can be depth to adjust the display level, the z value is not used, which is exactly the opposite of Ngui 2. There is also the depth value of the sprite of different Atlas try not to be interspersed back and forth. For example, in Atlas a There are two sprite A and aa,depth two sprite B and BB in 1,3;atlas b respectively, depth 2, 4, Drawcall total is 4 instead of 2. (In Ngui 3, you can click the Panel and see the details of each Drawcall call in the Inspector panel)

The simple thing is,the number of Drawcall is not only related to the number of Atlas, but also related to the sequence of Atlas calls, preferably using only a panel, different Atlas sprite depth try not to interspersed

Reference article: http://game.ceeger.com/forum/read.php?tid=14653

Ii. NGUI Reduction of Drawcall

Pre-description One:

The Drawcall definition in unity:

Each time the engine prepares the data and notifies the GPU that the process is called a draw call.

The process of creating a frame with Unity (or basically all graphics engines) can be roughly simplified: The engine first passes a simple visibility test, determines what the camera can see, and then puts the vertices of those objects (including local locations, Normals, Uvs, and so on) (how vertices form triangles), Transform (that is, the position of the object, rotation, scaling, and camera position, etc.), the relevant light source, texture, rendering method (determined by the material/shader) and other data prepared, and then notify the graphics api--or simply as a notification gpu--start drawing, the GPU based on these data, after a series of operations, Draw thousands of triangles on the screen, eventually forming an image.

Front Note two:

The display order of Uiwidget in Ngui:

The order in which each uiwidget is displayed is determined by the depth value, which is not the same as the z-axis, and the depth value is a two-part, one is weighted by the uipanel of depth and uiwidget of depth, which is uiwidget.

And, the weight of the uipanel is very large, it can be considered that all uiwidget of the uipanel depth is larger than uipanel depth, all uiwidget is larger than the last calculated depth. As an example:

UIPanel1 depth x UIPanel2 depth y

UIWidget1 depth m UIWidget2 depth n

As long as x > y, then no matter the size of M and N, UIWidget1 the final depth must be greater than UIWidget2.

Rules to reduce Drawcall:

1, the same uipanel under the texture and font as far as possible under the same altals. Also expressed another meaning, use the same altals elements as far as possible under the same uipanel.

2, if a uipanel under the use of multiple altals, then try to make use of the same altals elements of continuous, as far as possible to avoid altals crossover.

The first half of rule 1 is well understood. In the second half, you can tell the problem by referring to the previous display order. If you use elements of the same altals under two different uipanel, this will inevitably lead to their drawcall separation. So even if the depth is aligned, it cannot be combined into a single drawcall.

The meaning of rule 2, as an example, is clear:

There are 4 uiwidget,w1,w2,w3,w4 under the same uipanel.

where W1 and W2 refer to ALTALS1.

where W3 and W4 refer to ALTALS2.

If they have a depth order of w1:1,w2:2,w3:3,w4:4.

Then the entire rendering requires 2 drawcall, because the rendering order is w1,w2,w3,w4.

While W1 and W2 have a common altals, they can be combined into a single drawcall, and W3 and W4 can be combined into a single drawcall.

And if their depth order is: W1:1,w2:3,w3:2,w4:4.

Then the entire rendering requires 4 Drawcall, because the rendering order is w1,w3,w2,w4.

Because W1 and W3 are not a common altals, they can only be rendered separately. Similarly W3 and w2,w2 and W4 can only be rendered separately.

Reference article: http://blog.csdn.net/monzart7an/article/details/25212561

Third, the source code Analysis Ngui Drawcall merger principle

Ngui to reduce the consumption of GPU State switching (such as switching material), combine the same material widgets to reduce the number of drawcall. The following describes how Ngui classifies widgets and reduces the drawcall needs to be noted.

The code for the collation widget is in Fillalldrawcalls () in UIPanel, and the code is as follows:

void Fillalldrawcalls () {for (int i = 0; i < drawcalls.size; ++i) Uidraw                Call.destroy (Drawcalls.buffer[i]);                Drawcalls.clear ();                Material mat = null;                Texture tex = null;                Shader SDR = null;                Uidrawcall DC = null;                if (msortwidgets) sortwidgets ();                        for (int i = 0; i < widgets.size; ++i) {Uiwidget w = widgets.buffer[i];  if (w.isvisible && w.hasvertices) {Material                                Mt = w.material;                                Texture tx = W.maintexture;                                Shader sd = W.shader; if (Mat! = MT | | Tex! = TX | | SDR! = SD) {if (Mvert                         S.size! = 0) {                       Submitdrawcall (DC);                                        DC = null;                                        } mat = MT;                                        Tex = TX;                                SDR = SD;                                        } if (mat! = NULL | | SDR! = NULL | | Tex! = NULL) {                                                if (DC = = null) {                                                DC = Uidrawcall.create (this, Mat, Tex, SDR);                                                Dc.depthstart = w.depth;                                                Dc.depthend = Dc.depthstart;                                        Dc.panel = this;                                          } else {      int rd = w.depth;                                                if (Rd < Dc.depthstart) Dc.depthstart = rd;                                        if (rd > Dc.depthend) dc.depthend = rd;                                        } w.drawcall = DC;                                        if (generatenormals) w.writetobuffers (Mverts, MUVs, Mcols, Mnorms, Mtans);                                else W.writetobuffers (Mverts, MUVs, mcols, NULL, NULL);                }} else W.drawcall = null;        } if (Mverts.size! = 0) Submitdrawcall (DC); }

The algorithm is described below

First, the UIPanel in the widget by depth from small to large, if depth the same, according to the material ID to sort. It then iterates through each element and classifies the same widget into the same drawcall. Material. The results after merging are as follows

Finally, 3 Drawcall are generated and the GPU drawing is submitted sequentially.

Why should we use this algorithm? Since Ngui's material is a transparent material and will not be written to the depth cache (but will be tested in depth to ensure that the level of non-transparent objects is correct), we can see the colored of unlit/transparent shader used by the Ngui material, There is a sentence zwrite Off. So the back-and-forth relationship of the widget is not related to the z-coordinate, but to the drawing order of the Drawcall. So if you want to follow the depth to show the widget, it must be divided into 3 drawcall, and in order to draw.


Reference article: http://bbs.9ria.com/thread-282804-1-1.html


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Unity Ngui game Development Four" Ngui number of Drawcall

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.