先放官方对于Draw call batch的解释:https://docs.unity3d.com/Manual/DrawCallBatching.html
在Unity 的优化中,我们经常说要优化Draw call。但是我们在Unity 的视图里的 stats窗口里,看到的与draw call 有关的参数大部分情况下只有SetPassCall 和 Batches两个。
那这两个值分别代表着什么呢?它们谁更贴近draw call 呢?
先来看Setpass Call , 我们不用官方那么高大上的翻译,我们通俗来说,它大致代表的是摄像机照射范围内,所有GameObject所包含的Material种类数量。(不完全准确,但是这样说易于理解)。假如有30种material,那这个SetpassCall的数值也会离这个30比较接近。这个值越大,肯定会使draw call 越来越大。但是setpasscall很小的时候,也有可能draw call的值比较大。为什么我们要看下面这个属性: Batches.
Batches,是unity中合批次。具体的概念这里就不赘述,网上有很多资料。这个值是和Draw call 比较接近的。有时候在项目中,我们的setpass call 数值不高,但是batches出奇的高,结果也会导致draw call 过高而影响游戏的CPU性能。那这个值为什么会过高呢? 主要是因为需要渲染的一些游戏物体不满足Unity的Dynamic batch的要求。当需要渲染的Gameobject过多,而且这些gameobject又不能被Unity 内置的Batch功能自动合并时,每多一个这样的Gameobject,就会多一个Batch,进而就会导致Draw call过高,造成卡顿。这种情况是要尽量避免的。(这里不讨论Static Batch)。那么怎么判断哪些gameobject 是可以被unity 进行动态batch的呢?有如下几点(这里图快,只贴英文):
Unity can automatically batch moving GameObjects into the same draw call if they share the same Material and fulfill other criteria. Dynamic batching is done automatically and does not require any additional effort on your side.
- 1.Batching dynamic GameObjects has certain overhead per vertex, so batching is applied only to Meshes containing fewer than 900 vertex attributes in total.
- If your Shader is using Vertex Position, Normal and single UV, then you can batch up to 300 verts, while if your Shader is using Vertex Position, Normal, UV0, UV1 and Tangent, then only 180 verts.
- Note: attribute count limit might be changed in future.
- 2.GameObjects are not batched if they contain mirroring on the transform (for example GameObject A with +1 scale and GameObject B with –1 scale cannot be batched together).
- 3.Using different Material instances causes GameObjects not to batch together, even if they are essentially the same. The exception is shadow caster rendering.
- 4.GameObjects with lightmaps have additional renderer parameters: lightmap index and offset/scale into the lightmap. Generally, dynamic lightmapped GameObjects should point to exactly the same lightmap location to be batched.
- 5.Multi-pass Shaders break batching.
-
- Almost all Unity Shaders support several Lights in forward rendering, effectively doing additional passes for them. The draw calls for “additional per-pixel lights” are not batched.
- The Legacy Deferred (light pre-pass) rendering path has dynamic batching disabled, because it has to draw GameObjects twice.
-
Because it works by transforming all GameObject vertices into world space on the CPU, it is only an advantage if that work is smaller than doing a draw call. The resource requirements of a draw call depends on many factors, primarily the graphics API used. For example, on consoles or modern APIs like Apple Metal, the draw call overhead is generally much lower, and often dynamic batching cannot be an advantage at all.
因此优化Draw call 的时候,我们重点要将Bathes 的数值降低。
结论:Setpass call 的数值低,Draw call 不一定低,但是SetPass call 的数值高的花,Draw call 一定高;
Batches 的数值一定情况下与Draw call的数值比较接近,这个数值要尽量去优化,尽量多利用Unity 的 Dynamic Batch 功能。