Hi everyone!
Here is a simple MSBuild task to automate shader compilation, as specifying an pre-build event for each shader file separately can be cumbersome in large projects.
Next is the original "readme" for this component (included as a docx file in attachment):
Shader compilation MSBuild task
Warning: was built and tested only on Windows 8 Release Preview using Visual Studio 2012 RC
Warning: this solution involves manual editing of project file, make sure you have an backup before doing anything! Nothing is guaranteed, use it at your own risk.
General description:
Can be included in project to automate DirectX shader build and deployment tasks.
One shader file can be compiled only once using this method. It is not suitable if several compilations are needed for same source file.
Project configuration:
Edit manually the "*.csproj" file and add at end the following code:
<UsingTask TaskName="MSBuild.Fxc.CompileShader" AssemblyFile="MSBuild.Fxc.dll" />
<Target Name="BeforeBuild">
<CompileShader InputFiles="@(Hlsl)" OutputPath="$(OutputPath)" />
</Target>The file "MSBuild.Fxc.dll" should be placed in same folder as "csproj" file.
Notice the keyword "Hlsl" in InputFiles attribute: this should be the "Build Action" for shader files in project. To do this, select the shader source file in Solution Explorer and open its properties (Alt+Enter or F4), then in "Build Action" property write the needed value (it can be any identifier). Several tasks with different input files can be used in one project. The file declaration in "csproj" file should look like this:
<ItemGroup>
<Hlsl Include="Shaders\Shader.fx" />
</ItemGroup> Task parameters:
• InputFiles - [Required]: Input shader files for compilation.
• OutputPath - [Required]: Project build output path where compiled shaders are copied. Relative paths are preserved (for example if input file is "Shaders\MyShader.ps" then output file will be at "bin\Debug\Shaders\MyShader.pso").
• FxcPath - [Optional]: Path to "fxc.exe" executable. Default value is " C:\Program Files (x86)\Windows Kits\8.0\bin\x86\fxc.exe".
• EntryMethod - [Optional]: Entry method of shader unit. Default value is "main".
• Profile - [Optional]: Shader profile for compilation. Should include only version string, for example "4_1" is correct but "ps_4_1" is incorrect.
Remarks:
Shader type is inferred from file extension, for example "MyShader.ps" will be compiled as pixel shader, but "MyShader.vs" will be compiled as vertex shader. "hlsl" extension is not supported.
If shader profile is embedded in filename - it will be used, otherwise will be used the value provided in parameter "Profile". If no value is provided - an default will be used ("4_0_level_9_3" for pixel and vertex shaders, and the smallest possible for others).
For example, the file "MyShader_4_1.ps" will be compiled with profile "ps_4_1", the file "Compute5_0.cs" will be compiled with profile "cs_5_0".
All compiled files are outputted with name "<source file>o" (the letter "o" is added at end of filename). For example the file "MyShader.vs" will be compiled as "MyShader.vso".
Potential issues:
• For compute shader, the extension "cs" will conflict with ".cs" extensions for C# code.
• If shader profile version is not included in file, then for each version should be used a different msbuild task.
• To avoid manual editing for each project, the task DLL can be installed in GAC and task inclusion code can be added to some common "props" or "targets" file, but this will reduce solution portability.
Feel free to use and modify at your taste.