Skip to content

Instantly share code, notes, and snippets.

@jdryg
Last active August 14, 2018 17:15
Show Gist options
  • Save jdryg/d2c9c0a423308e5814425461d90c3e52 to your computer and use it in GitHub Desktop.
Save jdryg/d2c9c0a423308e5814425461d90c3e52 to your computer and use it in GitHub Desktop.
bgfx shader custom MSBuild rule for VS2013

Installation

  1. Download all 3 files and place them at the same folder as your VS solution
  2. Build shaderc and move the executable to the same folder. If you get the original executable from the build directory, remember to rename it to shaderc.exe instead of shadercRelease.exe
  3. Open your solution into VS, right click on the project and select Build Dependencies > Build Customizations...
  4. Click Find existing... and select the bgfxshader.targets file you just created
  5. Add .sc files to your project and they should be treated as bgfx shaders

To verify that the new target works correctly you can right click on an .sc file and select Properties. Item Type property should read "bgfx Shader"

Notes

  • IMPORTANT: The include directory for bgfx_shader.sh is hardcoded to ../bgfx/src in the batch file. Change it to your prefered location.
  • Vertex shaders should have a vs_ prefix and fragment shaders should have a fs_ prefix
  • You can exclude .sc files from the build from their properties page. E.g. varying.def.sc shouldn't be compiled (it's actually ignored by the batch file).
  • The script creates 4 folders (dx9, dx11, glsl and metal) under the folder where the .sc file is located.

TODO

  • Specify additional include directories from within VS and pass them to the batch file.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PropertyPageSchema
Include="$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml" />
<AvailableItemName Include="bgfxShaderTask">
<Targets>_bgfxShaderTask</Targets>
</AvailableItemName>
</ItemGroup>
<PropertyGroup>
<ContentFilesProjectOutputGroupDependsOn>$(ContentFilesProjectOutputGroupDependsOn);MakeOutputDirs</ContentFilesProjectOutputGroupDependsOn>
</PropertyGroup>
<Target Name="MakeOutputDirs">
<ItemGroup Condition="'@(bgfxShaderTask)'!=''">
<bgfxShaderTaskDirsToMake Include="%(bgfxShaderTask.RelativeDir)dx9;%(bgfxShaderTask.RelativeDir)dx11;%(bgfxShaderTask.RelativeDir)glsl;%(bgfxShaderTask.RelativeDir)metal" />
</ItemGroup>
<MakeDir Directories="@(bgfxShaderTaskDirsToMake)" />
<ItemGroup>
<bgfxShaderTaskDirsToMake Remove="@(bgfxShaderTaskDirsToMake)" />
</ItemGroup>
</Target>
<PropertyGroup>
<CleanDependsOn>
$(CleanDependsOn);
CustomAfterClean
</CleanDependsOn>
</PropertyGroup>
<!-- Clean the generated output files -->
<Target Name="CustomAfterClean" DependsOnTargets="MakeOutputDirs">
<Delete Files="%(bgfxShaderTask.RelativeDir)dx9\%(bgfxShaderTask.Filename).bin;%(bgfxShaderTask.RelativeDir)dx11\%(bgfxShaderTask.Filename).bin;%(bgfxShaderTask.RelativeDir)glsl\%(bgfxShaderTask.Filename).bin;%(bgfxShaderTask.RelativeDir)metal\%(bgfxShaderTask.Filename).bin" />
</Target>
<Target
Name="_bgfxShaderTask"
BeforeTargets="ClCompile"
DependsOnTargets="_SelectedFiles;MakeOutputDirs"
Inputs="%(bgfxShaderTask.Identity)"
Outputs="%(bgfxShaderTask.RelativeDir)dx9\%(bgfxShaderTask.Filename).bin;%(bgfxShaderTask.RelativeDir)dx11\%(bgfxShaderTask.Filename).bin;%(bgfxShaderTask.RelativeDir)glsl\%(bgfxShaderTask.Filename).bin;%(bgfxShaderTask.RelativeDir)metal\%(bgfxShaderTask.Filename).bin">
<Message Text="Building shader: %(bgfxShaderTask.Identity)" />
<Exec
Command="build_shader.bat %(bgfxShaderTask.Identity)"
Condition="'%(bgfxShaderTask.ExcludedFromBuild)' != 'true'" />
</Target>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<ProjectSchemaDefinitions xmlns="http://schemas.microsoft.com/build/2009/properties" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ItemType
Name="bgfxShaderTask"
DisplayName="bgfx Shader" />
<FileExtension
Name="*.sc"
ContentType="bgfxShaderTask" />
<ContentType
Name="bgfxShaderTask"
ItemType="bgfxShaderTask"
DisplayName="bgfx Shader" />
</ProjectSchemaDefinitions>
@echo off
set @bgfxIncludeDirectory=../bgfx/src
set @inputFolder=%~dp1
set @filePathWithoutExt=%~dpn1
set @filename=%~n1;
set @prefix=%@filename:~0,3%
setlocal EnableDelayedExpansion
if "%@prefix%" equ "vs_" (
echo Building vertex shader %~n1.sc
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\dx9\%~n1.bin --type v --platform windows -p vs_3_0 -O 3 -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\dx11\%~n1.bin --type v --platform windows -p vs_4_0 -O 3 -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\glsl\%~n1.bin --type v --platform linux -p 120 -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\metal\%~n1.bin --type v --platform osx -p metal -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
) else (
if "%@prefix%" equ "fs_" (
echo Building fragment shader %~n1.sc
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\dx9\%~n1.bin --type f --platform windows -p ps_3_0 -O 3 -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\dx11\%~n1.bin --type f --platform windows -p ps_4_0 -O 3 -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\glsl\%~n1.bin --type f --platform linux -p 120 -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
shaderc.exe -f %@filePathWithoutExt%.sc -o %@inputFolder%\metal\%~n1.bin --type f --platform osx -p metal -i %@bgfxIncludeDirectory%
if !errorlevel! neq 0 (exit /b !errorlevel!)
) else (
echo Unknown shader file prefix. Ignoring shader source %~n1.sc
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment