6
$\begingroup$

I don't know any shader languages. I've heard of GLSL and HLSL, and I'm interested in learning one or both.

Are there significant differences between them that would make one or other better in certain situations? Is it useful to know both or would either cover most needs?

I don't want vague answers indicating personal preference. I'm looking for specific measurable differences so that I can decide for myself which will suit me best. I don't have a specific task in mind - I'm hoping to discover whether there is one or other that I can learn and then apply to any future tasks, rather than having to learn a new language for each new task.

If there are other shader languages which I have not mentioned I would be interested to hear the comparison for those too, provided they are not dependent on any particular GPU manufacturer. I want my code to be portable across different graphics cards.

$\endgroup$
7
$\begingroup$

The shader language is bound to the APIs/engines that support it (glsl to openGL & WebGL and hlsl to D3D). There are tools to translate from one to the other but they aren't perfect.

This means the main reason to pick one over the other is which platform you will be working on.

However by the end of the year a new binary shader representation will come out called Spir-V designed for Vulkan and an OpenGL extension for it is expected to follow shortly. This will allow you to write shaders in many languages as long as a translation tool to Spir-V exists (several are in the works including a Python and Haskell tool).

$\endgroup$
  • $\begingroup$ You were like a few seconds faster than I and even have information about Spir-V, which I did not even know. $\endgroup$ – Nero Aug 20 '15 at 11:07
4
$\begingroup$

The HLSL shader language is used in DirectX family APIs and GLSL for the OpenGL family APIs. This means that the choice of graphics API will limit the shader language as well, so if cross platform is a concern then GLSL will be the choice. However, this comes with some caveats as well.

One difference is that in OpenGL the shading language is compiled on driver level, this means that the binary representation is not stable and can change with driver updates. In HLSL it is compiled to a hardware independent representation that can be used on multiple GPUs. This should really be a problem for writing shaders, but it sometimes means that certain things work different with other drivers and especially operating systems.

Another thing to take note is that OpenGL features extensions, these can be enabled inside GLSL to utilize extra functionality. With newer OpenGL versions some of these extensions might be added to the core profile. Vendors then have to implement these extensions to be compliant, which sometimes results in things working different. The HLSL shading language syntax only changes with new introduction of DirectX APIs.

Some syntactic differences are that HLSL can use #include, in GLSL this is not default. When searching for GLSL tutorials you might find different syntax, because it has changed quite a lot as of OpenGL 3 and 4. GLSL shaders have a void main() as shader entry point. In HLSL this entry point can be named and have input (vertex data) and output (pixel shader input).

Learning either of these will introduce you to some common ground which will make it easy to learn to other one. HLSL is more standardized, but it also only works on a single operating system. There are also other attempts of shader languages that compile to both OpenGL and DirectX, one example is Cg which is deprecated by now. Many game engines (Unity, Unreal Engine) also have their own flavors, but their syntax is like HLSL or GLSL.

As already mentioned Vulkan will introduce an open intermediate language representation called Spir-V. This means that if a language can be compiled to this representation it can be used to write shaders.

$\endgroup$
  • 1
    $\begingroup$ GLSL functions cannot return a value? Hum? From where did you get this idea? $\endgroup$ – glampert Aug 20 '15 at 17:41
  • $\begingroup$ I always used to the value-return calling convention as described here opengl.org/wiki/Core_Language_%28GLSL%29#Functions. If it is possible to define the return type and use "return variable;" syntax I will change it. EDIT: nvm, you're right. The OpenGL 4.5 specification says it should be possible (but I don't know when it was introduced) $\endgroup$ – Maurice Laveaux Aug 20 '15 at 18:14
  • 1
    $\begingroup$ Return values have always been supported, AFAIK. If you look at the 1.2 specification, section 6.1, you can see the function declaration syntax: returnType functionName (type0 arg0, type1 arg1, ..., typen argn);. Plus, a lot of builtin functions return a value... Perhaps the existence of in/out parameters has tricked you into thinking it didn't support return values, but both are orthogonal concepts. Apart from that, your answer is pretty good, btw ;) $\endgroup$ – glampert Aug 20 '15 at 18:26
  • 1
    $\begingroup$ Yeah, I probably just looked at the wiki instead of the specification itself. Never occurred to me that both methods are possible, although indeed the buildin functions do use it. $\endgroup$ – Maurice Laveaux Aug 20 '15 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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