How can I debug OpenGL errors?

satya - Wed Aug 15 2012 17:43:30 GMT-0400 (Eastern Daylight Time)

You can do this


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);
        }
    }

satya - Wed Aug 15 2012 17:44:28 GMT-0400 (Eastern Daylight Time)

Then you can do


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

satya - Wed Aug 15 2012 17:45:57 GMT-0400 (Eastern Daylight Time)

You can use glGetProgramInfoLog


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

satya - Wed Aug 15 2012 17:46:53 GMT-0400 (Eastern Daylight Time)

Here is the whole segment


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;
            }

satya - Wed Aug 15 2012 17:47:51 GMT-0400 (Eastern Daylight Time)

here is how to use getShaderInfoLog


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;
    }

satya - Fri Sep 14 2012 19:34:05 GMT-0400 (Eastern Daylight Time)

glGetProgramInfoLog API

glGetProgramInfoLog API

satya - Fri Sep 14 2012 19:45:27 GMT-0400 (Eastern Daylight Time)

So what does it do?

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.

satya - Fri Sep 14 2012 19:46:03 GMT-0400 (Eastern Daylight Time)

Here is an example


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;
    }

satya - Fri Sep 14 2012 19:48:42 GMT-0400 (Eastern Daylight Time)

glGetProgamiv(program, query_param, intarray)

glGetProgamiv(program, query_param, intarray)

satya - Fri Sep 14 2012 19:49:30 GMT-0400 (Eastern Daylight Time)

Returns the values of one of these


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

satya - Fri Sep 14 2012 19:51:02 GMT-0400 (Eastern Daylight Time)

Communicated status may return a boolean status or quantity


GL_TRUE: good
GL_FALSE: bad

satya - Fri Sep 14 2012 19:58:08 GMT-0400 (Eastern Daylight Time)

glGetShaderInfoLog

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

satya - Fri Sep 14 2012 19:59:13 GMT-0400 (Eastern Daylight Time)

So for compiles


glGetShaderiv: status of compile
glGetShaderInfoLog: what happened

satya - Fri Sep 14 2012 20:00:32 GMT-0400 (Eastern Daylight Time)

For linking


glGetProgramiv: status of link
glGetProgramLogInfo: what happened

satya - Fri Sep 14 2012 20:15:30 GMT-0400 (Eastern Daylight Time)

glError API docs

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.

satya - Fri Sep 14 2012 20:17:21 GMT-0400 (Eastern Daylight Time)

Other values are


GL_NO_ERROR
GL_INVALID_ENUM
GL_INVALID_VALUE
GL_INVALID_OPERATION (Bad state)
GL_INVALID_FRAMEBUFFER_OPERATION
GL_OUT_OF_MEMORY

satya - Fri Sep 14 2012 21:17:43 GMT-0400 (Eastern Daylight Time)

So what are the queries accepted by glGetShaderiv?


GL_SHADER_TYPE
GL_COMPILE_STATUS
GL_DELETE_STATUS
GL_INFO_LOG_LENGTH
GL_SHADER_SOURCE_LENGTH