Skip to content

Commit 256699e

Browse files
committed
OpenXR: Add support for Wayland on Linux
1 parent 2e14492 commit 256699e

File tree

15 files changed

+149
-9
lines changed

15 files changed

+149
-9
lines changed

.github/workflows/linux_builds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ jobs:
106106
# TODO: Figure out somehow how to embed this one.
107107
- name: wayland-scanner dependency
108108
run: |
109-
sudo apt-get install libwayland-bin
109+
sudo apt-get install libwayland-bin libegl-dev
110110
111111
- name: Free disk space on runner
112112
run: |

doc/classes/DisplayServer.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,12 +2138,14 @@
21382138
<constant name="DISPLAY_HANDLE" value="0" enum="HandleType">
21392139
Display handle:
21402140
- Linux (X11): [code]X11::Display*[/code] for the display.
2141+
- Linux (Wayland): [code]wl_display[/code] for the display.
21412142
- Android: [code]EGLDisplay[/code] for the display.
21422143
</constant>
21432144
<constant name="WINDOW_HANDLE" value="1" enum="HandleType">
21442145
Window handle:
21452146
- Windows: [code]HWND[/code] for the window.
21462147
- Linux (X11): [code]X11::Window*[/code] for the window.
2148+
- Linux (Wayland): [code]wl_surface[/code] for the window.
21472149
- macOS: [code]NSWindow*[/code] for the window.
21482150
- iOS: [code]UIViewController*[/code] for the view controller.
21492151
- Android: [code]jObject[/code] for the activity.
@@ -2158,9 +2160,20 @@
21582160
OpenGL context (only with the GL Compatibility renderer):
21592161
- Windows: [code]HGLRC[/code] for the window (native GL), or [code]EGLContext[/code] for the window (ANGLE).
21602162
- Linux (X11): [code]GLXContext*[/code] for the window.
2163+
- Linux (Wayland): [code]EGLContext[/code] for the window.
21612164
- macOS: [code]NSOpenGLContext*[/code] for the window (native GL), or [code]EGLContext[/code] for the window (ANGLE).
21622165
- Android: [code]EGLContext[/code] for the window.
21632166
</constant>
2167+
<constant name="EGL_DISPLAY" value="4" enum="HandleType">
2168+
- Windows: [code]EGLDisplay[/code] for the window (ANGLE).
2169+
- macOS: [code]EGLDisplay[/code] for the window (ANGLE).
2170+
- Linux (Wayland): [code]EGLDisplay[/code] for the window.
2171+
</constant>
2172+
<constant name="EGL_CONFIG" value="5" enum="HandleType">
2173+
- Windows: [code]EGLConfig[/code] for the window (ANGLE).
2174+
- macOS: [code]EGLConfig[/code] for the window (ANGLE).
2175+
- Linux (Wayland): [code]EGLConfig[/code] for the window.
2176+
</constant>
21642177
<constant name="TTS_UTTERANCE_STARTED" value="0" enum="TTSUtteranceEvent">
21652178
Utterance has begun to be spoken.
21662179
</constant>

drivers/egl/egl_manager.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ EGLContext EGLManager::get_context(DisplayServer::WindowID p_window_id) {
351351
return display.egl_context;
352352
}
353353

