diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index fb5e07a242..9965272482 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -23,6 +23,7 @@ use crate::{ resource::TexturePlacement, swap_chain::{SwapChainLink, SwapImageEpoch}, track::{DummyUsage, Stitch, TrackerSet}, + BufferId, BufferHandle, Color, CommandBufferHandle, @@ -32,6 +33,7 @@ use crate::{ LifeGuard, Stored, TextureHandle, + TextureId, TextureUsageFlags, TextureViewId, }; @@ -99,8 +101,8 @@ impl CommandBufferHandle { base: &mut TrackerSet, head: &TrackerSet, stitch: Stitch, - buffer_guard: &Storage, - texture_guard: &Storage, + buffer_guard: &Storage, + texture_guard: &Storage, ) { let buffer_barriers = base.buffers diff --git a/wgpu-native/src/hub.rs b/wgpu-native/src/hub.rs index c0b7cf268a..2c145e18ee 100644 --- a/wgpu-native/src/hub.rs +++ b/wgpu-native/src/hub.rs @@ -1,21 +1,39 @@ use crate::{ AdapterHandle, + AdapterId, BindGroupHandle, + BindGroupId, BindGroupLayoutHandle, + BindGroupLayoutId, BufferHandle, + BufferId, CommandBufferHandle, + CommandBufferId, ComputePassHandle, + ComputePassId, ComputePipelineHandle, + ComputePipelineId, DeviceHandle, + DeviceId, InstanceHandle, + InstanceId, PipelineLayoutHandle, + PipelineLayoutId, RenderPassHandle, + RenderPassId, RenderPipelineHandle, + RenderPipelineId, SamplerHandle, + SamplerId, ShaderModuleHandle, + ShaderModuleId, SurfaceHandle, + SurfaceId, TextureHandle, + TextureId, TextureViewHandle, + TextureViewId, + TypedId }; use lazy_static::lazy_static; @@ -87,73 +105,70 @@ impl IdentityManager { } } -pub struct Storage { +pub struct Storage { //TODO: consider concurrent hashmap? map: VecMap<(T, Epoch)>, _phantom: std::marker::PhantomData<&'static I>, } -impl ops::Index for Storage { +impl ops::Index for Storage { type Output = T; fn index(&self, id: I) -> &T { - let (ref value, epoch) = self.map[id.id().0 as usize]; - assert_eq!(epoch, id.id().1); + let (ref value, epoch) = self.map[id.raw().index() as usize]; + assert_eq!(epoch, id.raw().1); value } } -impl ops::IndexMut for Storage { +impl ops::IndexMut for Storage { fn index_mut(&mut self, id: I) -> &mut T { - let (ref mut value, epoch) = self.map[id.id().0 as usize]; - assert_eq!(epoch, id.id().1); + let (ref mut value, epoch) = self.map[id.raw().index() as usize]; + assert_eq!(epoch, id.raw().1); value } } -impl Storage { +impl Storage { pub fn contains(&self, id: I) -> bool { - match self.map.get(id.id().0 as usize) { - Some(&(_, epoch)) if epoch == id.id().1 => true, + match self.map.get(id.raw().index() as usize) { + Some(&(_, epoch)) if epoch == id.raw().1 => true, _ => false, } } } -use crate::ToId; -pub struct Registry> { +pub struct Registry> { #[cfg(feature = "local")] identity: Mutex, data: RwLock>, - _phantom: std::marker::PhantomData<&'static I>, } -impl> Default for Registry { +impl> Default for Registry { fn default() -> Self { Registry { #[cfg(feature = "local")] identity: Mutex::new(IdentityManager::default()), data: RwLock::new(Storage { map: VecMap::new(), _phantom: std::marker::PhantomData }), - _phantom: std::marker::PhantomData, } } } -impl> ops::Deref for Registry { +impl> ops::Deref for Registry { type Target = RwLock>; fn deref(&self) -> &Self::Target { &self.data } } -impl> ops::DerefMut for Registry { +impl> ops::DerefMut for Registry { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data } } -impl + Clone> Registry { +impl + Clone> Registry { pub fn register(&self, id: I, value: T) { - let old = self.data.write().map.insert(id.id().0 as usize, (value, id.id().1)); + let old = self.data.write().map.insert(id.raw().0 as usize, (value, id.raw().1)); assert!(old.is_none()); } @@ -167,13 +182,13 @@ impl + Clone> Registry { pub fn unregister(&self, id: I) -> T { #[cfg(feature = "local")] - self.identity.lock().free(id.id()); - let (value, epoch) = self.data.write().map.remove(id.id().0 as usize).unwrap(); - assert_eq!(epoch, id.id().1); + self.identity.lock().free(id.raw()); + let (value, epoch) = self.data.write().map.remove(id.raw().0 as usize).unwrap(); + assert_eq!(epoch, id.raw().1); value } } -use crate::*; + #[derive(Default)] pub struct Hub { pub instances: Arc>, diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index fb17c265c0..2557d8b434 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -37,6 +37,7 @@ pub use self::instance::*; pub use self::pipeline::*; pub use self::resource::*; pub use self::swap_chain::*; +use self::hub::{Epoch, Id, Index, NewId}; use std::ptr; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -166,40 +167,42 @@ pub struct ByteArray { pub length: usize, } +macro_rules! define_id { + ($i:ident) => { + transparent!($i); + typed_id!($i); + } +} + + macro_rules! transparent { - ($i:item) => ( + ($i:ident) => ( #[repr(transparent)] #[derive(Clone, Copy, Debug, Hash, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] - $i + pub struct $i(Id); ) } -pub trait ToId { - fn id(&self) -> hub::Id; +pub trait TypedId { + fn raw(&self) -> Id; } -macro_rules! to_id { +macro_rules! typed_id { ($i:ident) => ( - impl ToId for $i { - fn id(&self) -> hub::Id { + impl TypedId for $i { + fn raw(&self) -> Id { self.0 } - } - ) -} - -macro_rules! from_id { - ($i:ident) => ( - impl From for $i { - fn from(id:hub::Id) -> $i { + } + impl From for $i { + fn from(id:Id) -> $i { $i(id) } } ) } -use hub::{Index, Epoch, NewId, Id}; macro_rules! new_id { ($i:ident) => ( impl NewId for $i { @@ -209,114 +212,75 @@ macro_rules! new_id { } fn index(&self) -> Index { - (self.id()).index() + (self.raw()).index() } fn epoch(&self) -> Epoch { - (self.id()).epoch() + (self.raw()).epoch() } } ) } -transparent!(pub struct InstanceId(hub::Id);); -to_id!(InstanceId); -from_id!(InstanceId); +define_id!(InstanceId); type InstanceHandle = back::Instance; -transparent!(pub struct AdapterId(hub::Id);); -to_id!(AdapterId); -from_id!(AdapterId); +define_id!(AdapterId); type AdapterHandle = hal::Adapter; -transparent!(pub struct DeviceId(hub::Id);); -to_id!(DeviceId); -from_id!(DeviceId); +define_id!(DeviceId); type DeviceHandle = Device; -//transparent!(pub struct QueueId(DeviceId);); pub type QueueId = DeviceId; -transparent!(pub struct BufferId(hub::Id);); -to_id!(BufferId); -from_id!(BufferId); +define_id!(BufferId); new_id!(BufferId); type BufferHandle = Buffer; // Resource -transparent!(pub struct TextureViewId(hub::Id);); -to_id!(TextureViewId); -from_id!(TextureViewId); +define_id!(TextureViewId); new_id!(TextureViewId); type TextureViewHandle = TextureView; -transparent!(pub struct TextureId(hub::Id);); -to_id!(TextureId); -from_id!(TextureId); +define_id!(TextureId); new_id!(TextureId); type TextureHandle = Texture; -transparent!(pub struct SamplerId(hub::Id);); -to_id!(SamplerId); -from_id!(SamplerId); +define_id!(SamplerId); type SamplerHandle = Sampler; // Binding model -transparent!(pub struct BindGroupLayoutId(hub::Id);); -to_id!(BindGroupLayoutId); -from_id!(BindGroupLayoutId); +define_id!(BindGroupLayoutId); type BindGroupLayoutHandle = BindGroupLayout; -transparent!(pub struct PipelineLayoutId(hub::Id);); -to_id!(PipelineLayoutId); -from_id!(PipelineLayoutId); +define_id!(PipelineLayoutId); type PipelineLayoutHandle = PipelineLayout; -transparent!(pub struct BindGroupId(hub::Id);); -to_id!(BindGroupId); -from_id!(BindGroupId); +define_id!(BindGroupId); type BindGroupHandle = BindGroup; // Pipeline -transparent!(pub struct InputStateId(hub::Id);); -to_id!(InputStateId); -from_id!(InputStateId); -transparent!(pub struct ShaderModuleId(hub::Id);); -to_id!(ShaderModuleId); -from_id!(ShaderModuleId); +define_id!(InputStateId); +define_id!(ShaderModuleId); type ShaderModuleHandle = ShaderModule; -transparent!(pub struct RenderPipelineId(hub::Id);); -to_id!(RenderPipelineId); -from_id!(RenderPipelineId); +define_id!(RenderPipelineId); type RenderPipelineHandle = RenderPipeline; -transparent!(pub struct ComputePipelineId(hub::Id);); -to_id!(ComputePipelineId); -from_id!(ComputePipelineId); +define_id!(ComputePipelineId); type ComputePipelineHandle = ComputePipeline; // Command -transparent!(pub struct CommandBufferId(hub::Id);); -to_id!(CommandBufferId); -from_id!(CommandBufferId); +define_id!(CommandBufferId); type CommandBufferHandle = CommandBuffer; -//transparent!(pub struct CommandEncoderId(CommandBufferId);); pub type CommandEncoderId = CommandBufferId; -transparent!(pub struct RenderPassId(hub::Id);); -to_id!(RenderPassId); -from_id!(RenderPassId); +define_id!(RenderPassId); type RenderPassHandle = RenderPass; -transparent!(pub struct ComputePassId(hub::Id);); -to_id!(ComputePassId); -from_id!(ComputePassId); +define_id!(ComputePassId); type ComputePassHandle = ComputePass; // Swap chain -transparent!(pub struct SurfaceId(hub::Id);); -to_id!(SurfaceId); -from_id!(SurfaceId); +define_id!(SurfaceId); type SurfaceHandle = Surface; -//transparent!(pub struct SwapChainId(SurfaceId);); pub type SwapChainId = SurfaceId; diff --git a/wgpu-native/src/track.rs b/wgpu-native/src/track.rs index dd4a70c5df..b331eb2931 100644 --- a/wgpu-native/src/track.rs +++ b/wgpu-native/src/track.rs @@ -1,10 +1,11 @@ use crate::{ - hub::{Epoch, Id, Index, NewId, Storage}, + hub::{Epoch, Index, NewId, Storage}, resource::{BufferUsageFlags, TextureUsageFlags}, BufferId, RefCount, TextureId, TextureViewId, + TypedId }; use bitflags::bitflags; @@ -276,7 +277,7 @@ impl + PartialEq> Tracker + PartialEq> Tracker { +impl + PartialEq> Tracker { fn _get_with_usage<'a, T: 'a + Borrow>( &mut self, storage: &'a Storage,