开发者

SDL / OpenGL: Implementing a "Loading thread"

I currently try to implement a "Loading thread" for a very basic gaming engine, which takes care of loading e.g. te开发者_如何转开发xtures 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