EsoErik

Friday, January 23, 2015

 

In PyQt5, with QOpenGlWidget, on OS X, with OpenGL 4.1 and format.setProfile(Qt.QSurfaceFormat.CoreProfile), how do I avoid crashing?

YOU DON'T.  Also, it's not PyQt5's fault; the following C++ code also causes the same crash for the same reason:

#include 
#include 
#include 

class GlWidget : public QOpenGLWidget
{
public:
    GlWidget()
    {
        QSurfaceFormat f;
        f.setRenderableType(QSurfaceFormat::OpenGL);
        f.setVersion(4,1);
        f.setProfile(QSurfaceFormat::CoreProfile);
        f.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
        f.setStereo(false);
        setFormat(f);
    }

    virtual void initializeGL()
    {
    }

    virtual void paintGL()
    {
    }

    virtual void resizeGL(int w, int h)
    {
    }
};

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    GlWidget* glw = new GlWidget();
    glw->show();
    return app.exec();
}

Here is what the crash looks like (stack trace from the offending thread):

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                   0x00007fff848a30dd objc_msgSend + 29
1   libqcocoa.dylib                   0x00000001129839d3 QCocoaIntegration::createPlatformOpenGLContext(QOpenGLContext*) const + 83
2   org.qt-project.QtGui              0x000000010fce471a QOpenGLContext::create() + 74
3   org.qt-project.QtWidgets          0x000000010f65304d QOpenGLWidgetPrivate::initialize() + 141
4   org.qt-project.QtWidgets          0x000000010f653e1a QOpenGLWidget::resizeEvent(QResizeEvent*) + 58
5   org.qt-project.QtWidgets          0x000000010f6334e2 QWidget::event(QEvent*) + 2162
6   org.qt-project.QtWidgets          0x000000010f5f3dbb QApplicationPrivate::notify_helper(QObject*, QEvent*) + 251
7   org.qt-project.QtWidgets          0x000000010f5f7110 QApplication::notify(QObject*, QEvent*) + 8192
8   org.qt-project.QtCore             0x0000000110475fb3 QCoreApplication::notifyInternal(QObject*, QEvent*) + 115
9   org.qt-project.QtWidgets          0x000000010f62bf17 QWidgetPrivate::sendPendingMoveAndResizeEvents(bool, bool) + 327
10  org.qt-project.QtWidgets          0x000000010f631c1a QWidgetPrivate::show_helper() + 42
11  org.qt-project.QtWidgets          0x000000010f632a42 QWidget::setVisible(bool) + 1394
12  qt5glcrashtest                    0x000000010f552963 main + 67
13  qt5glcrashtest                    0x000000010f552914 start + 52


Three important things to note:
  1. On OS X, with OpenGL compatibility profile, which allows calls deprecated by OpenGL 3, you are strictly limited to OpenGL 2.1
  2. The QPainter related stuff supported by the widget-ness of QOpenGLWidget relies on deprecated OpenGL calls
  3. In Qt5 (the logic responsible may reside at a lower level) on OS X, specifically requesting an OpenGL 4.1 context with compatibility profile results in an OpenGL 2.1 context.  Specifically requesting 4.1 with core profile does result in a 4.1 context - one that does not support the deprecated calls.
In short, it's no surprise that QOpenGLWidget with a 4.1 core profile context on OS X fails in some way.  The workaround is to chose to either use QOpenGLWidget with OpenGL 2.1 or QOpenGLWindow with OpenGL 4.1.  This deprives you of direct QPainter support, but it does not prevent you from making your own QPainter that draws to a buffer you then composite via OpenGL in your QOpenGLWindow-derived object.  NB: if you want to make your QOpenGLWindow-derived object's window a child of a normal Qt widget that can be placed by the QLayout classes, you'll want to do something along the lines of:
Incidentally, if you need to support OpenGLWindow with OpenGL 4.3 on OS X, then I have bad news for you.  OS X does not support OpenGL 4.3.  Apple makes approximately all of their money on iOS trash.  Any money spent on OS X development is, by comparison, completely wasted.  Don't hold your breath waiting for them to improve OS X.  Instead, expect half-baked iOS features to appear in OS X quarter-baked.  That is, at best, OS X might support some new OpenGL ES features, with a huge number of wont-fix bugs.  Too bad, so sad, get a real OS.

Archives

July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   September 2010   December 2010   January 2011   February 2011   April 2011   June 2011   August 2011   February 2012   June 2012   July 2012   August 2012   October 2012   November 2012   January 2014   April 2014   June 2014   August 2014   September 2014   October 2014   January 2015   March 2015   April 2015   June 2015   November 2015   December 2015   January 2016   June 2016   August 2016   January 2017   March 2017  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]