dx12)编译Shader耗时过长

dx12) It takes too long to compile Shader

分析时,仅着色器编译就花费了大约 14 秒。 (虽然加载所有 obj 文件只用了 7 秒。)

我该如何优化它?我可以选择预编译 hlsl 着色器吗?

建议实际上是编译着色器 off-line(在构建时),然后在运行时加载生成的二进制着色器 blob。

  • 您可以使用 built-in Visual Studio HLSL 集成,它将生成 .cso 文件(编译着色器对象)。参见 Microsoft Docs for details. For notes on using Shader Model 6 DXC with VS, see this page

  • 您可以使用批处理脚本从 command-line 调用 FXC 或 DXC,并将其用作 VS 自定义构建或 CMake 自定义目标的一部分。参见 DirectX Tool Kit's CompileShaders.cmd and this tutorial

您的着色器需要一个新的 ctor 类,在上面的代码片段中列出,它直接将着色器二进制 blob 而不是字符串带到 HLSL 源进行编译。

For Win32 desktop applications, you need to handle the fact that your .cso files will end up next to your built EXE instead of where you have your project CWD set. See ReadData for an example of this. For UWP and Xbox apps, the .cso files are placed into the appx package automatically.

From your question, it's not clear if you are using the legacy FXC.EXE compiler with Shader Model 5.1 or the current DirectX 12 shader DXC.EXE for Shader Model 6. Still the same advice applies here. FWIW, the DXC compiler is LLVM based so it's a bit more complex and therefore does more analysis than the legacy FXC compiler did.