The Unity performance Optimization Draw call principle < go >

Source: Internet
Author: User
Tags visibility

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 places the vertices of these objects (including local locations, normals, Uvs, etc.), and indexes (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.

In unity, the process of preparing data and notifying the GPU each time the engine is called is a draw call. This process is done on an object-by-item basis, and for each object, not just GPU rendering, the engine/shader material is also a very time-consuming operation. So the number of draw call per frame is a very important performance indicator, for iOS should be controlled within 20 times, this value can be seen in the editor's statistic window.

Unity's built-in draw call batching technology, as the name suggests, is the main goal of batch processing of multiple objects in one draw call. As long as the objects are transformed and the materials are the same, the GPU can be processed in exactly the same way, that is, they can be placed in a draw call. The core of the draw call batching technique is to examine the material of all objects to be drawn after the visibility test, divide the same material into a group (a batch), and then combine them into an object (unified transformation) so that it can be used in a draw Call handles multiple objects (actually a combination of objects).

But there is a flaw in the draw call batching that it needs to combine all the objects in a batch to create an object as large as those objects, while at the same time allocating the corresponding size of memory. This not only consumes more memory, it also consumes CPU time. Especially for moving objects, each frame has to be re-assembled, which requires some trade-offs, otherwise not worth the candle. But for stationary objects, only one combination is needed, and then it can be used all the time, much more efficiently.

Unity offers two ways of dynamic batching and static batching. The Dynamic batching is fully automated and does not require or intervene, and for movable objects within 300 of the vertex count, batch is formed as long as the same material is used. Static batching require static objects to be marked as static, and then batch is formed, regardless of size. As mentioned above, the static batching is obviously much more efficient than the dynamic batching, so the static batching function is chargeable ...

To effectively utilize the draw call batching, the first is to minimize the number of materials used in the scene, that is, to share the material as much as possible, and to combine textures into a larger texture (called texture atlasing) for textures that differ only in texture. Then it marks the object that does not move as static. It is also possible to manually group objects together through the Combinechildren script (Standard assets/scripts/unity scripts/combinechildren), but this script affects the visibility test, Because objects that are grouped together are always treated as an object, which increases the amount of geometry the GPU will handle, so use caution.

For complex static scenarios, it is also possible to consider a self-designed occlusion culling algorithm that reduces the number of visible objects and also reduces draw call.

In short, understanding draw call and draw call batching principle, according to the scene characteristics of the design of the corresponding scheme to minimize the number of draw call is kingly, and so on.

The Unity performance Optimization Draw call principle < go >

Related Article

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.