[Qt-interest] QGLFrameBufferObject and multiple threads

cyril.mailing.list cyril.mailing.list at gmail.com
Thu Sep 3 21:29:09 CEST 2009


Samuel Rødal a écrit :
> cyril.mailing.list wrote:
>> Hi
>>
>> I am working on a program that basically sports :
>>
>> - one regular QGLWidget "W" in the GUI thread
>> - one QGLContext "C1" that is created in the GUI thread and shared 
>> with the QGLWidget's context
>> - one QGLFrameBufferObject "FBO1" that is created in the GUI thread
>> - one QGLContext "C2" that is created in the GUI thread and shared 
>> with the QGLWidget's context
>> - one QGLFrameBufferObject "FBO2" that is created in the GUI thread
>>
>> After creating these objects, the program starts two worker threads 
>> that will be used to render some stuff onto FBO1 and FBO2 respectively.
>> To achieve this, the first (resp second) worker thread simply makes 
>> C1 (resp C2) the current GL context, then binds FBO1 (resp FBO2), 
>> then draws to this FBO using a QPainter, and finally unbinds the FBO 
>> and releases the GL context. Each thread does this in a loop to 
>> produce successive frames.
>>
>> On Windows, everything works just fine as long as I critical-section 
>> the QPainter::begin() to QPainter::end() part. Otherwise, I get 
>> random concurrency crashes, essentially due to a QPainter's internal 
>> "state" attribute being nullified and any moment.
>>
>> On Linux, I get crashes after fractions of seconds even with only one 
>> worker thread.
>>
>> I have the same problem with Qt4.5.2 (opengl paint engine) and Qt 
>> master (opengl2 paint engine), except that with Qt master I get a 
>> crash on program termination, due to a multiple release of a resource 
>> or something like that.
>>
>> What am I doing wrong? I thoroughly separated everything needed so no 
>> concurrent access is done on any public object. QPainter is said to 
>> be reentrant in the doc.
>>
>> note1: As seen in a couple of other threads I had to resort to the 
>> internal constructor QGLContext::QGLContext(QGLFormat, QPaintDevice*) 
>> to be able to set up my two GL contexts.
>> note2: This example deals with 2 threads but in could be any number.
>> note3: This example deals with 1 FBO per thread but there could be 
>> any number of FBOs per thread, e.g. to achieve some multiple buffering.
>> note4: I worked on machines with OpenGL 2.0+ support.
>>
>> Thanks for your time reading this!
>> Cyril
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> QtOpenGL is not reentrant, so you should avoid using 
> QGLFramebufferObject and QGLContext from threads other than the GUI 
> thread. It might happen to work on Windows but it won't on X11.
>
> Regards,
>
> Samuel
Thanks for you answer.
Any plans to make QtOpenGL reentrant? I can see in qt-master sources 
some things like "QThreadStorage<QGLThreadContext *>" and comments about 
making GL texture cache thread-safe, which actually made me confident in 
the first place.

Regards,
Cyril




More information about the Qt-interest-old mailing list