opentk icon indicating copy to clipboard operation
opentk copied to clipboard

[5.0] GLFWException when using Wayland

Open dmsch opened this issue 3 years ago • 6 comments
trafficstars

Description

When the OpenTK application is started in a Wayland environment, a GLFWException is thrown.

Unhandled exception. OpenTK.Windowing.GraphicsLibraryFramework.GLFWException: The GLFW library is not initialized
   at OpenTK.Windowing.Desktop.GLFWProvider.<>c.<.cctor>b__5_0(ErrorCode errorCode, String description)
Aborted (core dumped)

Repro steps

Please provide the steps required to reproduce the problem

  1. Run OpenTK application in a Wayland Environment (eg. Sway), Xwayland is disabled.

Related information

  • OS: Ubuntu 22.04
  • SDL or Native backend (if using OpenTK 3.x): Native Backend
  • What version of OpenTK (Installer, NuGet, Source Code): Nuget, 5.0.0-pre.7
  • What runtime (.NET Framework, CoreCLR, Mono): .NET 6.0
  • Workarounds (if any): Use Xorg

Example:

        static void Main(string[] args)
        {
            var gameSettings = new GameWindowSettings()
            {
                IsMultiThreaded = false, 
                RenderFrequency = 60,
                UpdateFrequency = 60, 
            };

            var windowSettings = new NativeWindowSettings()
            {
                APIVersion = new Version(4, 1),
                WindowBorder = WindowBorder.Hidden
            };
            
            using (var game = new MainWindow(gameSettings, windowSettings))
            {
                game.Run();
            }
        }

dmsch avatar May 24 '22 14:05 dmsch

What happens if you manually call GLFW.Init() before all of the code? It could also be the GLFW version used. Can you also check what GLFW.GetVersionString() returns.

You could also try manually downloading the latest OpenTK.redist.glfw package from nuget and see if that fixes anything.

NogginBops avatar May 24 '22 18:05 NogginBops

GLFW Version: 3.3.7 X11 GLX EGL OSMesa clock_gettime evdev shared GLFW.Init() is executed without exception.

The GLFW Binaries are present (runtimes/linux-x64/native/libglfw.so.3.3). The same application runs fine in a Xorg server environment. I also tested with different Wayland compositors (labwc, wayfire), same issue.

The exception is thrown when GLFW.GetPrimaryMonitor() is called by the constructor of NativeWindowSettings.

dmsch avatar May 25 '22 07:05 dmsch

GLFW doesn't support switching between Wayland/X11 at runtime, it's hardcompiled into the binary which it uses. Only recommendation I can give is to use Xwayland.

PJB3005 avatar May 25 '22 07:05 PJB3005

@PJB3005 would this mean if we wanted to support Wayland we would need to distribute another glfw binary? Does .net have a RID for Wayland?

NogginBops avatar May 25 '22 09:05 NogginBops

we would need to distribute another glfw binary?

Yes

Does .net have a RID for Wayland?

No

Your only real solution is to ship both X11 and Wayland versions for Linux, then detect (probably via env var?) at runtime whether Wayland is used and if so use a DllImportResolver to switch between the two. This can only happen once, so if Wayland doesn't work at that point you're deadlocked and won't be able to fallback to X11.

This is still quite fragile for a variety of reasons. Really ideally GLFW would just support both at once but...

PJB3005 avatar May 25 '22 09:05 PJB3005

(probably via env var?)

$XDG_SESSION_TYPE == wayland or x11

source: this answer on unix stack exchange

utkumaden avatar Aug 19 '22 17:08 utkumaden

Ok so there seems like there is a way to fix this issue. I'm moving the milestone to 4.7.6 so we can fix it there and then it can get forward merged into 5.0 at some point.

NogginBops avatar Oct 26 '22 13:10 NogginBops

Moving this to 4.8.0 as it seems more appropriate for that version.

NogginBops avatar Feb 22 '23 14:02 NogginBops

@dmsch it would be great if you are able to test the PR that I've created that loads Wayland binaries for GLFW.

Though it seems like our implementations of GameWindow and NativeWindow rely on features that Wayland doesn't support, so maybe enabling Xwayland and running OpenTK that way is the way to go.

NogginBops avatar Jul 13 '23 20:07 NogginBops

Merged the PR, feel free to reopen if 4.8.0 doesn't solve this issue. Also see #1612

NogginBops avatar Jul 14 '23 17:07 NogginBops