diff --git a/crates/compose/src/field_cache.rs b/crates/compose/src/field_cache.rs index e03fcc94..42ad9ebc 100644 --- a/crates/compose/src/field_cache.rs +++ b/crates/compose/src/field_cache.rs @@ -6,6 +6,7 @@ use texturecraft_geom::UvGBuffer; use crate::eval::Inputs; use crate::field_cache_key::FieldCacheKey; +use crate::sampled_map::SampledMap; use crate::working_field::working_block; /// Most fields cached, and the bytes they may hold (the newest is kept whatever its size) @@ -47,7 +48,9 @@ impl FieldCache { ) -> Arc> { let gbuf = inp.gbuf; let block = working_block(inp); - let read = maps.iter().filter_map(|map| inp.baked.maps.get(map).map(|data| (Arc::downgrade(data), (inp.baked.resolution, inp.baked.height(*map))))); + let read = maps.iter().filter_map(|map| { + inp.baked.maps.get(map).map(|data| SampledMap { data: Arc::downgrade(data), baked_size: (inp.baked.resolution, inp.baked.height(*map)) }) + }); let key = FieldCacheKey { layout: gbuf.tri.as_ptr() as usize, size: (gbuf.width, gbuf.height), @@ -63,7 +66,7 @@ impl FieldCache { // A hit moves to the most recently used end let cached = { let mut entries = self.entries.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); - entries.retain(|(key, _)| key.maps.iter().all(|(map, _)| map.strong_count() > 0)); + entries.retain(|(key, _)| key.maps.iter().all(|map| map.data.strong_count() > 0)); let hit = entries.iter().position(|(cached, _)| *cached == key).map(|index| entries.remove(index)); hit.map(|(cached, field)| { entries.push((cached, field.clone())); diff --git a/crates/compose/src/field_cache_key.rs b/crates/compose/src/field_cache_key.rs index 1010ac97..2f44ab4f 100644 --- a/crates/compose/src/field_cache_key.rs +++ b/crates/compose/src/field_cache_key.rs @@ -1,9 +1,9 @@ -use std::sync::Weak; - use texturecraft_core::BlockScales; +use crate::sampled_map::SampledMap; + /// What a field was built from: the working layout (the g-buffer's address, size, coverage, UV tile block and tile scales), the mesh's positions and UVs, the mesh maps it reads and its settings' bits -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub(crate) struct FieldCacheKey { pub(crate) layout: usize, pub(crate) size: (u32, u32), @@ -14,23 +14,6 @@ pub(crate) struct FieldCacheKey { /// The mesh's positions and UVs, each by address and length: a UV edit changes island fields pub(crate) positions: (usize, usize), pub(crate) uvs: (usize, usize), - /// The mesh maps it samples, each with the size it was baked at: one allocation read as another layout reads other texels - pub(crate) maps: Vec<(Weak>, (u32, u32))>, + pub(crate) maps: Vec, pub(crate) settings: Vec, } - -impl PartialEq for FieldCacheKey { - fn eq(&self, other: &FieldCacheKey) -> bool { - let same_maps = self.maps.len() == other.maps.len() - && self.maps.iter().zip(&other.maps).all(|((a, a_size), (b, b_size))| a.strong_count() > 0 && a.ptr_eq(b) && a_size == b_size); - same_maps - && self.layout == other.layout - && self.size == other.size - && self.coverage == other.coverage - && self.block == other.block - && self.scales == other.scales - && self.positions == other.positions - && self.uvs == other.uvs - && self.settings == other.settings - } -} diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index e7d72fe8..724cf4ad 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -43,6 +43,7 @@ pub mod projector_cover; pub mod projector_depth; mod projector_depth_cache; pub mod projector_view; +mod sampled_map; mod sections; mod shape_distance; mod slope_texel; diff --git a/crates/compose/src/sampled_map.rs b/crates/compose/src/sampled_map.rs new file mode 100644 index 00000000..1583f3f6 --- /dev/null +++ b/crates/compose/src/sampled_map.rs @@ -0,0 +1,14 @@ +use std::sync::Weak; + +/// A mesh map a whole-map field samples: its allocation and the size it was baked at (one allocation read as another layout reads other texels) +#[derive(Clone)] +pub(crate) struct SampledMap { + pub(crate) data: Weak>, + pub(crate) baked_size: (u32, u32), +} + +impl PartialEq for SampledMap { + fn eq(&self, other: &SampledMap) -> bool { + self.data.strong_count() > 0 && self.data.ptr_eq(&other.data) && self.baked_size == other.baked_size + } +}