简体   繁体   English

如何在具有共享 OpenGL 上下文的 ZE8801102A40AD89DDCFDCAEBF008D25Z 应用程序中有条件地指定 OpenGL ES 版本?

[英]How do I conditionally specify OpenGL ES version in a Qt application with shared OpenGL contexts?

The hellogles3 sample constructs the QGuiApplication before testing for desktop OpenGL. hellogles3 示例在测试桌面QGuiApplication之前构建 QGuiApplication。 If you don't do this then QOpenGLContext::openGLModuleType() crashes.如果你不这样做,那么QOpenGLContext::openGLModuleType()崩溃。

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QSurfaceFormat fmt;
    fmt.setDepthBufferSize(24);

    // Request OpenGL 3.3 core or OpenGL ES 3.0.
    if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
        qDebug("Requesting 3.3 core context");
        fmt.setVersion(3, 3);
        fmt.setProfile(QSurfaceFormat::CoreProfile);
    } else {
        qDebug("Requesting 3.0 context");
        fmt.setVersion(3, 0);
    }

    QSurfaceFormat::setDefaultFormat(fmt);

    GLWindow glWindow;
    glWindow.showMaximized();

    return app.exec();
}

However, if you are using Qt::AA_ShareOpenGLContexts , then QSurfaceFormat::setDefaultFormat must be called before constructing QGuiApplication :但是,如果您使用的是Qt::AA_ShareOpenGLContexts ,则必须在构造QGuiApplication之前调用QSurfaceFormat::setDefaultFormat

Note: When setting Qt::AA_ShareOpenGLContexts, it is strongly recommended to place the call to this function before the construction of the QGuiApplication or QApplication.注意:在设置 Qt::AA_ShareOpenGLContexts 时,强烈建议在构造 QGuiApplication 或 QApplication 之前调用此 function。 Otherwise format will not be applied to the global share context and therefore issues may arise with context sharing afterwards.否则,格式将不会应用于全局共享上下文,因此之后的上下文共享可能会出现问题。

Attempting to call create on a standalone QOpenGLContext instance to test OpenGL type before constructing QGuiApplication also crashes, because QGuiApplicationPrivate::platformIntegration() has not been created yet.尝试在独立的QOpenGLContext实例上调用 create 以在构造QGuiApplication之前测试 OpenGL 类型也会崩溃,因为QGuiApplicationPrivate::platformIntegration()尚未创建。

Is there any way around this?有没有办法解决?

It appears to be safe to instantiate a temporary QGuiApplication for the check.为检查实例化一个临时的QGuiApplication似乎是安全的。 For example:例如:

int main(int argc, char *argv[])
{
    {
        QGuiApplication tempapp(argc, argv);

        QSurfaceFormat fmt;
        fmt.setDepthBufferSize(24);

        // Request OpenGL 3.3 core or OpenGL ES 3.0.
        if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
            qDebug("Requesting 3.3 core context");
            fmt.setVersion(3, 3);
            fmt.setProfile(QSurfaceFormat::CoreProfile);
        } else {
            qDebug("Requesting 3.0 context");
            fmt.setVersion(3, 0);
        }

        QSurfaceFormat::setDefaultFormat(fmt);
    }
    QCoreApplication::setAttribute( Qt::AA_ShareOpenGLContexts );
    
    QGuiApplication app(argc, argv);

    GLWindow glWindow;
    glWindow.showMaximized();

    return app.exec();
}

Note: my particular use case needed the fallback to OpenGL ES for remote desktop in Windows.注意:我的特定用例需要回退到 OpenGL ES 用于 Windows 中的远程桌面。 However, this seems to be less necessary now that desktop OpenGL works over RDP in Windows 10, apparently due to updates in WDDM.然而,现在桌面 OpenGL 在 Windows 10 中通过 RDP 工作,这似乎不太必要,显然是由于 WDDM 中的更新。 There may be other cases that benefit from this check though.不过,可能还有其他情况可以从此检查中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM