Specifying OpenGL Desktop instead of ES for Qt5

[亡魂溺海] 提交于 2020-01-16 05:35:33

问题


I am finally trying to wrap my head around shaders, using a tutorial I found. I decided to start with Qt5 (Windows) since I'm familiar with it and can focus on learning GLSL itself to start with. The only difference between what I'm doing and the tutorial is that I'm working with a QOpenGLWidget rather than a QOpenGLWindow (I just have a form with one widget on it, nothing special).

To get started with a fragment shader, in Qt I added a new desktop (not ES) fragment shader to my project, and Qt generates the following shader:

uniform sampler2D qt_Texture0;
varying vec4 qt_TexCoord0;

void main(void)
{
    gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st);
}

However, when compiling this shader, it generates this error:

QOpenGLShader::compile(Fragment): ERROR: 0:2: '' : No precision specified for (float)

I did a little bit of searching around and found this answer which states:

No default precision exists for fp types in fragment shaders in OpenGL ES 2.0.

From this, my conclusion is that my application is using OpenGL ES and not Desktop (otherwise it wouldn't expect a precision to be defined).

The GL version string I'm seeing is OpenGL ES 2.0 (ANGLE 2.1.0.8613f4946861). Fwiw, in Qt4 on this same machine, the version string is 3.0.0 - Build 9.17.10.4229.

Assuming my conclusion is correct, my question is: How can I configure the application to use regular OpenGL instead of OpenGL ES?


The suggestion in the comments to set the surface format's renderable type to OpenGL seemed promising, but it isn't working. For example, if I change it in the widget's constructor:

View::View (QWidget *parent) :
    QOpenGLWidget(parent),
    ...
{

    QSurfaceFormat f = format();
    qDebug() << "type was" << f.renderableType();

    f.setRenderableType(QSurfaceFormat::OpenGL);
    qDebug() << "type set to" << f.renderableType();

    setFormat(f);
    qDebug() << "type is now" << format().renderableType();

}

void View::initializeGL () {

    qDebug() << __FUNCTION__ << "type is now" << this->format().renderableType();
    ...

}

The issue persists and the output is (0 = default, 1 = OpenGL, 2 = OpenGLES):

type was 0
type set to 1
type is now 1
initializeGL type is now 2

So it appears to be being forced back to OpenGLES at some point between the constructor and initializeGL.

I observed similar behavior when setting the default surface format before constructing any GUI objects (and before constructing the QApplication) as well.


回答1:


Qt5 on Windows will use ANGLE as a fallback (emulating OpenGL ES 2.0 with Direct3D) if either the video card is blacklisted (in ANGLE configuration at the time Qt was compiled) or the video driver does not support modern OpenGL (i.e. if you have only the stock driver provided by Microsoft).

You can force the application to use OpenGL instead of angle by adding:

QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);

in your main.cpp file or by setting the environment variable QT_OPENGL to "desktop" (without quotes). You can find more details here: http://doc.qt.io/qt-5/windows-requirements.html



来源:https://stackoverflow.com/questions/55399801/specifying-opengl-desktop-instead-of-es-for-qt5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!