diff --git a/Makefile b/Makefile index fb30ee84c..16ca7442c 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ lib-rust: Cargo.lock wgpu-rs/Cargo.toml $(wildcard wgpu-rs/**/*.rs) cargo build --manifest-path wgpu-rs/Cargo.toml --features $(FEATURE_RUST) wgpu-bindings/wgpu.h: Cargo.lock wgpu-bindings/src/*.rs lib-native - cargo run --manifest-path wgpu-bindings/Cargo.toml + cargo +nightly-2018-12-27 run --manifest-path wgpu-bindings/Cargo.toml examples-native: lib-native wgpu-bindings/wgpu.h $(wildcard wgpu-native/**/*.c) $(MAKE) -C examples diff --git a/examples/hello_triangle_c/CMakeLists.txt b/examples/hello_triangle_c/CMakeLists.txt index ad6a9e6b2..204b0c468 100644 --- a/examples/hello_triangle_c/CMakeLists.txt +++ b/examples/hello_triangle_c/CMakeLists.txt @@ -8,12 +8,16 @@ add_executable(hello_triangle main.c) if(MSVC) target_compile_options(${TARGET_NAME} PRIVATE /W4) + add_compile_definitions(WGPU_TARGET=WGPU_TARGET_WINDOWS) + set(GLFW_LIBRARY glfw3) else(MSVC) target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic) + add_compile_definitions(WGPU_TARGET=WGPU_TARGET_LINUX) + set(GLFW_LIBRARY glfw) endif(MSVC) if(APPLE) - add_compile_definitions(WGPU_TARGET WGPU_TARGET_MACOS) + add_compile_definitions(WGPU_TARGET=WGPU_TARGET_MACOS) set(OS_LIBRARIES "-framework Cocoa" "-framework CoreVideo" "-framework IOKit" "-framework QuartzCore") target_compile_options(${TARGET_NAME} PRIVATE -x objective-c) endif(APPLE) @@ -24,4 +28,4 @@ find_library(WGPU_LIBRARY wgpu_native HINTS "${CMAKE_CURRENT_SOURCE_DIR}/../../target/debug" ) -target_link_libraries(${TARGET_NAME} glfw3 ${WGPU_LIBRARY} ${OS_LIBRARIES}) +target_link_libraries(${TARGET_NAME} ${GLFW_LIBRARY} ${WGPU_LIBRARY} ${OS_LIBRARIES}) diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index 415749e4c..4a130ff41 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -14,7 +14,8 @@ #if WGPU_TARGET == WGPU_TARGET_MACOS #define GLFW_EXPOSE_NATIVE_COCOA #elif WGPU_TARGET == WGPU_TARGET_LINUX -// TODO +#define GLFW_EXPOSE_NATIVE_X11 +#define GLFW_EXPOSE_NATIVE_WAYLAND #elif WGPU_TARGET == WGPU_TARGET_WINDOWS #define GLFW_EXPOSE_NATIVE_WIN32 #endif @@ -187,7 +188,9 @@ int main() } #elif WGPU_TARGET == WGPU_TARGET_LINUX { - // TODO + Display* x11_display = glfwGetX11Display(); + Window x11_window = glfwGetX11Window(window); + surface = wgpu_instance_create_surface_from_xlib(instance, (const void**)x11_display, x11_window); } #elif WGPU_TARGET == WGPU_TARGET_WINDOWS { diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index e5308f063..15b1a4604 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -513,6 +513,10 @@ WGPUSurfaceId wgpu_instance_create_surface_from_windows_hwnd(WGPUInstanceId inst void *hinstance, void *hwnd); +WGPUSurfaceId wgpu_instance_create_surface_from_xlib(WGPUInstanceId instance_id, + const void **display, + uint64_t window); + WGPUAdapterId wgpu_instance_get_adapter(WGPUInstanceId instance_id, const WGPUAdapterDescriptor *desc); diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 638482604..79f7ad1a6 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -63,6 +63,32 @@ pub extern "C" fn wgpu_instance_create_surface_from_winit( .register(surface) } +#[allow(unused)] +#[no_mangle] +pub extern "C" fn wgpu_instance_create_surface_from_xlib( + instance_id: InstanceId, + display: *mut *const std::ffi::c_void, + window: u64, +) -> SurfaceId { + #[cfg(not(feature = "gfx-backend-vulkan"))] + unimplemented!(); + + #[cfg(feature = "gfx-backend-vulkan")] + { + let raw = HUB.instances + .read() + .get(instance_id) + .create_surface_from_xlib(display, window); + let surface = Surface { + raw, + }; + + HUB.surfaces + .write() + .register(surface) + } +} + #[allow(unused)] #[no_mangle] pub extern "C" fn wgpu_instance_create_surface_from_macos_layer( @@ -95,7 +121,7 @@ pub extern "C" fn wgpu_instance_create_surface_from_windows_hwnd( hinstance: *mut std::ffi::c_void, hwnd: *mut std::ffi::c_void, ) -> SurfaceId { - #[cfg(not(any(feature = "gfx-backend-dx11", feature = "gfx-backend-dx12", feature = "gfx-backend-vulkan")))] + #[cfg(not(target_os = "windows"))] unimplemented!(); #[cfg(any(feature = "gfx-backend-dx11", feature = "gfx-backend-dx12"))] @@ -114,13 +140,13 @@ pub extern "C" fn wgpu_instance_create_surface_from_windows_hwnd( .register(surface) } - #[cfg(any(feature = "gfx-backend-vulkan"))] + #[cfg(all(target_os = "windows", feature = "gfx-backend-vulkan"))] { let raw = HUB.instances .read() .get(instance_id) .create_surface_from_hwnd(hinstance, hwnd); - + let surface = Surface { raw, };