Search code examples
openglglsl

Which GLSL versions to use for maximum compatibility


Update 2

I think my question might really be, "How do I write OpenGL code that will work on any machine that supports at least OpenGL 2.0?" Bearing in mind that a compatibility profile is not guaranteed to be available.

Update 1:

Rabbid76 is suggesting to either use a specific core profile or use a compatibility profile.

The compatibility profile isn't guaranteed to be available so let's say I choose to use OpenGL 3.3 core profile as the minimum requirement. Wouldn't it still be the case that I would have to write three different versions of the GLSL shaders?

  • 3.30 For OpenGL 3.3
  • 4.00 For OpenGL 4.0
  • 4.10 For OpenGL 4.1 and onwards

From reading around people seem to assume that GLSL 3.30 works on OpenGL 3.3 and all later versions, but that doesn't seem to be guaranteed by any spec before OpenGL 4.2.

Is this right?


Original question:

I've read various versions of the OpenGL spec I can't see any useful guarantees about backwards compatibility of GLSL support.

Here's what I've deduced from the specs:

  • OpenGL 2.0 supports GLSL 1.10
  • OpenGL 2.2 supports GLSL 1.20
  • OpenGL 3.0 supports GLSL 1.10, 1.20 & 1.30
  • OpenGL 3.1 supports GLSL 1.30 & 1.40
  • OpenGL 3.2 supports GLSL 1.40 & 1.50
  • OpenGL 3.3 supports GLSL 3.30
  • OpenGL 4.0 supports GLSL 4.00
  • OpenGL 4.1 supports GLSL 4.10
  • OpenGL 4.2 supports GLSL 4.20 and all versions back to 1.40
  • From OpenGL 4.2 onwards it seems that support back to GLSL version 1.40 is guaranteed.

Note that guarantees of backwards compatibility are quite rare until OpenGL 4.2!

So if I want to ship an application which supports all versions of OpenGL back to 2.0 I would need to write every shader in the following versions:

  • 1.10 For OpenGL 2.0
  • 1.20 For OpenGL 2.2 & 3.0
  • 1.40 For OpenGL 3.1, 3.2 & 4.2 onwards
  • 3.30 For OpenGL 3.3
  • 4.00 For OpenGL 4.0
  • 4.10 For OpenGL 4.1

That's six versions of each shader! Is there an easier way to make GLSL portable?

Why are the specs so unhelpful in this regard?


Solution

  • Wouldn't it still be the case that I would have to write three different versions of the GLSL shaders?

    No.

    In order to use the core 3.3 profile, you must ask the WGL/GLX/EGL system that creates OpenGL to give you what you asked for. That is, it must give you core 3.3 or any later implementation that is compatible with core 3.3.

    In order for an implementation to be compatible with 3.3, it must accept GLSL of version 3.30. So if you request a core 3.3 context, and you get a valid context of a higher version, that implementation will support 3.30 shaders.

    That is, just because a particular standard version doesn't mandate compatibility with older shader versions does not mean the implementation won't provide it.

    How do I write OpenGL code that will work on any machine that supports at least OpenGL 2.0?

    You can't. At least, not without distinct codepaths.

    Some implementations do not support the compatibility profile at all. As such, if you do not code against at least GL 3.2 (or whatever their lowest core implementation version is), your code won't be able to work on their implementations. It just isn't possible.

    Some implementations have a GL 2.x implementation as a fallback, but have a higher core profile implementation without the compatibility profile. You can access the fallback with your 2.0 code, but no extensions or core features for the newer stuff.

    It should also be noted that most implementations that are limited to OpenGL 2.0 are extremely old and likely quite bug-prone. You would have to test your code extremely broadly in order to know whether or not it would work on a bunch of them. So unless you have the resources to buy and test on dozens of outdated Intel integrated chipsets, it's best to pick a version that's more recent.