7

I currently try to implement a "Loading thread" for a very basic gaming engine, which takes care of loading e.g. textures or audio while the main thread keeps rendering a proper message/screen until the operation is finished or even render regular game scenes while loading of smaller objects occurs in background.

Now, I am by far no OpenGL expert, but as I implemented such a "Loading" mechanism I quickly found out that OGL doesn't like access to the rendering context from a thread other than the one it was created on very much. I googled around and the solution seems to be:

"Create a second rendering context on the thread and share it with the context of the main thread"

The problem with this is that I use SDL to take care of my window management and context creation, and as far as I can tell from inspecting the API there is no way to tell SDL to share contexts between each other :(

I came to the conclusion that the best solutions for my case are:

Approach A) Alter the SDL library to support context sharing with the platform specific functions (wglShareLists() and glXCreateContext() I assume)

Approach B) Let the "Loading Thread" only load the data into memory and process it to be in a OpenGL-friendly format and pass it to the main thread which e.g. takes care of uploading the texture to the graphics adapter. This, of course, only applies to data that needs a valid OpenGL context to be done

The first solution is the least efficient one I guess. I don't really want to mangle with SDL and beside that I read that context sharing is not a high-performance operation. So my next take would be on the second approach so far.

EDIT: Regarding the "high-performance operation": I read the article wrong, it actually isn't that performance intensive. The article suggested shifting the CPU intensive operations to the second thread with a second context. Sorry for that

After all this introduction I would really appreciate if anyone could give me some hints and comments to the following questions:

1) Is there any way to share contexts with SDL and would it be any good anyway to do so?

2) Is there any other more "elegant" way to load my data in the background that I may have missed or didn't think about?

3) Can my intention of going with approach B considered to be a good choice? There would still be slight overhead from the OpenGL operations on my main thread which blocks rendering, or is it that small that it can be ignored?

6
  • 2
    I would go with solution B. Let the background thread load all the necessary data and the main thread handle the drawing.
    – user500944
    Jul 25, 2011 at 19:33
  • Thanks, I am refactoring my code at the moment to load my data that way. But maybe someone still got some comments to make this question give more informations in case someone runs into the same issue :)
    – PuerNoctis
    Jul 25, 2011 at 19:36
  • Bear in mind, though, that this solution is easier to be said than done: depending on the details of you app, you threads will most likely have to be synchronized properly and this is not always a straightforward task. I assumed that you have investigated single-threaded alternatives and didn't find one that suited you well.
    – user500944
    Jul 25, 2011 at 19:40
  • Unfortunately that's the case, I didn't came accross a proper single-threaded solution :( But I am still searching. For the synchronization: I thought about querying the thread each frame if it has completed, and if so, I know my data is ready to be processed by OpenGL. After this step is done the data can be used by my application. If I have a loading screen running there's no much need to do anything more I think - for deferred loading while the game scene si running I would also have to check if the data is actually ready. Also, slight overhead again, but I think there's no other workaround
    – PuerNoctis
    Jul 25, 2011 at 19:45
  • "I read that context sharing is not a high-performance operation." Where did you read that? Because it's patently false. Jul 25, 2011 at 20:22

1 Answer 1

7

Is there any way to share contexts with SDL

No.

Yes!

You have to get the current context, using platform-specific calls. From there, you can create a new context and make it shared, also with platform-specific calls.

Is there any other more "elegant" way to load my data in the background that I may have missed or didn't think about?

Not really. You enumerated the options quite well: hack SDL to get the data you need, or load data inefficiently.

However, you can load the data into mapped buffer objects and transfer the data to OpenGL. You can only do the mapping/unmapping on the OpenGL thread, but the pointer you get when you map can be used on any thread. So map a buffer, pass it to the worker thread. It loads data into the mapped memory, and flips a switch. The GL thread unmaps the pointer (the worker thread should forget about the pointer now) and uploads the texture data.

Can my intention of going with approach B considered to be a good choice?

Define "good"? There's no way to answer this without knowing more about your problem domain.

4
  • With "good" I actually meant if it was generally a better or professional idea to get context sharing going no matter what. But thanks for your suggestion, I definetely will look into it tomorrow.
    – PuerNoctis
    Jul 25, 2011 at 20:39
  • 3
    @Nicol Bolas: Actually you can share contexts with SDL, if you don't boggle from writing plattform dependent code: wglGetCurrent{DC,Context}, glXGetCurrent{Display,Drawable,Context}, you figure the rest. I used those in my PBuffer convenience library.
    – datenwolf
    Jul 25, 2011 at 21:11
  • @datenworlf: I completely forgot you could just get the current context. Jul 25, 2011 at 21:15
  • 1
    @datenwolf Works perfectly! First I got the DC/RC with wglGetCurrent{DC,Context} as suggested, created a new context from within the thread and made it current, and then shared the display list spaces with wglShareLists(mainContext, threadContext). Thanks!
    – PuerNoctis
    Jul 26, 2011 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.