jcoder jcoder - 3 months ago 25
C++ Question

Can I make opengl extension pointers global?

Using opengl/windows 10/visual c++ 2015

I have a 'Window' class in my program which creates and maintains a win32 screen window including making a 'modern' opengl context on it. As part of doing this it obtains pointers to lots of opengl 'extension' functions.

My question is can I store those extension pointers globally/class static or do I need to keep a copy for every instance or Window as they'll have different opengl contexts? Are the pointers valid and the same for every opengl context I create or do I need to do it over and over again for every window I create?

Answer

Are the pointers valid and the same for every opengl context I create or do I need to do it over and over again for every window I create?

That depends on the operating system and the windowing system used. The WGL specification says, that OpenGL extension function pointers are permitted to be different for each OpenGL context:

wglGetProcAddress():

...

Remarks

The OpenGL library supports multiple implementations of its functions. Extension functions supported in one rendering context are not necessarily available in a separate rendering context. Thus, for a given rendering context in an application, use the function addresses returned by the wglGetProcAddress function only.

In contrast to that X11/GLX specifies (page 35, section 3.3.12) that OpenGL extension function pointers are invariant (i.e. don't change) regardless of the context:

GL function pointers returned by glXGetProcAddress are independent of the currently bound context and may be used by any context which supports the extension.

In case of Windows the most viable approach would be to have a map between context handle and a structure holding all the function pointers, i.e.

struct glfunctions {
    /* all the function pointers */
};
std::map<HGLRC,glfunctions> context_glfunctions;

Feel free to add some form of LRU to the map, so that repeated lookups of the context don't have to go through the whole map.

Comments