diff --git a/Cargo.toml b/Cargo.toml index 6bcaab8..c234b06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ bytemuck = { version = "^1.0", features = ["derive"] } glam = "0.20" multimap = "0.8" notify = "^4" +parking_lot = "^0.11" pollster = "0.2" rayon = "1" slab = "^0.4" diff --git a/src/lib.rs b/src/lib.rs index 8fe1c85..8a1e593 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ use rayon::prelude::*; use std::sync::Arc; -use std::sync::Mutex; +use parking_lot::Mutex; pub mod camera; pub mod pass; @@ -244,12 +244,12 @@ impl Renderer { let pass = &self.render_passes[*pass_index]; 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 diff --git a/src/shader.rs b/src/shader.rs index 8b80c04..3ac07ee 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -1,10 +1,11 @@ use notify::{raw_watcher, RawEvent, RecommendedWatcher, RecursiveMode, Watcher}; +use parking_lot::{RwLock, RwLockReadGuard}; use slab::Slab; use std::collections::{BTreeSet, HashMap}; use std::fs::read_to_string; use std::path::{Path, PathBuf}; use std::sync::mpsc::{channel, Receiver, TryRecvError}; -use std::sync::{Arc, RwLock, RwLockReadGuard}; +use std::sync::Arc; #[repr(transparent)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] @@ -42,12 +43,12 @@ impl ShaderStore { pub fn load(&self, module: &naga::Module) -> Result { let source = self.generate_wgsl(module)?; let shader = self.load_wgsl(source)?; - let index = self.shaders.write().unwrap().insert(shader); + let index = self.shaders.write().insert(shader); Ok(ShaderHandle(index)) } 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) { Some(stored) => { let source = self.generate_wgsl(module)?; @@ -90,7 +91,7 @@ impl ShaderStore { } pub fn get(&self, handle: &ShaderHandle) -> Option { - let guard = self.shaders.read().unwrap(); + let guard = self.shaders.read(); if let Some(_) = guard.get(handle.0) { Some(ShaderStoreReadGuard { guard, @@ -233,7 +234,7 @@ impl ShaderWatcher { } 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(); match infos_read.get(&path_buf) { Some(handle) => { @@ -250,7 +251,7 @@ impl ShaderWatcher { let mut loader = ShaderLoader::new(&self.store, &self.root_path, None); loader.include_file(&shader_path_buf); 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); Ok(info.handle) } diff --git a/src/staging.rs b/src/staging.rs index 5bb72dd..5b7122b 100644 --- a/src/staging.rs +++ b/src/staging.rs @@ -5,7 +5,8 @@ //! TODO: pass access to a wgpu::Queue for write_buffer, staging belt recall, or command encoding use std::collections::VecDeque; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; +use parking_lot::Mutex; pub struct StagingPool { device: Arc, @@ -28,7 +29,7 @@ impl StagingPool { get_dst: impl Fn(&T) -> CopyDest<'a>, on_complete: impl Fn(T), ) { - let mut spillover = self.spillover.lock().unwrap(); + let mut spillover = self.spillover.lock(); if spillover.is_empty() { return; } @@ -70,7 +71,7 @@ impl StagingPool { } pub fn queue_copies(&self, copies: Vec>) { - let mut spillover = self.spillover.lock().unwrap(); + let mut spillover = self.spillover.lock(); spillover.reserve(copies.len()); spillover.extend(copies.into_iter()); } diff --git a/src/storage/mesh.rs b/src/storage/mesh.rs index a8391fd..e3b319c 100644 --- a/src/storage/mesh.rs +++ b/src/storage/mesh.rs @@ -3,7 +3,8 @@ use slab::Slab; use smallvec::SmallVec; use std::any::TypeId; 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. #[derive(Debug)] @@ -82,7 +83,7 @@ impl AttrStore { /// Dynamically creates a new [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) } @@ -92,7 +93,7 @@ impl AttrStore { /// existing [AttrId]. pub fn get_type(&self) -> AttrId { let type_id = TypeId::of::(); - 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 { id @@ -106,14 +107,14 @@ impl AttrStore { default_pool_size, }; let id = self.add(info); - self.types.write().unwrap().insert(type_id, id); + self.types.write().insert(type_id, id); id } } /// Gets the [AttrInfo] for an [AttrId]. pub fn get_info(&self, id: &AttrId) -> Option { - self.attributes.read().unwrap().get(id.0).copied() + self.attributes.read().get(id.0).copied() } } @@ -357,7 +358,7 @@ pub type MeshLayoutBindingIndices = smallmap::Map; /// Mappings of the attributes in a [MeshLayout] to specific pools. pub struct MeshLayoutBindings<'a> { - lock: std::sync::RwLockReadGuard<'a, HashMap>>, + lock: RwLockReadGuard<'a, HashMap>>, indices: MeshLayoutBindingIndices, } @@ -405,7 +406,7 @@ impl MeshPool { /// /// TODO: keep track of mesh allocations that fit each layout pub fn add_layout(&self, layout: MeshLayoutDesc) -> Result { - let idx = self.mesh_layouts.write().unwrap().insert(layout); + let idx = self.mesh_layouts.write().insert(layout); Ok(MeshLayoutId(idx)) } @@ -423,7 +424,7 @@ impl MeshPool { let mut attr_allocs = SmallVec::new(); let mut copies = Vec::new(); 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) { Some(pools) => pools, @@ -465,13 +466,13 @@ impl MeshPool { let alloc = MeshAlloc { attributes: attr_allocs, }; - let key = self.allocs.write().unwrap().insert(alloc); + let key = self.allocs.write().insert(alloc); let handle = MeshHandle(key); Ok(handle) } 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| { pools_read @@ -502,7 +503,6 @@ impl MeshPool { let layout = self .mesh_layouts .read() - .unwrap() .get(layout.0) .ok_or(PoolError::LayoutUnregistered)? .clone(); @@ -510,7 +510,7 @@ impl MeshPool { let layout_attrs: Vec = layout.iter().map(|(id, _)| *id).collect(); let mut layouts = Vec::>::new(); - let allocs_read = self.allocs.read().unwrap(); + let allocs_read = self.allocs.read(); let mut attr_allocs: Vec = Vec::with_capacity(layout_attrs.len()); for mesh in meshes { let handle = get_handle(&mesh); @@ -535,7 +535,7 @@ impl MeshPool { let mut layout_bindings = MeshLayoutBindingIndices::default(); let mut alloc_infos = MeshAllocInfos::default(); for alloc in attr_allocs.iter() { - let pools_read = self.pools.read().unwrap(); + let pools_read = self.pools.read(); let pools = pools_read .get(&alloc.attr) .ok_or(PoolError::AttrUnregistered)?; @@ -565,7 +565,7 @@ impl MeshPool { } 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 } } }