Clean code

This commit is contained in:
psincf 2019-04-27 12:17:16 +02:00
parent 44cf64495a
commit bfc983a8cc
4 changed files with 84 additions and 102 deletions

View File

@ -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<BufferHandle, crate::BufferId>,
texture_guard: &Storage<TextureHandle, crate::TextureId>,
buffer_guard: &Storage<BufferHandle, BufferId>,
texture_guard: &Storage<TextureHandle, TextureId>,
) {
let buffer_barriers =
base.buffers

View File

@ -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<T, I:'static + ToId> {
pub struct Storage<T, I:'static + TypedId> {
//TODO: consider concurrent hashmap?
map: VecMap<(T, Epoch)>,
_phantom: std::marker::PhantomData<&'static I>,
}
impl<T, I:ToId> ops::Index<I> for Storage<T, I> {
impl<T, I:TypedId> ops::Index<I> for Storage<T, I> {
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<T, I:ToId> ops::IndexMut<I> for Storage<T, I> {
impl<T, I:TypedId> ops::IndexMut<I> for Storage<T, I> {
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<T, I:ToId> Storage<T, I> {
impl<T, I:TypedId> Storage<T, I> {
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<T, I: 'static + ToId + From<Id>> {
pub struct Registry<T, I: 'static + TypedId + From<Id>> {
#[cfg(feature = "local")]
identity: Mutex<IdentityManager>,
data: RwLock<Storage<T, I>>,
_phantom: std::marker::PhantomData<&'static I>,
}
impl<T, I: ToId + From<Id>> Default for Registry<T, I> {
impl<T, I: TypedId + From<Id>> Default for Registry<T, I> {
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<T, I: ToId + From<Id>> ops::Deref for Registry<T, I> {
impl<T, I: TypedId + From<Id>> ops::Deref for Registry<T, I> {
type Target = RwLock<Storage<T, I>>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T, I: ToId + From<Id>> ops::DerefMut for Registry<T, I> {
impl<T, I: TypedId + From<Id>> ops::DerefMut for Registry<T, I> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl<T, I: ToId + From<Id> + Clone> Registry<T, I> {
impl<T, I: TypedId + From<Id> + Clone> Registry<T, I> {
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<T, I: ToId + From<Id> + Clone> Registry<T, I> {
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<Registry<InstanceHandle, InstanceId>>,

View File

@ -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<hub::Id> for $i {
fn from(id:hub::Id) -> $i {
}
impl From<Id> 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<back::Backend>;
transparent!(pub struct DeviceId(hub::Id););
to_id!(DeviceId);
from_id!(DeviceId);
define_id!(DeviceId);
type DeviceHandle = Device<back::Backend>;
//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<back::Backend>;
// Resource
transparent!(pub struct TextureViewId(hub::Id););
to_id!(TextureViewId);
from_id!(TextureViewId);
define_id!(TextureViewId);
new_id!(TextureViewId);
type TextureViewHandle = TextureView<back::Backend>;
transparent!(pub struct TextureId(hub::Id););
to_id!(TextureId);
from_id!(TextureId);
define_id!(TextureId);
new_id!(TextureId);
type TextureHandle = Texture<back::Backend>;
transparent!(pub struct SamplerId(hub::Id););
to_id!(SamplerId);
from_id!(SamplerId);
define_id!(SamplerId);
type SamplerHandle = Sampler<back::Backend>;
// Binding model
transparent!(pub struct BindGroupLayoutId(hub::Id););
to_id!(BindGroupLayoutId);
from_id!(BindGroupLayoutId);
define_id!(BindGroupLayoutId);
type BindGroupLayoutHandle = BindGroupLayout<back::Backend>;
transparent!(pub struct PipelineLayoutId(hub::Id););
to_id!(PipelineLayoutId);
from_id!(PipelineLayoutId);
define_id!(PipelineLayoutId);
type PipelineLayoutHandle = PipelineLayout<back::Backend>;
transparent!(pub struct BindGroupId(hub::Id););
to_id!(BindGroupId);
from_id!(BindGroupId);
define_id!(BindGroupId);
type BindGroupHandle = BindGroup<back::Backend>;
// 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<back::Backend>;
transparent!(pub struct RenderPipelineId(hub::Id););
to_id!(RenderPipelineId);
from_id!(RenderPipelineId);
define_id!(RenderPipelineId);
type RenderPipelineHandle = RenderPipeline<back::Backend>;
transparent!(pub struct ComputePipelineId(hub::Id););
to_id!(ComputePipelineId);
from_id!(ComputePipelineId);
define_id!(ComputePipelineId);
type ComputePipelineHandle = ComputePipeline<back::Backend>;
// Command
transparent!(pub struct CommandBufferId(hub::Id););
to_id!(CommandBufferId);
from_id!(CommandBufferId);
define_id!(CommandBufferId);
type CommandBufferHandle = CommandBuffer<back::Backend>;
//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<back::Backend>;
transparent!(pub struct ComputePassId(hub::Id););
to_id!(ComputePassId);
from_id!(ComputePassId);
define_id!(ComputePassId);
type ComputePassHandle = ComputePass<back::Backend>;
// Swap chain
transparent!(pub struct SurfaceId(hub::Id););
to_id!(SurfaceId);
from_id!(SurfaceId);
define_id!(SurfaceId);
type SurfaceHandle = Surface<back::Backend>;
//transparent!(pub struct SwapChainId(SurfaceId););
pub type SwapChainId = SurfaceId;

View File

@ -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<I: NewId, U: Copy + GenericUsage + BitOr<Output = U> + PartialEq> Tracker<I
}
}
impl<I: crate::ToId + NewId + Clone, U: Copy + GenericUsage + BitOr<Output = U> + PartialEq> Tracker<I, U> {
impl<I: TypedId + NewId + Clone, U: Copy + GenericUsage + BitOr<Output = U> + PartialEq> Tracker<I, U> {
fn _get_with_usage<'a, T: 'a + Borrow<RefCount>>(
&mut self,
storage: &'a Storage<T, I>,