diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 25dc50774..f3de6d69a 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -8,7 +8,7 @@ use crate::{ init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction}, resource::{ DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError, ParentDevice, - Resource, ResourceErrorIdent, ResourceInfo, ResourceType, + Resource, ResourceErrorIdent, ResourceInfo, }, resource_log, snatch::{SnatchGuard, Snatchable}, @@ -492,9 +492,9 @@ impl Drop for BindGroupLayout { } } -impl Resource for BindGroupLayout { - const TYPE: ResourceType = "BindGroupLayout"; +crate::impl_resource_type!(BindGroupLayout); +impl Resource for BindGroupLayout { type Marker = crate::id::markers::BindGroupLayout; fn as_info(&self) -> &ResourceInfo { @@ -721,9 +721,9 @@ impl PipelineLayout { } } -impl Resource for PipelineLayout { - const TYPE: ResourceType = "PipelineLayout"; +crate::impl_resource_type!(PipelineLayout); +impl Resource for PipelineLayout { type Marker = crate::id::markers::PipelineLayout; fn as_info(&self) -> &ResourceInfo { @@ -935,9 +935,9 @@ impl BindGroup { } } -impl Resource for BindGroup { - const TYPE: ResourceType = "BindGroup"; +crate::impl_resource_type!(BindGroup); +impl Resource for BindGroup { type Marker = crate::id::markers::BindGroup; fn as_info(&self) -> &ResourceInfo { diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 07c4f3211..2e401d6d7 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -95,9 +95,7 @@ use crate::{ id, init_tracker::{BufferInitTrackerAction, MemoryInitKind, TextureInitTrackerAction}, pipeline::{PipelineFlags, RenderPipeline, VertexStep}, - resource::{ - Buffer, DestroyedResourceError, ParentDevice, Resource, ResourceInfo, ResourceType, - }, + resource::{Buffer, DestroyedResourceError, ParentDevice, Resource, ResourceInfo}, resource_log, snatch::SnatchGuard, track::RenderBundleScope, @@ -1182,9 +1180,9 @@ impl RenderBundle { } } -impl Resource for RenderBundle { - const TYPE: ResourceType = "RenderBundle"; +crate::impl_resource_type!(RenderBundle); +impl Resource for RenderBundle { type Marker = id::markers::RenderBundle; fn as_info(&self) -> &ResourceInfo { diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 9cee09ea0..258187473 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, ResourceType}; +use crate::resource::{ParentDevice, Resource, ResourceInfo}; use crate::track::{Tracker, UsageScope}; use crate::LabelHelpers; use crate::{api_log, global::Global, hal_api::HalApi, id, resource_log, Label}; @@ -527,9 +527,9 @@ impl CommandBuffer { } } -impl Resource for CommandBuffer { - const TYPE: ResourceType = "CommandBuffer"; +crate::impl_resource_type!(CommandBuffer); +impl Resource for CommandBuffer { type Marker = id::markers::CommandBuffer; fn as_info(&self) -> &ResourceInfo { diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index a39dc337b..10a8c8573 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, ResourceType, - StagingBuffer, Texture, TextureInner, + DestroyedTexture, ParentDevice, Resource, ResourceErrorIdent, ResourceInfo, StagingBuffer, + Texture, TextureInner, }, resource_log, track::{self, TrackerIndex}, @@ -42,9 +42,9 @@ pub struct Queue { pub(crate) info: ResourceInfo, } -impl Resource for Queue { - const TYPE: ResourceType = "Queue"; +crate::impl_resource_type!(Queue); +impl Resource for Queue { type Marker = id::markers::Queue; fn as_info(&self) -> &ResourceInfo { diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 14238126b..969491db1 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -24,8 +24,8 @@ use crate::{ pool::ResourcePool, registry::Registry, resource::{ - self, Buffer, ParentDevice, QuerySet, Resource, ResourceInfo, ResourceType, Sampler, - Texture, TextureView, TextureViewNotRenderableReason, + self, Buffer, ParentDevice, QuerySet, Resource, ResourceInfo, Sampler, Texture, + TextureView, TextureViewNotRenderableReason, }, resource_log, snatch::{SnatchGuard, SnatchLock, Snatchable}, @@ -3667,9 +3667,9 @@ impl Device { } } -impl Resource for Device { - const TYPE: ResourceType = "Device"; +crate::impl_resource_type!(Device); +impl Resource for Device { type Marker = id::markers::Device; fn as_info(&self) -> &ResourceInfo { diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 21591c547..40a59853d 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -6,8 +6,7 @@ use crate::{ device::{queue::Queue, resource::Device, DeviceDescriptor}, global::Global, hal_api::HalApi, - id::markers, - id::{AdapterId, DeviceId, Id, Marker, QueueId, SurfaceId}, + id::{markers, AdapterId, DeviceId, Id, Marker, QueueId, SurfaceId}, lock::{rank, Mutex}, present::Presentation, resource::{Resource, ResourceInfo, ResourceType}, @@ -146,9 +145,11 @@ pub struct Surface { pub gl: Option>, } -impl Resource for Surface { - const TYPE: ResourceType = "Surface"; +impl ResourceType for Surface { + const TYPE: &'static str = "Surface"; +} +impl Resource for Surface { type Marker = markers::Surface; fn as_info(&self) -> &ResourceInfo { @@ -369,9 +370,9 @@ impl Adapter { } } -impl Resource for Adapter { - const TYPE: ResourceType = "Adapter"; +crate::impl_resource_type!(Adapter); +impl Resource for Adapter { type Marker = markers::Adapter; fn as_info(&self) -> &ResourceInfo { diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 8753f38df..60b451595 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, ResourceType}, + resource::{ParentDevice, Resource, ResourceInfo}, resource_log, validation, Label, }; use arrayvec::ArrayVec; @@ -65,9 +65,9 @@ impl Drop for ShaderModule { } } -impl Resource for ShaderModule { - const TYPE: ResourceType = "ShaderModule"; +crate::impl_resource_type!(ShaderModule); +impl Resource for ShaderModule { type Marker = crate::id::markers::ShaderModule; fn as_info(&self) -> &ResourceInfo { @@ -229,9 +229,9 @@ impl Drop for ComputePipeline { } } -impl Resource for ComputePipeline { - const TYPE: ResourceType = "ComputePipeline"; +crate::impl_resource_type!(ComputePipeline); +impl Resource for ComputePipeline { type Marker = crate::id::markers::ComputePipeline; fn as_info(&self) -> &ResourceInfo { @@ -293,9 +293,9 @@ impl Drop for PipelineCache { } } -impl Resource for PipelineCache { - const TYPE: ResourceType = "PipelineCache"; +crate::impl_resource_type!(PipelineCache); +impl Resource for PipelineCache { type Marker = crate::id::markers::PipelineCache; fn as_info(&self) -> &ResourceInfo { @@ -548,9 +548,9 @@ impl Drop for RenderPipeline { } } -impl Resource for RenderPipeline { - const TYPE: ResourceType = "RenderPipeline"; +crate::impl_resource_type!(RenderPipeline); +impl Resource for RenderPipeline { type Marker = crate::id::markers::RenderPipeline; fn as_info(&self) -> &ResourceInfo { diff --git a/wgpu-core/src/registry.rs b/wgpu-core/src/registry.rs index 7d5368482..1c9ff81c1 100644 --- a/wgpu-core/src/registry.rs +++ b/wgpu-core/src/registry.rs @@ -213,11 +213,13 @@ mod tests { struct TestDataId; impl Marker for TestDataId {} + impl ResourceType for TestData { + const TYPE: &'static str = "TestData"; + } + impl Resource for TestData { type Marker = TestDataId; - const TYPE: ResourceType = "Test data"; - fn as_info(&self) -> &ResourceInfo { &self.info } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index ff8b4ba0f..f86f526eb 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -120,7 +120,7 @@ impl ResourceInfo { #[derive(Clone, Debug)] pub struct ResourceErrorIdent { - r#type: ResourceType, + r#type: &'static str, label: String, } @@ -160,11 +160,22 @@ pub(crate) trait ParentDevice: Resource { } } -pub(crate) type ResourceType = &'static str; +pub(crate) trait ResourceType { + const TYPE: &'static str; +} -pub(crate) trait Resource: 'static + Sized + WasmNotSendSync { +#[macro_export] +macro_rules! impl_resource_type { + ($ty:ident) => { + impl $crate::resource::ResourceType for $ty { + const TYPE: &'static str = stringify!($ty); + } + }; +} + +pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + ResourceType { type Marker: Marker; - const TYPE: ResourceType; + fn as_info(&self) -> &ResourceInfo; /// Returns a string identifying this resource for logging and errors. @@ -779,9 +790,9 @@ pub enum CreateBufferError { MissingDownlevelFlags(#[from] MissingDownlevelFlags), } -impl Resource for Buffer { - const TYPE: ResourceType = "Buffer"; +crate::impl_resource_type!(Buffer); +impl Resource for Buffer { type Marker = crate::id::markers::Buffer; fn as_info(&self) -> &ResourceInfo { @@ -871,9 +882,9 @@ impl Drop for StagingBuffer { } } -impl Resource for StagingBuffer { - const TYPE: ResourceType = "StagingBuffer"; +crate::impl_resource_type!(StagingBuffer); +impl Resource for StagingBuffer { type Marker = crate::id::markers::StagingBuffer; fn as_info(&self) -> &ResourceInfo { @@ -1407,9 +1418,9 @@ pub enum CreateTextureError { MissingDownlevelFlags(#[from] MissingDownlevelFlags), } -impl Resource for Texture { - const TYPE: ResourceType = "Texture"; +crate::impl_resource_type!(Texture); +impl Resource for Texture { type Marker = crate::id::markers::Texture; fn as_info(&self) -> &ResourceInfo { @@ -1578,9 +1589,9 @@ pub enum CreateTextureViewError { #[non_exhaustive] pub enum TextureViewDestroyError {} -impl Resource for TextureView { - const TYPE: ResourceType = "TextureView"; +crate::impl_resource_type!(TextureView); +impl Resource for TextureView { type Marker = crate::id::markers::TextureView; fn as_info(&self) -> &ResourceInfo { @@ -1696,9 +1707,9 @@ pub enum CreateSamplerError { MissingFeatures(#[from] MissingFeatures), } -impl Resource for Sampler { - const TYPE: ResourceType = "Sampler"; +crate::impl_resource_type!(Sampler); +impl Resource for Sampler { type Marker = crate::id::markers::Sampler; fn as_info(&self) -> &ResourceInfo { @@ -1753,9 +1764,9 @@ impl ParentDevice for QuerySet { } } -impl Resource for QuerySet { - const TYPE: ResourceType = "QuerySet"; +crate::impl_resource_type!(QuerySet); +impl Resource for QuerySet { type Marker = crate::id::markers::QuerySet; fn as_info(&self) -> &ResourceInfo {