Multiwindow app using OpenGL?

Anyone got a multi-window app running using OpenGL in the child windows as well as the main window?

My understanding is a separate OpenGLContext is required for each OS window.


It’s certainly doable! I just hacked an example into my existing app in progress and it appears to work just fine. I should forewarn that I only tested on Windows though.

Your understanding is spot on: you need a GL context per window handle.


Thanks for checking it out!

Thanks, this answers my first question. But following those questions:

  • Do I understand correctly, that those contexts will run each on its own thread and still be thread safe?

  • Couldn’t the second renderer switch the other context active creating a mess?

  • If I want to create OpenGLTextures on a background thread (reading a video), do I have to lock against the rendering thread?

  • Can I share the texture in both contexts? I want to show the same video on two screens at the same time.

I did a little homework, so

  • using one context per thread is safe according to the openGL wiki at, there is no problem using the two windows each having its own thread and context.

And there exists a shared state, I need to learn how to use that. There seems to be a safe way

1 Like

So long as you’re sticking to the thread the context was created on while using it (and cleaning up) you’re in the clear.

Something important to bear in mind: creating more than 1 context is implementation specific.

Depending on your goal (releasing an app for people to use?) I suggest doing heavy testing against multiple platforms and hardware configurations/drivers (Intel vs Nvidia vs GPU, old and new) to be sure that your software works consistently… And, well, you might be in a bind on mobile.

It could create a mess, or at least make your designs messy…

I’m not sure what kind of decoding context you’re working in… Assuming native: native video decoding already pulls from its own thread. The OS should be offloading and syncing all of this for you under the hood (especially so if it has to decrypt and manage DRM).

Been a while since I’ve had to do this… iirc, you have to copy the texture regardless of multi-context. I don’t remember there being a cross-platform (high level GL API) way of sharing this kind of data.

Something of note - the thing about creating multiple contexts is that you’re at the mercy of the hardware and driver; even though you want to do things in parallel, the GPU still has to synchronise threads. You might be gaining a lot in a multi-GPU situation though…

Quick edit:

Finally I am back to the project and had success to display the textures from one context.
But one use case is to display the same video on two different displays, so I will need to somehow share the textures.

I tried to create an OpenGLContext for upload and let the rendering components share that context using

openGLContext.setNativeSharedContext (engine.textureContext.getRawContext());

But now I realize that the OpenGL has no nativeContext unless it is attached to a component.

Is there a way around that?

EDIT: I thought creating a juce::OpenGLContext::NativeContext might be sufficient, but it seems not accessible outside OpenGLContext as far as I can see