use crate::{Backend, Epoch, Index}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use std::{fmt, marker::PhantomData}; const BACKEND_BITS: usize = 3; const EPOCH_MASK: u32 = (1 << (32 - BACKEND_BITS)) - 1; type Dummy = crate::backend::Empty; #[repr(transparent)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Id(u64, PhantomData); impl Id { pub fn backend(&self) -> Backend { match self.0 >> (64 - BACKEND_BITS) as u8 { 0 => Backend::Empty, 1 => Backend::Vulkan, 2 => Backend::Metal, 3 => Backend::Dx12, 4 => Backend::Dx11, 5 => Backend::Gl, _ => unreachable!(), } } } impl Copy for Id {} impl Clone for Id { fn clone(&self) -> Self { Self(self.0, PhantomData) } } impl fmt::Debug for Id { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(formatter) } } impl std::hash::Hash for Id { fn hash(&self, state: &mut H) { self.0.hash(state); } } impl PartialEq for Id { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } pub trait TypedId { fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self; fn unzip(self) -> (Index, Epoch, Backend); } impl TypedId for Id { fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self { assert_eq!(0, epoch >> (32 - BACKEND_BITS)); let v = index as u64 | ((epoch as u64) << 32) | ((backend as u64) << (64 - BACKEND_BITS)); Id(v, PhantomData) } fn unzip(self) -> (Index, Epoch, Backend) { ( self.0 as u32, (self.0 >> 32) as u32 & EPOCH_MASK, self.backend(), ) } } #[cfg(feature = "remote")] pub type Input = T; #[cfg(not(feature = "remote"))] pub type Input = PhantomData; #[cfg(not(feature = "remote"))] pub type Output = T; #[cfg(feature = "remote")] pub type Output = PhantomData; pub type AdapterId = Id>; pub type DeviceId = Id>; pub type QueueId = DeviceId; // Resource pub type BufferId = Id>; pub type TextureViewId = Id>; pub type TextureId = Id>; pub type SamplerId = Id>; // Binding model pub type BindGroupLayoutId = Id>; pub type PipelineLayoutId = Id>; pub type BindGroupId = Id>; // Pipeline pub type InputStateId = Id; pub type ShaderModuleId = Id>; pub type RenderPipelineId = Id>; pub type ComputePipelineId = Id>; // Command pub type CommandBufferId = Id>; pub type CommandEncoderId = CommandBufferId; pub type RenderBundleId = Id>; pub type RenderPassId = Id>; pub type ComputePassId = Id>; // Swap chain pub type SurfaceId = Id; pub type SwapChainId = Id>; #[test] fn test_id_backend() { for &b in &[ Backend::Empty, Backend::Vulkan, Backend::Metal, Backend::Dx12, Backend::Dx11, Backend::Gl, ] { let id: Id<()> = Id::zip(0, 0, b); assert_eq!(id.backend(), b); } }