Files
BlueRoseNote/03-UnrealEngine/Rendering/RenderingPipeline/PSO Precache机制以及BundledPSO笔记.md

87 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: PSO Precache机制笔记
date: 2025-02-08 20:42:16
excerpt:
tags:
- Precache
- BundledPSO
- PSO
rating: ⭐
---
# 前言
- 官方文档
- [Creating a Bundled PSO Cache](https://dev.epicgames.com/documentation/zh-cn/unreal-engine/manually-creating-bundled-pso-caches-in-unreal-engine)
- 视频
- [Unreal Engine 5: Setup Precompiling Shaders (Bundled PSO's)](https://www.youtube.com/watch?v=NVoXDgXKS5k)
- [Unreal Engine 5 fixing shader stuttering](https://youtu.be/ibIFKEYyBYo?si=OfM_2QSHxtE6oqsf)
- [Unreal Engine 5: Show Number Of Compiling Shaders](https://www.youtube.com/watch?v=8h1k2ViADgU)
- 文章
- 渲染管线相关
- [UE5.3] PSO Cache&PreCache 源码阅读:https://zhuanlan.zhihu.com/p/679832250
- [UE5.3] PSO Cache&PreCache 源码阅读(二):https://zhuanlan.zhihu.com/p/681803986
- Unreal Engine 5.2 MeshPass拓展:https://zhuanlan.zhihu.com/p/671423486
- 优化UE5的PSO卡顿FileCachePreCache和异步PSO https://zhuanlan.zhihu.com/p/1898646962561094034
- 移动端BundledPSO
- [***UE PSO Cache机制、使用与优化***](https://zhuanlan.zhihu.com/p/681319390)
- [***Unity6 + UE5.4 PSO缓存实践记录***](https://zhuanlan.zhihu.com/p/714231961)
- 这些数据终究还是表明Precache系统做不到100%的PSO覆盖率实际我们还是要结合Bundle PSO来使用在接下来的古代山谷项目里我们会尝试使用二者结合的工作流
- [UE4.27 PSO Caching](https://zhuanlan.zhihu.com/p/700765767)
# BundledPSO
- 相关启动参数
- -logPSO
- -clearPSODriverCache
- 需要添加的配置(可加子啊`DefaultEngine.ini`或者`(Platform)Engine.ini`中,推荐后者,不同平台需要不同的收集)
- RendererSettings
- r.ShaderPipelineCache.Enable=1
- r.ShaderPipelineCache.ExcludePrecachePSO=1用于确保Bundle PSO不会收集PreCache已经收集过的PSO。
- DevOptions.Shaders
- NeedsShaderStableKeys=true
- /Script/UnrealEd.ProjectPackagingSettings
- bShareMaterialShaderCode=True
- bSharedMaterialNativeLibraries=True
PS.
1. 可以使用UnitGraph查看CPU来判断是否有PSO正在编译。
2. 理论上需要对所有伸缩性进行跑测来收集PSO。
## 大致步骤
1. 可选:使用快捷方式 -clearPSODriverCache清空PSO。确保`(Project)/Build/(Platform)/PipelineCaches`文件夹是空的或根本不存在
2. 添加上述配置。
3. 执行Launch打包并且启动项目。
4. 收集打包PSO完成后可以在Saved/Metadata/PipelineCaches/找到打包的PSO文件将其复制出来。
5. 收集RuntimePSO运行打包后的项目跑测后在Saved/CollectedPSOs可以找到Runtime PSO文件将其复制出来。
6. 将所有收集的PSO放到一个制定的文件夹里比如项目目录下PSOCollection进行CommandLet`ShaderPipelineCacheTools`之后会生成制定名称的spc文件。
7. 回到打包后的项目在指定平台的目录文件夹在项目的Build文件夹下的Windows、Android中新建文件夹PipelineCaches并将spc文件放入。
8. 重新使用Launch打包完成后可以在log中知道spc文件中 pso数量。
## 将收集的PSO打包进Pak中
```bash
Engine\Binaries\Win64\UnrealEditor-Cmd.exe -run=ShaderPipelineCacheTools expand #Run ShaderPipelineCacheTools with expand optionl
Project\PSOCollection\*.rec.upipelinecache #Use all .rec.upipelinecache files
Project\PSOCollection\*.shk #Use all .shk files
Project\PSOCollection\PSO_SKGZombies_PCD3D_SM6.spc # Output PSO to be bundled
cmd /k #保证CMD开启
```
```bash
"D:\UnrealEngine\UE_5.6\Engine\Binaries\Win64\UnrealEditor-Cmd.exe" -run=ShaderPipelineCacheTools expand "D:\Project\Demo\PSOCollection\*.rec.upipelinecache" "D:\Project\Demo\PSOCollection\*.shk" "D:\Project\Demo\PSOCollection\PSO_Demo_PCD3D_SM6.spc"
```
# 计算Shader编译进度
UE5.6之后提供了UKismetRenderingLibrary::NumPrecompilingPSOsRemaining(),使用计时器来获取进度。
# PSO执行链
- FGraphEventArray UPrimitiveComponent::PrecachePSOs()
- void UMaterialInterface::InitDefaultMaterials()
- UMaterial::PrecachePSOs
UMaterial::PrecachePSOs => **MaterialResource->CollectPSOs()** => PrecacheMaterialPSOs() => PrecachePSOs() => CollectPSOs() => CollectPSOInitializers()
# CollectPSOInitializers()
接口IPSOCollector::CollectPSOInitializers()
## 其他MeshProcessor实现