GPU Instancing 功能测试

GPU Instancing 用于减少渲染大量相同物体时的DrawCall,同样减少DrawCall的方式有 Dynamic Batching 和 Static Batching,这两种方法都存在一些限制。
Dynamic Batching :只能处理小于900个顶点属性的物体,必须使用同一个材质球等
Static Batching:内存占用比较大,物体不能移动等

Batch数量对比

以下两张图是1000个立方体生成到场景中后的Batch数量对比

没有使用GPU Instancing

使用了GPU Instancing

可以看到没有使用GPU Instancing时Batch数量是1002,使用GPU Instancing后Batch数量降到4,非常大的差距。

GPU Instancing 的使用

Unity自带的Standard,StandardSpecular 和 Surface Shader都实现了对GPU Instancing的支持,在使用这些Shader时,在对应的材质球上勾选Enable GPU Instancing 即可。
勾选Enable GPU Instancing

如果要让自己写的VF Shader支持GPU Instancing,还需要在Shader代码中做一些修改,需要添加诸如 UNITY_VERTEX_INPUT_INSTANCE_ID, UNITY_INSTANCING_CBUFFER_START(Props),UNITY_INSTANCING_CBUFFER_END 和 UNITY_ACCESS_INSTANCED_PROP 之类的宏,Unity文档 中说的很详细,我测试用的Unity版本是 5.6.3,文档上Shader代码对应的版本是2018.3,所有一些宏的使用不太一样,比如 UNITY_ACCESS_INSTANCED_PROP。

文档里的 UNITY_INSTANCING_BUFFER_START 和 UNITY_INSTANCING_BUFFER_END 宏 我怀疑是写错了,应该是 UNITY_INSTANCING_CBUFFER_START和UNITY_INSTANCING_CBUFFER_END,有知道的大神请不吝赐教啊。

以只含有一个颜色属性的Shader作为测试例子,代码如下:

Shader "MJ/GPU_Instancing"
{
	Properties
	{
		_Color ("Main Color", Color) = (1,1,1,1)
	}

	SubShader
	{
		Tags { "Queue"="Geometry" "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			
			#pragma multi_compile_instancing

			// float4 _Color;

			struct appdata
			{
				float4 vertex : POSITION;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			struct v2f
			{
				float4 pos : SV_POSITION;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			UNITY_INSTANCING_CBUFFER_START(Props)
				UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
			UNITY_INSTANCING_CBUFFER_END

			v2f vert (appdata v)
			{
				v2f o;

				UNITY_SETUP_INSTANCE_ID(v);
				UNITY_TRANSFER_INSTANCE_ID(v, o);

				o.pos = UnityObjectToClipPos(v.vertex);
				
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				UNITY_SETUP_INSTANCE_ID(i);		
				return UNITY_ACCESS_INSTANCED_PROP(_Color);
			}

			ENDCG
		}
	}

	Fallback Off
}

对应的C#代码中需要用到 Graphics.DrawMeshInstanced 方法,这个方法可以让我们只调用一次就渲染最多1023个相同的mesh,并且可以给每一个物体设置相应的矩阵来控制其平移、旋转和缩放,也可以通过一个MaterialPropertyBlock对象给每一个物体设置float、vector 和 matrix 的属性,这样就可以使这1000多个物体的外观各不相同,这么一看确实比Static Batching厉害多了。

C# 代码

for (int i = 0; i < MaxObjectNum; i++)
{
    m_matrix[i] = Matrix4x4.identity;
    Vector3 curPos = Vector3.Lerp(m_srcPosArr[i], m_dstPosArr[i], percent);

    // 设置位移 //
    m_matrix[i].m03 = curPos.x;
    m_matrix[i].m13 = curPos.y;
    m_matrix[i].m23 = curPos.z;

    // 设置颜色 //
    m_colorArr[i] = Color.Lerp(m_srcColorArr[i], m_dstColorArr[i], percent);
}

// 设置颜色 //
m_propBlock.SetVectorArray("_Color", m_colorArr);

Graphics.DrawMeshInstanced(m_mesh, 0, m_material, m_matrix, m_matrix.Length, 
    m_propBlock, ShadowCastingMode.Off, false);
参考链接
https://docs.unity3d.com/Manual/GPUInstancing.html
https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstanced.html
https://www.cnblogs.com/hont/p/7143626.html
https://forum.unity.com/threads/drawmeshinstanced-option-to-provide-per-instance-material-property-block.435716/
https://docs.unity3d.com/Manual/DrawCallBatching.html

猜你喜欢

转载自blog.csdn.net/h5502637/article/details/85065105
GPU