0
votes

I am developing a Breakout-style game on Android using OpenGL ES 2.0 and I've run into a fatal error that only happens on my Android device (version 2.3.4) but doesn't happen on my emulator (Android version 5.0.1). I first thought the bug is with the difference of versions but I'm not entirely convinced.

I had the game working nicely on both emulator and my device, but the problem occurred when I wanted to add a Particle effect shader, and I received a RunTime exception during the compilation of the second shader as seen in the stack trace below.

03-31 15:50:52.339  24359-24371/com.package E/GLSLProgram Class﹕ glAttachShader: glError 1281
03-31 15:50:52.359  24359-24371/com.package W/dalvikvm﹕ threadid=9: thread exiting with uncaught exception (group=0x40018560)
03-31 15:50:52.359  24359-24371/com.package E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 10
    java.lang.RuntimeException: glAttachShader: glError 1281
            at com.package.GLSLProgram.checkGlError(GLSLProgram.java:55)
            at com.package.GLSLProgram.<init>(GLSLProgram.java:38)
            at com.package.ResourceManager.insertShader(ResourceManager.java:103)
            at com.package.GameRenderer.onSurfaceCreated(GameRenderer.java:69)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1349)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1119)
03-31 15:50:52.369  24359-24359/com.package D/GameActivity﹕ in onPause
03-31 15:50:52.729  24359-24359/com.package  E/libEGL﹕ call to OpenGL ES API with no current context (logged once per thread)

I have a method from the Android OpenGL ES 2 tutorial that I call after many OpenGL method calls (notably the ones I don't want to cause problems):

public static void checkGlError(String glOperation)
    {
        int error;
        while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
            Log.e(TAG, glOperation + ": glError " + error);
            throw new RuntimeException(glOperation + ": glError " + error);
        }
    }

I notice the stack trace says the error occurs after shader compilation (glAttachShader). If I comment out the checkGlError function then the program won't crash but the particle effect that I'm trying to create won't be show which means the shader didn't compile. Thus, I assume the issue is with the particle vertex and fragment shaders but those compile and run as expected on the emulator. Here are the particle vertex and fragment shaders:

Vertex:

attribute vec4 aVertex; // vec2 position, vec2 tex coords

varying vec2 vTexCoords;
varying vec4 vParticleColor;

uniform mat4 uParticleProjection;
uniform vec2 uParticleOffset;
uniform vec4 uParticleColor;

void main() {
    float scale = 10.0f;
    vTexCoords = aVertex.zw;
    vParticleColor = uParticleColor;
    gl_Position = uParticleProjection * vec4((aVertex.xy * scale) + uParticleOffset, 0.0, 1.0);
}

Fragment:

precision mediump float;

varying vec2 vTexCoords;
varying vec4 vParticleColor;

uniform sampler2D uParticle;

void main() {
    gl_FragColor = vParticleColor * texture2D(uParticle, vTexCoords);
}

I have the OpenGL ES 2 feature enabled in my manifest. As I mentioned previously the app runs fine on my device until I introduced the Particle shader. The fact that it continues to run on my emulator has me baffled. I figure its something with the version of my device or the shaders but I can't figure it out.

1

1 Answers

0
votes

Is this function exposed in the Java OpenGL API? If so you can get the compile log back from the driver and find out exactly why it doesn't compile.

At a guess though:

float scale = 10.0f; 
// should be ->
float scale = 10.0;

precision mediump float; // This should be in the vertex shader also?

Adding that log function will give you more to work with rather than just guess.