mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-21 06:50:53 +01:00
Configure and clear the library preview cache
This commit is contained in:
1 parent
db0baa1c2d
commit
941aa5722f
17 files changed
+665
-155
No files matched your search
@@ -66,6 +66,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
|
||||
v.extend(crate::material_cmds::specs());
|
||||
v.extend(crate::edit_cmds::specs());
|
||||
v.extend(crate::document_history_commands::specs());
|
||||
v.extend(crate::library_cache_commands::specs());
|
||||
v.extend(crate::embedded_edit_commands::specs());
|
||||
v.extend(crate::graph_cmds::specs());
|
||||
v.extend(crate::exposed_input_commands::specs());
|
||||
|
||||
@@ -91,6 +91,7 @@ pub fn session(session: &Session, requested_graph: Option<usize>) -> J {
|
||||
"dirty": session.dirty,
|
||||
"history": session.history(),
|
||||
"historyPreferences": session.history_prefs,
|
||||
"libraryCachePreferences": session.library_cache_prefs,
|
||||
"canUndo": session.can_undo(),
|
||||
"canRedo": session.can_redo(),
|
||||
"log": session.log.iter().rev().take(20).collect::<Vec<_>>(),
|
||||
|
||||
@@ -58,8 +58,10 @@ pub mod graph_cmds;
|
||||
mod graph_property_commands;
|
||||
mod graph_target;
|
||||
mod history_prefs;
|
||||
mod library_cache_commands;
|
||||
pub use graph_target::GraphTarget;
|
||||
pub use history_prefs::HistoryPrefs;
|
||||
pub use library_cache_commands::LibraryCachePrefs;
|
||||
pub mod graph_edit;
|
||||
pub mod graph_grid;
|
||||
pub mod inspect;
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
use crate::{
|
||||
Session, cmd,
|
||||
commands::{CommandSpec, always, bad},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// Disposable library thumbnails; independent of package contents and undo history.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct LibraryCachePrefs {
|
||||
/// Zero disables disk persistence.
|
||||
pub disk_mib: u32,
|
||||
pub max_previews: u32,
|
||||
}
|
||||
|
||||
impl Default for LibraryCachePrefs {
|
||||
fn default() -> Self {
|
||||
Self { disk_mib: 64, max_previews: 4096 }
|
||||
}
|
||||
}
|
||||
|
||||
impl LibraryCachePrefs {
|
||||
pub const MAX_DISK_MIB: u32 = 65_536;
|
||||
// Bound directory scans and metadata allocations even with externally supplied settings.
|
||||
pub const MAX_PREVIEWS: u32 = 65_536;
|
||||
|
||||
pub fn sanitized(self) -> Self {
|
||||
Self { disk_mib: self.disk_mib.min(Self::MAX_DISK_MIB), max_previews: self.max_previews.clamp(1, Self::MAX_PREVIEWS) }
|
||||
}
|
||||
}
|
||||
|
||||
fn limit(parameters: &Value, key: &str, default: u32, min: u32, max: u32) -> crate::Result<u32> {
|
||||
match parameters.get(key) {
|
||||
None => Ok(default),
|
||||
Some(value) => value
|
||||
.as_u64()
|
||||
.and_then(|v| u32::try_from(v).ok())
|
||||
.filter(|v| (min..=max).contains(v))
|
||||
.ok_or_else(|| bad("library.cache.configure", format!("{key} must be an integer from {min} to {max}"))),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"library.cache.configure",
|
||||
"Library preview cache preferences",
|
||||
[],
|
||||
None,
|
||||
"{diskMib?: 0..65536, maxPreviews?: 1..65536}; 0 MiB disables persistence; {} reads preferences; does not modify packages",
|
||||
always,
|
||||
|session, parameters| {
|
||||
let prefs = session.library_cache_prefs;
|
||||
let disk_mib = limit(parameters, "diskMib", prefs.disk_mib, 0, LibraryCachePrefs::MAX_DISK_MIB)?;
|
||||
let max_previews = limit(parameters, "maxPreviews", prefs.max_previews, 1, LibraryCachePrefs::MAX_PREVIEWS)?;
|
||||
session.library_cache_prefs = LibraryCachePrefs { disk_mib, max_previews };
|
||||
Ok(json!(session.library_cache_prefs))
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"library.cache.clear",
|
||||
"Clear library preview cache",
|
||||
[],
|
||||
None,
|
||||
"{} requests background disk cleanup and discards loaded library thumbnails; visible previews rebuild on demand in the app",
|
||||
always,
|
||||
|session: &mut Session, _| {
|
||||
session.library_cache_clear_epoch = session.library_cache_clear_epoch.wrapping_add(1);
|
||||
Ok(json!({"requested": true}))
|
||||
}
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn preferences_and_clear_do_not_edit_packages_and_invalid_updates_are_atomic() {
|
||||
let mut session = Session::new(crate::core::Package::new("Cache preferences"));
|
||||
let history = session.history();
|
||||
let modified = session.modified;
|
||||
crate::execute(&mut session, "library.cache.configure", &json!({"diskMib": 512, "maxPreviews": 12000})).unwrap();
|
||||
let expected = LibraryCachePrefs { disk_mib: 512, max_previews: 12000 };
|
||||
assert_eq!(session.library_cache_prefs, expected);
|
||||
for parameters in [
|
||||
json!({"diskMib": -1}),
|
||||
json!({"diskMib": 65537}),
|
||||
json!({"diskMib": "64"}),
|
||||
json!({"diskMib": 32, "maxPreviews": 0}),
|
||||
json!({"maxPreviews": 65537}),
|
||||
json!({"diskMib": 1.5}),
|
||||
] {
|
||||
assert!(crate::execute(&mut session, "library.cache.configure", ¶meters).is_err());
|
||||
assert_eq!(session.library_cache_prefs, expected);
|
||||
}
|
||||
assert_eq!(crate::execute(&mut session, "library.cache.configure", &json!({})).unwrap(), json!(expected));
|
||||
crate::execute(&mut session, "library.cache.clear", &json!({})).unwrap();
|
||||
assert_eq!(session.library_cache_clear_epoch, 1);
|
||||
assert_eq!(session.history(), history);
|
||||
assert_eq!(session.modified, modified);
|
||||
crate::execute(&mut session, "library.cache.configure", &json!({"diskMib": 0})).unwrap();
|
||||
assert_eq!(session.library_cache_prefs.disk_mib, 0);
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,9 @@ pub struct Session {
|
||||
/// 3D view camera preferences (set with [`Session::set_camera_prefs`]).
|
||||
pub camera: CameraPrefs,
|
||||
pub history_prefs: crate::HistoryPrefs,
|
||||
pub library_cache_prefs: crate::LibraryCachePrefs,
|
||||
/// Requests handled by the UI without editing packages or their undo histories.
|
||||
pub library_cache_clear_epoch: u64,
|
||||
pub modified: bool,
|
||||
pub log: Vec<String>,
|
||||
/// What the last opened SBS package held that MaterialCraft couldn't read; empty after a clean open.
|
||||
@@ -203,6 +206,8 @@ impl Session {
|
||||
preview_size: Some(512),
|
||||
camera: CameraPrefs::default(),
|
||||
history_prefs: crate::HistoryPrefs::default(),
|
||||
library_cache_prefs: crate::LibraryCachePrefs::default(),
|
||||
library_cache_clear_epoch: 0,
|
||||
modified: false,
|
||||
log: Vec::new(),
|
||||
load_warnings: Vec::new(),
|
||||
|
||||
@@ -262,6 +262,9 @@ fn handle_inner(app: &mut MaterialCraftApp, ctx: &egui::Context, method: &str, p
|
||||
if id == "view.previewSize" {
|
||||
app.ui.prefs.preview_size = app.session.preview_size.unwrap_or(0);
|
||||
}
|
||||
if id == "library.cache.configure" {
|
||||
app.ui.prefs.library_cache = app.session.library_cache_prefs;
|
||||
}
|
||||
if id == "session.history" {
|
||||
app.ui.prefs.history = app.session.history_prefs;
|
||||
}
|
||||
@@ -471,6 +474,28 @@ mod tests {
|
||||
assert_eq!(app.ui.prefs.autosave_minutes, 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_commands_and_saved_preferences_stay_in_sync() {
|
||||
let mut app = app();
|
||||
let ctx = egui::Context::default();
|
||||
let reply = handle(&mut app, &ctx, "command.run", &json!({"id": "library.cache.configure", "params": {"diskMib": 256, "maxPreviews": 8000}}));
|
||||
assert_eq!(reply["ok"], json!(true), "{reply}");
|
||||
app.apply_prefs();
|
||||
assert_eq!(app.ui.prefs.library_cache, app.session.library_cache_prefs);
|
||||
assert_eq!(app.ui.prefs.library_cache.disk_mib, 256);
|
||||
let saved: crate::UiState = serde_json::from_value(serde_json::to_value(&app.ui).unwrap()).unwrap();
|
||||
let mut restored = MaterialCraftApp::headless(materialcraft_engine::sample_session(), saved);
|
||||
assert_eq!(restored.session.library_cache_prefs, app.session.library_cache_prefs);
|
||||
let reply = handle(&mut restored, &ctx, "ui.set", &json!({"prefs": {"libraryCache": {"diskMib": 32}}}));
|
||||
assert_eq!(reply["ok"], json!(true), "{reply}");
|
||||
restored.apply_prefs();
|
||||
assert_eq!(restored.session.library_cache_prefs.disk_mib, 32);
|
||||
assert_eq!(restored.session.library_cache_prefs.max_previews, 8000);
|
||||
let reply = handle(&mut restored, &ctx, "command.run", &json!({"id": "library.cache.clear"}));
|
||||
assert_eq!(reply["ok"], json!(true), "{reply}");
|
||||
assert_eq!(restored.session.library_cache_clear_epoch, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ui_set_clamps_camera_zoom() {
|
||||
let mut app = app();
|
||||
|
||||
@@ -8,7 +8,11 @@ use materialcraft_engine::NodeLibrary;
|
||||
|
||||
use super::LIBRARY_PREVIEW_SIZE;
|
||||
|
||||
type Key = (String, String);
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
enum Key {
|
||||
Graph(String, String),
|
||||
Native(String),
|
||||
}
|
||||
const MAX_TEXTURES: usize = 256; // About 5 MiB of RGBA pixels, independent of library size.
|
||||
const MAX_VISIBLE_REQUESTS: usize = 64;
|
||||
|
||||
@@ -16,7 +20,7 @@ const MAX_VISIBLE_REQUESTS: usize = 64;
|
||||
#[derive(Default)]
|
||||
pub(super) struct GraphPreviews {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub persist: bool,
|
||||
pub persistence: Option<super::preview_cache_settings::Settings>,
|
||||
library: Option<Arc<NodeLibrary>>,
|
||||
textures: HashMap<Key, (Option<TextureHandle>, u64)>,
|
||||
wanted: Vec<Key>,
|
||||
@@ -41,7 +45,14 @@ impl Drop for Active {
|
||||
impl GraphPreviews {
|
||||
pub fn get(&mut self, _ctx: &egui::Context, library: &Arc<NodeLibrary>, package: &str, graph: &str) -> Option<egui::TextureId> {
|
||||
self.set_library(library);
|
||||
let key = (package.to_string(), graph.to_string());
|
||||
self.request(Key::Graph(package.to_string(), graph.to_string()))
|
||||
}
|
||||
|
||||
pub fn native(&mut self, _ctx: &egui::Context, id: &str) -> Option<egui::TextureId> {
|
||||
self.request(Key::Native(id.into()))
|
||||
}
|
||||
|
||||
fn request(&mut self, key: Key) -> Option<egui::TextureId> {
|
||||
if let Some((texture, used)) = self.textures.get_mut(&key) {
|
||||
*used = self.frame;
|
||||
return texture.as_ref().map(TextureHandle::id);
|
||||
@@ -52,6 +63,15 @@ impl GraphPreviews {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.textures.clear();
|
||||
self.wanted.clear();
|
||||
if let Some(active) = &self.active {
|
||||
active.cancel.store(true, Ordering::Relaxed);
|
||||
}
|
||||
// Keep the receiver until its worker exits so clearing cannot pile up render threads.
|
||||
}
|
||||
|
||||
fn set_library(&mut self, library: &Arc<NodeLibrary>) {
|
||||
if !self.library.as_ref().is_some_and(|current| Arc::ptr_eq(current, library)) {
|
||||
self.library = Some(library.clone());
|
||||
@@ -80,7 +100,7 @@ impl GraphPreviews {
|
||||
if !active.cancel.load(Ordering::Relaxed) && self.library.as_ref().is_some_and(|library| Arc::ptr_eq(library, &active.library)) {
|
||||
let texture = image.map(|rgba| {
|
||||
let image = ColorImage::from_rgba_unmultiplied([LIBRARY_PREVIEW_SIZE as usize; 2], &rgba);
|
||||
ctx.load_texture(format!("library-graph-{}#{}", active.key.0, active.key.1), image, TextureOptions::LINEAR)
|
||||
ctx.load_texture(format!("library-preview-{:?}", active.key), image, TextureOptions::LINEAR)
|
||||
});
|
||||
self.remember(active.key.clone(), texture);
|
||||
}
|
||||
@@ -91,6 +111,7 @@ impl GraphPreviews {
|
||||
active.cancel.store(true, Ordering::Relaxed);
|
||||
}
|
||||
} else if !foreground_busy
|
||||
&& self.cache_ready()
|
||||
&& let Some(library) = self.library.clone()
|
||||
&& let Some(key) = self.wanted.iter().rev().find(|key| !self.textures.contains_key(*key)).cloned()
|
||||
{
|
||||
@@ -112,14 +133,26 @@ impl GraphPreviews {
|
||||
let (sender, result) = std::sync::mpsc::sync_channel(1);
|
||||
let cancel = Arc::new(AtomicBool::new(false));
|
||||
let (thread_cancel, thread_library, thread_key, context) = (cancel.clone(), library.clone(), key.clone(), ctx.clone());
|
||||
let persist = self.persist;
|
||||
let persistence = self.persistence.clone();
|
||||
let spawned = std::thread::Builder::new().name("materialcraft-library-graphs".into()).spawn(move || {
|
||||
let render = || {
|
||||
NodeLibrary::preview_cancellable(&thread_library, &thread_key.0, &thread_key.1, LIBRARY_PREVIEW_SIZE, &thread_cancel)
|
||||
.map(|image| image.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display())
|
||||
if thread_cancel.load(Ordering::Relaxed) {
|
||||
return None;
|
||||
}
|
||||
match &thread_key {
|
||||
Key::Native(id) => super::render_native(id),
|
||||
Key::Graph(package, graph) => NodeLibrary::preview_cancellable(&thread_library, package, graph, LIBRARY_PREVIEW_SIZE, &thread_cancel)
|
||||
.map(|image| image.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display()),
|
||||
}
|
||||
};
|
||||
let disk = persist.then(super::preview_disk_cache::PreviewDiskCache::configured).flatten();
|
||||
let fingerprint = disk.as_ref().and_then(|_| thread_library.preview_fingerprint(&thread_key.0, &thread_key.1, &thread_cancel));
|
||||
let disk = persistence.and_then(super::preview_disk_cache::PreviewDiskCache::configured);
|
||||
let fingerprint = disk.as_ref().filter(|disk| disk.enabled()).and_then(|_| match &thread_key {
|
||||
Key::Native(id) => {
|
||||
use sha2::Digest;
|
||||
Some(sha2::Sha256::digest(format!("materialcraft-atomic-preview-v1:{id}")).into())
|
||||
}
|
||||
Key::Graph(package, graph) => thread_library.preview_fingerprint(package, graph, &thread_cancel),
|
||||
});
|
||||
let rgba = match (disk, fingerprint) {
|
||||
(Some(disk), Some(key)) => disk.get_or_render(key, LIBRARY_PREVIEW_SIZE, &thread_cancel, render),
|
||||
_ => render(),
|
||||
@@ -134,10 +167,30 @@ impl GraphPreviews {
|
||||
}
|
||||
}
|
||||
|
||||
fn cache_ready(&self) -> bool {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Some(settings) = &self.persistence {
|
||||
return !settings.get().clear_pending;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn start(&mut self, _ctx: &egui::Context, _library: Arc<NodeLibrary>, key: Key) {
|
||||
// The browser has no cook worker; keep the category icon without retrying every frame.
|
||||
self.remember(key, None);
|
||||
fn start(&mut self, ctx: &egui::Context, _library: Arc<NodeLibrary>, key: Key) {
|
||||
// Native previews remain available in the browser without a filesystem or cook worker.
|
||||
let rgba = match &key {
|
||||
Key::Native(id) => super::render_native(id),
|
||||
Key::Graph(_, _) => None,
|
||||
};
|
||||
let texture = rgba.map(|rgba| {
|
||||
ctx.load_texture(
|
||||
format!("library-preview-{key:?}"),
|
||||
ColorImage::from_rgba_unmultiplied([LIBRARY_PREVIEW_SIZE as usize; 2], &rgba),
|
||||
TextureOptions::LINEAR,
|
||||
)
|
||||
});
|
||||
self.remember(key, texture);
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +208,7 @@ mod tests {
|
||||
let context = egui::Context::default();
|
||||
let library = Arc::new(NodeLibrary::default());
|
||||
let mut previews = GraphPreviews::default();
|
||||
let key = ("test".into(), "graph".into());
|
||||
let key = Key::Graph("test".into(), "graph".into());
|
||||
let (job, _sender) = active(library.clone(), key.clone());
|
||||
let cancelled = job.cancel.clone();
|
||||
previews.active = Some(job);
|
||||
@@ -175,7 +228,7 @@ mod tests {
|
||||
let context = egui::Context::default();
|
||||
let library = Arc::new(NodeLibrary::default());
|
||||
let old = Arc::downgrade(&library);
|
||||
let (job, sender) = active(library.clone(), ("test".into(), "graph".into()));
|
||||
let (job, sender) = active(library.clone(), Key::Graph("test".into(), "graph".into()));
|
||||
let mut previews = GraphPreviews { library: Some(library), active: Some(job), ..Default::default() };
|
||||
let new_library = Arc::new(NodeLibrary::default());
|
||||
previews.get(&context, &new_library, "new", "graph");
|
||||
@@ -194,15 +247,45 @@ mod tests {
|
||||
let mut previews = GraphPreviews::default();
|
||||
let texture = context.load_texture("old", ColorImage::new([1, 1], vec![egui::Color32::WHITE]), TextureOptions::LINEAR);
|
||||
let old_id = texture.id();
|
||||
previews.remember(("test".into(), "old".into()), Some(texture));
|
||||
previews.remember(Key::Graph("test".into(), "old".into()), Some(texture));
|
||||
for index in 0..MAX_TEXTURES {
|
||||
previews.frame += 1;
|
||||
previews.remember(("test".into(), index.to_string()), None);
|
||||
previews.remember(Key::Graph("test".into(), index.to_string()), None);
|
||||
}
|
||||
assert_eq!(previews.textures.len(), MAX_TEXTURES);
|
||||
assert!(context.tex_manager().read().meta(old_id).is_none(), "eviction must drop the GPU texture handle");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_discards_loaded_and_pending_tiles_and_rebuilds_only_requested_tiles() {
|
||||
let context = egui::Context::default();
|
||||
let library = Arc::new(NodeLibrary::default());
|
||||
let mut previews = GraphPreviews::default();
|
||||
previews.set_library(&library);
|
||||
let key = Key::Native("blur".into());
|
||||
let texture = context.load_texture("old", ColorImage::new([1, 1], vec![egui::Color32::WHITE]), TextureOptions::LINEAR);
|
||||
let old_id = texture.id();
|
||||
previews.remember(key.clone(), Some(texture));
|
||||
let (job, sender) = active(library.clone(), key);
|
||||
let cancelled = job.cancel.clone();
|
||||
previews.active = Some(job);
|
||||
previews.native(&context, "levels");
|
||||
previews.clear();
|
||||
assert!(previews.textures.is_empty());
|
||||
assert!(previews.wanted.is_empty());
|
||||
assert!(context.tex_manager().read().meta(old_id).is_none());
|
||||
assert!(cancelled.load(Ordering::Relaxed));
|
||||
sender.send(Some(vec![255; (LIBRARY_PREVIEW_SIZE * LIBRARY_PREVIEW_SIZE * 4) as usize])).unwrap();
|
||||
previews.collect(&context, &library, false);
|
||||
assert!(previews.active.is_none());
|
||||
assert!(previews.textures.is_empty(), "the old worker result must be discarded");
|
||||
previews.native(&context, "levels");
|
||||
previews.collect(&context, &library, false);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
assert_eq!(previews.active.as_ref().unwrap().key, Key::Native("levels".into()));
|
||||
assert!(previews.wanted.is_empty(), "clearing must not schedule the entire native catalog");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn editing_takes_priority_and_dropping_preview_state_cancels_work() {
|
||||
let context = egui::Context::default();
|
||||
@@ -211,7 +294,7 @@ mod tests {
|
||||
previews.get(&context, &library, "test", "graph");
|
||||
previews.collect(&context, &library, true);
|
||||
assert!(previews.active.is_none());
|
||||
let (job, _sender) = active(library.clone(), ("test".into(), "graph".into()));
|
||||
let (job, _sender) = active(library.clone(), Key::Graph("test".into(), "graph".into()));
|
||||
let cancelled = job.cancel.clone();
|
||||
previews.active = Some(job);
|
||||
drop(previews);
|
||||
|
||||
@@ -413,7 +413,7 @@ fn items(app: &mut MaterialCraftApp, ui: &mut Ui, entries: &[LibraryEntry], targ
|
||||
fn preview(app: &mut MaterialCraftApp, ui: &Ui, ctx: &egui::Context, entry: &LibraryEntry, rect: Rect, fade: f32) {
|
||||
let painter = ui.painter();
|
||||
let (texture, color) = match &entry.item {
|
||||
LibraryItem::Node { def } if entry.missing.is_none() && def != "dot" => (app.thumbs.library(ctx, def), header_color(def)),
|
||||
LibraryItem::Node { def } if entry.missing.is_none() && def != "dot" && ui.is_rect_visible(rect) => (app.thumbs.library(ctx, def), header_color(def)),
|
||||
// Only tiles on screen ask for their graph to be cooked
|
||||
LibraryItem::Instance { package, graph } if entry.missing.is_none() && ui.is_rect_visible(rect) => {
|
||||
(app.thumbs.graph(ctx, &app.session.library, package, graph), category_color(&entry.category))
|
||||
|
||||
@@ -77,7 +77,7 @@ impl MaterialCraftApp {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
app.dialogs = crate::native_dialogs::attached(&cc.egui_ctx);
|
||||
app.thumbs.enable_persistence();
|
||||
app.thumbs.enable_persistence(app.ui.prefs.library_cache);
|
||||
}
|
||||
app
|
||||
}
|
||||
@@ -137,6 +137,11 @@ impl MaterialCraftApp {
|
||||
|
||||
/// Push application preferences into the session
|
||||
pub fn apply_prefs(&mut self) {
|
||||
self.ui.prefs.library_cache = self.ui.prefs.library_cache.sanitized();
|
||||
if self.session.library_cache_prefs != self.ui.prefs.library_cache {
|
||||
let _ = materialcraft_engine::execute(&mut self.session, "library.cache.configure", &serde_json::json!(self.ui.prefs.library_cache));
|
||||
}
|
||||
self.thumbs.configure_cache(self.ui.prefs.library_cache, self.session.library_cache_clear_epoch);
|
||||
self.ui.prefs.history = self.ui.prefs.history.sanitized();
|
||||
if self.session.history_prefs != self.ui.prefs.history {
|
||||
let _ = materialcraft_engine::execute(&mut self.session, "session.history", &serde_json::json!(self.ui.prefs.history));
|
||||
@@ -224,6 +229,9 @@ impl MaterialCraftApp {
|
||||
if id == "view.previewSize" {
|
||||
self.ui.prefs.preview_size = self.session.preview_size.unwrap_or(0);
|
||||
}
|
||||
if id == "library.cache.configure" {
|
||||
self.ui.prefs.library_cache = self.session.library_cache_prefs;
|
||||
}
|
||||
if id == "session.history" {
|
||||
self.ui.prefs.history = self.session.history_prefs;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
//! Shared limits for existing workers and one background maintenance job at a time.
|
||||
use materialcraft_engine::LibraryCachePrefs;
|
||||
use std::sync::{
|
||||
Arc, Mutex,
|
||||
mpsc::{Receiver, TryRecvError},
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) struct Limits {
|
||||
pub budget: u64,
|
||||
pub entries: usize,
|
||||
}
|
||||
|
||||
impl From<LibraryCachePrefs> for Limits {
|
||||
fn from(prefs: LibraryCachePrefs) -> Self {
|
||||
let prefs = prefs.sanitized();
|
||||
Self { budget: u64::from(prefs.disk_mib) * 1024 * 1024, entries: prefs.max_previews as usize }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) struct State {
|
||||
pub limits: Limits,
|
||||
pub revision: u64,
|
||||
pub epoch: u64,
|
||||
pub clear_pending: bool,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct Settings(Arc<Mutex<State>>);
|
||||
|
||||
impl Settings {
|
||||
pub fn new(prefs: LibraryCachePrefs) -> Self {
|
||||
Self(Arc::new(Mutex::new(State { limits: prefs.into(), revision: 0, epoch: 0, clear_pending: false, disabled: false })))
|
||||
}
|
||||
|
||||
pub fn get(&self) -> State {
|
||||
*self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
pub fn update(&self, limits: Limits, clear: bool) {
|
||||
let mut state = self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if state.limits != limits || clear {
|
||||
state.limits = limits;
|
||||
state.revision = state.revision.wrapping_add(1);
|
||||
state.disabled = false;
|
||||
if clear {
|
||||
state.epoch = state.epoch.wrapping_add(1);
|
||||
state.clear_pending = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish(&self, epoch: u64, failed: bool) {
|
||||
let mut state = self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if state.epoch == epoch {
|
||||
state.clear_pending = false;
|
||||
// If cleanup fails, rebuild from source instead of showing files we could not clear.
|
||||
state.disabled = failed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct Maintenance {
|
||||
pub settings: Settings,
|
||||
active: Option<Receiver<std::io::Result<Option<State>>>>,
|
||||
applied: Option<u64>,
|
||||
retry_at: Option<Instant>,
|
||||
}
|
||||
|
||||
impl Maintenance {
|
||||
pub fn new(prefs: LibraryCachePrefs) -> Self {
|
||||
Self { settings: Settings::new(prefs), active: None, applied: None, retry_at: None }
|
||||
}
|
||||
|
||||
pub fn tick(&mut self, ctx: &egui::Context) -> Option<String> {
|
||||
let mut error = None;
|
||||
if let Some(active) = &self.active {
|
||||
let result = match active.try_recv() {
|
||||
Ok(result) => Some(result),
|
||||
Err(TryRecvError::Empty) => None,
|
||||
Err(TryRecvError::Disconnected) => Some(Err(std::io::Error::other("preview cache worker stopped"))),
|
||||
};
|
||||
if let Some(result) = result {
|
||||
self.active = None;
|
||||
match result {
|
||||
Ok(Some(state)) => {
|
||||
self.applied = Some(state.revision);
|
||||
self.settings.finish(state.epoch, false);
|
||||
}
|
||||
Ok(None) => self.retry_at = Some(Instant::now() + Duration::from_millis(100)),
|
||||
Err(reason) => {
|
||||
let state = self.settings.get();
|
||||
self.applied = Some(state.revision);
|
||||
self.settings.finish(state.epoch, true);
|
||||
error = Some(format!("Library preview cache unavailable: {reason}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.active.is_none() && self.applied != Some(self.settings.get().revision) {
|
||||
if let Some(delay) = self.retry_at.and_then(|when| when.checked_duration_since(Instant::now())) {
|
||||
ctx.request_repaint_after(delay);
|
||||
return error;
|
||||
}
|
||||
self.retry_at = None;
|
||||
let settings = self.settings.clone();
|
||||
let context = ctx.clone();
|
||||
let (sender, receiver) = std::sync::mpsc::sync_channel(1);
|
||||
match std::thread::Builder::new().name("materialcraft-preview-cache".into()).spawn(move || {
|
||||
let state = settings.get();
|
||||
let result = match super::preview_disk_cache::PreviewDiskCache::configured(settings) {
|
||||
Some(cache) => cache.maintain(state).map(|done| done.then_some(state)),
|
||||
None => Ok(Some(state)),
|
||||
};
|
||||
let _ = sender.send(result);
|
||||
context.request_repaint();
|
||||
}) {
|
||||
Ok(_) => self.active = Some(receiver),
|
||||
Err(reason) => {
|
||||
let state = self.settings.get();
|
||||
self.applied = Some(state.revision);
|
||||
self.settings.finish(state.epoch, true);
|
||||
error = Some(format!("Library preview cache worker unavailable: {reason}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
error
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn an_old_cleanup_cannot_finish_a_new_clear_request() {
|
||||
let settings = Settings::new(LibraryCachePrefs::default());
|
||||
settings.update(settings.get().limits, true);
|
||||
let old = settings.get();
|
||||
settings.update(LibraryCachePrefs { disk_mib: 128, max_previews: 8192 }.into(), true);
|
||||
settings.finish(old.epoch, false);
|
||||
assert!(settings.get().clear_pending);
|
||||
let latest = settings.get();
|
||||
settings.finish(latest.epoch, true);
|
||||
assert!(!settings.get().clear_pending, "failed cleanup must allow rendering to resume");
|
||||
assert!(settings.get().disabled, "failed cleanup must bypass old files");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rapid_changes_keep_one_maintenance_worker_and_coalesce_the_latest_request() {
|
||||
let context = egui::Context::default();
|
||||
let mut maintenance = Maintenance::new(LibraryCachePrefs::default());
|
||||
let (sender, receiver) = std::sync::mpsc::sync_channel(1);
|
||||
maintenance.active = Some(receiver);
|
||||
for size in 1..=100 {
|
||||
maintenance.settings.update(LibraryCachePrefs { disk_mib: size, max_previews: 4096 }.into(), true);
|
||||
assert!(maintenance.tick(&context).is_none());
|
||||
assert!(maintenance.applied.is_none());
|
||||
}
|
||||
let latest = maintenance.settings.get();
|
||||
// The original receiver is still installed; no replacement jobs may have been spawned.
|
||||
sender.send(Ok(Some(latest))).unwrap();
|
||||
assert!(maintenance.tick(&context).is_none());
|
||||
assert!(maintenance.active.is_none());
|
||||
assert_eq!(maintenance.applied, Some(latest.revision));
|
||||
assert!(!maintenance.settings.get().clear_pending);
|
||||
}
|
||||
}
|
||||
@@ -10,19 +10,21 @@ use std::time::{Duration, SystemTime};
|
||||
use image::{ImageDecoder, ImageEncoder};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const MAX_ENTRIES: usize = 4096;
|
||||
use super::preview_cache_settings::{Limits, Settings, State};
|
||||
|
||||
const MAX_SCAN_ENTRIES: usize = materialcraft_engine::LibraryCachePrefs::MAX_PREVIEWS as usize * 2;
|
||||
const MAX_FILE_BYTES: u64 = 128 * 1024;
|
||||
const MAX_SIDE: u32 = 128;
|
||||
|
||||
pub(super) struct PreviewDiskCache {
|
||||
root: PathBuf,
|
||||
renderer: [u8; 32],
|
||||
budget: u64,
|
||||
entries: usize,
|
||||
settings: Settings,
|
||||
epoch: u64,
|
||||
}
|
||||
|
||||
impl PreviewDiskCache {
|
||||
pub fn configured() -> Option<Self> {
|
||||
pub fn configured(settings: Settings) -> Option<Self> {
|
||||
// Scripted runs must not touch the user's cache. An explicit override lets tests and
|
||||
// agents exercise persistence in an isolated folder; an empty override disables it.
|
||||
let root = match std::env::var_os("MATERIALCRAFT_CACHE_DIR") {
|
||||
@@ -33,12 +35,12 @@ impl PreviewDiskCache {
|
||||
// including redirected Windows profiles and absolute XDG paths.
|
||||
None => craft_update::Paths::per_user("materialcraft").ok()?.cache,
|
||||
};
|
||||
Self::open(root.join("previews"), 64 * 1024 * 1024)
|
||||
Self::open(root.join("previews"), settings)
|
||||
}
|
||||
|
||||
/// Called on a worker. The executable digest also invalidates development builds that keep
|
||||
/// the same version number. It is streamed once per process, never on the event-loop thread.
|
||||
pub fn open(root: PathBuf, budget: u64) -> Option<Self> {
|
||||
pub fn open(root: PathBuf, settings: Settings) -> Option<Self> {
|
||||
static RENDERER: OnceLock<Option<[u8; 32]>> = OnceLock::new();
|
||||
let renderer = *RENDERER.get_or_init(|| {
|
||||
let mut file = File::open(std::env::current_exe().ok()?).ok()?.take((1 << 30) + 1);
|
||||
@@ -46,7 +48,8 @@ impl PreviewDiskCache {
|
||||
let bytes = io::copy(&mut file, &mut hash).ok()?;
|
||||
(bytes <= 1 << 30).then(|| hash.finalize().into())
|
||||
});
|
||||
Some(Self { root, renderer: renderer?, budget, entries: MAX_ENTRIES })
|
||||
let epoch = settings.get().epoch;
|
||||
Some(Self { root, renderer: renderer?, settings, epoch })
|
||||
}
|
||||
|
||||
pub fn get_or_render(&self, key: [u8; 32], side: u32, cancel: &AtomicBool, render: impl FnOnce() -> Option<Vec<u8>>) -> Option<Vec<u8>> {
|
||||
@@ -78,7 +81,7 @@ impl PreviewDiskCache {
|
||||
}
|
||||
|
||||
fn read(&self, path: &Path, side: u32) -> Option<Vec<u8>> {
|
||||
if side == 0 || side > MAX_SIDE || self.budget == 0 {
|
||||
if side == 0 || side > MAX_SIDE || self.limits().is_none_or(|limits| limits.budget == 0) {
|
||||
return None;
|
||||
}
|
||||
let metadata = fs::symlink_metadata(path).ok()?;
|
||||
@@ -109,12 +112,12 @@ impl PreviewDiskCache {
|
||||
}
|
||||
|
||||
fn write(&self, path: &Path, side: u32, pixels: &[u8]) -> io::Result<()> {
|
||||
if side == 0 || side > MAX_SIDE || pixels.len() != (side * side * 4) as usize || self.budget == 0 {
|
||||
if side == 0 || side > MAX_SIDE || pixels.len() != (side * side * 4) as usize || self.limits().is_none_or(|limits| limits.budget == 0) {
|
||||
return Ok(());
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
image::codecs::png::PngEncoder::new(&mut bytes).write_image(pixels, side, side, image::ExtendedColorType::Rgba8).map_err(io::Error::other)?;
|
||||
if bytes.len() as u64 > MAX_FILE_BYTES || bytes.len() as u64 > self.budget {
|
||||
if bytes.len() as u64 > MAX_FILE_BYTES {
|
||||
return Ok(());
|
||||
}
|
||||
fs::create_dir_all(&self.root)?;
|
||||
@@ -127,16 +130,45 @@ impl PreviewDiskCache {
|
||||
if fs::symlink_metadata(path).is_ok_and(|metadata| !metadata.is_file()) {
|
||||
return Err(io::Error::other("preview cache entry is not a regular file"));
|
||||
}
|
||||
self.reserve(path, bytes.len() as u64)?;
|
||||
let Some(limits) = self.limits() else { return Ok(()) };
|
||||
if bytes.len() as u64 > limits.budget {
|
||||
return Ok(());
|
||||
}
|
||||
self.reserve(Some(path), bytes.len() as u64, limits)?;
|
||||
craft_atomic::WriteOptions::default().with_new_file_permissions(craft_atomic::NewFilePermissions::Private).atomic_write(path, &bytes)
|
||||
}
|
||||
|
||||
fn reserve(&self, target: &Path, incoming: u64) -> io::Result<()> {
|
||||
pub fn enabled(&self) -> bool {
|
||||
self.limits().is_some_and(|limits| limits.budget > 0)
|
||||
}
|
||||
|
||||
fn limits(&self) -> Option<Limits> {
|
||||
let state = self.settings.get();
|
||||
(!state.disabled && !state.clear_pending && state.epoch == self.epoch).then_some(state.limits)
|
||||
}
|
||||
|
||||
/// Returns false only while another process is writing; the coordinator retries later.
|
||||
pub fn maintain(&self, state: State) -> io::Result<bool> {
|
||||
if !self.root.try_exists()? {
|
||||
return Ok(true);
|
||||
}
|
||||
let lock = File::options().read(true).write(true).create(true).truncate(false).open(self.root.join("cache.lock"))?;
|
||||
match lock.try_lock() {
|
||||
Ok(()) => {}
|
||||
Err(std::fs::TryLockError::WouldBlock) => return Ok(false),
|
||||
Err(std::fs::TryLockError::Error(error)) => return Err(error),
|
||||
}
|
||||
let limits = if state.clear_pending || state.limits.budget == 0 { Limits { budget: 0, entries: 0 } } else { state.limits };
|
||||
self.reserve(None, 0, limits)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn reserve(&self, target: Option<&Path>, incoming: u64, limits: Limits) -> io::Result<()> {
|
||||
let mut entries = Vec::new();
|
||||
let mut total = 0u64;
|
||||
// Bound both scan time and metadata allocations in an externally populated directory.
|
||||
for (index, entry) in fs::read_dir(&self.root)?.enumerate() {
|
||||
if index >= MAX_ENTRIES * 2 {
|
||||
if index >= MAX_SCAN_ENTRIES {
|
||||
return Err(io::Error::other("preview cache directory exceeds its entry limit"));
|
||||
}
|
||||
let entry = entry?;
|
||||
@@ -151,7 +183,7 @@ impl PreviewDiskCache {
|
||||
fs::remove_file(entry.path())?;
|
||||
continue;
|
||||
}
|
||||
if !cache_name(&name) || entry.path() == target {
|
||||
if !cache_name(&name) || target.is_some_and(|target| entry.path() == target) {
|
||||
continue;
|
||||
}
|
||||
let metadata = entry.metadata()?;
|
||||
@@ -161,7 +193,7 @@ impl PreviewDiskCache {
|
||||
entries.sort_unstable_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
|
||||
let mut count = entries.len();
|
||||
for (_, path, size) in entries {
|
||||
if total.saturating_add(incoming) <= self.budget && count < self.entries {
|
||||
if total.saturating_add(incoming) <= limits.budget && count.saturating_add(usize::from(target.is_some())) <= limits.entries {
|
||||
break;
|
||||
}
|
||||
fs::remove_file(path)?;
|
||||
|
||||
@@ -6,7 +6,7 @@ const SIDE: u32 = 8;
|
||||
static TESTS: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
fn cache(root: &Path) -> PreviewDiskCache {
|
||||
PreviewDiskCache { root: root.to_owned(), renderer: [1; 32], budget: 64 * 1024 * 1024, entries: MAX_ENTRIES }
|
||||
PreviewDiskCache { root: root.to_owned(), renderer: [1; 32], settings: Settings::new(Default::default()), epoch: 0 }
|
||||
}
|
||||
|
||||
fn pixels() -> Vec<u8> {
|
||||
@@ -48,7 +48,7 @@ fn real_library_previews_survive_relaunch() {
|
||||
if let Some(root) = std::env::var_os(CHILD) {
|
||||
let library = materialcraft_engine::NodeLibrary::shared();
|
||||
let phase = std::env::var("MATERIALCRAFT_TEST_DISK_PHASE").unwrap();
|
||||
let cache = PreviewDiskCache::open(PathBuf::from(root), 64 * 1024 * 1024).unwrap();
|
||||
let cache = PreviewDiskCache::open(PathBuf::from(root), Settings::new(Default::default())).unwrap();
|
||||
let cancel = AtomicBool::new(false);
|
||||
for (package, graph) in
|
||||
[("sbs://blur_hq.sbs", "blur_hq"), ("sbs://noise_clouds_3_v2.sbs", "clouds_3"), ("sbs://materials/pbr/bricks_001.sbs", "bricks_001")]
|
||||
@@ -132,11 +132,11 @@ fn corrupt_oversized_and_wrong_size_files_are_misses_and_repaired() {
|
||||
fn cache_budget_evicts_old_files_and_preserves_unrelated_files() {
|
||||
let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mut cache = cache(root.path());
|
||||
let cache = cache(root.path());
|
||||
let old = cache.path([1; 32], SIDE);
|
||||
cache.write(&old, SIDE, &pixels()).unwrap();
|
||||
let file_bytes = fs::metadata(&old).unwrap().len();
|
||||
cache.budget = file_bytes * 2;
|
||||
cache.settings.update(Limits { budget: file_bytes * 2, entries: 4096 }, false);
|
||||
File::options().write(true).open(&old).unwrap().set_modified(SystemTime::UNIX_EPOCH).unwrap();
|
||||
fs::write(root.path().join("my-image.png"), b"unrelated").unwrap();
|
||||
let leftover = root.path().join(craft_atomic::temp_name(old.file_name().unwrap().to_str().unwrap()));
|
||||
@@ -150,13 +150,12 @@ fn cache_budget_evicts_old_files_and_preserves_unrelated_files() {
|
||||
assert_eq!(fs::read(root.path().join("my-image.png")).unwrap(), b"unrelated");
|
||||
let total: u64 =
|
||||
fs::read_dir(root.path()).unwrap().flatten().filter(|f| cache_name(&f.file_name().to_string_lossy())).map(|f| f.metadata().unwrap().len()).sum();
|
||||
assert!(total <= cache.budget);
|
||||
cache.budget = 64 * 1024 * 1024;
|
||||
cache.entries = 1;
|
||||
assert!(total <= cache.settings.get().limits.budget);
|
||||
cache.settings.update(Limits { budget: 64 * 1024 * 1024, entries: 1 }, false);
|
||||
cache.write(&cache.path([4; 32], SIDE), SIDE, &pixels()).unwrap();
|
||||
let count = fs::read_dir(root.path()).unwrap().flatten().filter(|file| cache_name(&file.file_name().to_string_lossy())).count();
|
||||
assert_eq!(count, 1, "the entry limit also applies when there is space left");
|
||||
cache.budget = file_bytes - 1;
|
||||
cache.settings.update(Limits { budget: file_bytes - 1, entries: 1 }, false);
|
||||
let oversized = cache.path([5; 32], SIDE);
|
||||
cache.write(&oversized, SIDE, &pixels()).unwrap();
|
||||
assert!(!oversized.exists());
|
||||
@@ -190,3 +189,63 @@ fn cache_does_not_read_or_evict_symlink_targets() {
|
||||
let _ = cache.write(&path, SIDE, &pixels());
|
||||
assert_eq!(fs::read(outside.path()).unwrap(), b"keep");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changed_limits_trim_a_warm_cache_without_another_render() {
|
||||
let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let cache = cache(root.path());
|
||||
for key in 1..=3 {
|
||||
cache.write(&cache.path([key; 32], SIDE), SIDE, &pixels()).unwrap();
|
||||
}
|
||||
let count = || fs::read_dir(root.path()).unwrap().flatten().filter(|f| cache_name(&f.file_name().to_string_lossy())).count();
|
||||
cache.settings.update(Limits { budget: 64 * 1024 * 1024, entries: 2 }, false);
|
||||
assert!(cache.maintain(cache.settings.get()).unwrap());
|
||||
assert_eq!(count(), 2);
|
||||
let file_bytes = fs::read_dir(root.path()).unwrap().flatten().find(|f| cache_name(&f.file_name().to_string_lossy())).unwrap().metadata().unwrap().len();
|
||||
cache.settings.update(Limits { budget: file_bytes, entries: 4096 }, false);
|
||||
assert!(cache.maintain(cache.settings.get()).unwrap());
|
||||
assert_eq!(count(), 1);
|
||||
cache.settings.update(Limits { budget: 0, entries: 4096 }, false);
|
||||
assert!(cache.maintain(cache.settings.get()).unwrap());
|
||||
assert_eq!(count(), 0);
|
||||
assert_eq!(cache.get_or_render([9; 32], SIDE, &AtomicBool::new(false), || Some(pixels())), Some(pixels()));
|
||||
assert_eq!(count(), 0, "disabled persistence must still render without writing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_removes_previews_and_prevents_old_workers_from_repopulating_them() {
|
||||
let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let cache = cache(root.path());
|
||||
let path = cache.path([1; 32], SIDE);
|
||||
cache.write(&path, SIDE, &pixels()).unwrap();
|
||||
fs::write(root.path().join("keep.txt"), b"keep").unwrap();
|
||||
cache.settings.update(cache.settings.get().limits, true);
|
||||
let state = cache.settings.get();
|
||||
assert!(cache.read(&path, SIDE).is_none(), "pending clear must bypass stale files");
|
||||
assert!(cache.maintain(state).unwrap());
|
||||
cache.settings.finish(state.epoch, false);
|
||||
assert!(!path.exists());
|
||||
assert_eq!(fs::read(root.path().join("keep.txt")).unwrap(), b"keep");
|
||||
cache.write(&path, SIDE, &pixels()).unwrap();
|
||||
assert!(!path.exists(), "a worker started before Clear cannot refill the cache");
|
||||
let fresh = PreviewDiskCache { epoch: state.epoch, ..cache };
|
||||
fresh.write(&path, SIDE, &pixels()).unwrap();
|
||||
assert!(path.exists(), "newly visible previews may persist again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn running_preview_observes_limits_changed_during_its_render() {
|
||||
let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let cache = cache(root.path());
|
||||
assert_eq!(
|
||||
cache.get_or_render([1; 32], SIDE, &AtomicBool::new(false), || {
|
||||
cache.settings.update(Limits { budget: 0, entries: 4096 }, false);
|
||||
Some(pixels())
|
||||
}),
|
||||
Some(pixels())
|
||||
);
|
||||
assert!(!cache.path([1; 32], SIDE).exists());
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::theme::{Accent, Brightness};
|
||||
use materialcraft_engine::{CameraPrefs, HistoryPrefs};
|
||||
use materialcraft_engine::{CameraPrefs, HistoryPrefs, LibraryCachePrefs};
|
||||
|
||||
/// Preferences (Edit ▸ Preferences)
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
@@ -17,6 +17,7 @@ pub struct Prefs {
|
||||
/// Minutes between autosaves of a modified package (0 = off)
|
||||
pub autosave_minutes: u32,
|
||||
pub history: HistoryPrefs,
|
||||
pub library_cache: LibraryCachePrefs,
|
||||
/// Show node thumbnails in the graph
|
||||
pub thumbnails: bool,
|
||||
pub snap_to_grid: bool,
|
||||
@@ -38,6 +39,7 @@ impl Default for Prefs {
|
||||
preview_size: 512,
|
||||
autosave_minutes: 5,
|
||||
history: HistoryPrefs::default(),
|
||||
library_cache: LibraryCachePrefs::default(),
|
||||
thumbnails: true,
|
||||
snap_to_grid: true,
|
||||
library_list: false,
|
||||
@@ -59,10 +61,17 @@ mod tests {
|
||||
assert_eq!(old.ui_scale, 1.25);
|
||||
assert!(old.snap_to_grid);
|
||||
assert_eq!(old.history, HistoryPrefs::default());
|
||||
let p = Prefs { camera: CameraPrefs { pan_limit: 8.0, pan_speed: 0.5 }, history: HistoryPrefs { undo_limit: 500 }, ..Prefs::default() };
|
||||
assert_eq!(old.library_cache, LibraryCachePrefs::default());
|
||||
let p = Prefs {
|
||||
camera: CameraPrefs { pan_limit: 8.0, pan_speed: 0.5 },
|
||||
history: HistoryPrefs { undo_limit: 500 },
|
||||
library_cache: LibraryCachePrefs { disk_mib: 512, max_previews: 12000 },
|
||||
..Prefs::default()
|
||||
};
|
||||
let v = serde_json::to_value(&p).unwrap();
|
||||
assert_eq!(v["camera"], serde_json::json!({"panLimit": 8.0, "panSpeed": 0.5}));
|
||||
assert_eq!(v["history"]["undoLimit"], 500);
|
||||
assert_eq!(v["libraryCache"], serde_json::json!({"diskMib": 512, "maxPreviews": 12000}));
|
||||
assert_eq!(serde_json::from_value::<Prefs>(v).unwrap(), p);
|
||||
}
|
||||
}
|
||||
+56
-103
@@ -1,11 +1,10 @@
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::sync::mpsc::Receiver;
|
||||
|
||||
use egui::{ColorImage, TextureHandle, TextureOptions};
|
||||
use materialcraft_engine::image::{Image, Kind};
|
||||
use materialcraft_engine::nodes::{CookContext, catalog, resolve_params};
|
||||
|
||||
/// Rendered previews by id: (id, RGBA8 pixels)
|
||||
/// Rendered previews by id: (id, RGBA8 pixels).
|
||||
pub type Previews = Vec<(String, Vec<u8>)>;
|
||||
|
||||
/// Side of node thumbnails and library previews, in pixels
|
||||
@@ -16,10 +15,9 @@ const LIBRARY_PREVIEW_SIZE: u32 = 72;
|
||||
pub struct Thumbs {
|
||||
/// (cook key, port) → texture and the frame it was last used
|
||||
nodes: HashMap<(u64, usize), (TextureHandle, u64)>,
|
||||
library: HashMap<String, TextureHandle>,
|
||||
library_rx: Option<Receiver<Previews>>,
|
||||
library_started: bool,
|
||||
persist_library: bool,
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
persistence: Option<preview_cache_settings::Maintenance>,
|
||||
clear_epoch: u64,
|
||||
graphs: GraphPreviews,
|
||||
frame: u64,
|
||||
}
|
||||
@@ -34,15 +32,31 @@ pub fn color_image(img: &Image, side: u32) -> ColorImage {
|
||||
mod graph_previews;
|
||||
use graph_previews::GraphPreviews;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[path = "preview_cache_settings.rs"]
|
||||
mod preview_cache_settings;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[path = "preview_disk_cache.rs"]
|
||||
mod preview_disk_cache;
|
||||
|
||||
impl Thumbs {
|
||||
/// Opt in only for a native app window; offscreen harnesses never use the user's cache.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn enable_persistence(&mut self) {
|
||||
self.persist_library = true;
|
||||
self.graphs.persist = true;
|
||||
pub fn enable_persistence(&mut self, prefs: materialcraft_engine::LibraryCachePrefs) {
|
||||
let persistence = preview_cache_settings::Maintenance::new(prefs);
|
||||
self.graphs.persistence = Some(persistence.settings.clone());
|
||||
self.persistence = Some(persistence);
|
||||
}
|
||||
|
||||
pub fn configure_cache(&mut self, _prefs: materialcraft_engine::LibraryCachePrefs, clear_epoch: u64) {
|
||||
let clear = clear_epoch != self.clear_epoch;
|
||||
self.clear_epoch = clear_epoch;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Some(persistence) = &self.persistence {
|
||||
persistence.settings.update(_prefs.into(), clear);
|
||||
}
|
||||
if clear {
|
||||
self.graphs.clear();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_nodes(&mut self) {
|
||||
@@ -63,22 +77,18 @@ impl Thumbs {
|
||||
}
|
||||
|
||||
/// Once per frame: forget thumbnails unused for a few seconds; collect library previews
|
||||
pub fn tick(&mut self, ctx: &egui::Context, library: &std::sync::Arc<materialcraft_engine::NodeLibrary>, foreground_busy: bool) {
|
||||
pub fn tick(&mut self, ctx: &egui::Context, library: &std::sync::Arc<materialcraft_engine::NodeLibrary>, foreground_busy: bool) -> Option<String> {
|
||||
self.frame += 1;
|
||||
let f = self.frame;
|
||||
if self.nodes.len() > 256 {
|
||||
self.nodes.retain(|_, (_, used)| f - *used < 120);
|
||||
}
|
||||
if let Some(rx) = &self.library_rx
|
||||
&& let Ok(list) = rx.try_recv()
|
||||
{
|
||||
for (id, rgba) in list {
|
||||
let img = ColorImage::from_rgba_unmultiplied([LIBRARY_PREVIEW_SIZE as usize, LIBRARY_PREVIEW_SIZE as usize], &rgba);
|
||||
self.library.insert(id.clone(), ctx.load_texture(format!("library-{id}"), img, TextureOptions::LINEAR));
|
||||
}
|
||||
self.library_rx = None;
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let error = self.persistence.as_mut().and_then(|persistence| persistence.tick(ctx));
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let error = None;
|
||||
self.graphs.collect(ctx, library, foreground_busy);
|
||||
error
|
||||
}
|
||||
|
||||
/// The preview of library graph `graph` of `package`, cooked in the background on first use;
|
||||
@@ -95,11 +105,7 @@ impl Thumbs {
|
||||
|
||||
/// The library preview of node `def` (generated in the background on first use)
|
||||
pub fn library(&mut self, ctx: &egui::Context, def: &str) -> Option<egui::TextureId> {
|
||||
if !self.library_started {
|
||||
self.library_started = true;
|
||||
self.library_rx = Some(spawn_library_previews(ctx.clone(), self.persist_library));
|
||||
}
|
||||
self.library.get(def).map(TextureHandle::id)
|
||||
self.graphs.native(ctx, def)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,87 +119,34 @@ fn sample_inputs() -> (Image, Image, Image) {
|
||||
(gray, color, normal)
|
||||
}
|
||||
|
||||
fn previews(_persist: bool) -> Previews {
|
||||
fn render_native(id: &str) -> Option<Vec<u8>> {
|
||||
let def = catalog().iter().find(|def| def.id == id && def.id != "graph_instance")?;
|
||||
let (gray, color, normal) = sample_inputs();
|
||||
let no_res = |_: &str| None;
|
||||
let vars = BTreeMap::new();
|
||||
let mut out = Vec::new();
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let disk = _persist.then(preview_disk_cache::PreviewDiskCache::configured).flatten();
|
||||
for def in catalog() {
|
||||
if def.id == "graph_instance" {
|
||||
continue;
|
||||
}
|
||||
let render = || {
|
||||
let inputs: Vec<Option<Image>> = def
|
||||
.inputs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, p)| {
|
||||
// Masks and secondary inputs stay empty so the main effect shows
|
||||
if i > 0 && !matches!(def.id, "blend" | "height_blend" | "normal_blend" | "rgba_merge") {
|
||||
return None;
|
||||
}
|
||||
Some(if p.id.contains("normal") || def.category.contains("Normal") && p.kind == materialcraft_engine::nodes::PortKind::Color {
|
||||
normal.clone()
|
||||
} else if p.kind == materialcraft_engine::nodes::PortKind::Color {
|
||||
color.clone()
|
||||
} else if i == 1 {
|
||||
gray.map(Kind::Gray, |v| [1.0 - v[0], 0.0, 0.0, 0.0])
|
||||
} else {
|
||||
gray.clone()
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let params = resolve_params(def, &BTreeMap::new());
|
||||
let ctx = CookContext {
|
||||
w: LIBRARY_PREVIEW_SIZE,
|
||||
h: LIBRARY_PREVIEW_SIZE,
|
||||
seed: 3,
|
||||
tile: true,
|
||||
inputs: &inputs,
|
||||
params: ¶ms,
|
||||
resource: &no_res,
|
||||
vars: &vars,
|
||||
};
|
||||
let imgs = (def.cook)(&ctx).ok()?;
|
||||
Some(imgs.first()?.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display())
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let rgba = {
|
||||
use sha2::Digest;
|
||||
match &disk {
|
||||
Some(disk) => disk.get_or_render(
|
||||
sha2::Sha256::digest(format!("materialcraft-atomic-preview-v1:{}", def.id)).into(),
|
||||
LIBRARY_PREVIEW_SIZE,
|
||||
&std::sync::atomic::AtomicBool::new(false),
|
||||
render,
|
||||
),
|
||||
None => render(),
|
||||
let inputs: Vec<Option<Image>> = def
|
||||
.inputs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, p)| {
|
||||
// Masks and secondary inputs stay empty so the main effect shows.
|
||||
if i > 0 && !matches!(def.id, "blend" | "height_blend" | "normal_blend" | "rgba_merge") {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let rgba = render();
|
||||
if let Some(rgba) = rgba {
|
||||
out.push((def.id.to_string(), rgba));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn spawn_library_previews(ctx: egui::Context, persist: bool) -> Receiver<Previews> {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let _ = std::thread::Builder::new().name("materialcraft-library".into()).spawn(move || {
|
||||
let _ = tx.send(previews(persist));
|
||||
ctx.request_repaint();
|
||||
});
|
||||
rx
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn spawn_library_previews(_ctx: egui::Context, persist: bool) -> Receiver<Previews> {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let _ = tx.send(previews(persist));
|
||||
rx
|
||||
Some(if p.id.contains("normal") || def.category.contains("Normal") && p.kind == materialcraft_engine::nodes::PortKind::Color {
|
||||
normal.clone()
|
||||
} else if p.kind == materialcraft_engine::nodes::PortKind::Color {
|
||||
color.clone()
|
||||
} else if i == 1 {
|
||||
gray.map(Kind::Gray, |v| [1.0 - v[0], 0.0, 0.0, 0.0])
|
||||
} else {
|
||||
gray.clone()
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let params = resolve_params(def, &BTreeMap::new());
|
||||
let ctx =
|
||||
CookContext { w: LIBRARY_PREVIEW_SIZE, h: LIBRARY_PREVIEW_SIZE, seed: 3, tile: true, inputs: &inputs, params: ¶ms, resource: &no_res, vars: &vars };
|
||||
let imgs = (def.cook)(&ctx).ok()?;
|
||||
Some(imgs.first()?.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display())
|
||||
}
|
||||
@@ -76,7 +76,9 @@ thread_local! {
|
||||
}
|
||||
|
||||
pub fn show(app: &mut MaterialCraftApp, ctx: &egui::Context) {
|
||||
app.thumbs.tick(ctx, &app.session.library, app.cooker.busy() || app.session.dirty);
|
||||
if let Some(error) = app.thumbs.tick(ctx, &app.session.library, app.cooker.busy() || app.session.dirty) {
|
||||
app.toast(error, true);
|
||||
}
|
||||
if app.ui.windows.new_graph {
|
||||
new_graph(app, ctx);
|
||||
}
|
||||
@@ -174,7 +176,8 @@ fn new_graph(app: &mut MaterialCraftApp, ctx: &egui::Context) {
|
||||
|
||||
fn preferences(app: &mut MaterialCraftApp, ctx: &egui::Context) {
|
||||
let mut open = true;
|
||||
modal(ctx, "prefs", tr("Preferences"), &mut open, vec2(380.0, 340.0), |ui| {
|
||||
let mut clear_cache = false;
|
||||
modal(ctx, "prefs", tr("Preferences"), &mut open, vec2(400.0, 500.0), |ui| {
|
||||
let p = &mut app.ui.prefs;
|
||||
let current_lang = crate::i18n::current();
|
||||
crate::widgets::combo(ui, tr("Language"), current_lang.name, |ui| {
|
||||
@@ -208,9 +211,30 @@ fn preferences(app: &mut MaterialCraftApp, ctx: &egui::Context) {
|
||||
crate::preview_resolution::choices(ui, &mut p.preview_size);
|
||||
});
|
||||
ui.checkbox(&mut p.thumbnails, tr("Show node thumbnails"));
|
||||
ui.separator();
|
||||
ui.label(tr("Library preview cache"));
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use materialcraft_engine::LibraryCachePrefs;
|
||||
ui.horizontal(|ui| {
|
||||
let label = ui.label(tr("Disk limit (MiB, 0 = off)"));
|
||||
ui.add(egui::DragValue::new(&mut p.library_cache.disk_mib).range(0..=LibraryCachePrefs::MAX_DISK_MIB)).labelled_by(label.id);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
let label = ui.label(tr("Maximum stored previews"));
|
||||
ui.add(egui::DragValue::new(&mut p.library_cache.max_previews).range(1..=LibraryCachePrefs::MAX_PREVIEWS)).labelled_by(label.id);
|
||||
});
|
||||
}
|
||||
clear_cache = ui.button(tr("Clear cache")).clicked();
|
||||
ui.label(tr("Previews rebuild as they come into view."));
|
||||
ui.separator();
|
||||
crate::widgets::slider(ui, tr("3D pan limit (mesh radii)"), &mut p.camera.pan_limit, 0.0..=20.0, 1);
|
||||
crate::widgets::slider(ui, tr("3D pan speed"), &mut p.camera.pan_speed, 0.1..=4.0, 2);
|
||||
});
|
||||
if clear_cache {
|
||||
app.run("library.cache.clear", serde_json::json!({}));
|
||||
app.apply_prefs();
|
||||
}
|
||||
if !open {
|
||||
app.ui.windows.preferences = false;
|
||||
}
|
||||
|
||||
@@ -1584,3 +1584,22 @@ fn explorer_inputs_and_graph_drop_share_labels_and_material_assignment() {
|
||||
assert!(frame.chunks_exact(4).filter(|pixel| u16::from(pixel[0]) > u16::from(pixel[1]) + 30).count() > 100);
|
||||
save(&mut harness, &directory, "graph-dropped-on-material");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires an offscreen GPU"]
|
||||
fn library_cache_preferences() {
|
||||
let app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default());
|
||||
let mut h = harness(app, 1100.0, 850.0);
|
||||
settle(&mut h, 4);
|
||||
h.state_mut().ui.windows.preferences = true;
|
||||
settle(&mut h, 4);
|
||||
assert!(h.query_all(By::new().label("Disk limit (MiB, 0 = off)")).next().is_some());
|
||||
assert!(h.query_all(By::new().label("Maximum stored previews")).next().is_some());
|
||||
h.get_by_label("Clear cache").click();
|
||||
settle(&mut h, 4);
|
||||
assert_eq!(h.state().session.library_cache_clear_epoch, 1);
|
||||
if let Some(dir) = std::env::var_os("MC_SNAPSHOT_DIR").map(std::path::PathBuf::from) {
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
save(&mut h, &dir, "library-cache-preferences");
|
||||
}
|
||||
}
|
||||
+17
-6
@@ -131,7 +131,7 @@ as missing until a package in the folder provides it. Installed and browser buil
|
||||
packages and shared resources, compressed at build time. Library packages share a flat dependency
|
||||
registry; only sources reachable from open packages refresh their instance interfaces on edits.
|
||||
|
||||
Library thumbnails cook one visible graph at a time, with 32 MiB of output caching and 16 MiB
|
||||
Library thumbnails cook one visible native node or graph at a time, with 32 MiB of output caching and 16 MiB
|
||||
of decoded-image caching. Only output dependencies are evaluated. Editing takes priority, and
|
||||
leaving a tile or reloading the library cancels its cook between nodes. The next job waits for
|
||||
that worker to finish; hidden tiles cannot accumulate queued work or workers. At most 256
|
||||
@@ -141,17 +141,28 @@ Native Library previews also persist as PNGs in the platform cache folder select
|
||||
the existing `craft_update::Paths::per_user("materialcraft").cache`, under `previews/`: `%LOCALAPPDATA%` on
|
||||
Windows, `$XDG_CACHE_HOME` or `~/.cache` on Linux/BSD, and `~/Library/Caches` on macOS.
|
||||
This follows the [Craft storage policy](https://github.com/storytold/craft-libs/blob/main/docs/settings-storage.md).
|
||||
The cache covers native nodes and SBS graphs, retains at most 64 MiB / 4096 PNGs across builds,
|
||||
and evicts older files when admitting new previews (access times refresh at most hourly). It stores disposable
|
||||
thumbnails, independent of projects, autosaves and temporary render storage.
|
||||
The cache covers native nodes and SBS graphs. Edit → Preferences → Library preview cache stores
|
||||
the disk limit (default 64 MiB, 0 disables persistence) and maximum stored previews (default 4096).
|
||||
The supported ranges are 0–65536 MiB and 1–65536 previews. Settings apply immediately to running
|
||||
workers, with background eviction when lowering either limit, including when all previews are warm.
|
||||
Older files are evicted first (access times refresh at most hourly). These disposable thumbnails are
|
||||
independent of projects, autosaves and temporary render storage.
|
||||
|
||||
Preview workers do all reads, encoding, atomic writes (`craft-atomic`) and eviction; none run
|
||||
**Clear cache** removes stored previews and loaded library textures, cancels the active preview,
|
||||
and discards its result. Previews rebuild passively, one visible tile at a time; hidden native
|
||||
nodes and graphs do not cook. A job from before Clear cannot write its old result back. One
|
||||
maintenance worker handles trimming and clearing without blocking the UI. Cleanup failures
|
||||
report an error and bypass disk persistence for the session so stale previews cannot reappear.
|
||||
The same actions are available through `library.cache.configure` and `library.cache.clear`;
|
||||
`session.inspect` exposes `libraryCachePreferences`. These commands never edit package contents.
|
||||
|
||||
Background workers do all reads, encoding, atomic writes (`craft-atomic`) and eviction; none run
|
||||
on the UI thread. Keys include the preview size, an executable content digest (including local
|
||||
rebuilds with an unchanged release version), and, for SBS graphs, the source packages and all
|
||||
resource bytes in their dependency closure. Changes become visible after `library.reload` or
|
||||
the next launch, just like the in-memory library. Corrupt, oversized or unavailable cache files
|
||||
are misses; failed or cancelled renders are never persisted. Concurrent writers use a
|
||||
nonblocking file lock to share the disk budget. The browser retains its existing behavior.
|
||||
nonblocking file lock to share the disk budget. The browser keeps previews in memory only.
|
||||
|
||||
`MATERIALCRAFT_CACHE_DIR` overrides the app cache folder (`previews/` is appended); an empty
|
||||
value disables persistence. Scripted `MATERIALCRAFT_NO_PREFS` runs leave the user's cache
|
||||
|
||||
Reference in new issue
Block a user