UE4 Dynamic Instance 自动合批分析过程记录
分析流程
新建关卡,为了验证动态合批的代码主要执行的位置,在场景中只添加五个使用相同材质的球,和一个默认的天空球。
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142342956-1060908141.png)
然后开始调试,需要在MeshPass提交的时候进行断点,在void FParallelMeshDrawCommandPass::DispatchPassSetup()
的函数开头设置一个断点。这里第一次断点会断到Depth Pass
,点击F5
进入到Base Pass
处理。
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142340035-187334457.png)
然后逐步进行调试,这里我们需要对上下文环境中的一个变量进行观察,以确定MeshDrawCommand的数量。
void FParallelMeshDrawCommandPass::DispatchPassSetup();
通过调用堆栈我们可以发现:
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142338923-2129618261.png)
上图即可确定MeshDrawCommand
中的数量为6个,在场景中一共5个小球和一个天空球,共6个图元。但是小球应该被合批优化掉,所以最后的MeshDrawCommand
应该为2个。
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142337892-263144791.png)
在下图中,我们看到MeshDrawCommand
的数量发生变化
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142337434-1721020357.png)
球被Instance了
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142336184-778905770.png)
这里我们主要确定的是通过MeshDrawCommand
进行合批优化的实现位置,通过TGraphTask
并行执行 FMeshDrawCommandPassSetupTask::DoTask()->FMeshDrawCommandPassSetupTask::AnyThreadTask()->BuildMeshDrawCommandPrimitiveIdBuffer()
实现对MeshDrawCommand
的合批处理。
FParallelMeshDrawCommandPass::DispatchPassSetup()
中的核心变量TaskContext
其位置在 FSceneRenderer::Views::ParallelMeshDrawCommandPasses
,其中按照顺序来,第二个是Base Pass
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142335634-1654329832.png)
这里我们也可以查看MeshRenderCommand
相关的信息
![](https://img2022.cnblogs.com/blog/1570845/202202/1570845-20220213142334325-829051731.png)
相关参考
Refactoring the Mesh Drawing Pipeline | Unreal Fest Europe 2019 | Unreal Engine - YouTube
UE4 Mesh Piple Line & Auto Instacing | 码农家园 (codenong.com)
Mesh Auto-Instancing on Mobile | Unreal Engine Documentation
4. UE4 的auto instancing 下 - 知乎 (zhihu.com)
3. UE4 的auto instancing 中 - 知乎 (zhihu.com)
2. UE4 的auto instancing 上 - 知乎 (zhihu.com)
Using SV_InstanceID to retrieve the instanceID - UE4 AnswerHub (unrealengine.com)
GPU-Driven Rendering Pipelines (realtimerendering.com)
Unreal 4.22渲染数据管线重构和动态Instancing - 每日头条 (kknews.cc)