From bb73f49d90f93097d70c668b76a83df5eb3dd289 Mon Sep 17 00:00:00 2001 From: Jinlei Li Date: Thu, 18 Jan 2024 18:14:03 +0800 Subject: [PATCH] Update to wgpu 0.19 --- Cargo.lock | 43 ++++++++++++++---------- Cargo.toml | 7 ++-- app-surface/Cargo.toml | 2 +- app-surface/src/android.rs | 61 ++++++++++++++++++++++------------ app-surface/src/app_surface.rs | 7 ++-- app-surface/src/ios.rs | 1 + wgpu-in-app/Cargo.toml | 6 ++-- 7 files changed, 76 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 713fa60..fa4675d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,7 +155,7 @@ dependencies = [ [[package]] name = "app-surface" -version = "0.3.5" +version = "0.4.0" dependencies = [ "android_logger", "ash", @@ -574,11 +574,12 @@ checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" [[package]] name = "d3d12" -version = "0.7.0" -source = "git+https://github.com/gfx-rs/wgpu?rev=445fa6019b47079c9d336881dbee1c3be3ed4c38#445fa6019b47079c9d336881dbee1c3be3ed4c38" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e3d747f100290a1ca24b752186f61f6637e1deffe3bf6320de6fcb29510a307" dependencies = [ "bitflags 2.4.1", - "libloading 0.7.4", + "libloading 0.8.0", "winapi", ] @@ -787,8 +788,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "glow" -version = "0.13.0" -source = "git+https://github.com/grovesNL/glow.git?rev=29ff917a2b2ff7ce0a81b2cc5681de6d4735b36e#29ff917a2b2ff7ce0a81b2cc5681de6d4735b36e" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd348e04c43b32574f2de31c8bb397d96c9fcfa1371bd4ca6d8bdc464ab121b1" dependencies = [ "js-sys", "slotmap", @@ -1124,8 +1126,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "naga" -version = "0.14.2" -source = "git+https://github.com/gfx-rs/wgpu?rev=445fa6019b47079c9d336881dbee1c3be3ed4c38#445fa6019b47079c9d336881dbee1c3be3ed4c38" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8878eb410fc90853da3908aebfe61d73d26d4437ef850b70050461f939509899" dependencies = [ "bit-set", "bitflags 2.4.1", @@ -2117,8 +2120,9 @@ dependencies = [ [[package]] name = "wgpu" -version = "0.18.0" -source = "git+https://github.com/gfx-rs/wgpu?rev=445fa6019b47079c9d336881dbee1c3be3ed4c38#445fa6019b47079c9d336881dbee1c3be3ed4c38" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0b71d2ded29e2161db50ab731d6cb42c037bd7ab94864a98fa66ff36b4721a8" dependencies = [ "arrayvec", "cfg-if", @@ -2141,8 +2145,9 @@ dependencies = [ [[package]] name = "wgpu-core" -version = "0.18.0" -source = "git+https://github.com/gfx-rs/wgpu?rev=445fa6019b47079c9d336881dbee1c3be3ed4c38#445fa6019b47079c9d336881dbee1c3be3ed4c38" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b15e451d4060ada0d99a64df44e4d590213496da7c4f245572d51071e8e30ed" dependencies = [ "arrayvec", "bit-vec", @@ -2166,8 +2171,9 @@ dependencies = [ [[package]] name = "wgpu-hal" -version = "0.18.0" -source = "git+https://github.com/gfx-rs/wgpu?rev=445fa6019b47079c9d336881dbee1c3be3ed4c38#445fa6019b47079c9d336881dbee1c3be3ed4c38" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11f259ceb56727fb097da108d92f8a5cbdb5b74a77f9e396bd43626f67299d61" dependencies = [ "android_system_properties", "arrayvec", @@ -2187,7 +2193,7 @@ dependencies = [ "js-sys", "khronos-egl", "libc", - "libloading 0.7.4", + "libloading 0.8.0", "log", "metal", "naga", @@ -2209,7 +2215,7 @@ dependencies = [ [[package]] name = "wgpu-in-app" -version = "0.2.3" +version = "0.3.0" dependencies = [ "android_logger", "app-surface", @@ -2235,8 +2241,9 @@ dependencies = [ [[package]] name = "wgpu-types" -version = "0.18.0" -source = "git+https://github.com/gfx-rs/wgpu?rev=445fa6019b47079c9d336881dbee1c3be3ed4c38#445fa6019b47079c9d336881dbee1c3be3ed4c38" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "895fcbeb772bfb049eb80b2d6e47f6c9af235284e9703c96fc0218a42ffd5af2" dependencies = [ "bitflags 2.4.1", "js-sys", diff --git a/Cargo.toml b/Cargo.toml index b472311..d5674bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,13 +11,10 @@ log = "0.4" noise = { version = "0.8", default-features = false } pollster = "0.3" rand = "0.7.2" -# wgpu = "0.18" -wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "445fa6019b47079c9d336881dbee1c3be3ed4c38" } -# wgpu = { path = "../../forks/wgpu/wgpu" } +wgpu = "0.19" +# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "445fa6019b47079c9d336881dbee1c3be3ed4c38" } # wgpu = { git = "https://github.com/jinleili/wgpu", branch="visionOS" } - async-executor = "1.6" -# winit = "0.28.7" winit = { version = "0.29.10" } raw-window-handle = "0.6" env_logger = "0.10" diff --git a/app-surface/Cargo.toml b/app-surface/Cargo.toml index d0a0e61..179ad4b 100644 --- a/app-surface/Cargo.toml +++ b/app-surface/Cargo.toml @@ -3,7 +3,7 @@ name = "app-surface" authors = ["jinleili"] description = "Integrate wgpu into your existing iOS and Android apps." edition = "2021" -version = "0.3.5" +version = "0.4.0" rust-version = "1.65" repository = "https://github.com/jinleili/wgpu-in-app" keywords = ["android", "SurfaceView", "ios", "app", "wgpu"] diff --git a/app-surface/src/android.rs b/app-surface/src/android.rs index 0cf35b7..901828f 100644 --- a/app-surface/src/android.rs +++ b/app-surface/src/android.rs @@ -2,13 +2,13 @@ use core::ffi::c_void; use jni::sys::jobject; use jni::JNIEnv; use raw_window_handle::{ - AndroidDisplayHandle, AndroidNdkWindowHandle, HasRawDisplayHandle, HasRawWindowHandle, - RawDisplayHandle, RawWindowHandle, + AndroidDisplayHandle, AndroidNdkWindowHandle, DisplayHandle, HandleError, HasDisplayHandle, + HasWindowHandle, RawDisplayHandle, RawWindowHandle, WindowHandle, }; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; pub struct AppSurface { - pub native_window: NativeWindow, + pub native_window: Arc, pub scale_factor: f32, pub sdq: crate::SurfaceDeviceQueue, pub instance: wgpu::Instance, @@ -17,13 +17,16 @@ pub struct AppSurface { impl AppSurface { pub fn new(env: *mut JNIEnv, surface: jobject) -> Self { - let native_window = NativeWindow::new(env, surface); + let native_window = Arc::new(NativeWindow::new(env, surface)); let backends = wgpu::Backends::VULKAN; let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends, ..Default::default() }); - let surface = unsafe { instance.create_surface(&native_window).unwrap() }; + let handle: Box = Box::new(native_window.clone()); + let surface = instance + .create_surface(wgpu::SurfaceTarget::Window(handle)) + .unwrap(); let (adapter, device, queue) = pollster::block_on(crate::request_device(&instance, &surface)); @@ -38,6 +41,7 @@ impl AppSurface { present_mode: wgpu::PresentMode::Fifo, alpha_mode: caps.alpha_modes[0], view_formats: vec![], + desired_maximum_frame_latency: 2, }; surface.configure(&device, &config); @@ -65,7 +69,7 @@ impl AppSurface { } pub struct NativeWindow { - a_native_window: *mut ndk_sys::ANativeWindow, + a_native_window: Arc>, } impl NativeWindow { @@ -75,41 +79,56 @@ impl NativeWindow { // 此函数在返回 ANativeWindow 的同时会自动将其引用计数 +1,以防止该对象在安卓端被意外释放。 ndk_sys::ANativeWindow_fromSurface(env as *mut _, surface as *mut _) }; - Self { a_native_window } + Self { + a_native_window: Arc::new(Mutex::new(a_native_window)), + } } pub fn get_raw_window(&self) -> *mut ndk_sys::ANativeWindow { - self.a_native_window + let a_native_window = self.a_native_window.lock().unwrap(); + *a_native_window } fn get_width(&self) -> u32 { - unsafe { ndk_sys::ANativeWindow_getWidth(self.a_native_window) as u32 } + unsafe { ndk_sys::ANativeWindow_getWidth(*self.a_native_window.lock().unwrap()) as u32 } } fn get_height(&self) -> u32 { - unsafe { ndk_sys::ANativeWindow_getHeight(self.a_native_window) as u32 } + unsafe { ndk_sys::ANativeWindow_getHeight(*self.a_native_window.lock().unwrap()) as u32 } } } impl Drop for NativeWindow { fn drop(&mut self) { unsafe { - ndk_sys::ANativeWindow_release(self.a_native_window); + ndk_sys::ANativeWindow_release(*self.a_native_window.lock().unwrap()); } } } -unsafe impl HasRawWindowHandle for NativeWindow { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = AndroidNdkWindowHandle::new(NonNull::new( - self.a_native_window as *mut _ as *mut c_void, - )); - RawWindowHandle::AndroidNdk(handle) +impl HasWindowHandle for NativeWindow { + fn window_handle(&self) -> Result { + unsafe { + let a_native_window = self.a_native_window.lock().unwrap(); + let handle = AndroidNdkWindowHandle::new( + std::ptr::NonNull::new(*a_native_window as *mut _ as *mut c_void).unwrap(), + ); + Ok(WindowHandle::borrow_raw(RawWindowHandle::AndroidNdk( + handle, + ))) + } } } -unsafe impl HasRawDisplayHandle for NativeWindow { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Android(AndroidDisplayHandle::new()) +impl HasDisplayHandle for NativeWindow { + fn display_handle(&self) -> Result, HandleError> { + unsafe { + Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Android( + AndroidDisplayHandle::new(), + ))) + } } } + +unsafe impl Send for NativeWindow {} +unsafe impl Sync for NativeWindow {} diff --git a/app-surface/src/app_surface.rs b/app-surface/src/app_surface.rs index a8d9435..36f65fa 100644 --- a/app-surface/src/app_surface.rs +++ b/app-surface/src/app_surface.rs @@ -77,14 +77,14 @@ impl AppSurface { if #[cfg(target_arch = "wasm32")] { let surface = if is_offscreen_canvas { // let offscreen = canvas.transfer_control_to_offscreen().unwrap(); - instance.create_surface_from_offscreen_canvas( - view_setting.offscreen_canvas.unwrap(), + instance.create_surface( + wgpu::SurfaceTarget::OffscreenCanvas(view_setting.offscreen_canvas.unwrap()) ) } else { use winit::platform::web::WindowExtWebSys; let canvas: web_sys::HtmlCanvasElement = view.as_ref().canvas().unwrap(); - instance.create_surface_from_canvas(canvas) + instance.create_surface(wgpu::SurfaceTarget::Canvas(canvas)) }; } else { let surface = instance.create_surface(view.clone()); @@ -139,6 +139,7 @@ impl AppSurface { present_mode: wgpu::PresentMode::Fifo, alpha_mode, view_formats, + desired_maximum_frame_latency: 2, }; surface.configure(&device, &config); diff --git a/app-surface/src/ios.rs b/app-surface/src/ios.rs index 21d4471..e7b2485 100644 --- a/app-surface/src/ios.rs +++ b/app-surface/src/ios.rs @@ -67,6 +67,7 @@ impl AppSurface { present_mode: wgpu::PresentMode::Fifo, alpha_mode: wgpu::CompositeAlphaMode::PostMultiplied, view_formats: vec![], + desired_maximum_frame_latency: 2, }; surface.configure(&device, &config); AppSurface { diff --git a/wgpu-in-app/Cargo.toml b/wgpu-in-app/Cargo.toml index 3c7f2c2..7b611f5 100644 --- a/wgpu-in-app/Cargo.toml +++ b/wgpu-in-app/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "wgpu-in-app" authors = ["jinleili"] -version = "0.2.3" +version = "0.3.0" edition = "2021" rust-version = "1.65" @@ -165,8 +165,8 @@ web-sys = { workspace = true, features = [ "OffscreenCanvas", "ImageBitmap", "ImageBitmapRenderingContext", - "Window" + "Window", ] } wasm-bindgen.workspace = true js-sys.workspace = true -wasm-bindgen-futures.workspace = true \ No newline at end of file +wasm-bindgen-futures.workspace = true