Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of DrawCall for Unity performance Optimization

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Unity performance optimization of DrawCall example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The process of rendering a picture by a graphics engine

The process of generating a frame by Unity (or almost all graphics engines) can be described roughly as follows:

1. Visibility test

1. The engine first goes through a simple visibility test to determine what the camera can see.

two。 Prepare the data of the object

two。 Then prepare the vertices of these objects (including local position, normal, UV, etc.), index (how vertices form triangles), transformations (that is, object position, rotation, scaling, camera position, etc.), related light sources, textures, rendering methods (determined by material / Shader), etc.

3. Notify graphic API to start drawing

3. Then notify the graphic API-- or simply as telling GPU-- to start drawing. Based on these data, GPU draws thousands of triangles on the screen after a series of operations, and finally forms an image.

What is Draw Call?

In Unity, the process of each time the engine prepares data and notifies GPU is called a Draw Call.

This process is done object by object, for each object, not only GPU rendering, the engine reset material / Shader is also a very time-consuming operation.

Therefore, 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.

Draw Call Batching technology

Unity has built-in Draw Call Batching technology, and as you can see from the name, its main goal is to batch process multiple objects in a single Draw Call.

As long as the transformations of objects and materials are the same, GPU can handle them in exactly the same way, that is, you can put them in a Draw Call.

The core of Draw Call Batching

The core of Draw Call Batching technology is to check the materials of all the objects to be drawn after the visibility test, divide the objects of the same material into a group (a Batch), and then combine them into one object (unified transformation), so that you can deal with multiple objects in a Draw Call (actually a combined object).

Defects of Draw Call Batching

But the drawback of Draw Call Batching is that it needs to combine all the objects in a Batch, which is equivalent to creating an object as big as these objects put together.

At the same time, the appropriate amount of memory needs to be allocated. This consumes not only more memory, but also CPU time.

Especially for moving objects, each frame has to be reassembled.

But for a stationary object, it only needs to be combined once, and then it can be used all the time, which is much more efficient.

This requires some trade-offs, otherwise the loss outweighs the gain.

Dynamic Batching and Static Batching

Unity provides both Dynamic Batching and Static Batching.

Dynamic Batching

Dynamic Batching is completely automated and does not require and cannot make any intervention.

For movable objects with less than 300 vertices, Batch will be formed as long as the same material is used.

Static Batching

Static Batching, on the other hand, needs to mark the stationary object as Static, and then it will form a Batch, big or small.

As mentioned earlier, Static Batching is obviously much more efficient than Dynamic Batching, so the Static Batching function is free of charge.

Efficient use of Draw Call Batching

To make effective use of Draw Call Batching, there are the following points to note:

1. The first is to minimize the number of materials used in the scene, that is, to share materials as much as possible, and for materials with only different textures, you can combine textures into a larger texture (called Texture Atlasing).

two。 Then mark the objects that will not move as Static. You can also group objects together manually through a CombineChildren script (Standard Assets/Scripts/Unity Scripts/CombineChildren), but this script affects visibility testing because objects that are grouped together are always treated as one object, which increases the amount of geometry that GPU has to deal with, so be careful.

3. For complex static scenes, we can also consider self-designed occlusion culling algorithm, which can reduce the number of visible objects as well as Draw Call.

In short, to understand the principles of Draw Call and Draw Call Batching, according to the characteristics of the scene to design the corresponding scheme to minimize the number of Draw Call is the king, as well as other aspects.

Reference: http://ravenw.com/blog/2011/10/14/unity-optimization-of-draw-call/#sthash.eWSmEU7K.dpuf

Understanding 2: DrawCall views DrawCall in Unity

When developing games, you must be reminded to reduce Draw Call from time to time. Of course, Unity is no exception. Open the Stats in the Game window and you can see the numbers of Draw Call and Batched.

Draw Call definition

But what exactly is Draw Call? The performance of the impact comes from CPU? Or GPU?

First, let's define what "Draw Call" is:

"one Draw Call, equal to one call to DrawIndexedPrimitive (DX) or glDrawElements (OGL), equal to one Batch"

People who have touched DirectX or OpenGL must be familiar with the API of DrawIndexedPrimitive and glDrawElements.

This function must be called when we have the data (usually the vertices of triangles) to GPU.

Give examples to illustrate

In other words, if there is a "wooden" chair and an "iron" table in the picture, there will theoretically be two Draw Call.

Have you specifically pointed out "wood" and "iron"? This means that the two objects use different shaders or different Shader.

In DirectX or OpenGL, you need to call Draw Call twice if you assign different maps or different Shader descriptions to different objects.

Example code SetShader ("Diffuse"); SetTexture ("iron"); DrawPrimitive (DeskVertexBuffer); SetShader ("VertexLight"); SetTexture ("wood"); DrawPrimitive (ChairVertexBuffer)

Every change to Shader or map is basically a change to the settings of Rendering Pipeline, so a different Draw Call is needed to complete the drawing of the object.

Now you can see why you are always asked to use the same shader as much as possible in the official UNITY documents to reduce the number of Draw Call.

Batch (another name for Draw Call)

When it comes to Batch, it is actually another name for Draw Call.

You can understand that each Draw Call will generate a Batch, while the Batch contains the object vertex data.

Batch the vertex data is sent to GPU,GPU by CPU through the "driver" to take over, and then the object is drawn on the screen.

So the more Draw Call,CPU you have, the busier you are. It is now more clear that the number of Draw Call affects CPU performance rather than GPU.

NVIDIA proposed in GDC that 25K batchs/sec will be full of 1GHz's CPU,100% utilization. So they came up with a formula to estimate how many Batch you can Run in the game:

For example: if your goal is to run the game 30FPS, use 2GHz's CPU, and send 20% of the workload to Draw Call, how much Draw Call can you have per second?

333 Batchs/Frame = 25K * 2 * (0.2max 30)

Well, since Batch is a box containing the top data of the object, and according to our description above,

So the objects of the same material and Shader can be combined into a Batch and sent to GPU, which is the most convenient way? Yes! That's the way it is!

Two functional options of UNITY in Player Setting: Static Batching and Dynamic Batching. The functional description is as follows:

Static Batching

Static Batching is a static object that will be marked as Static. If you use the same shader, UNITY will automatically combine the two objects into a Batch and send them to GPU for processing.

This feature is very helpful in terms of performance, so you have to pay for it.

Dynamic Batching

Dynamic Batching is under the condition that the object is less than 300 faces (regardless of whether the object is non-static or dynamic). Using the same shader, UNITY will automatically combine you into a Batch and send it to GPU for processing.

Thank you for reading this article carefully. I hope the article "sample Analysis of DrawCall for Unity performance Optimization" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report