Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base-Qt reports wrong OpenGL Version #22

Closed
mschollmeier opened this issue Oct 8, 2019 · 9 comments
Closed

base-Qt reports wrong OpenGL Version #22

mschollmeier opened this issue Oct 8, 2019 · 9 comments

Comments

@mschollmeier
Copy link

@mschollmeier mschollmeier commented Oct 8, 2019

When I compile the magnum-bootstrap-base-qt code using the latest Qt 5.13 on MacOS, _context_create() reports

OpenGL version: 2.1 ATI-2.11.20

which is much older than what my GPU supports. Compiling a standard example with CMake and SDLApplication (e.g., the triangle example), results in

OpenGL version: 4.1 ATI-2.11.20

Any idea why Qt and/or Magnum picks an older OpenGL version?

@mosra mosra added this to the 2019.0b milestone Oct 8, 2019
@mosra

This comment has been minimized.

Copy link
Owner

@mosra mosra commented Oct 8, 2019

IIRC Qt is at fault here, unfortunately. Mac GL drivers (and Mesa drivers as well) have this peculiar feature where there's

  • either the "old GL", 2.1 (or 3.1 in case of Mesa) with all deprecated fixed-function APIs being present
  • or the "core GL", as we know it nowadays, 3.2 and up

but never both. For Qt this can be fixed by requesting core context on the QOpenGLWidget, something like:

QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
setFormat(format);

However, this OTOH makes AMD and NVidia drivers being stuck on 3.2 instead of going all the way to 4.6. Magnum's own application implementations solve this by a rather nasty piece of code where first a core 3.2 context is requested and then if AMD or NV drivers are detected, the context is destroyed and a new one requested without asking for core. See the code e.g. here.

I'm frankly not sure how to do this for the Qt bootstrap without scaring everyone off :D Ideally Qt itself would handle it, but so far I'm not aware of any such feature there.

@mschollmeier

This comment has been minimized.

Copy link
Author

@mschollmeier mschollmeier commented Oct 8, 2019

Thank you for the quick reply. If I do this, the OpenGL version is reported correctly now:

OpenGL version: 4.1 ATI-2.11.20

I had actually tried this before, but it results in a crash:

qt.qpa.openglcontext: Could not create NSOpenGLContext with shared context, falling back to unshared context.
Platform::Context::tryCreate(): context already created
The program has unexpectedly finished.

The code runs all the way thru paintGL where it wraps the qtDefaultFramebuffer, but then fails in initializeGL at _context_create().

@mosra

This comment has been minimized.

Copy link
Owner

@mosra mosra commented Oct 8, 2019

Argh ... that can only mean initializeGL() is called more than once. Qt docs say setFormat() has to be called before the widget is parented to anything. Would something like this work?

MyApplication::MyApplication(Platform::GLContext& context, QWidget* parent, Qt::WindowFlags f): QOpenGLWidget{nullptr, f}, _context(context) {
    ...
    setFormat(format);
    setParent(parent);
}
@mschollmeier

This comment has been minimized.

Copy link
Author

@mschollmeier mschollmeier commented Oct 8, 2019

I've found out that this is actually a bug in Qt caused by Apple's decision to deprecate OpenGL support: QTBUG-63180. In short, we can only select Version 4.1 or 2.1 on MacOS.

The following fixes this issue in main:

int main(int argc, char** argv) {
    Platform::GLContext context{NoCreate, argc, argv};
    QApplication app{argc, argv};

    MyApplication w{context};

    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(4, 1);
    format.setProfile(QSurfaceFormat::CoreProfile);
    QSurfaceFormat::setDefaultFormat(format);
    w.setFormat(format);

    w.show();

    return app.exec();
}
@mschollmeier

This comment has been minimized.

Copy link
Author

@mschollmeier mschollmeier commented Oct 8, 2019

By the way, setting the format to (4,1) also fixes an issue with the MouseInteractionExample. Using (2,1), the depthAt function returns garbage; with (4,1) the return value is close to 1.0 as it should be.

@mosra

This comment has been minimized.

Copy link
Owner

@mosra mosra commented Oct 8, 2019

Wait a moment, so this affects also Sdl2Application? Or you mean mouse interaction example rewritten with Qt?

I'll add your snippet above for macOS, I guess it can only improve things, not make them worse :)

@mschollmeier

This comment has been minimized.

Copy link
Author

@mschollmeier mschollmeier commented Oct 8, 2019

Mouse interaction rewritten with Qt, sorry for the confusion. The SDL example works as-is. I guess the Magnum context creation implementation you mentioned above is robust enough.

@mosra

This comment has been minimized.

Copy link
Owner

@mosra mosra commented Oct 8, 2019

Thanks for the clarification -- yeah, the example needs some GL3+ functionality (not sure where exactly tho), so that's kinda expected.

Your patch is applied as 67ee392, thanks a lot. (Travis CI is stuck with mac builds, so I hope I didn't make any silly mistake there.) Anything left to be done here? :)

@mschollmeier

This comment has been minimized.

Copy link
Author

@mschollmeier mschollmeier commented Oct 8, 2019

Looks like it's fixed, nothing else to be done here. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.