# How does GLSL code get “loaded” and “compiled”?

I just wanted to find out for sure how GLSL code gets loaded and compiled.

Does the g++ compiler do it?

No, g++/gcc are not involved.

In OpenGL (prior to Vulkan), GLSL code is submitted to the driver as source code strings via a glShaderSource call. The driver is fully responsible for compilation, i.e. the driver must provide a built-in GLSL compiler for that GPU architecture. This would typically happen on startup of an app, or during level loading for a game.

With Vulkan, SPIR-V was introduced, which is a device-independent bytecode for shaders (vaguely based on LLVM IR). Now, GLSL code can be pre-compiled to SPIR-V offline using the shaderc toolchain. The developers would do this ahead of time and ship the SPIR-V bytecode with their app. Then at runtime the SPIR-V gets submitted to the Vulkan driver and compiled the rest of the way to GPU machine code.

It's also possible to compile other languages to SPIR-V; for instance, Microsoft's dxc compiler for HLSL can optionally output SPIR-V, so you can use this to write Vulkan shaders in HLSL. Also, SPIR-V support has been backported to OpenGL 4.6, so you can now also load precompiled SPIR-V shaders in OpenGL.

• Does that mean you could write an app in GL 4.6 and do your shaders in HLSL as well?
– russ
Dec 2, 2018 at 3:38
• @russ Maybe! I've never tried it, but that may well be possible. Dec 2, 2018 at 5:49
• @russ Yes, it's possible to compile HLSL shaders to GLSL using the SPIRV-Cross compiler. Jul 4, 2020 at 13:47