Skip to content

Commit

Permalink
Support both X11 and Wayland in the same build.
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
mcourteaux committed Oct 4, 2024
1 parent 3f9fe0d commit 9a077da
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 196 deletions.
15 changes: 15 additions & 0 deletions examples/22-windows/windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ class ExampleWindows : public entry::AppI
m_fbh[viewId].idx = bgfx::kInvalidHandle;
}

// Before we reattach a SwapChain to the window
// we must actually free up the previous one.
// The DestroyFrameBuffer command goes in the
// cmdPost CommandBuffer, which happens after
// the frame. The CreateFrameBuffer command goes
// int the cmdPre CommandBuffer, which happens
// at the beginning of the frame. Without this
// bgfx::frame() call, the creation would happen
// before it's destroyed, which would cause
// the platform window to have two SwapChains
// associated with it.
// Ideally, we have an operation of ResizeFrameBuffer.
bgfx::frame();

win.m_nwh = m_state.m_nwh;
win.m_width = m_state.m_width;
win.m_height = m_state.m_height;
Expand Down Expand Up @@ -276,6 +290,7 @@ class ExampleWindows : public entry::AppI
int64_t now = bx::getHPCounter();
float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );

bgfx::dbgTextClear();
if (NULL != m_bindings)
{
bgfx::dbgTextPrintf(0, 1, 0x2f, "Press 'c' to create or 'd' to destroy window.");
Expand Down
68 changes: 26 additions & 42 deletions examples/common/entry/entry_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
#endif // GLFW_VERSION_MINOR < 2

#if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
# include <wayland-egl.h>
# define GLFW_EXPOSE_NATIVE_WAYLAND
# else
# define GLFW_EXPOSE_NATIVE_X11
# define GLFW_EXPOSE_NATIVE_GLX
# endif
# define GLFW_EXPOSE_NATIVE_WAYLAND
# define GLFW_EXPOSE_NATIVE_X11
# define GLFW_EXPOSE_NATIVE_GLX
#elif BX_PLATFORM_OSX
# define GLFW_EXPOSE_NATIVE_COCOA
# define GLFW_EXPOSE_NATIVE_NSGL
Expand All @@ -45,36 +41,20 @@ namespace entry
static void* glfwNativeWindowHandle(GLFWwindow* _window)
{
# if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
struct wl_surface* surface = (struct wl_surface*)glfwGetWaylandWindow(_window);
return (void*)surface;
# else
return (void*)(uintptr_t)glfwGetX11Window(_window);
# endif
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
{
return glfwGetWaylandWindow(_window);
} else
{
return (void*)(uintptr_t)glfwGetX11Window(_window);
}
# elif BX_PLATFORM_OSX
return glfwGetCocoaWindow(_window);
# elif BX_PLATFORM_WINDOWS
return glfwGetWin32Window(_window);
# endif // BX_PLATFORM_
}

static void glfwDestroyWindowImpl(GLFWwindow *_window)
{
if(!_window)
return;
# if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
wl_egl_window *win_impl = (wl_egl_window*)glfwGetWindowUserPointer(_window);
if(win_impl)
{
glfwSetWindowUserPointer(_window, nullptr);
wl_egl_window_destroy(win_impl);
}
# endif
# endif
glfwDestroyWindow(_window);
}

static uint8_t translateKeyModifiers(int _glfw)
{
uint8_t modifiers = 0;
Expand Down Expand Up @@ -515,7 +495,7 @@ namespace entry
{
GLFWwindow* window = m_window[msg->m_handle.idx];
m_eventQueue.postWindowEvent(msg->m_handle);
glfwDestroyWindowImpl(window);
glfwDestroyWindow(window);
m_window[msg->m_handle.idx] = NULL;
}
}
Expand Down Expand Up @@ -607,7 +587,7 @@ namespace entry
m_eventQueue.postExitEvent();
m_thread.shutdown();

glfwDestroyWindowImpl(m_window[0]);
glfwDestroyWindow(m_window[0]);
glfwTerminate();

return m_thread.getExitCode();
Expand Down Expand Up @@ -855,11 +835,13 @@ namespace entry
void* getNativeDisplayHandle()
{
# if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
return glfwGetWaylandDisplay();
# else
return glfwGetX11Display();
# endif // ENTRY_CONFIG_USE_WAYLAND
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
{
return glfwGetWaylandDisplay();
} else
{
return glfwGetX11Display();
}
# else
return NULL;
# endif // BX_PLATFORM_*
Expand All @@ -868,11 +850,13 @@ namespace entry
bgfx::NativeWindowHandleType::Enum getNativeWindowHandleType()
{
# if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
return bgfx::NativeWindowHandleType::Wayland;
# else
return bgfx::NativeWindowHandleType::Default;
# endif // ENTRY_CONFIG_USE_WAYLAND
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
{
return bgfx::NativeWindowHandleType::Wayland;
} else
{
return bgfx::NativeWindowHandleType::Default;
}
# else
return bgfx::NativeWindowHandleType::Default;
# endif // BX_PLATFORM_*
Expand Down
4 changes: 0 additions & 4 deletions examples/common/entry/entry_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
# define ENTRY_CONFIG_USE_GLFW 0
#endif // ENTRY_CONFIG_USE_GLFW

#ifndef ENTRY_CONFIG_USE_WAYLAND
# define ENTRY_CONFIG_USE_WAYLAND 0
#endif // ENTRY_CONFIG_USE_WAYLAND

#if !defined(ENTRY_CONFIG_USE_NATIVE) \
&& !ENTRY_CONFIG_USE_NOOP \
&& !ENTRY_CONFIG_USE_SDL \
Expand Down
33 changes: 10 additions & 23 deletions examples/common/entry/entry_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ namespace entry
}

# if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
return (void*)wmi.info.wl.surface;
else
# endif // ENTRY_CONFIG_USE_WAYLAND
return (void*)wmi.info.x11.window;
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
return (void*)wmi.info.wl.surface;
else
return (void*)wmi.info.x11.window;
# elif BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
return wmi.info.cocoa.window;
# elif BX_PLATFORM_WINDOWS
Expand All @@ -61,13 +59,6 @@ namespace entry
# endif // BX_PLATFORM_
}

static void sdlDestroyWindow(SDL_Window* _window)
{
if(!_window)
return;
SDL_DestroyWindow(_window);
}

static uint8_t translateKeyModifiers(uint16_t _sdl)
{
uint8_t modifiers = 0;
Expand Down Expand Up @@ -779,7 +770,7 @@ namespace entry
if (isValid(handle) )
{
m_eventQueue.postWindowEvent(handle);
sdlDestroyWindow(m_window[handle.idx]);
SDL_DestroyWindow(m_window[handle.idx]);
m_window[handle.idx] = NULL;
}
}
Expand Down Expand Up @@ -873,7 +864,7 @@ namespace entry
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {};
m_thread.shutdown();

sdlDestroyWindow(m_window[0]);
SDL_DestroyWindow(m_window[0]);
SDL_Quit();

return m_thread.getExitCode();
Expand Down Expand Up @@ -1063,12 +1054,10 @@ namespace entry
return NULL;
}
# if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
return wmi.info.wl.display;
else
# endif // ENTRY_CONFIG_USE_WAYLAND
return wmi.info.x11.display;
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
return wmi.info.wl.display;
else
return wmi.info.x11.display;
# else
return NULL;
# endif // BX_PLATFORM_*
Expand All @@ -1083,13 +1072,11 @@ namespace entry
return bgfx::NativeWindowHandleType::Default;
}
# if BX_PLATFORM_LINUX
# if ENTRY_CONFIG_USE_WAYLAND
if (wmi.subsystem == SDL_SYSWM_WAYLAND)
{
return bgfx::NativeWindowHandleType::Wayland;
}
else
# endif // ENTRY_CONFIG_USE_WAYLAND
{
return bgfx::NativeWindowHandleType::Default;
}
Expand Down
6 changes: 0 additions & 6 deletions scripts/example-common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ project ("example-common")
}
end

