From cc5900b160dea589eee88fb23a590b6670d931d3 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 26 Feb 2019 23:05:36 -0500 Subject: [PATCH] Refactor the view tracker --- wgpu-native/src/command/compute.rs | 1 - wgpu-native/src/command/mod.rs | 10 +++-- wgpu-native/src/command/render.rs | 8 +--- wgpu-native/src/device.rs | 11 ++--- wgpu-native/src/track.rs | 70 ++++++++---------------------- 5 files changed, 31 insertions(+), 69 deletions(-) diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index 8b18308c5..cedd1d33e 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -74,7 +74,6 @@ pub extern "C" fn wgpu_compute_pass_set_bind_group( &*HUB.buffers.read(), &*HUB.textures.read(), ); - pass.trackers.views.consume(&bind_group.used.views); if let Some(pipeline_layout_id) = pass.binder.provide_entry(index as usize, bind_group_id, bind_group) { let pipeline_layout_guard = HUB.pipeline_layouts.read(); diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index a8da9770e..803e4694d 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -16,7 +16,7 @@ use crate::device::{ use crate::hub::{HUB, Storage}; use crate::resource::TexturePlacement; use crate::swap_chain::{SwapChainLink, SwapImageEpoch}; -use crate::track::TrackerSet; +use crate::track::{DummyUsage, TrackerSet}; use crate::conv; use crate::{ BufferHandle, TextureHandle, @@ -123,7 +123,9 @@ impl CommandBufferHandle { families: None, } }); - base.views.consume(&head.views); + base.views + .consume_by_extend(&head.views) + .unwrap(); let stages = all_buffer_stages() | all_image_stages(); unsafe { @@ -194,7 +196,7 @@ pub fn command_encoder_begin_render_pass( } else { extent = Some(view.extent); } - trackers.views.query(at.attachment, &view.life_guard.ref_count); + trackers.views.query(at.attachment, &view.life_guard.ref_count, DummyUsage); let query = trackers.textures.query( view.texture_id.value, &view.texture_id.ref_count, @@ -237,7 +239,7 @@ pub fn command_encoder_begin_render_pass( } else { extent = Some(view.extent); } - trackers.views.query(at.attachment, &view.life_guard.ref_count); + trackers.views.query(at.attachment, &view.life_guard.ref_count, DummyUsage); let query = trackers.textures.query( view.texture_id.value, &view.texture_id.ref_count, diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index aa37e59c7..fe280da04 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -175,13 +175,7 @@ pub extern "C" fn wgpu_render_pass_set_bind_group( let bind_group_guard = HUB.bind_groups.read(); let bind_group = &bind_group_guard[bind_group_id]; - pass.trackers.buffers - .consume_by_extend(&bind_group.used.buffers) - .unwrap(); - pass.trackers.textures - .consume_by_extend(&bind_group.used.textures) - .unwrap(); - pass.trackers.views.consume(&bind_group.used.views); + pass.trackers.consume_by_extend(&bind_group.used); if let Some(pipeline_layout_id) = pass.binder.provide_entry(index as usize, bind_group_id, bind_group) { let pipeline_layout_guard = HUB.pipeline_layouts.read(); diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 0b8516ad5..61711940d 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,6 +1,6 @@ use crate::{binding_model, command, conv, pipeline, resource, swap_chain}; use crate::hub::HUB; -use crate::track::{TrackerSet, TrackPermit}; +use crate::track::{DummyUsage, TrackerSet, TrackPermit}; use crate::{ LifeGuard, RefCount, Stored, SubmissionIndex, BufferMapAsyncStatus, BufferMapOperation, @@ -640,13 +640,13 @@ pub fn device_track_view( let device_id = HUB.textures .read() [texture_id].device_id.value; - let initialized = HUB.devices + let query = HUB.devices .read() [device_id].trackers .lock() .views - .query(view_id, &ref_count); - assert!(initialized); + .query(view_id, &ref_count, DummyUsage); + assert!(query.initialized); } #[cfg(feature = "local")] @@ -905,7 +905,7 @@ pub fn device_create_bind_group( } binding_model::BindingResource::TextureView(id) => { let view = &texture_view_guard[id]; - used.views.query(id, &view.life_guard.ref_count); + used.views.query(id, &view.life_guard.ref_count, DummyUsage); used.textures .transit( view.texture_id.value, @@ -1599,6 +1599,7 @@ pub fn swap_chain_populate_textures( trackers.views.query( view_id.value, &view_id.ref_count, + DummyUsage, ); swap_chain.frames.push(swap_chain::Frame { diff --git a/wgpu-native/src/track.rs b/wgpu-native/src/track.rs index e25d150aa..998a069eb 100644 --- a/wgpu-native/src/track.rs +++ b/wgpu-native/src/track.rs @@ -56,6 +56,15 @@ bitflags! { pub trait GenericUsage { fn is_exclusive(&self) -> bool; } +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct DummyUsage; +impl BitOr for DummyUsage { + type Output = Self; + fn bitor(self, other: Self) -> Self { + other + } +} + impl GenericUsage for BufferUsageFlags { fn is_exclusive(&self) -> bool { BufferUsageFlags::WRITE_ALL.intersects(*self) @@ -66,6 +75,11 @@ impl GenericUsage for TextureUsageFlags { TextureUsageFlags::WRITE_ALL.intersects(*self) } } +impl GenericUsage for DummyUsage { + fn is_exclusive(&self) -> bool { + false + } +} #[derive(Clone)] struct Track { @@ -82,11 +96,7 @@ pub struct Tracker { } pub type BufferTracker = Tracker; pub type TextureTracker = Tracker; -pub struct DummyTracker { - map: FastHashMap, - _phantom: PhantomData, -} -pub type TextureViewTracker = DummyTracker; +pub type TextureViewTracker = Tracker; pub struct TrackerSet { pub buffers: BufferTracker, @@ -111,53 +121,9 @@ impl TrackerSet { self.textures .consume_by_extend(&other.textures) .unwrap(); - self.views.consume(&other.views); - } -} - -impl DummyTracker { - pub fn new() -> Self { - DummyTracker { - map: FastHashMap::default(), - _phantom: PhantomData, - } - } - - /// Remove an id from the tracked map. - pub(crate) fn remove(&mut self, id: I) -> bool { - match self.map.remove(&id.index()) { - Some((_, epoch)) => { - assert_eq!(epoch, id.epoch()); - true - } - None => false, - } - } - - /// Get the last usage on a resource. - pub(crate) fn query(&mut self, id: I, ref_count: &RefCount) -> bool { - match self.map.entry(id.index()) { - Entry::Vacant(e) => { - e.insert((ref_count.clone(), id.epoch())); - true - } - Entry::Occupied(e) => { - assert_eq!(e.get().1, id.epoch()); - false - } - } - } - - /// Consume another tacker. - pub fn consume(&mut self, other: &Self) { - for (&index, &(ref ref_count, epoch)) in &other.map { - self.query(I::new(index, epoch), ref_count); - } - } - - /// Return an iterator over used resources keys. - pub fn used<'a>(&'a self) -> impl 'a + Iterator { - self.map.iter().map(|(&index, &(_, epoch))| I::new(index, epoch)) + self.views + .consume_by_extend(&other.views) + .unwrap(); } }