View Full Version : Sharing textures between contexts

08-13-2014, 02:19 AM
Is there a way to share textures between several contexts? I want to load textures in the background with a "loader" thread while rendering continues in the main rendering thread (rendering without the textures that are not available yet, obviously). Since an OpenGL context cannot be shared by other threads, I tried to create a loader context using the "sharing" feature mentioned in the context creation documentation but this does not seem to work. It doesn't even work in a single-thread design -- textures loaded in one context are not available to the other context. Any working example using either glx or wxwidgets would be most appreciated!


Agent D
08-13-2014, 03:42 AM
AFAIK context sharing is the best/easiest/most portable way to acomplish this and textures should be shareable among contexts.

I used this in an applicaton several years ago, where a resource loading thread loaded (among other things) textures while the main rendering thread kept rendering, but unfortunately I no longer have access to that source code. However, I can confirm that this works (after a bit of tinkering, especially for shaders).

How do you diagnose that it does not work for you? Do you get a gl error or is the texture blank? Could you post a few source code snippets of how you try to do this?

08-13-2014, 05:16 AM
Here's how to do it with GLX:

In main thread Init function :


renderingContext = glXCreateContext( Display*, XVisualInfo*, NULL, True);

workerContext = glXCreateContext( Display*, XVisualInfo*, renderingContext, True );

glXMakeCurrent(Display*, GLXDrawable, renderingContext);

In worker thread init function :

glXMakeCurrent(Display*, GLXDrawable, workerContext);

Finally, don't forget to call glFinish() before using the loaded resources in the main thread

Dark Photon
08-13-2014, 07:18 PM
Also while you're studying up, be sure to read up on the new GL extension:

* {GL|GLX|WGL}_ARB_context_flush_control (http://www.opengl.org/registry/specs/KHR/context_flush_control.txt)

just released. Good to know about this even if you decide you don't need it.

08-19-2014, 03:53 AM
Thanks a lot! Turns out that this is pretty much what I did, my problems where mostly threads-related. Also forgot to call XInitThreads().