cyborg/src/mesh/mod.rs

179 lines
6.1 KiB
Rust

//! 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 [AttrIds][AttrId] 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.
//!
//! Meshes are pooled by [groups][MeshGroup], so all mesh data in a group
//! shares the same memory. This allows the rendering pipeline to operate on as
//! much mesh data simultaneously as possible without rebinding buffers,
//! enabling some highly-efficient rendering techniques like bindless forward
//! rendering, bindless vertex skinning, and mesh shading.
//!
//! However, because a mesh groups' underlying buffers are so large, they cannot
//! be resized without copying all of the mesh data within to a new allocation,
//! putting a lot of pressure on the GPU's memory bus and causing massive lag
//! spikes. Instead, an entirely new group must be created to store more mesh
//! data. In practice, new groups will not be created often, again due to the
//! large size of their underlying buffers.
//!
//! When a mesh is loaded, the pool is searched for a group that has spare room
//! for all of the mesh's attributes. If one is found, the pool copies the mesh's
//! attribute data into the pool's internal staging buffer, which is later
//! copied by the GPU into the corresponding attribute pools in the selected
//! group. If no group has enough free space to store all of the attributes, a
//! new group is created.
//!
//! Staging buffers are fixed-size, so when a large amount of mesh data is loaded
//! at once and the pool can't fit it all into an available staging buffer, the
//! memory is instead copied to a CPU-side spillover buffer, and GPU transfer is
//! deferred to a future staging pass. Because of this, meshes are not guaranteed
//! to be available for drawing on the frame that they are loaded.
//!
//! TODO: mesh coherency
//! TODO: make spillover buffers GPU-transferrable on iGPUs
use slab::Slab;
use smallvec::SmallVec;
use std::collections::HashMap;
use std::sync::Arc;
pub mod attr;
pub mod group;
pub mod staging;
use attr::*;
use group::*;
use staging::*;
/// An error that can be returned when allocating a mesh.
pub enum PoolError {
TooBig,
NoMoreRoom,
InvalidIndex,
AttrTaken,
AttrUnregistered,
MismatchedId,
MismatchedLayout,
}
/// The number of attributes a mesh can have before they're moved to the heap.
pub const MAX_MESH_INLINE_ATTRIBUTES: usize = 16;
/// The data and layout of a single mesh attribute.
pub struct AttrBuffer {
pub id: AttrId,
pub layout: AttrLayout,
pub count: usize,
pub data: Vec<u8>,
}
/// 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]>,
}
/// A handle to an allocated mesh.
pub struct MeshHandle {
pub(crate) group: usize,
pub(crate) sub: usize,
}
impl HasMeshHandle for MeshHandle {
fn get_mesh_handle(&self) -> &Self {
self
}
}
/// A trait for structs containing [MeshHandles][MeshHandle] that are not
/// themselves handles. Used for iteration.
pub trait HasMeshHandle {
fn get_mesh_handle(&self) -> &MeshHandle;
}
/// The top-level mesh data pool.
pub struct MeshPool {
staging: StagingPool,
attr_store: Arc<AttrStore>,
groups: Vec<MeshGroup>,
}
impl MeshPool {
pub fn new(attr_store: Arc<AttrStore>) -> Self {
Self {
staging: StagingPool::new(1_000_000),
attr_store,
groups: Default::default(),
}
}
pub fn load(&mut self, buf: &MeshBuffer) -> Result<MeshHandle, PoolError> {
for group in self.groups.iter_mut() {
match group.load(buf) {
Ok((handle, copies)) => {
self.staging.queue_copies(copies);
return Ok(handle);
}
Err(PoolError::NoMoreRoom) => {}
Err(e) => return Err(e),
}
}
let group_index = self.groups.len();
let attr_store = self.attr_store.clone();
self.groups.push(MeshGroup::new(group_index, attr_store));
let group = self.groups.get_mut(group_index).unwrap();
let (handle, copies) = group.load(buf)?;
self.staging.queue_copies(copies);
Ok(handle)
}
pub fn iter_meshes<T: HasMeshHandle>(
&self,
meshes: impl IntoIterator<Item = T>,
) -> Vec<(&MeshGroup, Vec<(&MeshAlloc, T)>)> {
let group_num = self.groups.len();
let mut by_group = Vec::with_capacity(group_num);
for index in 0..group_num {
let group = self.groups.get(index);
let meshes = Vec::new();
by_group.push((group, meshes));
}
for mesh in meshes {
let handle = mesh.get_mesh_handle();
let (group, meshes) = match by_group.get_mut(handle.group) {
Some((Some(group), meshes)) => (group, meshes),
_ => continue,
};
let alloc = match group.get_alloc(handle) {
Some(alloc) => alloc,
None => continue, // TODO err out on invalid handle?
};
meshes.push((alloc, mesh));
}
by_group
.into_iter()
.filter_map(|(group, meshes)| group.map(|group| (group, meshes)))
.collect()
}
}