Unity Performance Optimization - Draw Call

1. What is Draw Call

Draw Call is a concept in Unity, which refers to the number of calls required by the GPU to render an object. Reducing Draw Calls can improve the frame rate of the game. Draw Calls can be reduced by merging materials, merging meshes, etc.

2. How to optimize Draw Call

The main optimization methods are as follows:

  1. Merge grids: Merge multiple grids into one grid to reduce Draw Call. Meshes can be merged using the Mesh.CombineMeshes method in Unity.
  2. Merge Material: Merge multiple objects using the same material into one object, which can reduce Draw Call. You can use MaterialPropertyBlock in Unity to share materials.
  3. Use static batch processing: Merge multiple static objects into one batch for rendering, which can reduce Draw Call. This can be achieved by turning on static batching in Unity.
  4. Use dynamic batch processing: Combine multiple dynamic objects into one batch for rendering, which can reduce Draw Call. This can be achieved by enabling dynamic batching in Unity.
  5. Use GPU Instancing: Use GPU instancing technology to instantiate multiple identical objects and reduce Draw Call. GPU Instancing can be achieved by creating a MaterialPropertyBlock object and calling the MaterialPropertyBlock.SetVectorArray method.
  6. Use Atlas textures: Combine multiple small textures into one large texture to reduce Draw Call. You can use the SpritePacker tool in Unity to combine textures.
  7. Reduce the number of dynamic objects: Dynamic objects need to be redrawn every frame, so too many will lead to an increase in Draw Call. The number of dynamic objects can be reduced by using static objects, using LOD, etc.
  8. Reduce the number of transparent objects: Transparent objects require additional rendering steps, so too many will lead to an increase in Draw Call. The number of transparent objects can be reduced by using opaque objects, using Alpha Test, etc.
  9. Use Occlusion Culling: Improves rendering performance by reducing the number of UI elements that need to be rendered based on the visible UI elements within the camera frustum.

3. Detailed introduction of different optimization methods

1. Static batch processing:

The principle of static batch processing:

Static batch processing is to combine multiple static objects into one grid for rendering to reduce the number of Draw Calls. In static batch processing, it is first necessary to merge the grid data of multiple static objects into a large grid data, and then pass the grid data to the rendering engine for drawing. By merging multiple static objects into one grid for rendering, you can avoid repeatedly setting rendering states and switching rendering resources, thereby improving rendering efficiency.

Resource requirements for static batch processing:

Static batch processing has relatively high resource requirements. It needs to combine multiple static objects into a grid and use the grid for drawing when rendering. Therefore, when using static batch processing, multiple objects need to be merged, and a new mesh resource is generated after the merge. This may lead to some high resource occupation, so the size and number of merged grids need to be properly controlled to avoid resource waste and performance degradation. 

Static batch processing requirements for the number of faces of the model:

When using static batch processing, multiple static objects will be merged into one grid for rendering. Therefore, the number of faces of the merged grid should be as small as possible to avoid performance impact during rendering. Generally speaking, the number of faces of a static mesh should not exceed several thousand, and it is better to keep it under one thousand. If there are too many faces, the merged grid may be too large and consume too many resources, and it will consume a lot of computing resources during rendering, resulting in performance degradation.

 It should be noted that the actual face limit depends on the support capabilities of the hardware and game engine. When using static batch processing and dynamic batch processing, a reasonable polygon limit should be set according to the support capabilities of the hardware and game engine, as well as the actual needs of the game.

 Pros and cons of static batching:

advantage:

  1. Reduce Draw Call: Combine multiple static objects into one batch for rendering, which can reduce Draw Call and improve game performance.
  2. Support for dynamic lighting: Static batch processing can support dynamic lighting, so it is suitable for scenes that require real-time lighting.
  3. Ease of use: The use of static batch processing is relatively simple, just check the "Static" option on the material.

shortcoming:

  1. Does not support dynamic objects: Static batch processing is only applicable to static objects, and does not support the merging of dynamic objects.
  2. High memory usage: Static batch processing needs to create a new merged mesh object in memory, so it takes up a lot of memory. 

Effects of static batching on occlusion culling:

When using static batch processing, multiple static objects are merged into one batch for rendering, which may cause some occluded objects to be rendered, affecting the effect of occlusion culling. Therefore, when using static batch processing, you need to pay attention to the construction of the scene and the placement of objects, and try to avoid occlusion between objects.

 2. Dynamic batch processing:

The principle of dynamic batch processing:

Dynamic batch processing is to combine multiple dynamic objects into one grid for rendering to reduce the number of Draw Calls. In dynamic batch processing, each frame needs to regenerate a grid data, merge the grid data of multiple dynamic objects into a large grid data, and then pass the grid data to the rendering engine for drawing. By merging multiple dynamic objects into one grid for rendering, you can avoid repeatedly setting rendering states and switching rendering resources, thereby improving rendering efficiency.

Resource requirements for dynamic batch processing: 

