2022-04-18 04:05:44 +00:00
|
|
|
use crate::staging::*;
|
2022-05-17 00:57:54 +00:00
|
|
|
use parking_lot::{RwLock, RwLockReadGuard};
|
2022-04-18 04:05:44 +00:00
|
|
|
use slab::Slab;
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use std::any::TypeId;
|
|
|
|
use std::collections::HashMap;
|
2022-05-08 22:25:33 +00:00
|
|
|
use std::sync::Arc;
|
2022-04-18 04:05:44 +00:00
|
|
|
|
|
|
|
/// An error that can be returned when allocating a mesh.
|
2022-04-18 07:20:50 +00:00
|
|
|
#[derive(Debug)]
|
2022-04-18 04:05:44 +00:00
|
|
|
pub enum PoolError {
|
|
|
|
TooBig,
|
|
|
|
NoMoreRoom,
|
|
|
|
InvalidIndex,
|
|
|
|
AttrTaken,
|
|
|
|
AttrUnregistered,
|
2022-04-18 06:52:17 +00:00
|
|
|
LayoutUnregistered,
|
2022-04-18 04:05:44 +00:00
|
|
|
MismatchedId,
|
|
|
|
MismatchedLayout,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An identifier for a mesh attribute.
|
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub struct AttrId(usize);
|
|
|
|
|
|
|
|
/// A description of an attribute's layout in memory.
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub struct AttrLayout {
|
|
|
|
/// The size (in bytes) of this attribute.
|
|
|
|
pub size: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about an [Attribute] registered in [AttrStore].
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub struct AttrInfo {
|
|
|
|
pub layout: AttrLayout,
|
2022-04-18 07:20:50 +00:00
|
|
|
pub usages: wgpu::BufferUsages,
|
2022-04-18 04:05:44 +00:00
|
|
|
pub default_pool_size: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The data single mesh attribute.
|
2022-04-19 02:10:59 +00:00
|
|
|
#[derive(Clone)]
|
2022-04-18 04:05:44 +00:00
|
|
|
pub struct AttrBuffer {
|
|
|
|
pub id: AttrId,
|
|
|
|
pub count: usize,
|
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 07:20:50 +00:00
|
|
|
/// The [wgpu::BufferUsages] of this data.
|
|
|
|
fn get_usages() -> wgpu::BufferUsages;
|
|
|
|
|
2022-04-18 04:05:44 +00:00
|
|
|
/// 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 {
|
2022-04-19 02:10:59 +00:00
|
|
|
attributes: RwLock<Slab<AttrInfo>>,
|
|
|
|
types: RwLock<HashMap<TypeId, AttrId>>,
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AttrStore {
|
|
|
|
pub fn new() -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
|
|
|
attributes: Default::default(),
|
|
|
|
types: Default::default(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dynamically creates a new [AttrId].
|
2022-04-19 02:10:59 +00:00
|
|
|
pub fn add(&self, info: AttrInfo) -> AttrId {
|
2022-05-08 22:25:33 +00:00
|
|
|
let index = self.attributes.write().insert(info);
|
2022-04-18 04:05:44 +00:00
|
|
|
AttrId(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the [AttrId] for a type implementing [Attribute].
|
|
|
|
///
|
|
|
|
/// Creates a new [AttrId] for unrecognized types, otherwise reuses an
|
|
|
|
/// existing [AttrId].
|
2022-04-19 02:10:59 +00:00
|
|
|
pub fn get_type<T: 'static + Attribute>(&self) -> AttrId {
|
2022-04-18 04:05:44 +00:00
|
|
|
let type_id = TypeId::of::<T>();
|
2022-05-08 22:25:33 +00:00
|
|
|
let existing_id = self.types.read().get(&type_id).copied();
|
2022-04-19 02:10:59 +00:00
|
|
|
|
|
|
|
if let Some(id) = existing_id {
|
|
|
|
id
|
2022-04-18 04:05:44 +00:00
|
|
|
} else {
|
|
|
|
let layout = T::get_layout();
|
2022-04-18 07:20:50 +00:00
|
|
|
let usages = T::get_usages();
|
2022-04-18 04:05:44 +00:00
|
|
|
let default_pool_size = T::get_default_pool_size();
|
|
|
|
let info = AttrInfo {
|
|
|
|
layout,
|
2022-04-18 07:20:50 +00:00
|
|
|
usages,
|
2022-04-18 04:05:44 +00:00
|
|
|
default_pool_size,
|
|
|
|
};
|
|
|
|
let id = self.add(info);
|
2022-05-08 22:25:33 +00:00
|
|
|
self.types.write().insert(type_id, id);
|
2022-04-18 04:05:44 +00:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the [AttrInfo] for an [AttrId].
|
2022-04-19 02:10:59 +00:00
|
|
|
pub fn get_info(&self, id: &AttrId) -> Option<AttrInfo> {
|
2022-05-08 22:25:33 +00:00
|
|
|
self.attributes.read().get(id.0).copied()
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attribute pool allocation location.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AttrAllocKey {
|
|
|
|
/// The target attribute.
|
|
|
|
pub attr: AttrId,
|
|
|
|
|
|
|
|
/// The index of the attribute pool.
|
|
|
|
pub pool: usize,
|
|
|
|
|
|
|
|
/// The allocation within the attribute pool.
|
|
|
|
pub alloc: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Info about an array of attributes that has been allocated in an [AttrPool].
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub struct AttrAlloc {
|
2022-04-19 07:48:05 +00:00
|
|
|
pub offset: usize,
|
|
|
|
pub count: usize,
|
|
|
|
pub offset_bytes: usize,
|
|
|
|
pub count_bytes: usize,
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An unused space range in an [AttrPool].
|
|
|
|
pub struct FreeSpace {
|
|
|
|
offset: usize,
|
|
|
|
count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single GPU buffer containing linear arrays of attributes.
|
|
|
|
pub struct AttrPool {
|
2022-04-18 07:20:50 +00:00
|
|
|
buffer: wgpu::Buffer,
|
2022-04-18 04:05:44 +00:00
|
|
|
id: AttrId,
|
|
|
|
pool_id: usize,
|
|
|
|
layout: AttrLayout,
|
|
|
|
size: usize,
|
|
|
|
allocs: Slab<AttrAlloc>,
|
|
|
|
free_space: Vec<FreeSpace>,
|
|
|
|
}
|
|
|
|
|
2022-04-18 06:52:17 +00:00
|
|
|
impl PartialEq for AttrPool {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.pool_id == other.pool_id && self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 04:05:44 +00:00
|
|
|
impl AttrPool {
|
2022-04-18 07:20:50 +00:00
|
|
|
pub fn new(device: &wgpu::Device, id: AttrId, pool_id: usize, info: AttrInfo) -> Self {
|
2022-04-18 04:05:44 +00:00
|
|
|
let layout = info.layout;
|
|
|
|
let size = info.default_pool_size;
|
|
|
|
|
2022-04-18 07:20:50 +00:00
|
|
|
// TODO debug strings for attributes + pools + buffers
|
|
|
|
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
|
label: None,
|
|
|
|
usage: wgpu::BufferUsages::COPY_DST | info.usages,
|
|
|
|
mapped_at_creation: false,
|
|
|
|
size: (size * layout.size) as wgpu::BufferAddress,
|
|
|
|
});
|
|
|
|
|
2022-04-18 04:05:44 +00:00
|
|
|
Self {
|
2022-04-18 07:20:50 +00:00
|
|
|
buffer,
|
2022-04-18 04:05:44 +00:00
|
|
|
id,
|
|
|
|
pool_id,
|
|
|
|
layout,
|
|
|
|
size,
|
|
|
|
free_space: vec![FreeSpace {
|
|
|
|
offset: 0,
|
|
|
|
count: size,
|
|
|
|
}],
|
|
|
|
allocs: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads an [AttrBuffer].
|
|
|
|
///
|
|
|
|
/// Returns the new [AttrAllocKey].
|
|
|
|
pub fn load(&mut self, buf: &AttrBuffer) -> Result<AttrAllocKey, PoolError> {
|
|
|
|
let best_index = self.can_load(buf)?;
|
|
|
|
self.alloc_at(best_index, buf.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates an array of attributes.
|
|
|
|
///
|
|
|
|
/// Returns the new [AttrAllocKey].
|
|
|
|
pub fn alloc(&mut self, count: usize) -> Result<AttrAllocKey, PoolError> {
|
|
|
|
let best_index = self.can_alloc(count)?;
|
|
|
|
self.alloc_at(best_index, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests if an [AttrBuffer] can be loaded.
|
|
|
|
///
|
|
|
|
/// Returns the result of [Self::best_fit].
|
|
|
|
pub fn can_load(&self, buf: &AttrBuffer) -> Result<usize, PoolError> {
|
|
|
|
if buf.id != self.id {
|
|
|
|
Err(PoolError::MismatchedId)
|
|
|
|
} else if buf.count * self.layout.size != buf.data.len() {
|
|
|
|
Err(PoolError::MismatchedLayout)
|
|
|
|
} else {
|
|
|
|
self.can_alloc(buf.count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests if an array of attributes can be allocated.
|
|
|
|
///
|
|
|
|
/// Returns the result of [Self::best_fit].
|
|
|
|
pub fn can_alloc(&self, count: usize) -> Result<usize, PoolError> {
|
|
|
|
if count > self.size {
|
|
|
|
Err(PoolError::TooBig)
|
|
|
|
} else if let Some(best_index) = self.best_fit(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
|
|
|
|
fn best_fit(&self, count: usize) -> Option<usize> {
|
|
|
|
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 array of attribute at a specific free space index.
|
|
|
|
///
|
|
|
|
/// Returns the new [AttrAllocKey].
|
2022-04-18 06:52:17 +00:00
|
|
|
fn alloc_at(&mut self, index: usize, count: usize) -> Result<AttrAllocKey, PoolError> {
|
2022-04-18 04:05:44 +00:00
|
|
|
let free_space = match self.free_space.get_mut(index) {
|
|
|
|
Some(index) => index,
|
|
|
|
None => return Err(PoolError::InvalidIndex),
|
|
|
|
};
|
|
|
|
|
|
|
|
let offset = free_space.offset;
|
|
|
|
|
|
|
|
let alloc = AttrAlloc {
|
|
|
|
offset,
|
|
|
|
count,
|
|
|
|
offset_bytes: offset * self.layout.size,
|
|
|
|
count_bytes: count * self.layout.size,
|
|
|
|
};
|
|
|
|
|
|
|
|
let index = self.allocs.insert(alloc);
|
|
|
|
|
|
|
|
let key = AttrAllocKey {
|
|
|
|
attr: self.id,
|
|
|
|
pool: self.pool_id,
|
|
|
|
alloc: index,
|
|
|
|
};
|
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
match free_space.count.cmp(&count) {
|
|
|
|
Ordering::Less => {
|
|
|
|
return Err(PoolError::TooBig);
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
|
|
|
self.free_space.remove(index);
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
free_space.count -= count;
|
|
|
|
free_space.offset += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Frees an allocation (by key) from the pool.
|
|
|
|
pub fn free(&mut self, key: usize) -> Result<(), PoolError> {
|
|
|
|
let alloc = self.allocs.try_remove(key).ok_or(PoolError::InvalidIndex)?;
|
|
|
|
|
|
|
|
let free_space = FreeSpace {
|
|
|
|
offset: alloc.offset,
|
|
|
|
count: alloc.count,
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO merge free spaces
|
|
|
|
self.free_space.push(free_space);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-18 06:52:17 +00:00
|
|
|
|
2022-04-19 07:48:05 +00:00
|
|
|
/// Retrieves an [AttrAlloc] by key.
|
|
|
|
pub fn get(&self, key: usize) -> Option<AttrAlloc> {
|
|
|
|
self.allocs.get(key).copied()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets this pool's internal GPU buffer.
|
|
|
|
pub fn get_buffer(&self) -> &wgpu::Buffer {
|
|
|
|
&self.buffer
|
2022-04-18 06:52:17 +00:00
|
|
|
}
|
2022-04-18 07:20:50 +00:00
|
|
|
|
|
|
|
/// Gets a [CopyDest] for an allocation, by key.
|
|
|
|
pub fn get_copy_dest(&self, key: usize) -> Result<CopyDest, PoolError> {
|
2022-04-19 07:48:05 +00:00
|
|
|
let offset = self.get(key).ok_or(PoolError::InvalidIndex)?.offset_bytes;
|
2022-04-18 07:20:50 +00:00
|
|
|
Ok(CopyDest {
|
2022-04-19 07:48:05 +00:00
|
|
|
buffer: self.get_buffer(),
|
2022-04-18 07:20:50 +00:00
|
|
|
offset,
|
|
|
|
})
|
|
|
|
}
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 and all of its attributes.
|
2022-04-19 02:10:59 +00:00
|
|
|
#[derive(Clone, Default)]
|
2022-04-18 04:05:44 +00:00
|
|
|
pub struct MeshBuffer {
|
|
|
|
pub attributes: SmallVec<[AttrBuffer; MAX_MESH_INLINE_ATTRIBUTES]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An allocated mesh.
|
|
|
|
pub struct MeshAlloc {
|
|
|
|
pub attributes: SmallVec<[AttrAllocKey; MAX_MESH_INLINE_ATTRIBUTES]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A handle to an allocated mesh.
|
|
|
|
#[repr(transparent)]
|
2022-05-08 04:17:10 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-04-18 04:05:44 +00:00
|
|
|
pub struct MeshHandle(usize);
|
|
|
|
|
2022-04-18 06:52:17 +00:00
|
|
|
/// A reusable set of [AttrIds][AttrId], for use with querying compatible meshes.
|
|
|
|
pub type MeshLayoutDesc = smallmap::Set<AttrId>;
|
2022-04-18 04:05:44 +00:00
|
|
|
|
2022-04-18 06:52:17 +00:00
|
|
|
/// The ID of a [MeshLayoutDesc] registered in a [MeshPool].
|
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub struct MeshLayoutId(usize);
|
|
|
|
|
2022-04-19 07:48:05 +00:00
|
|
|
/// Mappings of the attributes in a [MeshLayout] to specific pool indices.
|
|
|
|
pub type MeshLayoutBindingIndices = smallmap::Map<AttrId, usize>;
|
|
|
|
|
2022-04-18 06:52:17 +00:00
|
|
|
/// Mappings of the attributes in a [MeshLayout] to specific pools.
|
2022-04-19 07:48:05 +00:00
|
|
|
pub struct MeshLayoutBindings<'a> {
|
2022-05-08 22:25:33 +00:00
|
|
|
lock: RwLockReadGuard<'a, HashMap<AttrId, Vec<AttrPool>>>,
|
2022-04-19 07:48:05 +00:00
|
|
|
indices: MeshLayoutBindingIndices,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> MeshLayoutBindings<'a> {
|
|
|
|
pub fn get(&self, attr: AttrId) -> Option<&AttrPool> {
|
|
|
|
let pool_id = self.indices.get(&attr)?;
|
|
|
|
let pools = self.lock.get(&attr)?;
|
|
|
|
pools.get(*pool_id)
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 06:52:17 +00:00
|
|
|
|
|
|
|
/// Mappings of the attributes in a [MeshAlloc] to specific pool offsets.
|
2022-04-19 07:48:05 +00:00
|
|
|
pub type MeshAllocInfos = SmallVec<[(AttrId, AttrAlloc); MAX_MESH_INLINE_ATTRIBUTES]>;
|
2022-04-18 06:52:17 +00:00
|
|
|
|
|
|
|
/// A set of mesh instances fitting a common [MeshLayoutDesc].
|
|
|
|
#[derive(Default)]
|
2022-04-19 02:10:59 +00:00
|
|
|
pub struct MeshLayoutInstances<T> {
|
2022-04-19 07:48:05 +00:00
|
|
|
pub bindings: MeshLayoutBindingIndices,
|
|
|
|
pub instances: Vec<(T, MeshAllocInfos)>,
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A mesh data pool.
|
|
|
|
pub struct MeshPool {
|
|
|
|
device: Arc<wgpu::Device>,
|
|
|
|
staging: StagingPool<AttrAllocKey>,
|
|
|
|
attr_store: Arc<AttrStore>,
|
2022-04-19 02:10:59 +00:00
|
|
|
allocs: RwLock<Slab<MeshAlloc>>,
|
|
|
|
mesh_layouts: RwLock<Slab<MeshLayoutDesc>>,
|
|
|
|
pools: RwLock<HashMap<AttrId, Vec<AttrPool>>>,
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MeshPool {
|
|
|
|
pub fn new(device: Arc<wgpu::Device>, attr_store: Arc<AttrStore>) -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
|
|
|
device: device.clone(),
|
|
|
|
staging: StagingPool::new(device, 1024 * 1024),
|
|
|
|
attr_store,
|
2022-04-18 06:52:17 +00:00
|
|
|
mesh_layouts: Default::default(),
|
2022-04-18 04:05:44 +00:00
|
|
|
allocs: Default::default(),
|
|
|
|
pools: Default::default(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-18 06:52:17 +00:00
|
|
|
/// Registers a [MeshLayoutDesc] with this pool.
|
|
|
|
///
|
|
|
|
/// TODO: keep track of mesh allocations that fit each layout
|
2022-04-19 02:10:59 +00:00
|
|
|
pub fn add_layout(&self, layout: MeshLayoutDesc) -> Result<MeshLayoutId, PoolError> {
|
2022-05-08 22:25:33 +00:00
|
|
|
let idx = self.mesh_layouts.write().insert(layout);
|
2022-04-18 06:52:17 +00:00
|
|
|
Ok(MeshLayoutId(idx))
|
|
|
|
}
|
|
|
|
|
2022-04-18 04:05:44 +00:00
|
|
|
/// Loads a [MeshBuffer].
|
2022-04-19 02:10:59 +00:00
|
|
|
pub fn load(&self, buf: MeshBuffer) -> Result<MeshHandle, PoolError> {
|
2022-04-18 04:05:44 +00:00
|
|
|
let mut attrs = HashMap::new();
|
|
|
|
|
|
|
|
for attr in buf.attributes.into_iter() {
|
|
|
|
if let Some(_) = attrs.insert(attr.id, attr) {
|
|
|
|
// TODO: support for loading duplicate attribute IDs
|
|
|
|
return Err(PoolError::AttrTaken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut attr_allocs = SmallVec::new();
|
|
|
|
let mut copies = Vec::new();
|
|
|
|
for (id, buf) in attrs.drain() {
|
2022-05-08 22:25:33 +00:00
|
|
|
let mut pools_write = self.pools.write();
|
2022-04-19 02:10:59 +00:00
|
|
|
|
|
|
|
let pools = match pools_write.get_mut(&id) {
|
2022-04-18 04:05:44 +00:00
|
|
|
Some(pools) => pools,
|
|
|
|
None => {
|
|
|
|
let info = match self.attr_store.get_info(&id) {
|
|
|
|
Some(info) => info,
|
|
|
|
None => return Err(PoolError::AttrUnregistered),
|
|
|
|
};
|
|
|
|
|
2022-04-19 02:10:59 +00:00
|
|
|
let pool = AttrPool::new(&self.device, id, 0, info);
|
|
|
|
pools_write.insert(id, vec![pool]);
|
|
|
|
pools_write.get_mut(&id).unwrap()
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for pool in pools.iter_mut() {
|
|
|
|
match pool.load(&buf) {
|
|
|
|
Ok(alloc) => {
|
|
|
|
let copy = CopyBuffer {
|
|
|
|
target: alloc.clone(),
|
|
|
|
offset: 0,
|
|
|
|
data: buf.data,
|
|
|
|
};
|
|
|
|
|
|
|
|
attr_allocs.push(alloc);
|
|
|
|
copies.push(copy);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(PoolError::NoMoreRoom) => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 02:17:31 +00:00
|
|
|
|
|
|
|
// TODO create a new pool if no available pool was found
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.staging.queue_copies(copies);
|
|
|
|
|
|
|
|
let alloc = MeshAlloc {
|
|
|
|
attributes: attr_allocs,
|
|
|
|
};
|
2022-05-08 22:25:33 +00:00
|
|
|
let key = self.allocs.write().insert(alloc);
|
2022-04-18 04:05:44 +00:00
|
|
|
let handle = MeshHandle(key);
|
|
|
|
Ok(handle)
|
|
|
|
}
|
2022-04-18 06:52:17 +00:00
|
|
|
|
2022-04-19 02:10:59 +00:00
|
|
|
pub fn flush(&self, commands: &mut wgpu::CommandEncoder) {
|
2022-05-08 22:25:33 +00:00
|
|
|
let pools_read = self.pools.read();
|
2022-04-19 02:10:59 +00:00
|
|
|
|
2022-04-18 07:20:50 +00:00
|
|
|
let get_dst = |target: &AttrAllocKey| {
|
2022-04-19 02:10:59 +00:00
|
|
|
pools_read
|
2022-04-18 07:20:50 +00:00
|
|
|
.get(&target.attr)
|
|
|
|
.unwrap()
|
|
|
|
.get(target.pool)
|
|
|
|
.unwrap()
|
|
|
|
.get_copy_dest(target.alloc)
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: keep track of loaded/unloaded meshes
|
2022-05-08 21:42:30 +00:00
|
|
|
let on_complete = |_target| {};
|
2022-04-18 07:20:50 +00:00
|
|
|
|
|
|
|
self.staging.flush(commands, get_dst, on_complete);
|
|
|
|
}
|
|
|
|
|
2022-04-18 06:52:17 +00:00
|
|
|
pub fn iter_meshes<T, I, F>(
|
|
|
|
&self,
|
|
|
|
layout: MeshLayoutId,
|
|
|
|
meshes: I,
|
|
|
|
get_handle: F,
|
|
|
|
) -> Result<Vec<MeshLayoutInstances<T>>, PoolError>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
2022-04-19 07:48:05 +00:00
|
|
|
F: Fn(&T) -> &MeshHandle,
|
2022-04-18 06:52:17 +00:00
|
|
|
{
|
|
|
|
let layout = self
|
|
|
|
.mesh_layouts
|
2022-04-19 02:10:59 +00:00
|
|
|
.read()
|
2022-04-18 06:52:17 +00:00
|
|
|
.get(layout.0)
|
2022-04-19 02:10:59 +00:00
|
|
|
.ok_or(PoolError::LayoutUnregistered)?
|
|
|
|
.clone();
|
2022-04-18 06:52:17 +00:00
|
|
|
|
|
|
|
let layout_attrs: Vec<AttrId> = layout.iter().map(|(id, _)| *id).collect();
|
|
|
|
let mut layouts = Vec::<MeshLayoutInstances<T>>::new();
|
|
|
|
|
2022-05-08 22:25:33 +00:00
|
|
|
let allocs_read = self.allocs.read();
|
2022-04-18 06:52:17 +00:00
|
|
|
let mut attr_allocs: Vec<AttrAllocKey> = Vec::with_capacity(layout_attrs.len());
|
|
|
|
for mesh in meshes {
|
|
|
|
let handle = get_handle(&mesh);
|
2022-04-19 02:10:59 +00:00
|
|
|
let alloc = allocs_read.get(handle.0).ok_or(PoolError::InvalidIndex)?;
|
2022-04-18 06:52:17 +00:00
|
|
|
|
|
|
|
attr_allocs.clear();
|
|
|
|
for layout_attr in layout_attrs.iter() {
|
|
|
|
match alloc
|
|
|
|
.attributes
|
|
|
|
.iter()
|
|
|
|
.find(|attr_alloc| attr_alloc.attr == *layout_attr)
|
|
|
|
{
|
|
|
|
Some(alloc) => attr_allocs.push(alloc.clone()),
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr_allocs.len() != layout_attrs.len() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:48:05 +00:00
|
|
|
let mut layout_bindings = MeshLayoutBindingIndices::default();
|
|
|
|
let mut alloc_infos = MeshAllocInfos::default();
|
2022-04-18 06:52:17 +00:00
|
|
|
for alloc in attr_allocs.iter() {
|
2022-05-08 22:25:33 +00:00
|
|
|
let pools_read = self.pools.read();
|
2022-04-19 02:10:59 +00:00
|
|
|
let pools = pools_read
|
2022-04-18 06:52:17 +00:00
|
|
|
.get(&alloc.attr)
|
|
|
|
.ok_or(PoolError::AttrUnregistered)?;
|
|
|
|
|
|
|
|
let pool = pools.get(alloc.pool).ok_or(PoolError::InvalidIndex)?;
|
2022-04-19 07:48:05 +00:00
|
|
|
let alloc_info = pool.get(alloc.alloc).ok_or(PoolError::InvalidIndex)?;
|
2022-04-18 06:52:17 +00:00
|
|
|
|
2022-04-19 02:10:59 +00:00
|
|
|
layout_bindings.insert(alloc.attr, alloc.pool);
|
2022-04-19 07:48:05 +00:00
|
|
|
alloc_infos.push((alloc.attr, alloc_info));
|
2022-04-18 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 07:48:05 +00:00
|
|
|
let instance = (mesh, alloc_infos);
|
2022-04-18 06:52:17 +00:00
|
|
|
|
|
|
|
match layouts
|
|
|
|
.iter_mut()
|
|
|
|
.find(|layout| layout.bindings == layout_bindings)
|
|
|
|
{
|
|
|
|
Some(layout) => layout.instances.push(instance),
|
|
|
|
None => layouts.push(MeshLayoutInstances {
|
|
|
|
bindings: layout_bindings,
|
|
|
|
instances: vec![instance],
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(layouts)
|
|
|
|
}
|
2022-04-19 07:48:05 +00:00
|
|
|
|
|
|
|
pub fn get_bindings<'a>(&'a self, indices: MeshLayoutBindingIndices) -> MeshLayoutBindings<'a> {
|
2022-05-08 22:25:33 +00:00
|
|
|
let lock = self.pools.read();
|
2022-04-19 07:48:05 +00:00
|
|
|
MeshLayoutBindings { lock, indices }
|
|
|
|
}
|
2022-04-18 04:05:44 +00:00
|
|
|
}
|