4
$\begingroup$

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

Does the g++ compiler do it?

$\endgroup$

1 Answer 1

3
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.