From b96983a56be606d1cf005bfd273cc797e39aa2ba Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 29 Mar 2019 16:50:38 -0400 Subject: [PATCH] Fix submission tracking --- wgpu-native/src/command/allocator.rs | 7 ++++++- wgpu-native/src/device.rs | 27 +++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/wgpu-native/src/command/allocator.rs b/wgpu-native/src/command/allocator.rs index ef0663002b..78d7826e32 100644 --- a/wgpu-native/src/command/allocator.rs +++ b/wgpu-native/src/command/allocator.rs @@ -9,6 +9,7 @@ use hal::{ pool::RawCommandPool, Device, }; +use log::trace; use parking_lot::Mutex; use std::{ @@ -17,6 +18,7 @@ use std::{ thread, }; + struct CommandPool { raw: B::CommandPool, available: Vec, @@ -109,7 +111,9 @@ impl CommandAllocator { pool.available.pop().unwrap() } - pub fn after_submit(&self, cmd_buf: CommandBuffer) { + pub fn after_submit(&self, cmd_buf: CommandBuffer, submit_index: SubmissionIndex) { + cmd_buf.life_guard.submission_index + .store(submit_index, Ordering::Release); self.inner.lock().pending.push(cmd_buf); } @@ -122,6 +126,7 @@ impl CommandAllocator { .load(Ordering::Acquire); if index <= last_done { let cmd_buf = inner.pending.swap_remove(i); + trace!("recycling comb submitted in {} when {} is done", index, last_done); inner.recycle(cmd_buf); } } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index ccca03195a..78752b70ac 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -138,6 +138,7 @@ impl DestroyedResources { for i in (0..self.active.len()).rev() { if 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); self.free.extend(a.resources.into_iter().map(|(_, r)| r)); unsafe { @@ -1087,7 +1088,7 @@ pub extern "C" fn wgpu_queue_submit( let command_buffer_ids = unsafe { slice::from_raw_parts(command_buffer_ptr, command_buffer_count) }; - let (old_submit_index, fence) = { + let (submit_index, fence) = { let mut device_guard = HUB.devices.write(); let device = &mut device_guard[queue_id]; @@ -1098,7 +1099,7 @@ pub extern "C" fn wgpu_queue_submit( destroyed.triage_referenced(&mut *trackers); destroyed.triage_framebuffers(&mut *device.framebuffers.lock()); - let old_submit_index = device + let submit_index = 1 + device .life_guard .submission_index .fetch_add(1, Ordering::Relaxed); @@ -1117,26 +1118,23 @@ pub extern "C" fn wgpu_queue_submit( let comb = &mut command_buffer_guard[cmb_id]; swap_chain_links.extend(comb.swap_chain_links.drain(..)); // update submission IDs - comb.life_guard - .submission_index - .store(old_submit_index, Ordering::Release); for id in comb.trackers.buffers.used() { buffer_guard[id] .life_guard .submission_index - .store(old_submit_index, Ordering::Release); + .store(submit_index, Ordering::Release); } for id in comb.trackers.textures.used() { texture_guard[id] .life_guard .submission_index - .store(old_submit_index, Ordering::Release); + .store(submit_index, Ordering::Release); } for id in comb.trackers.views.used() { texture_view_guard[id] .life_guard .submission_index - .store(old_submit_index, Ordering::Release); + .store(submit_index, Ordering::Release); } // execute resource transitions @@ -1201,7 +1199,7 @@ pub extern "C" fn wgpu_queue_submit( } } - (old_submit_index, fence) + (submit_index, fence) }; // No need for write access to the device from here on out @@ -1214,7 +1212,7 @@ pub extern "C" fn wgpu_queue_submit( destroyed.handle_mapping(&device.raw, &device.limits); destroyed.active.alloc().init(ActiveSubmission { - index: old_submit_index + 1, + index: submit_index, fence, resources: Vec::new(), mapped: Vec::new(), @@ -1230,7 +1228,7 @@ pub extern "C" fn wgpu_queue_submit( // finally, return the command buffers to the allocator for &cmb_id in command_buffer_ids { let cmd_buf = HUB.command_buffers.unregister(cmb_id); - device.com_allocator.after_submit(cmd_buf); + device.com_allocator.after_submit(cmd_buf, submit_index); } } @@ -1760,10 +1758,7 @@ pub extern "C" fn wgpu_buffer_set_sub_data( .com_allocator .allocate(buffer.device_id.clone(), &device.raw); // mark as used by the next submission, conservatively - let last_submit_index = device.life_guard.submission_index.load(Ordering::Acquire); - comb.life_guard - .submission_index - .store(last_submit_index + 1, Ordering::Release); + let submit_index = 1 + device.life_guard.submission_index.load(Ordering::Acquire); unsafe { let raw = comb.raw.last_mut().unwrap(); raw.begin( @@ -1792,7 +1787,7 @@ pub extern "C" fn wgpu_buffer_set_sub_data( .submit::<_, _, ::Semaphore, _, _>(submission, None); } - device.com_allocator.after_submit(comb); + device.com_allocator.after_submit(comb, submit_index); } #[no_mangle]