Jump to content
  • Advertisement
Sign in to follow this  
blueshogun96

OpenGL OpenGL multi-threading problems.

This topic is 2790 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

Right now, I'm dealing with a fairly complex threading model for my application and I can't get OpenGL to work across multiple threads to save my life. Before I go any further, I should take a moment to explain how my threading structure works and why it's absolutely necessary.

My applocation requires 3 threads which are executed from a .dll and not the .exe file itself. Thread #1 is started when the .dll is loaded and handles all of the Win32 event handling as well as window creation within it. Then there are two more threads that have to be running and they are created within thread #1. The first thread does not require OpenGL to be initialized, but the remainig two threads do. Thread #2 handles rendering and drawing while thread #3 handles updates to the screen (calls SwapBuffers when necessary).

Now, before I go any further, I'd better explain why this is necessary to begin with. A normal game does not require this type of functionality and I wouldn't recommend it either, but what I'm doing is writing an x86 VM (Virtual Machine) that uses OpenGL for graphics rendering. The method I'm using for this VM is direct code execution and some basic MMU hacking for the hardware read/writes and privileged code. How I do that is beyond the scope of this thread, but I should still explain why the other threads are necessary. Thread #2 in detail executes the code, catches exceptions and emulates them manually and if gfx hardware triggers rendering, do that. Thread #3 acts as my VBlank emulation routine. When VBlank is done, then refresh the screen. I honestly couldn't think of a better way to organize the whole thing, and it doesn't seem like it's going to be any simpler than this.

So, my question is, how do I get OpenGL working in the two threads that were spawned from another thread? The context switching code appears to work fine in other situation, but on this one, it never works no matter what I do. I really hope I won't be forced to use Direct3D instead. This sucks. Any ideas? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
How do you do it now, and what exactly isn't working?
Does the window stay black, or does it crash?
The documentation for SwapBuffers says With multithread applications, flush the drawing commands in any other threads drawing to the same window before calling SwapBuffers.
Are you synchronizing the swap between the two threads, or do you want the swapping thread to just show whatever is in the back-buffer right now, even if the other thread is busy drawing to it?

Share this post


Link to post
Share on other sites
Maybe this can help :

http://www.equalizergraphics.com/documentation/parallelOpenGLFAQ.html

In short, it says you can't manage one rendering context from multiple threads in openGL. I believe ths is what you're trying to do.

Share this post


Link to post
Share on other sites
Sure you can manage.
You just need to call wglMakeCurrent(rc, glrc), then do your gl calls and at the end of your render function, wglMakeCurrent(NULL, NULL).
You also need to check on the other thread. Is the other thread using the context?

It will drag down performance.

Share this post


Link to post
Share on other sites
I haven't checked GetLastError yet. I'm away from my computer, so I'll get back to you with that.

Quote:
Original post by V-man
Sure you can manage.
You just need to call wglMakeCurrent(rc, glrc), then do your gl calls and at the end of your render function, wglMakeCurrent(NULL, NULL).
You also need to check on the other thread. Is the other thread using the context?

It will drag down performance.


The problem is creating and/or setting the context to begin with. That's what's killing me.

So does this mean I'll have to switch to Direct3D?

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!