Skip to content

Compiling Shaders

Adam Gorski edited this page Aug 22, 2021 · 3 revisions

Compiling a Shader

To compile a shader, you will first need to parse it with XFDraw's parser:

ShaderCompile sModule = ShaderParser.Parse("basicVS.glsl", "basicFS.glsl", "basicShader");
XFDraw checks if the old and new files have any changes and will only parse and compile if changes were made to the shader files!

Parsing these shaders will create a temp folder which will contain the the merged files. You should see 6 new files:

File Description
basicVS.glsl Duplicate used for version checking
basicFS.glsl Duplicate used for version checking
xfbasicShader.cpp Vertex and Fragment shader merged into final shader
xfconfig.cpp File used to store shader serialization data
xfcore.cpp File contains clipping methods for 3D rendering
xfcore.h Header file that contains structs and functions

Next up its time to setup the compiler: Obviously, XFDraw uses cl.exe to compile the shaders. This results in very high performance shaders with near zero overhead, however it complicates support a bit. You will need to specify the location of cl.exe OR compile the shader yourself.

//Default compiler settings:
ShaderCompile.COMPILER_LOCATION = @"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\";

//Default environment settings:
ShaderCompile.COMPILER_ENV_SETUP = @"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat";

You can also specify command line arguments. Note that by default XFDraw uses /GL (program wide optimization) which can take up to 10 seconds to compile:

//Default settings:
ShaderCompile.COMMAND_LINE = @"/openmp /nologo /GS /GL /Oi /MD /O2 /fp:fast -Ofast /Oy /Ox /Ot";

//No settings:
ShaderCompile.COMMAND_LINE = "";

//Debug settings
ShaderCompile.COMMAND_LINE = "/DEBUG /ZI";

Next up its time to compile the shader:

Shader basicShader;

if (!sModule.Compile(out basicShader))
   throw new Exception("Failed to compile shader!");

And thats it!

Compile options

The XFParser includes a few compile options in the form of the CompileOption enum:

Compile Option Implemented Description
CompileOption.None Yes Self-Explanatory
CompileOption.AddInlineAll No Inlines all shader methods (NOT `void main()
CompileOption.ForceRecompile Yes Forcefully recompiles the shader regardless if changes were made
CompileOption.DoNotSerialize Yes Skips shader data serialization. This will prevent you from loading shader dll's directly.
CompileOption.EnableMSAA Mostly Enable MSAA data. Unfortunately MSAA is currently broken
CompileOption.EnableSIMD No Includes special matrix multiplication and bilinear sampling functions
CompileOption.IncludeCstdio Yes Includes cstdio.h for printf debugging support
CompileOption.UseOMP Partially Forces the shader to use OpenMP parallelization
CompileOption.UsePPL Partially Forces the shader to use the parallel pattern library
CompileOption.UseFor Partially Disables all parallelization
Note: By default screenspace shaders use OpenMP and non screenspace shaders use the parallel patterns library. Note that OpenMP had a massive performance loss when a large amount of faces were clipped. Because of this it is recommended to leave the parallelization on it default settings.

Clone this wiki locally