PDA

View Full Version : OpenGL rendering in separate thread, Qt 5.0.1



jaf
13th March 2013, 09:41
Hi,

I use Qt 5.0.1 and tried demo http://qt-project.org/doc/qt-4.8/demos-glhypnotizer.html. It doesn't work on 5.0.1 because glWidget->makeCurrent() function in GLPainter::start() leads to the assertion "Cannot make QOpenGLContext current in a different thread" and crash. I added string

context()->moveToThread(&glThread);

to GLWidget::startRendering() before calling connect and this almost fixed the demo - it works stable if only one MDI window is opened and almost always crashes if I try to create new window with new thread. Also it is complaining a lot in the application output like this:

QGLShader::link: "(45,20): warning X3206: implicit truncation of vector type
QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent
QBackingStore::flush() called with non-exposed window, behavior is undefined
QGLShader::link: "(45,20): warning X3206: implicit truncation of vector type
QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent

etc etc.

I wander is it possible to make it work stable? Was it supposed to be enough in Qt 5.0.1 just to move QGLWidget context to the rendering thread before running it?

I attached the hypnotizer demo with changes mentioned.

optikradio
21st March 2013, 17:09
Hi,

I independently experienced the same behavior with that demo.
I found that as long as I had a primary context on the main thread,
regardless of whether or not I actually used it, I could have multiple
windows open, shared context or not. I could subsequently close
any window except the one using the main context. If I closed
that one, the program crashes.

I'm also curious what the 'intended' behavior of this demo is, and
I am very anxious to arrive at the optimal solution to this problem.

XxOsurfer3xX
19th June 2013, 12:02
Any updates on this example, because I have the same problem and would like to know the correct way of rendering OpenGL in a different thread.

wesblake
27th October 2015, 23:03
Bump. I'd love to hear an update for this as well. I used the same example (new link here: https://doc-snapshots.qt.io/4.8/demos-glhypnotizer-main-cpp.html) only I'm trying to make it work with QOpenGLWidget since QGLWidget is deprecated and have the same error showing up. I'm just trying to get the example to work at this point for a proof of concept that rendering can be done on a separate thread while still using a QPainter.

UPDATE: My own test is a much simplified version of the original. I am just trying to render a QImage, since we'll be rendering video. So basically, from the original example in question:
- I've changed QGLWidget to QOpenGLWidget
- Used UI file vs MDI Window since our app works that way
- Separated classes for my own clarity
- Removed all the extra GLPainter stuff and simply use drawImage with a QImage

Like the original, I get the error, but if I press ignore I still see my image rendered.
Attaching my own project. :)
11473