[wxGTK][wx3.1.2] Can't make GL context current in frame constructor

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
williamjcm
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Oct 06, 2014 3:29 pm

[wxGTK][wx3.1.2] Can't make GL context current in frame constructor

Post by williamjcm » Fri Aug 16, 2019 6:37 pm

Hello!

I'm working on a wxWidgets-based project that depends on the GL library for the GUI, and the Magnum graphics library for the actual GL work.

I've been working a lot on Windows recently, where I could set the wxGLContext as current despite the frame containing my wxGLCanvas not being shown. But now that I'm focusing on Linux, I've been hitting that "assert failed in SetCurrent(): window must be shown" assert, with no solution in sight.

Other threads in this forum (and in particular, one post by Doublemax) suggest moving the GL initialisation code in my paint event handler.

Problem is, I don't want to show my frame before all GL stuff is initialised and ready, as I'm using the constructor to check if the user's system actually supports some OpenGL features, and work around driver bugs, thanks to Magnum's GL context management features (which need a context to be current). I also don't want to have a frame that appears, giving hope to the user, before hitting them with a "sorry, but your system doesn't support <feature I absolutely need to support>" error.

Would there be a way to still make the context current without showing the window ? According to Magnum's developer, it should be doable, as his SDL2 wrapper keeps the window hidden during GL initialisation on all platforms (though that could be a SDL2 thing, according to Magnum's source).

Note: the project I work on is proprietary, so I'm limited in how much code I'm allowed to share.

Thanks in advance for your answers!
I'm French, so, sorry if I make English mistakes.

Current environment as of 16/08/2019:
  • GCC 9.1.0 (Arch Linux; I use the equivalent MinGW-w64 on MSYS2 on Windows)
  • wxWidgets 3.1.2

Manolo
Can't get richer than this
Can't get richer than this
Posts: 702
Joined: Mon Apr 30, 2012 11:07 pm

Re: [wxGTK][wx3.1.2] Can't make GL context current in frame constructor

Post by Manolo » Mon Aug 19, 2019 3:56 pm

This is an old discussion. See https://trac.wxwidgets.org/ticket/16193
Some resume on it:

There are three steps to go:
a) Creating a gl-contex, which needs a "window" (in X11 this means a "drawable", not necessary a physical window).
b) Setting the context as current. This is the point in discussion.
c) Using GL-API.

The thing is whether GLX (Linux) drivers refuse or not to do b) in a hidden window. The specs allow it. Some guys suspect the driver goes too far by something like "Why allow b) in a hidden window if c) is then going to be dismissed?" My personal opinion is that those guys (or the drivers behaving so) are wrong, again, because the GLX specs allow it.

The only difference between Linux (X11, I don't know about Wayland) and MSWindows or OSX, is X11 asynchronous nature. The window (and then its drawable) must be "realized" before creating and using the context. Doing gl-context stuff in the constructor of a wxWindow may be too early, the window may not be realized yet.

And very likely you use glViewport command, or anything else that needs the size of the window. So a good point to be sure about realization is handling the wxSizeEvent (even fire it with a SetSize(...) command).

I see you want to do some not-drawing job before the window is shown. This is a strong argument in favor of removing the wx-assert, allowing a hidden window. Drawing job may be arguable (mmmhhh... not sure, specs are silent on this subject), even using FBOs.

If you want to do some checks on your own (best with several drivers and several graphics cards) you can remove the wxASSERT_MSG at src/common/glcmn.cpp at wxGLCanvasBase::SetCurrent(...), and also the wxCHECK2_MSG at src/unix/glx11.cpp at wxGLContext::SetCurrent. Then, recompile wx, and do the test with your current app.
I'd like to know the results of those checks. If you do them, please post them here and in trac-ticket of the former link.

Post Reply