Bithack / principia

Open source physics-based sandbox game.
https://principia-web.se
Other
283 stars 30 forks source link

Crash on wayland #174

Closed fgaz closed 3 months ago

fgaz commented 3 months ago
Principia version

2024.06.28, NixOS package

OS / Hardware
Summary

principia crashes when run under wayland (SDL_VIDEODRIVER=wayland). It works fine under X11 (SDL_VIDEODRIVER=x11)

Steps to reproduce
$ SDL_VIDEODRIVER=wayland principia 
I: Starting fifo listener thread
I: chdirring to /nix/store/ljwcc8l3acwngm45m3mqx1bnk50yl53i-principia-2024.06.28/bin/
I: attempting to open /tmp/principia.run O_RDONLY
I: Storage path: /home/fgaz/.principia
I: Redirecting log output to /home/fgaz/.principia/run.log

contents of run.log:

            _            _       _       
 _ __  _ __(_)_ __   ___(_)_ __ (_) __ _ 
| '_ \| '__| | '_ \ / __| | '_ \| |/ _` |
| |_) | |  | | | | | (__| | |_) | | (_| |
| .__/|_|  |_|_| |_|\___|_| .__/|_|\__,_|
|_|                       |_|            
Version 36, built Jan  1 1980 00:00:00
I: chdirring to ../
I: chdirring to ./share/principia/
I: Compiled against SDL v2.30.3
I: Linked against SDL v2.30.3
I: Initializing SDL...
I: set initial res to 1015x570
I: num workers (real): 16
I: Loading settings...
I: num workers (user): 16
I: Shadow quality: 1 (1280x720)
I: Creating window...
I: Initializing GLEW...
I: ERROR: Unknown error
rollerozxa commented 3 months ago

I've been able to run Principia with the native Wayland SDL video driver on my Arch system, but I just tested in a NixOS live environment and can reproduce the issue there. I ended up looking at the packaging for my system's GLEW package and saw that it has this patch applied for something with Wayland support, and rebuilding my GLEW package before this commit makes me able to reproduce the crash on Arch too. Seems like upstream GLEW knows about there being issues like this on Wayland: https://github.com/nigels-com/glew/issues/172

The workaround other projects seems to have done is just ignore the GLEW_ERROR_NO_GLX_DISPLAY error and apparently it will just work like regularly on Wayland, as concerning as it may look. With my rebuilt GLEW without distro patches the following change to Principia makes the game work again:

diff --git a/src/tms/backend/main.cc b/src/tms/backend/main.cc
index 375a03d7..ca9ba0d9 100644
--- a/src/tms/backend/main.cc
+++ b/src/tms/backend/main.cc
@@ -400,7 +400,7 @@ tbackend_init_surface()

     tms_infof("Initializing GLEW...");
     GLenum err = glewInit();
-    if (err != GLEW_OK) {
+    if (err != GLEW_OK && err != GLEW_ERROR_NO_GLX_DISPLAY) {
         tms_infof("ERROR: %s", glewGetErrorString(err));
         exit(1);
     }

I wonder if this workaround would work under NixOS too.

rollerozxa commented 3 months ago

Apparently the fix also fixes our AppImage builds that have previously been broken when trying with the Wayland video driver. So I assume this was it.

fgaz commented 3 months ago

I confirm that applying the patch in https://github.com/Bithack/principia/issues/174#issuecomment-2197494069 fixes the issue for me