What is shareable between OpenGL contexts and how to enable sharing
Asked Answered
M

3

12

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).

Mraz answered 27/4, 2019 at 21:47 Comment(1)
Unless you use some library (glfw, etc.) it is not possible to create a shared context in a platform independent way since context creation itself is platform specific.Walliw
K
6

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

Keffer answered 28/4, 2019 at 2:17 Comment(2)
"In general all objects the hold data (textures, vertex/pixel/element/render buffer objects) are shared" ... that's not how it works. There are plenty of state-only objects that get shared: sampler objects, query objects, sync objects, etc. It's container objects (objects that get other objects attached to them) that are not shared. Also there is no such thing as a "render buffer object"; there are "renderbuffer object", which is fundamentally distinct from a "vertex/pixel/element" buffer object.Ventriloquism
@NicolBolas: I figured OP was a new to OpenGL and I think it's better to save the finer semantics for later. IMHO it's a good enough coarse, broad strokes picture to get the idea across. For anything else the only sensible thing you can do anyway is to refer to the specification (or copy the spec out here).Keffer
V
16

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.

Ventriloquism answered 28/4, 2019 at 2:25 Comment(0)
K
6

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

Keffer answered 28/4, 2019 at 2:17 Comment(2)
"In general all objects the hold data (textures, vertex/pixel/element/render buffer objects) are shared" ... that's not how it works. There are plenty of state-only objects that get shared: sampler objects, query objects, sync objects, etc. It's container objects (objects that get other objects attached to them) that are not shared. Also there is no such thing as a "render buffer object"; there are "renderbuffer object", which is fundamentally distinct from a "vertex/pixel/element" buffer object.Ventriloquism
@NicolBolas: I figured OP was a new to OpenGL and I think it's better to save the finer semantics for later. IMHO it's a good enough coarse, broad strokes picture to get the idea across. For anything else the only sensible thing you can do anyway is to refer to the specification (or copy the spec out here).Keffer
C
0

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!

Chemurgy answered 25/4 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.