How to share OpenGL context with OpenCL?



Currently I’m trying to create some simulations within JUCE and I’m looking to improve performance using OpenCL. For this I need context sharing (in order to share buffers and prevent unnecessary copying), but how would I go about this? I already have OpenGL rendering to a component using OpenGLRenderer and an OpenGLContext, and I somehow need to pass the OpenGLContext to OpenCL. I’m on MacOS atm.

I’ve tried calling getRawContext() and casting this to NSOpenGLContext (which is what is used on MacOS internally, correct?), but I get an unknown type name-error and I don’t know what to include to solve this.

After this I plan to get a CGLContextObj from that, which I can hopefully pass to OpenCL. Does this make any sense?

Thanks in advance!


Anyone? It is kinda becoming a blocking issue atm and any help is appreciated…


Ok, I still have the same problem, is there no one who can lend me a helping hand? It seems quite straightforward to get the (if I understand correctly, OS-based) OpenGL context from the JUCE-based OpenGLContext (which is some sort of wrapper then). OpenGLContext even has a function getRawContext(), so I’m assuming I have to use that.

I just don’t really know what to cast the returning void* to, and what headers to import to actually use that. Is there anyone that does?

Many thanks in advance!


I am currently working on a project using OpenGL as well as starting a project relying on OpenCL acceleration in the next month. Those two building blocks will be put together in future and it’s likely that such kind of interoperability between GL and CL could help me at that point, this is why I had a quick read on that topic some time ago.

That being said I have absolutely no practical experience in implementing any feature like this, however I came across this document back then, maybe it will help you

This document has the section Creating the Interoperability-Capable OpenCL Context where they basically query all available CL devices that allow GL context sharing and create a context for the device matching the current device used in the GL context if I got it right.

Don’t know if I got it right, but I thought that this could maybe at least give you a starting point…

But: If you find out how to get started I would be very exited to learn what basic steps you did!


Thanks for your reply, if I’ll ever figure this out I’ll let you know what I did.

But I know about the OpenCL and OpenGL interoperability and have read similar articles, but the problem I’m currently having is that all I have is a “JUCE OpenGLContext”, while I want to distill a “MacOS OpenGLContext” from that. Because OpenCL will of course only accept such a context, as it has no notion of JUCE. I just don’t know how to do that.


Alright, seems like you might have done more research then I have so far :wink:

Just to follow you better, to which CL function would you pass this context?

It might be completely possible that I overlooked something or got it completely wrong, but as I understood it from the document linked above I got the impression that it is enough to create a regular CL context to make all this work and just make sure it uses the same device as your GL context uses, without the need for a function to pass whichever GL context object to?


In the examples I’ve found I see similar code snippets (MacOS specific, but I guess for other OS’es it would go similar):

CGLContextObj glContext = CGLGetCurrentContext();
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);

cl_context_properties props[] =
contextZ = clCreateContext( props, 1, &dev_idZ[0], clLogMessagesToStdoutAPPLE, NULL, &err );

So you pass the wanted GLContext in with the cl_context_properties. I think just picking the same device doesn’t work, because there can be multiple OpenGL contexts running on one device (IIRC).

I see that JUCE internally uses NSOpenGLContext, which in turn has a CGLContextObj (which I would need in the above snippet), which can be accessed with cglContextObject().