user11164186
user11164186

Reputation:

Premake5: How to build HLSL shaders?

I'm struggling to figure out how to set up my hlsl shaders to work with Premake5 and Visual Studio 2017.

I have no idea how to tell Premake5 to compile my hlsl shaders, as a pre-build step.

Here are my goals for this pre-build step:

  • Specify shader model
  • Specify debug/release compilation
  • Only compile files that have changed
  • Produce dxasm files
  • Place resulting *.asms and *.cso in appropriate release/debug folders

Update 1: Investigating a little further I found that Premake5 has buildaction which makes direct reference to FxCompile.

Update 2: Thanks to mukunda. I was able to configure my project perfectly! I had to build premake5 from source in order to get this premake script to run.

Because as of March, 2019 the binary distributed doesn't support shader model greater than 5.

   hf.deactivate_filter()

   files(src_dir.."shaders/*.hlsl")
   shadermodel("6.3")

   shaderassembler("AssemblyCode")

   local shader_dir = _WORKING_DIR.."/"..src_dir.."shaders/%{cfg.buildcfg}/"

   -- 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")

   hf.deactivate_filter()

   -- Warnings as errors
   shaderoptions({"/WX"})

Update 3: I figured out how to handle command line parameters.

Upvotes: 2

Views: 1923

Answers (1)

mukunda
mukunda

Reputation: 2995

I was just messing around with this today. For my HLSL files I have a simple naming scheme:

  • *-p.hlsl for pixel shader files.
  • *-v.hlsl for vertex shader files.
  • *.hlsl for generic files meant to be included by the shader programs. I just use the hlsl extension so that it shows up with proper HLSL syntax highlighting in the editor.

You don't need custom build rules to compile them. Premake seems to be able to output a proper block in the Visual Studio project for using the shader compiler, and then things like only recompiling files that have changed (with #include dependencies) are handled just fine. Here's what my configuration block looks like:

filter { "files:**.hlsl" }
   flags "ExcludeFromBuild"
   shadermodel "5.0"
filter { "files:**-p.hlsl" }
   removeflags "ExcludeFromBuild"
   shadertype "Pixel"
   shaderentry "ForPixel"
filter { "files:**-v.hlsl" }
   removeflags "ExcludeFromBuild"
   shadertype "Vertex"
   shaderentry "ForVertex"
filter {}

Basically sets up the shadermodel and entry points for my files (For DirectX 12 you probably want shadermodel "6.0"). Then I can add my shaders to the project through files:

files { "shaders/**.hlsl"; }

This is all very new stuff in premake, so you won't find much documentation on it. I saw these options here: https://github.com/premake/premake-core/blob/master/modules/vstudio/_preload.lua.

As for your question about exporting the dxasm files, there are some other options such as shaderassembleroutput and shaderassembler, the latter I believe to be the switch that exports the assembly code to the location specified, but I have not done any testing with these to give an elaborate answer.

Unfortunately I'm not really sure how to enable debugging information in the output files, as there doesn't seem to be a premake option for that yet. I was digging through the documentation to look for any way to add the /Zi (enable debug symbols) flag for shader compilation, but I'm not entirely sure if it's possible.

Upvotes: 2

Related Questions