Reputation: 30045
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?
Upvotes: 1
Views: 428
Reputation: 162297
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.
Upvotes: 4
Reputation: 1010
I can't understand why you think that copying pointers will make any different ? It does not matter, because of the pointer nature, if you copy them you will get several different pointers pointing to the same function - they won't point to another function thus you can store them ether globally or as a static member in a class. no matter
Upvotes: -1