Skip to main content
GameDev.net gamedev.net
🔒 Locked

GLSL compiled to SPIR-V

Started by taby Mar 27, 2023 at 5:03 PM 8 replies 10.5k views
Original Post
taby
taby

Let me see if I’ve got this straight: Vulkan uses SPIR-V shaders, which are basically pre-compiled GLSL shaders?

cgrant
cgrant

SPIR-V afaik is just an intermediate representation, any compiler that generate SPIR-V ( obviously matching the Vulkan programmable graphics pipeline stages) would theoretically work and is not limited to GLSL. You have compilers that will generate SPIR-V from HLSL also. The GPU specific Vulkan shader compiler would then convert that intermediate representation into HW specific shader blob. Not too different from what they do for OpenGL except that the compiler can be simpler as its now just functioning as a translator( gross simplification as there is still potential HW specific optimization).

taby
taby

I use glslc from the Vulkan SDK to generate the spv files.

PAM79
PAM79

taby said:

Let me see if I’ve got this straight: Vulkan uses SPIR-V shaders, which are basically pre-compiled GLSL shaders?

Yep! A SPIR-V module is a sequence of 32-bit words stored in memory, generated by a tool from GLSL or HLSL code. The Vulkan driver uses this binary blob to compile machine (GPU) code during pipeline creation. Therefore, as stated by the specification, SPIR-V is a binary intermediate representation interchange format used to interface with a heterogeneous machine.

taby
taby

Does this conversion obfuscate the meaning of the code? Like, are there good SPIR-V disassemblers, or are they all relatively useless?

hplus0603
hplus0603

Some constructs will be de-sugared by the compiler, and won't be reversed back to the original code, for sure.

The code can be read, though. (You'd also need to come up with nice names for the bindings for yourself, those are not stored in the bytecode.)

Try SPIR-V-Cross, or Shader Playground (which lets you play with a variety of compilers / decompilers)

https://github.com/KhronosGroup/SPIRV-Cross​

https://github.com/tgjones/shader-playground​

enum Bool { True, False, FileNotFound };
taby
taby

I'm trying to grok this.

In OpenGL, I create a vector etc., and add triangle data into that. Then, I pass it to the GPU via glVertexAttribPointer.

In Vulkan, I use the exact same vector, but I pass it using some Vulkan function(s). Is this correct? It would seem so, since the shaders are still GLSL, right?

taby
taby

Is Vulkan seriously just OpenGL with ray tracing plus a neurotic amount of control over the GPU?

PAM79
PAM79

Yes and no. The graphic and computing functionalities offered by OpenGL and Vulkan overlap, and you probably won't get a performance boost from the GPU by using Vulkan instead of OpenGL. In other words, the GPU won't render more frames just because you're using Vulkan. However, there are still advantages in using Vulkan rather than OpenGL, such as lower CPU overhead. Please refer to section 1.2.1 of my "Hello Window" tutorial for additional details.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.