2025-08-02 12:09:34 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								---
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								title: PSO Precache机制笔记
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								date: 2025-02-08 20:42:16
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								excerpt:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								tags:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								  - Precache
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								  - BundledPSO
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								  - PSO
							 | 
						
					
						
							
								
									
										
										
										
											2025-08-02 12:09:34 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								rating: ⭐
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								---
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								# 前言
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- 官方文档
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- [Creating a Bundled PSO Cache](https://dev.epicgames.com/documentation/zh-cn/unreal-engine/manually-creating-bundled-pso-caches-in-unreal-engine)
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 21:57:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- 视频
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 22:54:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- [Unreal Engine 5: Setup Precompiling Shaders (Bundled PSO's)](https://www.youtube.com/watch?v=NVoXDgXKS5k)
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 13:27:32 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- [Unreal Engine 5 fixing shader stuttering](https://youtu.be/ibIFKEYyBYo?si=OfM_2QSHxtE6oqsf)
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 22:54:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- [Unreal Engine 5: Show Number Of Compiling Shaders](https://www.youtube.com/watch?v=8h1k2ViADgU)
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 21:57:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- 文章
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- 渲染管线相关
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
										- [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
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 21:57:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
										
									
								 | 
							
							
									- 优化UE5的PSO卡顿:FileCache,PreCache和异步PSO https://zhuanlan.zhihu.com/p/1898646962561094034
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- 移动端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)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 22:54:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								# BundledPSO
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 15:40:29 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- 相关启动参数
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 22:54:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- -logPSO
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- -clearPSODriverCache
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- 需要添加的配置(可加子啊`DefaultEngine.ini`或者`(Platform)Engine.ini`中,推荐后者,不同平台需要不同的收集)
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 00:00:08 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- RendererSettings
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
										- r.ShaderPipelineCache.Enable=1
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
										
									
								 | 
							
							
										- r.ShaderPipelineCache.ExcludePrecachePSO=1:用于确保Bundle PSO不会收集PreCache已经收集过的PSO。
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 00:00:08 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- DevOptions.Shaders
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
										- NeedsShaderStableKeys=true
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 00:00:08 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
									- /Script/UnrealEd.ProjectPackagingSettings
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
										- bShareMaterialShaderCode=True
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
										- bSharedMaterialNativeLibraries=True
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-23 22:54:41 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 13:27:32 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								PS.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								1. 可以使用UnitGraph查看CPU来判断是否有PSO正在编译。
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								2. 理论上需要对所有伸缩性进行跑测来收集PSO。
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 16:22:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								## 大致步骤
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
										
									
								 | 
							
							
								1. 可选:使用快捷方式 -clearPSODriverCache,清空PSO。确保`(Project)/Build/(Platform)/PipelineCaches`文件夹是空的或根本不存在
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 16:22:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								2. 添加上述配置。
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 19:12:40 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								3. 执行Launch打包并且启动项目。
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 12:14:24 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
										
									
								 | 
							
							
								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数量。
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-24 16:22:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 12:14:24 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								## 将收集的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"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								```
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-27 15:39:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								# 计算Shader编译进度
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								UE5.6之后提供了UKismetRenderingLibrary::NumPrecompilingPSOsRemaining(),使用计时器来获取进度。
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								# PSO执行链
							 | 
						
					
						
							
								
									
										
										
										
											2025-08-02 12:09:34 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- FGraphEventArray UPrimitiveComponent::PrecachePSOs()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- void UMaterialInterface::InitDefaultMaterials()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								- UMaterial::PrecachePSOs
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								 UMaterial::PrecachePSOs => **MaterialResource->CollectPSOs()** => PrecacheMaterialPSOs() => PrecachePSOs() => CollectPSOs() => CollectPSOInitializers()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								# CollectPSOInitializers()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								接口IPSOCollector::CollectPSOInitializers()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								## 其他MeshProcessor实现
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
								
									
								 | 
							
							
								
							 |