Jump to content
  • Advertisement
Sign in to follow this  
indigox3

gl contexts

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know if its possible to draw to the same gl rendering context from multiple threads? It doesn't really seem like a good idea, but just wondering if its possible...

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by indigox3
Does anyone know if its possible to draw to the same gl rendering context from multiple threads? It doesn't really seem like a good idea, but just wondering if its possible...
It is possible, but you have to set the context to be active in the thread, and a context can only be active in one thread at a time. It's also fairly expensive to do, so in most cases it is not really worth it.

I'm ~95% positive that's all true, but I'm sure someone more knowledgable will be along shortly to correct me if I'm wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalidor
Quote:
Original post by indigox3
Does anyone know if its possible to draw to the same gl rendering context from multiple threads? It doesn't really seem like a good idea, but just wondering if its possible...
It is possible, but you have to set the context to be active in the thread, and a context can only be active in one thread at a time. It's also fairly expensive to do, so in most cases it is not really worth it.

I'm ~95% positive that's all true, but I'm sure someone more knowledgable will be along shortly to correct me if I'm wrong.



That mean that everytime there is a context switch between threads, you would have to call something like wglMakeCurrent() from the thread that starts running right?

Since you don't know when a context switch could occur is this even possible?

Share this post


Link to post
Share on other sites
its infact worse than that [smile]
In order for the context to be successfull set in one thread it must be released from any other thread which has it set as its current context.

Share this post


Link to post
Share on other sites
Some of the previous responces are incorrect. Multiple threads can access the same context, although the user is responsible for sychronizing those accesses.

You only need to call SetCurrentContext() (or whatever form it takes on your platform) once for each thread. From that point, you can make OpenGL calls from both threads, but you must ensure that only one thread is actually in a OpenGL function at any given time (easiest to do by protecting each context with a mutex).

On Mac OS X, CoreGraphics provides a default mutex along with each context, accessed via CGLLockContext and CGLUnlockContext. (Necessary so that the OS X window server doesn't try to access the context from another thread behind your back).

Share this post


Link to post
Share on other sites
Hmmm, I'll be intrested to see where you got that infomation from as it certainly doesnt work on Win32 as I've just tested it.

The context is created in one thread, a new thread is created and the context is activated in that thread and setup and one frame is drawn to, the thread then yeilds and the main thread tries to get the context back again, and so it repeats. This results in an intrestingly black screen [smile]

If I leave it so the context is created in the thread which is doing the drawing it works with no troubles at all.

For the record the MSDN wglMakeCurrent() documentation has this to say;
Quote:

A thread can have one current rendering context. A process can have multiple rendering contexts by means of multithreading. A thread must set a current rendering context before calling any OpenGL functions. Otherwise, all OpenGL calls are ignored.

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


Now, if you can make a rendering context hope from one thread to another without first releasing it feel free to share your code/app [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
its infact worse than that [smile]
In order for the context to be successfull set in one thread it must be released from any other thread which has it set as its current context.


I was wondering about that. How would you release the rendering context? I don't see anything along the lines of wglReleaseContext() I'm guessing that its released when another thread calls wglMakeCurrent() for that context.

Share this post


Link to post
Share on other sites
Quote:
Original post by indigox3
I was wondering about that. How would you release the rendering context? I don't see anything along the lines of wglReleaseContext() I'm guessing that its released when another thread calls wglMakeCurrent() for that context.
Call wglMakeCurrent with the hglrc parameter set to NULL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!