I guess a lot of you that use premake often already know this, but since I find this to be handy I want to share it. A couple of days ago I needed to get shaders up and running in a very simple way. I started out by using the obligatory methods from the DirectX Api:
D3DCompileFromFile or the alternative D3DCompileToBlob. I still employ these methods however instead of compiling the file at runtime I decided to load pre-compiled shaders for now.
Since I don’t want to compile shaders manually each time I compile the project I looked for a solution that can be used with premake5. Everything I write I’ve found at https://stackoverflow.com/questions/55055150/premake5-how-to-build-hlsl-shaders. For an in depth look navigate to the link.
How does it work?
Navigate to your main premake5.lua script file and open it. Add the following lines of code to your project section.
shadermodel("5.0")
shaderassembler("AssemblyCode")
local shader_dir = "../Assets/Shader/"
-- HLSL files that don't end with 'Extensions' will be ignored as they will be
-- used as includes
filter("files:**.hlsl")
flags("ExcludeFromBuild")
shaderobjectfileoutput(shader_dir.."%{file.basename}"..".cso")
shaderassembleroutput(shader_dir.."%{file.basename}"..".asm")
filter("files:**_ps.hlsl")
removeflags("ExcludeFromBuild")
shadertype("Pixel")
filter("files:**_vs.hlsl")
removeflags("ExcludeFromBuild")
shadertype("Vertex")
-- Warnings as errors
shaderoptions({"/WX"})
The file tells premake which kind of shader is contained in each of the files. I think the keywords in here are very self-explanatory however with each filter you can specify how each shader file will be compiled. Shadertype defines what type of shader the file is containing, the shaderentry contains the name of the entry function of the shader.