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

View File

@ -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<A: HalApi> RenderBundle<A> {
}
}
impl<A: HalApi> Resource for RenderBundle<A> {
const TYPE: ResourceType = "RenderBundle";
crate::impl_resource_type!(RenderBundle);
impl<A: HalApi> Resource for RenderBundle<A> {
type Marker = id::markers::RenderBundle;
fn as_info(&self) -> &ResourceInfo {

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, 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<A: HalApi> CommandBuffer<A> {
}
}
impl<A: HalApi> Resource for CommandBuffer<A> {
const TYPE: ResourceType = "CommandBuffer";
crate::impl_resource_type!(CommandBuffer);
impl<A: HalApi> Resource for CommandBuffer<A> {
type Marker = id::markers::CommandBuffer;
fn as_info(&self) -> &ResourceInfo {

View File

@ -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<A: HalApi> {
pub(crate) info: ResourceInfo,
}
impl<A: HalApi> Resource for Queue<A> {
const TYPE: ResourceType = "Queue";
crate::impl_resource_type!(Queue);
impl<A: HalApi> Resource for Queue<A> {
type Marker = id::markers::Queue;
fn as_info(&self) -> &ResourceInfo {

View File

@ -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<A: HalApi> Device<A> {
}
}
impl<A: HalApi> Resource for Device<A> {
const TYPE: ResourceType = "Device";
crate::impl_resource_type!(Device);
impl<A: HalApi> Resource for Device<A> {
type Marker = id::markers::Device;
fn as_info(&self) -> &ResourceInfo {

View File

@ -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<HalSurface<hal::api::Gles>>,
}
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<A: HalApi> Adapter<A> {
}
}
impl<A: HalApi> Resource for Adapter<A> {
const TYPE: ResourceType = "Adapter";
crate::impl_resource_type!(Adapter);
impl<A: HalApi> Resource for Adapter<A> {
type Marker = markers::Adapter;
fn as_info(&self) -> &ResourceInfo {

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, ResourceType},
resource::{ParentDevice, Resource, ResourceInfo},
resource_log, validation, Label,
};
use arrayvec::ArrayVec;
@ -65,9 +65,9 @@ impl<A: HalApi> Drop for ShaderModule<A> {
}
}
impl<A: HalApi> Resource for ShaderModule<A> {
const TYPE: ResourceType = "ShaderModule";
crate::impl_resource_type!(ShaderModule);
impl<A: HalApi> Resource for ShaderModule<A> {
type Marker = crate::id::markers::ShaderModule;
fn as_info(&self) -> &ResourceInfo {
@ -229,9 +229,9 @@ impl<A: HalApi> Drop for ComputePipeline<A> {
}
}
impl<A: HalApi> Resource for ComputePipeline<A> {
const TYPE: ResourceType = "ComputePipeline";
crate::impl_resource_type!(ComputePipeline);
impl<A: HalApi> Resource for ComputePipeline<A> {
type Marker = crate::id::markers::ComputePipeline;
fn as_info(&self) -> &ResourceInfo {
@ -293,9 +293,9 @@ impl<A: HalApi> Drop for PipelineCache<A> {
}
}
impl<A: HalApi> Resource for PipelineCache<A> {
const TYPE: ResourceType = "PipelineCache";
crate::impl_resource_type!(PipelineCache);
impl<A: HalApi> Resource for PipelineCache<A> {
type Marker = crate::id::markers::PipelineCache;
fn as_info(&self) -> &ResourceInfo {
@ -548,9 +548,9 @@ impl<A: HalApi> Drop for RenderPipeline<A> {
}
}
impl<A: HalApi> Resource for RenderPipeline<A> {
const TYPE: ResourceType = "RenderPipeline";
crate::impl_resource_type!(RenderPipeline);
impl<A: HalApi> Resource for RenderPipeline<A> {
type Marker = crate::id::markers::RenderPipeline;
fn as_info(&self) -> &ResourceInfo {

View File

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

View File

@ -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<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;
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<A: HalApi> Resource for Buffer<A> {
const TYPE: ResourceType = "Buffer";
crate::impl_resource_type!(Buffer);
impl<A: HalApi> Resource for Buffer<A> {
type Marker = crate::id::markers::Buffer;
fn as_info(&self) -> &ResourceInfo {
@ -871,9 +882,9 @@ impl<A: HalApi> Drop for StagingBuffer<A> {
}
}
impl<A: HalApi> Resource for StagingBuffer<A> {
const TYPE: ResourceType = "StagingBuffer";
crate::impl_resource_type!(StagingBuffer);
impl<A: HalApi> Resource for StagingBuffer<A> {
type Marker = crate::id::markers::StagingBuffer;
fn as_info(&self) -> &ResourceInfo {
@ -1407,9 +1418,9 @@ pub enum CreateTextureError {
MissingDownlevelFlags(#[from] MissingDownlevelFlags),
}
impl<A: HalApi> Resource for Texture<A> {
const TYPE: ResourceType = "Texture";
crate::impl_resource_type!(Texture);
impl<A: HalApi> Resource for Texture<A> {
type Marker = crate::id::markers::Texture;
fn as_info(&self) -> &ResourceInfo {
@ -1578,9 +1589,9 @@ pub enum CreateTextureViewError {
#[non_exhaustive]
pub enum TextureViewDestroyError {}
impl<A: HalApi> Resource for TextureView<A> {
const TYPE: ResourceType = "TextureView";
crate::impl_resource_type!(TextureView);
impl<A: HalApi> Resource for TextureView<A> {
type Marker = crate::id::markers::TextureView;
fn as_info(&self) -> &ResourceInfo {
@ -1696,9 +1707,9 @@ pub enum CreateSamplerError {
MissingFeatures(#[from] MissingFeatures),
}
impl<A: HalApi> Resource for Sampler<A> {
const TYPE: ResourceType = "Sampler";
crate::impl_resource_type!(Sampler);
impl<A: HalApi> Resource for Sampler<A> {
type Marker = crate::id::markers::Sampler;
fn as_info(&self) -> &ResourceInfo {
@ -1753,9 +1764,9 @@ impl<A: HalApi> ParentDevice<A> for QuerySet<A> {
}
}
impl<A: HalApi> Resource for QuerySet<A> {
const TYPE: ResourceType = "QuerySet";
crate::impl_resource_type!(QuerySet);
impl<A: HalApi> Resource for QuerySet<A> {
type Marker = crate::id::markers::QuerySet;
fn as_info(&self) -> &ResourceInfo {