[Qt-interest] opengl thread

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Tue Jan 19 10:36:29 CET 2010


bert.ddecker wrote on Monday, January 18, 2010 11:28 PM:

> ... Or do all my opengl
> calls have to be made from my main thread ?

OpenGL - in general - is *not* thread-safe! That in mind, when you synchronise your access to the OpenGL API, you may very well use OpenGL in a multi-threaded application. Whether that's worth or not you have to decide, given your mileage.

Refer to her (a bit outdated): http://www.gamedev.net/community/forums/topic.asp?topic_id=380835&whichpage=1&#2515457

The conclusion seems to be: (on any OS), make sure just one thread at a time is in any gl* in a given GL context!

(But note that at least on Mac certain GL calls /can/ be made thread-safe, at least according to this (german!): http://www.osxentwicklerforum.de/thread.php?threadid=10862: "Seit Leopard und der kCGLCEMPEngine-Erweiterung und einem multithread Pixelformat (NSOpenGLPFAMPSafe) ist OpenGL "quasi-thread-safe" (since Leopard and the kCGLCEMPEngine extension and a multi-threaded pixel format (NSOpenGLPFAMPSafe) OpenGL is "quasi-thread-safe" (almost thread safe?))

Then this article (I just quickly read diagonal) seems to be interesting, too: http://hacksoflife.blogspot.com/2008/02/creating-opengl-objects-in-second.html: it describes how to generate VBOs etc. in a second thread.


But then there is still the issue of the actual painting (rendering). All Qt painting has to be done from the main (aka "GUI") thread! I think that not only applies to all widget and QPainter drawing, but also GL calls (not sure though about the later).

But what you can do is calculate the *model* in a worker thread (movements, shadows, "game" logic, ...), and whenever you are ready to render, signal the main thread to render the GL scene (and while it is rendering, start calculating the next frame, given the "mean delta" (time difference) it takes to render a scene (as to properly calculate object movements, for example).

So my general advice: try to calculate vertex data and other expensive "logic" in a worker thread, but keep the actual drawing in the main / "GUI" thread. Also keep in mind that many GL calls are already *asynchronous* (e.g. data transfer from/to graphic card - "sequential deferred execution within a context"), so you would not gain much my putting those calls in different threads anyway!

Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list