unity 合批失败

转载 : https://zhuanlan.zhihu.com/p/109200416

3 framedebuger观测结果时主要注意Shadows.RenderShadowMap中的Shadows.RenderJobDir和RenderForward.RenderLoopJob。可以看出来合批主要是在这两个函数中实现作用,分别是描画阴影和描画实体。有可能有些设置在一个函数中实现合批了但是在另一个函数中合批失败。

Different Combined Meshes— the object belongs to another combined static mesh

编辑器中,静态批处理在GO上面勾选static后会在运行时构建一个CombineMesh,打安装包是会在打包时构建CombineMesh。但是在游戏运行中没有勾选Static的GO也可以生成CombineMesh

StaticBatchingUtility.Combine(gameObject);

gameObject的子物体的mesh就会生成一个独立的CombineMesh。两个不同的CombineMesh就不可以合批

Different Custom Properties— the object has a different MaterialProperyBlock set

当想要改变很多拥有共同材质的物体时可以直接更改材质的属性比如

material.SetColor(name,value)

但是这样会在内存中实例化很多InstanceMat(Instance)。

使用MaterialProperyBlock则不会在内存中生成

MaterialPropertyBlock mpb = new MaterialPropertyBlock();
for (int i = 0; i < MeshRenderers.Length; i++)
{
	mpb.SetColor("_Color", Colors[i]);
	MeshRenderers[i].SetPropertyBlock(mpb);
}

但是这样在提交时不会合批。注意这里的Material不是Enable GPU Instancing的

Different Lights— the object is affected by a different forward light

一个场景中有多个光源,每个光源照射着不同层级的物体。灯光把这些物体分为了不同种类。则有多少种类的物体则有多少DC。注意这里不是一个物体受到多个光源照射,如果一个物体受到多个光源照射会有Multiple Forward Lights的warning,下面会提到。

Different Materials— the object has a different material

不同材质

Different Shadow Caster Hash— the objects either have different shadow caster shaders, or have different shader properties / keywords that affect the output of the shadow caster pass

使用了不同的阴影投射着色器;或者相同着色器的参数不同,这些参数的会影响阴影投射Pass的输出

Different Shadow Receiving Settings— the objects either have different "Receive Shadows" settings, or some objects are within the shadow distance, while some other objects are not

接收阴影设置不同

Different Static Batching Flags— the object has different static batching settings

没有勾选Static。这里我遇到个情况就是新建项目创建两个cube一个勾选static一个不勾选,这样是走的动态合批。创建三个cube,两个勾选static一个不勾选提示的是这个原因different static batching setting。

Dynamic Batching Disabled to Avoid Z-Fighting— dynamic batching is turned off in Player Settings or disabled temporarily in the current context to avoid z-fighting

暂时在当前环境中禁用,可以调整Camera的RenderingPath。具体我也没有遇到过。。

Instancing Different Geometries— rendering different meshes or sub-meshes with GPU instancing

使用了开启Enable GPU Instancing的材质,又使用了不同的网格。在对象的材质开启了Instancing,合批的时候会判断合批列表中的第一个节点的submesh的index是否等于该对象的submesh的index,如果相等才可以进行instancing处理。

10 Lightmapped Objects— the object uses a different light map or has different light map uv transformations within the same light map

使用了不同index的lightmap,或者lightmap的uv不同。这里我的(为数不多的)经验就是如果两个物体同时参与了lightmap的烘培,那就不可以合批,我试验的是将一个物体拷贝,这时这两个物体的Transform设置是相同的然后参与烘焙,最后这两个物体在lightmap中的offset依然是不同的,也没有实现合批。

11Mixed Sided Mode Shadow Casters— objects have different "Cast Shadows" settings

castshadow设置一个选择TwoSided另外一个不同。两个物体一个投射阴影时会投射两面,另一个不会这样不会合批。

12 Multipass— the object is using a multi-pass shader

使用了多Pass的shader。一个shader有多个pass就会被描画多次,多个物体每个物体有多个pass就会成倍上涨批次。因为不同pass处理时必须保证所有计算全部在cpu上或者gpu上来保证数据的精度。

13 Multiple Forward Lights— the object is affected by multiple forward lights

如果一个物体被多个光源照射,有几个光源就会有几个DC。在FrameDebuger中会提示不能与前一个Node合批的原因是这个物体受到多个前向光影响。

14 Non-instanceable Property Set

在Enable GPU Instancing的材质中设置 not instanceable的属性

MaterialPropertyBlock mpb = new MaterialPropertyBlock();
for (int i = 0; i < meshRenderers.Length; ++i)
{
	mpb.SetColor("_Color", colors[i]);		// instanceable
	mpb.SetFloat("_Metallic", 1.0f);		// not instanceable
	meshRenderers[i].SetPropertyBlock(mpb);
}

15 Odd Negative Scaling— the object has odd negative scaling (e.g. (1, -1, 1))

Transform的Scale设置中有负数

16 Shader Disables Batching— the shader explicitly disables batching with the "DisableBatching" tag

shader有标签

"DisableBatching"="True"

例如有些顶点动画的shader不希望被合批。

17 Too Many Indices in Dynamic Batch— there are too many indices (more than 32k) in a dynamic batch ;Too Many Indices in Static Batch— there are too many indices in the combined mesh of a static batch. The limit is 48k indices on OpenGL ES, 32k on OSX and 64k on other platforms

这里是索引Index超过32K在动态合批和静态合批中的warning。这个在测试中我没有遇到,测试静态合批超过index时,为了超过索引我在scene中生成特别多的Capsule,在framedebuger中观察到的warning是Objects belong to different static batches。会不会静态合批的处理改变了。。在编辑器运行时会生成CombineMesh,这时如果超过索引Index,就会生成多个CombineMesh。所以这里报错是属于不同的batches。。动态合批测试中的warning一致。

18 Too Many Vertices for Dynamic Batching— a submesh we are trying to dynamically batch has more than 300 vertices

动态合批的物体超过了300个顶点,300个什么概念呢,右键创建的Sphere小球是515个顶点,Capsule胶囊是550个顶点。这里物体的材质是不具备GPU Instancing功能的,如果具备则会提示开启GPU Instancing功能。这里强调一下别的文档可能写错的地方,不管你顶点着色器中用了多少顶点属性,都不会动态合批一个超过300顶点的对象。

19 Too Many Vertex Attributes for Dynamic Batching— a submesh we are trying to dynamically batch has more than 900 vertex attributes

在对象shader的顶点着色器中调用了很多顶点属性,总数量超过900了,该对象就不会进行动态合批。这里的900是指顶点属性的总和并不是指顶点数量。

20 文档中为标明的合批失败条件

对象数量超过4000,原因不想在一个批次中处理很多的job,这样会影响调用性能。

动态合批中每次最多包含88个renderer,引擎分配给每个线程的tis内存可能不允许超过88个renderer,可能会导致内存溢出

因此不建议创建过多的顶点数量少的renderer,这样也会打断合批

猜你喜欢

转载自blog.csdn.net/LM514104/article/details/109247288
今日推荐