--- title: UE的多线程渲染 date: 2022-11-23 16:55:20 excerpt: tags: Rendering rating: ⭐⭐ --- 视频:https://www.bilibili.com/video/BV1sP4y117Dg/?spm_id_from=333.999.0.0&vd_source=d47c0bb42f9c72fd7d74562185cee290 # 并行渲染 因为一些平台的RHI指令需要等待驱动层返回才能继续执行,所以会使用分离RHI线程的方式来解决。 ![800](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221123173341.png) 渲染线程会每帧的帧头处等待RHI线程执行结束。 ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221123173030.png) ## RHI 同时渲染线程会将这些RHI命令都打包在一起,以链表的方式发送到RHI线程进行处理。其中FRHICommandListImmediate(单例,只存在一个)只存放立即执行的RHI命令,比如LockTextre2D子类的。 ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221123174116.png) ## 多线程翻译RHI指令 - IRHIComputeContext:只有一些ComputeShader相关逻辑。 - IRHICommandContext(继承自IRHIComputeContext):其他更多GPU指令。 Command会以链表的方式被构建成CommandBuffer之后加入驱动层的Queue中。 ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221124105422.png) ## 线程同步 - 前端使用了TaskGraph System来同步,主要通过设置Task与Task之间的条件来实现的。 - 后端使用Barrier、Fence、Semaphore等方法。 ## MeshDrawCommand的并行过程 ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221124112824.png) ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221124113024.png) ## RDG并行渲染 ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221124113241.png) ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221124113657.png) ![](https://cdn.jsdelivr.net/gh/blueroseslol/ImageBag@latest/ImageBag/Images/20221124123846.png) ## 并行渲染Debug - ByPass - r.RHICmdBypass - - forcerhibypass - GRHISupportsRHIThread - console variables =>r.metal.IOSRHIThread r.OpenGL.AllowRHIThread r.vulkan.RHIThread - -rhithread -norhithread - GRHISupportsParallelRHIExecute - r.Vulkan.RHIThread >1