From 1be2ce6190bbc11686553ba8a7f8fa44a5164033 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Thu, 23 Oct 2025 14:06:21 -0700 Subject: [PATCH] Clean up device poll API --- player/src/bin/play.rs | 9 ++------- player/tests/player/main.rs | 7 +++---- wgpu-core/src/device/global.rs | 4 ++-- wgpu-core/src/device/mod.rs | 3 +-- wgpu-core/src/device/resource.rs | 15 +++++++++++++++ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/player/src/bin/play.rs b/player/src/bin/play.rs index cc251e881..d8d1342a3 100644 --- a/player/src/bin/play.rs +++ b/player/src/bin/play.rs @@ -125,9 +125,7 @@ fn main() { } unsafe { global.device_stop_graphics_debugger_capture(device) }; - let (user_closures, result) = device.poll(wgt::PollType::wait_indefinitely()); - user_closures.fire(); - result.unwrap(); + device.poll(wgt::PollType::wait_indefinitely()).unwrap(); } #[cfg(feature = "winit")] { @@ -212,10 +210,7 @@ fn main() { }, Event::LoopExiting => { log::info!("Closing"); - let (user_closures, result) = - device.poll(wgt::PollType::wait_indefinitely()); - user_closures.fire(); - result.unwrap(); + device.poll(wgt::PollType::wait_indefinitely()).unwrap(); } _ => {} } diff --git a/player/tests/player/main.rs b/player/tests/player/main.rs index e29f01f67..5b7e6566f 100644 --- a/player/tests/player/main.rs +++ b/player/tests/player/main.rs @@ -116,12 +116,11 @@ impl Test<'_> { } println!("\t\t\tWaiting..."); - let (user_closures, result) = device.poll(wgt::PollType::Wait { + device.poll(wgt::PollType::Wait { submission_index: None, timeout: Some(std::time::Duration::from_secs(1)), // Tests really shouldn't need longer than that! - }); - user_closures.fire(); - result.unwrap(); + }) + .unwrap(); for expect in self.expectations { println!("\t\t\tChecking {}", expect.name); diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index f0e2007c2..1aa096601 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1792,7 +1792,7 @@ impl Global { let device = self.hub.devices.get(device_id); - let (closures, result) = device.poll(poll_type); + let (closures, result) = device.poll_and_return_closures(poll_type); closures.fire(); @@ -1825,7 +1825,7 @@ impl Global { wgt::PollType::Poll }; - let (closures, result) = device.poll(poll_type); + let (closures, result) = device.poll_and_return_closures(poll_type); let is_queue_empty = matches!(result, Ok(wgt::PollStatus::QueueEmpty)); diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 07401f414..1ce57c264 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -174,8 +174,7 @@ impl UserClosures { .extend(other.device_lost_invocations); } - /// Not a public API. For use by `player` only. - pub fn fire(self) { + fn fire(self) { // Note: this logic is specifically moved out of `handle_mapping()` in order to // have nothing locked by the time we execute users callback code. diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 67b2c00c7..ff3bd6d0c 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -715,6 +715,21 @@ impl Device { pub fn poll( &self, poll_type: wgt::PollType, + ) -> Result { + let (user_closures, result) = self.poll_and_return_closures(poll_type); + user_closures.fire(); + result + } + + /// Poll the device, returning any `UserClosures` that need to be executed. + /// + /// The caller must invoke the `UserClosures` even if this function returns + /// an error. This is an internal helper, used by `Device::poll` and + /// `Global::poll_all_devices`, so that `poll_all_devices` can invoke + /// closures once after all devices have been polled. + pub(crate) fn poll_and_return_closures( + &self, + poll_type: wgt::PollType, ) -> (UserClosures, Result) { let snatch_guard = self.snatchable_lock.read(); let fence = self.fence.read();