2
votes

I'm developing a cross-platform game, for iOS, Windows, and OS X. Right now I'm at the point where I'm writing all my shaders for my graphics. So here's the question:

If I have working GL ES 2.0 shaders (#version 100) for the mobile version of the game (which I developed fist), should I rewrite them in a more modern version of desktop GLSL (like #version 330 or something) for the desktop port of the game, or should I just use those shaders as-is? I tested the ES 2.0 shaders in a desktop environment in a test application, and they seem to work perfectly.

So do I (generally speaking) benefit from rewriting the shaders in a more modern GLSL version? I'm not talking about optimizing the shader code itself for mobile/desktop, that's fairly obvious, I will do that of course. I'm talking about using different GLSL language versions on different platforms. Is there any performance difference, compatibility issue, or anything of that sort, that's a good enough reason for porting the ES 2.0 shaders to a more modern version on desktop?

Since we're talking about fairly simple shaders for a 2D game, I won't really benefit from the extra functionality and better API of more modern desktop GLSL versions. #version 100 has enough to get the job done, it works fine for me. So the more modern functionality, I can do without. Other than that, is there any reason not to use #version 100 on desktop?

2

2 Answers

3
votes

So do I (generally speaking) benefit from rewriting the shaders in a more modern GLSL version? I'm not talking about optimizing the shader code itself for mobile/desktop, that's fairly obvious, I will do that of course. I'm talking about using different GLSL language versions on different platforms. Is there any performance difference, compatibility issue, or anything of that sort, that's a good enough reason for porting the ES 2.0 shaders to a more modern version on desktop?

Performance, no, not really.

Compatibility, on the other hand, is a very real concern.

The precision qualifiers will confuse older versions of GLSL, but everything else should port fine without any re-write. It is understood that precision in desktop GLSL is offered only for compatibility and that its addition to later version does nothing beyond prevent parse errors. It will not improve performance or anything of that sort.

The only reason you might be forced to significantly re-write the shaders is if you were to try to port the software to a core profile on OS X. You have to use GLSL 1.30+ syntax in that situation.

So the more modern functionality, I can do without. Other than that, is there any reason not to use #version 100 on desktop?

Technically speaking, #version 100 does not exist in desktop GL. There was an extension that pre-dates GL 2.0 that vaguely defined it, but generally you assume that in desktop GL the GLSL version numbering starts at 110. GLSL ES version 100 is actually closer in syntax and functionality to desktop GLSL 120, but with a few extra things such as precision that desktop GLSL did not understand for quite some time.

You are going to have to re-write the #version directive at the very least. Most desktop GLSL compilers will not like it if you tell them to compile the shader against GLSL 1.00.

3
votes

When making cross-platform shaders, we have found that most simple shaders will work fine on both.

When you need to optimise those shaders for mobile, one port of call is to reduce precision using the precision qualifiers (lowp/mediump/highp).

However, these will not compile on desktop glsl without #version 130 being added at the top of the shader.

However, adding that will break the shaders on mobile.

The balance we have found, is to optimise the mobile shaders with precision qualifiers. In the shader loading code on desktop platforms, we insert the following lines at the top of every shader:

#define lowp
#define mediump
#define highp

This blanks out those definitions using the preprocessor. This effectively bypasses this optimisation on desktop, but as the answer from Andon M. Coleman states, they won't do anything anyway.

If any of the shaders are particularly complex, I would recommend using separate shaders that can take into account the hardware strengths and weaknesses.