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,
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),

View File

@ -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<A: HalApi> {
/// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder
is_open: bool,
label: Option<String>,
hal_label: Option<String>,
}
//TODO: handle errors better
@ -218,8 +219,8 @@ impl<A: HalApi> CommandEncoder<A> {
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<A: HalApi> CommandEncoder<A> {
/// 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<A: HalApi> CommandBuffer<A> {
encoder: A::CommandEncoder,
device: &Arc<Device<A>>,
#[cfg(feature = "trace")] enable_tracing: bool,
label: Option<String>,
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("<CommandBuffer>"), None),
info: ResourceInfo::new(label, None),
data: Mutex::new(
rank::COMMAND_BUFFER_DATA,
Some(CommandBufferMutable {
@ -353,7 +354,7 @@ impl<A: HalApi> CommandBuffer<A> {
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(),

View File

@ -811,7 +811,7 @@ impl<'a, 'd, A: HalApi> RenderPassInfo<'a, 'd, A> {
fn start(
device: &'d Device<A>,
label: Option<&str>,
hal_label: Option<&str>,
color_attachments: &[Option<RenderPassColorAttachment>],
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,

View File

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

View File

@ -268,7 +268,7 @@ impl<A: HalApi> Device<A> {
queue: OnceCell::new(),
queue_to_drop: OnceCell::new(),
zero_buffer: Some(zero_buffer),
info: ResourceInfo::new("<device>", 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<A: HalApi> Device<A> {
),
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
),
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
let bgl_flags = conv::bind_group_layout_flags(self.features);
let hal_bindings = entry_map.values().copied().collect::<Vec<_>>();
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<A: HalApi> Device<A> {
entries: entry_map,
origin,
binding_count_validator: count_validator,
info: ResourceInfo::new(
label.unwrap_or("<BindGroupLayout>"),
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
_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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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(|_| ()),
})
}

View File

@ -158,10 +158,6 @@ impl Resource for Surface {
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
fn label(&self) -> &str {
"<Surface>"
}
}
impl Surface {
@ -204,7 +200,7 @@ impl<A: HalApi> Adapter<A> {
Self {
raw,
info: ResourceInfo::new("<Adapter>", None),
info: ResourceInfo::new(&None, None),
}
}
@ -309,7 +305,7 @@ impl<A: HalApi> Adapter<A> {
let queue = Queue {
device: None,
raw: Some(hal_device.queue),
info: ResourceInfo::new("<Queue>", 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("<Surface>", None),
info: ResourceInfo::new(&None, None),
#[cfg(vulkan)]
vulkan: init::<hal::api::Vulkan>(
@ -596,7 +592,7 @@ impl Global {
let surface = Surface {
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(
Err(CreateSurfaceError::BackendNotEnabled(Backend::Metal)),
|inst| {
@ -625,7 +621,7 @@ impl Global {
) -> Result<SurfaceId, CreateSurfaceError> {
let surface = Surface {
presentation: Mutex::new(rank::SURFACE_PRESENTATION, None),
info: ResourceInfo::new("<Surface>", None),
info: ResourceInfo::new(&None, None),
dx12: Some(create_surface_func(
self.instance
.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.
!*/
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(
"<Surface Texture>",
&Some(Cow::Borrowed("<Surface Texture>")),
Some(device.tracker_indices.textures.clone()),
),
clear_mode: RwLock::new(

View File

@ -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);

View File

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