pass Label to ResourceInfo::new

This commit is contained in:
teoxoy 2024-06-21 11:27:22 +02:00 committed by Teodor Tanasoaia
parent beaa6e95e4
commit 5f78485f30
9 changed files with 50 additions and 78 deletions

View File

@ -101,7 +101,7 @@ use crate::{
resource_log, resource_log,
snatch::SnatchGuard, snatch::SnatchGuard,
track::RenderBundleScope, track::RenderBundleScope,
Label, LabelHelpers, Label,
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
@ -805,10 +805,7 @@ impl RenderBundleEncoder {
buffer_memory_init_actions, buffer_memory_init_actions,
texture_memory_init_actions, texture_memory_init_actions,
context: self.context, context: self.context,
info: ResourceInfo::new( info: ResourceInfo::new(&desc.label, Some(device.tracker_indices.bundles.clone())),
desc.label.borrow_or_default(),
Some(device.tracker_indices.bundles.clone()),
),
discard_hal_labels: device discard_hal_labels: device
.instance_flags .instance_flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS), .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS),

View File

@ -33,6 +33,7 @@ use crate::snatch::SnatchGuard;
use crate::init_tracker::BufferInitTrackerAction; use crate::init_tracker::BufferInitTrackerAction;
use crate::resource::{ParentDevice, Resource, ResourceInfo, ResourceType}; use crate::resource::{ParentDevice, Resource, ResourceInfo, ResourceType};
use crate::track::{Tracker, UsageScope}; use crate::track::{Tracker, UsageScope};
use crate::LabelHelpers;
use crate::{api_log, global::Global, hal_api::HalApi, id, resource_log, Label}; use crate::{api_log, global::Global, hal_api::HalApi, id, resource_log, Label};
use hal::CommandEncoder as _; use hal::CommandEncoder as _;
@ -142,7 +143,7 @@ pub(crate) struct CommandEncoder<A: HalApi> {
/// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder
is_open: bool, is_open: bool,
label: Option<String>, hal_label: Option<String>,
} }
//TODO: handle errors better //TODO: handle errors better
@ -218,8 +219,8 @@ impl<A: HalApi> CommandEncoder<A> {
pub(crate) fn open(&mut self) -> Result<&mut A::CommandEncoder, DeviceError> { pub(crate) fn open(&mut self) -> Result<&mut A::CommandEncoder, DeviceError> {
if !self.is_open { if !self.is_open {
self.is_open = true; self.is_open = true;
let label = self.label.as_deref(); let hal_label = self.hal_label.as_deref();
unsafe { self.raw.begin_encoding(label)? }; unsafe { self.raw.begin_encoding(hal_label)? };
} }
Ok(&mut self.raw) Ok(&mut self.raw)
@ -229,9 +230,9 @@ impl<A: HalApi> CommandEncoder<A> {
/// its own label. /// its own label.
/// ///
/// The underlying hal encoder is put in the "recording" state. /// 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; self.is_open = true;
unsafe { self.raw.begin_encoding(label)? }; unsafe { self.raw.begin_encoding(hal_label)? };
Ok(()) Ok(())
} }
@ -339,13 +340,13 @@ impl<A: HalApi> CommandBuffer<A> {
encoder: A::CommandEncoder, encoder: A::CommandEncoder,
device: &Arc<Device<A>>, device: &Arc<Device<A>>,
#[cfg(feature = "trace")] enable_tracing: bool, #[cfg(feature = "trace")] enable_tracing: bool,
label: Option<String>, label: &Label,
) -> Self { ) -> Self {
CommandBuffer { CommandBuffer {
device: device.clone(), device: device.clone(),
limits: device.limits.clone(), limits: device.limits.clone(),
support_clear_texture: device.features.contains(wgt::Features::CLEAR_TEXTURE), support_clear_texture: device.features.contains(wgt::Features::CLEAR_TEXTURE),
info: ResourceInfo::new(label.as_deref().unwrap_or("<CommandBuffer>"), None), info: ResourceInfo::new(label, None),
data: Mutex::new( data: Mutex::new(
rank::COMMAND_BUFFER_DATA, rank::COMMAND_BUFFER_DATA,
Some(CommandBufferMutable { Some(CommandBufferMutable {
@ -353,7 +354,7 @@ impl<A: HalApi> CommandBuffer<A> {
raw: encoder, raw: encoder,
is_open: false, is_open: false,
list: Vec::new(), list: Vec::new(),
label, hal_label: label.to_hal(device.instance_flags).map(str::to_owned),
}, },
status: CommandEncoderStatus::Recording, status: CommandEncoderStatus::Recording,
trackers: Tracker::new(), trackers: Tracker::new(),

View File

@ -811,7 +811,7 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
fn start( fn start(
device: &'d Device<A>, device: &'d Device<A>,
label: Option<&str>, hal_label: Option<&str>,
color_attachments: &[Option<RenderPassColorAttachment>], color_attachments: &[Option<RenderPassColorAttachment>],
depth_stencil_attachment: Option<&RenderPassDepthStencilAttachment>, depth_stencil_attachment: Option<&RenderPassDepthStencilAttachment>,
timestamp_writes: Option<&RenderPassTimestampWrites>, timestamp_writes: Option<&RenderPassTimestampWrites>,
@ -1223,7 +1223,7 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
}; };
let hal_desc = hal::RenderPassDescriptor { let hal_desc = hal::RenderPassDescriptor {
label: hal_label(label, device.instance_flags), label: hal_label,
extent, extent,
sample_count, sample_count,
color_attachments: &colors, color_attachments: &colors,
@ -1389,7 +1389,7 @@ impl Global {
.instance .instance
.flags .flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS); .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); let pass_scope = PassErrorScope::PassEncoder(encoder_id);
@ -1436,7 +1436,7 @@ impl Global {
encoder.close().map_pass_err(pass_scope)?; encoder.close().map_pass_err(pass_scope)?;
// We will reset this to `Recording` if we succeed, acts as a fail-safe. // We will reset this to `Recording` if we succeed, acts as a fail-safe.
*status = CommandEncoderStatus::Error; *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 query_set_guard = hub.query_sets.read();
let view_guard = hub.texture_views.read(); let view_guard = hub.texture_views.read();
@ -1448,7 +1448,7 @@ impl Global {
let mut info = RenderPassInfo::start( let mut info = RenderPassInfo::start(
device, device,
label, hal_label,
color_attachments, color_attachments,
depth_stencil_attachment, depth_stencil_attachment,
timestamp_writes, timestamp_writes,

View File

@ -348,10 +348,7 @@ fn prepare_staging_buffer<A: HalApi>(
raw: Mutex::new(rank::STAGING_BUFFER_RAW, Some(buffer)), raw: Mutex::new(rank::STAGING_BUFFER_RAW, Some(buffer)),
device: device.clone(), device: device.clone(),
size, size,
info: ResourceInfo::new( info: ResourceInfo::new(&None, Some(device.tracker_indices.staging_buffers.clone())),
"<StagingBuffer>",
Some(device.tracker_indices.staging_buffers.clone()),
),
is_coherent: mapping.is_coherent, is_coherent: mapping.is_coherent,
}; };

View File

@ -268,7 +268,7 @@ impl<A: HalApi> Device<A> {
queue: OnceCell::new(), queue: OnceCell::new(),
queue_to_drop: OnceCell::new(), queue_to_drop: OnceCell::new(),
zero_buffer: Some(zero_buffer), zero_buffer: Some(zero_buffer),
info: ResourceInfo::new("<device>", None), info: ResourceInfo::new(&desc.label, None),
command_allocator, command_allocator,
active_submission_index: AtomicU64::new(0), active_submission_index: AtomicU64::new(0),
fence: RwLock::new(rank::DEVICE_FENCE, Some(fence)), fence: RwLock::new(rank::DEVICE_FENCE, Some(fence)),
@ -656,10 +656,7 @@ impl<A: HalApi> Device<A> {
), ),
sync_mapped_writes: Mutex::new(rank::BUFFER_SYNC_MAPPED_WRITES, None), sync_mapped_writes: Mutex::new(rank::BUFFER_SYNC_MAPPED_WRITES, None),
map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle),
info: ResourceInfo::new( info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.buffers.clone())),
desc.label.borrow_or_default(),
Some(self.tracker_indices.buffers.clone()),
),
bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()),
}) })
} }
@ -686,10 +683,7 @@ impl<A: HalApi> Device<A> {
mips: 0..desc.mip_level_count, mips: 0..desc.mip_level_count,
layers: 0..desc.array_layer_count(), layers: 0..desc.array_layer_count(),
}, },
info: ResourceInfo::new( info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.textures.clone())),
desc.label.borrow_or_default(),
Some(self.tracker_indices.textures.clone()),
),
clear_mode: RwLock::new(rank::TEXTURE_CLEAR_MODE, clear_mode), clear_mode: RwLock::new(rank::TEXTURE_CLEAR_MODE, clear_mode),
views: Mutex::new(rank::TEXTURE_VIEWS, Vec::new()), views: Mutex::new(rank::TEXTURE_VIEWS, Vec::new()),
bind_groups: Mutex::new(rank::TEXTURE_BIND_GROUPS, Vec::new()), bind_groups: Mutex::new(rank::TEXTURE_BIND_GROUPS, Vec::new()),
@ -712,10 +706,7 @@ impl<A: HalApi> Device<A> {
), ),
sync_mapped_writes: Mutex::new(rank::BUFFER_SYNC_MAPPED_WRITES, None), sync_mapped_writes: Mutex::new(rank::BUFFER_SYNC_MAPPED_WRITES, None),
map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle),
info: ResourceInfo::new( info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.buffers.clone())),
desc.label.borrow_or_default(),
Some(self.tracker_indices.buffers.clone()),
),
bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()),
} }
} }
@ -1292,7 +1283,7 @@ impl<A: HalApi> Device<A> {
samples: texture.desc.sample_count, samples: texture.desc.sample_count,
selector, selector,
info: ResourceInfo::new( info: ResourceInfo::new(
desc.label.borrow_or_default(), &desc.label,
Some(self.tracker_indices.texture_views.clone()), Some(self.tracker_indices.texture_views.clone()),
), ),
}) })
@ -1400,10 +1391,7 @@ impl<A: HalApi> Device<A> {
Ok(Sampler { Ok(Sampler {
raw: Some(raw), raw: Some(raw),
device: self.clone(), device: self.clone(),
info: ResourceInfo::new( info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.samplers.clone())),
desc.label.borrow_or_default(),
Some(self.tracker_indices.samplers.clone()),
),
comparison: desc.compare.is_some(), comparison: desc.compare.is_some(),
filtering: desc.min_filter == wgt::FilterMode::Linear filtering: desc.min_filter == wgt::FilterMode::Linear
|| desc.mag_filter == wgt::FilterMode::Linear, || desc.mag_filter == wgt::FilterMode::Linear,
@ -1536,7 +1524,7 @@ impl<A: HalApi> Device<A> {
raw: Some(raw), raw: Some(raw),
device: self.clone(), device: self.clone(),
interface: Some(interface), interface: Some(interface),
info: ResourceInfo::new(desc.label.borrow_or_default(), None), info: ResourceInfo::new(&desc.label, None),
}) })
} }
@ -1578,7 +1566,7 @@ impl<A: HalApi> Device<A> {
raw: Some(raw), raw: Some(raw),
device: self.clone(), device: self.clone(),
interface: None, interface: None,
info: ResourceInfo::new(desc.label.borrow_or_default(), None), info: ResourceInfo::new(&desc.label, None),
}) })
} }
@ -1599,7 +1587,7 @@ impl<A: HalApi> Device<A> {
self, self,
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
self.trace.lock().is_some(), self.trace.lock().is_some(),
label.to_hal(self.instance_flags).map(str::to_owned), label,
)) ))
} }
@ -1821,9 +1809,8 @@ impl<A: HalApi> Device<A> {
let bgl_flags = conv::bind_group_layout_flags(self.features); let bgl_flags = conv::bind_group_layout_flags(self.features);
let hal_bindings = entry_map.values().copied().collect::<Vec<_>>(); let hal_bindings = entry_map.values().copied().collect::<Vec<_>>();
let label = label.to_hal(self.instance_flags);
let hal_desc = hal::BindGroupLayoutDescriptor { let hal_desc = hal::BindGroupLayoutDescriptor {
label, label: label.to_hal(self.instance_flags),
flags: bgl_flags, flags: bgl_flags,
entries: &hal_bindings, entries: &hal_bindings,
}; };
@ -1851,10 +1838,7 @@ impl<A: HalApi> Device<A> {
entries: entry_map, entries: entry_map,
origin, origin,
binding_count_validator: count_validator, binding_count_validator: count_validator,
info: ResourceInfo::new( info: ResourceInfo::new(label, Some(self.tracker_indices.bind_group_layouts.clone())),
label.unwrap_or("<BindGroupLayout>"),
Some(self.tracker_indices.bind_group_layouts.clone()),
),
}) })
} }
@ -2283,10 +2267,7 @@ impl<A: HalApi> Device<A> {
raw: Snatchable::new(raw), raw: Snatchable::new(raw),
device: self.clone(), device: self.clone(),
layout: layout.clone(), layout: layout.clone(),
info: ResourceInfo::new( info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.bind_groups.clone())),
desc.label.borrow_or_default(),
Some(self.tracker_indices.bind_groups.clone()),
),
used, used,
used_buffer_ranges, used_buffer_ranges,
used_texture_ranges, used_texture_ranges,
@ -2569,7 +2550,7 @@ impl<A: HalApi> Device<A> {
raw: Some(raw), raw: Some(raw),
device: self.clone(), device: self.clone(),
info: ResourceInfo::new( info: ResourceInfo::new(
desc.label.borrow_or_default(), &desc.label,
Some(self.tracker_indices.pipeline_layouts.clone()), Some(self.tracker_indices.pipeline_layouts.clone()),
), ),
bind_group_layouts, bind_group_layouts,
@ -2758,7 +2739,7 @@ impl<A: HalApi> Device<A> {
_shader_module: shader_module, _shader_module: shader_module,
late_sized_buffer_groups, late_sized_buffer_groups,
info: ResourceInfo::new( info: ResourceInfo::new(
desc.label.borrow_or_default(), &desc.label,
Some(self.tracker_indices.compute_pipelines.clone()), Some(self.tracker_indices.compute_pipelines.clone()),
), ),
}; };
@ -3412,7 +3393,7 @@ impl<A: HalApi> Device<A> {
vertex_steps, vertex_steps,
late_sized_buffer_groups, late_sized_buffer_groups,
info: ResourceInfo::new( info: ResourceInfo::new(
desc.label.borrow_or_default(), &desc.label,
Some(self.tracker_indices.render_pipelines.clone()), Some(self.tracker_indices.render_pipelines.clone()),
), ),
}; };
@ -3460,7 +3441,7 @@ impl<A: HalApi> Device<A> {
let cache = pipeline::PipelineCache { let cache = pipeline::PipelineCache {
device: self.clone(), device: self.clone(),
info: ResourceInfo::new( info: ResourceInfo::new(
desc.label.borrow_or_default(), &desc.label,
Some(self.tracker_indices.pipeline_caches.clone()), Some(self.tracker_indices.pipeline_caches.clone()),
), ),
// This would be none in the error condition, which we don't implement yet // This would be none in the error condition, which we don't implement yet
@ -3578,7 +3559,7 @@ impl<A: HalApi> Device<A> {
Ok(QuerySet { Ok(QuerySet {
raw: Some(unsafe { self.raw().create_query_set(&hal_desc).unwrap() }), raw: Some(unsafe { self.raw().create_query_set(&hal_desc).unwrap() }),
device: self.clone(), 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(|_| ()), desc: desc.map_label(|_| ()),
}) })
} }

View File

@ -158,10 +158,6 @@ impl Resource for Surface {
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> { fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info &mut self.info
} }
fn label(&self) -> &str {
"<Surface>"
}
} }
impl Surface { impl Surface {
@ -204,7 +200,7 @@ impl<A: HalApi> Adapter<A> {
Self { Self {
raw, raw,
info: ResourceInfo::new("<Adapter>", None), info: ResourceInfo::new(&None, None),
} }
} }
@ -309,7 +305,7 @@ impl<A: HalApi> Adapter<A> {
let queue = Queue { let queue = Queue {
device: None, device: None,
raw: Some(hal_device.queue), raw: Some(hal_device.queue),
info: ResourceInfo::new("<Queue>", None), info: ResourceInfo::new(&None, None),
}; };
return Ok((device, queue)); return Ok((device, queue));
} }
@ -532,7 +528,7 @@ impl Global {
let surface = Surface { let surface = Surface {
presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), presentation: Mutex::new(rank::SURFACE_PRESENTATION, None),
info: ResourceInfo::new("<Surface>", None), info: ResourceInfo::new(&None, None),
#[cfg(vulkan)] #[cfg(vulkan)]
vulkan: init::<hal::api::Vulkan>( vulkan: init::<hal::api::Vulkan>(
@ -596,7 +592,7 @@ impl Global {
let surface = Surface { let surface = Surface {
presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), presentation: Mutex::new(rank::SURFACE_PRESENTATION, None),
info: ResourceInfo::new("<Surface>", None), info: ResourceInfo::new(&None, None),
metal: Some(self.instance.metal.as_ref().map_or( metal: Some(self.instance.metal.as_ref().map_or(
Err(CreateSurfaceError::BackendNotEnabled(Backend::Metal)), Err(CreateSurfaceError::BackendNotEnabled(Backend::Metal)),
|inst| { |inst| {
@ -625,7 +621,7 @@ impl Global {
) -> Result<SurfaceId, CreateSurfaceError> { ) -> Result<SurfaceId, CreateSurfaceError> {
let surface = Surface { let surface = Surface {
presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), presentation: Mutex::new(rank::SURFACE_PRESENTATION, None),
info: ResourceInfo::new("<Surface>", None), info: ResourceInfo::new(&None, None),
dx12: Some(create_surface_func( dx12: Some(create_surface_func(
self.instance self.instance
.dx12 .dx12

View File

@ -9,7 +9,10 @@ When this texture is presented, we remove it from the device tracker as well as
extract it from the hub. extract it from the hub.
!*/ !*/
use std::{borrow::Borrow, sync::Arc}; use std::{
borrow::{Borrow, Cow},
sync::Arc,
};
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
use crate::device::trace::Action; use crate::device::trace::Action;
@ -225,7 +228,7 @@ impl Global {
mips: 0..1, mips: 0..1,
}, },
info: ResourceInfo::new( info: ResourceInfo::new(
"<Surface Texture>", &Some(Cow::Borrowed("<Surface Texture>")),
Some(device.tracker_indices.textures.clone()), Some(device.tracker_indices.textures.clone()),
), ),
clear_mode: RwLock::new( clear_mode: RwLock::new(

View File

@ -255,7 +255,7 @@ mod tests {
s.spawn(|| { s.spawn(|| {
for _ in 0..1000 { for _ in 0..1000 {
let value = Arc::new(TestData { let value = Arc::new(TestData {
info: ResourceInfo::new("Test data", None), info: ResourceInfo::new(&None, None),
}); });
let new_id = registry.prepare(None); let new_id = registry.prepare(None);
let (id, _) = new_id.assign(value); let (id, _) = new_id.assign(value);

View File

@ -83,10 +83,8 @@ impl<T: Resource> Drop for ResourceInfo<T> {
} }
impl<T: Resource> ResourceInfo<T> { impl<T: Resource> ResourceInfo<T> {
// 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( pub(crate) fn new(
label: &str, label: &Label,
tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>, tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>,
) -> Self { ) -> Self {
let tracker_index = tracker_indices let tracker_index = tracker_indices
@ -98,7 +96,10 @@ impl<T: Resource> ResourceInfo<T> {
tracker_index, tracker_index,
tracker_indices, tracker_indices,
submission_index: AtomicUsize::new(0), 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<A: HalApi> Resource for StagingBuffer<A> {
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> { fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info &mut self.info
} }
fn label(&self) -> &str {
"<StagingBuffer>"
}
} }
impl<A: HalApi> ParentDevice<A> for StagingBuffer<A> { impl<A: HalApi> ParentDevice<A> for StagingBuffer<A> {