Skip to content

Commit 3165da0

Browse files
committed
example-runner-wgpu: replace custom PortableInstant with web-time crate
1 parent d9bb8aa commit 3165da0

File tree

3 files changed

+5
-50
lines changed

3 files changed

+5
-50
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/runners/wgpu/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ winit = { version = "0.30.0", features = ["android-native-activity", "rwh_05"] }
2626
clap = { version = "4", features = ["derive"] }
2727
strum = { version = "0.27.2", default-features = false, features = ["std", "derive"] }
2828
bytemuck = "1.6.3"
29+
web-time = "1.1.0"
2930

3031
[target.'cfg(not(any(target_os = "android", target_arch = "wasm32")))'.dependencies]
3132
env_logger = "0.11.0"
@@ -37,7 +38,7 @@ android_logger = "0.15.1"
3738
# to avoid specifying the dependency twice, but only applies to android builds.
3839

3940
[target.'cfg(target_arch = "wasm32")'.dependencies]
40-
web-sys = { version = "0.3.78", features = ["Performance"] }
41+
web-sys = "0.3.78"
4142
console_error_panic_hook = "0.1.7"
4243
console_log = "1.0.0"
4344
wasm-bindgen-futures = "0.4.51"

examples/runners/wgpu/src/graphics.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,53 +24,6 @@ mod shaders {
2424
include!(concat!(env!("OUT_DIR"), "/entry_points.rs"));
2525
}
2626

27-
/// Abstraction for getting timestamps even when `std::time` isn't supported.
28-
enum PortableInstant {
29-
#[cfg(not(target_arch = "wasm32"))]
30-
Native(std::time::Instant),
31-
32-
#[cfg(target_arch = "wasm32")]
33-
Web {
34-
performance_timestamp_ms: f64,
35-
36-
// HACK(eddyb) cached `window().performance()` to speed up/simplify `elapsed`.
37-
cached_window_performance: web_sys::Performance,
38-
},
39-
}
40-
41-
impl PortableInstant {
42-
fn now() -> Self {
43-
#[cfg(not(target_arch = "wasm32"))]
44-
{
45-
Self::Native(std::time::Instant::now())
46-
}
47-
#[cfg(target_arch = "wasm32")]
48-
{
49-
let performance = web_sys::window()
50-
.expect("missing window")
51-
.performance()
52-
.expect("missing window.performance");
53-
Self::Web {
54-
performance_timestamp_ms: performance.now(),
55-
cached_window_performance: performance,
56-
}
57-
}
58-
}
59-
60-
fn elapsed_secs_f32(&self) -> f32 {
61-
match self {
62-
#[cfg(not(target_arch = "wasm32"))]
63-
Self::Native(instant) => instant.elapsed().as_secs_f32(),
64-
65-
#[cfg(target_arch = "wasm32")]
66-
Self::Web {
67-
performance_timestamp_ms,
68-
cached_window_performance,
69-
} => ((cached_window_performance.now() - performance_timestamp_ms) / 1000.0) as f32,
70-
}
71-
}
72-
}
73-
7427
fn mouse_button_index(button: MouseButton) -> usize {
7528
match button {
7629
MouseButton::Left => 0,
@@ -243,7 +196,7 @@ async fn run(
243196
compiled_shader_modules,
244197
);
245198

246-
let start = PortableInstant::now();
199+
let start = web_time::Instant::now();
247200

248201
let (mut cursor_x, mut cursor_y) = (0.0, 0.0);
249202
let (mut drag_start_x, mut drag_start_y) = (0.0, 0.0);
@@ -363,7 +316,7 @@ async fn run(
363316
..Default::default()
364317
});
365318

366-
let time = start.elapsed_secs_f32();
319+
let time = start.elapsed().as_secs_f32();
367320
for (i, press_time) in mouse_button_press_time.iter_mut().enumerate() {
368321
if (mouse_button_press_since_last_frame & (1 << i)) != 0 {
369322
*press_time = time;

0 commit comments

Comments
 (0)