4

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

Does the g++ compiler do it?

Nicol Bolas
  • 9,762
  • 18
  • 25
user9778
  • 191
  • 3
  • 7

1 Answers1

3

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.

Nathan Reed
  • 25,002
  • 2
  • 68
  • 107