Creating multiple OpenGL contexts with GLFW on different threads

Started by
1 comment, last by cgrant 9 years, 7 months ago

I'm using GLFW 3.0.4.
I'm trying to improve my loading times by having all resources, including textures, loaded on a separate thread. Problem is, to actually use any opengl functions in a different thread, without blocking the main one, I need a separate opengl context as well.
GLFW doesn't seem to provide any functions to create an independent context, you can only create it in combination with a window. That's not much of a problem however, considering you can just hide it. The problem is this:

Note: This function may only be called from the main thread.

(http://www.glfw.org/docs/latest/group__window.html#ga5c336fddf2cbb5b92f65f10fb6043344)

So that path is basically down the drain.

Are there any alternatives for what I'm trying to do, or would I have to end up using a different library for creating the window/rendering context? If so, which one?

Advertisement

OpenGL is not very helpful with multi-threading, and you may simply be trying to do something that is going to be inherently inefficient or widely supported. I'm sure someone here will eventually chime in on the current state of affairs.

See: http://www.gamedev.net/topic/656684-cases-for-multithreading-opengl-code/

One thing you can do is utilize SSE though, especially for multimedia (textures). You are pretty much guaranteed SSE3 support these days, so if that helps you, you could take a look at where the bottlenecks are and see if you are IO bound or there are real cases for vectorization.

You can load and parse your resources on another thread, I guess, and then simply upload them on the rendering thread using pointer swapping with locks (as that will reduce allocation).

The IO also doesn't necessarily have to block in your main thread though, but I don't know anything about multiplatform asynch IO.

I only have a passing familiarity with linux AIO: http://man7.org/linux/man-pages/man7/aio.7.html

Another solution is to map the buffer on your main thread and pass the mapped pointer to your resource loading thread to populate it with whatever data your are loading. Once the loading is complete, signal the main thread that loading is complete, unmap the buffer, then use the resource. NOTE: This is a gross oversimplification, because both GPU and CPU synchronization will be necessary, but thats the general gist.

This topic is closed to new replies.

Advertisement