Jump to content
  • Advertisement
Sign in to follow this  
htz92127

OpenGL Load Image fail in multithread?

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

I load image with the DevIL(OpenIL). It could be seccessful when the fuction been called in the same thread as render therad. However, it failed in multithread.
I want to know is the DevIL support multithread? Otherwise, I need open same OpenGL state? I remember there is a D3DCREATE_MULTITHREADED Flags in DX.

Share this post


Link to post
Share on other sites
Advertisement
The rendering context can only be active in a single thread at any time. So to load your texture in another thread, you need to activate it in that thread. However, be aware that doing so will deactivate it in the previous thread, which will stop any OpenGL command to work in that thread instead.

I have not checked the very latest in OpenGL, so there may be some way around this to load resources asynchronously in different threads nowadays (I have a vague memory of this being in the pipe sometime). But you'll have to check the specification or the extensions available.

Share this post


Link to post
Share on other sites
Three solutions:
1. Map/unmap buffer objects in the main thread and do the loading and copy to buffer in another thread.
2. Create a shared context and use that one for uploading data.
3. Do the loading off disk in another thread and screw multithreading for the actual data copy. Just do it in the main thread.

The last solution has the disadvantage of taking away time from your render loop and not using more than one core when it would matter. It has the advantage of being simple, though, and it works.

The first solution has the problem that it is a terrible mess. One thread writes to a data buffer that it does not own which another thread maps and unmaps and which may "randomly" be lost and may need to be filled again (thank you, Khronos guys, for such a feature!), in which case the latter thread has to signal the former one again and blah.

The second solution has the advantage that it is only moderately complicated and that it works, and there is no mess. However, sharing contexts may not work equally well with all drivers (especially older ones) and may (presumably?) cause data swapping on some systems, which is why many people deem it bad Karma to go anywhere near sharing contexts.

Share this post


Link to post
Share on other sites
Thanks for your replies.
But I am a beginner of the OpenGL, I didn't know how to create shared context?
Could you tell me how to do that?

Share this post


Link to post
Share on other sites
In Windows you can do it like this:

HGLRC hGLRC = wglCreateContext(hDC);
HGLRC hGLRCThread = wglCreateContext(hDC);

// Enable sharing
wglShareLists(hGLRC, hGLRCThread);




Then you use wglMakeCurrent(...) to set a different context in each thread, and you can use the same textures in both.

Share this post


Link to post
Share on other sites
Is the function wglCreateContext() Windows-only? And that I am using ilutGLBindTexImage() to help me bind image, how can I share the context? Should I bind it by myself?

Share this post


Link to post
Share on other sites
Yes, that is Windows only. There are wglXXXXXXX functions for Windows and glxXXXXXXX for Unix derivates (mostly identical or similar).

Note that wglCreateContext + wglShareLists is not atomic, which is usually not a problem. It's the same as with making a singleton threadsafe -- it's silly.
Simply spawn threads after these function calls, and you have no issue that you need to take care of.

If thread safety is a problem, you can instead use wglCreateContextAttribsARB.
This has a somewhat different usage (and, you must load it with wglGetProcAddress first!), but it is nice and easy once you grasp it. More options, kind of more comprehensive, and thread-safe.

Share this post


Link to post
Share on other sites
I write the app with opengl instead DX as I don't want to use windows-only function. I think I know how to solve the problem:
Load all images in the main thread.

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!