Dynamic batch processing has relatively low resource requirements, and does not need to generate new grid resources, but batches drawing when rendering dynamic objects. Therefore, when using dynamic batch processing, there is no need to merge multiple objects, and objects can be dynamically generated and rendered directly at runtime. This reduces resource usage and improves game performance.

Requirements for dynamic batch processing on the number of faces of the model: 

When using dynamic batch processing, multiple dynamic objects will also be merged into one grid for rendering, but unlike static batch processing, dynamic objects can have more faces. Generally speaking, the number of faces of a dynamic grid can be controlled below tens of thousands of faces. If there are too many faces, it may consume a lot of computing resources during rendering, resulting in performance degradation.

It should be noted that the actual face limit depends on the support capabilities of the hardware and game engine. When using static batch processing and dynamic batch processing, a reasonable polygon limit should be set according to the support capabilities of the hardware and game engine, as well as the actual needs of the game.

 Pros and cons of dynamic batching:

advantage:

  1. Support for dynamic objects: Dynamic batch processing can combine multiple dynamic objects into one batch for rendering, so it is suitable for scenes that need to dynamically generate objects.
  2. Low memory footprint: Dynamic batching does not require the creation of new merged mesh objects, so the memory footprint is low.
  3. Support GPU Instancing: Dynamic batch processing supports GPU instancing technology, which can further reduce Draw Call.

shortcoming:

  1. Does not support dynamic lighting: Dynamic batching does not support dynamic lighting, so it does not work well in scenes that require real-time lighting.
  2. Large impact on CPU performance: Dynamic batch processing needs to be calculated and merged on the CPU, so it has a large impact on CPU performance. 

 Impact of dynamic batching on occlusion culling:

When using dynamic batch processing, multiple dynamic objects are merged into one batch for rendering, which can effectively reduce Draw Call, but may also affect the effect of occlusion culling. If the number of dynamic objects is large and dense, some occluded objects may also be rendered, affecting the effect of occlusion culling. Therefore, when using dynamic batch processing, it needs to be selected and applied according to specific scenarios and requirements, and combined with other optimization techniques to improve the effect of occlusion culling.

3. Package the atlas:

The principle of packaging atlas:

 The UI packaged atlas (Sprite Atlas) function in Unity is a technology that combines multiple images into a large texture atlas to improve rendering performance.
The principle is to combine multiple images into a large texture atlas according to specified rules at runtime, and then use this texture atlas to render UI elements. When using a texture atlas, Unity will paste a small part of the texture atlas to the corresponding UI element according to the UV coordinates of the texture.

Advantages and disadvantages:

advantage:

  1.  Reduce Draw Call: Combining multiple pictures into a large texture atlas can reduce the number of times to draw, thereby reducing rendering overhead and improving game performance.
  2. Reduce texture switching: Combining multiple images into a large texture atlas can reduce the overhead when switching textures, thereby improving game performance.
  3. Improve memory utilization: Combining multiple images into a large texture atlas can reduce the number of textures that need to be loaded into memory, thereby improving memory utilization and reducing memory usage overhead.
  4. Simplified resource management: Merging multiple images into one large texture atlas can simplify the workload of resource management, reduce development costs, and improve development efficiency.
  5. Convenience for art production: Combining multiple pictures into one large texture atlas can facilitate the art to process multiple small pictures and reduce the complexity of art work.

shortcoming:

  1.  Atlas size limit: There is a certain limit on the size of the atlas. If there are too many pictures to be packaged or the size is too large, the size of the atlas may exceed the limit, so that it cannot be packaged normally.
  2. Image distortion: Merging multiple images into one large texture atlas may cause images to be compressed or stretched during the merging process, resulting in image distortion.
  3. Memory consumption of the atlas: At runtime, the entire atlas needs to be loaded into the memory, which may occupy a large memory space. It is necessary to reasonably control the size of the atlas and the number of pictures contained in the atlas.
  4. Image duplication: If multiple UI elements use the same image, but with different positions and sizes, this image needs to be repeated multiple times in the atlas, resulting in an increase in the size of the atlas.

The following points should be paid attention to when packaging the atlas:

  1.  Reasonably control the size of the atlas and the number of pictures included to avoid the situation that the atlas cannot be packaged normally due to excessive size or too many pictures.
  2. For pictures that need to maintain the original image ratio, you need to set the corresponding Packing Tag in the atlas to prevent the pictures from being compressed or stretched during the merging process.
  3. Try to avoid using too many repeated images to reduce the size of the atlas.
  4. For images that need to be switched frequently, it is not advisable to package them into an atlas to avoid performance degradation due to the need to re-render the atlas when switching textures.
  5. Correct texture coordinates need to be set on UI elements that use atlases to ensure the correct image is used.

4. You can use dynamic atlas to optimize draw call:

Unity Performance Optimization - Dynamic Atlas 

Guess you like

Origin blog.csdn.net/qq_33808037/article/details/129530241