introduce Labeled trait

This commit is contained in:
teoxoy 2024-06-27 14:51:46 +02:00 committed by Teodor Tanasoaia
parent 985968fd38
commit ae7da8f669
21 changed files with 180 additions and 98 deletions

View File

@ -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<A: HalApi> {
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<A: HalApi> Drop for BindGroupLayout<A> {
}
crate::impl_resource_type!(BindGroupLayout);
crate::impl_labeled!(BindGroupLayout);
crate::impl_storage_item!(BindGroupLayout);
impl<A: HalApi> Resource for BindGroupLayout<A> {
@ -612,6 +615,8 @@ pub struct PipelineLayoutDescriptor<'a> {
pub struct PipelineLayout<A: HalApi> {
pub(crate) raw: Option<A::PipelineLayout>,
pub(crate) device: Arc<Device<A>>,
/// The `label` from the descriptor used to create the resource.
pub(crate) label: String,
pub(crate) info: ResourceInfo,
pub(crate) bind_group_layouts: ArrayVec<Arc<BindGroupLayout<A>>, { hal::MAX_BIND_GROUPS }>,
pub(crate) push_constant_ranges: ArrayVec<wgt::PushConstantRange, { SHADER_STAGE_COUNT }>,
@ -721,6 +726,7 @@ impl<A: HalApi> PipelineLayout<A> {
}
crate::impl_resource_type!(PipelineLayout);
crate::impl_labeled!(PipelineLayout);
crate::impl_storage_item!(PipelineLayout);
impl<A: HalApi> Resource for PipelineLayout<A> {
@ -840,6 +846,8 @@ pub struct BindGroup<A: HalApi> {
pub(crate) raw: Snatchable<A::BindGroup>,
pub(crate) device: Arc<Device<A>>,
pub(crate) layout: Arc<BindGroupLayout<A>>,
/// The `label` from the descriptor used to create the resource.
pub(crate) label: String,
pub(crate) info: ResourceInfo,
pub(crate) used: BindGroupStates<A>,
pub(crate) used_buffer_ranges: Vec<BufferInitTrackerAction<A>>,
@ -934,6 +942,7 @@ impl<A: HalApi> BindGroup<A> {
}
crate::impl_resource_type!(BindGroup);
crate::impl_labeled!(BindGroup);
crate::impl_storage_item!(BindGroup);
impl<A: HalApi> Resource for BindGroup<A> {

View File

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

View File

@ -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<A: HalApi> {
pub(super) buffer_memory_init_actions: Vec<BufferInitTrackerAction<A>>,
pub(super) texture_memory_init_actions: Vec<TextureInitTrackerAction<A>>,
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<A: HalApi> RenderBundle<A> {
}
crate::impl_resource_type!(RenderBundle);
crate::impl_labeled!(RenderBundle);
crate::impl_storage_item!(RenderBundle);
impl<A: HalApi> Resource for RenderBundle<A> {

View File

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

View File

@ -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},

View File

@ -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<A: HalApi> CommandBufferMutable<A> {
pub struct CommandBuffer<A: HalApi> {
pub(crate) device: Arc<Device<A>>,
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<A: HalApi> CommandBuffer<A> {
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<A: HalApi> CommandBuffer<A> {
}
crate::impl_resource_type!(CommandBuffer);
crate::impl_labeled!(CommandBuffer);
crate::impl_storage_item!(CommandBuffer);
impl<A: HalApi> Resource for CommandBuffer<A> {

View File

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

View File

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

View File

@ -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<T: Resource>(
pub(crate) fn check_compatible<T: Labeled>(
&self,
other: &Self,
res: &T,

View File

@ -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<A: HalApi> {
}
crate::impl_resource_type!(Queue);
// TODO: remove once we get rid of Registry.label_for_resource
impl<A: HalApi> Labeled for Queue<A> {
fn label(&self) -> &str {
""
}
}
crate::impl_storage_item!(Queue);
impl<A: HalApi> Resource for Queue<A> {
@ -344,7 +350,7 @@ fn prepare_staging_buffer<A: HalApi>(
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,
};

View File

@ -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<A: HalApi> {
pub(crate) queue: OnceCell<Weak<Queue<A>>>,
queue_to_drop: OnceCell<A::Queue>,
pub(crate) zero_buffer: Option<A::Buffer>,
/// The `label` from the descriptor used to create the resource.
label: String,
pub(crate) info: ResourceInfo,
pub(crate) command_allocator: command::CommandAllocator<A>,
@ -268,7 +270,8 @@ impl<A: HalApi> Device<A> {
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<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, 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<A: HalApi> Device<A> {
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<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, 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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
};
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<A: HalApi> Device<A> {
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<A: HalApi> Device<A> {
}
crate::impl_resource_type!(Device);
crate::impl_labeled!(Device);
crate::impl_storage_item!(Device);
impl<A: HalApi> Resource for Device<A> {

View File

@ -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<A: HalApi> Adapter<A> {
Self {
raw,
info: ResourceInfo::new(&None, None),
info: ResourceInfo::new(None),
}
}
@ -303,7 +309,7 @@ impl<A: HalApi> Adapter<A> {
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<A: HalApi> Adapter<A> {
}
crate::impl_resource_type!(Adapter);
// TODO: remove once we get rid of Registry.label_for_resource
impl<A: HalApi> Labeled for Adapter<A> {
fn label(&self) -> &str {
""
}
}
crate::impl_storage_item!(Adapter);
impl<A: HalApi> Resource for Adapter<A> {
@ -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::<hal::api::Vulkan>(
@ -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<SurfaceId, CreateSurfaceError> {
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

View File

@ -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> {

View File

@ -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<A: HalApi> {
pub(crate) raw: Option<A::ShaderModule>,
pub(crate) device: Arc<Device<A>>,
pub(crate) interface: Option<validation::Interface>,
/// The `label` from the descriptor used to create the resource.
pub(crate) label: String,
pub(crate) info: ResourceInfo,
}
@ -66,6 +68,7 @@ impl<A: HalApi> Drop for ShaderModule<A> {
}
crate::impl_resource_type!(ShaderModule);
crate::impl_labeled!(ShaderModule);
crate::impl_storage_item!(ShaderModule);
impl<A: HalApi> Resource for ShaderModule<A> {
@ -213,6 +216,8 @@ pub struct ComputePipeline<A: HalApi> {
pub(crate) device: Arc<Device<A>>,
pub(crate) _shader_module: Arc<ShaderModule<A>>,
pub(crate) late_sized_buffer_groups: ArrayVec<LateSizedBufferGroup, { hal::MAX_BIND_GROUPS }>,
/// The `label` from the descriptor used to create the resource.
pub(crate) label: String,
pub(crate) info: ResourceInfo,
}
@ -229,6 +234,7 @@ impl<A: HalApi> Drop for ComputePipeline<A> {
}
crate::impl_resource_type!(ComputePipeline);
crate::impl_labeled!(ComputePipeline);
crate::impl_storage_item!(ComputePipeline);
impl<A: HalApi> Resource for ComputePipeline<A> {
@ -276,6 +282,8 @@ impl From<hal::PipelineCacheError> for CreatePipelineCacheError {
pub struct PipelineCache<A: HalApi> {
pub(crate) raw: Option<A::PipelineCache>,
pub(crate) device: Arc<Device<A>>,
/// The `label` from the descriptor used to create the resource.
pub(crate) label: String,
pub(crate) info: ResourceInfo,
}
@ -292,6 +300,7 @@ impl<A: HalApi> Drop for PipelineCache<A> {
}
crate::impl_resource_type!(PipelineCache);
crate::impl_labeled!(PipelineCache);
crate::impl_storage_item!(PipelineCache);
impl<A: HalApi> Resource for PipelineCache<A> {
@ -530,6 +539,8 @@ pub struct RenderPipeline<A: HalApi> {
pub(crate) strip_index_format: Option<wgt::IndexFormat>,
pub(crate) vertex_steps: Vec<VertexStep>,
pub(crate) late_sized_buffer_groups: ArrayVec<LateSizedBufferGroup, { hal::MAX_BIND_GROUPS }>,
/// The `label` from the descriptor used to create the resource.
pub(crate) label: String,
pub(crate) info: ResourceInfo,
}
@ -546,6 +557,7 @@ impl<A: HalApi> Drop for RenderPipeline<A> {
}
crate::impl_resource_type!(RenderPipeline);
crate::impl_labeled!(RenderPipeline);
crate::impl_storage_item!(RenderPipeline);
impl<A: HalApi> Resource for RenderPipeline<A> {

View File

@ -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("<Surface Texture>")),
Some(device.tracker_indices.textures.clone()),
),
label: String::from("<Surface Texture>"),
info: ResourceInfo::new(Some(device.tracker_indices.textures.clone())),
clear_mode: RwLock::new(
rank::TEXTURE_CLEAR_MODE,
resource::TextureClearMode::Surface {

View File

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

View File

@ -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<Arc<SharedTrackerIndexAllocator>>,
) -> Self {
pub(crate) fn new(tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>) -> 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<A: HalApi>: Resource {
pub(crate) trait ParentDevice<A: HalApi>: Labeled {
fn device(&self) -> &Arc<Device<A>>;
fn same_device_as<O: ParentDevice<A>>(&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<A: HalApi> $crate::resource::Labeled for $ty<A> {
fn label(&self) -> &str {
&self.label
}
}
};
}
pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + Labeled {
fn as_info(&self) -> &ResourceInfo;
fn ref_count(self: &Arc<Self>) -> usize {
Arc::strong_count(self)
@ -191,12 +199,6 @@ pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + ResourceType {
fn is_equal(self: &Arc<Self>, other: &Arc<Self>) -> 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<A: HalApi> {
pub(crate) size: wgt::BufferAddress,
pub(crate) initialization_status: RwLock<BufferInitTracker>,
pub(crate) sync_mapped_writes: Mutex<Option<hal::MemoryRange>>,
/// The `label` from the descriptor used to create the resource.
pub(crate) label: String,
pub(crate) info: ResourceInfo,
pub(crate) map_state: Mutex<BufferMapState<A>>,
pub(crate) bind_groups: Mutex<Vec<Weak<BindGroup<A>>>>,
@ -786,6 +790,7 @@ pub enum CreateBufferError {
}
crate::impl_resource_type!(Buffer);
crate::impl_labeled!(Buffer);
crate::impl_storage_item!(Buffer);
impl<A: HalApi> Resource for Buffer<A> {
@ -877,6 +882,12 @@ impl<A: HalApi> Drop for StagingBuffer<A> {
}
crate::impl_resource_type!(StagingBuffer);
// TODO: remove once we get rid of Registry.label_for_resource
impl<A: HalApi> Labeled for StagingBuffer<A> {
fn label(&self) -> &str {
""
}
}
crate::impl_storage_item!(StagingBuffer);
impl<A: HalApi> Resource for StagingBuffer<A> {
@ -939,6 +950,8 @@ pub struct Texture<A: HalApi> {
pub(crate) format_features: wgt::TextureFormatFeatures,
pub(crate) initialization_status: RwLock<TextureInitTracker>,
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<TextureClearMode<A>>,
pub(crate) views: Mutex<Vec<Weak<TextureView<A>>>>,
@ -1412,6 +1425,7 @@ pub enum CreateTextureError {
}
crate::impl_resource_type!(Texture);
crate::impl_labeled!(Texture);
crate::impl_storage_item!(Texture);
impl<A: HalApi> Resource for Texture<A> {
@ -1498,6 +1512,8 @@ pub struct TextureView<A: HalApi> {
pub(crate) render_extent: Result<wgt::Extent3d, TextureViewNotRenderableReason>,
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<A: HalApi> Resource for TextureView<A> {
@ -1629,6 +1646,8 @@ pub struct SamplerDescriptor<'a> {
pub struct Sampler<A: HalApi> {
pub(crate) raw: Option<A::Sampler>,
pub(crate) device: Arc<Device<A>>,
/// 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<A: HalApi> Resource for Sampler<A> {
@ -1732,6 +1752,8 @@ pub type QuerySetDescriptor<'a> = wgt::QuerySetDescriptor<Label<'a>>;
pub struct QuerySet<A: HalApi> {
pub(crate) raw: Option<A::QuerySet>,
pub(crate) device: Arc<Device<A>>,
/// 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<A: HalApi> ParentDevice<A> for QuerySet<A> {
}
crate::impl_resource_type!(QuerySet);
crate::impl_labeled!(QuerySet);
crate::impl_storage_item!(QuerySet);
impl<A: HalApi> Resource for QuerySet<A> {

View File

@ -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<T> {
#[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;
}

View File

@ -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,
},
};

View File

@ -106,7 +106,7 @@ use crate::{
hal_api::HalApi,
lock::{rank, Mutex, RwLock},
pipeline,
resource::{self, Resource, ResourceErrorIdent},
resource::{self, Labeled, ResourceErrorIdent},
snatch::SnatchGuard,
};

View File

@ -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::{