Skip to content

Commit

Permalink
fix(model): don't keep material references
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed Aug 20, 2024
1 parent 21d10a1 commit 853f779
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/nodes/drawable/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use color_eyre::eyre::{bail, eyre, Result};
use glam::{Mat4, Vec2, Vec3};
use once_cell::sync::{Lazy, OnceCell};
use parking_lot::Mutex;
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashMap;
use stardust_xr::values::ResourceID;
use std::ffi::OsStr;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -57,17 +57,25 @@ unsafe impl Send for MaterialWrapper {}
unsafe impl Sync for MaterialWrapper {}

#[derive(Default)]
struct MaterialRegistry(Mutex<FxHashSet<Arc<MaterialWrapper>>>);
struct MaterialRegistry(Mutex<FxHashMap<u64, String>>);
impl MaterialRegistry {
fn add_or_get(&self, material: Arc<MaterialWrapper>) -> Arc<MaterialWrapper> {
let mut lock = self.0.lock();
let hash = {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
material.hash(&mut hasher);
hasher.finish()
};

if let Some(existing) = lock.get(&material) {
existing.clone()
} else {
lock.insert(material.clone());
material
if let Some(id) = lock.get(&hash) {
if let Ok(existing) = Material::find(id) {
return Arc::new(MaterialWrapper(existing));
}
}

lock.insert(hash, material.0.get_id().to_string());
material
}
}

Expand Down

0 comments on commit 853f779

Please sign in to comment.