为什么我不能在 Qt 中使用 OpenGL ES 3.0?

Why can't I use OpenGL ES 3.0 in Qt?

我在 window 上设置了 QSurfaceFormat,并且此表面格式的 GL 版本号设置为“3.0”。代码:

static QSurfaceFormat createSurfaceFormat() {
    QSurfaceFormat format;
    format.setSamples(4);
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(3, 0);
    return format;
}

int main(int argc, char *argv[]) {
    // ...

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    QWindow* window = (QWindow*) engine.rootObjects().first();
    window->setFormat(::createSurfaceFormat());

    // ...
}

此外,在 main() 中,我启用了 OpenGL ES 模式,如下所示:

QGuiApplication::setAttribute(Qt::AA_UseOpenGLES);

这意味着我正在请求 GL ES 3.0 上下文。

ANGLE docs 说(在接近开头的 table 中)实现了 GL ES 3.0 -> D3D 11 API 翻译支持。根据 dxdiag.exe.

,我的系统支持 D3D 11

但是当我启动包含此 QML 代码的应用程序时...

Text {
    text: OpenGLInfo.majorVersion + "." + OpenGLInfo.minorVersion
}

...我看到显示“2.0”。此外,使用我描述的方法 here,我确定我的 PC 上支持的最大着色语言版本是“100”,即 1.0。

同时,从this Qt blog post得知Qt支持GL ES 3.0应用

那么为什么我不能在 Qt 中使用 OpenGL ES 3.0?

您需要在创建 window 本身之前在 QWindow 上设置 QSurfaceFormat(通过 create())。如果您通过 QML 创建顶级 windows,您无法控制 create() 何时被实际调用,因此解决方案是在您创建 Q(Gui)Application 之前在某处更改默认表面格式:

int main(int argc, char **argv) {
    // createSurfaceFormat() is the function you pasted above
    QSurfaceFormat::setDefaultFormat(createSurfaceFormat());

    QApplication app(argc, argv); 
    // etc.