How to use/create a single persistent OpenGLContext?


Hi Everyone,

Currently I’m using JUCE in combination with OpenCL (if anyone needs some info on that, please let me know, it was surprisingly easy to setup once you know how). For this I currently create an OpenGL context, which I share with OpenCL so they can share buffers etc. This works fine as I’m currently working within one component, but I now actually need to call the OpenCL context throughout the entire program.

For this, I need a persistent OpenGL context throughout the lifetime of the program (because my OpenCL context is built on top of that one). How do I go about this? OpenGLContext and OpenGLRenderer slightly confuse me tbh. Whenever I inherit OpenGLContext, I can’t start creating buffers in the constructor. I need to attach an OpenGLRenderer and create the buffers in newOpenGLContextCreated(). Now when I attach the OpenGLRenderer to a different component, newOpenGLContextCreated() is called again. So it will recreate the OpenGLContext, making all old buffers invalid, right?

In this thread, Jules mentions that the plan was to have a single OpenGLContext per window. Is this indeed (still) the case?

Many thanks in advance!


I just quickly scanned over your post, as a side not I’m happy to read other people doing OpenCL stuff, I’m currently working on OpenCL stuff too and will start building JUCE applications but not to combine it with OpenCL but to use it for DSP.

Now to your questions, as I said, I did not do any work that connected OpenCL and OpenGL, but a single OpenGL context, suitable to render multiple Components was discussed here recently, mostly because of performance issues. You might want to have a read on this thread, im not sure but there might be a chance that it could help you :wink:

I remember you having trouble back in the last year with that. I think it might be great if you would share how you did it :wink:


Thank you very much for your answer! Good to hear indeed that other people are also using OpenCL.

Haha, my past always catches up to me ;p. I’ll do a short write-up when I figured out this problem and the right publishing method (I’ve been putting of a code blog for a while now, otherwise it might just be a forum post). Shoot me a PM if you have any questions before that.

Regarding the issue, the proposed solution looks interesting. I just think it is a bit complicated for what essentially means keeping the OpenGLContext and it’s related resources alive, while switching the Component to which it is rendering. I’ll have a better look at it tomorrow, but is there really no easier way to do this? Perhaps extending OpenGLContext::attachTo (Component& ), to something like OpenGLContext::attachTo (Component&, bool keepExistingContext), or am I overseeing something here?

Also, I’m guessing the solution does not work in case multiple windows are involved?


I’d have to go back through the code, but I setup an offscreen OpenGL context, and share any new contexts to that. It works with GL shared textures - presume OpenCL would be fine too.



Hmm, yes I currently have a somewhat similar setup (also based on the thread mentioned by PluginPenguin), but how do you get create an offscreen OpenGL-context? I currently have mine attached to my MainComponent, as the OpenGL-renderer functions simply don’t get called as long as it’s not attached to a component that is also visible.


You set up a pbuffer to render too. But do you need to render? I just have it as a stable share context and do the work in other contexts.

It’s different on each platform, but you need to use the standard platform utilities to make an offscreen buffer. I think it may link to link to certain ‘screens’ to make sure it’s on the same GPU as your work will be.