introduce ResourceType trait

This commit is contained in:
teoxoy 2024-06-26 16:23:08 +02:00 committed by Teodor Tanasoaia
parent a422d1cba2
commit 90f7377cc9
9 changed files with 68 additions and 56 deletions

View File

@ -8,7 +8,7 @@ use crate::{
init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction}, init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction},
resource::{ resource::{
DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError, ParentDevice, DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError, ParentDevice,
Resource, ResourceErrorIdent, ResourceInfo, ResourceType, Resource, ResourceErrorIdent, ResourceInfo,
}, },
resource_log, resource_log,
snatch::{SnatchGuard, Snatchable}, snatch::{SnatchGuard, Snatchable},
@ -492,9 +492,9 @@ impl<A: HalApi> Drop for BindGroupLayout<A> {
} }
} }
impl<A: HalApi> Resource for BindGroupLayout<A> { crate::impl_resource_type!(BindGroupLayout);
const TYPE: ResourceType = "BindGroupLayout";
impl<A: HalApi> Resource for BindGroupLayout<A> {
type Marker = crate::id::markers::BindGroupLayout; type Marker = crate::id::markers::BindGroupLayout;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -721,9 +721,9 @@ impl<A: HalApi> PipelineLayout<A> {
} }
} }
impl<A: HalApi> Resource for PipelineLayout<A> { crate::impl_resource_type!(PipelineLayout);
const TYPE: ResourceType = "PipelineLayout";
impl<A: HalApi> Resource for PipelineLayout<A> {
type Marker = crate::id::markers::PipelineLayout; type Marker = crate::id::markers::PipelineLayout;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -935,9 +935,9 @@ impl<A: HalApi> BindGroup<A> {
} }
} }
impl<A: HalApi> Resource for BindGroup<A> { crate::impl_resource_type!(BindGroup);
const TYPE: ResourceType = "BindGroup";
impl<A: HalApi> Resource for BindGroup<A> {
type Marker = crate::id::markers::BindGroup; type Marker = crate::id::markers::BindGroup;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {

View File

@ -95,9 +95,7 @@ use crate::{
id, id,
init_tracker::{BufferInitTrackerAction, MemoryInitKind, TextureInitTrackerAction}, init_tracker::{BufferInitTrackerAction, MemoryInitKind, TextureInitTrackerAction},
pipeline::{PipelineFlags, RenderPipeline, VertexStep}, pipeline::{PipelineFlags, RenderPipeline, VertexStep},
resource::{ resource::{Buffer, DestroyedResourceError, ParentDevice, Resource, ResourceInfo},
Buffer, DestroyedResourceError, ParentDevice, Resource, ResourceInfo, ResourceType,
},
resource_log, resource_log,
snatch::SnatchGuard, snatch::SnatchGuard,
track::RenderBundleScope, track::RenderBundleScope,
@ -1182,9 +1180,9 @@ impl<A: HalApi> RenderBundle<A> {
} }
} }
impl<A: HalApi> Resource for RenderBundle<A> { crate::impl_resource_type!(RenderBundle);
const TYPE: ResourceType = "RenderBundle";
impl<A: HalApi> Resource for RenderBundle<A> {
type Marker = id::markers::RenderBundle; type Marker = id::markers::RenderBundle;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {

View File

@ -36,7 +36,7 @@ use crate::lock::{rank, Mutex};
use crate::snatch::SnatchGuard; 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};
use crate::track::{Tracker, UsageScope}; use crate::track::{Tracker, UsageScope};
use crate::LabelHelpers; 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};
@ -527,9 +527,9 @@ impl<A: HalApi> CommandBuffer<A> {
} }
} }
impl<A: HalApi> Resource for CommandBuffer<A> { crate::impl_resource_type!(CommandBuffer);
const TYPE: ResourceType = "CommandBuffer";
impl<A: HalApi> Resource for CommandBuffer<A> {
type Marker = id::markers::CommandBuffer; type Marker = id::markers::CommandBuffer;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {

View File

@ -17,8 +17,8 @@ use crate::{
lock::{rank, Mutex, RwLockWriteGuard}, lock::{rank, Mutex, RwLockWriteGuard},
resource::{ resource::{
Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, DestroyedResourceError, Buffer, BufferAccessError, BufferMapState, DestroyedBuffer, DestroyedResourceError,
DestroyedTexture, ParentDevice, Resource, ResourceErrorIdent, ResourceInfo, ResourceType, DestroyedTexture, ParentDevice, Resource, ResourceErrorIdent, ResourceInfo, StagingBuffer,
StagingBuffer, Texture, TextureInner, Texture, TextureInner,
}, },
resource_log, resource_log,
track::{self, TrackerIndex}, track::{self, TrackerIndex},
@ -42,9 +42,9 @@ pub struct Queue<A: HalApi> {
pub(crate) info: ResourceInfo, pub(crate) info: ResourceInfo,
} }
impl<A: HalApi> Resource for Queue<A> { crate::impl_resource_type!(Queue);
const TYPE: ResourceType = "Queue";
impl<A: HalApi> Resource for Queue<A> {
type Marker = id::markers::Queue; type Marker = id::markers::Queue;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {

View File

@ -24,8 +24,8 @@ use crate::{
pool::ResourcePool, pool::ResourcePool,
registry::Registry, registry::Registry,
resource::{ resource::{
self, Buffer, ParentDevice, QuerySet, Resource, ResourceInfo, ResourceType, Sampler, self, Buffer, ParentDevice, QuerySet, Resource, ResourceInfo, Sampler, Texture,
Texture, TextureView, TextureViewNotRenderableReason, TextureView, TextureViewNotRenderableReason,
}, },
resource_log, resource_log,
snatch::{SnatchGuard, SnatchLock, Snatchable}, snatch::{SnatchGuard, SnatchLock, Snatchable},
@ -3667,9 +3667,9 @@ impl<A: HalApi> Device<A> {
} }
} }
impl<A: HalApi> Resource for Device<A> { crate::impl_resource_type!(Device);
const TYPE: ResourceType = "Device";
impl<A: HalApi> Resource for Device<A> {
type Marker = id::markers::Device; type Marker = id::markers::Device;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {

View File

@ -6,8 +6,7 @@ use crate::{
device::{queue::Queue, resource::Device, DeviceDescriptor}, device::{queue::Queue, resource::Device, DeviceDescriptor},
global::Global, global::Global,
hal_api::HalApi, hal_api::HalApi,
id::markers, id::{markers, AdapterId, DeviceId, Id, Marker, QueueId, SurfaceId},
id::{AdapterId, DeviceId, Id, Marker, QueueId, SurfaceId},
lock::{rank, Mutex}, lock::{rank, Mutex},
present::Presentation, present::Presentation,
resource::{Resource, ResourceInfo, ResourceType}, resource::{Resource, ResourceInfo, ResourceType},
@ -146,9 +145,11 @@ pub struct Surface {
pub gl: Option<HalSurface<hal::api::Gles>>, pub gl: Option<HalSurface<hal::api::Gles>>,
} }
impl Resource for Surface { impl ResourceType for Surface {
const TYPE: ResourceType = "Surface"; const TYPE: &'static str = "Surface";
}
impl Resource for Surface {
type Marker = markers::Surface; type Marker = markers::Surface;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -369,9 +370,9 @@ impl<A: HalApi> Adapter<A> {
} }
} }
impl<A: HalApi> Resource for Adapter<A> { crate::impl_resource_type!(Adapter);
const TYPE: ResourceType = "Adapter";
impl<A: HalApi> Resource for Adapter<A> {
type Marker = markers::Adapter; type Marker = markers::Adapter;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {

View File

@ -5,7 +5,7 @@ use crate::{
device::{Device, DeviceError, MissingDownlevelFlags, MissingFeatures, RenderPassContext}, device::{Device, DeviceError, MissingDownlevelFlags, MissingFeatures, RenderPassContext},
hal_api::HalApi, hal_api::HalApi,
id::{PipelineCacheId, PipelineLayoutId, ShaderModuleId}, id::{PipelineCacheId, PipelineLayoutId, ShaderModuleId},
resource::{ParentDevice, Resource, ResourceInfo, ResourceType}, resource::{ParentDevice, Resource, ResourceInfo},
resource_log, validation, Label, resource_log, validation, Label,
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
@ -65,9 +65,9 @@ impl<A: HalApi> Drop for ShaderModule<A> {
} }
} }
impl<A: HalApi> Resource for ShaderModule<A> { crate::impl_resource_type!(ShaderModule);
const TYPE: ResourceType = "ShaderModule";
impl<A: HalApi> Resource for ShaderModule<A> {
type Marker = crate::id::markers::ShaderModule; type Marker = crate::id::markers::ShaderModule;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -229,9 +229,9 @@ impl<A: HalApi> Drop for ComputePipeline<A> {
} }
} }
impl<A: HalApi> Resource for ComputePipeline<A> { crate::impl_resource_type!(ComputePipeline);
const TYPE: ResourceType = "ComputePipeline";
impl<A: HalApi> Resource for ComputePipeline<A> {
type Marker = crate::id::markers::ComputePipeline; type Marker = crate::id::markers::ComputePipeline;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -293,9 +293,9 @@ impl<A: HalApi> Drop for PipelineCache<A> {
} }
} }
impl<A: HalApi> Resource for PipelineCache<A> { crate::impl_resource_type!(PipelineCache);
const TYPE: ResourceType = "PipelineCache";
impl<A: HalApi> Resource for PipelineCache<A> {
type Marker = crate::id::markers::PipelineCache; type Marker = crate::id::markers::PipelineCache;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -548,9 +548,9 @@ impl<A: HalApi> Drop for RenderPipeline<A> {
} }
} }
impl<A: HalApi> Resource for RenderPipeline<A> { crate::impl_resource_type!(RenderPipeline);
const TYPE: ResourceType = "RenderPipeline";
impl<A: HalApi> Resource for RenderPipeline<A> {
type Marker = crate::id::markers::RenderPipeline; type Marker = crate::id::markers::RenderPipeline;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {

View File

@ -213,11 +213,13 @@ mod tests {
struct TestDataId; struct TestDataId;
impl Marker for TestDataId {} impl Marker for TestDataId {}
impl ResourceType for TestData {
const TYPE: &'static str = "TestData";
}
impl Resource for TestData { impl Resource for TestData {
type Marker = TestDataId; type Marker = TestDataId;
const TYPE: ResourceType = "Test data";
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -120,7 +120,7 @@ impl ResourceInfo {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ResourceErrorIdent { pub struct ResourceErrorIdent {
r#type: ResourceType, r#type: &'static str,
label: String, label: String,
} }
@ -160,11 +160,22 @@ pub(crate) trait ParentDevice<A: HalApi>: 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<A: HalApi> $crate::resource::ResourceType for $ty<A> {
const TYPE: &'static str = stringify!($ty);
}
};
}
pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + ResourceType {
type Marker: Marker; type Marker: Marker;
const TYPE: ResourceType;
fn as_info(&self) -> &ResourceInfo; fn as_info(&self) -> &ResourceInfo;
/// Returns a string identifying this resource for logging and errors. /// Returns a string identifying this resource for logging and errors.
@ -779,9 +790,9 @@ pub enum CreateBufferError {
MissingDownlevelFlags(#[from] MissingDownlevelFlags), MissingDownlevelFlags(#[from] MissingDownlevelFlags),
} }
impl<A: HalApi> Resource for Buffer<A> { crate::impl_resource_type!(Buffer);
const TYPE: ResourceType = "Buffer";
impl<A: HalApi> Resource for Buffer<A> {
type Marker = crate::id::markers::Buffer; type Marker = crate::id::markers::Buffer;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -871,9 +882,9 @@ impl<A: HalApi> Drop for StagingBuffer<A> {
} }
} }
impl<A: HalApi> Resource for StagingBuffer<A> { crate::impl_resource_type!(StagingBuffer);
const TYPE: ResourceType = "StagingBuffer";
impl<A: HalApi> Resource for StagingBuffer<A> {
type Marker = crate::id::markers::StagingBuffer; type Marker = crate::id::markers::StagingBuffer;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -1407,9 +1418,9 @@ pub enum CreateTextureError {
MissingDownlevelFlags(#[from] MissingDownlevelFlags), MissingDownlevelFlags(#[from] MissingDownlevelFlags),
} }
impl<A: HalApi> Resource for Texture<A> { crate::impl_resource_type!(Texture);
const TYPE: ResourceType = "Texture";
impl<A: HalApi> Resource for Texture<A> {
type Marker = crate::id::markers::Texture; type Marker = crate::id::markers::Texture;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -1578,9 +1589,9 @@ pub enum CreateTextureViewError {
#[non_exhaustive] #[non_exhaustive]
pub enum TextureViewDestroyError {} pub enum TextureViewDestroyError {}
impl<A: HalApi> Resource for TextureView<A> { crate::impl_resource_type!(TextureView);
const TYPE: ResourceType = "TextureView";
impl<A: HalApi> Resource for TextureView<A> {
type Marker = crate::id::markers::TextureView; type Marker = crate::id::markers::TextureView;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -1696,9 +1707,9 @@ pub enum CreateSamplerError {
MissingFeatures(#[from] MissingFeatures), MissingFeatures(#[from] MissingFeatures),
} }
impl<A: HalApi> Resource for Sampler<A> { crate::impl_resource_type!(Sampler);
const TYPE: ResourceType = "Sampler";
impl<A: HalApi> Resource for Sampler<A> {
type Marker = crate::id::markers::Sampler; type Marker = crate::id::markers::Sampler;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
@ -1753,9 +1764,9 @@ impl<A: HalApi> ParentDevice<A> for QuerySet<A> {
} }
} }
impl<A: HalApi> Resource for QuerySet<A> { crate::impl_resource_type!(QuerySet);
const TYPE: ResourceType = "QuerySet";
impl<A: HalApi> Resource for QuerySet<A> {
type Marker = crate::id::markers::QuerySet; type Marker = crate::id::markers::QuerySet;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {