From 7200f727e2656bd2f820b0266bd01ed7f7b14ed7 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 23 Apr 2019 16:45:33 -0400 Subject: [PATCH] Expose device polling --- wgpu-bindings/wgpu.h | 2 ++ wgpu-native/src/device.rs | 18 ++++++++++++++++-- wgpu-rs/src/lib.rs | 10 ++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 688cde37be..b413c5ab46 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -758,6 +758,8 @@ void wgpu_device_destroy(WGPUDeviceId device_id); WGPUQueueId wgpu_device_get_queue(WGPUDeviceId device_id); +void wgpu_device_poll(WGPUDeviceId device_id); + void wgpu_device_wait_idle(WGPUDeviceId device_id); WGPUSurfaceId wgpu_instance_create_surface_from_macos_layer(WGPUInstanceId instance_id, diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index fd78438470..2abdfb52bc 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -100,6 +100,15 @@ struct ActiveSubmission { mapped: Vec, } +/// A class responsible for tracking resource lifetimes. +/// +/// Here is how host mapping is handled: +/// 1. When mapping is requested we add the buffer to the pending list of `mapped` buffers. +/// 2. When `triage_referenced` is called, it checks the last submission index associated with each of the mapped buffer, +/// and register the buffer with either a submission in flight, or straight into `ready_to_map` vector. +/// 3. when `ActiveSubmission` is retired, the mapped buffers associated with it are moved to `ready_to_map` vector. +/// 4. Finally, `handle_mapping` issues all the callbacks. + struct DestroyedResources { /// Resources that the user has requested be mapped, but are still in use. mapped: Vec>, @@ -222,8 +231,8 @@ impl DestroyedResources { let buffer_guard = HUB.buffers.read(); - for i in (0..self.mapped.len()).rev() { - let resource_id = self.mapped.swap_remove(i).value; + for stored in self.mapped.drain(..) { + let resource_id = stored.value; let buf = &buffer_guard[resource_id]; let usage = match buf.pending_map_operation { @@ -1818,6 +1827,11 @@ pub extern "C" fn wgpu_device_wait_idle(device_id: DeviceId) { device.maintain(); } +#[no_mangle] +pub extern "C" fn wgpu_device_poll(device_id: DeviceId) { + HUB.devices.read()[device_id].maintain(); +} + #[no_mangle] pub extern "C" fn wgpu_device_destroy(device_id: DeviceId) { let device = HUB.devices.unregister(device_id); diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 66cbc7a2a7..1324214db0 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -285,6 +285,16 @@ impl Adapter { } impl Device { + /// Check for resource cleanups and mapping callbacks. + pub fn poll(&self) { + wgn::wgpu_device_poll(self.id); + } + + /// Wait for GPU work to finish and process all the callbacks. + pub fn wait_idle(&self) { + wgn::wgpu_device_wait_idle(self.id); + } + pub fn create_shader_module(&self, spv: &[u8]) -> ShaderModule { let desc = wgn::ShaderModuleDescriptor { code: wgn::ByteArray {