484 VI Debugging and Proﬁling
33.2 Exposing the Extension
As stated in the speciﬁcation, 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
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  =
Listing 33.1. Flags to create a core debug context in WGL and GLX.
33.3 Using a Callback Function
The extension allows the deﬁnition of a callback function that will be invoked each
time an event is issued. This directs the ﬂow 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 deﬁned 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
book’s 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-