Use parking_lot sync primitives

This commit is contained in:
mars 2022-05-08 16:25:33 -06:00
parent ce22735180
commit b06a8c7982
5 changed files with 29 additions and 26 deletions

View File

@ -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"

View File

@ -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

View File

@ -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<ShaderHandle, ShaderError> {
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<ShaderStoreReadGuard> {
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)
}

View File

@ -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<T> {
device: Arc<wgpu::Device>,
@ -28,7 +29,7 @@ impl<T: Clone> StagingPool<T> {
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<T: Clone> StagingPool<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.extend(copies.into_iter());
}

View File

@ -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<T: 'static + Attribute>(&self) -> AttrId {
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 {
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<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.
pub struct MeshLayoutBindings<'a> {
lock: std::sync::RwLockReadGuard<'a, HashMap<AttrId, Vec<AttrPool>>>,
lock: RwLockReadGuard<'a, HashMap<AttrId, Vec<AttrPool>>>,
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<MeshLayoutId, PoolError> {
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<AttrId> = layout.iter().map(|(id, _)| *id).collect();
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());
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 }
}
}