Unity URP performance optimization Static Batching, GPU Instancing, SRPBatcher


Static Batching
batches the models of a group of static objects into one model and submits it to the GPU as a whole. When drawing, these objects can be culled normally, and Unity will use the index offset to draw the objects that pass culling.

 

GPU Instancing
Use GPU Instancing to draw (or render) multiple copies of the same mesh at once using a small number of draw calls. It is useful for drawing recurring objects in a scene such as buildings, trees, and grass.

The prerequisite for merging batches is that the same mesh has the same material, but the parameters of the material can be different, and then draw multiple models at once based on an Instanced Draw Call.

The most used ones are related to vegetation, such as grass and trees. In use, it should be noted that when the code calls to change the property, it needs to use MaterialPropertyBlock, so as not to destroy GPU Instancing. For more detailed knowledge, you can read the official documentation.

 

 SRP Batcher
SRP Batcher is a new batching technology of URP, which can speed up the CPU. For the DX platform, the improvement is relatively large, but for the mobile platform, the improvement is not very obvious, about 1.2 times, and this part depends on different models. There are some gaps, those who are interested can take the machine to verify.

The principle is to reduce the set pass call, that is, for different materials based on the same shader variant, put their material properties (uniform) into a block (UBO), and upload all the property blocks of these different materials to the GPU. And upload the properties of the model using these materials (such as matrix, etc.) to the video memory. When drawing, just bind the block to be used from the video memory, and then execute a draw call. In this way, the number of data transfers (set pass calls) from the CPU to the GPU is greatly reduced.

When a drawcall is used by a new material, it needs to prepare for rendering settings. This part of the time-consuming is the main time-consuming point of a drawcall, so if the scene has more materials, the more CPU will have to be used. Set GPU data. The traditional optimization method is to reduce the number of drawcalls to improve CPU rendering performance. In fact, the real CPU consumption comes from those settings, not the GPU drawcall itself. Drawcall is just some byte data sent by Unity to the command buffer of the GPU.

URP's default material supports SRP Batcher

 

The priority of several Bathcing methods in SRP
1. If the object is static (Batching Static), Static Batching will be used. If the material of the object is compatible with SRP Batcher, SRP Batcher will be used at the same time.
2. If the material and Renderer of the object are compatible with GPU Instancing, GPU Instancing will be enabled.
3. If Dynamic Batching is enabled, Dynamic Batch will be used.
It can be seen from the above that if you want to enable GPU Instancing, you must not enable Static Batching, and you must not meet the SRP Batcher conditions.
If the material is compatible with GPU Instancing and the object has Static Batching turned on, Unity will give a prompt in the Inspector of the object:

Conditions for enabling GPU Instacing 
1. First, Shader must be compatible with Instancing.
2. The material enables Enable GPU Instancing
3. The priority of SRP Batcher is higher than that of GPU Instancing. For Game Objects, if SRP Batcher can be used (Shader is compatible with SRP Batcher, the node itself is also compatible, etc.), SRP Batcher will be used, even if It is useless to turn on Enable GPU Instancing for the material.
4. If the condition of SPR Batcher is broken, for example, MaterialPropertyBlock is used, and Enable GPU Instancing is turned on, then GPU Instancing will be enabled.

 

Performance of GPU Instancing
GPU Instancing may not necessarily improve performance for models with a relatively small number of vertices, because when the number of vertices is small, the GPU cannot fully allocate resources to draw multiple instances. The threshold of the number of vertices varies with different graphics cards, but generally It is not suitable for less than 256 vertices.
If there are many objects with few vertices that need to be drawn, they can be merged into a mesh for drawing


Tips:
When using GPU Instancing and SRP Batcher in the URP pipeline, you need to pay attention. Only one of them can exist, and SRP Batcher has the highest priority. For shaders that write grass and trees, it is recommended to directly support GPU Instancing instead of SRP Batcher is more efficient than SRP Batcher on the mobile side. Static batching is generally used for static objects with relatively fine mesh grids. The grids are rendered in batches, but due to the formation of new meshes, memory usage will increase.
————————————————
Original link: https://blog.csdn.net/Star_MengMeng/article/details/126526498

Guess you like

Origin blog.csdn.net/weixin_42565127/article/details/130627814
Recommended