OpenGL shared context.. Simple example..

I guess I am dumb, or missing something…

I want to share an OpenGL context across the threads in a thread pool.
Don’t worry I have all the handshaking sorted out so that they will not conflict…

But try as I might I cannot find a simple example of how to actually share the parent threads context with other threads… Can someone point me to one please.

Thanks.

Just to clarify, either in SDL or AGL, or is it possible to simply query it directly from OpenGL? I just can’t find anything that clearly describes it… :frowning:

I’ve never done this, but I think you need to create multiple rendering contexts one for each thread and use wglShareLists to share resources between them. Then make these rendering contexts current one per thread. Quoting documentation for wglMakeCurrent:

A rendering context can be current to only one thread at a time. You cannot make a rendering context current to multiple threads.

An application can perform multithread drawing by making different rendering contexts current to different threads, supplying each thread with its own rendering context and device context.

I maybe should have put this in the Macintosh area. But at the time of posting I thought it was more likely to be solvable in SDL, or OpenGL itself.

Thanks for the feedback.

What I thought would work and someone else suggested was to use CGLGetContext and CGLSetContext.

That way I was relinquishing and grabbing the context from a global value from the main thread, and controlling all that with mutexs to stop clashes.

Unfortunately that is unstable, and I can’t figure out why…

If anyone else has a way to do this I would be very grateful of suggestions…

Did you read the documentation? When you create any context, you can pass in a pointer to any other context you want to share with. The pixel formats have to be compatible (they must choose the same set of renderers) for sharing to work-- this is easy if you’re creating the pixel formats.

I did… But it was more a problem of ‘asking the right question’.
It took me a while to pin down what I needed.
I was also not sure exactly what SDL did when it created a context.

As it stands now I have a thread pool which is controlled from the main thread, and it simply passes the main OpenGL context to each thread, and the first thing they do is make it their context also. That’s actually all you have to do.

Some mutexs stop thread collisions. But I’ll tidy that up later with a Q system.
Ben threw me over a few suggestions on the OpenGL list.

IMO in Windows you must free the contex in the “old” tread by makeCurrent(NULL) but i do not know this is also for Mac true.
However context switching is not the solution with the most performance … on window. [;)] Don’t know this apply also for macs.

I actually tried that at one point… Everything went bang in a big way, and a couple of people I asked about it laughed and said that’s what they would expect!
So in my experience not good!

As for performance: There are cases to be made both ways. However, in a set of maths heavy parallel threads that do a little bit of OpenGL updating at the end, and on a machine with more than one core it is certainly worth looking at parallel processing IMO.