Help end child hunger

GLSL Core Tutorial – Troubleshooting: the Infolog

Prev: Setup Example Next: Cleaning Up
 

Debugging a shader is hard. There is no printf yet and probably never will be, although developer tools with debugging capability are to be expected in the future. It is true that you can use some tricks but these are not trivial by any means. In this section some functions are presented to check if your code compiled and linked successfully.

Both, the compiler and linker, output some messages when errors are found, as well as some warnings that help us to fix things. The nature of these messages is very similar to C compilers and linkers.

The status of the compile steps can be queried with the following function:

void glGetShaderiv(GLuint object, GLenum type, int *param);

Parameters:

  • object – the handler to the object. Either a shader or a program;
  • type – GL_COMPILE_STATUS;
  • param – the return value, GL_TRUE if OK, GL_FALSE otherwise.

The status of the link step can be queried with the following function:

void glGetProgramiv(GLuint object, GLenum type, int *param);

Parameters:

  • object – the handler to the object, a program if we’re looking for linking info;
  • type – GL_LINK_STATUS;
  • param – the return value, GL_TRUE if OK, GL_FALSE otherwise.

When GL_FALSE is obtained in any of the above functions it is possible to get further information with the InfoLog. This log stores information about the last operation performed per object, shaders and programs, such as warnings and errors in the compilation, and problems during the link step. The log can even tell you if your shaders will run in software, meaning your hardware does not support some feature you’re using, or hardware, the ideal situation. There is no specification for the InfoLog messages, so different drivers/hardware may produce different logs.

In order to get the InfoLog for a particular shader or program we can use the following functions:

void glGetShaderInfoLog(GLuint object, int maxLen, int *len, char *log);
void glGetProgramInfoLog(GLuint object, int maxLen, int *len, char *log);

Parameters:

  • object – the handler to the object. Either a shader or a program;
  • maxLen – The maximum number of chars to retrieve from the InfoLog;
  • len – returns the actual length of the retrieved InfoLog;
  • log – The log itself.

Note that you must know the length of the InfoLog to retrieve it. To find this precious bit of information use the following functions (in OpenGL notation):

void glGetShaderiv(GLuint object, GLenum type, int *param);
void glGetProgramiv(GLuint object, GLenum type, int *param);

Parameters:

  • object – the handler to the object. Either a shader or a program;
  • type – GL_INFO_LOG_LENGTH;
  • param – the return value, the length of the InfoLog.

The following functions can be used to retrieve and print the contents of the infoLog:

void printShaderInfoLog(GLuint obj)
{
    int infologLength = 0;
    int charsWritten  = 0;
    char *infoLog;

    glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &infologLength);

    if (infologLength > 0)
    {
        infoLog = (char *)malloc(infologLength);
        glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
	printf("%s\n",infoLog);
        free(infoLog);
    }
}

void printProgramInfoLog(GLuint obj)
{
    int infologLength = 0;
    int charsWritten  = 0;
    char *infoLog;

    glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength);

    if (infologLength > 0)
    {
        infoLog = (char *)malloc(infologLength);
        glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
	printf("%s\n",infoLog);
        free(infoLog);
    }
}

 

Prev: Setup Example Next: Cleaning Up
 

Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2014 Lighthouse3d.com Suffusion theme by Sayontan Sinha