Skip to content

Instantly share code, notes, and snippets.

@DrizztDoUrden
Last active October 18, 2017 14:04
Show Gist options
  • Save DrizztDoUrden/27b35e7268fb11598bf605b6a8eb5c1b to your computer and use it in GitHub Desktop.
Save DrizztDoUrden/27b35e7268fb11598bf605b6a8eb5c1b to your computer and use it in GitHub Desktop.
MSBuild shader auto compile before build
<Project>
<PropertyGroup>
<WindowsKitVersion Condition="$(WindowsKitVersion)==''">10</WindowsKitVersion>
<SaveAsm Condition="$(SaveAsm)==''">True</SaveAsm>
<ShadersSubDir Condition="'$(ShadersSubDir)'==''">$(OutputPath)\Shaders</ShadersSubDir>
<ShadersAsmSubDir Condition="'$(ShadersAsmSubDir)'==''">Shaders</ShadersAsmSubDir>
<ShaderModel Condition="$(ShaderModel)==''">5_1</ShaderModel>
<ShaderAsmExtension Condition="$(ShaderAsmExtension)==''">asm</ShaderAsmExtension>
<ShaderBinExtension Condition="$(ShaderBinExtension)==''">cso</ShaderBinExtension>
<FxcArgs Condition="'$(Configuration)'=='Debug'">$(FxcArgs) /Od /Zi</FxcArgs>
<FxcArgs Condition="'$(Configuration)'!='Debug'">$(FxcArgs) /O3</FxcArgs>
</PropertyGroup>
<PropertyGroup>
<WindowsKitsRoot Condition="$(WindowsKitsRoot)==''">$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots', 'KitsRoot$(WindowsKitVersion)', null, RegistryView.Registry32, RegistryView.Default))</WindowsKitsRoot>
<FxcPath>"$(WindowsKitsRoot)bin\x64\fxc.exe"</FxcPath>
</PropertyGroup>
<Target Name="CheckShaderCompilationDirectory">
<MakeDir Condition="!Exists('$(ShadersSubDir)')" Directories="$(ShadersSubDir)" />
<MakeDir Condition="!Exists('$(ShadersAsmSubDir)')" Directories="$(ShadersAsmSubDir)" />
</Target>
<Target Name="CompileShaders" BeforeTargets="Build" DependsOnTargets="CheckShaderCompilationDirectory"
Inputs="@(CompileShader);%(CompileShader.Dependancies)"
Outputs="@(CompileShader -> '$(ShadersSubDir)\%(filename).%(Shader).$(ShaderBinExtension)');@(CompileShader -> '$(ShadersAsmSubDir)\%(filename).%(Shader).$(ShaderAsmExtension)')" >
<Exec WorkingDirectory="$(ProjectDir)" Condition="$(SaveAsm)=='True'"
Command="$(FxcPath) $(FxcArgs) &quot;%(fullpath)&quot; /T %(Shader)_$(ShaderModel) /E %(Entry) /Fo &quot;$(ShadersSubDir)\%(filename).%(Shader).$(ShaderBinExtension)&quot; %(CompileShader.Arguments) /Fc &quot;$(ShadersAsmSubDir)\%(filename).%(Shader).$(ShaderAsmExtension)&quot;" />
<Exec WorkingDirectory="$(ProjectDir)" Condition="$(SaveAsm)!='True'"
Command="$(FxcPath) $(FxcArgs) &quot;%(fullpath)&quot; /T %(Shader)_$(ShaderModel) /E %(Entry) /Fo &quot;$(ShadersSubDir)\%(filename).%(Shader).$(ShaderBinExtension)&quot; %(CompileShader.Arguments)" />
</Target>
</Project>
<Project>
<ItemGroup>
<CompileShader Include="shaders.hlsl" Visible="false">
<Shader>vs</Shader>
<Entry>VSMain</Entry>
</CompileShader>
<CompileShader Include="shaders.hlsl" Visible="false">
<Shader>ps</Shader>
<Entry>PSMain</Entry>
<Dependancies>include.hlsl</Dependancies>
</CompileShader>
</ItemGroup>
<PropertyGroup>
<FxcArgs>/nologo</FxcArgs>
<FxcArgs Condition="$(Configuration)!='Debug'">$(FxcArgs) /Od /Zi</FxcArgs>
</PropertyGroup>
<Import Project="CompileShaders.targets" />
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment