O'Reilly logo

OpenGL Insights by Christophe Riccio, Patrick Cozzi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ARB debug output:
A Helping Hand for
Desperate Developers
Ant
´
onio Ramires Fernandes and Bruno Oliveira
33.1 Introduction
Since the inception of OpenGL, error handling has not been without some contro-
versy, as the only available mechanism to provide feedback was the glGetError
function. For each OpenGL command, the application had to explicitly query for
possible errors, getting in return a single and very broad meaning error identifier for
the latest error.
ARB
debug output [Konttinen 10a], originally proposed by AMD [Kontti-
nen 10b], introduces a new feedback mechanism. It allows developers to define a
callback function that will be invoked by OpenGL to report events back to the appli-
cation. The callback mechanism frees the developer from having to explicitly check
for errors during execution, populating the code with glGetError function calls.
Nevertheless, the extension specification does not force the definition of a callback
function. When no callback is defined, the implementation will keep an internal log
called the message log.
The nature of the reported events is very broad a nd can relate, for instance, to
errors using the API, the usage of deprecated functionality, performance warnings, or
GLSL compiler/linker issues. Event information contains a driver-implementation
dependant message and other data such as its severity, type, and source.
The extension also allows user-defined filtering of the events that are reported by
selecting only the ones of interest. Finally, the application, or any third-party library,
may also generate custom events. In the following sections, we will show how to use
the aforementioned extension and its features. We will also have a peek at the current
implementation approaches.
483
33
484 VI Debugging and Profiling
33.2 Exposing the Extension
As stated in the specification, it is recommended that the extension is available only
in an OpenGL debug context to avoid a potential performance impact; hence, it
may be required to create such a context. Using freeglut as an example, this can be
achieved with glutInitContextFlags(GLUT
DEBUG).Anexampleoftheags
to create an OpenGL 4.1 core debug context u sing WGL and GLX is presented in
Listing 33.1. To check if the extension is available, we can use glGetStringi.
int attribs [] =
{
#ifdef WIN32
WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
WGL_CONTEXT_PROFILE_MASK, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
#endif
#ifdef __linux__
GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
GLX_CONTEXT_MINOR_VERSION_ARB, 1,
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
GLX_CONTEXT_PROFILE_MASK, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
#endif
0
};
Listing 33.1. Flags to create a core debug context in WGL and GLX.
33.3 Using a Callback Function
The extension allows the definition of a callback function that will be invoked each
time an event is issued. This directs the flow of generated events to the callback, and
they will not be stored in the message log.
If working with multiple contexts, each one should have its own callback func-
tion. Multithreading applications are allowed to use the same callback for multiple
threads, and the application is fully responsible for ensuring thread safety.
Listing 33.2 is an example of such a callback function; it prints the event passed
by OpenGL in a human-readable form. The enumerations are defined in the exten-
sions spec [Konttinen 10a]. The getStringFor* functions translates the enumer-
ation value into a human readable format. The complete code can be found on the
books web site (www.openglinsights.com).
The function glDebugMessageCallbackARB,used to specify the callback func-
tion, takes two arguments: the name of the callback function and a pointer to the
user data. The pointer to the user data can only be changed with a new call to
glDebugMessageCallbackARB. This pointer allows the callback to receive user-

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required