From 0e1c1f7c07a36b0f77a1401eece652d637c2376e Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:59:56 +0200 Subject: [PATCH] replace the tracker in `Device` with a new `DeviceTracker` --- wgpu-core/src/command/bundle.rs | 6 ------ wgpu-core/src/command/memory_init.rs | 6 +++--- wgpu-core/src/command/mod.rs | 26 +++++++++++++++++++++++- wgpu-core/src/device/queue.rs | 2 +- wgpu-core/src/device/resource.rs | 30 +++------------------------- wgpu-core/src/track/mod.rs | 19 +++++++++++++++--- 6 files changed, 48 insertions(+), 41 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 2b42c5e79..20ff40efe 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -583,12 +583,6 @@ impl RenderBundleEncoder { let render_bundle = Arc::new(render_bundle); - device - .trackers - .lock() - .bundles - .insert_single(render_bundle.clone()); - Ok(render_bundle) } diff --git a/wgpu-core/src/command/memory_init.rs b/wgpu-core/src/command/memory_init.rs index bf31aba1a..895901d92 100644 --- a/wgpu-core/src/command/memory_init.rs +++ b/wgpu-core/src/command/memory_init.rs @@ -8,7 +8,7 @@ use crate::{ init_tracker::*, resource::{DestroyedResourceError, ParentDevice, Texture, Trackable}, snatch::SnatchGuard, - track::{TextureTracker, Tracker}, + track::{DeviceTracker, TextureTracker}, FastHashMap, }; @@ -167,7 +167,7 @@ impl BakedCommands { // executing the commands and updates resource init states accordingly pub(crate) fn initialize_buffer_memory( &mut self, - device_tracker: &mut Tracker, + device_tracker: &mut DeviceTracker, snatch_guard: &SnatchGuard<'_>, ) -> Result<(), DestroyedResourceError> { profiling::scope!("initialize_buffer_memory"); @@ -267,7 +267,7 @@ impl BakedCommands { // uninitialized pub(crate) fn initialize_texture_memory( &mut self, - device_tracker: &mut Tracker, + device_tracker: &mut DeviceTracker, device: &Device, snatch_guard: &SnatchGuard<'_>, ) -> Result<(), DestroyedResourceError> { diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index f5bfcec24..28301fbef 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -35,7 +35,7 @@ use crate::snatch::SnatchGuard; use crate::init_tracker::BufferInitTrackerAction; use crate::resource::Labeled; -use crate::track::{Tracker, UsageScope}; +use crate::track::{DeviceTracker, Tracker, UsageScope}; use crate::LabelHelpers; use crate::{api_log, global::Global, hal_api::HalApi, id, resource_log, Label}; @@ -421,6 +421,30 @@ impl CommandBuffer { raw.transition_textures(texture_barriers); } } + + pub(crate) fn insert_barriers_from_device_tracker( + raw: &mut A::CommandEncoder, + base: &mut DeviceTracker, + head: &Tracker, + snatch_guard: &SnatchGuard, + ) { + profiling::scope!("insert_barriers_from_device_tracker"); + + base.buffers.set_from_tracker(&head.buffers); + base.textures.set_from_tracker(&head.textures); + + let buffer_barriers = base.buffers.drain_transitions(snatch_guard); + let (transitions, textures) = base.textures.drain_transitions(snatch_guard); + let texture_barriers = transitions + .into_iter() + .enumerate() + .map(|(i, p)| p.into_hal(textures[i].unwrap().raw().unwrap())); + + unsafe { + raw.transition_buffers(buffer_barriers); + raw.transition_textures(texture_barriers); + } + } } impl CommandBuffer { diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 75407744d..d7774bd4d 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -1320,7 +1320,7 @@ impl Global { baked.initialize_texture_memory(&mut *trackers, device, &snatch_guard)?; //Note: stateless trackers are not merged: // device already knows these resources exist. - CommandBuffer::insert_barriers_from_tracker( + CommandBuffer::insert_barriers_from_device_tracker( &mut baked.encoder, &mut *trackers, &baked.trackers, diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 444205aac..34bc9f1b0 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -28,7 +28,7 @@ use crate::{ resource_log, snatch::{SnatchGuard, SnatchLock, Snatchable}, track::{ - BindGroupStates, TextureSelector, Tracker, TrackerIndexAllocators, UsageScope, + BindGroupStates, DeviceTracker, TextureSelector, TrackerIndexAllocators, UsageScope, UsageScopePool, }, validation::{self, validate_color_attachment_bytes_per_sample}, @@ -112,7 +112,7 @@ pub struct Device { /// /// Has to be locked temporarily only (locked last) /// and never before pending_writes - pub(crate) trackers: Mutex>, + pub(crate) trackers: Mutex>, pub(crate) tracker_indices: TrackerIndexAllocators, // Life tracker should be locked right after the device and before anything else. life_tracker: Mutex>, @@ -261,7 +261,7 @@ impl Device { fence: RwLock::new(rank::DEVICE_FENCE, Some(fence)), snatchable_lock: unsafe { SnatchLock::new(rank::DEVICE_SNATCHABLE_LOCK) }, valid: AtomicBool::new(true), - trackers: Mutex::new(rank::DEVICE_TRACKERS, Tracker::new()), + trackers: Mutex::new(rank::DEVICE_TRACKERS, DeviceTracker::new()), tracker_indices: TrackerIndexAllocators::new(), life_tracker: Mutex::new(rank::DEVICE_LIFE_TRACKER, LifetimeTracker::new()), bgl_pool: ResourcePool::new(), @@ -1273,8 +1273,6 @@ impl Device { views.push(Arc::downgrade(&view)); } - self.trackers.lock().views.insert_single(view.clone()); - Ok(view) } @@ -1390,8 +1388,6 @@ impl Device { let sampler = Arc::new(sampler); - self.trackers.lock().samplers.insert_single(sampler.clone()); - Ok(sampler) } @@ -2286,11 +2282,6 @@ impl Device { bind_groups.push(weak_ref.clone()); } - self.trackers - .lock() - .bind_groups - .insert_single(bind_group.clone()); - Ok(bind_group) } @@ -2711,11 +2702,6 @@ impl Device { let pipeline = Arc::new(pipeline); - self.trackers - .lock() - .compute_pipelines - .insert_single(pipeline.clone()); - if is_auto_layout { for bgl in pipeline.layout.bind_group_layouts.iter() { bgl.exclusive_pipeline @@ -3342,11 +3328,6 @@ impl Device { let pipeline = Arc::new(pipeline); - self.trackers - .lock() - .render_pipelines - .insert_single(pipeline.clone()); - if is_auto_layout { for bgl in pipeline.layout.bind_group_layouts.iter() { bgl.exclusive_pipeline @@ -3523,11 +3504,6 @@ impl Device { let query_set = Arc::new(query_set); - self.trackers - .lock() - .query_sets - .insert_single(query_set.clone()); - Ok(query_set) } diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index 8d920b383..ce063d989 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -598,12 +598,26 @@ impl<'a, A: HalApi> UsageScope<'a, A> { } } -/// A full double sided tracker used by CommandBuffers and the Device. +/// A tracker used by Device. +pub(crate) struct DeviceTracker { + pub buffers: BufferTracker, + pub textures: TextureTracker, +} + +impl DeviceTracker { + pub fn new() -> Self { + Self { + buffers: BufferTracker::new(), + textures: TextureTracker::new(), + } + } +} + +/// A full double sided tracker used by CommandBuffers. pub(crate) struct Tracker { pub buffers: BufferTracker, pub textures: TextureTracker, pub views: StatelessTracker>, - pub samplers: StatelessTracker>, pub bind_groups: StatelessTracker>, pub compute_pipelines: StatelessTracker>, pub render_pipelines: StatelessTracker>, @@ -617,7 +631,6 @@ impl Tracker { buffers: BufferTracker::new(), textures: TextureTracker::new(), views: StatelessTracker::new(), - samplers: StatelessTracker::new(), bind_groups: StatelessTracker::new(), compute_pipelines: StatelessTracker::new(), render_pipelines: StatelessTracker::new(),