354+
EGLDisplay EGLManager::get_display(DisplayServer::WindowID p_window_id) {
355+
GLWindow &glwindow = windows[p_window_id];
356+
357+
if (!glwindow.initialized) {
358+
return EGL_NO_CONTEXT;
359+
}
360+
361+
GLDisplay &display = displays[glwindow.gldisplay_id];
362+
363+
return display.egl_display;
364+
}
365+
366+
EGLConfig EGLManager::get_config(DisplayServer::WindowID p_window_id) {
367+
GLWindow &glwindow = windows[p_window_id];
368+
369+
if (!glwindow.initialized) {
370+
return nullptr;
371+
}
372+
373+
GLDisplay &display = displays[glwindow.gldisplay_id];
374+
375+
return display.egl_config;
376+
}
377+
354378
Error EGLManager::initialize(void *p_native_display) {
355379
#if defined(GLAD_ENABLED) && !defined(EGL_STATIC)
356380
// Loading EGL with a new display gets us just the bare minimum API. We'll then

drivers/egl/egl_manager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class EGLManager {
106106
bool is_using_vsync() const;
107107

108108
EGLContext get_context(DisplayServer::WindowID p_window_id);
109+
EGLDisplay get_display(DisplayServer::WindowID p_window_id);
110+
EGLConfig get_config(DisplayServer::WindowID p_window_id);
109111

110112
Error initialize(void *p_native_display = nullptr);
111113

modules/openxr/SCsub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ elif env["platform"] == "linuxbsd":
2626
env_openxr.AppendUnique(CPPDEFINES=["XR_USE_PLATFORM_XLIB"])
2727

2828
if env["wayland"]:
29-
env_openxr.AppendUnique(CPPDEFINES=["XR_USE_PLATFORM_WAYLAND"])
29+
env_openxr.AppendUnique(CPPDEFINES=["XR_USE_PLATFORM_EGL"])
3030

3131
# FIXME: Review what needs to be set for Android and macOS.
3232
env_openxr.AppendUnique(CPPDEFINES=["HAVE_SECURE_GETENV"])

modules/openxr/extensions/platform/openxr_opengl_extension.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ HashMap<String, bool *> OpenXROpenGLExtension::get_requested_extensions() {
6464
#else
6565
request_extensions[XR_KHR_OPENGL_ENABLE_EXTENSION_NAME] = nullptr;
6666
#endif
67+
#if defined(LINUXBSD_ENABLED) && defined(EGL_ENABLED)
68+
request_extensions[XR_MNDX_EGL_ENABLE_EXTENSION_NAME] = &egl_extension_enabled;
69+
#endif
6770

6871
return request_extensions;
6972
}
@@ -128,9 +131,14 @@ bool OpenXROpenGLExtension::check_graphics_api_support(XrVersion p_desired_versi
128131
XrGraphicsBindingOpenGLWin32KHR OpenXROpenGLExtension::graphics_binding_gl;
129132
#elif defined(ANDROID_ENABLED)
130133
XrGraphicsBindingOpenGLESAndroidKHR OpenXROpenGLExtension::graphics_binding_gl;
131-
#elif defined(X11_ENABLED)
134+
#elif defined(LINUXBSD_ENABLED)
135+
#ifdef X11_ENABLED
132136
XrGraphicsBindingOpenGLXlibKHR OpenXROpenGLExtension::graphics_binding_gl;
133137
#endif
138+
#ifdef EGL_ENABLED
139+
XrGraphicsBindingEGLMNDX OpenXROpenGLExtension::graphics_binding_egl;
140+
#endif
141+
#endif
134142

135143
void *OpenXROpenGLExtension::set_session_create_and_get_next_pointer(void *p_next_pointer) {
136144
XrVersion desired_version = XR_MAKE_VERSION(3, 3, 0);
@@ -142,10 +150,6 @@ void *OpenXROpenGLExtension::set_session_create_and_get_next_pointer(void *p_nex
142150

143151
DisplayServer *display_server = DisplayServer::get_singleton();
144152

145-
#ifdef WAYLAND_ENABLED
146-
ERR_FAIL_COND_V_MSG(display_server->get_name() == "Wayland", p_next_pointer, "OpenXR is not yet supported on OpenGL Wayland.");
147-
#endif
148-
149153
#ifdef WIN32
150154
graphics_binding_gl.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR,
151155
graphics_binding_gl.next = p_next_pointer;
@@ -159,7 +163,23 @@ void *OpenXROpenGLExtension::set_session_create_and_get_next_pointer(void *p_nex
159163
graphics_binding_gl.display = (void *)display_server->window_get_native_handle(DisplayServer::DISPLAY_HANDLE);
160164
graphics_binding_gl.config = (EGLConfig)0; // https://github.com/KhronosGroup/OpenXR-SDK-Source/blob/master/src/tests/hello_xr/graphicsplugin_opengles.cpp#L122
161165
graphics_binding_gl.context = (void *)display_server->window_get_native_handle(DisplayServer::OPENGL_CONTEXT);
162-
#elif defined(X11_ENABLED)
166+
#else
167+
#if defined(EGL_ENABLED) && defined(WAYLAND_ENABLED)
168+
if (display_server->get_name() == "Wayland") {
169+
ERR_FAIL_COND_V_MSG(!egl_extension_enabled, p_next_pointer, "OpenXR cannot initialize on Wayland without the XR_MNDX_egl_enable extension.");
170+
171+
graphics_binding_egl.type = XR_TYPE_GRAPHICS_BINDING_EGL_MNDX;
172+
graphics_binding_egl.next = p_next_pointer;
173+
174+
graphics_binding_egl.getProcAddress = eglGetProcAddress;
175+
graphics_binding_egl.display = (void *)display_server->window_get_native_handle(DisplayServer::EGL_DISPLAY);
176+
graphics_binding_egl.config = (void *)display_server->window_get_native_handle(DisplayServer::EGL_CONFIG);
177+
graphics_binding_egl.context = (void *)display_server->window_get_native_handle(DisplayServer::OPENGL_CONTEXT);
178+
179+
return &graphics_binding_egl;
180+
}
181+
#endif
182+
#if defined(X11_ENABLED)
163183
graphics_binding_gl.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
164184
graphics_binding_gl.next = p_next_pointer;
165185

@@ -175,8 +195,13 @@ void *OpenXROpenGLExtension::set_session_create_and_get_next_pointer(void *p_nex
175195
graphics_binding_gl.visualid = 0;
176196
graphics_binding_gl.glxFBConfig = 0;
177197
#endif
198+
#endif
178199

200+
#if defined(WIN32) || defined(ANDROID_ENABLED) || defined(X11_ENABLED)
179201
return &graphics_binding_gl;
202+
#else
203+
return p_next_pointer;
204+
#endif
180205
}
181206

182207
void OpenXROpenGLExtension::get_usable_swapchain_formats(Vector<int64_t> &p_usable_swap_chains) {

modules/openxr/extensions/platform/openxr_opengl_extension.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,17 @@ class OpenXROpenGLExtension : public OpenXRGraphicsExtensionWrapper {
6464
static XrGraphicsBindingOpenGLWin32KHR graphics_binding_gl;
6565
#elif defined(ANDROID_ENABLED)
6666
static XrGraphicsBindingOpenGLESAndroidKHR graphics_binding_gl;
67-
#else // Linux/X11
67+
#elif defined(LINUXBSD_ENABLED)
68+
#ifdef X11_ENABLED
6869
static XrGraphicsBindingOpenGLXlibKHR graphics_binding_gl;
70+
#endif
71+
#ifdef EGL_ENABLED
72+
static XrGraphicsBindingEGLMNDX graphics_binding_egl;
73+
74+
bool egl_extension_enabled = false;
75+
#endif
76+
#else
77+
#error "OpenXR with OpenGL isn't supported on this platform"
6978
#endif
7079

7180
struct SwapchainGraphicsData {

modules/openxr/openxr_platform_inc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
#else
5050
#define XR_USE_GRAPHICS_API_OPENGL
5151
#endif // ANDROID_ENABLED
52+
#if defined(LINUXBSD_ENABLED) && defined(EGL_ENABLED)
53+
#ifdef GLAD_ENABLED
54+
#include "thirdparty/glad/glad/egl.h"
55+
#else
56+
#include <EGL/egl.h>
57+
#endif // GLAD_ENABLED
58+
#endif // defined(LINUXBSD_ENABLED) && defined(EGL_ENABLED)
5259
#ifdef X11_ENABLED
5360
#define GL_GLEXT_PROTOTYPES 1
5461
#define GL3_PROTOTYPES 1

platform/android/display_server_android.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ int64_t DisplayServerAndroid::window_get_native_handle(HandleType p_handle_type,
382382
}
383383
return 0;
384384
}
385+
case EGL_DISPLAY: {
386+
// @todo Find a way to get this from the Java side.
387+
return 0;
388+
}
389+
case EGL_CONFIG: {
390+
// @todo Find a way to get this from the Java side.
391+
return 0;
392+
}
385393
#endif
386394
default: {
387395
return 0;

platform/linuxbsd/wayland/display_server_wayland.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,18 @@ int64_t DisplayServerWayland::window_get_native_handle(HandleType p_handle_type,
627627
}
628628
return 0;
629629
} break;
630+
case EGL_DISPLAY: {
631+
if (egl_manager) {
632+
return (int64_t)egl_manager->get_display(p_window);
633+
}
634+
return 0;
635+
}
636+
case EGL_CONFIG: {
637+
if (egl_manager) {
638+
return (int64_t)egl_manager->get_config(p_window);
639+
}
640+
return 0;
641+
}
630642
#endif // GLES3_ENABLED
631643

632644
default: {

0 commit comments

Comments
 (0)