How can I debug OpenGL errors?


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

GLES20.glUseProgram(mProgram);
checkGlError("glUseProgram");

GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
            if (linkStatus[0] != GLES20.GL_TRUE) {
                Log.e(TAG, "Could not link program: ");
                Log.e(TAG, GLES20.glGetProgramInfoLog(program));
                GLES20.glDeleteProgram(program);
                program = 0;
            }

In this code the variable "program" refers to the shader program object


Log.d(TAG,"fragment shader created");
        int program = GLES20.glCreateProgram();
        if (program != 0) {
           Log.d(TAG,"program created");
            GLES20.glAttachShader(program, vertexShader);
            checkGlError("glAttachShader");
            GLES20.glAttachShader(program, pixelShader);
            checkGlError("glAttachShader");
            GLES20.glLinkProgram(program);
            int[] linkStatus = new int[1];
            GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
            if (linkStatus[0] != GLES20.GL_TRUE) {
                Log.e(TAG, "Could not link program: ");
                Log.e(TAG, GLES20.glGetProgramInfoLog(program));
                GLES20.glDeleteProgram(program);
                program = 0;
            }

private int loadShader(int shaderType, String source) {
        int shader = GLES20.glCreateShader(shaderType);
        if (shader != 0) {
            GLES20.glShaderSource(shader, source);
            GLES20.glCompileShader(shader);
            int[] compiled = new int[1];
            GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
            if (compiled[0] == 0) {
                Log.e(TAG, "Could not compile shader " + shaderType + ":");
                Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
                GLES20.glDeleteShader(shader);
                shader = 0;
            }
        }
        return shader;
    }

glGetProgramInfoLog API

glGetProgramInfoLog returns the information log for the specified program object. The information log for a program object is modified when the program object is linked or validated. You should use this call if the last link operation fails.


GLES20.glLinkProgram(program);
    int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] == GLES20.GL_TRUE) {
        return program;
    }

glGetProgamiv(program, query_param, intarray)


GL_DELETE_STATUS, 
GL_LINK_STATUS, 
GL_VALIDATE_STATUS, 
GL_INFO_LOG_LENGTH, 
GL_ATTACHED_SHADERS, 
GL_ACTIVE_ATTRIBUTES, 
GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, 
GL_ACTIVE_UNIFORMS, 
GL_ACTIVE_UNIFORM_MAX_LENGTH

GL_TRUE: good
GL_FALSE: bad

very similar to glGetProgramInfoLog, bur is typically issued after compiling a shader source.


//compile a shader
//check status: glGetShaderiv
//if status is bad call glGetShaderInfoLog
//compile another shader...

//attach shader
//use glError

//link shader
//check status: glGetProgramiv
//if status is bad glGetProgramInfoLog

glGetShaderiv: status of compile
glGetShaderInfoLog: what happened

glGetProgramiv: status of link
glGetProgramLogInfo: what happened

glError API docs

This method just returns an int value indicating an error. But we are advised to call this method in a loop until it returns GL_NO_ERROR.

Why?

apparently there could be more than one error flag to allow for distributed implementations. when a glGetError is called the current value of one of the flags is returned and it is set to GL_NO_ERROR. So you have to call until it returns a GL_NO_ERROR.


GL_NO_ERROR
GL_INVALID_ENUM
GL_INVALID_VALUE
GL_INVALID_OPERATION (Bad state)
GL_INVALID_FRAMEBUFFER_OPERATION
GL_OUT_OF_MEMORY

GL_SHADER_TYPE
GL_COMPILE_STATUS
GL_DELETE_STATUS
GL_INFO_LOG_LENGTH
GL_SHADER_SOURCE_LENGTH