Add AttrStore

This commit is contained in:
mars 2022-04-16 23:32:32 -06:00
parent b45cf13386
commit 5b75299832
3 changed files with 117 additions and 46 deletions

View File

@ -4,19 +4,92 @@
//! fit more data. //! fit more data.
use super::*; use super::*;
use std::any::TypeId;
use std::collections::HashMap;
/// An externally-defined identifier for a mesh attribute. /// An externally-defined identifier for a mesh attribute.
#[repr(transparent)] #[repr(transparent)]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct AttrId(pub usize); pub struct AttrId(pub usize);
/// A description of a mesh attribute. /// A description of a attribute's layout in memory.
#[derive(Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct AttrLayout { pub struct AttrLayout {
/// The size (in bytes) of this attribute. /// The size (in bytes) of this attribute.
pub size: usize, pub size: usize,
} }
/// Information about an [Attribute] registered in [AttrStore].
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct AttrInfo {
pub layout: AttrLayout,
pub default_pool_size: usize,
}
/// A compile-time attribute data type.
pub trait Attribute: Sized {
/// The memory layout of this data.
fn get_layout() -> AttrLayout {
AttrLayout {
size: std::mem::size_of::<Self>(),
}
}
/// The default size for new pools of this attribute.
///
/// Defaults to 1024 * 1024. (Around one million.)
fn get_default_pool_size() -> usize {
1024 * 1024
}
}
/// A store of [AttrIds][AttrId].
pub struct AttrStore {
attributes: Slab<AttrInfo>,
types: HashMap<TypeId, AttrId>,
}
impl AttrStore {
pub fn new() -> Self {
Self {
attributes: Default::default(),
types: Default::default(),
}
}
/// Dynamically creates a new [AttrId].
pub fn add(&mut self, info: AttrInfo) -> AttrId {
let index = self.attributes.insert(info);
AttrId(index)
}
/// Gets the [AttrId] for a type implementing [Attribute].
///
/// Creates a new [AttrId] for unrecognized types, otherwise reuses an
/// existing [AttrId].
pub fn get_type<T: 'static + Attribute>(&mut self) -> AttrId {
let type_id = TypeId::of::<T>();
if let Some(id) = self.types.get(&type_id) {
*id
} else {
let layout = T::get_layout();
let default_pool_size = T::get_default_pool_size();
let info = AttrInfo {
layout,
default_pool_size,
};
let id = self.add(info);
self.types.insert(type_id, id);
id
}
}
/// Gets the [AttrInfo] for an [AttrId].
pub fn get_info(&self, id: &AttrId) -> Option<&AttrInfo> {
self.attributes.get(id.0)
}
}
/// An attribute buffer that has been allocated in an [AttrPool]. /// An attribute buffer that has been allocated in an [AttrPool].
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct AttrAlloc { pub struct AttrAlloc {
@ -41,12 +114,10 @@ pub struct AttrPool {
} }
impl AttrPool { impl AttrPool {
pub fn new( pub fn new(group: usize, id: AttrId, info: AttrInfo) -> Result<Self, PoolError> {
group: usize, let layout = info.layout;
id: AttrId, let size = info.default_pool_size;
layout: AttrLayout,
size: usize,
) -> Result<Self, PoolError> {
Ok(Self { Ok(Self {
group, group,
id, id,

View File

@ -1,6 +1,7 @@
//! Fixed-room pooling of mesh data. //! Fixed-room pooling of mesh data.
use super::*; use super::*;
use std::sync::Arc;
/// A mesh that has been allocated in a [MeshGroup]. /// A mesh that has been allocated in a [MeshGroup].
pub struct MeshAlloc { pub struct MeshAlloc {
@ -10,49 +11,35 @@ pub struct MeshAlloc {
/// A set of GPU-side vertex attribute pools and index pools. /// A set of GPU-side vertex attribute pools and index pools.
pub struct MeshGroup { pub struct MeshGroup {
id: usize, id: usize,
attr_store: Arc<AttrStore>,
pools: HashMap<AttrId, AttrPool>, pools: HashMap<AttrId, AttrPool>,
meshes: Slab<MeshAlloc>, meshes: Slab<MeshAlloc>,
} }
impl MeshGroup { impl MeshGroup {
pub fn new(id: usize) -> Self { pub fn new(id: usize, attr_store: Arc<AttrStore>) -> Self {
Self { Self {
id, id,
attr_store,
pools: Default::default(), pools: Default::default(),
meshes: Default::default(), meshes: Default::default(),
} }
} }
/// Registers an [AttrId], and creates the [AttrPool] for it.
///
/// Fails if the [AttrId] has already been registered.
///
/// `pool_size` defines the size of the new pool. Once an attribute pool
/// has been created, it cannot be resized, so if it runs out of room for
/// new attributes, a new [MeshGroup] must be created.
pub fn add_attribute(
&mut self,
id: AttrId,
layout: AttrLayout,
pool_size: usize,
) -> Result<(), PoolError> {
if self.pools.contains_key(&id) {
return Err(PoolError::AttrTaken);
}
let pool = AttrPool::new(self.id, id, layout, pool_size)?;
self.pools.insert(id, pool);
Ok(())
}
/// Checks to see if a mesh can be loaded within this group. /// Checks to see if a mesh can be loaded within this group.
pub fn can_load(&self, buf: &MeshBuffer) -> Result<(), PoolError> { pub fn can_load(&self, buf: &MeshBuffer) -> Result<(), PoolError> {
for attr in buf.attributes.iter() { for attr in buf.attributes.iter() {
match self.pools.get(&attr.id) { match self.pools.get(&attr.id) {
None => return Err(PoolError::AttrUnregistered), None => {
Some(pool) => pool.can_load(attr)?, match self.attr_store.get_info(&attr.id) {
}; Some(_) => {} // new pools of valid attrs can be made
None => return Err(PoolError::AttrUnregistered),
};
}
Some(pool) => {
pool.can_load(attr)?;
}
}
} }
Ok(()) Ok(())
@ -66,14 +53,24 @@ impl MeshGroup {
let mut copies = Vec::new(); let mut copies = Vec::new();
for attr in buf.attributes.iter() { for attr in buf.attributes.iter() {
match self.pools.get_mut(&attr.id) { let id = &attr.id;
None => unreachable!(), let pool = match self.pools.get_mut(id) {
Some(pool) => { Some(pool) => pool,
let (alloc, copy) = pool.load(attr)?; None => {
allocs.push((alloc, attr.id)); let info = match self.attr_store.get_info(id) {
copies.push(copy); Some(info) => *info,
None => return Err(PoolError::AttrUnregistered),
};
let new_pool = AttrPool::new(self.id, *id, info)?;
self.pools.insert(*id, new_pool);
self.pools.get_mut(id).unwrap()
} }
} };
let (alloc, copy) = pool.load(attr)?;
allocs.push((alloc, attr.id));
copies.push(copy);
} }
let mesh = MeshAlloc { attributes: allocs }; let mesh = MeshAlloc { attributes: allocs };

View File

@ -45,6 +45,7 @@
//! TODO: make spillover buffers GPU-transferrable on iGPUs //! TODO: make spillover buffers GPU-transferrable on iGPUs
use slab::Slab; use slab::Slab;
use std::sync::Arc;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::collections::HashMap; use std::collections::HashMap;
@ -94,14 +95,16 @@ pub struct MeshHandle {
/// The top-level mesh data pool. /// The top-level mesh data pool.
pub struct MeshPool { pub struct MeshPool {
pub staging: StagingPool, staging: StagingPool,
pub groups: Vec<MeshGroup>, attr_store: Arc<AttrStore>,
groups: Vec<MeshGroup>,
} }
impl MeshPool { impl MeshPool {
pub fn new() -> Self { pub fn new(attr_store: Arc<AttrStore>) -> Self {
Self { Self {
staging: StagingPool::new(1_000_000), staging: StagingPool::new(1_000_000),
attr_store,
groups: Default::default(), groups: Default::default(),
} }
} }
@ -119,7 +122,7 @@ impl MeshPool {
} }
let group_index = self.groups.len(); let group_index = self.groups.len();
self.groups.push(MeshGroup::new(group_index)); self.groups.push(MeshGroup::new(group_index, self.attr_store.clone()));
let group = self.groups.get_mut(group_index).unwrap(); let group = self.groups.get_mut(group_index).unwrap();
let (handle, copies) = group.load(buf)?; let (handle, copies) = group.load(buf)?;