Fix frame drops on windows due to sleep timings being too coarse #2

Closed
opened 2022-05-08 17:03:44 +02:00 by dranke · 0 comments
Owner
From 52896f6a40dd494c74d42cd53f6ce3dab32eb678 Mon Sep 17 00:00:00 2001
From: Dennis Ranke <dennis.ranke@gmail.com>
Date: Sun, 8 May 2022 16:59:59 +0200
Subject: [PATCH] improve sleep timer resolution on windows

---
 Cargo.lock        | 1 +
 Cargo.toml        | 5 +++--
 src/run_native.rs | 3 +++
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index e330f73..2ac8515 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2655,6 +2655,7 @@ dependencies = [
  "wasmtime",
  "wat",
  "webbrowser",
+ "winapi 0.3.9",
 ]
 
 [[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 6b67f29..54eb4e2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,7 +7,7 @@ edition = "2021"
 
 [features]
 default = ["native", "browser"]
-native = ["wasmtime", "minifb", "cpal", "rubato"]
+native = ["wasmtime", "minifb", "cpal", "rubato", "winapi" ]
 browser = ["warp", "tokio", "tokio-stream", "webbrowser"]
 
 [dependencies]
@@ -26,4 +26,5 @@ tokio-stream = { version = "0.1.8", features = ["sync"], optional = true }
 webbrowser = { version = "0.6.0", optional = true }
 ansi_term = "0.12.1"
 cpal = { version = "0.13.5", optional = true }
-rubato = { version = "0.11.0", optional = true }
\ No newline at end of file
+rubato = { version = "0.11.0", optional = true }
+winapi = { version = "0.3.9", features = ["timeapi"], optional = true }
\ No newline at end of file
diff --git a/src/run_native.rs b/src/run_native.rs
index 8502253..fcd142b 100644
--- a/src/run_native.rs
+++ b/src/run_native.rs
@@ -59,6 +59,9 @@ struct UW8WatchDog {
 
 impl MicroW8 {
     pub fn new() -> Result<MicroW8> {
+        #[cfg(target_os = "windows")]
+        unsafe { winapi::um::timeapi::timeBeginPeriod(1); }
+
         let engine = wasmtime::Engine::new(wasmtime::Config::new().interruptable(true))?;
 
         let loader_module =
-- 
2.35.1.windows.2

``` From 52896f6a40dd494c74d42cd53f6ce3dab32eb678 Mon Sep 17 00:00:00 2001 From: Dennis Ranke <dennis.ranke@gmail.com> Date: Sun, 8 May 2022 16:59:59 +0200 Subject: [PATCH] improve sleep timer resolution on windows --- Cargo.lock | 1 + Cargo.toml | 5 +++-- src/run_native.rs | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e330f73..2ac8515 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2655,6 +2655,7 @@ dependencies = [ "wasmtime", "wat", "webbrowser", + "winapi 0.3.9", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6b67f29..54eb4e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [features] default = ["native", "browser"] -native = ["wasmtime", "minifb", "cpal", "rubato"] +native = ["wasmtime", "minifb", "cpal", "rubato", "winapi" ] browser = ["warp", "tokio", "tokio-stream", "webbrowser"] [dependencies] @@ -26,4 +26,5 @@ tokio-stream = { version = "0.1.8", features = ["sync"], optional = true } webbrowser = { version = "0.6.0", optional = true } ansi_term = "0.12.1" cpal = { version = "0.13.5", optional = true } -rubato = { version = "0.11.0", optional = true } \ No newline at end of file +rubato = { version = "0.11.0", optional = true } +winapi = { version = "0.3.9", features = ["timeapi"], optional = true } \ No newline at end of file diff --git a/src/run_native.rs b/src/run_native.rs index 8502253..fcd142b 100644 --- a/src/run_native.rs +++ b/src/run_native.rs @@ -59,6 +59,9 @@ struct UW8WatchDog { impl MicroW8 { pub fn new() -> Result<MicroW8> { + #[cfg(target_os = "windows")] + unsafe { winapi::um::timeapi::timeBeginPeriod(1); } + let engine = wasmtime::Engine::new(wasmtime::Config::new().interruptable(true))?; let loader_module = -- 2.35.1.windows.2 ```
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dranke/microw8#2