diff --git a/Cargo.lock b/Cargo.lock index 5f2bfe34cd..fdc5a17b1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -972,6 +972,7 @@ dependencies = [ "strum", "wasm-bindgen-futures", "web-sys", + "web-time", "wgpu", "winit", ] diff --git a/examples/runners/wgpu/Cargo.toml b/examples/runners/wgpu/Cargo.toml index 9263d551ed..b4e240ebc3 100644 --- a/examples/runners/wgpu/Cargo.toml +++ b/examples/runners/wgpu/Cargo.toml @@ -26,6 +26,7 @@ winit = { version = "0.30.0", features = ["android-native-activity", "rwh_05"] } clap = { version = "4", features = ["derive"] } strum = { version = "0.27.2", default-features = false, features = ["std", "derive"] } bytemuck = "1.6.3" +web-time = "1.1.0" [target.'cfg(not(any(target_os = "android", target_arch = "wasm32")))'.dependencies] env_logger = "0.11.0" @@ -37,7 +38,7 @@ android_logger = "0.15.1" # to avoid specifying the dependency twice, but only applies to android builds. [target.'cfg(target_arch = "wasm32")'.dependencies] -web-sys = { version = "0.3.78", features = ["Performance"] } +web-sys = "0.3.78" console_error_panic_hook = "0.1.7" console_log = "1.0.0" wasm-bindgen-futures = "0.4.51" diff --git a/examples/runners/wgpu/src/graphics.rs b/examples/runners/wgpu/src/graphics.rs index 0934197c33..06554d0f33 100644 --- a/examples/runners/wgpu/src/graphics.rs +++ b/examples/runners/wgpu/src/graphics.rs @@ -24,53 +24,6 @@ mod shaders { include!(concat!(env!("OUT_DIR"), "/entry_points.rs")); } -/// Abstraction for getting timestamps even when `std::time` isn't supported. -enum PortableInstant { - #[cfg(not(target_arch = "wasm32"))] - Native(std::time::Instant), - - #[cfg(target_arch = "wasm32")] - Web { - performance_timestamp_ms: f64, - - // HACK(eddyb) cached `window().performance()` to speed up/simplify `elapsed`. - cached_window_performance: web_sys::Performance, - }, -} - -impl PortableInstant { - fn now() -> Self { - #[cfg(not(target_arch = "wasm32"))] - { - Self::Native(std::time::Instant::now()) - } - #[cfg(target_arch = "wasm32")] - { - let performance = web_sys::window() - .expect("missing window") - .performance() - .expect("missing window.performance"); - Self::Web { - performance_timestamp_ms: performance.now(), - cached_window_performance: performance, - } - } - } - - fn elapsed_secs_f32(&self) -> f32 { - match self { - #[cfg(not(target_arch = "wasm32"))] - Self::Native(instant) => instant.elapsed().as_secs_f32(), - - #[cfg(target_arch = "wasm32")] - Self::Web { - performance_timestamp_ms, - cached_window_performance, - } => ((cached_window_performance.now() - performance_timestamp_ms) / 1000.0) as f32, - } - } -} - fn mouse_button_index(button: MouseButton) -> usize { match button { MouseButton::Left => 0, @@ -243,7 +196,7 @@ async fn run( compiled_shader_modules, ); - let start = PortableInstant::now(); + let start = web_time::Instant::now(); let (mut cursor_x, mut cursor_y) = (0.0, 0.0); let (mut drag_start_x, mut drag_start_y) = (0.0, 0.0); @@ -363,7 +316,7 @@ async fn run( ..Default::default() }); - let time = start.elapsed_secs_f32(); + let time = start.elapsed().as_secs_f32(); for (i, press_time) in mouse_button_press_time.iter_mut().enumerate() { if (mouse_button_press_since_last_frame & (1 << i)) != 0 { *press_time = time;