Multithreaded OpenGL (wglShareLists and equivalents)

Hi,

I’m working on a terrain demo which loads most of its data using a
separate thread, to try to avoid frame rate hitches in the main
rendering thread. It works pretty well, except for one problem: when
loading a new texture, I can’t actually make the OpenGL calls to
create the texture in the loader thread; I have to pass the image data
over to the main thread, and do the glTexImage2D calls there. This is
because OpenGL texture objects (and display lists) are not shared
between threads, by default. This is causing some frame rate hitches
for me, which I’d like to get rid of if possible.

Under Windows, there’s a call “wglShareLists” that can be used to make
the background thread share textures with the foreground thread. Also
under X, it appears there’s some way to get the same effect, although
I’m not as familiar with it. Basically, when I do glBindTextures and
glTexImage2D in thread B, I want the texture id to be valid in thread
A.

My question:

  • Is there any way to do this via SDL? (Couldn’t find anything)

  • Does someone have some cross-platform code that handles this, that
    they wouldn’t mind sharing? By “cross-platform”, I’d at least like
    to cover Win32 and Linux/XFree86; anything more is a bonus.

You can read more about this particular project here:
http://tulrich.com/geekstuff/chunklod.html--
Thatcher Ulrich
http://tulrich.com

Under Windows, there’s a call “wglShareLists” that can be used to make
the background thread share textures with the foreground thread. Also
under X, it appears there’s some way to get the same effect, although
I’m not as familiar with it. Basically, when I do glBindTextures and
glTexImage2D in thread B, I want the texture id to be valid in thread
A.

I looked at the wglShareLists call, and it looks like it shares between
contexts. Does that mean you have to create a separate context in the
loader thread, call wglShareLists() and then bind the textures?

To answer your question, the next release of SDL will provide the OpenGL
context in the Win32 SDL_SysWMinfo structure, so you’ll be able to get
to the context to make this call.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment