Building .fx hlsl files with a Custom Build Rule under Visual Studio 2008
This blog post was originally posted on my previous blog code4kIt's sometimes convenient to be able to compile directly a .fx file under a VS C++ project. NShader provides a nice way to highlight your code, but it's not able (yet, not sure I will update it up to this feature) to compile anything or to display syntax errors.
Hopefully, there is a simple workaround, avoiding to develop your own custom building task : using a custom build rule. Unfortunately, I was able to find one a while ago on Internet, but I lost it, and if I remember, it was lacking some features (like showing all available profiles)... So I decided yesterday to implement my own building rules files, leveraging on the existing .asm rules files that is shipped with Visual Studio, and It was surprisingly easy to write it.
To add this custom building rules to VS, follow these instructions:
- Download the fx.rules custom build rules files.
- Put the fx.rules files under in the following path (according to the installation path of your VS) :
C:\Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults
- Allow the use of the fx custom build rules under your project. For this, go to the solution explorer, right click on your project and select the menu "Custom Build Rules..."
- Then enable the fx rules in the "Visual C++ Custom Build Rules" window :
- Then you need to have the fxc.exe compiler in the Visual C++ path. You could add it to your system path although i prefer to add it inside VS itself. For this, go to the
Tools/Optionsmenu. Then go to the subtree : "Projects and Solutions/VC++ Directories". On the right pane, in "Show Directories for: " select "Executable files", and add below the path to the directory containing the fxc.exe tool.
- The nice thing is that you can customize the compilation per file. Just click on the property of your ".fx" file and you will see all the fxc options :
- And you are done!
- The only drawback is that it's only working with a VC++ project, so it makes it harder to use it for a C# XNA Project, but you can create an almost empty VC++ project, add it to your solution, this c++ project would only contains links to your fx files in your C# Project (assuming that you have VS 2008 C++ and C# integrated together).