diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index c47296cd7e..a245756fa1 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -7,8 +7,8 @@ use crate::{ id::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId}, init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction}, resource::{ - DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError, ParentDevice, - Resource, ResourceErrorIdent, ResourceInfo, + DestroyedResourceError, Labeled, MissingBufferUsageError, MissingTextureUsageError, + ParentDevice, Resource, ResourceErrorIdent, ResourceInfo, }, resource_log, snatch::{SnatchGuard, Snatchable}, @@ -474,6 +474,8 @@ pub struct BindGroupLayout { pub(crate) origin: bgl::Origin, #[allow(unused)] pub(crate) binding_count_validator: BindingTypeMaxCountValidator, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, } @@ -493,6 +495,7 @@ impl Drop for BindGroupLayout { } crate::impl_resource_type!(BindGroupLayout); +crate::impl_labeled!(BindGroupLayout); crate::impl_storage_item!(BindGroupLayout); impl Resource for BindGroupLayout { @@ -612,6 +615,8 @@ pub struct PipelineLayoutDescriptor<'a> { pub struct PipelineLayout { pub(crate) raw: Option, pub(crate) device: Arc>, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, pub(crate) bind_group_layouts: ArrayVec>, { hal::MAX_BIND_GROUPS }>, pub(crate) push_constant_ranges: ArrayVec, @@ -721,6 +726,7 @@ impl PipelineLayout { } crate::impl_resource_type!(PipelineLayout); +crate::impl_labeled!(PipelineLayout); crate::impl_storage_item!(PipelineLayout); impl Resource for PipelineLayout { @@ -840,6 +846,8 @@ pub struct BindGroup { pub(crate) raw: Snatchable, pub(crate) device: Arc>, pub(crate) layout: Arc>, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, pub(crate) used: BindGroupStates, pub(crate) used_buffer_ranges: Vec>, @@ -934,6 +942,7 @@ impl BindGroup { } crate::impl_resource_type!(BindGroup); +crate::impl_labeled!(BindGroup); crate::impl_storage_item!(BindGroup); impl Resource for BindGroup { diff --git a/wgpu-core/src/command/bind.rs b/wgpu-core/src/command/bind.rs index c195a207ae..d8091cbfea 100644 --- a/wgpu-core/src/command/bind.rs +++ b/wgpu-core/src/command/bind.rs @@ -5,7 +5,7 @@ use crate::{ device::SHADER_STAGE_COUNT, hal_api::HalApi, pipeline::LateSizedBufferGroup, - resource::Resource, + resource::Labeled, }; use arrayvec::ArrayVec; @@ -15,7 +15,12 @@ type BindGroupMask = u8; mod compat { use arrayvec::ArrayVec; - use crate::{binding_model::BindGroupLayout, device::bgl, hal_api::HalApi, resource::Resource}; + use crate::{ + binding_model::BindGroupLayout, + device::bgl, + hal_api::HalApi, + resource::{Labeled, Resource}, + }; use std::{ops::Range, sync::Arc}; #[derive(Debug, Clone)] diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index a6e46ed1b1..fffa329c8f 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -95,11 +95,11 @@ use crate::{ id, init_tracker::{BufferInitTrackerAction, MemoryInitKind, TextureInitTrackerAction}, pipeline::{PipelineFlags, RenderPipeline, VertexStep}, - resource::{Buffer, DestroyedResourceError, ParentDevice, Resource, ResourceInfo}, + resource::{Buffer, DestroyedResourceError, Labeled, ParentDevice, Resource, ResourceInfo}, resource_log, snatch::SnatchGuard, track::RenderBundleScope, - Label, + Label, LabelHelpers, }; use arrayvec::ArrayVec; @@ -578,7 +578,8 @@ impl RenderBundleEncoder { buffer_memory_init_actions, texture_memory_init_actions, context: self.context, - info: ResourceInfo::new(&desc.label, Some(tracker_indices)), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(tracker_indices)), discard_hal_labels, }) } @@ -970,6 +971,8 @@ pub struct RenderBundle { pub(super) buffer_memory_init_actions: Vec>, pub(super) texture_memory_init_actions: Vec>, pub(super) context: RenderPassContext, + /// The `label` from the descriptor used to create the resource. + label: String, pub(crate) info: ResourceInfo, discard_hal_labels: bool, } @@ -1181,6 +1184,7 @@ impl RenderBundle { } crate::impl_resource_type!(RenderBundle); +crate::impl_labeled!(RenderBundle); crate::impl_storage_item!(RenderBundle); impl Resource for RenderBundle { diff --git a/wgpu-core/src/command/clear.rs b/wgpu-core/src/command/clear.rs index f9d3249cca..7a0828fde8 100644 --- a/wgpu-core/src/command/clear.rs +++ b/wgpu-core/src/command/clear.rs @@ -12,7 +12,7 @@ use crate::{ id::{BufferId, CommandEncoderId, TextureId}, init_tracker::{MemoryInitKind, TextureInitRange}, resource::{ - DestroyedResourceError, ParentDevice, Resource, ResourceErrorIdent, Texture, + DestroyedResourceError, Labeled, ParentDevice, ResourceErrorIdent, Texture, TextureClearMode, }, snatch::SnatchGuard, diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index a56860c208..e780c954ec 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -19,8 +19,8 @@ use crate::{ init_tracker::{BufferInitTrackerAction, MemoryInitKind}, pipeline::ComputePipeline, resource::{ - self, Buffer, DestroyedResourceError, MissingBufferUsageError, ParentDevice, Resource, - ResourceErrorIdent, + self, Buffer, DestroyedResourceError, Labeled, MissingBufferUsageError, ParentDevice, + Resource, ResourceErrorIdent, }, snatch::SnatchGuard, track::{ResourceUsageCompatibilityError, Tracker, TrackerIndex, UsageScope}, diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 19ba12b2c0..b5355b2b5a 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -36,7 +36,7 @@ use crate::lock::{rank, Mutex}; use crate::snatch::SnatchGuard; use crate::init_tracker::BufferInitTrackerAction; -use crate::resource::{ParentDevice, Resource, ResourceInfo}; +use crate::resource::{Labeled, ParentDevice, Resource, ResourceInfo}; use crate::track::{Tracker, UsageScope}; use crate::LabelHelpers; use crate::{api_log, global::Global, hal_api::HalApi, id, resource_log, Label}; @@ -311,6 +311,8 @@ impl CommandBufferMutable { pub struct CommandBuffer { pub(crate) device: Arc>, support_clear_texture: bool, + /// The `label` from the descriptor used to create the resource. + label: String, pub(crate) info: ResourceInfo, /// The mutable state of this command buffer. @@ -349,7 +351,8 @@ impl CommandBuffer { CommandBuffer { device: device.clone(), support_clear_texture: device.features.contains(wgt::Features::CLEAR_TEXTURE), - info: ResourceInfo::new(label, None), + label: label.to_string(), + info: ResourceInfo::new(None), data: Mutex::new( rank::COMMAND_BUFFER_DATA, Some(CommandBufferMutable { @@ -528,6 +531,7 @@ impl CommandBuffer { } crate::impl_resource_type!(CommandBuffer); +crate::impl_labeled!(CommandBuffer); crate::impl_storage_item!(CommandBuffer); impl Resource for CommandBuffer { diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 6056e956b3..104f055aa6 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -28,8 +28,8 @@ use crate::{ init_tracker::{MemoryInitKind, TextureInitRange, TextureInitTrackerAction}, pipeline::{self, PipelineFlags}, resource::{ - DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError, ParentDevice, - QuerySet, Texture, TextureView, TextureViewNotRenderableReason, + DestroyedResourceError, Labeled, MissingBufferUsageError, MissingTextureUsageError, + ParentDevice, QuerySet, Texture, TextureView, TextureViewNotRenderableReason, }, track::{ResourceUsageCompatibilityError, TextureSelector, Tracker, UsageScope}, Label, diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index c77063a023..b915e02383 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -10,7 +10,7 @@ use crate::{ lock::Mutex, pipeline::{ComputePipeline, RenderPipeline}, resource::{ - self, Buffer, DestroyedBuffer, DestroyedTexture, QuerySet, Resource, Sampler, + self, Buffer, DestroyedBuffer, DestroyedTexture, Labeled, QuerySet, Resource, Sampler, StagingBuffer, Texture, TextureView, }, snatch::SnatchGuard, diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 39b74ddcbb..cd9eaa7d24 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -4,7 +4,7 @@ use crate::{ hub::Hub, id::{BindGroupLayoutId, PipelineLayoutId}, resource::{ - Buffer, BufferAccessError, BufferAccessResult, BufferMapOperation, Resource, + Buffer, BufferAccessError, BufferAccessResult, BufferMapOperation, Labeled, ResourceErrorIdent, }, snatch::SnatchGuard, @@ -106,7 +106,7 @@ pub enum RenderPassCompatibilityError { impl RenderPassContext { // Assumes the renderpass only contains one subpass - pub(crate) fn check_compatible( + pub(crate) fn check_compatible( &self, other: &Self, res: &T, diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index a9d51e5f2b..45c97a973e 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -17,8 +17,8 @@ use crate::{ lock::{rank, Mutex, RwLockWriteGuard}, resource::{ Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, DestroyedResourceError, - DestroyedTexture, ParentDevice, Resource, ResourceErrorIdent, ResourceInfo, StagingBuffer, - Texture, TextureInner, + DestroyedTexture, Labeled, ParentDevice, Resource, ResourceErrorIdent, ResourceInfo, + StagingBuffer, Texture, TextureInner, }, resource_log, track::{self, TrackerIndex}, @@ -43,6 +43,12 @@ pub struct Queue { } crate::impl_resource_type!(Queue); +// TODO: remove once we get rid of Registry.label_for_resource +impl Labeled for Queue { + fn label(&self) -> &str { + "" + } +} crate::impl_storage_item!(Queue); impl Resource for Queue { @@ -344,7 +350,7 @@ fn prepare_staging_buffer( raw: Mutex::new(rank::STAGING_BUFFER_RAW, Some(buffer)), device: device.clone(), size, - info: ResourceInfo::new(&None, Some(device.tracker_indices.staging_buffers.clone())), + info: ResourceInfo::new(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 1ce4032b21..02633157d5 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -24,7 +24,7 @@ use crate::{ pool::ResourcePool, registry::Registry, resource::{ - self, Buffer, ParentDevice, QuerySet, Resource, ResourceInfo, Sampler, Texture, + self, Buffer, Labeled, ParentDevice, QuerySet, Resource, ResourceInfo, Sampler, Texture, TextureView, TextureViewNotRenderableReason, }, resource_log, @@ -94,6 +94,8 @@ pub struct Device { pub(crate) queue: OnceCell>>, queue_to_drop: OnceCell, pub(crate) zero_buffer: Option, + /// The `label` from the descriptor used to create the resource. + label: String, pub(crate) info: ResourceInfo, pub(crate) command_allocator: command::CommandAllocator, @@ -268,7 +270,8 @@ impl Device { queue: OnceCell::new(), queue_to_drop: OnceCell::new(), zero_buffer: Some(zero_buffer), - info: ResourceInfo::new(&desc.label, None), + label: desc.label.to_string(), + info: ResourceInfo::new(None), command_allocator, active_submission_index: AtomicU64::new(0), fence: RwLock::new(rank::DEVICE_FENCE, Some(fence)), @@ -656,7 +659,8 @@ 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, Some(self.tracker_indices.buffers.clone())), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.buffers.clone())), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()), }) } @@ -683,7 +687,8 @@ impl Device { mips: 0..desc.mip_level_count, layers: 0..desc.array_layer_count(), }, - info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.textures.clone())), + label: desc.label.to_string(), + info: ResourceInfo::new(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()), @@ -706,7 +711,8 @@ 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, Some(self.tracker_indices.buffers.clone())), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.buffers.clone())), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, Vec::new()), } } @@ -1282,10 +1288,8 @@ impl Device { render_extent, samples: texture.desc.sample_count, selector, - info: ResourceInfo::new( - &desc.label, - Some(self.tracker_indices.texture_views.clone()), - ), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.texture_views.clone())), }) } @@ -1391,7 +1395,8 @@ impl Device { Ok(Sampler { raw: Some(raw), device: self.clone(), - info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.samplers.clone())), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.samplers.clone())), comparison: desc.compare.is_some(), filtering: desc.min_filter == wgt::FilterMode::Linear || desc.mag_filter == wgt::FilterMode::Linear, @@ -1524,7 +1529,8 @@ impl Device { raw: Some(raw), device: self.clone(), interface: Some(interface), - info: ResourceInfo::new(&desc.label, None), + label: desc.label.to_string(), + info: ResourceInfo::new(None), }) } @@ -1566,7 +1572,8 @@ impl Device { raw: Some(raw), device: self.clone(), interface: None, - info: ResourceInfo::new(&desc.label, None), + label: desc.label.to_string(), + info: ResourceInfo::new(None), }) } @@ -1838,7 +1845,8 @@ impl Device { entries: entry_map, origin, binding_count_validator: count_validator, - info: ResourceInfo::new(label, Some(self.tracker_indices.bind_group_layouts.clone())), + label: label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.bind_group_layouts.clone())), }) } @@ -2267,7 +2275,8 @@ impl Device { raw: Snatchable::new(raw), device: self.clone(), layout: layout.clone(), - info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.bind_groups.clone())), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.bind_groups.clone())), used, used_buffer_ranges, used_texture_ranges, @@ -2549,10 +2558,8 @@ impl Device { Ok(binding_model::PipelineLayout { raw: Some(raw), device: self.clone(), - info: ResourceInfo::new( - &desc.label, - Some(self.tracker_indices.pipeline_layouts.clone()), - ), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.pipeline_layouts.clone())), bind_group_layouts, push_constant_ranges: desc.push_constant_ranges.iter().cloned().collect(), }) @@ -2738,10 +2745,8 @@ impl Device { device: self.clone(), _shader_module: shader_module, late_sized_buffer_groups, - info: ResourceInfo::new( - &desc.label, - Some(self.tracker_indices.compute_pipelines.clone()), - ), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.compute_pipelines.clone())), }; Ok(pipeline) } @@ -3392,10 +3397,8 @@ impl Device { strip_index_format: desc.primitive.strip_index_format, vertex_steps, late_sized_buffer_groups, - info: ResourceInfo::new( - &desc.label, - Some(self.tracker_indices.render_pipelines.clone()), - ), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.render_pipelines.clone())), }; Ok(pipeline) } @@ -3440,10 +3443,8 @@ impl Device { }; let cache = pipeline::PipelineCache { device: self.clone(), - info: ResourceInfo::new( - &desc.label, - Some(self.tracker_indices.pipeline_caches.clone()), - ), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.pipeline_caches.clone())), // This would be none in the error condition, which we don't implement yet raw: Some(raw), }; @@ -3559,7 +3560,8 @@ impl Device { Ok(QuerySet { raw: Some(unsafe { self.raw().create_query_set(&hal_desc).unwrap() }), device: self.clone(), - info: ResourceInfo::new(&desc.label, Some(self.tracker_indices.query_sets.clone())), + label: desc.label.to_string(), + info: ResourceInfo::new(Some(self.tracker_indices.query_sets.clone())), desc: desc.map_label(|_| ()), }) } @@ -3668,6 +3670,7 @@ impl Device { } crate::impl_resource_type!(Device); +crate::impl_labeled!(Device); crate::impl_storage_item!(Device); impl Resource for Device { diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index feaa9d2439..c3e2483467 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -9,7 +9,7 @@ use crate::{ id::{markers, AdapterId, DeviceId, Id, Marker, QueueId, SurfaceId}, lock::{rank, Mutex}, present::Presentation, - resource::{Resource, ResourceInfo, ResourceType}, + resource::{Labeled, Resource, ResourceInfo, ResourceType}, resource_log, LabelHelpers, DOWNLEVEL_WARNING_MESSAGE, }; @@ -148,6 +148,12 @@ pub struct Surface { impl ResourceType for Surface { const TYPE: &'static str = "Surface"; } +// TODO: remove once we get rid of Registry.label_for_resource +impl Labeled for Surface { + fn label(&self) -> &str { + "" + } +} impl crate::storage::StorageItem for Surface { type Marker = markers::Surface; } @@ -198,7 +204,7 @@ impl Adapter { Self { raw, - info: ResourceInfo::new(&None, None), + info: ResourceInfo::new(None), } } @@ -303,7 +309,7 @@ impl Adapter { let queue = Queue { device: None, raw: Some(hal_device.queue), - info: ResourceInfo::new(&None, None), + info: ResourceInfo::new(None), }; return Ok((device, queue)); } @@ -372,6 +378,12 @@ impl Adapter { } crate::impl_resource_type!(Adapter); +// TODO: remove once we get rid of Registry.label_for_resource +impl Labeled for Adapter { + fn label(&self) -> &str { + "" + } +} crate::impl_storage_item!(Adapter); impl Resource for Adapter { @@ -521,7 +533,7 @@ impl Global { let surface = Surface { presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), - info: ResourceInfo::new(&None, None), + info: ResourceInfo::new(None), #[cfg(vulkan)] vulkan: init::( @@ -585,7 +597,7 @@ impl Global { let surface = Surface { presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), - info: ResourceInfo::new(&None, None), + info: ResourceInfo::new(None), metal: Some(self.instance.metal.as_ref().map_or( Err(CreateSurfaceError::BackendNotEnabled(Backend::Metal)), |inst| { @@ -614,7 +626,7 @@ impl Global { ) -> Result { let surface = Surface { presentation: Mutex::new(rank::SURFACE_PRESENTATION, None), - info: ResourceInfo::new(&None, None), + info: ResourceInfo::new(None), dx12: Some(create_surface_func( self.instance .dx12 diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index cb6968a5a5..b8b468d08e 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -99,6 +99,7 @@ trait LabelHelpers<'a> { fn borrow_option(&'a self) -> Option<&'a str>; fn to_hal(&'a self, flags: wgt::InstanceFlags) -> Option<&'a str>; fn borrow_or_default(&'a self) -> &'a str; + fn to_string(&self) -> String; } impl<'a> LabelHelpers<'a> for Label<'a> { fn borrow_option(&'a self) -> Option<&'a str> { @@ -114,6 +115,9 @@ impl<'a> LabelHelpers<'a> for Label<'a> { fn borrow_or_default(&'a self) -> &'a str { self.borrow_option().unwrap_or_default() } + fn to_string(&self) -> String { + self.as_ref().map(|cow| cow.to_string()).unwrap_or_default() + } } pub fn hal_label(opt: Option<&str>, flags: wgt::InstanceFlags) -> Option<&str> { diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 2b89e5e33c..f31ca8c3b0 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -5,7 +5,7 @@ use crate::{ device::{Device, DeviceError, MissingDownlevelFlags, MissingFeatures, RenderPassContext}, hal_api::HalApi, id::{PipelineCacheId, PipelineLayoutId, ShaderModuleId}, - resource::{ParentDevice, Resource, ResourceInfo}, + resource::{Labeled, ParentDevice, Resource, ResourceInfo}, resource_log, validation, Label, }; use arrayvec::ArrayVec; @@ -50,6 +50,8 @@ pub struct ShaderModule { pub(crate) raw: Option, pub(crate) device: Arc>, pub(crate) interface: Option, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, } @@ -66,6 +68,7 @@ impl Drop for ShaderModule { } crate::impl_resource_type!(ShaderModule); +crate::impl_labeled!(ShaderModule); crate::impl_storage_item!(ShaderModule); impl Resource for ShaderModule { @@ -213,6 +216,8 @@ pub struct ComputePipeline { pub(crate) device: Arc>, pub(crate) _shader_module: Arc>, pub(crate) late_sized_buffer_groups: ArrayVec, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, } @@ -229,6 +234,7 @@ impl Drop for ComputePipeline { } crate::impl_resource_type!(ComputePipeline); +crate::impl_labeled!(ComputePipeline); crate::impl_storage_item!(ComputePipeline); impl Resource for ComputePipeline { @@ -276,6 +282,8 @@ impl From for CreatePipelineCacheError { pub struct PipelineCache { pub(crate) raw: Option, pub(crate) device: Arc>, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, } @@ -292,6 +300,7 @@ impl Drop for PipelineCache { } crate::impl_resource_type!(PipelineCache); +crate::impl_labeled!(PipelineCache); crate::impl_storage_item!(PipelineCache); impl Resource for PipelineCache { @@ -530,6 +539,8 @@ pub struct RenderPipeline { pub(crate) strip_index_format: Option, pub(crate) vertex_steps: Vec, pub(crate) late_sized_buffer_groups: ArrayVec, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, } @@ -546,6 +557,7 @@ impl Drop for RenderPipeline { } crate::impl_resource_type!(RenderPipeline); +crate::impl_labeled!(RenderPipeline); crate::impl_storage_item!(RenderPipeline); impl Resource for RenderPipeline { diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index 0376260e26..209f269e29 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -9,10 +9,7 @@ When this texture is presented, we remove it from the device tracker as well as extract it from the hub. !*/ -use std::{ - borrow::{Borrow, Cow}, - sync::Arc, -}; +use std::{borrow::Borrow, sync::Arc}; #[cfg(feature = "trace")] use crate::device::trace::Action; @@ -227,10 +224,8 @@ impl Global { layers: 0..1, mips: 0..1, }, - info: ResourceInfo::new( - &Some(Cow::Borrowed("")), - Some(device.tracker_indices.textures.clone()), - ), + label: String::from(""), + info: ResourceInfo::new(Some(device.tracker_indices.textures.clone())), clear_mode: RwLock::new( rank::TEXTURE_CLEAR_MODE, resource::TextureClearMode::Surface { diff --git a/wgpu-core/src/registry.rs b/wgpu-core/src/registry.rs index a1e3858412..6810ae221a 100644 --- a/wgpu-core/src/registry.rs +++ b/wgpu-core/src/registry.rs @@ -202,7 +202,7 @@ mod tests { use crate::{ id::Marker, - resource::{Resource, ResourceInfo, ResourceType}, + resource::{Labeled, Resource, ResourceInfo, ResourceType}, storage::StorageItem, }; @@ -216,7 +216,12 @@ mod tests { impl ResourceType for TestData { const TYPE: &'static str = "TestData"; } - + // TODO: remove once we get rid of Registry.label_for_resource + impl Labeled for TestData { + fn label(&self) -> &str { + "" + } + } impl StorageItem for TestData { type Marker = TestDataId; } @@ -235,7 +240,7 @@ mod tests { s.spawn(|| { for _ in 0..1000 { let value = Arc::new(TestData { - info: ResourceInfo::new(&None, None), + info: ResourceInfo::new(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 9d7786756c..2db8956962 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -65,9 +65,6 @@ pub(crate) struct ResourceInfo { /// resources used in that submission and any lower-numbered submissions are /// no longer in use by the GPU. submission_index: AtomicUsize, - - /// The `label` from the descriptor used to create the resource. - label: String, } impl Drop for ResourceInfo { @@ -79,10 +76,7 @@ impl Drop for ResourceInfo { } impl ResourceInfo { - pub(crate) fn new( - label: &Label, - tracker_indices: Option>, - ) -> Self { + pub(crate) fn new(tracker_indices: Option>) -> Self { let tracker_index = tracker_indices .as_ref() .map(|indices| indices.alloc()) @@ -91,10 +85,6 @@ impl ResourceInfo { tracker_index, tracker_indices, submission_index: AtomicUsize::new(0), - label: label - .as_ref() - .map(|label| label.to_string()) - .unwrap_or_default(), } } @@ -127,7 +117,7 @@ impl std::fmt::Display for ResourceErrorIdent { } } -pub(crate) trait ParentDevice: Resource { +pub(crate) trait ParentDevice: Labeled { fn device(&self) -> &Arc>; fn same_device_as>(&self, other: &O) -> Result<(), DeviceError> { @@ -170,17 +160,35 @@ macro_rules! impl_resource_type { }; } -pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + ResourceType { - fn as_info(&self) -> &ResourceInfo; - +pub(crate) trait Labeled: ResourceType { /// Returns a string identifying this resource for logging and errors. /// /// It may be a user-provided string or it may be a placeholder from wgpu. /// /// It is non-empty unless the user-provided string was empty. - fn label(&self) -> &str { - &self.as_info().label + fn label(&self) -> &str; + + fn error_ident(&self) -> ResourceErrorIdent { + ResourceErrorIdent { + r#type: Self::TYPE, + label: self.label().to_owned(), + } } +} + +#[macro_export] +macro_rules! impl_labeled { + ($ty:ident) => { + impl $crate::resource::Labeled for $ty { + fn label(&self) -> &str { + &self.label + } + } + }; +} + +pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + Labeled { + fn as_info(&self) -> &ResourceInfo; fn ref_count(self: &Arc) -> usize { Arc::strong_count(self) @@ -191,12 +199,6 @@ pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + ResourceType { fn is_equal(self: &Arc, other: &Arc) -> bool { Arc::ptr_eq(self, other) } - fn error_ident(&self) -> ResourceErrorIdent { - ResourceErrorIdent { - r#type: Self::TYPE, - label: self.label().to_owned(), - } - } } /// The status code provided to the buffer mapping callback. @@ -444,6 +446,8 @@ pub struct Buffer { pub(crate) size: wgt::BufferAddress, pub(crate) initialization_status: RwLock, pub(crate) sync_mapped_writes: Mutex>, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, pub(crate) map_state: Mutex>, pub(crate) bind_groups: Mutex>>>, @@ -786,6 +790,7 @@ pub enum CreateBufferError { } crate::impl_resource_type!(Buffer); +crate::impl_labeled!(Buffer); crate::impl_storage_item!(Buffer); impl Resource for Buffer { @@ -877,6 +882,12 @@ impl Drop for StagingBuffer { } crate::impl_resource_type!(StagingBuffer); +// TODO: remove once we get rid of Registry.label_for_resource +impl Labeled for StagingBuffer { + fn label(&self) -> &str { + "" + } +} crate::impl_storage_item!(StagingBuffer); impl Resource for StagingBuffer { @@ -939,6 +950,8 @@ pub struct Texture { pub(crate) format_features: wgt::TextureFormatFeatures, pub(crate) initialization_status: RwLock, pub(crate) full_range: TextureSelector, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, pub(crate) clear_mode: RwLock>, pub(crate) views: Mutex>>>, @@ -1412,6 +1425,7 @@ pub enum CreateTextureError { } crate::impl_resource_type!(Texture); +crate::impl_labeled!(Texture); crate::impl_storage_item!(Texture); impl Resource for Texture { @@ -1498,6 +1512,8 @@ pub struct TextureView { pub(crate) render_extent: Result, pub(crate) samples: u32, pub(crate) selector: TextureSelector, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, } @@ -1582,6 +1598,7 @@ pub enum CreateTextureViewError { pub enum TextureViewDestroyError {} crate::impl_resource_type!(TextureView); +crate::impl_labeled!(TextureView); crate::impl_storage_item!(TextureView); impl Resource for TextureView { @@ -1629,6 +1646,8 @@ pub struct SamplerDescriptor<'a> { pub struct Sampler { pub(crate) raw: Option, pub(crate) device: Arc>, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, /// `true` if this is a comparison sampler pub(crate) comparison: bool, @@ -1699,6 +1718,7 @@ pub enum CreateSamplerError { } crate::impl_resource_type!(Sampler); +crate::impl_labeled!(Sampler); crate::impl_storage_item!(Sampler); impl Resource for Sampler { @@ -1732,6 +1752,8 @@ pub type QuerySetDescriptor<'a> = wgt::QuerySetDescriptor>; pub struct QuerySet { pub(crate) raw: Option, pub(crate) device: Arc>, + /// The `label` from the descriptor used to create the resource. + pub(crate) label: String, pub(crate) info: ResourceInfo, pub(crate) desc: wgt::QuerySetDescriptor<()>, } @@ -1755,6 +1777,7 @@ impl ParentDevice for QuerySet { } crate::impl_resource_type!(QuerySet); +crate::impl_labeled!(QuerySet); crate::impl_storage_item!(QuerySet); impl Resource for QuerySet { diff --git a/wgpu-core/src/storage.rs b/wgpu-core/src/storage.rs index c8bcb7ca8f..8d6733a785 100644 --- a/wgpu-core/src/storage.rs +++ b/wgpu-core/src/storage.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use wgt::Backend; use crate::id::{Id, Marker}; -use crate::resource::{Resource, ResourceType}; +use crate::resource::{Labeled, ResourceType}; use crate::{Epoch, Index}; /// An entry in a `Storage::map` table. @@ -27,8 +27,8 @@ pub(crate) enum Element { #[derive(Clone, Debug)] pub(crate) struct InvalidId; -// The Resource bound is still needed because of label_for_resource -pub(crate) trait StorageItem: ResourceType + Resource { +// The Labeled bound is still needed because of label_for_resource +pub(crate) trait StorageItem: ResourceType + Labeled { type Marker: Marker; } diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index 093498f9e4..adc76fcb62 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -15,7 +15,7 @@ use crate::{ resource_log, snatch::SnatchGuard, track::{ - invalid_resource_state, skip_barrier, ResourceMetadata, ResourceMetadataProvider, + invalid_resource_state, skip_barrier, Labeled, ResourceMetadata, ResourceMetadataProvider, ResourceUsageCompatibilityError, ResourceUses, }, }; diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index 0071b6e002..d97824ecf8 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -106,7 +106,7 @@ use crate::{ hal_api::HalApi, lock::{rank, Mutex, RwLock}, pipeline, - resource::{self, Resource, ResourceErrorIdent}, + resource::{self, Labeled, ResourceErrorIdent}, snatch::SnatchGuard, }; diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index b8e3ccce61..d5bb0681d4 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -25,7 +25,7 @@ use super::{ use crate::{ hal_api::HalApi, lock::{rank, Mutex}, - resource::{Resource, Texture, TextureInner}, + resource::{Labeled, Resource, Texture, TextureInner}, resource_log, snatch::SnatchGuard, track::{