Wednesday, August 7, 2013

Using fx.rules in Visual Studio 2010

For my home projects, up till now I have always used Visual Studio 2012 which comes with an inbuilt option for compiling shaders. Recently, I decided to spend more time on it on my workstation. But my workstation had only Visual Studio 2010, and I really wanted the solution to compile my shaders instead of doing it in runtime. After searching for it on the net for sometime, I came across two options:

1. Stick to Visual Studio 2008 and add the custom build rule (http://code4k.blogspot.com/2010/01/building-fx-hlsl-files-under-visual.html)
2. Convert fx.rules to something thats supported in Visual Studio 2010

I stuck with option 2 as I got used to the whole floating window look of Visual Studio 2010. To my luck, Visual Studio 2010 decided to do away with "rules" and use the MSBuild process for customized builds using targets and props file. So basically I had to convert the rules into the corresponding targets and props file. Unfortunately, I couldnt find the converted files on the net, so I decided to do it myself. I have attached the corresponding files below. But if you choose to create it yourself, the process is quite simple actually:

1. Download fx.rules from the above link (or any link). Skip this step if you already have it
2. You might need to add the options for Shader Model 5 in fx.rules. Edit the file and look for

        <EnumProperty
                    Name="Profile"
                    DisplayName="Profile"
                    Description="Target Profile"
                    DefaultValue="19"
                    >
    Under this you'll have to add the enum valuesfor vs_5_0, ps_5_0, gs_5_0, cs_5_0 (and other missing ones). Make sure the "value" is changed in each of the enum values. Here is an example

            <EnumValue
                            Value="8"
                            Switch="/Tvs_5_0"
                            DisplayName="vs_5_0"
                        />
  Skip this step if you already have the options for shader model 5
3. Create a dummy solution in Visual Studio 2008 and add fx.rules as a custom rule. You can keep that file in the same directory as the solution and choose "Find Existing" when you add the custom build rule

4. Save the solution and close it
5. Open up the solution in Visual Studio 2010. Now you'll get the option to convert the old solution format to the new one. This automatically converts the fx.rules into 3 files - fx.targets, fx.props, fx.xml
6. If you build the shaders using this, it will store the output in the same folder as your shader source. If you need to change this, then open up the props file and look for this line :

<CommandLineTemplate>fxc [AllOptions] %(FullPath) /Fo%(RootDir)%(Directory)%(Filename).cfx</CommandLineTemplate>

Make your changes onto the red path. I changed mine to "$(OutDir)Shaders\$(PlatformName)\%(Filename).cso". The $ options are taken from the macros in your solution.

7. Another thing that's missing is the entry point specification.  You'll have to open up fx.xml and hand edit it to add this option.Under
  <Rule
    Name="CompileShader"
    PageTemplate="tool"
    DisplayName="Effect File"
    Order="200">
   It specifies the inputs in properties window, when you right click on a shader file and choose "Effect File" as the compile option. Place these lines anywhere in between the String Properties

    <StringProperty
      Name="EntryPoint"
      DisplayName="Entry Point"
      Visible="True"
      Switch="/E[value]"/>  
This will add the option to enter the entry point for the shader file.
 Now go back to the fx.props file and under the  <CompileShader> tag, add this line:      <EntryPoint>main</EntryPoint>
This will specify the default entry point to be "main".

8. Once you are done with this, place the props, targets and xml file whereever you want and then use the Find Existing option under Build Customizations. The IDE will ask if you want to add that folder into the search path in future.
9.  Right click on any shader file and you should see this option :

 


10. Remember, if you make ANY changes to any of these files, you will have to close the solution and open it up again !!


If you want to skip all the steps, and download these files, Here it is :

You will have to edit the props file if you want to store the compiled shader effects into another location (step 6)