Differences between GLSL and GLSL ES 2

Mr. Boy picture Mr. Boy · Apr 30, 2012 · Viewed 9.7k times · Source

Two questions really...

  1. Is GLSL ES 2 a totally separate language, or a special version of GLSL?
  2. What are the differences between them, in terms of "standard library" functions, syntax and capabilities?

I am writing shaders for an application targeted at Windows, Mac and iPad and I would prefer not to have to add more versions of each shader - well simpler shaders anyway.

Answer

Nicol Bolas picture Nicol Bolas · Apr 30, 2012

Is GLSL ES 2 a totally separate language, or a special version of GLSL?

Every version of GLSL is ultimately a "totally separate language;" some aren't even backwards compatible with previous versions. However, the ES variation of GLSL is even moreso.

Think of it as a fork of desktop GLSL. A fork of GLSL from six versions ago, mind you.

What are the differences between them, in terms of "standard library" functions, syntax and capabilities?

Between which versions?

The only possible reason you could have to try to share shaders between these platforms is if you're trying to share OpenGL code between them. So you'd have to be limiting yourself to desktop OpenGL 2.1 functionality.

That means you'd be using desktop GLSL version 1.20. This is similar to GLSL ES version 1.00 (the version of GLSL is not given the same number as the matching GL version. Well, until desktop GL 3.3/4.0 and GLSL 3.30/4.00), but still different. In ES, you have a lot of precision qualifiers that desktop GLSL 1.20 doesn't handle.

The best way to handle that is to use a "preamble" shader string. See, glShaderSource takes multiple shader strings, which it slaps together and compiles as one. You can take your main shader string (main and its various code, attribute definitions, uniforms, etc) and stick a "preamble" shader string in front of that. The preamble would be different depending on what you're compiling for.

Your ES GLSL 1.00 preamble would look like this:

#version 100  //Must start with a version specifier.

Your desktop GLSL 1.20 preamble would look like this:

#version 120
#define lowp
#define mediump
#define highp

And maybe a few other judicious #defines. This way, your shader can have those precision qualifiers set, and they'll just be #defined away when using desktop GLSL.

This is a lot like platform-neutral coding in C/C++, where you often have some platform-specific header that changes the definition and/or has #defines that you can condition code based on.