IOS Metal Shader 编译

Windows Metal Shader Compiler for IOS

在 4.26 及更高的引擎版本中,支持在 Windows 上直接安装 Metal Sahder Compiler 来支持在 Windows 上编译 Metal 的 Shader,只需要在 Apple 开发者网站 上安装 Metal Developer Tools for Windows 工具安装即可。

4.26 引擎执行 Cook 时的 Log,可以看到创建了 Metallib:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Running: C:\Program Files\Epic Games\UE_4.26\Engine\Binaries\Win64\UE4Editor-Cmd.exe "C:\Users\lipengzha\Documents\Unreal Projects\StarterContent426\StarterContent426.uproject" -run=Cook  -TargetPlatform=IOS -fileopenlog -ddc=InstalledDerivedDataBackendGraph -unversioned -abslog="C:\Program Files\Epic Games\UE_4.26\Engine\Programs
\AutomationTool\Saved\Cook-2021.04.08-10.06.40.txt" -stdout -CrashForUAT -unattended -NoLogTimes -UTF8Output
LogInit: Display: Running engine for game: StarterContent426
LogHAL: Display: Platform has ~ 32 GB [34123063296 / 34359738368 / 32], which maps to Largest [LargestMinGB=32, LargerMinGB=12, DefaultMinGB=8, SmallerMinGB=6, SmallestMinGB=0)
LogTargetPlatformManager: Display: Loaded TargetPlatform 'AllDesktop'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_ASTC'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_DXT'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_ETC2'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'AndroidClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_ASTCClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_DXTClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_ETC2Client'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_Multi'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Android_MultiClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'IOSClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'IOS'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Linux'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'LinuxNoEditor'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'LinuxClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'LinuxServer'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'LinuxAArch64NoEditor'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'LinuxAArch64Client'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'LinuxAArch64Server'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Lumin'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'LuminClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'MacNoEditor'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Mac'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'MacClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'MacServer'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'TVOSClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'TVOS'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'WindowsNoEditor'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'Windows'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'WindowsClient'
LogTargetPlatformManager: Display: Loaded TargetPlatform 'WindowsServer'
LogTargetPlatformManager: Display: Building Assets For IOS
LogAudioDebug: Display: Lib vorbis DLL was dynamically loaded.
LogShaderCompilers: Display: Using Local Shader Compiler.
LogDerivedDataCache: Display: Max Cache Size: 512 MB
LogDerivedDataCache: Display: Loaded Boot cache: C:/Users/lipengzha/AppData/Local/UnrealEngine/4.26/DerivedDataCache/Boot.ddc
LogDerivedDataCache: Display: Pak cache opened for reading ../../../Engine/DerivedDataCache/Compressed.ddp.
LogDerivedDataCache: Display: Performance to C:/Users/lipengzha/AppData/Local/UnrealEngine/Common/DerivedDataCache: Latency=0.06ms. RandomReadSpeed=291.68MBs, RandomWriteSpeed=137.99MBs. Assigned SpeedClass 'Local'
LogMaterial: Display: Missing cached shader map for material Widget3DPassThrough, compiling.
LogMaterial: Display: Missing cached shader map for material Widget3DPassThrough, compiling.
LogMaterial: Display: Missing cached shader map for material Widget3DPassThrough, compiling.
LogMaterial: Display: Missing cached shader map for material Widget3DPassThrough, compiling.
LogMaterial: Display: Missing cached shader map for material Widget3DPassThrough, compiling.
LogMaterial: Display: Missing cached shader map for material Widget3DPassThrough, compiling.
LogMaterial: Display: Missing cached shader map for material DefaultSpriteMaterial, compiling.
LogAudioCaptureCore: Display: No Audio Capture implementations found. Audio input will be silent.
LogAudioCaptureCore: Display: No Audio Capture implementations found. Audio input will be silent.
LogCook: Display: CookSettings for Memory: MemoryMaxUsedVirtual 0MiB, MemoryMaxUsedPhysical 16384MiB, MemoryMinFreeVirtual 0MiB, MemoryMinFreePhysical 1024MiB
LogCook: Display: Mobile HDR setting 1
LogCook: Display: Creating asset registry
LogCook: Display: Discovering localized assets for cultures: en
LogCook: Display: Unable to read previous cook inisettings for platform IOS invalidating cook
LogCook: Display: Clearing all cooked content for platform IOS
LogCook: Display: Sandbox cleanup took 0.025 seconds for platforms IOS
LogMetalShaderCompiler: Display: Creating Native Library C:/Users/lipengzha/Documents/Unreal Projects/StarterContent426/Saved/Cooked/IOS/StarterContent426/Content/Global_SF_METAL.0.metallib
LogMetalShaderCompiler: Display: Archiving 685 shaders for shader platform: SF_METAL
LogZipArchiveWriter: Display: Closing zip file with 0 entries.
LogMetalShaderCompiler: Display: Post-processing archive for shader platform: SF_METAL
LogCook: Display: Cooked packages 0 Packages Remain 314 Total 314

打包出来的 Shadercode 不是 ushaderbytecode 文件,而是和 Mac 上打包一致的metallib

Loaded a text shader (will be slower to load)

当使用远程构建的方式打包了 IOS 包,在运行时会有以下 log:

1
LogMetal: Display: Loaded a text shader (will be slower to load)

该日志在以下代码中输出:

1
2
3
4
5
6
7
Source\Runtime\Apple\MetalRHI\Private\MetalShaders.cpp
/** Initialization constructor. */
template<typename BaseResourceType, int32 ShaderType>
void TMetalBaseShader<BaseResourceType, ShaderType>::Init(TArrayView<const uint8> InShaderCode, FMetalCodeHeader& Header, mtlpp::Library InLibrary)
{
// ...
}

这是因为加载的 Shader 需要实时编译,会比较慢,可以在项目设置中为 IOS 开启remote shader compile,在 UE4.26 之后,也可以通过本地安装 metal 的工具链在本地编译 metal 的 shader。