From 5f78485f3046f0c5867c93bc542d1c42eb2b462b Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Fri, 21 Jun 2024 11:27:22 +0200 Subject: [PATCH] pass `Label` to `ResourceInfo::new` --- wgpu-core/src/command/bundle.rs | 7 ++--- wgpu-core/src/command/mod.rs | 17 +++++----- wgpu-core/src/command/render.rs | 10 +++--- wgpu-core/src/device/queue.rs | 5 +-- wgpu-core/src/device/resource.rs | 53 ++++++++++---------------------- wgpu-core/src/instance.rs | 14 +++------ wgpu-core/src/present.rs | 7 +++-- wgpu-core/src/registry.rs | 2 +- wgpu-core/src/resource.rs | 13 +++----- 9 files changed, 50 insertions(+), 78 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index d1ecbc229..449da69a7 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -101,7 +101,7 @@ use crate::{ resource_log, snatch::SnatchGuard, track::RenderBundleScope, - Label, LabelHelpers, + Label, }; use arrayvec::ArrayVec; @@ -805,10 +805,7 @@ impl RenderBundleEncoder { buffer_memory_init_actions, texture_memory_init_actions, context: self.context, - info: ResourceInfo::new( - desc.label.borrow_or_default(), - Some(device.tracker_indices.bundles.clone()), - ), + info: ResourceInfo::new(&desc.label, Some(device.tracker_indices.bundles.clone())), discard_hal_labels: device .instance_flags .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS), diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index cd79ea31c..b4bac5428 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -33,6 +33,7 @@ use crate::snatch::SnatchGuard; use crate::init_tracker::BufferInitTrackerAction; use crate::resource::{ParentDevice, Resource, ResourceInfo, ResourceType}; use crate::track::{Tracker, UsageScope}; +use crate::LabelHelpers; use crate::{api_log, global::Global, hal_api::HalApi, id, resource_log, Label}; use hal::CommandEncoder as _; @@ -142,7 +143,7 @@ pub(crate) struct CommandEncoder { /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder is_open: bool, - label: Option, + hal_label: Option, } //TODO: handle errors better @@ -218,8 +219,8 @@ impl CommandEncoder { pub(crate) fn open(&mut self) -> Result<&mut A::CommandEncoder, DeviceError> { if !self.is_open { self.is_open = true; - let label = self.label.as_deref(); - unsafe { self.raw.begin_encoding(label)? }; + let hal_label = self.hal_label.as_deref(); + unsafe { self.raw.begin_encoding(hal_label)? }; } Ok(&mut self.raw) @@ -229,9 +230,9 @@ impl CommandEncoder { /// its own label. /// /// The underlying hal encoder is put in the "recording" state. - fn open_pass(&mut self, label: Option<&str>) -> Result<(), DeviceError> { + fn open_pass(&mut self, hal_label: Option<&str>) -> Result<(), DeviceError> { self.is_open = true; - unsafe { self.raw.begin_encoding(label)? }; + unsafe { self.raw.begin_encoding(hal_label)? }; Ok(()) } @@ -339,13 +340,13 @@ impl CommandBuffer { encoder: A::CommandEncoder, device: &Arc>, #[cfg(feature = "trace")] enable_tracing: bool, - label: Option, + label: &Label, ) -> Self { CommandBuffer { device: device.clone(), limits: device.limits.clone(), support_clear_texture: device.features.contains(wgt::Features::CLEAR_TEXTURE), - info: ResourceInfo::new(label.as_deref().unwrap_or(""), None), + info: ResourceInfo::new(label, None), data: Mutex::new( rank::COMMAND_BUFFER_DATA, Some(CommandBufferMutable { @@ -353,7 +354,7 @@ impl CommandBuffer { raw: encoder, is_open: false, list: Vec::new(), - label, + hal_label: label.to_hal(device.instance_flags).map(str::to_owned), }, status: CommandEncoderStatus::Recording, trackers: Tracker::new(), diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 44a34375f..ab44dc879 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -811,7 +811,7 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> { fn start( device: &'d Device, - label: Option<&str>, + hal_label: Option<&str>, color_attachments: &[Option], depth_stencil_attachment: Option<&RenderPassDepthStencilAttachment>, timestamp_writes: Option<&RenderPassTimestampWrites>, @@ -1223,7 +1223,7 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> { }; let hal_desc = hal::RenderPassDescriptor { - label: hal_label(label, device.instance_flags), + label: hal_label, extent, sample_count, color_attachments: &colors, @@ -1389,7 +1389,7 @@ impl Global { .instance .flags .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS); - let label = hal_label(base.label.as_deref(), self.instance.flags); + let hal_label = hal_label(base.label.as_deref(), self.instance.flags); let pass_scope = PassErrorScope::PassEncoder(encoder_id); @@ -1436,7 +1436,7 @@ impl Global { encoder.close().map_pass_err(pass_scope)?; // We will reset this to `Recording` if we succeed, acts as a fail-safe. *status = CommandEncoderStatus::Error; - encoder.open_pass(label).map_pass_err(pass_scope)?; + encoder.open_pass(hal_label).map_pass_err(pass_scope)?; let query_set_guard = hub.query_sets.read(); let view_guard = hub.texture_views.read(); @@ -1448,7 +1448,7 @@ impl Global { let mut info = RenderPassInfo::start( device, - label, + hal_label, color_attachments, depth_stencil_attachment, timestamp_writes, diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index a6fee7537..42fb158e5 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -348,10 +348,7 @@ fn prepare_staging_buffer( raw: Mutex::new(rank::STAGING_BUFFER_RAW, Some(buffer)), device: device.clone(), size, - info: ResourceInfo::new( - "", - Some(device.tracker_indices.staging_buffers.clone()), - ), + info: ResourceInfo::new(&None, Some(device.tracker_indices.staging_buffers.clone())), is_coherent: mapping.is_coherent, }; diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 92c854252..67e21d6f3 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -268,7 +268,7 @@ impl Device { queue: OnceCell::new(), queue_to_drop: OnceCell::new(), zero_buffer: Some(zero_buffer), - info: ResourceInfo::new("", None), + info: ResourceInfo::new(&desc.label, None), command_allocator, active_submission_index: AtomicU64::new(0), fence: RwLock::new(rank::DEVICE_FENCE, Some(fence)), @@ -656,10 +656,7 @@ impl Device { ), sync_mapped_writes: Mutex::new(rank::BUFFER_SYNC_MAPPED_WRITES, None), map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), - info: ResourceInfo::new( - desc.label.borrow_or_default(), - Some(self.tracker_indices.buffers.clone()), - ), + info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.buffers.clone())), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()), }) } @@ -686,10 +683,7 @@ impl Device { mips: 0..desc.mip_level_count, layers: 0..desc.array_layer_count(), }, - info: ResourceInfo::new( - desc.label.borrow_or_default(), - Some(self.tracker_indices.textures.clone()), - ), + info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.textures.clone())), clear_mode: RwLock::new(rank::TEXTURE_CLEAR_MODE, clear_mode), views: Mutex::new(rank::TEXTURE_VIEWS, Vec::new()), bind_groups: Mutex::new(rank::TEXTURE_BIND_GROUPS, Vec::new()), @@ -712,10 +706,7 @@ impl Device { ), sync_mapped_writes: Mutex::new(rank::BUFFER_SYNC_MAPPED_WRITES, None), map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), - info: ResourceInfo::new( - desc.label.borrow_or_default(), - Some(self.tracker_indices.buffers.clone()), - ), + info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.buffers.clone())), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()), } } @@ -1292,7 +1283,7 @@ impl Device { samples: texture.desc.sample_count, selector, info: ResourceInfo::new( - desc.label.borrow_or_default(), + &desc.label, Some(self.tracker_indices.texture_views.clone()), ), }) @@ -1400,10 +1391,7 @@ impl Device { Ok(Sampler { raw: Some(raw), device: self.clone(), - info: ResourceInfo::new( - desc.label.borrow_or_default(), - Some(self.tracker_indices.samplers.clone()), - ), + info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.samplers.clone())), comparison: desc.compare.is_some(), filtering: desc.min_filter == wgt::FilterMode::Linear || desc.mag_filter == wgt::FilterMode::Linear, @@ -1536,7 +1524,7 @@ impl Device { raw: Some(raw), device: self.clone(), interface: Some(interface), - info: ResourceInfo::new(desc.label.borrow_or_default(), None), + info: ResourceInfo::new(&desc.label, None), }) } @@ -1578,7 +1566,7 @@ impl Device { raw: Some(raw), device: self.clone(), interface: None, - info: ResourceInfo::new(desc.label.borrow_or_default(), None), + info: ResourceInfo::new(&desc.label, None), }) } @@ -1599,7 +1587,7 @@ impl Device { self, #[cfg(feature = "trace")] self.trace.lock().is_some(), - label.to_hal(self.instance_flags).map(str::to_owned), + label, )) } @@ -1821,9 +1809,8 @@ impl Device { let bgl_flags = conv::bind_group_layout_flags(self.features); let hal_bindings = entry_map.values().copied().collect::>(); - let label = label.to_hal(self.instance_flags); let hal_desc = hal::BindGroupLayoutDescriptor { - label, + label: label.to_hal(self.instance_flags), flags: bgl_flags, entries: &hal_bindings, }; @@ -1851,10 +1838,7 @@ impl Device { entries: entry_map, origin, binding_count_validator: count_validator, - info: ResourceInfo::new( - label.unwrap_or(""), - Some(self.tracker_indices.bind_group_layouts.clone()), - ), + info: ResourceInfo::new(label, Some(self.tracker_indices.bind_group_layouts.clone())), }) } @@ -2283,10 +2267,7 @@ impl Device { raw: Snatchable::new(raw), device: self.clone(), layout: layout.clone(), - info: ResourceInfo::new( - desc.label.borrow_or_default(), - Some(self.tracker_indices.bind_groups.clone()), - ), + info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.bind_groups.clone())), used, used_buffer_ranges, used_texture_ranges, @@ -2569,7 +2550,7 @@ impl Device { raw: Some(raw), device: self.clone(), info: ResourceInfo::new( - desc.label.borrow_or_default(), + &desc.label, Some(self.tracker_indices.pipeline_layouts.clone()), ), bind_group_layouts, @@ -2758,7 +2739,7 @@ impl Device { _shader_module: shader_module, late_sized_buffer_groups, info: ResourceInfo::new( - desc.label.borrow_or_default(), + &desc.label, Some(self.tracker_indices.compute_pipelines.clone()), ), }; @@ -3412,7 +3393,7 @@ impl Device { vertex_steps, late_sized_buffer_groups, info: ResourceInfo::new( - desc.label.borrow_or_default(), + &desc.label, Some(self.tracker_indices.render_pipelines.clone()), ), }; @@ -3460,7 +3441,7 @@ impl Device { let cache = pipeline::PipelineCache { device: self.clone(), info: ResourceInfo::new( - desc.label.borrow_or_default(), + &desc.label, Some(self.tracker_indices.pipeline_caches.clone()), ), // This would be none in the error condition, which we don't implement yet @@ -3578,7 +3559,7 @@ impl Device { Ok(QuerySet { raw: Some(unsafe { self.raw().create_query_set(&hal_desc).unwrap() }), device: self.clone(), - info: ResourceInfo::new("", Some(self.tracker_indices.query_sets.clone())), + info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.query_sets.clone())), desc: desc.map_label(|_| ()), }) } diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index e499a9f61..c9aeca1c7 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -158,10 +158,6 @@ impl Resource for Surface { fn as_info_mut(&mut self) -> &mut ResourceInfo { &mut self.info } - - fn label(&self) -> &str { - "" - } } impl Surface { @@ -204,7 +200,7 @@ impl Adapter { Self { raw, - info: ResourceInfo::new("", None), + info: ResourceInfo::new(&None, None), } } @@ -309,7 +305,7 @@ impl Adapter { let queue = Queue { device: None, raw: Some(hal_device.queue), - info: ResourceInfo::new("", None), + info: ResourceInfo::new(&None, None), }; return Ok((device, queue)); } @@ -532,7 +528,7 @@ impl Global { let surface = Surface { presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), - info: ResourceInfo::new("", None), + info: ResourceInfo::new(&None, None), #[cfg(vulkan)] vulkan: init::( @@ -596,7 +592,7 @@ impl Global { let surface = Surface { presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), - info: ResourceInfo::new("", None), + info: ResourceInfo::new(&None, None), metal: Some(self.instance.metal.as_ref().map_or( Err(CreateSurfaceError::BackendNotEnabled(Backend::Metal)), |inst| { @@ -625,7 +621,7 @@ impl Global { ) -> Result { let surface = Surface { presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), - info: ResourceInfo::new("", None), + info: ResourceInfo::new(&None, None), dx12: Some(create_surface_func( self.instance .dx12 diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index bd465e910..0376260e2 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -9,7 +9,10 @@ When this texture is presented, we remove it from the device tracker as well as extract it from the hub. !*/ -use std::{borrow::Borrow, sync::Arc}; +use std::{ + borrow::{Borrow, Cow}, + sync::Arc, +}; #[cfg(feature = "trace")] use crate::device::trace::Action; @@ -225,7 +228,7 @@ impl Global { mips: 0..1, }, info: ResourceInfo::new( - "", + &Some(Cow::Borrowed("")), Some(device.tracker_indices.textures.clone()), ), clear_mode: RwLock::new( diff --git a/wgpu-core/src/registry.rs b/wgpu-core/src/registry.rs index d14d88206..49222aa75 100644 --- a/wgpu-core/src/registry.rs +++ b/wgpu-core/src/registry.rs @@ -255,7 +255,7 @@ mod tests { s.spawn(|| { for _ in 0..1000 { let value = Arc::new(TestData { - info: ResourceInfo::new("Test data", None), + info: ResourceInfo::new(&None, None), }); let new_id = registry.prepare(None); let (id, _) = new_id.assign(value); diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index abf5cc480..26848e999 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -83,10 +83,8 @@ impl Drop for ResourceInfo { } impl ResourceInfo { - // Note: Abstractly, this function should take `label: String` to minimize string cloning. - // But as actually used, every input is a literal or borrowed `&str`, so this is convenient. pub(crate) fn new( - label: &str, + label: &Label, tracker_indices: Option>, ) -> Self { let tracker_index = tracker_indices @@ -98,7 +96,10 @@ impl ResourceInfo { tracker_index, tracker_indices, submission_index: AtomicUsize::new(0), - label: label.to_string(), + label: label + .as_ref() + .map(|label| label.to_string()) + .unwrap_or_default(), } } @@ -913,10 +914,6 @@ impl Resource for StagingBuffer { fn as_info_mut(&mut self) -> &mut ResourceInfo { &mut self.info } - - fn label(&self) -> &str { - "" - } } impl ParentDevice for StagingBuffer {