Draw Call for Unity performance optimization

Unity performance optimization of Draw Call
Unity (or basically all graphics engines) The process of generating a frame of picture can be roughly described in this way: the engine first passes a simple visibility test to determine the objects that the camera can see, and then converts the objects of these objects. Vertices (including local position, normal, UV, etc.), index (how vertices form a triangle), transformation (that is, the position, rotation, scaling, camera position, etc. of the object), related light sources, textures, rendering methods (by material/Shader decision) when the data is ready, then notify the graphics API—or simply, the GPU—to start drawing, and the GPU, based on this data, goes through a series of operations to draw thousands of triangles on the screen, and finally form an image.

In Unity, each time the engine prepares data and notifies the GPU is called a Draw Call. This process is carried out object by object. For each object, not only the rendering of the GPU, but also the resetting of the material/Shader by the engine is also a very time-consuming operation. Therefore, the number of Draw Calls per frame is a very important performance indicator. For iOS, it should be controlled within 20 times as much as possible. This value can be seen in the Statistic window of the editor.

Unity has built-in Draw Call Batching technology. As can be seen from the name, its main goal is to batch process multiple objects in one Draw Call. As long as the object's transform and material are the same, the GPU can handle it in exactly the same way, i.e. you can put them in a single draw call. The core of the Draw Call Batching technology is to check the materials of all objects to be drawn after the visibility test, group the same materials into a group (a Batch), and then combine them into one object (unified transformation), so that you can Multiple objects are processed in one Draw Call (actually a combined object).

However, Draw Call Batching has a flaw, that is, it needs to combine all the objects in a Batch, which is equivalent to creating an object as large as these objects combined, and at the same time, it needs to allocate memory of the corresponding size. This not only consumes more memory, but also consumes CPU time. Especially with moving objects, each frame has to be recombined, which requires some trade-offs, or the gains outweigh the losses. But for stationary objects, it only needs to be combined once, and then it can be used all the time, which is much more efficient.

Unity provides two methods, Dynamic Batching and Static Batching. Dynamic Batching is completely automatic, and no intervention is required or possible. For movable objects with less than 300 vertices, as long as the same material is used, a Batch will be formed. Static Batching needs to mark static objects as Static, and then no matter the size, they will form a Batch. As mentioned above, Static Batching is obviously much more efficient than Dynamic Batching, so the Static Batching function is charged...

To effectively use Draw Call Batching, the first thing is to minimize the number of materials used in the scene, that is, try to share materials as much as possible. Materials with only different textures can combine textures into a larger texture (called Texture Atlasing). Then mark the non-moving object as Static. In addition, it is also possible to manually combine objects through the CombineChildren script (Standard Assets/Scripts/Unity Scripts/CombineChildren), but this script will affect the visibility test, because the combined objects are always treated as one object, which increases the The amount of geometry to be processed by the GPU, so use with care.

For complex static scenes, you can also consider designing your own occlusion culling algorithm to reduce the number of visible objects and draw calls.

In short, understanding the principles of Draw Call and Draw Call Batching, and designing corresponding solutions according to the characteristics of the scene to minimize the number of Draw Calls is the kingly way, and the same is true for other aspects.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326200869&siteId=291194637