Skip to content
systemcoding.dev
GPU

Compile a shader to SPIR-V with glslc

Compile a GLSL shader to SPIR-V with glslc from the Vulkan SDK, letting the file extension pick the stage and setting the target environment.

1 min read
Summarize in AI

Vulkan does not accept GLSL. It takes SPIR-V, so every shader goes through a compiler first. glslc comes with the LunarG Vulkan SDK 1.3 and wraps Google's shaderc. It picks the shader stage from the file extension: .vert, .frag, .comp, and the others.

triangle.vertglsl
#version 450

layout(location = 0) in vec3 in_pos;
layout(location = 1) in vec3 in_color;
layout(location = 0) out vec3 v_color;

void main()
{
    gl_Position = vec4(in_pos, 1.0);
    v_color = in_color;
}
Shell
glslc --target-env=vulkan1.3 -O triangle.vert -o triangle.vert.spv

--target-env=vulkan1.3 makes glslc emit SPIR-V 1.6 and allows features of that Vulkan version. Without it the default is Vulkan 1.0 and the output is older SPIR-V, which still loads but rejects newer extensions in the source. -O runs the optimiser, and -O0 skips it, which keeps names for debugging.

The result is a binary, not text. The first four bytes are the SPIR-V magic number 0x07230203 in little-endian order, which is a quick way to confirm the file is real:

Shell
xxd -l 4 triangle.vert.spv
Text
00000000: 0302 2307                                ..#.

For a file with a non-standard extension, force the stage with -fshader-stage=vertex. Compile at build time in your build system and not at run time, so a shader syntax error fails the build.

Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.