Update your bookmarks! This blog is now hosted on http://xoofx.com/blog

Friday, January 8, 2010

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

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! ;)


    1. Excellent work both on this and the NShader Highlighter!

      Any chance of converting this to work with VS2010? I'm gonna take a stab at converting the standard LC build customization to a FX build customization but I have no experience with msbuild and may fail miserably.

    2. Wow... I haven't checked it under VS2010... so i didn't know that VS2008 custom build rules style are no longer supported... Sadly, I don't have time right now to do the conversion...
      Although, we might follow this conversion process described here : Quick Help on VS2010 Custom Build Rule


    Comments are disabled

    Note: Only a member of this blog may post a comment.