introduce StorageItem trait

This commit is contained in:
teoxoy 2024-06-26 16:46:39 +02:00 committed by Teodor Tanasoaia
parent 90f7377cc9
commit 727956fcde
10 changed files with 52 additions and 58 deletions

View File

@ -493,10 +493,9 @@ impl<A: HalApi> Drop for BindGroupLayout<A> {
} }
crate::impl_resource_type!(BindGroupLayout); crate::impl_resource_type!(BindGroupLayout);
crate::impl_storage_item!(BindGroupLayout);
impl<A: HalApi> Resource for BindGroupLayout<A> { impl<A: HalApi> Resource for BindGroupLayout<A> {
type Marker = crate::id::markers::BindGroupLayout;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -722,10 +721,9 @@ impl<A: HalApi> PipelineLayout<A> {
} }
crate::impl_resource_type!(PipelineLayout); crate::impl_resource_type!(PipelineLayout);
crate::impl_storage_item!(PipelineLayout);
impl<A: HalApi> Resource for PipelineLayout<A> { impl<A: HalApi> Resource for PipelineLayout<A> {
type Marker = crate::id::markers::PipelineLayout;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -936,10 +934,9 @@ impl<A: HalApi> BindGroup<A> {
} }
crate::impl_resource_type!(BindGroup); crate::impl_resource_type!(BindGroup);
crate::impl_storage_item!(BindGroup);
impl<A: HalApi> Resource for BindGroup<A> { impl<A: HalApi> Resource for BindGroup<A> {
type Marker = crate::id::markers::BindGroup;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -1181,10 +1181,9 @@ impl<A: HalApi> RenderBundle<A> {
} }
crate::impl_resource_type!(RenderBundle); crate::impl_resource_type!(RenderBundle);
crate::impl_storage_item!(RenderBundle);
impl<A: HalApi> Resource for RenderBundle<A> { impl<A: HalApi> Resource for RenderBundle<A> {
type Marker = id::markers::RenderBundle;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -528,10 +528,9 @@ impl<A: HalApi> CommandBuffer<A> {
} }
crate::impl_resource_type!(CommandBuffer); crate::impl_resource_type!(CommandBuffer);
crate::impl_storage_item!(CommandBuffer);
impl<A: HalApi> Resource for CommandBuffer<A> { impl<A: HalApi> Resource for CommandBuffer<A> {
type Marker = id::markers::CommandBuffer;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -43,10 +43,9 @@ pub struct Queue<A: HalApi> {
} }
crate::impl_resource_type!(Queue); crate::impl_resource_type!(Queue);
crate::impl_storage_item!(Queue);
impl<A: HalApi> Resource for Queue<A> { impl<A: HalApi> Resource for Queue<A> {
type Marker = id::markers::Queue;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -3668,10 +3668,9 @@ impl<A: HalApi> Device<A> {
} }
crate::impl_resource_type!(Device); crate::impl_resource_type!(Device);
crate::impl_storage_item!(Device);
impl<A: HalApi> Resource for Device<A> { impl<A: HalApi> Resource for Device<A> {
type Marker = id::markers::Device;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -148,10 +148,11 @@ pub struct Surface {
impl ResourceType for Surface { impl ResourceType for Surface {
const TYPE: &'static str = "Surface"; const TYPE: &'static str = "Surface";
} }
impl crate::storage::StorageItem for Surface {
type Marker = markers::Surface;
}
impl Resource for Surface { impl Resource for Surface {
type Marker = markers::Surface;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -371,10 +372,9 @@ impl<A: HalApi> Adapter<A> {
} }
crate::impl_resource_type!(Adapter); crate::impl_resource_type!(Adapter);
crate::impl_storage_item!(Adapter);
impl<A: HalApi> Resource for Adapter<A> { impl<A: HalApi> Resource for Adapter<A> {
type Marker = markers::Adapter;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -66,10 +66,9 @@ impl<A: HalApi> Drop for ShaderModule<A> {
} }
crate::impl_resource_type!(ShaderModule); crate::impl_resource_type!(ShaderModule);
crate::impl_storage_item!(ShaderModule);
impl<A: HalApi> Resource for ShaderModule<A> { impl<A: HalApi> Resource for ShaderModule<A> {
type Marker = crate::id::markers::ShaderModule;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -230,10 +229,9 @@ impl<A: HalApi> Drop for ComputePipeline<A> {
} }
crate::impl_resource_type!(ComputePipeline); crate::impl_resource_type!(ComputePipeline);
crate::impl_storage_item!(ComputePipeline);
impl<A: HalApi> Resource for ComputePipeline<A> { impl<A: HalApi> Resource for ComputePipeline<A> {
type Marker = crate::id::markers::ComputePipeline;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -294,10 +292,9 @@ impl<A: HalApi> Drop for PipelineCache<A> {
} }
crate::impl_resource_type!(PipelineCache); crate::impl_resource_type!(PipelineCache);
crate::impl_storage_item!(PipelineCache);
impl<A: HalApi> Resource for PipelineCache<A> { impl<A: HalApi> Resource for PipelineCache<A> {
type Marker = crate::id::markers::PipelineCache;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -549,10 +546,9 @@ impl<A: HalApi> Drop for RenderPipeline<A> {
} }
crate::impl_resource_type!(RenderPipeline); crate::impl_resource_type!(RenderPipeline);
crate::impl_storage_item!(RenderPipeline);
impl<A: HalApi> Resource for RenderPipeline<A> { impl<A: HalApi> Resource for RenderPipeline<A> {
type Marker = crate::id::markers::RenderPipeline;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -6,8 +6,7 @@ use crate::{
id::Id, id::Id,
identity::IdentityManager, identity::IdentityManager,
lock::{rank, RwLock, RwLockReadGuard, RwLockWriteGuard}, lock::{rank, RwLock, RwLockReadGuard, RwLockWriteGuard},
resource::Resource, storage::{Element, InvalidId, Storage, StorageItem},
storage::{Element, InvalidId, Storage},
}; };
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] #[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
@ -37,14 +36,14 @@ impl RegistryReport {
/// any other dependent resource /// any other dependent resource
/// ///
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Registry<T: Resource> { pub(crate) struct Registry<T: StorageItem> {
// Must only contain an id which has either never been used or has been released from `storage` // Must only contain an id which has either never been used or has been released from `storage`
identity: Arc<IdentityManager<T::Marker>>, identity: Arc<IdentityManager<T::Marker>>,
storage: RwLock<Storage<T>>, storage: RwLock<Storage<T>>,
backend: Backend, backend: Backend,
} }
impl<T: Resource> Registry<T> { impl<T: StorageItem> Registry<T> {
pub(crate) fn new(backend: Backend) -> Self { pub(crate) fn new(backend: Backend) -> Self {
Self { Self {
identity: Arc::new(IdentityManager::new()), identity: Arc::new(IdentityManager::new()),
@ -59,12 +58,12 @@ impl<T: Resource> Registry<T> {
} }
#[must_use] #[must_use]
pub(crate) struct FutureId<'a, T: Resource> { pub(crate) struct FutureId<'a, T: StorageItem> {
id: Id<T::Marker>, id: Id<T::Marker>,
data: &'a RwLock<Storage<T>>, data: &'a RwLock<Storage<T>>,
} }
impl<T: Resource> FutureId<'_, T> { impl<T: StorageItem> FutureId<'_, T> {
#[allow(dead_code)] #[allow(dead_code)]
pub fn id(&self) -> Id<T::Marker> { pub fn id(&self) -> Id<T::Marker> {
self.id self.id
@ -99,7 +98,7 @@ impl<T: Resource> FutureId<'_, T> {
} }
} }
impl<T: Resource> Registry<T> { impl<T: StorageItem> Registry<T> {
pub(crate) fn prepare(&self, id_in: Option<Id<T::Marker>>) -> FutureId<T> { pub(crate) fn prepare(&self, id_in: Option<Id<T::Marker>>) -> FutureId<T> {
FutureId { FutureId {
id: match id_in { id: match id_in {
@ -204,6 +203,7 @@ mod tests {
use crate::{ use crate::{
id::Marker, id::Marker,
resource::{Resource, ResourceInfo, ResourceType}, resource::{Resource, ResourceInfo, ResourceType},
storage::StorageItem,
}; };
use super::Registry; use super::Registry;
@ -217,9 +217,11 @@ mod tests {
const TYPE: &'static str = "TestData"; const TYPE: &'static str = "TestData";
} }
impl Resource for TestData { impl StorageItem for TestData {
type Marker = TestDataId; type Marker = TestDataId;
}
impl Resource for TestData {
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -8,10 +8,7 @@ use crate::{
}, },
global::Global, global::Global,
hal_api::HalApi, hal_api::HalApi,
id::{ id::{AdapterId, BufferId, CommandEncoderId, DeviceId, SurfaceId, TextureId, TextureViewId},
AdapterId, BufferId, CommandEncoderId, DeviceId, Marker, SurfaceId, TextureId,
TextureViewId,
},
init_tracker::{BufferInitTracker, TextureInitTracker}, init_tracker::{BufferInitTracker, TextureInitTracker},
lock::{Mutex, RwLock}, lock::{Mutex, RwLock},
resource_log, resource_log,
@ -174,8 +171,6 @@ macro_rules! impl_resource_type {
} }
pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + ResourceType { pub(crate) trait Resource: 'static + Sized + WasmNotSendSync + ResourceType {
type Marker: Marker;
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.
@ -791,10 +786,9 @@ pub enum CreateBufferError {
} }
crate::impl_resource_type!(Buffer); crate::impl_resource_type!(Buffer);
crate::impl_storage_item!(Buffer);
impl<A: HalApi> Resource for Buffer<A> { impl<A: HalApi> Resource for Buffer<A> {
type Marker = crate::id::markers::Buffer;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -883,10 +877,9 @@ impl<A: HalApi> Drop for StagingBuffer<A> {
} }
crate::impl_resource_type!(StagingBuffer); crate::impl_resource_type!(StagingBuffer);
crate::impl_storage_item!(StagingBuffer);
impl<A: HalApi> Resource for StagingBuffer<A> { impl<A: HalApi> Resource for StagingBuffer<A> {
type Marker = crate::id::markers::StagingBuffer;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -1419,10 +1412,9 @@ pub enum CreateTextureError {
} }
crate::impl_resource_type!(Texture); crate::impl_resource_type!(Texture);
crate::impl_storage_item!(Texture);
impl<A: HalApi> Resource for Texture<A> { impl<A: HalApi> Resource for Texture<A> {
type Marker = crate::id::markers::Texture;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -1590,10 +1582,9 @@ pub enum CreateTextureViewError {
pub enum TextureViewDestroyError {} pub enum TextureViewDestroyError {}
crate::impl_resource_type!(TextureView); crate::impl_resource_type!(TextureView);
crate::impl_storage_item!(TextureView);
impl<A: HalApi> Resource for TextureView<A> { impl<A: HalApi> Resource for TextureView<A> {
type Marker = crate::id::markers::TextureView;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -1708,10 +1699,9 @@ pub enum CreateSamplerError {
} }
crate::impl_resource_type!(Sampler); crate::impl_resource_type!(Sampler);
crate::impl_storage_item!(Sampler);
impl<A: HalApi> Resource for Sampler<A> { impl<A: HalApi> Resource for Sampler<A> {
type Marker = crate::id::markers::Sampler;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }
@ -1765,10 +1755,9 @@ impl<A: HalApi> ParentDevice<A> for QuerySet<A> {
} }
crate::impl_resource_type!(QuerySet); crate::impl_resource_type!(QuerySet);
crate::impl_storage_item!(QuerySet);
impl<A: HalApi> Resource for QuerySet<A> { impl<A: HalApi> Resource for QuerySet<A> {
type Marker = crate::id::markers::QuerySet;
fn as_info(&self) -> &ResourceInfo { fn as_info(&self) -> &ResourceInfo {
&self.info &self.info
} }

View File

@ -3,8 +3,8 @@ use std::sync::Arc;
use wgt::Backend; use wgt::Backend;
use crate::id::Id; use crate::id::{Id, Marker};
use crate::resource::Resource; use crate::resource::{Resource, ResourceType};
use crate::{Epoch, Index}; use crate::{Epoch, Index};
/// An entry in a `Storage::map` table. /// An entry in a `Storage::map` table.
@ -27,6 +27,20 @@ pub(crate) enum Element<T> {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct InvalidId; pub(crate) struct InvalidId;
// The Resource bound is still needed because of label_for_resource
pub(crate) trait StorageItem: ResourceType + Resource {
type Marker: Marker;
}
#[macro_export]
macro_rules! impl_storage_item {
($ty:ident) => {
impl<A: HalApi> $crate::storage::StorageItem for $ty<A> {
type Marker = $crate::id::markers::$ty;
}
};
}
/// A table of `T` values indexed by the id type `I`. /// A table of `T` values indexed by the id type `I`.
/// ///
/// `Storage` implements [`std::ops::Index`], accepting `Id` values as /// `Storage` implements [`std::ops::Index`], accepting `Id` values as
@ -38,7 +52,7 @@ pub(crate) struct InvalidId;
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Storage<T> pub(crate) struct Storage<T>
where where
T: Resource, T: StorageItem,
{ {
pub(crate) map: Vec<Element<T>>, pub(crate) map: Vec<Element<T>>,
kind: &'static str, kind: &'static str,
@ -46,7 +60,7 @@ where
impl<T> ops::Index<Id<T::Marker>> for Storage<T> impl<T> ops::Index<Id<T::Marker>> for Storage<T>
where where
T: Resource, T: StorageItem,
{ {
type Output = Arc<T>; type Output = Arc<T>;
fn index(&self, id: Id<T::Marker>) -> &Arc<T> { fn index(&self, id: Id<T::Marker>) -> &Arc<T> {
@ -55,7 +69,7 @@ where
} }
impl<T> Storage<T> impl<T> Storage<T>
where where
T: Resource, T: StorageItem,
{ {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
@ -67,7 +81,7 @@ where
impl<T> Storage<T> impl<T> Storage<T>
where where
T: Resource, T: StorageItem,
{ {
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) fn contains(&self, id: Id<T::Marker>) -> bool { pub(crate) fn contains(&self, id: Id<T::Marker>) -> bool {