Anuken/Arc

glewInit() called too early, fails with "Missing GL version" when used with EGL on Wayland

chkno opened this issue · 1 comments

chkno commented

(Fwd: NixOS/nixpkgs#198903 . FYI @wanderer @fgaz)

When Mindustry is built with an EGL-enabled GLEW and run on Wayland, it fails to start and emits this error message:

[I] [Core] Initialized SDL v2.24.0
[E] java.lang.ExceptionInInitializerError
        at arc.backend.sdl.SdlGL20.glGetString(SdlGL20.java:36)
        at arc.backend.sdl.SdlGraphics.<init>(SdlGraphics.java:38)
        at arc.backend.sdl.SdlApplication.<init>(SdlApplication.java:39)
        at mindustry.desktop.DesktopLauncher.main(DesktopLauncher.java:39)
Caused by: arc.util.ArcRuntimeException: GLEW failed to initialize: Missing GL version
        at arc.backend.sdl.jni.SDLGL.<clinit>(SDLGL.java:103)
        ... 4 more

The problem appears to be that Arc calls glewInit() too early. GLEW documentation specifies that glewInit should be called after the GL rendering context is created. Arc calls glewInit in a static initializer block.

I also have no idea what I'm doing, but if I move the SDLGL.init call from a static initializer block to SdlGraphics' constructor, the problem goes away. I don't know if SdlGraphics' constructor runs too late for some other reason. Here's a PR for this if this seems like a good idea.

(History: Previous Mindustry-on-wayland work: Anuken/Mindustry#1393 )

I think it should be called as the first thing in the constructor, since it then proceeds to call glGetString. Using OpenGL functions before glew is initialized... sounds like a very bad idea, I'm surprised it worked for you at all.