From 1f4d67aeb2808db4bfb7bd2e12b46498db564fc4 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 22 Apr 2019 13:57:35 -0400 Subject: [PATCH 1/8] Track async mapping properly --- examples/hello_compute_rust/main.rs | 9 +--- wgpu-bindings/wgpu.h | 4 +- wgpu-native/src/command/allocator.rs | 13 ++++++ wgpu-native/src/device.rs | 64 +++++++++++++++++----------- wgpu-rs/src/lib.rs | 57 ++++++++++++++++--------- 5 files changed, 94 insertions(+), 53 deletions(-) diff --git a/examples/hello_compute_rust/main.rs b/examples/hello_compute_rust/main.rs index bac59cf1c..b8da27208 100644 --- a/examples/hello_compute_rust/main.rs +++ b/examples/hello_compute_rust/main.rs @@ -91,13 +91,8 @@ fn main() { device.get_queue().submit(&[encoder.finish()]); staging_buffer.map_read_async(0, size, |result: wgpu::BufferMapAsyncResult<&[u32]>| { - if let wgpu::BufferMapAsyncResult::Success(data) = result { - println!("Times: {:?}", data); + if let Ok(mapping) = result { + println!("Times: {:?}", mapping.data); } }); - - let encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); - device.get_queue().submit(&[encoder.finish()]); - - staging_buffer.unmap(); // TODO: staging_buffer can't be referenced from the callback } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index ac4592960..688cde37b 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -754,10 +754,12 @@ WGPUSwapChainId wgpu_device_create_swap_chain(WGPUDeviceId device_id, WGPUTextureId wgpu_device_create_texture(WGPUDeviceId device_id, const WGPUTextureDescriptor *desc); -void wgpu_device_destroy(WGPUBufferId device_id); +void wgpu_device_destroy(WGPUDeviceId device_id); WGPUQueueId wgpu_device_get_queue(WGPUDeviceId device_id); +void wgpu_device_wait_idle(WGPUDeviceId device_id); + WGPUSurfaceId wgpu_instance_create_surface_from_macos_layer(WGPUInstanceId instance_id, void *layer); diff --git a/wgpu-native/src/command/allocator.rs b/wgpu-native/src/command/allocator.rs index 78d7826e3..d83b295c7 100644 --- a/wgpu-native/src/command/allocator.rs +++ b/wgpu-native/src/command/allocator.rs @@ -131,4 +131,17 @@ impl CommandAllocator { } } } + + pub fn destroy(self, device: &B::Device) { + let mut inner = self.inner.lock(); + while let Some(cmd_buf) = inner.pending.pop() { + inner.recycle(cmd_buf); + } + for (_, mut pool) in inner.pools.drain() { + unsafe { + pool.raw.free(pool.available); + device.destroy_command_pool(pool.raw); + } + } + } } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index e0e19dfb7..e7f765590 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -141,6 +141,7 @@ impl DestroyedResources { trace!("Active submission {} is done", a.index); last_done = last_done.max(a.index); self.free.extend(a.resources.into_iter().map(|(_, r)| r)); + self.ready_to_map.extend(a.mapped); unsafe { device.destroy_fence(a.fence); } @@ -459,6 +460,22 @@ impl Device { } } +impl Device { + fn maintain(&self) { + let mut destroyed = self.destroyed.lock(); + let mut trackers = self.trackers.lock(); + + destroyed.triage_referenced(&mut *trackers); + destroyed.triage_framebuffers(&mut *self.framebuffers.lock()); + let last_done = destroyed.cleanup(&self.raw); + destroyed.handle_mapping(&self.raw, &self.limits); + + if last_done != 0 { + self.com_allocator.maintain(last_done); + } + } +} + pub struct ShaderModule { pub(crate) raw: B::ShaderModule, } @@ -1094,13 +1111,8 @@ pub extern "C" fn wgpu_queue_submit( let (submit_index, fence) = { let mut device_guard = HUB.devices.write(); let device = &mut device_guard[queue_id]; - - let mut swap_chain_links = Vec::new(); - let mut trackers = device.trackers.lock(); - let mut destroyed = device.destroyed.lock(); - destroyed.triage_referenced(&mut *trackers); - destroyed.triage_framebuffers(&mut *device.framebuffers.lock()); + let mut swap_chain_links = Vec::new(); let submit_index = 1 + device .life_guard @@ -1213,24 +1225,13 @@ pub extern "C" fn wgpu_queue_submit( let device_guard = HUB.devices.read(); let device = &device_guard[queue_id]; - let last_done = { - let mut destroyed = device.destroyed.lock(); - let last_done = destroyed.cleanup(&device.raw); - destroyed.handle_mapping(&device.raw, &device.limits); - - destroyed.active.alloc().init(ActiveSubmission { - index: submit_index, - fence, - resources: Vec::new(), - mapped: Vec::new(), - }); - - last_done - }; - - if last_done != 0 { - device.com_allocator.maintain(last_done); - } + device.maintain(); + device.destroyed.lock().active.alloc().init(ActiveSubmission { + index: submit_index, + fence, + resources: Vec::new(), + mapped: Vec::new(), + }); // finally, return the command buffers to the allocator for &cmb_id in command_buffer_ids { @@ -1800,8 +1801,19 @@ pub extern "C" fn wgpu_buffer_set_sub_data( } #[no_mangle] -pub extern "C" fn wgpu_device_destroy(device_id: BufferId) { - HUB.devices.unregister(device_id); +pub extern "C" fn wgpu_device_wait_idle(device_id: DeviceId) { + let device_guard = HUB.devices.read(); + let device = &device_guard[device_id]; + device.raw.wait_idle().unwrap(); + device.maintain(); +} + +#[no_mangle] +pub extern "C" fn wgpu_device_destroy(device_id: DeviceId) { + let device = HUB.devices.unregister(device_id); + device.raw.wait_idle().unwrap(); + device.maintain(); + device.com_allocator.destroy(&device.raw); } pub type BufferMapReadCallback = diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 24515bc60..66cbc7a2a 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -493,15 +493,24 @@ impl Device { impl Drop for Device { fn drop(&mut self) { + wgn::wgpu_device_wait_idle(self.id); //TODO: make this work in general #[cfg(feature = "metal-auto-capture")] wgn::wgpu_device_destroy(self.id); } } -pub enum BufferMapAsyncResult { - Success(T), - Error, +pub struct BufferAsyncMapping { + pub data: T, + buffer_id: wgn::BufferId, +} +//TODO: proper error type +pub type BufferMapAsyncResult = Result, ()>; + +impl Drop for BufferAsyncMapping { + fn drop(&mut self) { + wgn::wgpu_buffer_unmap(self.buffer_id); + } } struct BufferMapReadAsyncUserData @@ -510,6 +519,7 @@ where { size: u32, callback: F, + buffer_id: wgn::BufferId, phantom: std::marker::PhantomData, } @@ -519,6 +529,7 @@ where { size: u32, callback: F, + buffer_id: wgn::BufferId, phantom: std::marker::PhantomData, } @@ -539,28 +550,32 @@ impl Buffer { extern "C" fn buffer_map_read_callback_wrapper( status: wgn::BufferMapAsyncStatus, data: *const u8, - userdata: *mut u8, + user_data: *mut u8, ) where F: FnOnce(BufferMapAsyncResult<&[T]>), { - let userdata = - unsafe { Box::from_raw(userdata as *mut BufferMapReadAsyncUserData) }; + let user_data = + unsafe { Box::from_raw(user_data as *mut BufferMapReadAsyncUserData) }; let data = unsafe { slice::from_raw_parts( data as *const T, - userdata.size as usize / std::mem::size_of::(), + user_data.size as usize / std::mem::size_of::(), ) }; if let wgn::BufferMapAsyncStatus::Success = status { - (userdata.callback)(BufferMapAsyncResult::Success::<&[T]>(data)); + (user_data.callback)(Ok(BufferAsyncMapping { + data, + buffer_id: user_data.buffer_id, + })); } else { - (userdata.callback)(BufferMapAsyncResult::Error); + (user_data.callback)(Err(())) } } - let userdata = Box::new(BufferMapReadAsyncUserData { + let user_data = Box::new(BufferMapReadAsyncUserData { size, callback, + buffer_id: self.id, phantom: std::marker::PhantomData, }); wgn::wgpu_buffer_map_read_async( @@ -568,7 +583,7 @@ impl Buffer { start, size, buffer_map_read_callback_wrapper::, - Box::into_raw(userdata) as *mut u8, + Box::into_raw(user_data) as *mut u8, ); } @@ -584,28 +599,32 @@ impl Buffer { extern "C" fn buffer_map_write_callback_wrapper( status: wgn::BufferMapAsyncStatus, data: *mut u8, - userdata: *mut u8, + user_data: *mut u8, ) where F: FnOnce(BufferMapAsyncResult<&mut [T]>), { - let userdata = - unsafe { Box::from_raw(userdata as *mut BufferMapWriteAsyncUserData) }; + let user_data = + unsafe { Box::from_raw(user_data as *mut BufferMapWriteAsyncUserData) }; let data = unsafe { slice::from_raw_parts_mut( data as *mut T, - userdata.size as usize / std::mem::size_of::(), + user_data.size as usize / std::mem::size_of::(), ) }; if let wgn::BufferMapAsyncStatus::Success = status { - (userdata.callback)(BufferMapAsyncResult::Success::<&mut [T]>(data)); + (user_data.callback)(Ok(BufferAsyncMapping { + data, + buffer_id: user_data.buffer_id, + })); } else { - (userdata.callback)(BufferMapAsyncResult::Error); + (user_data.callback)(Err(())) } } - let userdata = Box::new(BufferMapWriteAsyncUserData { + let user_data = Box::new(BufferMapWriteAsyncUserData { size, callback, + buffer_id: self.id, phantom: std::marker::PhantomData, }); wgn::wgpu_buffer_map_write_async( @@ -613,7 +632,7 @@ impl Buffer { start, size, buffer_map_write_callback_wrapper::, - Box::into_raw(userdata) as *mut u8, + Box::into_raw(user_data) as *mut u8, ); } From e2dcac91d202ba9d88c3f235dacedfcd0737ad77 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 23 Apr 2019 16:27:39 -0400 Subject: [PATCH 2/8] Round up mappable buffer size to non-coherent atom, if needed --- wgpu-native/src/device.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index e7f765590..fd7843847 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -502,10 +502,20 @@ pub fn device_create_buffer( .unwrap() .into(); // TODO: allocate with rendy + + // if the memory is mapped but not coherent, round up to the atom size + let mut mem_size = requirements.size; + if memory_properties.contains(hal::memory::Properties::CPU_VISIBLE) && + !memory_properties.contains(hal::memory::Properties::COHERENT) + { + let mask = device.limits.non_coherent_atom_size as u64 - 1; + mem_size = ((mem_size - 1 ) | mask) + 1; + } + let memory = unsafe { device .raw - .allocate_memory(device_type, requirements.size) + .allocate_memory(device_type, mem_size) .unwrap() }; unsafe { From 7200f727e2656bd2f820b0266bd01ed7f7b14ed7 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 23 Apr 2019 16:45:33 -0400 Subject: [PATCH 3/8] 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 688cde37b..b413c5ab4 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 fd7843847..2abdfb52b 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 66cbc7a2a..1324214db 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 { From 326af8cce601a8441a7e5a4392410bc9fee355ec Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 23 Apr 2019 17:22:53 -0400 Subject: [PATCH 4/8] Do mapping transitions earlier. --- wgpu-native/src/device.rs | 88 +++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 2abdfb52b..8a0f8c38d 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,7 +1,7 @@ use crate::{ binding_model, command, conv, pipeline, resource, swap_chain, hub::HUB, - track::{DummyUsage, Stitch, TrackPermit, TrackerSet}, + track::{DummyUsage, Stitch, Tracktion, TrackPermit, TrackerSet}, AdapterId, BindGroupId, BufferId, CommandBufferId, DeviceId, QueueId, SurfaceId, TextureId, TextureViewId, BufferMapAsyncStatus, BufferMapOperation, LifeGuard, RefCount, Stored, SubmissionIndex, @@ -235,16 +235,6 @@ impl DestroyedResources { let resource_id = stored.value; let buf = &buffer_guard[resource_id]; - let usage = match buf.pending_map_operation { - Some(BufferMapOperation::Read(..)) => resource::BufferUsageFlags::MAP_READ, - Some(BufferMapOperation::Write(..)) => resource::BufferUsageFlags::MAP_WRITE, - _ => unreachable!(), - }; - trackers - .buffers - .get_with_replaced_usage(&buffer_guard, resource_id, usage) - .unwrap(); - let submit_index = buf.life_guard.submission_index.load(Ordering::Acquire); self.active .iter_mut() @@ -1853,22 +1843,36 @@ pub extern "C" fn wgpu_buffer_map_read_async( callback: BufferMapReadCallback, userdata: *mut u8, ) { - let mut buffer_guard = HUB.buffers.write(); - let buffer = &mut buffer_guard[buffer_id]; + let (device_id, ref_count) = { + let mut buffer_guard = HUB.buffers.write(); + let buffer = &mut buffer_guard[buffer_id]; - if buffer.pending_map_operation.is_some() { - log::error!("wgpu_buffer_map_read_async failed: buffer mapping is pending"); - callback(BufferMapAsyncStatus::Error, std::ptr::null_mut(), userdata); - return; + if buffer.pending_map_operation.is_some() { + log::error!("wgpu_buffer_map_read_async failed: buffer mapping is pending"); + callback(BufferMapAsyncStatus::Error, std::ptr::null_mut(), userdata); + return; + } + + let range = start as u64..(start + size) as u64; + buffer.pending_map_operation = Some(BufferMapOperation::Read(range, callback, userdata)); + (buffer.device_id.value, buffer.life_guard.ref_count.clone()) + }; + + let device_guard = HUB.devices.read(); + let device = &device_guard[device_id]; + + let usage = resource::BufferUsageFlags::MAP_READ; + match device.trackers.lock().buffers.transit(buffer_id, &ref_count, usage, TrackPermit::REPLACE) { + Ok(Tracktion::Keep) => {} + Ok(Tracktion::Replace { .. }) => { + //TODO: launch a memory barrier into `HOST_READ` access? + } + other => panic!("Invalid mapping transition {:?}", other), } - let range = start as u64..(start + size) as u64; - buffer.pending_map_operation = Some(BufferMapOperation::Read(range, callback, userdata)); - - HUB.devices.read()[buffer.device_id.value] - .destroyed + device.destroyed .lock() - .map(buffer_id, buffer.life_guard.ref_count.clone()); + .map(buffer_id, ref_count); } #[no_mangle] @@ -1879,22 +1883,36 @@ pub extern "C" fn wgpu_buffer_map_write_async( callback: BufferMapWriteCallback, userdata: *mut u8, ) { - let mut buffer_guard = HUB.buffers.write(); - let buffer = &mut buffer_guard[buffer_id]; + let (device_id, ref_count) = { + let mut buffer_guard = HUB.buffers.write(); + let buffer = &mut buffer_guard[buffer_id]; - if buffer.pending_map_operation.is_some() { - log::error!("wgpu_buffer_map_write_async failed: buffer mapping is pending"); - callback(BufferMapAsyncStatus::Error, std::ptr::null_mut(), userdata); - return; + if buffer.pending_map_operation.is_some() { + log::error!("wgpu_buffer_map_write_async failed: buffer mapping is pending"); + callback(BufferMapAsyncStatus::Error, std::ptr::null_mut(), userdata); + return; + } + + let range = start as u64..(start + size) as u64; + buffer.pending_map_operation = Some(BufferMapOperation::Write(range, callback, userdata)); + (buffer.device_id.value, buffer.life_guard.ref_count.clone()) + }; + + let device_guard = HUB.devices.read(); + let device = &device_guard[device_id]; + + let usage = resource::BufferUsageFlags::MAP_WRITE; + match device.trackers.lock().buffers.transit(buffer_id, &ref_count, usage, TrackPermit::REPLACE) { + Ok(Tracktion::Keep) => {} + Ok(Tracktion::Replace { .. }) => { + //TODO: launch a memory barrier into `HOST_WRITE` access? + } + other => panic!("Invalid mapping transition {:?}", other), } - let range = start as u64..(start + size) as u64; - buffer.pending_map_operation = Some(BufferMapOperation::Write(range, callback, userdata)); - - HUB.devices.read()[buffer.device_id.value] - .destroyed + device.destroyed .lock() - .map(buffer_id, buffer.life_guard.ref_count.clone()); + .map(buffer_id, ref_count); } #[no_mangle] From aa1f876de32789775dd563cc7e5fe426658af520 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 23 Apr 2019 17:26:31 -0400 Subject: [PATCH 5/8] Assert on a mapping buffer being used --- wgpu-native/src/device.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 8a0f8c38d..522797ff4 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1144,7 +1144,9 @@ pub extern "C" fn wgpu_queue_submit( // update submission IDs for id in comb.trackers.buffers.used() { - buffer_guard[id] + let buffer = &buffer_guard[id]; + assert!(buffer.pending_map_operation.is_none()); + buffer .life_guard .submission_index .store(submit_index, Ordering::Release); From 41141b1ae0881f05fe2b70753aea77bde3c886b1 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 24 Apr 2019 22:42:49 -0400 Subject: [PATCH 6/8] Wait for fences instead of device idle --- examples/hello_compute_rust/main.rs | 2 +- wgpu-bindings/wgpu.h | 4 +-- wgpu-native/src/device.rs | 41 +++++++++++++++++------------ wgpu-native/src/instance.rs | 3 +++ wgpu-rs/src/lib.rs | 11 +++----- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/examples/hello_compute_rust/main.rs b/examples/hello_compute_rust/main.rs index b8da27208..f8eebc968 100644 --- a/examples/hello_compute_rust/main.rs +++ b/examples/hello_compute_rust/main.rs @@ -20,7 +20,7 @@ fn main() { let instance = wgpu::Instance::new(); let adapter = instance.get_adapter(&wgpu::AdapterDescriptor { - power_preference: wgpu::PowerPreference::LowPower, + power_preference: wgpu::PowerPreference::Default, }); let mut device = adapter.create_device(&wgpu::DeviceDescriptor { extensions: wgpu::Extensions { diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index b413c5ab4..7781d3c37 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -758,9 +758,7 @@ 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); +void wgpu_device_poll(WGPUDeviceId device_id, bool force_wait); WGPUSurfaceId wgpu_instance_create_surface_from_macos_layer(WGPUInstanceId instance_id, void *layer); diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 522797ff4..e8c56a2b8 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -141,11 +141,23 @@ impl DestroyedResources { } /// Returns the last submission index that is done. - fn cleanup(&mut self, device: &B::Device) -> SubmissionIndex { + fn cleanup(&mut self, device: &B::Device, force_wait: bool) -> SubmissionIndex { let mut last_done = 0; + if force_wait { + unsafe { + device.wait_for_fences( + self.active.iter().map(|a| &a.fence), + hal::device::WaitFor::All, + !0, + ) + }.unwrap(); + } + for i in (0..self.active.len()).rev() { - if unsafe { device.get_fence_status(&self.active[i].fence).unwrap() } { + if force_wait || unsafe { + device.get_fence_status(&self.active[i].fence).unwrap() + } { let a = self.active.swap_remove(i); trace!("Active submission {} is done", a.index); last_done = last_done.max(a.index); @@ -236,6 +248,10 @@ impl DestroyedResources { let buf = &buffer_guard[resource_id]; let submit_index = buf.life_guard.submission_index.load(Ordering::Acquire); + trace!("Mapping of {:?} at submission {:?} gets assigned to active {:?}", + resource_id, submit_index, + self.active.iter().position(|a| a.index == submit_index)); + self.active .iter_mut() .find(|a| a.index == submit_index) @@ -460,13 +476,13 @@ impl Device { } impl Device { - fn maintain(&self) { + fn maintain(&self, force_wait: bool) { let mut destroyed = self.destroyed.lock(); let mut trackers = self.trackers.lock(); destroyed.triage_referenced(&mut *trackers); destroyed.triage_framebuffers(&mut *self.framebuffers.lock()); - let last_done = destroyed.cleanup(&self.raw); + let last_done = destroyed.cleanup(&self.raw, force_wait); destroyed.handle_mapping(&self.raw, &self.limits); if last_done != 0 { @@ -1236,7 +1252,7 @@ pub extern "C" fn wgpu_queue_submit( let device_guard = HUB.devices.read(); let device = &device_guard[queue_id]; - device.maintain(); + device.maintain(false); device.destroyed.lock().active.alloc().init(ActiveSubmission { index: submit_index, fence, @@ -1812,23 +1828,14 @@ pub extern "C" fn wgpu_buffer_set_sub_data( } #[no_mangle] -pub extern "C" fn wgpu_device_wait_idle(device_id: DeviceId) { - let device_guard = HUB.devices.read(); - let device = &device_guard[device_id]; - device.raw.wait_idle().unwrap(); - device.maintain(); -} - -#[no_mangle] -pub extern "C" fn wgpu_device_poll(device_id: DeviceId) { - HUB.devices.read()[device_id].maintain(); +pub extern "C" fn wgpu_device_poll(device_id: DeviceId, force_wait: bool) { + HUB.devices.read()[device_id].maintain(force_wait); } #[no_mangle] pub extern "C" fn wgpu_device_destroy(device_id: DeviceId) { let device = HUB.devices.unregister(device_id); - device.raw.wait_idle().unwrap(); - device.maintain(); + device.maintain(true); device.com_allocator.destroy(&device.raw); } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 3f1699c2e..4cff38031 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -5,6 +5,8 @@ use crate::{ #[cfg(feature = "local")] use crate::{DeviceId, SurfaceId}; +#[cfg(feature = "local")] +use log::info; #[cfg(feature = "remote")] use serde::{Deserialize, Serialize}; @@ -168,6 +170,7 @@ pub extern "C" fn wgpu_instance_get_adapter( desc: &AdapterDescriptor, ) -> AdapterId { let adapter = instance_get_adapter(instance_id, desc); + info!("Adapter {:?}", adapter.info); HUB.adapters.register_local(adapter) } diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 1324214db..ce60b02b4 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -286,13 +286,8 @@ 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 poll(&self, force_wait: bool) { + wgn::wgpu_device_poll(self.id, force_wait); } pub fn create_shader_module(&self, spv: &[u8]) -> ShaderModule { @@ -503,7 +498,7 @@ impl Device { impl Drop for Device { fn drop(&mut self) { - wgn::wgpu_device_wait_idle(self.id); + wgn::wgpu_device_poll(self.id, true); //TODO: make this work in general #[cfg(feature = "metal-auto-capture")] wgn::wgpu_device_destroy(self.id); From 8578643f383d3b369c7b0a0183bf35f3bb62b49f Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 25 Apr 2019 08:29:40 -0400 Subject: [PATCH 7/8] Rename DestroyedResources to PendingResources --- wgpu-native/src/device.rs | 44 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index e8c56a2b8..346aac123 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -100,7 +100,7 @@ struct ActiveSubmission { mapped: Vec, } -/// A class responsible for tracking resource lifetimes. +/// A struct 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. @@ -109,7 +109,7 @@ struct ActiveSubmission { /// 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 { +struct PendingResources { /// Resources that the user has requested be mapped, but are still in use. mapped: Vec>, /// Resources that are destroyed by the user but still referenced by @@ -124,10 +124,10 @@ struct DestroyedResources { ready_to_map: Vec, } -unsafe impl Send for DestroyedResources {} -unsafe impl Sync for DestroyedResources {} +unsafe impl Send for PendingResources {} +unsafe impl Sync for PendingResources {} -impl DestroyedResources { +impl PendingResources { fn destroy(&mut self, resource_id: ResourceId, ref_count: RefCount) { debug_assert!(!self.referenced.iter().any(|r| r.0 == resource_id)); self.referenced.push((resource_id, ref_count)); @@ -192,7 +192,7 @@ impl DestroyedResources { } } -impl DestroyedResources { +impl PendingResources { fn triage_referenced(&mut self, trackers: &mut TrackerSet) { for i in (0..self.referenced.len()).rev() { let num_refs = self.referenced[i].1.load(); @@ -387,7 +387,7 @@ pub struct Device { pub(crate) render_passes: Mutex>, pub(crate) framebuffers: Mutex>, desc_pool: Mutex, - destroyed: Mutex>, + pending: Mutex>, } impl Device { @@ -464,7 +464,7 @@ impl Device { render_passes: Mutex::new(FastHashMap::default()), framebuffers: Mutex::new(FastHashMap::default()), desc_pool, - destroyed: Mutex::new(DestroyedResources { + pending: Mutex::new(PendingResources { mapped: Vec::new(), referenced: Vec::new(), active: Vec::new(), @@ -477,13 +477,13 @@ impl Device { impl Device { fn maintain(&self, force_wait: bool) { - let mut destroyed = self.destroyed.lock(); + let mut pending = self.pending.lock(); let mut trackers = self.trackers.lock(); - destroyed.triage_referenced(&mut *trackers); - destroyed.triage_framebuffers(&mut *self.framebuffers.lock()); - let last_done = destroyed.cleanup(&self.raw, force_wait); - destroyed.handle_mapping(&self.raw, &self.limits); + pending.triage_referenced(&mut *trackers); + pending.triage_framebuffers(&mut *self.framebuffers.lock()); + let last_done = pending.cleanup(&self.raw, force_wait); + pending.handle_mapping(&self.raw, &self.limits); if last_done != 0 { self.com_allocator.maintain(last_done); @@ -629,7 +629,7 @@ pub extern "C" fn wgpu_buffer_destroy(buffer_id: BufferId) { let buffer_guard = HUB.buffers.read(); let buffer = &buffer_guard[buffer_id]; HUB.devices.read()[buffer.device_id.value] - .destroyed + .pending .lock() .destroy( ResourceId::Buffer(buffer_id), @@ -827,7 +827,7 @@ pub extern "C" fn wgpu_texture_destroy(texture_id: TextureId) { let texture_guard = HUB.textures.read(); let texture = &texture_guard[texture_id]; HUB.devices.read()[texture.device_id.value] - .destroyed + .pending .lock() .destroy( ResourceId::Texture(texture_id), @@ -840,7 +840,7 @@ pub extern "C" fn wgpu_texture_view_destroy(texture_view_id: TextureViewId) { let texture_view_guard = HUB.texture_views.read(); let view = &texture_view_guard[texture_view_id]; let device_id = HUB.textures.read()[view.texture_id.value].device_id.value; - HUB.devices.read()[device_id].destroyed.lock().destroy( + HUB.devices.read()[device_id].pending.lock().destroy( ResourceId::TextureView(texture_view_id), view.life_guard.ref_count.clone(), ); @@ -1253,7 +1253,7 @@ pub extern "C" fn wgpu_queue_submit( let device = &device_guard[queue_id]; device.maintain(false); - device.destroyed.lock().active.alloc().init(ActiveSubmission { + device.pending.lock().active.alloc().init(ActiveSubmission { index: submit_index, fence, resources: Vec::new(), @@ -1599,14 +1599,14 @@ pub fn device_create_swap_chain( Some(mut old) => { //TODO: remove this once gfx-rs stops destroying the old swapchain device.raw.wait_idle().unwrap(); - let mut destroyed = device.destroyed.lock(); + let mut pending = device.pending.lock(); assert_eq!(old.device_id.value, device_id); for frame in old.frames { - destroyed.destroy( + pending.destroy( ResourceId::Texture(frame.texture_id.value), frame.texture_id.ref_count, ); - destroyed.destroy( + pending.destroy( ResourceId::TextureView(frame.view_id.value), frame.view_id.ref_count, ); @@ -1879,7 +1879,7 @@ pub extern "C" fn wgpu_buffer_map_read_async( other => panic!("Invalid mapping transition {:?}", other), } - device.destroyed + device.pending .lock() .map(buffer_id, ref_count); } @@ -1919,7 +1919,7 @@ pub extern "C" fn wgpu_buffer_map_write_async( other => panic!("Invalid mapping transition {:?}", other), } - device.destroyed + device.pending .lock() .map(buffer_id, ref_count); } From 06d34830d40dc5158e2f1173fae23fee7e2796cb Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 25 Apr 2019 08:29:53 -0400 Subject: [PATCH 8/8] Update OSX CI version to 9.4 --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index bf67663c4..29cd64187 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,18 +17,18 @@ matrix: - env: MACOSX_DEPLOYMENT_TARGET=10.9 os: osx rust: stable - osx_image: xcode9 + osx_image: xcode9.4 compiler: clang #- env: MACOSX_DEPLOYMENT_TARGET=10.9 # os: osx # rust: nightly - # osx_image: xcode9 + # osx_image: xcode9.4 # compiler: clang # iPhoneOS 64bit #- env: TARGET=aarch64-apple-ios # os: osx - # osx_image: xcode9 + # osx_image: xcode9.4 # rust: nightly branches: