Page 1 of 1

Reflecting GLBase in shaders

Posted: Mon Jun 10, 2013 9:34 pm
by Rado1
The basic problem here is to write a shader which would work for both ZApplication.GLBase settings, for the Compatible mode and for the ES/GL3 mode.

The most effective way, is to obtain this information in the form of preprocessor macro/variable, because it should influence shader code compilation; a uniform variable does not seem to be an optimal solution. If we look at what is already available in GLSL, we can use GL_ES and __VERSION__. I tried the following:

Code: Select all

#if defined GL_ES  || __VERSION__ >= 130
#define ES2_GL3
#endif
#ifdef ES2_GL3
...
but these shaders do not work on Android. Could you please tell me where's the problem?

Anyway, even if __VERSION__ and GL_ES macros are used, they do not reflect setting of the ZApplication.GLBase propety; but just what version of the shading language is available on the device.

The desired solution would be to write code like this:

Code: Select all

#ifdef ES2_GL3
...
#endif
...
Of course, there is no ES2_GL3 macro defined (yet). Isn't it possible to define this (or similar) macro in ZGE used to reflect the ZApplication.GLBase property in its shaders?

Posted: Tue Jun 11, 2013 11:44 am
by VilleK
Hi,

I Googled and there does not seem to be a way to externally define preprocessor macros so that ES2_GL3 could be predefined by ZGE.

As for your first attempt, try breaking it up like this and see if it helps:

Code: Select all

#ifdef GL_ES
#define ES2_GL3
#endif

#if __VERSION__ >= 130
#define ES2_GL3
#endif

#ifdef ES2_GL3 
...
/Ville

Posted: Tue Jun 11, 2013 12:09 pm
by Kjell
Hmm,

Perhaps you could go with / write a super basic parser that goes through the shader source and generates the appropriate source / string depending on the platform you're building to. That way only the platform specific implementation ends up in the build.

K

Posted: Thu Jun 13, 2013 7:37 pm
by Rado1
Ville, unfortunately we cannot rely on the __VERSION__ variable - it returns 110 even if my GPU supports GLSL 4.20.
Kjell, the parser you mentioned is probably the only reliable solution to reflect ZApplication.GLBase in shaders. It can be relatively simple: e.g. to replace predefined string (e.g. GLBASE) to the value of ZApplication.GLBase (0 or 1). It would be possible to write:

Code: Select all

#if GLBASE == 1
precision mediump float;
#endif
But having this feature is not of very high importance, because for Android-compatible applications a simple rule can be used: if no shaders are used in the project - compatibility mode can be used; if shaders are used - they should be GLSL-compatible and ES2/GL3 mode should be used.

Of course, another reliable solution is to have 2 versions of shaders for the both GLBase modes and to use just one of them in a Material component, depending on setting the ZApplication.GLBase property. I'm using this style now, but it is difficult to maintain.