Talking about Draw Call Optimization Unity Learning Miscellaneous (1)

1. What is a draw call?
The process of collecting and preparing data for each CUP and notifying the GPU is called a Draw Call

Take a look at the manifestation in Unity:

just create a few pictures Sprite and text Text in the scene. After running, you can see Batches in the game stats: 7, which means that the number of drawcalls is seven, but I only have two materials here. Ball: The image and text shader ball, plus the sky and lights, should be four drawcalls in total, so this obviously caused a waste of performance.

The detailed process of drawcall , the information found on the Internet is summarized as: Unity (or basically all graphics engines) generates a frame of picture processing process can roughly be simplified as follows: the engine first goes through a simple visibility test to make sure that the camera can see Objects, and then the vertices of these objects (including local position, normal, UV, etc.), (how the vertices form triangles), transformation (that is, the position of the object, rotation, zoom, and camera position, etc.), related light sources, textures, The rendering method (determined by the material/Shader) and other data are prepared, and then the graphics API is notified-or simply as a notification to the GPU-to start drawing. Based on these data, the GPU performs a series of calculations to draw the result on the screen. Thousands of triangles finally constitute an image.

2. How to optimize The
optimization method is very simple. It can be summarized in one sentence: the rendering levels of different shaders should not intersect.

Insert picture description here
For example: if the Depth of the image is between (0, 10), then the Depth of the text can be between (10, 20). The ranges of the rendering levels do not overlap with each other, which will not cause redundant drawcalls.

The picture below is the number of drawcalls displayed after I sorted the Depth of the picture and text :

now the number of drawcalls has been reduced to four, which is exactly the number inferred earlier.
In UGUI: Depth is the order in which objects are stored in the scene. The higher the priority, the smaller the Depth.
Insert picture description here

3. Drawcall rendering principle
During rendering, the program will search for the shader. The order of finding the shader is based on the object’s Depth, from small to large. If the Depth of ten objects is sorted from 0 to 9, the first one will be found The shader with a Depth of 0 will not be rendered at this time. It will continue to search and find the shader with a Depth of 1. If the two shaders found are the same, then it will continue to look for the third shader until it encounters For different shaders, a drawcall will be performed to render the first shader found, and then repeat this process until all objects are rendered.
AsInsert picture description here
can be seen in the figure above, four objects with the same shader only have one drawcall, and the Depth of the objects with different shaders mentioned above should not overlap, which can effectively reduce the number of drawcalls.

4. The performance bottleneck of drawcall is that when the CPU or GPU
is rendering scene objects, the CPU collects and processes information such as vertices, normal UVs, textures, shaders, etc., and then transmits them to the GPU for rendering for us to see.
In the operation of the game, the CPU bottleneck is crucial. If the CPU is running slowly, the GPU will wait for the CPU. At this time, the game is mainly limited by the CPU.

The above is my rough understanding of drawcall optimization. Welcome everyone to comment!

Guess you like

Origin blog.csdn.net/qq_40385747/article/details/108173793