OpenGL: Working around inability to share VAOs

Started by
4 comments, last by Osbios 7 years ago

I'm building a level (map) design tool using Qt, and my user interface will contain multiple different OpenGL-driven viewports. In Qt, each different viewport is created with its own context, and I currently have the application-wide setting enabled so that all contexts share resources with each other. I'm currently targeting OpenGL 4.1 core.

My current implementation uses one "render model" per map - the map's actual geometry (the "model", with planes and texture projection parameters and all that stuff) is converted when required to OpenGL-specific geometry data, which is stored and batched in the render model for that map. Each render model, for better or worse, uses a single VAO - I figured having one VAO per batch would equate to an awful lot of objects.

However, I've just learned today that VAOs cannot be shared between contexts. It's my suspicion that this is what's causing OpenGL to return INVALID_OPERATION when attempting to bind a VAO, or do any other drawing whatsoever, now that I've refactored my code to begin using multiple viewports. Helpfully, Mac doesn't give any errors at all, and just renders in incredibly confusing and undefined ways.

In theory I'd like for any viewport to be considered a rendering destination, so that a map's render model is the one location that stores all the geometry required, and can be drawn onto any renderable surface. If VAOs cannot be shared, I don't think I'll be able to store one per render model.

I have the following questions:

  • Should I be using one VAO per viewport, created when the viewport's context is initialised? Will this obstruct the population process of the render model (ie. does that have to be done in the same context)? Will the data still be shareable with other contexts/VAOs?
  • Alternatively, should I be using one VAO per batch? I'd imagine this would definitely tie the creation of the batch to one of the viewports' contexts, and I'm not sure how I'd manage that.
Advertisement

Personally, I would retool everything to render in one context to four FBOs. Then have the four contexts copy the contents of those FBOs to their respective viewports. Seems like that would be a much simpler approach. I guess that won't work if you're running each context in a separate thread... if that's possible.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

OpenGL is fundamently anti threading on the client side. Sure it makes sense to use threading for a few things like big data transfers from or to the client side or shader compilation (thats really more about sync objects, and having a seperate context for the independent sync signal).

I tried to make usable threaded interface for OpenGL but there are just to many non shared objects (Even bindless texture handler states ARG! :[ ) and also the stupid behavior that you can't create sync objects without at the same time inserting them into the work queue.

I would just make one render thread. And use threaded helper functions for the few casses where it makes sense.

Also VAOs are slower except for MESA based drivers. Using old style binding and if available GL_ARB_vertex_attrib_binding and GL_ARB_multi_bind will be faster on AMD and Nvidia blob drivers. In that case you don't even use non-shared objects.

You don't need to create multiple contexts.

You can have multiple HDC associated with the same HGLRC (GL context).
When rendering to each window, you need to do:


wglMakeCurrent( hdcWindow0, context0 );
// ... Draw ...
wglMakeCurrent( hdcWindow1, context0 );
// ... Draw ...
wglMakeCurrent( hdcWindow2, context0 );
// ... Draw ...
wglMakeCurrent( hdcWindow3, context0 );

The only caveats are:

  • All windows must have the same pixel formats (i.e. 32bpp/16bpp, MSAA settings)
  • Only use VSync on one of the windows (i.e. the last one), otherwise 4 windows with VSync in all of them will get you 15 fps (assuming a 60hz monitor).

This is far more stable and easier to work with than the clusterf*** that is working with shared contexts.

Thanks for the prompt replies! Just for clarity, I'm not actually using different threads, just different contexts created by each of the Qt viewports. I also have one explcitly-created "background" context, which the application uses to perform tasks when viewports aren't currently active. This is to allow OpenGL-related operations to occur if there aren't actually viewports available at the time - if the user hasn't opened a viewport yet, I might still want to create VBOs and the like within the render models, or import textures and other assets.

First of all I think it would be useful for me to go and re-read the Qt OpenGL documentation, to see whether the contexts are actually physically different, or whether a single context is just shared across widgets in the way that Mathias mentioned. All I know at the moment is that a QOpenGLWidget manages its own context, and makes it current before my drawing code is called. Alternatively, Promit's method could potentially be both feasible and scalable, as I'd like to be able (in theory, performance permitting) to allow the user to create as many viewports as they wish.

Also your render thread with the gl context does not have to be a windows even thread.

I'm not actually using different threads, just different contexts

Thats actually even worse then multiple threads each with its own context.

EDIT: I read the QT docs and they use a full blown context. So I would strongly advice to not use that!

This topic is closed to new replies.

Advertisement