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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,8 +3,8 @@ use std::sync::Arc;
use wgt::Backend;
use crate::id::Id;
use crate::resource::Resource;
use crate::id::{Id, Marker};
use crate::resource::{Resource, ResourceType};
use crate::{Epoch, Index};
/// An entry in a `Storage::map` table.
@ -27,6 +27,20 @@ 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 {
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`.
///
/// `Storage` implements [`std::ops::Index`], accepting `Id` values as
@ -38,7 +52,7 @@ pub(crate) struct InvalidId;
#[derive(Debug)]
pub(crate) struct Storage<T>
where
T: Resource,
T: StorageItem,
{
pub(crate) map: Vec<Element<T>>,
kind: &'static str,
@ -46,7 +60,7 @@ where
impl<T> ops::Index<Id<T::Marker>> for Storage<T>
where
T: Resource,
T: StorageItem,
{
type Output = Arc<T>;
fn index(&self, id: Id<T::Marker>) -> &Arc<T> {
@ -55,7 +69,7 @@ where
}
impl<T> Storage<T>
where
T: Resource,
T: StorageItem,
{
pub(crate) fn new() -> Self {
Self {
@ -67,7 +81,7 @@ where
impl<T> Storage<T>
where
T: Resource,
T: StorageItem,
{
#[allow(dead_code)]
pub(crate) fn contains(&self, id: Id<T::Marker>) -> bool {