Getting OpenGL & WGL extensions on Windows

References

When linking normal C libraries, your code declares the signature of the functions and variables you expect to dynamically link (usually by #including a header file), and the operating system's dynamic linker links those objects into your program at runtime. OpenGL is rather different. Microsoft's Windows SDK includes implementations of included OpenGL 1.1, GLU 1.2, and base WGL symbols. Therefore, you can #include the relevant headers, dynamically link to the relevant libraries, and expect to get linked to those symbols. For any symbols exported by later versions of GL and GLU, and WGL extensions, you must query and retrieve them manually at runtime. Note that you can also use this type of mechanism to manually link symbols from other libraries, rather than creating a dynamic executable.

I'm not going to bother with GLU 1.3 or extensions, because you'll probably never need to use them.

We already had an example of this in the "Specifying your OpenGL context version" tutorial with wglCreateContextAttribsARB.

Build with:

gcc main.c -lgdi32 -lopengl32
bool GLHasExtensionPre30 (const char *const extension) { const char *gl_extensions = (const char*)glGetString (GL_EXTENSIONS); const auto len = strlen (extension); for (;;) { gl_extensions = strstr (gl_extensions, extension); if (gl_extensions == NULL) return false; char next = *(gl_extensions + len); if (next == ' ' || next == '\0') return true; gl_extensions += len; } return false; } bool GLHasExtension30Plus (const char *const extension) { typedef const GLubyte* (*glGetStringi_t) (GLenum name, GLuint index); const auto glGetStringi = (glGetStringi_t)wglGetProcAddress ((const GLubyte*)"glGetStringi"); if (glGetStringi == NULL) { puts ("Failed to retrieve glGetStringi function pointer"); return false; } GLint extension_count; glGetIntegerv (GL_NUM_EXTENSIONS, &extension_count); for (int i = 0; i < extension_count; ++i) { const auto e = glGetStringi (GL_EXTENSIONS, i); if (strcmp ((const char*)e, extension) == 0) return true; } return false; } bool (*GLHasExtension) (const char *const extension) = GLHasExtension30Plus; if (glversion.major < 3) { GLHasExtension = GLHasExtensionPre30; puts ("Using legacy glGetString for extensions"); }

Before OpenGL 3.0, the only way to get the list of available extensions was using glGetString to retrieve a space-separated list of all available extensions in the context. This mechanism was deprecated in GL 3.0, and won't work in forward-compatible contexts. Therefore, if in a 3.0+ context, it's best to use the new glGetStringi method which provides each extension as its own string. This method also makes it easier to check for an exact string match. I've used a function pointer to provide a transparent interface to these two functions, which is assigned to the older version only if the context is pre-3.0.

while (glversion.full >= (glversion_t){.major = 2, .minor = 0}.full && glversion_compatibility) { typedef GLuint (*glCreateShader_t) (GLenum shaderType); const auto glCreateShader = (glCreateShader_t)wglGetProcAddress ((const GLubyte*)"glCreateShader"); if (glCreateShader == NULL) { puts ("Failed to retrieve glCreateShader function pointer"); break; } const auto vertex_shader = glCreateShader (GL_VERTEX_SHADER); printf ("glCreateShader found and called: %u\n", vertex_shader); break;}

glCreateShader was introduced in OpenGL 2.0, so we check the context version against that. We create a type for the function pointer. The function signature can be copied from the OpenGL specification. Next, we use wglGetProcAddress to request the symbol by name, casting the pointer to the correct type. If the symbol exists, we'll have its address. If not, wglGetProcAddress returns NULL. After retrieving the function pointer and verifying we got it, we call it just to demonstrate that it works.

When retrieving a function that is part of a specific OpenGL version specification, you can just check that your context is a compatible version and then retrieve the function pointer. Given this, if you know all of your GL functions are from version 2.1 and earlier, for example, you can do a single check for the version, then retrieve all the functions pointers.

if (GLHasExtension ("GL_ARB_vertex_buffer_object")) { do { typedef void (*glGenBuffersARB_t) (GLsizei n, GLuint *buffers); const auto glGenBuffersARB = (glGenBuffersARB_t)wglGetProcAddress ((const GLubyte*)"glGenBuffersARB"); if (glGenBuffersARB == NULL) { puts ("Failed to retrieve glGenBuffersARB function pointer"); break; } GLuint buffer = 0; glGenBuffersARB (1, &buffer); printf ("glGenBuffersARB found and called: %u\n", buffer); } while (false); }

Here we check for the extension, GL_ARB_vertex_buffer_object, then get the function pointer for glGenBuffersARB. Pretty much the same as for glCreateShader, except it's an extension function, not an OpenGL core specification function.

if (WGLHasExtension ("WGL_EXT_swap_control_tear")) { do { typedef BOOL (*wglSwapIntervalEXT_t) (int interval); const auto wglSwapIntervalEXT = (wglSwapIntervalEXT_t)wglGetProcAddress ((const GLubyte*)"wglSwapIntervalEXT"); if (wglSwapIntervalEXT == NULL) { puts ("Failed to retrieve wglSwapIntervalEXT function pointer"); break; } wglSwapIntervalEXT (-1); puts ("wglSwapIntervalEXT found and called"); } while (false); }

Note that we call WGLHasExtension. Other than that, this is practically the same as the prior code.

You may wonder: why check for the version/extension at all? Why not just attempt to retrieve the function pointer without checking those things? In many cases that will work, but wglSwapIntervalEXT is a good example of why it is sometimes necessary to check for the relevant extension first. You see, wglSwapIntervalEXT is included in two different extensions: WGL_EXT_swap_control, and WGL_EXT_swap_control_tear. If the _tear extension is present, you can call wglSwapIntervalEXT with a -1 interval value in order to allow tearing when the framerate dips below the vertical sync rate. But when only the non-tear extension is present, the function fails when called with a negative value.

The above code works for retrieving any GL and WGL functions not included in the Windows SDK. In my game engine, I use fewer than 20 such functions, so I just created a simple macro and called it once for each function. If you're using more functions than that, or want to retrieve everything up to a particular version of OpenGL, you might use a library such as GLEW or Glad, or you could download the OpenGL API registry XML and WGL API registry XML, and write a program to parse them and generate your own code for loading extensions. This is what I plan to do whenever I use more OpenGL in my next engine.