Hey everyone. This week I had the strange itch to see if I could do any OpenGL work on worker threads, and if so, if it would actually have any benefits. TL:DR, yes it can happen without too much trouble (depending on your architecture), and it did help me get my load time down by ~5ms per 2048x2048 RGB glTexImage2D(...)
All you need to set this up is to create an OpenGL context for each thread, who has its context shared with your main thread's context. The process for doing this is surprisingly easy, it can just be one function call. Here's an example
HGLRC MainContext = wglCreateContextAttribsARB(WindowDC, 0, OpenGLAttribs);
HGLRC WorkerThreadContext = wglCreateContextAttribsARB(WindowDC, MainContext, OpenGLAttribs);
By passing the main context to the wglCreateContextAttribsARB
function, it creates and shares all in one call. If for some reason you created your contexts separately, you can also use wglShareLists(...)
After creating the context, all you have to do is call wglMakeCurrent(WindowDC, WorkerThreadContext)
from within your thread. You only have to call this once within the thread's life cycle.
Buyer beware, I wouldn't recommend multithreading OpenGL on a large-scale. Just keep it to simple things like this. This is also throwing a GL_INVALID_OPERATION for me 1/50 times so maybe its not shippable?