pecet
pecet

Reputation: 125

Shaders and variables in OpenGL 2 on Android / LibGDX

Following code is fragment shader which I created using default LibGDX SpriteBatch shader simply modified to scramble RGB channels, and it works just fine on both Android and PC:

#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 v_color;
varying vec2 v_texCoord;
uniform sampler2D u_texture;

void main()
{
    gl_FragColor = v_color * texture2D(u_texture, v_texCoord);
    gl_FragColor.rgb = vec3(gl_FragColor.g, gl_FragColor.r, gl_FragColor.b)
}

however when I try to add any variable to shader program like so (i excluded #ifdef ... u_texture; but it's still in shader itself):

float test;

void main()
{
    gl_FragColor = v_color * texture2D(u_texture, v_texCoord);
    test = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0f;
    gl_FragColor.rgb = vec3(test, test, test);
}

it works on pc, but crashes on android - shader compiles fine on android too, but when SpriteBatch ApplyShader method is called app is crashing (according to logcat).

Tried defining variable inline like so:

float test = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0f;

But it doesn't change a thing - still works perfectly fine on PC but not on Android. Moreover it crashes even if I write something like:

test = 0.4f;

Upvotes: 2

Views: 751

Answers (1)

Syntactic Fructose
Syntactic Fructose

Reputation: 20124

GLSL ES differs from traditional GLSL in that it requires precision modifiers e.g.

precision highp float;
void main(){
    //....
{

your program works fine on your computer because plain ol' GLSL does not require these precision modifiers. Once you jump into GLSL ES however you're crashing because you aren't using them.

Upvotes: 1

Related Questions