diff --git a/Cargo.toml b/Cargo.toml index 482f978..867e11a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,6 @@ edition = "2021" [dependencies] rayon = "1" +slab = "^0.4" +smallvec = "^1.0" strum = { version = "0.24", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index 5f55b49..67babb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use rayon::prelude::*; use std::sync::{Arc, RwLock}; use strum::IntoEnumIterator; +pub mod mesh; pub mod pass; pub mod phase; diff --git a/src/mesh.rs b/src/mesh.rs new file mode 100644 index 0000000..5655799 --- /dev/null +++ b/src/mesh.rs @@ -0,0 +1,243 @@ +//! Dynamic mesh data storage. +//! +//! Meshes are based on ECS-like archetypes. Each pool contains a set of mesh +//! "attributes," which can be either vertex attributes, indices of different +//! formats (U8, U16, U32), or in the future, fixed-size mesh chunklets too. +//! The mesh pool itself is agnostic to specific rendering implementation. It +//! has no implicit knowledge of what a vertex position, normal, or texture +//! coordinate is, or even what an index is. +//! +//! Multiple attributes can have the same layout. For example, a rudimentary +//! mesh format might use three 32-bit floating point values (`[f32; 3]`) for +//! both vertex position and vertex normals. In this case, positions and normals +//! would have different [AttrId]s to distuingish them, and must each be +//! registered to the pool. Once an attribute is registered in a pool instance, +//! it cannot be unregistered, although the mesh pool may free GPU buffers for +//! unused attribute pools. +//! +//! TODO: mesh coherency + +use slab::Slab; +use smallvec::SmallVec; +use std::collections::HashMap; + +/// An externally-defined identifier for a mesh attribute. +#[repr(transparent)] +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] +pub struct AttrId(pub usize); + +/// A description of a mesh attribute. +#[derive(Clone, Debug, Hash, PartialEq, Eq)] +pub struct AttrLayout {} + +/// The data and layout of a single mesh attribute. +pub struct AttrBuffer { + pub id: AttrId, + pub layout: AttrLayout, + pub count: usize, + pub data: Vec, +} + +/// A mesh and all of its attributes. +/// +/// An attribute ID can be used multiple times in a mesh, corresponding to +/// multiple allocations within an [AttrPool]. +pub struct MeshBuffer { + pub attributes: SmallVec<[AttrBuffer; MAX_MESH_INLINE_ATTRIBUTES]>, +} + +/// The number of attributes a mesh can have before they're moved to the heap. +pub const MAX_MESH_INLINE_ATTRIBUTES: usize = 16; + +/// A mesh that has been allocated in a [MeshPool]. +pub struct MeshAlloc { + pub attributes: SmallVec<[AttrAlloc; MAX_MESH_INLINE_ATTRIBUTES]>, +} + +/// An error that can be returned when allocating a mesh. +pub enum PoolError { + TooBig, + NoMoreRoom, + InvalidFree, + AttrTaken, + AttrUnregistered, + MismatchedId, + MismatchedLayout, +} + +/// An attribute buffer that has been allocated in an [AttrPool]. +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] +pub struct AttrAlloc { + id: AttrId, + offset: usize, + count: usize, +} + +/// An unused space range in an [AttrPool]. +pub struct FreeSpace { + offset: usize, + count: usize, +} + +/// A single GPU buffer containing linear arrays of individual attributes. +pub struct AttrPool { + id: AttrId, + layout: AttrLayout, + count: usize, + allocs: Vec, + free_space: Vec, +} + +impl AttrPool { + pub fn new(id: AttrId, layout: AttrLayout, count: usize) -> Result { + Ok(Self { + id, + layout, + count, + free_space: vec![FreeSpace { offset: 0, count }], + allocs: vec![], + }) + } + + /// Tests if an [AttrBuffer] can be allocated without taking ownership. + /// + /// Returns the result of [Self::best_fit]. + pub fn can_alloc(&self, buf: &AttrBuffer) -> Result { + if buf.id != self.id { + Err(PoolError::MismatchedId) + } else if buf.layout != self.layout { + Err(PoolError::MismatchedLayout) + } else if buf.count > self.count { + Err(PoolError::TooBig) + } else if let Some(best_index) = self.best_fit(buf.count) { + Ok(best_index) + } else { + Err(PoolError::NoMoreRoom) + } + } + + /// Finds the index of the best-fit free space for an array of attributes. + /// + /// TODO: use a binary tree to find best-fit free space in logarithmic time + pub fn best_fit(&self, count: usize) -> Option { + let mut best_index = None; + let mut best_count = usize::MAX; + for (index, space) in self.free_space.iter().enumerate() { + if space.count >= count && space.count < best_count { + best_index = Some(index); + best_count = space.count; + } + } + + best_index + } + + /// Allocates an [AttrBuffer]. + /// + /// If you need to check if an [AttrBuffer] can be successfully + /// allocated without moving it into this function, try using + /// [Self::can_alloc] instead. + pub fn alloc(&mut self, buf: AttrBuffer) -> Result { + self.can_alloc(&buf)?; + + // can_alloc() should catch potential panics + let best_index = self.best_fit(buf.count).unwrap(); + let free_space = self.free_space.get_mut(best_index).unwrap(); + + let alloc = AttrAlloc { + id: buf.id, + offset: free_space.offset, + count: buf.count, + }; + + self.allocs.push(alloc); + + if free_space.count > buf.count { + free_space.count -= buf.count; + free_space.offset += buf.count; + } else { + self.free_space.remove(best_index); + } + + Ok(alloc) + } + + /// Frees an [AttrAlloc] from the pool. + pub fn free(&mut self, alloc: AttrAlloc) -> Result<(), PoolError> { + todo!() + } +} + +/// A set of GPU-side vertex attribute pools and index pools. +pub struct MeshPool { + pools: HashMap, + meshes: Slab, +} + +impl MeshPool { + pub fn new() -> Self { + Self { + pools: Default::default(), + meshes: Default::default(), + } + } + + /// Registers an [AttrId], and creates the pool 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 [MeshPool] 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(id, layout, pool_size)?; + self.pools.insert(id, pool); + + Ok(()) + } + + /// Checks to see if a mesh can be allocated within this pool. + /// + /// Because [Self::alloc] takes ownership of the [MeshBuffer], this function + /// can be called with a reference, to determine if a different pool needs + /// to be used instead. + pub fn can_alloc(&self, buf: &MeshBuffer) -> Result<(), PoolError> { + for attr in buf.attributes.iter() { + match self.pools.get(&attr.id) { + None => return Err(PoolError::AttrUnregistered), + Some(pool) => pool.can_alloc(attr)?, + }; + } + + Ok(()) + } + + /// Allocates a [MeshBuffer] in this pool. Returns a mesh key. + /// + /// If you need to still have ownership of the mesh in the occasion that + /// allocation fails, [Self::can_alloc] can be used instead without + /// consuming it. + pub fn alloc(&mut self, buf: MeshBuffer) -> Result { + self.can_alloc(&buf)?; + + let mut allocs = SmallVec::with_capacity(buf.attributes.len()); + for attr in buf.attributes.into_iter() { + match self.pools.get_mut(&attr.id) { + None => unreachable!(), + Some(pool) => allocs.push(pool.alloc(attr)?), + } + } + + let mesh = MeshAlloc { attributes: allocs }; + Ok(self.meshes.insert(mesh)) + } +}