C++ – OpenGL – Share existing textures with future contexts


I have an application where I draw frames to a texture using the FBO extensions.

These frames are then be displayed to different windows that are opened while the application is running. This means that textures can be created and pooled before the OpenGL context for any new window is created and wglShareLists is called.

Currently I have to do glReadPixels, glMapBufferRange and glTexSubImage to move the texture into the different window contexts, which is rather inefficient.

Any ideas on how to improve on this? Should I create multiple (as much as the maximum number of windows at any time ~4) OpenGL contexts and hidden windows during program startup? What would be the overhead of that?

Or even better, is there a way to make textures shared between existing and future OpenGL contexts?

Best Solution

Sharing Lists between OpenGL contexts will share all data encapsulating buffer objects as well (textures, vertex buffer objects, pixel buffer objects) but not abstract collection objects (framebuffer objects, vertex array objects). It is mandatory to create the sharing, before creating any new named objects in the new context, as creating names before the sharing may result in namespace collisions. Already created objects are shared. But this of course requires precautions, because what would happen if both context A and B had textures with ID=1? If however A had a lot of textures and B none, then the texture names of A can be transferred to B just fine. That's the caveat.

However if you just want to render to multiple windows: It is perfectly possible to use a single OpenGL context on an arbitrary number of windows, as long as their PIXELFORMATDESCRIPTOR is compatible. That's what the HDC parameter of wglMakeCurrent is meant for. If a context was strictly bound to a single window, wglMakeCurrent had only a HRC parameter.

Related Question