From fc85128c411af240476b085ec25ca8ad5d440bd6 Mon Sep 17 00:00:00 2001 From: Park Joon-Kyu Date: Thu, 2 Jan 2020 17:10:04 +0900 Subject: [PATCH 1/2] Expose FFI for Wayland --- ffi/wgpu.h | 6 ++++-- wgpu-native/src/device.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ffi/wgpu.h b/ffi/wgpu.h index 5b40400fdf..82864978ce 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -/* Generated with cbindgen:0.11.1 */ +/* Generated with cbindgen:0.12.1 */ /* DO NOT MODIFY THIS MANUALLY! This file was generated using cbindgen. * To generate this file: @@ -741,9 +741,11 @@ void wgpu_compute_pass_set_pipeline(WGPUComputePassId pass_id, WGPUComputePipeli WGPUSurfaceId wgpu_create_surface_from_metal_layer(void *layer); +WGPUSurfaceId wgpu_create_surface_from_wayland(void *surface, void *display); + WGPUSurfaceId wgpu_create_surface_from_windows_hwnd(void *_hinstance, void *hwnd); -WGPUSurfaceId wgpu_create_surface_from_xlib(const void **display, uint64_t window); +WGPUSurfaceId wgpu_create_surface_from_xlib(const void **display, unsigned long window); WGPUBindGroupId wgpu_device_create_bind_group(WGPUDeviceId device_id, const WGPUBindGroupDescriptor *desc); diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 32553b1fb3..7ad656bd73 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -90,6 +90,19 @@ pub extern "C" fn wgpu_create_surface_from_xlib( })) } +#[cfg(all(unix, not(target_os = "ios"), not(target_os = "macos")))] +#[no_mangle] +pub extern "C" fn wgpu_create_surface_from_wayland( + surface: *mut std::ffi::c_void, + display: *mut std::ffi::c_void, +) -> id::SurfaceId { + use raw_window_handle::unix::WaylandHandle; + wgpu_create_surface(raw_window_handle::RawWindowHandle::Wayland(WaylandHandle { + surface, display, + ..WaylandHandle::empty() + })) +} + #[cfg(any(target_os = "ios", target_os = "macos"))] #[no_mangle] pub extern "C" fn wgpu_create_surface_from_metal_layer( From e91cfa4dc3341dfc2d75015dd346694f34a54173 Mon Sep 17 00:00:00 2001 From: Park Joon-Kyu Date: Thu, 2 Jan 2020 17:10:45 +0900 Subject: [PATCH 2/2] Enable wayland support for examples/triangle --- examples/triangle/CMakeLists.txt | 6 +++++- examples/triangle/main.c | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/triangle/CMakeLists.txt b/examples/triangle/CMakeLists.txt index e02d478a59..de2d6bad52 100644 --- a/examples/triangle/CMakeLists.txt +++ b/examples/triangle/CMakeLists.txt @@ -15,7 +15,11 @@ elseif(APPLE) set(OS_LIBRARIES "-framework Cocoa" "-framework CoreVideo" "-framework IOKit" "-framework QuartzCore") target_compile_options(${TARGET_NAME} PRIVATE -x objective-c) else(MSVC) - add_definitions(-DWGPU_TARGET=WGPU_TARGET_LINUX) +if(USE_WAYLAND) + add_definitions(-DWGPU_TARGET=WGPU_TARGET_LINUX_WAYLAND) +else(USE_WAYLAND) + add_definitions(-DWGPU_TARGET=WGPU_TARGET_LINUX_X11) +endif(USE_WAYLAND) target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic) endif(MSVC) diff --git a/examples/triangle/main.c b/examples/triangle/main.c index e8c624f9ad..05eab3e59a 100644 --- a/examples/triangle/main.c +++ b/examples/triangle/main.c @@ -12,8 +12,9 @@ #include #define WGPU_TARGET_MACOS 1 -#define WGPU_TARGET_LINUX 2 +#define WGPU_TARGET_LINUX_X11 2 #define WGPU_TARGET_WINDOWS 3 +#define WGPU_TARGET_LINUX_WAYLAND 4 #if WGPU_TARGET == WGPU_TARGET_MACOS #include @@ -23,8 +24,9 @@ #include #if WGPU_TARGET == WGPU_TARGET_MACOS #define GLFW_EXPOSE_NATIVE_COCOA -#elif WGPU_TARGET == WGPU_TARGET_LINUX +#elif WGPU_TARGET == WGPU_TARGET_LINUX_X11 #define GLFW_EXPOSE_NATIVE_X11 +#elif WGPU_TARGET == WGPU_TARGET_LINUX_WAYLAND #define GLFW_EXPOSE_NATIVE_WAYLAND #elif WGPU_TARGET == WGPU_TARGET_WINDOWS #define GLFW_EXPOSE_NATIVE_WIN32 @@ -174,12 +176,18 @@ int main() { [ns_window.contentView setLayer:metal_layer]; surface = wgpu_create_surface_from_metal_layer(metal_layer); } -#elif WGPU_TARGET == WGPU_TARGET_LINUX +#elif WGPU_TARGET == WGPU_TARGET_LINUX_X11 { Display *x11_display = glfwGetX11Display(); Window x11_window = glfwGetX11Window(window); surface = wgpu_create_surface_from_xlib((const void **)x11_display, x11_window); } +#elif WGPU_TARGET == WGPU_TARGET_LINUX_WAYLAND + { + struct wl_display *wayland_display = glfwGetWaylandDisplay(); + struct wl_surface *wayland_surface = glfwGetWaylandWindow(window); + surface = wgpu_create_surface_from_wayland(wayland_surface, wayland_display); + } #elif WGPU_TARGET == WGPU_TARGET_WINDOWS { HWND hwnd = glfwGetWin32Window(window);