Building .fx hlsl files with a Custom Build Rule under Visual Studio 2008

This blog post was originally posted on my previous blog code4k
It'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:


  1. Download the fx.rules custom build rules files.
  2. 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
  3. 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..."

  4. Then enable the fx rules in the "Visual C++ Custom Build Rules" window :

  5. 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/Options menu. 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.
  6. 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 :
  7. And you are done!
  8. 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).
I will also publish this month an update of NShader to address some issues. If I had more time for this kind of thing, It would be great to add a real Shader IDE inside VS 2008 (in the way the still-long awaited NVidia's nexus beta.is done). Actually, It's not so hard to do such thing... but it would take a huge time, and I have to seriously code some demo instead of working on satellite projects and... before I'm getting too old for that! ;)
     
     

    Comments