Use parking_lot sync primitives
This commit is contained in:
parent
ce22735180
commit
b06a8c7982
|
@ -8,6 +8,7 @@ bytemuck = { version = "^1.0", features = ["derive"] }
|
||||||
glam = "0.20"
|
glam = "0.20"
|
||||||
multimap = "0.8"
|
multimap = "0.8"
|
||||||
notify = "^4"
|
notify = "^4"
|
||||||
|
parking_lot = "^0.11"
|
||||||
pollster = "0.2"
|
pollster = "0.2"
|
||||||
rayon = "1"
|
rayon = "1"
|
||||||
slab = "^0.4"
|
slab = "^0.4"
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
pub mod camera;
|
pub mod camera;
|
||||||
pub mod pass;
|
pub mod pass;
|
||||||
|
@ -244,12 +244,12 @@ impl Renderer {
|
||||||
let pass = &self.render_passes[*pass_index];
|
let pass = &self.render_passes[*pass_index];
|
||||||
|
|
||||||
if let Some(cmd) = pass.record_render(phase_data) {
|
if let Some(cmd) = pass.record_render(phase_data) {
|
||||||
cmds.lock().unwrap().push(cmd);
|
cmds.lock().push(cmd);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
cmds.into_inner().unwrap()
|
cmds.into_inner()
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: parallelize each phase's record_render
|
// TODO: parallelize each phase's record_render
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
use notify::{raw_watcher, RawEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
use notify::{raw_watcher, RawEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
||||||
|
use parking_lot::{RwLock, RwLockReadGuard};
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
use std::collections::{BTreeSet, HashMap};
|
use std::collections::{BTreeSet, HashMap};
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::mpsc::{channel, Receiver, TryRecvError};
|
use std::sync::mpsc::{channel, Receiver, TryRecvError};
|
||||||
use std::sync::{Arc, RwLock, RwLockReadGuard};
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
|
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
|
||||||
|
@ -42,12 +43,12 @@ impl ShaderStore {
|
||||||
pub fn load(&self, module: &naga::Module) -> Result<ShaderHandle, ShaderError> {
|
pub fn load(&self, module: &naga::Module) -> Result<ShaderHandle, ShaderError> {
|
||||||
let source = self.generate_wgsl(module)?;
|
let source = self.generate_wgsl(module)?;
|
||||||
let shader = self.load_wgsl(source)?;
|
let shader = self.load_wgsl(source)?;
|
||||||
let index = self.shaders.write().unwrap().insert(shader);
|
let index = self.shaders.write().insert(shader);
|
||||||
Ok(ShaderHandle(index))
|
Ok(ShaderHandle(index))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reload(&self, handle: &ShaderHandle, module: &naga::Module) -> Result<(), ShaderError> {
|
pub fn reload(&self, handle: &ShaderHandle, module: &naga::Module) -> Result<(), ShaderError> {
|
||||||
let mut write = self.shaders.write().unwrap();
|
let mut write = self.shaders.write();
|
||||||
match write.get_mut(handle.0) {
|
match write.get_mut(handle.0) {
|
||||||
Some(stored) => {
|
Some(stored) => {
|
||||||
let source = self.generate_wgsl(module)?;
|
let source = self.generate_wgsl(module)?;
|
||||||
|
@ -90,7 +91,7 @@ impl ShaderStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, handle: &ShaderHandle) -> Option<ShaderStoreReadGuard> {
|
pub fn get(&self, handle: &ShaderHandle) -> Option<ShaderStoreReadGuard> {
|
||||||
let guard = self.shaders.read().unwrap();
|
let guard = self.shaders.read();
|
||||||
if let Some(_) = guard.get(handle.0) {
|
if let Some(_) = guard.get(handle.0) {
|
||||||
Some(ShaderStoreReadGuard {
|
Some(ShaderStoreReadGuard {
|
||||||
guard,
|
guard,
|
||||||
|
@ -233,7 +234,7 @@ impl ShaderWatcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_changed(&self, path: &Path) {
|
pub fn on_changed(&self, path: &Path) {
|
||||||
let infos_read = self.file_infos.read().unwrap();
|
let infos_read = self.file_infos.read();
|
||||||
let path_buf = path.to_path_buf();
|
let path_buf = path.to_path_buf();
|
||||||
match infos_read.get(&path_buf) {
|
match infos_read.get(&path_buf) {
|
||||||
Some(handle) => {
|
Some(handle) => {
|
||||||
|
@ -250,7 +251,7 @@ impl ShaderWatcher {
|
||||||
let mut loader = ShaderLoader::new(&self.store, &self.root_path, None);
|
let mut loader = ShaderLoader::new(&self.store, &self.root_path, None);
|
||||||
loader.include_file(&shader_path_buf);
|
loader.include_file(&shader_path_buf);
|
||||||
let info = loader.finish();
|
let info = loader.finish();
|
||||||
let mut infos_write = self.file_infos.write().unwrap();
|
let mut infos_write = self.file_infos.write();
|
||||||
infos_write.insert(self.root_path.join(shader_path_buf), info.handle);
|
infos_write.insert(self.root_path.join(shader_path_buf), info.handle);
|
||||||
Ok(info.handle)
|
Ok(info.handle)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
//! TODO: pass access to a wgpu::Queue for write_buffer, staging belt recall, or command encoding
|
//! TODO: pass access to a wgpu::Queue for write_buffer, staging belt recall, or command encoding
|
||||||
|
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::Arc;
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
pub struct StagingPool<T> {
|
pub struct StagingPool<T> {
|
||||||
device: Arc<wgpu::Device>,
|
device: Arc<wgpu::Device>,
|
||||||
|
@ -28,7 +29,7 @@ impl<T: Clone> StagingPool<T> {
|
||||||
get_dst: impl Fn(&T) -> CopyDest<'a>,
|
get_dst: impl Fn(&T) -> CopyDest<'a>,
|
||||||
on_complete: impl Fn(T),
|
on_complete: impl Fn(T),
|
||||||
) {
|
) {
|
||||||
let mut spillover = self.spillover.lock().unwrap();
|
let mut spillover = self.spillover.lock();
|
||||||
if spillover.is_empty() {
|
if spillover.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -70,7 +71,7 @@ impl<T: Clone> StagingPool<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn queue_copies(&self, copies: Vec<CopyBuffer<T>>) {
|
pub fn queue_copies(&self, copies: Vec<CopyBuffer<T>>) {
|
||||||
let mut spillover = self.spillover.lock().unwrap();
|
let mut spillover = self.spillover.lock();
|
||||||
spillover.reserve(copies.len());
|
spillover.reserve(copies.len());
|
||||||
spillover.extend(copies.into_iter());
|
spillover.extend(copies.into_iter());
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,8 @@ use slab::Slab;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::any::TypeId;
|
use std::any::TypeId;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::Arc;
|
||||||
|
use parking_lot::{RwLock, RwLockReadGuard};
|
||||||
|
|
||||||
/// An error that can be returned when allocating a mesh.
|
/// An error that can be returned when allocating a mesh.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -82,7 +83,7 @@ impl AttrStore {
|
||||||
|
|
||||||
/// Dynamically creates a new [AttrId].
|
/// Dynamically creates a new [AttrId].
|
||||||
pub fn add(&self, info: AttrInfo) -> AttrId {
|
pub fn add(&self, info: AttrInfo) -> AttrId {
|
||||||
let index = self.attributes.write().unwrap().insert(info);
|
let index = self.attributes.write().insert(info);
|
||||||
AttrId(index)
|
AttrId(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +93,7 @@ impl AttrStore {
|
||||||
/// existing [AttrId].
|
/// existing [AttrId].
|
||||||
pub fn get_type<T: 'static + Attribute>(&self) -> AttrId {
|
pub fn get_type<T: 'static + Attribute>(&self) -> AttrId {
|
||||||
let type_id = TypeId::of::<T>();
|
let type_id = TypeId::of::<T>();
|
||||||
let existing_id = self.types.read().unwrap().get(&type_id).copied();
|
let existing_id = self.types.read().get(&type_id).copied();
|
||||||
|
|
||||||
if let Some(id) = existing_id {
|
if let Some(id) = existing_id {
|
||||||
id
|
id
|
||||||
|
@ -106,14 +107,14 @@ impl AttrStore {
|
||||||
default_pool_size,
|
default_pool_size,
|
||||||
};
|
};
|
||||||
let id = self.add(info);
|
let id = self.add(info);
|
||||||
self.types.write().unwrap().insert(type_id, id);
|
self.types.write().insert(type_id, id);
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the [AttrInfo] for an [AttrId].
|
/// Gets the [AttrInfo] for an [AttrId].
|
||||||
pub fn get_info(&self, id: &AttrId) -> Option<AttrInfo> {
|
pub fn get_info(&self, id: &AttrId) -> Option<AttrInfo> {
|
||||||
self.attributes.read().unwrap().get(id.0).copied()
|
self.attributes.read().get(id.0).copied()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,7 +358,7 @@ pub type MeshLayoutBindingIndices = smallmap::Map<AttrId, usize>;
|
||||||
|
|
||||||
/// Mappings of the attributes in a [MeshLayout] to specific pools.
|
/// Mappings of the attributes in a [MeshLayout] to specific pools.
|
||||||
pub struct MeshLayoutBindings<'a> {
|
pub struct MeshLayoutBindings<'a> {
|
||||||
lock: std::sync::RwLockReadGuard<'a, HashMap<AttrId, Vec<AttrPool>>>,
|
lock: RwLockReadGuard<'a, HashMap<AttrId, Vec<AttrPool>>>,
|
||||||
indices: MeshLayoutBindingIndices,
|
indices: MeshLayoutBindingIndices,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,7 +406,7 @@ impl MeshPool {
|
||||||
///
|
///
|
||||||
/// TODO: keep track of mesh allocations that fit each layout
|
/// TODO: keep track of mesh allocations that fit each layout
|
||||||
pub fn add_layout(&self, layout: MeshLayoutDesc) -> Result<MeshLayoutId, PoolError> {
|
pub fn add_layout(&self, layout: MeshLayoutDesc) -> Result<MeshLayoutId, PoolError> {
|
||||||
let idx = self.mesh_layouts.write().unwrap().insert(layout);
|
let idx = self.mesh_layouts.write().insert(layout);
|
||||||
Ok(MeshLayoutId(idx))
|
Ok(MeshLayoutId(idx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -423,7 +424,7 @@ impl MeshPool {
|
||||||
let mut attr_allocs = SmallVec::new();
|
let mut attr_allocs = SmallVec::new();
|
||||||
let mut copies = Vec::new();
|
let mut copies = Vec::new();
|
||||||
for (id, buf) in attrs.drain() {
|
for (id, buf) in attrs.drain() {
|
||||||
let mut pools_write = self.pools.write().unwrap();
|
let mut pools_write = self.pools.write();
|
||||||
|
|
||||||
let pools = match pools_write.get_mut(&id) {
|
let pools = match pools_write.get_mut(&id) {
|
||||||
Some(pools) => pools,
|
Some(pools) => pools,
|
||||||
|
@ -465,13 +466,13 @@ impl MeshPool {
|
||||||
let alloc = MeshAlloc {
|
let alloc = MeshAlloc {
|
||||||
attributes: attr_allocs,
|
attributes: attr_allocs,
|
||||||
};
|
};
|
||||||
let key = self.allocs.write().unwrap().insert(alloc);
|
let key = self.allocs.write().insert(alloc);
|
||||||
let handle = MeshHandle(key);
|
let handle = MeshHandle(key);
|
||||||
Ok(handle)
|
Ok(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn flush(&self, commands: &mut wgpu::CommandEncoder) {
|
pub fn flush(&self, commands: &mut wgpu::CommandEncoder) {
|
||||||
let pools_read = self.pools.read().unwrap();
|
let pools_read = self.pools.read();
|
||||||
|
|
||||||
let get_dst = |target: &AttrAllocKey| {
|
let get_dst = |target: &AttrAllocKey| {
|
||||||
pools_read
|
pools_read
|
||||||
|
@ -502,7 +503,6 @@ impl MeshPool {
|
||||||
let layout = self
|
let layout = self
|
||||||
.mesh_layouts
|
.mesh_layouts
|
||||||
.read()
|
.read()
|
||||||
.unwrap()
|
|
||||||
.get(layout.0)
|
.get(layout.0)
|
||||||
.ok_or(PoolError::LayoutUnregistered)?
|
.ok_or(PoolError::LayoutUnregistered)?
|
||||||
.clone();
|
.clone();
|
||||||
|
@ -510,7 +510,7 @@ impl MeshPool {
|
||||||
let layout_attrs: Vec<AttrId> = layout.iter().map(|(id, _)| *id).collect();
|
let layout_attrs: Vec<AttrId> = layout.iter().map(|(id, _)| *id).collect();
|
||||||
let mut layouts = Vec::<MeshLayoutInstances<T>>::new();
|
let mut layouts = Vec::<MeshLayoutInstances<T>>::new();
|
||||||
|
|
||||||
let allocs_read = self.allocs.read().unwrap();
|
let allocs_read = self.allocs.read();
|
||||||
let mut attr_allocs: Vec<AttrAllocKey> = Vec::with_capacity(layout_attrs.len());
|
let mut attr_allocs: Vec<AttrAllocKey> = Vec::with_capacity(layout_attrs.len());
|
||||||
for mesh in meshes {
|
for mesh in meshes {
|
||||||
let handle = get_handle(&mesh);
|
let handle = get_handle(&mesh);
|
||||||
|
@ -535,7 +535,7 @@ impl MeshPool {
|
||||||
let mut layout_bindings = MeshLayoutBindingIndices::default();
|
let mut layout_bindings = MeshLayoutBindingIndices::default();
|
||||||
let mut alloc_infos = MeshAllocInfos::default();
|
let mut alloc_infos = MeshAllocInfos::default();
|
||||||
for alloc in attr_allocs.iter() {
|
for alloc in attr_allocs.iter() {
|
||||||
let pools_read = self.pools.read().unwrap();
|
let pools_read = self.pools.read();
|
||||||
let pools = pools_read
|
let pools = pools_read
|
||||||
.get(&alloc.attr)
|
.get(&alloc.attr)
|
||||||
.ok_or(PoolError::AttrUnregistered)?;
|
.ok_or(PoolError::AttrUnregistered)?;
|
||||||
|
@ -565,7 +565,7 @@ impl MeshPool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_bindings<'a>(&'a self, indices: MeshLayoutBindingIndices) -> MeshLayoutBindings<'a> {
|
pub fn get_bindings<'a>(&'a self, indices: MeshLayoutBindingIndices) -> MeshLayoutBindings<'a> {
|
||||||
let lock = self.pools.read().unwrap();
|
let lock = self.pools.read();
|
||||||
MeshLayoutBindings { lock, indices }
|
MeshLayoutBindings { lock, indices }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue