HLSL Shader Build Task with Visual Studio Integration

VS

Do you wish Visual Studio would compile your HLSL files automatically for you? With this build task it can! This is especially useful for taking advantage of the new 3D Graphics support in Silverlight 5.

Grab the build task on the MSDN Code Gallery. Both the source code and the installer are included.

Building

To compile the solution you will need to install the DirectX SDK and the Visual Studio SDK. Afterwards you can open the ShaderBuildTask solution in Visual Studio to build.

Installing

The easiest way to install is to build and run ShaderBuildTaskSetup which will copy the build task to the GAC. The MSI installer is included in the download if you don’t want to build it.

Adding to a Project

Once installed, you can update a project to support the task by adding the following to the .csproj file after the Import element for Microsoft.Silverlight.CSharp.target.

Visual Studio

From Visual Studio, you can change the Build Action of HLSL files to either PixelShader or VertexShader. After building the project, shaders will be compiled and included in the final assembly as a resource.

Use Application.GetResourceStream() to access resources, keeping in mind that the relative path of the containing folder is preserved. For example:

Parameters

The build task supports the following parameters.

  • ShaderProfile – The profile to use (vs_2_0, ps_2_0, etc.). This is a required parameter.
  • EntryPoint – The default entry point is “main” but can be overidden.
  • PackMatrixRowOrder – A boolean that indicates whether to pack matrices in row major order (true) or column major (false).
  • OptimizationLevel – This is only supported on new compilers (specify a value 0 to 3).
  • Debug – Include debug information (default is false).
  • ExportConstants – In addition to exporting a compiled shader, also export an XML file with information about the constants in the shader (default is true).
  • IntermediateOutputPath – The path to export compiled shaders and constants to.

Constants Files

By default, constants are exported to an XML file with the same name as the compiled shader with “.constants” appended. This data can be used to map named variables on the application side to constant registers on the shader side.

A sample file snippet is below.

More Information

Original WPF shader build task by Greg Schechter and Gerhard Schneider. Updated by Aaron Oneal to include configurable properties for shader profile, entrypoint, matrix pack order, optimization level, debug, intermediate output path, and shader constants export.

About Aaron

Aaron is the founder of Spicy Pixel and works in the technology industry on nifty projects that he likes to write about. The contents of this website represent personal opinion and not necessarily those of his employers or sponsors.