Multi-threading/context sharing

Started by CodeBunny, December 19, 2012, 00:36:33

Previous topic - Next topic

CodeBunny

Where's the multi-threading example? I can't seem to find it on the wiki.

Also, how do multi-threaded OpenGL contexts work? Especially, how does background loading of assets work?

kappa


CodeBunny

Thank you.

Can you give me a quick rundown on how multi-threaded OpenGL calls are received by the GPU? Are they all just received one after another, or are the different threads considered to be running different series of commands that are interpreted differently?

spasi

First of all there's no such thing as a multi-threaded OpenGL context. The only way to submit OpenGL commands from multiple threads is by having multiple OpenGL contexts. How the actual command submission and execution happens is an implementation detail, but the only safe assumption you can make is that each context has its own command buffer.

This means that no matter what you do on the CPU side, GL commands will be serially executed for each context independently, with no guaranteed order for commands from multiple contexts. You may create and upload a texture in one context and render with it in another. Even if the actual commands are send in the correct order using CPU synchronization, the GL is free to go ahead and render before the actual texture upload is complete. Hence, you also need GL synchronization, either with explicit glFinish/glFlush commands or (preferably) with fence objects.

Also note that context sharing introduces substantial overhead to the GL operation. Not only you'll have to do a lot of manual synchronization, which is costly, the GL driver itself will have lots of additional work to do. Every time you mutate an object in one context, the driver will have to communicate that change to any other context that uses the object, context switching is expensive, etc. So, if you're really interested in this, you'll have to make sure that the overhead is worth any gains you'll get.

I'd only bother if game loading took too long and wanted to render something dynamic at the same time. Or if I wanted to have game world streaming.

CodeBunny

So what happens if I'm loading while executing rendering commands? Does it matter at all?