Skip to content

Commit 73540ff

Browse files
belegdolmcourteauxbkaradzic
authored andcommitted
Cherry-pick wayland improvements from upstream bgfx (mamedev#13070)
* Properly support Wayland under EGL and Vulkan. (mamedev#3358) * Dynamically load libwayland-egl.so.1 when dealing with Wayland to remove dependencies at program startup. (mamedev#3359) * Cleanup. * Support both X11 and Wayland in the same build. (mamedev#3360) * Support both X11 and Wayland in the same build. - Works for both Vulkan and OpenGL. - Remove --with-wayland from genie options. - Vulkan loads all three extensions for surface creation instead of only one. - Add width and height parameter to GlContext::createSwapChain(), which is needed for EGL to create a SwapChain with the given window size. - Dirty-fix the example-22-windows to recreate the FrameBuffer by first destroying and then recreating to make sure the window is released of its swapchain. - Fix dbgText glitch in example-22-windows. - Remove old X11-related dependencies for GLFW3. * Formatting. * Adapt to latest bgfx wayland code * Cleanup. * Fix Vulkan swapchain invalidation issue. (mamedev#3379) * Fix Vulkan swapchain invalidation issue. * Always clamp render pass to frame buffer size. * Fix formatting. * Hopefully fix macOS build * Hopefully fix macOS build, attempt 2 --------- Co-authored-by: Martijn Courteaux <courteauxmartijn@gmail.com> Co-authored-by: Бранимир Караџић <branimirkaradzic@gmail.com>
1 parent 4b0f809 commit 73540ff

28 files changed

+321
-346
lines changed

3rdparty/bgfx/examples/22-windows/windows.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ class ExampleWindows : public entry::AppI
204204
m_fbh[viewId].idx = bgfx::kInvalidHandle;
205205
}
206206

207+
// Before we reattach a SwapChain to the window
208+
// we must actually free up the previous one.
209+
// The DestroyFrameBuffer command goes in the
210+
// cmdPost CommandBuffer, which happens after
211+
// the frame. The CreateFrameBuffer command goes
212+
// int the cmdPre CommandBuffer, which happens
213+
// at the beginning of the frame. Without this
214+
// bgfx::frame() call, the creation would happen
215+
// before it's destroyed, which would cause
216+
// the platform window to have two SwapChains
217+
// associated with it.
218+
// Ideally, we have an operation of ResizeFrameBuffer.
219+
bgfx::frame();
220+
207221
win.m_nwh = m_state.m_nwh;
208222
win.m_width = m_state.m_width;
209223
win.m_height = m_state.m_height;
@@ -276,6 +290,7 @@ class ExampleWindows : public entry::AppI
276290
int64_t now = bx::getHPCounter();
277291
float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
278292

293+
bgfx::dbgTextClear();
279294
if (NULL != m_bindings)
280295
{
281296
bgfx::dbgTextPrintf(0, 1, 0x2f, "Press 'c' to create or 'd' to destroy window.");

3rdparty/bgfx/examples/42-bunnylod/bunnylod.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ class ExampleBunnyLOD : public entry::AppI
6868
static void remapIndices(uint32_t* _indices, uint32_t _num)
6969
{
7070
uint32_t target = 0;
71-
for (uint32_t i = 0; i < _num; i++) {
71+
for (uint32_t i = 0; i < _num; i++)
72+
{
7273
uint32_t map = _indices[i];
73-
if (i != map) {
74+
if (i != map)
75+
{
7476
_indices[i] = _indices[map];
75-
} else {
77+
}
78+
else
79+
{
7680
_indices[i] = target;
7781
++target;
7882
}

3rdparty/bgfx/examples/common/entry/entry.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ BX_PRAGMA_DIAGNOSTIC_POP();
767767
handle = size->m_handle;
768768
_width = size->m_width;
769769
_height = size->m_height;
770+
BX_TRACE("Window resize event: %d: %dx%d", handle, _width, _height);
770771

771772
needReset = true;
772773
}
@@ -800,6 +801,7 @@ BX_PRAGMA_DIAGNOSTIC_POP();
800801
&& needReset)
801802
{
802803
_reset = s_reset;
804+
BX_TRACE("bgfx::reset(%d, %d, 0x%x)", _width, _height, _reset)
803805
bgfx::reset(_width, _height, _reset);
804806
inputSetMouseResolution(uint16_t(_width), uint16_t(_height) );
805807
}
@@ -979,6 +981,7 @@ BX_PRAGMA_DIAGNOSTIC_POP();
979981
if (needReset)
980982
{
981983
_reset = s_reset;
984+
BX_TRACE("bgfx::reset(%d, %d, 0x%x)", s_window[0].m_width, s_window[0].m_height, _reset)
982985
bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
983986
inputSetMouseResolution(uint16_t(s_window[0].m_width), uint16_t(s_window[0].m_height) );
984987
}

3rdparty/bgfx/examples/common/entry/entry_glfw.cpp

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
#endif // GLFW_VERSION_MINOR < 2
1616

1717
#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
18-
# if ENTRY_CONFIG_USE_WAYLAND
19-
# include <wayland-egl.h>
20-
# define GLFW_EXPOSE_NATIVE_WAYLAND
21-
# else
22-
# define GLFW_EXPOSE_NATIVE_X11
23-
# define GLFW_EXPOSE_NATIVE_GLX
24-
# endif
18+
# define GLFW_EXPOSE_NATIVE_WAYLAND
19+
# define GLFW_EXPOSE_NATIVE_X11
20+
# define GLFW_EXPOSE_NATIVE_GLX
2521
#elif BX_PLATFORM_OSX
2622
# define GLFW_EXPOSE_NATIVE_COCOA
2723
# define GLFW_EXPOSE_NATIVE_NSGL
@@ -45,46 +41,21 @@ namespace entry
4541
static void* glfwNativeWindowHandle(GLFWwindow* _window)
4642
{
4743
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
48-
# if ENTRY_CONFIG_USE_WAYLAND
49-
wl_egl_window *win_impl = (wl_egl_window*)glfwGetWindowUserPointer(_window);
50-
if(!win_impl)
44+
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
45+
{
46+
return glfwGetWaylandWindow(_window);
47+
}
48+
else
5149
{
52-
int width, height;
53-
glfwGetWindowSize(_window, &width, &height);
54-
struct wl_surface* surface = (struct wl_surface*)glfwGetWaylandWindow(_window);
55-
if(!surface)
56-
return nullptr;
57-
win_impl = wl_egl_window_create(surface, width, height);
58-
glfwSetWindowUserPointer(_window, (void*)(uintptr_t)win_impl);
50+
return (void*)(uintptr_t)glfwGetX11Window(_window);
5951
}
60-
return (void*)(uintptr_t)win_impl;
61-
# else
62-
return (void*)(uintptr_t)glfwGetX11Window(_window);
63-
# endif
6452
# elif BX_PLATFORM_OSX
6553
return glfwGetCocoaWindow(_window);
6654
# elif BX_PLATFORM_WINDOWS
6755
return glfwGetWin32Window(_window);
6856
# endif // BX_PLATFORM_
6957
}
7058

71-
static void glfwDestroyWindowImpl(GLFWwindow *_window)
72-
{
73-
if(!_window)
74-
return;
75-
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
76-
# if ENTRY_CONFIG_USE_WAYLAND
77-
wl_egl_window *win_impl = (wl_egl_window*)glfwGetWindowUserPointer(_window);
78-
if(win_impl)
79-
{
80-
glfwSetWindowUserPointer(_window, nullptr);
81-
wl_egl_window_destroy(win_impl);
82-
}
83-
# endif
84-
# endif
85-
glfwDestroyWindow(_window);
86-
}
87-
8859
static uint8_t translateKeyModifiers(int _glfw)
8960
{
9061
uint8_t modifiers = 0;
@@ -525,7 +496,7 @@ namespace entry
525496
{
526497
GLFWwindow* window = m_window[msg->m_handle.idx];
527498
m_eventQueue.postWindowEvent(msg->m_handle);
528-
glfwDestroyWindowImpl(window);
499+
glfwDestroyWindow(window);
529500
m_window[msg->m_handle.idx] = NULL;
530501
}
531502
}
@@ -617,7 +588,7 @@ namespace entry
617588
m_eventQueue.postExitEvent();
618589
m_thread.shutdown();
619590

620-
glfwDestroyWindowImpl(m_window[0]);
591+
glfwDestroyWindow(m_window[0]);
621592
glfwTerminate();
622593

623594
return m_thread.getExitCode();
@@ -865,11 +836,14 @@ namespace entry
865836
void* getNativeDisplayHandle()
866837
{
867838
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
868-
# if ENTRY_CONFIG_USE_WAYLAND
869-
return glfwGetWaylandDisplay();
870-
# else
871-
return glfwGetX11Display();
872-
# endif // ENTRY_CONFIG_USE_WAYLAND
839+
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
840+
{
841+
return glfwGetWaylandDisplay();
842+
}
843+
else
844+
{
845+
return glfwGetX11Display();
846+
}
873847
# else
874848
return NULL;
875849
# endif // BX_PLATFORM_*
@@ -878,11 +852,14 @@ namespace entry
878852
bgfx::NativeWindowHandleType::Enum getNativeWindowHandleType(WindowHandle _handle)
879853
{
880854
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
881-
# if ENTRY_CONFIG_USE_WAYLAND
882-
return bgfx::NativeWindowHandleType::Wayland;
883-
# else
884-
return bgfx::NativeWindowHandleType::Default;
885-
# endif // ENTRY_CONFIG_USE_WAYLAND
855+
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
856+
{
857+
return bgfx::NativeWindowHandleType::Wayland;
858+
}
859+
else
860+
{
861+
return bgfx::NativeWindowHandleType::Default;
862+
}
886863
# else
887864
return bgfx::NativeWindowHandleType::Default;
888865
# endif // BX_PLATFORM_*

3rdparty/bgfx/examples/common/entry/entry_p.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
# define ENTRY_CONFIG_USE_GLFW 0
2626
#endif // ENTRY_CONFIG_USE_GLFW
2727

28-
#ifndef ENTRY_CONFIG_USE_WAYLAND
29-
# define ENTRY_CONFIG_USE_WAYLAND 0
30-
#endif // ENTRY_CONFIG_USE_WAYLAND
31-
3228
#if !defined(ENTRY_CONFIG_USE_NATIVE) \
3329
&& !ENTRY_CONFIG_USE_NOOP \
3430
&& !ENTRY_CONFIG_USE_SDL \

3rdparty/bgfx/examples/common/entry/entry_sdl.cpp

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#if ENTRY_CONFIG_USE_SDL
99

1010
#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
11-
# if ENTRY_CONFIG_USE_WAYLAND
12-
# include <wayland-egl.h>
13-
# endif
1411
#elif BX_PLATFORM_WINDOWS
1512
# define SDL_MAIN_HANDLED
1613
#endif
@@ -49,25 +46,14 @@ namespace entry
4946
}
5047

5148
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
52-
# if ENTRY_CONFIG_USE_WAYLAND
53-
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
54-
{
55-
wl_egl_window *win_impl = (wl_egl_window*)SDL_GetWindowData(_window, "wl_egl_window");
56-
if(!win_impl)
57-
{
58-
int width, height;
59-
SDL_GetWindowSize(_window, &width, &height);
60-
struct wl_surface* surface = wmi.info.wl.surface;
61-
if(!surface)
62-
return nullptr;
63-
win_impl = wl_egl_window_create(surface, width, height);
64-
SDL_SetWindowData(_window, "wl_egl_window", win_impl);
65-
}
66-
return (void*)(uintptr_t)win_impl;
67-
}
68-
else
69-
# endif // ENTRY_CONFIG_USE_WAYLAND
70-
return (void*)wmi.info.x11.window;
49+
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
50+
{
51+
return (void*)wmi.info.wl.surface;
52+
}
53+
else
54+
{
55+
return (void*)wmi.info.x11.window;
56+
}
7157
# elif BX_PLATFORM_OSX || BX_PLATFORM_IOS
7258
return wmi.info.cocoa.window;
7359
# elif BX_PLATFORM_WINDOWS
@@ -77,23 +63,6 @@ namespace entry
7763
# endif // BX_PLATFORM_
7864
}
7965

80-
static void sdlDestroyWindow(SDL_Window* _window)
81-
{
82-
if(!_window)
83-
return;
84-
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
85-
# if ENTRY_CONFIG_USE_WAYLAND
86-
wl_egl_window *win_impl = (wl_egl_window*)SDL_GetWindowData(_window, "wl_egl_window");
87-
if(win_impl)
88-
{
89-
SDL_SetWindowData(_window, "wl_egl_window", nullptr);
90-
wl_egl_window_destroy(win_impl);
91-
}
92-
# endif
93-
# endif
94-
SDL_DestroyWindow(_window);
95-
}
96-
9766
static uint8_t translateKeyModifiers(uint16_t _sdl)
9867
{
9968
uint8_t modifiers = 0;
@@ -493,7 +462,7 @@ namespace entry
493462

494463
// Force window resolution...
495464
WindowHandle defaultWindow = { 0 };
496-
setWindowSize(defaultWindow, m_width, m_height, true);
465+
entry::setWindowSize(defaultWindow, m_width, m_height);
497466

498467
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
499468

@@ -671,7 +640,15 @@ namespace entry
671640
case SDL_WINDOWEVENT_SIZE_CHANGED:
672641
{
673642
WindowHandle handle = findHandle(wev.windowID);
674-
setWindowSize(handle, wev.data1, wev.data2);
643+
uint32_t width = wev.data1;
644+
uint32_t height = wev.data2;
645+
if (width != m_width
646+
|| height != m_height)
647+
{
648+
m_width = width;
649+
m_height = height;
650+
m_eventQueue.postSizeEvent(handle, m_width, m_height);
651+
}
675652
}
676653
break;
677654

@@ -865,7 +842,7 @@ namespace entry
865842
if (isValid(handle) )
866843
{
867844
m_eventQueue.postWindowEvent(handle);
868-
sdlDestroyWindow(m_window[handle.idx]);
845+
SDL_DestroyWindow(m_window[handle.idx]);
869846
m_window[handle.idx] = NULL;
870847
}
871848
}
@@ -916,7 +893,7 @@ namespace entry
916893
Msg* msg = (Msg*)uev.data2;
917894
if (isValid(handle) )
918895
{
919-
setWindowSize(handle, msg->m_width, msg->m_height);
896+
SDL_SetWindowSize(m_window[handle.idx], msg->m_width, msg->m_height);
920897
}
921898
delete msg;
922899
}
@@ -959,7 +936,7 @@ namespace entry
959936
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {};
960937
m_thread.shutdown();
961938

962-
sdlDestroyWindow(m_window[0]);
939+
SDL_DestroyWindow(m_window[0]);
963940
SDL_Quit();
964941

965942
return m_thread.getExitCode();
@@ -988,20 +965,6 @@ namespace entry
988965
return invalid;
989966
}
990967

991-
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height, bool _force = false)
992-
{
993-
if (_width != m_width
994-
|| _height != m_height
995-
|| _force)
996-
{
997-
m_width = _width;
998-
m_height = _height;
999-
1000-
SDL_SetWindowSize(m_window[_handle.idx], m_width, m_height);
1001-
m_eventQueue.postSizeEvent(_handle, m_width, m_height);
1002-
}
1003-
}
1004-
1005968
GamepadHandle findGamepad(SDL_JoystickID _jid)
1006969
{
1007970
for (uint32_t ii = 0, num = m_gamepadAlloc.getNumHandles(); ii < num; ++ii)
@@ -1102,6 +1065,7 @@ namespace entry
11021065

11031066
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
11041067
{
1068+
// Function to set the window size programmatically from the examples/tools.
11051069
Msg* msg = new Msg;
11061070
msg->m_width = _width;
11071071
msg->m_height = _height;
@@ -1149,12 +1113,10 @@ namespace entry
11491113
return NULL;
11501114
}
11511115
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
1152-
# if ENTRY_CONFIG_USE_WAYLAND
1153-
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
1154-
return wmi.info.wl.display;
1155-
else
1156-
# endif // ENTRY_CONFIG_USE_WAYLAND
1157-
return wmi.info.x11.display;
1116+
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
1117+
return wmi.info.wl.display;
1118+
else
1119+
return wmi.info.x11.display;
11581120
# else
11591121
return NULL;
11601122
# endif // BX_PLATFORM_*
@@ -1169,11 +1131,9 @@ namespace entry
11691131
return bgfx::NativeWindowHandleType::Default;
11701132
}
11711133
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
1172-
# if ENTRY_CONFIG_USE_WAYLAND
11731134
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
11741135
return bgfx::NativeWindowHandleType::Wayland;
11751136
else
1176-
# endif // ENTRY_CONFIG_USE_WAYLAND
11771137
return bgfx::NativeWindowHandleType::Default;
11781138
# else
11791139
return bgfx::NativeWindowHandleType::Default;

0 commit comments

Comments
 (0)