I’m making a CAD software that will create different OpenGL contexts for similar view (if they aren’t showing the same thing).
I would like to share much data as possible between them OpenGL contexts, especially VBOs and shaders.
I want to know what I can share and how I share them, in a cross-platform way and possibly with plain OpenGL 3.2 (no engine).
1
The only things OpenGL contexts can share is objects. And even then, “container” objects cannot be shared. Container objects are objects whose primary purpose is to have other objects attached to them. Specifically, the following are container objects:
- Framebuffer objects
- Vertex array objects
- Transform feedback objects
- Program pipeline objects
All other objects can be shared.
Sharing objects is a context-based task, usually done either as part of the creation of the context or immediately afterwards. However, since this is done on the context itself, it cannot be a cross-platform operation. OpenGL only defines the behavior of the context, not how to manipulate the context object. The platform-specific APIs responsible for creating and managing contexts handle that: GLX, WGL, EGL, etc.
There are generally two ways this gets handled. One way is for the context creation function to take another context as a parameter; the newly created context will share all sharable objects with the given context. wglCreateContextAttribsARB
is the WGL context creation function that takes a context to share with the new one.
The other ways is to use a function immediately after creating the context. This function takes two contexts and shares objects between them. However, you should use such a function immediately after creating the context; you don’t want to create objects in the destination context which might conflict with those already in the source. WGL has an older function, wglShareLists
, that shares objects between contexts. I know it only talks about display lists, but it shares all sharable objects.
I’m making a CAD software that will create different OpenGL contexts for similar view (if they aren’t showing the same thing).
You don’t need different contexts for that. OpenGL contexts are not tied to a specific window or drawable. You can bind a OpenGL context to any window/drawable that is pixel format compatible.
I want to know what I can share and how I share them, in a cross-platform way
In general all objects the hold data (textures, vertex/pixel/element objects, renderbuffer objects) are shared, objects that hold state (vertex array object, framebuffer object) are not.
Sharing is setup in Windows either through calling wglShareLists
to connect a newly created context to previously existing ones. Or by passing a handle to the context to share with to wglCreateContextAttribsARB
; however to obtain a function pointer to that function you first have to create a proxy context (which requires jumping a few hoops, which I’ve implemented in https://git.datenwolf.net/wglarb/ so you can simply call wglarb_CreateContextAttribsARB
without having to care about the rest).
In a X11/GLX environment context sharing has always been set up at context creation time, practically identical to the wglCreateContextAttribsARB
way using the glXCreateContext
2
According to official information, Query objects cannot be shared either:
Most OpenGL objects are sharable, including Sync Objects and GLSL Objects. Container Objects are not sharable, nor are Query Objects.
So to complete the other answers: you cannot share the following objects:
- Framebuffer objects
- Vertex array objects
- Transform feedback objects
- Program pipeline objects
- Query objects
All other OpenGL objects can be shared. You are the most welcome!