Shader Toolkit
In this course a shader is compiled the moment you press Run, by the browser. In a real project, shaders go through a toolchain: compilers that turn source into GPU bytecode, cross-compilers that translate one language into another, validators, optimizers and editors. This lesson is a tour of the tools graphics programmers actually install, what each one is for, and the commands you will type.
The big picture
┌──────────── glslang / glslc (shaderc) ───────────┐
GLSL ───────────┤ ▼
HLSL ── DXC ────┼──── -spirv ───────────────────────────────► SPIR-V ──► Vulkan (and OpenGL 4.6)
│ │
Slang ─ slangc ─┼──► SPIR-V, HLSL, GLSL, Metal, WGSL, CUDA… ├─ spirv-cross ─► GLSL / HLSL / MSL
│ └─ naga / Tint ─► WGSL
HLSL ── DXC ────┴──────────────────────────────────────────► DXIL ──► Direct3D 12SPIR-V is the hub. It is Khronos' binary intermediate representation for shaders: Vulkan only accepts SPIR-V, OpenGL 4.6 can load it, and almost every cross-compiler reads or writes it. DXIL is the Direct3D 12 equivalent.
Compilers
glslang and glslangValidator
glslang is the Khronos reference front end for GLSL (and partial HLSL). Its command-line tool, glslangValidator (newer builds also install it as glslang), checks a shader against the spec and compiles it to SPIR-V. The stage comes from the file extension (.vert, .frag, .comp…):
glslangValidator shader.frag # just validate (OpenGL GLSL rules)
glslangValidator -V shader.frag -o frag.spv # compile for Vulkan to SPIR-V
glslangValidator -V -g shader.frag -o frag.spv # with debug info, for RenderDoc's source-level debuggingIt is also the engine behind many editor linters, so its error messages are the ones you will see most.
shaderc and glslc
shaderc is Google's library around glslang, adding #include support and a GCC-like command line, glslc. It ships with the Vulkan SDK and the Android NDK:
glslc shader.frag -o frag.spv # stage from the extension
glslc -fshader-stage=frag effect.glsl -o frag.spv
glslc -O --target-env=vulkan1.3 -I include/ shader.frag -o frag.spv
glslc -DUSE_FOG=1 shader.frag -o frag_fog.spv # preprocessor defines for variantsThe library form (libshaderc) is what engines link to compile shaders at build time or in an editor.
DXC
The DirectX Shader Compiler compiles HLSL (Shader Model 6.x) to DXIL for Direct3D 12, and with -spirv to SPIR-V for Vulkan. That second ability made HLSL a first-class Vulkan language, and it is why many engines write all their shaders in HLSL:
dxc -T ps_6_0 -E main shader.hlsl -Fo shader.cso # pixel shader → DXIL
dxc -T vs_6_0 -E VSMain shader.hlsl -Fo vs.cso
dxc -spirv -T ps_6_0 -E main -fspv-target-env=vulkan1.3 shader.hlsl -Fo shader.spv-T is the target profile (vs_6_0, ps_6_6, cs_6_0, lib_6_3 for ray tracing…), -E the entry point. The older fxc compiler (Shader Model 5 and below, DXBC bytecode) is still found in D3D11 code.
Slang
Slang started at NVIDIA and is now hosted by Khronos. It is an HLSL-compatible language extended with modules, generics and interfaces, and it compiles to SPIR-V, DXIL, HLSL, GLSL, Metal, WGSL and CUDA. For a new multi-API renderer, it is a strong choice because one source reaches every backend:
slangc shader.slang -target spirv -entry fragmentMain -stage fragment -o frag.spv
slangc shader.slang -target hlsl -entry fragmentMain -stage fragment -o frag.hlsl
slangc shader.slang -target wgsl -o shader.wgslCheck slangc -h for your version's options; the project moves quickly.
Cross-compilers and SPIR-V tools
SPIRV-Cross turns SPIR-V back into readable GLSL, GLSL ES, HLSL or Metal Shading Language, and reports the shader's resource bindings (reflection). It is how many engines reach Metal and WebGL from one source.
bashspirv-cross frag.spv --version 300 --es --output frag_es.glsl # GLSL ES 3.00 for WebGL2 spirv-cross frag.spv --msl --output frag.metal # Metal spirv-cross frag.spv --hlsl --shader-model 50 --output frag.hlsl spirv-cross frag.spv --reflect # JSON list of inputs, uniforms, bindingsSPIRV-Tools (in the Vulkan SDK):
spirv-valvalidates a module,spirv-disdisassembles it to text (great for seeing what the compiler did with your branch),spirv-opt -Ooptimizes it.naga (Rust, part of wgpu) and Tint (C++, part of Dawn) translate between WGSL, SPIR-V, GLSL, HLSL and MSL for WebGPU stacks.
nagahas a CLI (cargo install naga-cli;naga input.spv output.wgslpicks formats from the extensions).
Reflection: let the shader describe itself
A recurring engine problem: C++ code must know which uniform buffers, textures and vertex inputs a shader expects, at which bindings. Instead of maintaining that by hand, engines reflect the compiled shader with SPIRV-Reflect, SPIRV-Cross's reflection API, DXC's reflection interface or Slang's reflection API, and build descriptor set layouts or root signatures automatically.
Writing and previewing shaders
- Shadertoy: the community site for fragment-shader art, in the same
mainImagestyle as this course, with thousands of shaders to read. Mind each shader's license before copying code into a commercial project. - Editors: in VS Code, extensions for GLSL, HLSL and WGSL highlighting and linting (several GLSL linters run
glslangValidatorbehind the scenes); HLSL Tools for HLSL navigation and IntelliSense; Rider has built-in HLSL/ShaderLab support for Unity. Visual Studio has HLSL support through its graphics tools. - Node editors: Unity Shader Graph, Unreal's Material Editor, Godot's VisualShader, Blender's shader nodes. Great for iteration, and their generated code is a learning resource.
- Live-coding tools: KodeLife (desktop live shader editor), and the WebGL/WebGPU playgrounds of three.js and Babylon.js.
Debuggers and profilers
Covered in depth in Debugging Shaders: RenderDoc (default frame debugger), PIX (D3D12), NVIDIA Nsight Graphics, the Xcode Metal debugger, AMD Radeon GPU Profiler and Radeon GPU Analyzer (shows the ISA your HLSL/GLSL becomes on AMD hardware, plus register usage), and Spector.js for WebGL.
A minimal build step
Compile shaders as part of the build, not at runtime, so errors appear in CI and the game ships bytecode. With CMake and glslc from the Vulkan SDK:
find_package(Vulkan REQUIRED COMPONENTS glslc)
set(SHADERS shaders/triangle.vert shaders/triangle.frag)
foreach(SRC ${SHADERS})
get_filename_component(NAME ${SRC} NAME)
set(SPV ${CMAKE_BINARY_DIR}/shaders/${NAME}.spv)
add_custom_command(OUTPUT ${SPV}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/shaders
COMMAND Vulkan::glslc ${CMAKE_SOURCE_DIR}/${SRC} -o ${SPV}
DEPENDS ${SRC})
list(APPEND SPV_FILES ${SPV})
endforeach()
add_custom_target(shaders ALL DEPENDS ${SPV_FILES})Which tools to install first
| You are… | Install |
|---|---|
| learning GLSL (this course) | a browser, Shadertoy, Spector.js |
| writing Vulkan | the Vulkan SDK (glslang, glslc, SPIRV-Tools, SPIRV-Cross, validation layers), RenderDoc |
| writing D3D12 | DXC (also in the Windows SDK), PIX, RenderDoc |
| targeting several APIs | Slang or DXC + SPIRV-Cross, plus a reflection library |
| in an engine | the engine's editor, RenderDoc, your GPU vendor's profiler |
Test yourself
Which compiler turns HLSL into DXIL for Direct3D 12 and, with -spirv, into SPIR-V for Vulkan?
- glslc
- DXC
- fxc
- spirv-cross
What does SPIRV-Cross do?
- Compiles GLSL to SPIR-V
- Converts SPIR-V into GLSL, HLSL or Metal source and reflects resource bindings
- Profiles GPU performance
- Validates Vulkan API calls
Why compile shaders at build time instead of at runtime in a Vulkan game?
- Vulkan can't load shaders at runtime
- Vulkan only accepts SPIR-V, and build-time compilation catches errors early and avoids shipping a compiler
- It makes shaders run on any GPU
- It removes the need for pipelines