Fast OpenGL rendering with threads


I've developed an application using C++ and OpenGL, all the rendering for the application is done using OpenGL.  All works fine, but now I need some of the rendering to happen a little quicker.  The plan was to create another thread which would just keep rendering the specific part which I believe would work, but nothing gets rendered.  After some in-depth research I found that the reason that nothing gets rendered is because the OpenGL commands only work if they are called in the main thread.  Is there anyway around this?

Many thanks in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think you should try frustum culling check it out on

or This example

But its a start too get faster render results

this also might be interresting theory
Brent-CampbellAuthor Commented:
Unfortunately that's not quite what I'm after, I need to know how to render OpenGL commands in other threads that I create or an alternative.
I seem to recall you need to create the opengl context from within the thread that will be making the low level GL calls on that context.  I could be mistaken, as I usually thread OTHER things, and leave the GL rendering in the primary thread of the process (it's just easier that way).  So you do computational stuff in secondary thread(s), and mutex/sync with the primary thread so it can render updated stuff without accessing data in the midst of being recalc'd.

However, I've spent many years with OpenGL, and very rarely does threading make a big difference.  If you could explain in a bit more detail what you are doing, and what you think the threading will implement/help, it might be there are better approaches...

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Brent-CampbellAuthor Commented:
I read something about the opengl context elsewhere, do you know how I can go about creating it within a thread?

I'm actually using threading for other parts of the application but those threads need to render some data as well.  The problem I'm facing is that if I create a thread to do something which then makes a call to a function that calls gl commands they don't get rendered, and even if I pass in a pointer to the main thread and call functions within that I have the same problem.  There must be simple way around this.

Well, I take it back -- you MIGHT be able to have multiple threads accessing one context... HOWEVER, you need to SET the context to be the current rendering context within each thread that will be rendering.  I'm not sure if you need to do a 'make current' call every time you enter a given thread, or just upon initialization (quick experiment would prove it either way).

Also, you must be using very strict mutexes between the threads.  No two threads should be working within OpenGL at the same time, as most OpenGL implementations are not thread-safe.

So, you should be able to pass your GDC and context pointers into your threads, cache them, and when you enter a thread to do some rendering, make the context current, and then render.  Again, you might only need it once, but it kinda makes sense that make current would 'assign' the context to the given thread, and thus if multiple threads are rendering each would need to 'take over' the context for the period of rendering...


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brent - any further progress?  Here to help if you hit any other roadblocks.  GL in multiple threads can be REALLY tricky... ;)
Brent-CampbellAuthor Commented:
After a lot of discussion and research your right, you have to set the context to be the current rendering context within each thread that will be rendering, which isn't worth it due to the hastle of synchronising.  I wonder if DirectX has this problem too?


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.