When profiling, it took about 14 seconds only for shader compile. (Although it took only 7 seconds to load all that obj files.)
how can I optimize this? do I have any option to pre-compile hlsl shaders?
CodePudding user response:
The recommendation is in fact to compile the shaders off-line (at build time) and then load the resulting binary shader blob at runtime.
You can use the built-in Visual Studio HLSL integration which will generate
.cso
files (Compiled Shader Object). See Microsoft Docs for details. For notes on using Shader Model 6 DXC with VS, see this page.You can use a batch script to invoke FXC or DXC from the command-line, and use that as part of a VS custom build or CMake custom targets. See DirectX Tool Kit's CompileShaders.cmd and this tutorial.
You will need a new ctor for your shader classes listed in the code snippet above that take the shader binary blob directly instead of a string to the HLSL source to compile.
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 shaderDXC.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.