if _OPTIONS["with-wayland"] then
defines {
"ENTRY_CONFIG_USE_WAYLAND=1",
}
end

configuration { "android-*" }
includedirs {
path.join(BGFX_DIR, "3rdparty/native_app_glue")
Expand Down
18 changes: 0 additions & 18 deletions scripts/genie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ newoption {
description = "Enable GLFW entry.",
}

newoption {
trigger = "with-wayland",
description = "Enable Wayland support.",
}

newoption {
trigger = "with-profiler",
description = "Enable build with intrusive profiler.",
Expand Down Expand Up @@ -187,10 +182,6 @@ end
function copyLib()
end

if _OPTIONS["with-wayland"] then
defines { "WL_EGL_PLATFORM=1" }
end

if _OPTIONS["with-sdl"] then
if os.is("windows") then
if not os.getenv("SDL2_DIR") then
Expand Down Expand Up @@ -245,15 +236,6 @@ function exampleProjectDefaults()
defines { "ENTRY_CONFIG_USE_GLFW=1" }
links { "glfw3" }

configuration { "linux or freebsd" }
links {
"Xrandr",
"Xinerama",
"Xi",
"Xxf86vm",
"Xcursor",
}

configuration { "osx*" }
linkoptions {
"-framework CoreVideo",
Expand Down
22 changes: 0 additions & 22 deletions scripts/geometryv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ project ("geometryv")
defines { "ENTRY_CONFIG_USE_SDL=1" }
links { "SDL2" }

configuration { "linux or freebsd" }
if _OPTIONS["with-wayland"] then
links {
"wayland-egl",
}
end

configuration { "x32", "windows" }
libdirs { "$(SDL2_DIR)/lib/x86" }

Expand All @@ -55,21 +48,6 @@ project ("geometryv")
defines { "ENTRY_CONFIG_USE_GLFW=1" }
links { "glfw3" }

configuration { "linux or freebsd" }
if _OPTIONS["with-wayland"] then
links {
"wayland-egl",
}
else
links {
"Xrandr",
"Xinerama",
"Xi",
"Xxf86vm",
"Xcursor",
}
end

configuration { "osx*" }
linkoptions {
"-framework CoreVideo",
Expand Down
22 changes: 0 additions & 22 deletions scripts/texturev.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ project "texturev"
defines { "ENTRY_CONFIG_USE_SDL=1" }
links { "SDL2" }

configuration { "linux or freebsd" }
if _OPTIONS["with-wayland"] then
links {
"wayland-egl",
}
end

configuration { "x32", "windows" }
libdirs { "$(SDL2_DIR)/lib/x86" }

Expand All @@ -55,21 +48,6 @@ project "texturev"
defines { "ENTRY_CONFIG_USE_GLFW=1" }
links { "glfw3" }

configuration { "linux or freebsd" }
if _OPTIONS["with-wayland"] then
links {
"wayland-egl",
}
else
links {
"Xrandr",
"Xinerama",
"Xi",
"Xxf86vm",
"Xcursor",
}
end

configuration { "osx*" }
linkoptions {
"-framework CoreVideo",
Expand Down
Loading

0 comments on commit 9a077da

Please sign in to comment.