The DirectX 12 compiler DXC has received a PR about a month ago that adds template support to HLSL.
So you can now do this:
Which is great since that’s all going to happen at compile time and so the compiler will be able to unroll the loop correctly.
This, coupled with UE4’s shader permutation vectors via FPermutationDomain allows you to skip most of the dynamic branching in your code, which is fantastic!
Here’s what you need to do to make UE4 support HLSL templates:
- Install the prerequisites:
- Python 3.x
- Windows SDK: 10.0.19041.0 or newer recommended, installed via Visual Studio
- Windows Driver Kit (WDK) from https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk
- Fork this repo on GitHub: https://github.com/microsoft/DirectXShaderCompiler
- Clone its master branch
- Pull & merge this PR: https://github.com/microsoft/DirectXShaderCompiler/pull/3533
- Go inside the DirectXShaderCompiler directory and run utils\hct\hctshortcut.js, which places a shortcut on your desktop called “HLSL console”
- Run HLSL console from your desktop
- This builds the debug version of DXC and creates a folder called hlsl.bin next to the downloaded repo folder.
- You should see something like “Success – files are available at <SOME_DIR>\hlsl.bin\Debug\bin”
- This is good, but we need the Release version since it’s a lot faster!
- Open the generated LLVM.sln that’s inside hlsl.bin
- Build ALL_BUILD in Release in Visual Studio
- Copy hlsl.bin\Release\bin\dxcompiler.dll to:
- If anything goes wrong you can restore the original DLLs by running setup.bat on source-built engines.
- Edit Engine\Source\Developer\Windows\ShaderFormatD3D\Private\D3DShaderCompiler.cpp:
Find the D3DCreateDXCArguments() function and this:
Goes above the line
That’s it, you should now be able to compile HLSL shaders with templates in them!
As an added bonus, you can also get Microsoft PIX to recognize templates by replacing its c:\Program Files\Microsoft PIX\2103.16\dxcompiler.dll with the one you’ve just built.