Skip to content

GLSL compilation

Hilderin edited this page Nov 10, 2023 · 1 revision

Before you can use your shaders, you need to compile the glsl code to Spirv binary code. Only Spirv can be uploaded to the GPU. LunarG (creator of the Vulkan SDK) provides use with a command line executable to do exactly that: glslangValidator.exe Google also provides a compiler: glslc.exe You can use either of them, so far both seems to work well and are included with the Vulkan SDK in the bin directory. In MiniEngine, we use glslc.exe.

Prerequisites

  • First, you need the Vulkan SDK installed on your computer. You can download it here: https://www.lunarg.com/vulkan-sdk/
  • Be sure the environment variable 'VULKAN_SDK' is correctly configured and points to your Vulkan SDK root folder (ex: C:\VulkanSDK\1.3.261.1)

Manual compilation with glslangValidator.exe

Commandline:

glslangValidator.exe -V [sourcefile] -o [outputfile]

Exemple:

glslangValidator.exe -V shader-simple.vert -o shader-simple.vert.spv

Important: the extension of the sourcefile is important:

Extension Stage
.vert vertex
.frag fragment
.vert vertex
.tesc tesselation control
.tese tesselation evaluation
.geom geometry
.comp mesh
.rgen ray generation
.rint ray intersection
.rahit ray any hit
.rahit ray any hit
.rchit ray closest hit
.rmiss ray miss
.rcall ray callable
.glsl .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes
.hlsl .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes

Manual compilation with glslc.exe

Commandline:

glslc.exe [sourcefile] -o [outputfile]

Exemple:

glslc.exe shader-simple.vert -o shader-simple.vert.spv

Important: the extension of the sourcefile is important:

Extension Stage
.vert vertex
.frag fragment
.vert vertex
.tesc tesselation control
.tese tesselation evaluation
.geom geometry
.comp compute

Compilation from C#

MiniEngime providers the 'ShaderCompiler' class to encapsulate the call to glslc.exe.

The method Compile takes the shader string code and the stage in parameters and returns the spirv binary code. If some compilation errors occur, an Exception will be throwed.

Exemple:

using MiniEngine.Drivers.Vulkan;

string code = @"#version 450

layout(location = 0) out vec4 outColor;

void main() {
    outColor = vec4(1, 1, 1, 1);
}";

byte[] spirv = ShaderCompiler.Compile(code, ShaderStageFlags.Fragment);

Clone this wiki locally