diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c0e54a..e883fd0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,9 +22,8 @@ jobs: uses: storytold/craft-common-actions/.github/workflows/gate.yml@main with: # Same craft-fonts commit as release.yml. - craft-fonts-ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 + craft-fonts-ref: c1db6d1a8ed772191241c3d185e2104be84d8691 os: '["ubuntu-24.04","windows-latest"]' - toolchain: "1.96.0" # xtask ci runs cargo test (including doctests), not cargo nextest. tools: "" packages: >- diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml index 6d0ae2b..1b8991d 100644 --- a/.github/workflows/freebsd.yml +++ b/.github/workflows/freebsd.yml @@ -24,5 +24,5 @@ jobs: # Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos. uses: storytold/craft-common-actions/.github/workflows/freebsd.yml@main with: - craft-fonts-ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 + craft-fonts-ref: c1db6d1a8ed772191241c3d185e2104be84d8691 run-package-check: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dfb827d..4c3803b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -33,7 +33,7 @@ jobs: dry-run: ${{ github.event_name == 'schedule' }} app-name: materialcraft app-title: "MaterialCraft" - craft-fonts-ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 + craft-fonts-ref: c1db6d1a8ed772191241c3d185e2104be84d8691 web-craft-fonts: true version: ${{ inputs.version }} secrets: diff --git a/CODE_STYLE.md b/CODE_STYLE.md new file mode 100644 index 0000000..86dcc05 --- /dev/null +++ b/CODE_STYLE.md @@ -0,0 +1,77 @@ +# Code style + +Style conventions for MaterialCraft's Rust code. +Read alongside [AGENTS.md](AGENTS.md) for repository requirements. + +## Naming and Rust conventions + +- Use descriptive names that explain purpose; include units where useful. Avoid misleading names + and abbreviations whose meaning is unclear in context. +- Follow Rust naming conventions: `snake_case` for functions, variables, fields, and modules; + `UpperCamelCase` for types and traits; `SCREAMING_SNAKE_CASE` for constants and statics. +- Conventional Rust names are not cryptic abbreviations. Keep names such as `fmt`, `iter`, `len`, + `args`, `err`, `id`, and `ui` when their meaning is clear. Do not expand them just to make them + longer. +- Keep standard file and directory names such as `lib.rs`, `main.rs`, `mod.rs`, `src`, `tests`, + `examples`, and `benches`. Rust keywords such as `mut`, `ref`, `impl`, and `mod` are language + syntax, not naming problems. +- Short names are appropriate in a clear local context: `i` for an index, `x` and `y` for + coordinates, `w` and `h` for image dimensions, and conventional generic parameters such as `T`. + Use more descriptive names when scope or ambiguity makes those hard to follow. + +## Magic numbers + +- Give non-obvious limits, thresholds, sizes, and tuning values descriptive named constants + instead of leaving unexplained numeric literals in the code. +- Name constants for their purpose, with units where useful, rather than for their literal value. + Keep them in the narrowest useful scope. +- Reuse an existing constant when it represents the same concept. Equal numeric values alone do + not mean two concepts should share a constant. +- Explain the origin or rationale of non-obvious coefficients and tolerances in a short comment. + Use standard library constants for standard mathematical values. +- Keep literals inline when their role is clear, such as zero initialization, incrementing by one, + or dividing by two to find a midpoint. Do not introduce names such as `ZERO` or `TWO` that add + no meaning. +- Apply this guidance to tests too; clear fixture values and expected results can remain literal. + +## Formatting and spacing + +- Let `cargo fmt` and the repository's [rustfmt.toml](rustfmt.toml) handle mechanical formatting. + Do not introduce a competing indentation or line-wrapping convention. +- Use one blank line between distinct logical steps: input preparation, validation, computation, + mutation, and result construction. +- Keep closely related statements together. Do not add a blank line after every declaration or + fragment a single operation with extra spacing. +- Put a blank line after a multiline early-exit guard, including `if` and `let ... else`, before + the next statement. +- Put a blank line before a comment introducing a new step, with no blank line between the + comment and the code it explains. +- Keep documentation and attributes attached to their declarations. +- Separate function definitions with one blank line. Avoid extra empty lines just inside braces + or between every field, argument, or statement. +- In tests, separate preparation, action, and verification with blank lines when those phases + are distinct; keep related assertions together. + +## Source organization + +- Give each struct or enum its own source file, with its implementation beside it. Use the full + type name in `snake_case`, such as `mesh_curvature_settings.rs` for `MeshCurvatureSettings`. +- Preserve conventional crate roots and module entry points such as `lib.rs`, `main.rs`, and + `mod.rs`. Follow the command and node module placement required by `AGENTS.md`. +- Prefer singular folder names for new domain modules, such as `map`, `setting`, or `tracing`. + Keep standard Rust directories and repository-mandated paths unchanged. +- Group related types into focused modules rather than collecting unrelated types in one large file. +- Apply these preferences to new types and source organization changes within scope. Do not + reorganize unrelated existing code to enforce them. + +## Comments + +- Explain intent or a non-obvious constraint rather than narrating the code. +- Use short `//` comments. Avoid block comments and wrapped prose in ordinary implementation + comments; adjacent comment lines should each express a concise point. +- Do not add trailing punctuation to ordinary code comments. +- Preserve explanations of guards and invariants. Remove stale or redundant comments without + losing necessary rationale. +- API documentation, required license headers, and examples may use conventional multiline + formatting and punctuation. +- Apply the same comment guidance to production code and tests. diff --git a/Cargo.toml b/Cargo.toml index e235b72..2375d8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -141,6 +141,13 @@ opt-level = 1 lto = "thin" codegen-units = 1 +# Fast optimized verification: each test executable otherwise repeats cross-crate LTO +# `false` keeps thin LTO within each crate; release packaging keeps the profile above +[profile.verify] +inherits = "release" +lto = false +codegen-units = 16 + [profile.wasm-release] inherits = "release" opt-level = "s" diff --git a/crates/automation/src/security.rs b/crates/automation/src/security.rs index 33040fe..8a1c453 100644 --- a/crates/automation/src/security.rs +++ b/crates/automation/src/security.rs @@ -68,6 +68,7 @@ pub fn server_token(port: u16) -> Result<(String, Option), AutomationEr validate_token(t.trim())?; return Ok((t.trim().to_string(), None)); } + let token = generate_token()?; let path = token_file(port); write_token_file(&path, &token).map_err(|e| { @@ -111,12 +112,15 @@ fn read_token_file(path: &Path) -> std::io::Result { if !meta.is_file() { return refuse("not a regular file"); } + if meta.uid() != rustix::process::geteuid().as_raw() { return refuse("owned by another user"); } + if meta.mode() & 0o077 != 0 { return refuse("readable or writable by other users"); } + file }; #[cfg(not(unix))] @@ -146,12 +150,14 @@ pub fn read_bounded_line(r: &mut impl BufRead, line: &mut String) -> std::io::Re if n == 0 { return Ok(LineRead::Eof); } + if n > MAX_REQUEST_BYTES { if bytes.last() != Some(&b'\n') { discard_rest_of_line(r)?; } return Ok(LineRead::TooLong); } + *line = String::from_utf8(bytes).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; Ok(LineRead::Line) } @@ -162,10 +168,12 @@ fn discard_rest_of_line(r: &mut impl BufRead) -> std::io::Result<()> { if buf.is_empty() { return Ok(()); } + if let Some(end) = buf.iter().position(|&b| b == b'\n') { r.consume(end + 1); return Ok(()); } + let n = buf.len(); r.consume(n); } @@ -179,7 +187,7 @@ pub struct Permit(Arc); impl ConnectionLimiter { pub fn try_acquire(&self, max: usize) -> Option { - self.0.fetch_update(Ordering::AcqRel, Ordering::Acquire, |n| (n < max).then_some(n + 1)).ok().map(|_| Permit(self.0.clone())) + self.0.try_update(Ordering::AcqRel, Ordering::Acquire, |n| (n < max).then_some(n + 1)).ok().map(|_| Permit(self.0.clone())) } } @@ -201,7 +209,7 @@ impl AuthDeadline { let stream = stream.try_clone()?; let (tx, rx) = channel::<()>(); std::thread::Builder::new().name("materialcraft-auth-deadline".into()).spawn(move || { - // Dropping the deadline disconnects the channel and ends this thread at once. + // Dropping the deadline disconnects the channel and ends this thread at once if let Err(RecvTimeoutError::Timeout) = rx.recv_timeout(timeout) { let _ = stream.shutdown(Shutdown::Both); } @@ -274,24 +282,31 @@ mod tests { fn unsafe_token_files_are_refused() { use std::os::unix::fs::PermissionsExt; let dir = scratch("refuse"); - // Readable by others: another user may have planted or read it. + + // Readable by others: another user may have planted or read it let open = dir.join("open.token"); std::fs::write(&open, "planted-token-0123456789").unwrap(); std::fs::set_permissions(&open, std::fs::Permissions::from_mode(0o644)).unwrap(); + assert!(read_token_file(&open).is_err()); - // A symlink, even to an owner-only file: a client must not send that file as its token. + + // A symlink, even to an owner-only file: a client must not send that file as its token let secret = dir.join("secret"); write_token_file(&secret, "private-0123456789abcdef").unwrap(); let link = dir.join("link.token"); std::os::unix::fs::symlink(&secret, &link).unwrap(); + assert!(read_token_file(&link).is_err()); - // A FIFO is refused without blocking on open or read. + + // A FIFO is refused without blocking on open or read let fifo = dir.join("fifo.token"); if std::process::Command::new("mkfifo").arg(&fifo).status().is_ok_and(|s| s.success()) { assert!(read_token_file(&fifo).is_err()); } - // A directory is not a token. + + // A directory is not a token assert!(read_token_file(&dir).is_err()); + let _ = std::fs::remove_dir_all(dir); } diff --git a/crates/engine/examples/sbs_library_audit.rs b/crates/engine/examples/sbs_library_audit.rs index 9d8c992..187a5b8 100644 --- a/crates/engine/examples/sbs_library_audit.rs +++ b/crates/engine/examples/sbs_library_audit.rs @@ -19,6 +19,7 @@ fn main() -> Result<(), Box> { missing_resources.push(format!("{path}#{}", resource.sbs_path())); } } + for graph in &package.graphs { graphs += 1; if let Some(function) = &graph.function { @@ -31,6 +32,7 @@ fn main() -> Result<(), Box> { } } } + let result = serde_json::json!({ "files": files.len(), "loaded_packages": library.packages.len(), "graphs": graphs, "standalone_functions": functions, "resources": resources, "resource_declarations_without_bytes": missing_resources, diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index 64f711a..97ebb72 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -103,6 +103,7 @@ pub fn execute(s: &mut Session, id: &str, params: &J) -> Result { if let Some(result) = crate::selection_commands::execute(s, id, params) { return result; } + (spec.enabled)(s).map_err(|why| EngineError::Disabled { cmd: id.to_string(), why })?; let result = (spec.run)(s, params)?; if !s.selection.is_empty() || s.annotation_selection.is_some_and(|selection| !s.annotation_target.items(s).is_ok_and(|items| selection.exists(&items))) { @@ -179,6 +180,7 @@ pub(crate) fn ids_param(s: &Session, cmd: &str, p: &J) -> Result> { for id in &ids { g.node(*id).map_err(|_| bad(cmd, format!("no node {}", id.0)))?; } + Ok(ids) } diff --git a/crates/engine/src/decoded_images.rs b/crates/engine/src/decoded_images.rs index 1989f6f..1aa16f8 100644 --- a/crates/engine/src/decoded_images.rs +++ b/crates/engine/src/decoded_images.rs @@ -2,6 +2,9 @@ use std::collections::{HashMap, HashSet}; use materialcraft_image::Image; +const DEFAULT_LIMIT_BYTES: usize = 128 << 20; +const MAX_ENTRIES: usize = 128; + /// Decoded bitmaps are expendable: resources and active cooks own their source and output data. pub(crate) struct DecodedImages { images: HashMap<(u64, bool), (Image, u64)>, @@ -12,7 +15,7 @@ pub(crate) struct DecodedImages { impl Default for DecodedImages { fn default() -> Self { - Self::new(128 << 20) + Self::new(DEFAULT_LIMIT_BYTES) } } @@ -33,17 +36,21 @@ impl DecodedImages { if let Some((old, _)) = self.images.remove(&key) { self.bytes = self.bytes.saturating_sub(bytes(&old)); } + let size = bytes(&image); - // A large decode can serve its current cook without occupying the persistent cache. + + // A large decode can serve its current cook without occupying the persistent cache if size == 0 || size > self.limit { return; } - while self.bytes > self.limit - size || self.images.len() >= 128 { + + while self.bytes > self.limit - size || self.images.len() >= MAX_ENTRIES { let Some(key) = self.images.iter().min_by_key(|(_, (_, used))| *used).map(|(key, _)| *key) else { break }; if let Some((old, _)) = self.images.remove(&key) { self.bytes = self.bytes.saturating_sub(bytes(&old)); } } + self.tick = self.tick.saturating_add(1); self.images.insert(key, (image, self.tick)); self.bytes += size; @@ -78,12 +85,16 @@ mod tests { cache.insert((0, false), image); for key in 1..1_000 { cache.insert((key, key % 2 == 0), Image::filled(4, 4, Kind::Gray, [0.5; 4])); + assert!(cache.bytes <= 128); assert!(cache.images.len() <= 2); } + assert!(weak.upgrade().is_none()); assert!(cache.get(&(999, false)).is_some()); + cache.retain_resources(&HashSet::new()); + assert_eq!(cache.bytes, 0); assert!(cache.images.is_empty()); } @@ -94,15 +105,22 @@ mod tests { for key in 0..2 { cache.insert((key, false), Image::filled(4, 4, Kind::Gray, [0.5; 4])); } + assert!(cache.get(&(0, false)).is_some()); + cache.insert((2, false), Image::filled(4, 4, Kind::Gray, [0.5; 4])); + assert!(cache.get(&(1, false)).is_none()); + let large = Image::filled(16, 16, Kind::Gray, [0.5; 4]); let weak = Arc::downgrade(&large.data); cache.insert((3, false), large); + assert!(weak.upgrade().is_none()); assert!(cache.get(&(0, false)).is_some()); + cache.clear(); + assert_eq!(cache.bytes, 0); } } diff --git a/crates/engine/src/document_path.rs b/crates/engine/src/document_path.rs index dbcbc1f..9d53796 100644 --- a/crates/engine/src/document_path.rs +++ b/crates/engine/src/document_path.rs @@ -2,6 +2,7 @@ pub(crate) fn normalize(path: &str) -> String { if path.contains("://") { return path.to_string(); } + #[cfg(not(target_arch = "wasm32"))] { use std::path::{Component, PathBuf}; @@ -9,13 +10,16 @@ pub(crate) fn normalize(path: &str) -> String { if let Ok(path) = path.canonicalize() { return path.to_string_lossy().into_owned(); } + let absolute = if path.is_absolute() { path } else { std::env::current_dir().unwrap_or_default().join(path) }; + // New files need a canonical parent so their identity stays the same after saving if let (Some(parent), Some(filename)) = (absolute.parent(), absolute.file_name()) && let Ok(parent) = parent.canonicalize() { return parent.join(filename).to_string_lossy().into_owned(); } + let mut normalized = PathBuf::new(); for part in absolute.components() { match part { @@ -26,6 +30,7 @@ pub(crate) fn normalize(path: &str) -> String { part => normalized.push(part.as_os_str()), } } + normalized.to_string_lossy().into_owned() } #[cfg(target_arch = "wasm32")] @@ -38,13 +43,16 @@ pub(crate) fn resolve(owner: &str, reference: &str) -> String { if reference.contains("://") { return reference.to_string(); } + if owner.starts_with("sbs://") { return materialcraft_formats::sbs::library::resolve(owner, reference).unwrap_or_else(|_| format!("sbs://{reference}")); } + let path = std::path::Path::new(reference); if path.is_absolute() { return normalize(reference); } + normalize(&std::path::Path::new(owner).parent().unwrap_or(std::path::Path::new("")).join(path).to_string_lossy()) } @@ -56,9 +64,11 @@ pub(crate) fn relative(owner: &str, source: &str) -> String { { return self::relative(&normalize(owner), &materialcraft_formats::sbs::library::root().join(relative).to_string_lossy()); } + if source.contains("://") { return source.to_string(); } + let folder = std::path::Path::new(owner).parent().unwrap_or(std::path::Path::new("")); let from: Vec<_> = folder.components().collect(); let to: Vec<_> = std::path::Path::new(source).components().collect(); @@ -66,6 +76,7 @@ pub(crate) fn relative(owner: &str, source: &str) -> String { if common == 0 { return source.replace('\\', "/"); } + let mut path = std::path::PathBuf::new(); for _ in common..from.len() { path.push(".."); @@ -73,5 +84,6 @@ pub(crate) fn relative(owner: &str, source: &str) -> String { for part in to.iter().skip(common) { path.push(part.as_os_str()); } + path.to_string_lossy().replace('\\', "/") } diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs index 13465a9..8117961 100644 --- a/crates/engine/src/file_cmds.rs +++ b/crates/engine/src/file_cmds.rs @@ -185,6 +185,7 @@ pub(crate) fn read_package_bytes(bytes: &[u8], name: &str, linked: &dyn Fn(&str) if !name.is_empty() { crate::linked_files::normalize_references(&mut read.pkg, name); } + Ok(read) } @@ -229,6 +230,7 @@ pub fn save_files_at(session: &Session, path: &str) -> Result Result, name: Opti if let Some(resource) = s.pkg.resources.iter_mut().find(|resource| resource.name == name) { resource.folder = folder.into(); } + Ok(name) }) } diff --git a/crates/engine/src/function_output.rs b/crates/engine/src/function_output.rs index f4238d2..5d32025 100644 --- a/crates/engine/src/function_output.rs +++ b/crates/engine/src/function_output.rs @@ -39,6 +39,7 @@ pub fn validate(document: &Graph, target: &FunctionGraphTarget, function: &Funct if !materialcraft_nodes::function::can_be_output(&node.op) { return Err("Dot nodes cannot be function outputs".into()); } + let Some(expected) = expected_type(document, target) else { return Ok(()) }; let types = materialcraft_nodes::function::node_types(function)?; @@ -47,12 +48,15 @@ pub fn validate(document: &Graph, target: &FunctionGraphTarget, function: &Funct FunctionGraphTarget::Dynamic { node, parameter } => { document.node(*node).is_ok_and(|node| materialcraft_nodes::accepts_scalar_function(&node.def, parameter)) } - // Quadrant luminosity functions may return one channel or an RGBA value. + + // Quadrant luminosity functions may return one channel or an RGBA value FunctionGraphTarget::Pattern { parameter, .. } => parameter == "opacity", _ => false, }; + if actual != expected && !(scalar_channel && expected == ValueType::Float4 && actual == ValueType::Float) { return Err(format!("Output node returns {}; expected {}", actual.label(), expected.label())); } + Ok(()) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b8754da..3ab4364 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -59,9 +59,10 @@ mod graph_property_commands; mod graph_target; mod history_prefs; mod library_cache_commands; +mod library_cache_prefs; pub use graph_target::GraphTarget; pub use history_prefs::HistoryPrefs; -pub use library_cache_commands::LibraryCachePrefs; +pub use library_cache_prefs::LibraryCachePrefs; pub mod graph_edit; pub mod graph_grid; pub mod inspect; diff --git a/crates/engine/src/library_cache_commands.rs b/crates/engine/src/library_cache_commands.rs index 84a0521..02c77da 100644 --- a/crates/engine/src/library_cache_commands.rs +++ b/crates/engine/src/library_cache_commands.rs @@ -2,33 +2,9 @@ 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) } - } -} +use crate::library_cache_prefs::LibraryCachePrefs; fn limit(parameters: &Value, key: &str, default: u32, min: u32, max: u32) -> crate::Result { match parameters.get(key) { @@ -84,7 +60,9 @@ mod tests { 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}), @@ -96,12 +74,17 @@ mod tests { 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); } } diff --git a/crates/engine/src/library_cache_prefs.rs b/crates/engine/src/library_cache_prefs.rs new file mode 100644 index 0000000..e0aa693 --- /dev/null +++ b/crates/engine/src/library_cache_prefs.rs @@ -0,0 +1,27 @@ +use serde::{Deserialize, Serialize}; + +/// 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) } + } +} diff --git a/crates/engine/src/library_preview.rs b/crates/engine/src/library_preview.rs index c91a331..bb8cb5d 100644 --- a/crates/engine/src/library_preview.rs +++ b/crates/engine/src/library_preview.rs @@ -27,6 +27,7 @@ impl NodeLibrary { if cancel.load(Ordering::Relaxed) { return None; } + let source = library.packages.get(package)?; let index = source.graphs.iter().position(|candidate| candidate.function.is_none() && candidate.sbs_path() == graph)?; let size = size.clamp(1, COOKED_SIZE); @@ -37,16 +38,20 @@ impl NodeLibrary { if cancel.load(Ordering::Relaxed) { return None; } + let graph = session.pkg.graphs.get(index)?; let decode = session.resource_decoder(); let mut options = materialcraft_eval::Options::new(&decode); + // Thin lines and fine grain vanish when cooked at thumbnail size, so the graph cooks larger and is scaled down let cooked_size = size.max(COOKED_SIZE); options.max_size = Some(cooked_size); options.cancel = Some(cancel); - // Unconnected experiments and intermediate previews do not contribute to a library tile. + + // Unconnected experiments and intermediate previews do not contribute to a library tile options.only = Some(graph.nodes.iter().filter(|node| node.def == "output").map(|node| node.id).collect()); options.inputs = sample_inputs(graph, cooked_size); + let cooked = materialcraft_eval::cook(&session.pkg, index, &options, &session.cache); if cooked.cancelled || cancel.load(Ordering::Relaxed) { return None; @@ -59,27 +64,34 @@ impl NodeLibrary { } } -/// An image for every input node of `graph`, by identifier: normals for normal inputs, colors for color inputs, the height for the rest +/// An image for every image input of `graph`, by identifier: normals for normal inputs, colors for color inputs, the height for the rest fn sample_inputs(graph: &materialcraft_core::Graph, size: u32) -> BTreeMap { + // Strong relief keeps normal inputs legible in small previews + const NORMAL_INTENSITY: f32 = 4.0; + + let mut input_nodes = graph.nodes.iter().filter(|node| matches!(node.def.as_str(), "input_color" | "input_grayscale")).peekable(); + if input_nodes.peek().is_none() { + return BTreeMap::new(); + } + let height = Image::generate_uv(size, size, Kind::Gray, |u, v| { [0.5 + 0.25 * (u * std::f32::consts::TAU).sin() + 0.25 * (v * std::f32::consts::TAU).cos(), 0.0, 0.0, 0.0] }); - let color = height.map(Kind::Color, |pixel| [0.15 + 0.7 * pixel[0], 0.35 + 0.3 * pixel[0], 0.8 - 0.5 * pixel[0], 1.0]); - let normal = materialcraft_nodes::atomic::height_to_normal(&height, 4.0, false, true, false); + let mut color = None; + let mut normal = None; - graph - .nodes - .iter() - .filter(|node| matches!(node.def.as_str(), "input_color" | "input_grayscale")) + input_nodes .map(|node| { let identifier = materialcraft_eval::identifier(node); - let image = if identifier.to_ascii_lowercase().contains("normal") { - &normal + let image: &Image = if identifier.to_ascii_lowercase().contains("normal") { + normal.get_or_insert_with(|| materialcraft_nodes::atomic::height_to_normal(&height, NORMAL_INTENSITY, false, true, false)) } else if node.def == "input_color" { - &color + // A blue-to-sand palette distinguishes color inputs from the grayscale height + color.get_or_insert_with(|| height.map(Kind::Color, |pixel| [0.15 + 0.7 * pixel[0], 0.35 + 0.3 * pixel[0], 0.8 - 0.5 * pixel[0], 1.0])) } else { &height }; + (identifier, image.clone()) }) .collect() @@ -98,5 +110,6 @@ fn is_flat(image: &Image) -> bool { } } } + (0..3).all(|channel| high[channel] - low[channel] < FLAT_SPREAD) } diff --git a/crates/engine/src/linked_files.rs b/crates/engine/src/linked_files.rs index d99794b..63b5403 100644 --- a/crates/engine/src/linked_files.rs +++ b/crates/engine/src/linked_files.rs @@ -22,17 +22,21 @@ impl crate::Session { for document in self.documents.values_mut() { document.pkg.dependencies.remove(path); } + #[cfg(not(target_arch = "wasm32"))] { let mut package = self.pkg.clone(); + // Include inactive dependents so closing a source also refreshes their references package.graphs.extend(self.documents.values().flat_map(|document| document.pkg.graphs.iter().cloned())); package.dependencies = self.linked_sources.clone(); for warning in load(&mut package) { self.warn(warning); } + self.linked_sources.extend(package.dependencies); } + self.refresh_links_from(std::collections::BTreeSet::from([path.to_string()])); } } @@ -68,6 +72,7 @@ pub(crate) fn load_references(package: &mut Package, mut pending: Vec) - warnings.extend(read.warnings); return Ok(read.pkg); } + #[cfg(target_arch = "wasm32")] return Err("external file is unavailable in the browser".into()); #[cfg(not(target_arch = "wasm32"))] @@ -87,5 +92,6 @@ pub(crate) fn load_references(package: &mut Package, mut pending: Vec) - Err(error) => warnings.push(format!("Could not resolve {path}: {error}")), } } + warnings } diff --git a/crates/engine/src/linked_packages.rs b/crates/engine/src/linked_packages.rs index 8ecd205..b0212c7 100644 --- a/crates/engine/src/linked_packages.rs +++ b/crates/engine/src/linked_packages.rs @@ -16,13 +16,16 @@ fn reaches(package: &Package, changed: &BTreeSet, sources: &BTreeMap = self .open_packages() @@ -146,30 +150,36 @@ impl Session { pending.extend(references(source).filter(|instance| !instance.package.is_empty()).map(|instance| instance.package.clone())); } } + let mut updated = false; for (path, source) in &previous_sources { if !active.contains(path) { continue; } + let mut package = source.as_ref().clone(); if interfaces(&mut package, &previous_sources) { sources.insert(path.clone(), Arc::new(package)); updated = true; } } + updated |= interfaces(&mut self.pkg, &sources); for document in self.documents.values_mut() { updated |= interfaces(&mut document.pkg, &sources); } + if !updated { break; } } + if reaches(&self.pkg, &changed, &sources) { self.modified = true; self.dirty = true; self.cook_epoch = self.cook_epoch.wrapping_add(1); } + self.pkg.dependencies = sources.clone(); for document in self.documents.values_mut() { if reaches(&document.pkg, &changed, &sources) { @@ -184,6 +194,7 @@ impl Session { *owned = source.clone(); } } + self.linked_sources = sources; } } diff --git a/crates/engine/src/linked_resource.rs b/crates/engine/src/linked_resource.rs index 25a06cc..65158af 100644 --- a/crates/engine/src/linked_resource.rs +++ b/crates/engine/src/linked_resource.rs @@ -1,3 +1,10 @@ +#[cfg(not(target_arch = "wasm32"))] +use std::io::Read as _; + +/// Largest linked resource read from beside an SBS; matches the archive member cap. +#[cfg(not(target_arch = "wasm32"))] +const MAX_LINKED_RESOURCE_BYTES: u64 = 256 << 20; + /// Read resource paths relative to an SBS package, including portable library aliases. pub fn reader(path: &str) -> impl Fn(&str) -> Option> + use<> { let owner = path.to_string(); @@ -38,14 +45,46 @@ pub fn reader(path: &str) -> impl Fn(&str) -> Option> + use<> { let folder = folder.as_ref()?; let file = std::fs::canonicalize(folder.join(&relative)).ok()?; let metadata = std::fs::metadata(&file).ok()?; - // Parent references are legal inside the configured library, never beyond its root. - if !file.starts_with(allowed_root.as_ref()?) || !metadata.is_file() { + + // Parent references are legal inside the configured library, never beyond its root + if !file.starts_with(allowed_root.as_ref()?) || !metadata.is_file() || metadata.len() > MAX_LINKED_RESOURCE_BYTES { return None; } - std::fs::read(file).ok() + let mut bytes = Vec::new(); + std::fs::File::open(&file).ok()?.take(MAX_LINKED_RESOURCE_BYTES + 1).read_to_end(&mut bytes).ok()?; + (bytes.len() as u64 <= MAX_LINKED_RESOURCE_BYTES).then_some(bytes) } #[cfg(target_arch = "wasm32")] None } } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + fn temp(name: &str) -> PathBuf { + std::env::temp_dir().join(format!("mc-linked-{}-{name}", std::process::id())) + } + + #[test] + fn linked_resources_over_the_cap_are_ignored() { + let dir = temp("cap"); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let sbs = dir.join("p.sbs"); + std::fs::write(&sbs, b"x").unwrap(); + let big = dir.join("big.bin"); + std::fs::File::create(&big).unwrap().set_len(MAX_LINKED_RESOURCE_BYTES + 1).unwrap(); + + let read = reader(&sbs.to_string_lossy()); + assert!(read("big.bin").is_none(), "a resource over the cap is not read"); + + std::fs::write(dir.join("small.bin"), b"ok").unwrap(); + assert_eq!(read("small.bin").as_deref(), Some(&b"ok"[..])); + + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/engine/src/node_library.rs b/crates/engine/src/node_library.rs index ef2f8b9..0b2338d 100644 --- a/crates/engine/src/node_library.rs +++ b/crates/engine/src/node_library.rs @@ -140,6 +140,7 @@ impl NodeLibrary { if !seen.insert(uri) { continue; } + let bytes = match library::read(uri) { Ok(bytes) => bytes, Err(error) => { @@ -148,8 +149,16 @@ impl NodeLibrary { } }; - // A layout's frames name library categories; anything else is a package of graphs - let placements = match materialcraft_formats::sbs::read_layout(&bytes, uri) { + let document = match materialcraft_formats::sbs::SbsDocument::parse(&bytes) { + Ok(document) => document, + Err(error) => { + out.warnings.push(format!("Library package {uri}: {error}")); + continue; + } + }; + + // Reuse the XML document when the frames do not classify this package as a layout + let placements = match document.layout(uri) { Ok(frames) => placements(&frames), Err(error) => { out.warnings.push(format!("Library package {uri}: {error}")); @@ -162,7 +171,7 @@ impl NodeLibrary { continue; } - match materialcraft_formats::sbs::read_sbs(&bytes, uri, &crate::file_cmds::linked_files(uri)) { + match document.read(uri, &crate::file_cmds::linked_files(uri)) { Ok(mut read) => { crate::linked_files::normalize_references(&mut read.pkg, uri); out.warnings.extend(read.warnings); @@ -171,11 +180,26 @@ impl NodeLibrary { Err(error) => out.warnings.push(format!("Library package {uri}: {error}")), } } + let mut registry = Package::new("Library"); registry.dependencies = std::mem::take(&mut out.packages); let pending = registry.dependencies.keys().cloned().collect(); out.warnings.extend(crate::linked_files::load_references(&mut registry, pending)); out.packages = registry.dependencies; + + // Package identities stay distinct while equal immutable resource payloads share storage + let mut buffers = BTreeMap::::new(); + for package in out.packages.values_mut().filter_map(Arc::get_mut) { + for resource in &mut package.resources { + match buffers.get(&resource.bytes.content_hash()) { + Some(bytes) if bytes == &resource.bytes => resource.bytes = bytes.clone(), + _ => { + buffers.insert(resource.bytes.content_hash(), resource.bytes.clone()); + } + } + } + } + out } @@ -194,6 +218,7 @@ impl NodeLibrary { { return; } + // The same node may sit in two categories; the id keeps each placement apart if !ids.insert(entry.id.clone()) { entry.id = format!("{}@{}", entry.id, entry.category); @@ -208,6 +233,7 @@ impl NodeLibrary { let id = format!("item:{}", category.to_ascii_lowercase()); push(&mut out, entry(id, category, "Graph items", "", item, None)); } + push(&mut out, entry("node:dot".into(), "Dot node", "Graph items", "A reroute point for wires.", LibraryItem::Node { def: "dot".into() }, None)); for placement in &self.placements { @@ -233,6 +259,7 @@ impl NodeLibrary { if matches!(definition.id, "input_color" | "input_grayscale" | "input_value" | "output") { "Atomic nodes" } else { definition.category }; push(&mut out, entry(id, definition.label, category, definition.description, item, None)); } + for operation in materialcraft_nodes::function::FUNCTIONS { let id = format!("function:{}", operation.id); if operation.id == "passthrough" || listed.contains(&id) { @@ -242,6 +269,7 @@ impl NodeLibrary { let category = category_path("Function nodes", Some(operation.category)); push(&mut out, entry(id, operation.label, &category, "", LibraryItem::Operation { operation: operation.id.into() }, None)); } + for kind in PatternNodeKind::ALL.into_iter().filter(|kind| *kind != PatternNodeKind::Dot) { let id = format!("pattern:{}", kind.native_id()); push(&mut out, entry(id, kind.label(), "FxMap nodes", "", LibraryItem::Pattern { kind }, None)); @@ -278,6 +306,7 @@ impl NodeLibrary { } } } + out } } @@ -401,6 +430,7 @@ fn library_category(graph: &materialcraft_core::Graph) -> Option { if let Some(category) = named(GRAPH_CATEGORIES, graph.sbs_path().rsplit('/').next().unwrap_or_default()) { return Some(category); } + if let Some(category) = tags().find_map(|tag| named(TAG_CATEGORIES, tag)) { return Some(category); } @@ -433,6 +463,7 @@ fn function_categories(graph: &materialcraft_core::Graph, helpers_hidden: bool) } } } + if categories.is_empty() { let path = graph.sbs_path(); match library_category(graph) { @@ -441,6 +472,7 @@ fn function_categories(graph: &materialcraft_core::Graph, helpers_hidden: bool) None => categories.push(function_category(&path)), } } + categories } diff --git a/crates/engine/src/node_library.test.rs b/crates/engine/src/node_library.test.rs index e0df913..f95e846 100644 --- a/crates/engine/src/node_library.test.rs +++ b/crates/engine/src/node_library.test.rs @@ -4,6 +4,21 @@ use materialcraft_core::Graph; #[path = "../tests/support/node_library.rs"] mod fixture; +#[test] +fn loaded_packages_share_equal_resource_buffers_without_merging_resources() { + let files = ["sbs://rt_ao.sbs".to_string(), "sbs://rt_ao_v2.sbs".to_string()]; + + let library = NodeLibrary::load(&files); + let resources: Vec<_> = + files.iter().map(|file| library.packages[file].resources.iter().find(|resource| resource.source.ends_with("bluenoise_256.png")).unwrap()).collect(); + + assert!(!resources[0].bytes.is_empty()); + assert_eq!(resources[0].bytes, resources[1].bytes); + assert!(resources[0].bytes.ptr_eq(&resources[1].bytes), "equal resource bytes should use one immutable buffer"); + assert!(library.packages[&files[0]].graph_by_name("rt_ao").is_some()); + assert!(library.packages[&files[1]].graph_by_name("rt_ao").is_some()); +} + /// Installing a library must not compile and clone graphs that no open document references. #[test] fn unused_library_packages_remain_shared_without_interface_refresh() { @@ -17,6 +32,7 @@ fn unused_library_packages_remain_shared_without_interface_refresh() { let mut library = NodeLibrary::default(); library.packages.insert("sbs://unused.sbs".into(), source.clone()); let session = crate::Session::with_library(Package::new("Empty"), Arc::new(library)); + assert!(Arc::ptr_eq(&source, &session.pkg.dependencies["sbs://unused.sbs"])); } @@ -92,6 +108,7 @@ fn hidden_graphs_and_helper_functions_are_not_listed() { graph.function = Some(materialcraft_core::FunctionGraph::default()); graph }; + let mut package = Package::new("Tools"); let mut shown = attributed("shown", "Filter", "Blur"); shown.add_node("output", [0.0; 2]); @@ -113,6 +130,7 @@ fn hidden_graphs_and_helper_functions_are_not_listed() { ("sbs://tools.sbs#shown", "Filters/Blurs"), ("sbs://user.sbs#loose", "Functions"), ]; + assert_eq!(listed, BTreeMap::from(expected), "a package of functions alone lists its loose ones"); } @@ -129,7 +147,9 @@ fn the_whole_bundled_library_is_categorized() { }; let unknown: Vec<_> = packaged.iter().filter(|entry| !known(&entry.category)).map(|entry| (&entry.id, &entry.category)).collect(); + assert!(unknown.is_empty(), "{unknown:?}"); + let category = |id: &str| packaged.iter().find(|entry| entry.id == id).map(|entry| entry.category.as_str()); for (id, expected) in [ ("sbs://noise_perlin_noise_v2.sbs#perlin_noise", "Texture generators/Noises"), @@ -324,6 +344,7 @@ fn library_graphs_cook_previews_from_sample_inputs() { let preview = |package: &str, graph: &str| NodeLibrary::preview(&library, package, graph, 32); let filter = preview("sbs://blur_hq.sbs", "blur_hq").unwrap(); + assert_eq!((filter.w, filter.h), (32, 32)); assert!(preview("sbs://materials/pbr/bricks_001.sbs", "bricks_001").is_some()); assert!(preview("sbs://noise_bnw_spots_1.sbs", "bnw_spots_1").is_some(), "an FX-Map noise reads the variables its size function sets"); @@ -343,10 +364,12 @@ fn repeated_library_previews_release_images_and_source_references() { let image = NodeLibrary::preview(&library, package, graph, 72).unwrap_or_else(|| panic!("missing preview {package}#{graph}")); let buffer = Arc::downgrade(&image.data); drop(image); + assert!(buffer.upgrade().is_none(), "a completed thumbnail must release its image after upload"); assert_eq!(Arc::strong_count(&library), 1); assert_eq!(library.packages.values().map(Arc::strong_count).collect::>(), references); } + println!("completed preview cycle {cycle}"); #[cfg(target_os = "linux")] if let Ok(status) = std::fs::read_to_string("/proc/self/status") { @@ -360,6 +383,7 @@ fn repeated_library_previews_release_images_and_source_references() { fn cancelled_library_previews_stop_before_preparing_the_package() { let library = Arc::new(NodeLibrary::default()); let cancelled = std::sync::atomic::AtomicBool::new(true); + assert!(NodeLibrary::preview_cancellable(&library, "unused", "unused", 72, &cancelled).is_none()); assert_eq!(Arc::strong_count(&library), 1); } diff --git a/crates/engine/src/preview_fingerprint.rs b/crates/engine/src/preview_fingerprint.rs index fa758bf..b6e6a1c 100644 --- a/crates/engine/src/preview_fingerprint.rs +++ b/crates/engine/src/preview_fingerprint.rs @@ -7,6 +7,9 @@ use sha2::{Digest, Sha256}; use crate::NodeLibrary; +const MAX_DEPENDENCY_PACKAGES: usize = 4096; +const RESOURCE_HASH_CHUNK_BYTES: usize = 64 * 1024; + impl NodeLibrary { /// Hash the source packages and resource bytes used by a library preview. The caller must /// also key its rendering implementation and display size. Missing dependencies or an @@ -18,34 +21,39 @@ impl NodeLibrary { let mut pending = BTreeSet::from([package]); let mut visited = BTreeSet::new(); while let Some(path) = pending.pop_first() { - if cancel.load(Ordering::Relaxed) || visited.len() >= 4096 { + if cancel.load(Ordering::Relaxed) || visited.len() >= MAX_DEPENDENCY_PACKAGES { return None; } + if !visited.insert(path) { continue; } + let source = self.packages.get(path)?; - // Package serialization excludes dependency pointers and resource bytes. Stream the - // model, then hash every byte explicitly, without allocating a second package copy. + + // Stream the model to include bytes omitted from package serialization without a copy serde_json::to_writer(&mut hash, &(path, source.as_ref())).ok()?; for resource in &source.resources { hash.update((resource.bytes.len() as u64).to_le_bytes()); - for chunk in resource.bytes.as_slice().chunks(64 * 1024) { + for chunk in resource.bytes.as_slice().chunks(RESOURCE_HASH_CHUNK_BYTES) { if cancel.load(Ordering::Relaxed) { return None; } + hash.update(chunk); } } + for instance in source.graphs.iter().flat_map(|graph| graph.instances()) { if !instance.package.is_empty() && !visited.contains(instance.package.as_str()) { pending.insert(&instance.package); - if pending.len().saturating_add(visited.len()) > 4096 { + if pending.len().saturating_add(visited.len()) > MAX_DEPENDENCY_PACKAGES { return None; } } } } + Some(hash.finalize().into()) } } diff --git a/crates/engine/src/preview_fingerprint.test.rs b/crates/engine/src/preview_fingerprint.test.rs index 1b2cd4a..381f9b4 100644 --- a/crates/engine/src/preview_fingerprint.test.rs +++ b/crates/engine/src/preview_fingerprint.test.rs @@ -31,21 +31,31 @@ fn key(library: &NodeLibrary) -> Option<[u8; 32]> { #[test] fn keys_survive_reloading_and_track_nested_content_not_unrelated_packages() { let original = library(); + assert_eq!(key(&original), key(&library())); + let mut changed = library(); changed.packages.insert("unrelated".into(), Arc::new(Package::new("unused"))); + assert_eq!(key(&original), key(&changed)); + Arc::make_mut(changed.packages.get_mut("child").unwrap()).graphs[0].seed += 1; + assert_ne!(key(&original), key(&changed)); + let mut changed = library(); Arc::make_mut(changed.packages.get_mut("child").unwrap()).resources[0].bytes = ResourceBytes::new(vec![1, 2, 4]); + assert_ne!(key(&original), key(&changed), "same-size replacement must invalidate the preview"); } #[test] fn incomplete_and_cancelled_keys_are_not_persisted() { let mut library = library(); + assert!(library.preview_fingerprint("root", "main", &AtomicBool::new(true)).is_none()); + library.packages.remove("child"); + assert!(key(&library).is_none()); } diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs index 04e6d99..6d553cf 100644 --- a/crates/engine/src/session.rs +++ b/crates/engine/src/session.rs @@ -184,6 +184,7 @@ impl Session { library_sources.insert(path.clone(), package.clone()); } } + let mut session = Session { environment: Default::default(), document_id: 0, @@ -224,13 +225,16 @@ impl Session { group: None, decoded: Arc::new(Mutex::new(crate::decoded_images::DecodedImages::default())), }; + session.refresh_links(); for warning in &library.warnings { session.warn(warning.clone()); } + if !library.packages.contains_key(crate::function_library::STANDARD_PACKAGE) { session.warn(format!("Missing function library: {} did not load", crate::function_library::STANDARD_PACKAGE)); } + session.library = library; session } @@ -308,6 +312,7 @@ impl Session { } return result; } + self.begin_group(label); let r = f(self); match r { @@ -318,6 +323,7 @@ impl Session { } } } + r } @@ -362,6 +368,7 @@ impl Session { pub fn changed(&mut self) { self.modified = true; self.dirty = true; + // Publish a grouped edit only after it succeeds, so rollback cannot dirty other packages if self.group.is_none() { self.refresh_links(); @@ -427,6 +434,7 @@ impl Session { .flat_map(|(file, package)| package.resources.iter().map(move |resource| (format!("{file}\0{}", resource.name), resource.bytes.clone()))), ); let memo = self.decoded.clone(); + // Release decoded images of deleted or replaced resources let hashes = res.iter().map(|(_, bytes)| bytes.content_hash()).collect(); memo.lock().unwrap_or_else(PoisonError::into_inner).retain_resources(&hashes); @@ -437,6 +445,7 @@ impl Session { if let Some(i) = memo.lock().unwrap_or_else(PoisonError::into_inner).get(&key) { return Some(i); } + let img = materialcraft_formats::decode(bytes.as_slice(), linear).ok()?; memo.lock().unwrap_or_else(PoisonError::into_inner).insert(key, img.clone()); Some(img) @@ -472,6 +481,7 @@ impl Session { if result.graph != self.graph || result.cancelled() { return; } + self.cooked_graph = Some(result.graph); self.cooked = result.cooked; self.material_cooked = result.materials; diff --git a/crates/engine/tests/it/embedded_edit.rs b/crates/engine/tests/it/embedded_edit.rs index 0e3f2f8..92cd39c 100644 --- a/crates/engine/tests/it/embedded_edit.rs +++ b/crates/engine/tests/it/embedded_edit.rs @@ -649,9 +649,11 @@ fn library_samplers_read_image_inputs_in_both_processors() { if definition == "value_processor" { let expected = if sampler == "samplecol" { Value::Float4(color) } else { Value::Float(color[0]) }; + assert_eq!(session.cooked.value(owner, 0), Some(&expected)); } else { let expected = if sampler == "samplecol" { color } else { [color[0], color[0], color[0], 1.0] }; + assert_eq!(session.cooked.image(owner, 0).unwrap().get(0, 0, false), expected); } } diff --git a/crates/engine/tests/it/function_instances.rs b/crates/engine/tests/it/function_instances.rs index d8fa550..0ac105b 100644 --- a/crates/engine/tests/it/function_instances.rs +++ b/crates/engine/tests/it/function_instances.rs @@ -83,17 +83,23 @@ fn function_expansion_allows_several_bounded_bodies_but_still_caps_total_work() graph.output = output; graph }; + let mut allowed = caller(3); function::resolve_instances(&mut allowed, &package, &BTreeMap::new()); + assert_eq!(evaluate(&allowed), Value::Float(3.0)); + let mut oversized = caller(64); function::resolve_instances(&mut oversized, &package, &BTreeMap::new()); + assert!(function::compile(&oversized, 0).err().unwrap().contains("limit")); - // Compiling a pre-resolved tree must enforce its own budget too. + + // Compiling a pre-resolved tree must enforce its own budget too let source = allowed.nodes.iter().find_map(|node| node.source.clone()).unwrap(); for node in oversized.nodes.iter_mut().filter(|node| node.instance.is_some()) { node.source = Some(source.clone()); } + assert!(function::compile(&oversized, 0).err().unwrap().contains("limit")); } @@ -200,22 +206,28 @@ fn processor_calls_recook_when_source_changes_and_round_trip_without_copying_sou } session.preview_size = Some(16); session.cook(); + assert_eq!(session.cooked.image(processors[0], 0).unwrap().get(0, 0, false), [0.2, 0.4, 0.6, 1.0]); assert_eq!(session.cooked.value(processors[1], 0), Some(&Value::Float4([0.2, 0.4, 0.6, 1.0]))); + execute(&mut session, "package.activate", &json!({"document": 0})).unwrap(); execute(&mut session, "graph.open", &json!({"graph": 1})).unwrap(); edit(&mut session, &json!({"kind": "graph"}), json!({"action": "value", "id": 1, "value": Value::Float4([0.7,0.3,0.1,1.0])})); execute(&mut session, "file.save", &json!({})).unwrap(); execute(&mut session, "package.activate", &json!({"document": document})).unwrap(); session.cook(); + assert_eq!(session.cooked.image(processors[0], 0).unwrap().get(0, 0, false), [0.7, 0.3, 0.1, 1.0]); assert_eq!(session.cooked.value(processors[1], 0), Some(&Value::Float4([0.7, 0.3, 0.1, 1.0]))); + execute(&mut session, "file.saveAs", &json!({"path": target_path})).unwrap(); let saved = std::fs::read(&target_path).unwrap(); let xml = String::from_utf8(saved.clone()).unwrap(); + assert!(xml.contains("source.sbs") && !xml.contains(source_path.to_str().unwrap())); assert!(xml.contains("pkg:///Tint?dependency=")); assert!(!xml.contains("0.7 0.3 0.1")); + let mut reopened = Session::new(Package::new("Empty")); execute(&mut reopened, "file.open", &json!({"path": target_path})).unwrap(); reopened.preview_size = Some(16); @@ -223,11 +235,14 @@ fn processor_calls_recook_when_source_changes_and_round_trip_without_copying_sou for result in reopened.cooked.nodes.values() { assert!(result.error.is_none(), "{:?}", result.error); } + std::fs::remove_file(&source_path).unwrap(); let mut missing = Session::new(Package::new("Empty")); file_cmds::open_bytes(&mut missing, &saved, Some(target_path.to_string_lossy().into())).unwrap(); + assert!(missing.graph().unwrap().functions().all(|function| function.nodes.iter().any(|node| node.instance.is_some()))); assert!(missing.graph().unwrap().functions().all(|function| !function::warnings(function).is_empty())); + std::fs::remove_dir_all(directory).unwrap(); } @@ -280,6 +295,7 @@ fn calls_evaluate_arguments_once_and_share_the_variables_they_set() { let total = caller.add("add", &[call, get], None); caller.output = Some(caller.add("sequence", &[init, total], None)); function::resolve_instances(&mut caller, &package, &BTreeMap::new()); + // The Get node ran once, before the argument's Set: 1; the call doubles its argument 2 assert_eq!(evaluate(&caller), Value::Float(5.0)); @@ -287,11 +303,13 @@ fn calls_evaluate_arguments_once_and_share_the_variables_they_set() { let after = caller.add("get_float1", &[], Some(Value::Text("private".into()))); let read_after = caller.add("sequence", &[call, after], None); caller.output = Some(caller.add("sequence", &[init, read_after], None)); + assert_eq!(evaluate(&caller), Value::Float(99.0)); - // A separate Get after the call sees the assignment while the call result stays cached. + // A separate Get after the call sees the assignment while the call result stays cached let total = caller.add("add", &[call, after], None); caller.output = Some(caller.add("sequence", &[init, total], None)); + assert_eq!(evaluate(&caller), Value::Float(103.0)); } diff --git a/crates/engine/tests/it/function_library.rs b/crates/engine/tests/it/function_library.rs index 98e6609..a9f74b7 100644 --- a/crates/engine/tests/it/function_library.rs +++ b/crates/engine/tests/it/function_library.rs @@ -58,6 +58,7 @@ fn every_standard_function_has_a_resolved_executable_body() { assert_eq!(entries.len(), 171); assert!(missing.is_empty(), "missing bundled functions: {missing:?}"); + for entry in entries { session.graph_mut().unwrap().function = Some(FunctionGraph::default()); session.reset_history(); @@ -66,6 +67,7 @@ fn every_standard_function_has_a_resolved_executable_body() { let function = session.graph().unwrap().function.as_ref().unwrap(); assert!(matches!(function.nodes[0].source, Some(Ok(_))), "{}", entry["graph"]); + function::compile(function, 0).unwrap_or_else(|error| panic!("{}: {error}", entry["graph"])); } } @@ -193,6 +195,7 @@ fn supplied_functions_evaluate_before_and_after_save() { for node in function.nodes.iter().filter(|node| node.instance.is_some()) { assert!(matches!(node.source, Some(Ok(_))), "{}: unresolved instance {}", graph.sbs_path(), node.id); } + let source = function::compile(function, 0).unwrap(); let saved = after.pkg.graph_by_name(&graph.sbs_path()).unwrap().1; let saved = function::compile(saved.function.as_ref().unwrap(), 0).unwrap(); @@ -225,6 +228,7 @@ fn supplied_functions_evaluate_before_and_after_save() { (parameter.id.clone(), value) }) .collect(); + // This hidden helper requires a render region supplied by its caller if graph.name == "compute_total_number" { variables.insert("render_region".into(), Value::Float4([-0.5, 0.5, -0.5, 0.5])); @@ -235,6 +239,7 @@ fn supplied_functions_evaluate_before_and_after_save() { Ok(expected) => { assert_eq!(saved.evaluate(&context, &mut EvaluationState::default()).unwrap(), expected, "{}", graph.sbs_path()); assert_eq!(converted.evaluate(&context, &mut EvaluationState::default()).unwrap(), expected, "converted {}", graph.sbs_path()); + defaults += usize::from(profile == 0); errors.clear(); break; @@ -242,6 +247,7 @@ fn supplied_functions_evaluate_before_and_after_save() { Err(error) => errors.push(error), } } + if !errors.is_empty() { failures.push(format!("{}: {}", graph.sbs_path(), errors.join("; "))); } @@ -252,5 +258,6 @@ fn supplied_functions_evaluate_before_and_after_save() { before.pkg.graphs.len() - failures.len(), before.pkg.graphs.len() ); + assert!(failures.is_empty(), "{}", failures.join("\n")); } diff --git a/crates/engine/tests/it/function_output.rs b/crates/engine/tests/it/function_output.rs index c45d552..81a4cc4 100644 --- a/crates/engine/tests/it/function_output.rs +++ b/crates/engine/tests/it/function_output.rs @@ -55,6 +55,7 @@ fn pattern_luminosity_accepts_scalar_and_float4_but_rejects_other_types() { let Some(Value::PatternGraph(pattern)) = session.graph_mut().unwrap().node_mut(owner).unwrap().params.get_mut("pattern") else { panic!("expected pattern") }; + pattern.nodes[0].parameters.clear(); pattern.nodes[0].parameters.insert("opacity".into(), Value::Function(Box::default())); for color in [false, true] { @@ -64,12 +65,15 @@ fn pattern_luminosity_accepts_scalar_and_float4_but_rejects_other_types() { let output = function.output.unwrap(); execute(&mut session, "graph.function.set", &json!({"target": target, "function": function})).unwrap(); execute(&mut session, "graph.function.edit", &json!({"target": target, "edit": {"action": "output", "id": output}})).unwrap(); + assert!(materialcraft_engine::function_inspection::warnings(session.graph().unwrap()).is_empty()); } + for value in [Value::Float2([0.5; 2]), Value::Int(1)] { let function = FunctionGraph::constant(value); let output = function.output.unwrap(); execute(&mut session, "graph.function.set", &json!({"target": target, "function": function})).unwrap(); + assert!(execute(&mut session, "graph.function.edit", &json!({"target": target, "edit": {"action": "output", "id": output}})).is_err()); } } diff --git a/crates/engine/tests/it/graph_interaction.rs b/crates/engine/tests/it/graph_interaction.rs index 1b5d25f..e6575f9 100644 --- a/crates/engine/tests/it/graph_interaction.rs +++ b/crates/engine/tests/it/graph_interaction.rs @@ -134,6 +134,7 @@ fn fx_map_starts_with_a_quadrant_and_grows_image_inputs_as_connected() { execute(&mut session, "graph.node.param", &json!({"id": source.0, "param": "color_mode", "value": false})).unwrap(); let owner = add(&mut session, "fx_map"); let Value::PatternGraph(pattern) = &session.graph().unwrap().node(owner).unwrap().params["pattern"] else { panic!("missing default pattern") }; + assert_eq!(pattern.nodes.len(), 1); assert_eq!(pattern.nodes[0].kind, PatternNodeKind::Quadrant); assert_eq!(pattern.nodes[0].position, [0.0, 0.0]); @@ -141,17 +142,22 @@ fn fx_map_starts_with_a_quadrant_and_grows_image_inputs_as_connected() { assert_eq!(session.input_ports(owner), vec!["background", "pattern0"]); let fresh = read_sbs(&write_sbs(&session.pkg).unwrap(), "fresh_pattern.sbs", &|_| None).unwrap(); + assert!(fresh.warnings.is_empty(), "{:?}", fresh.warnings); + let loaded_pattern = fresh.pkg.graphs[0].nodes.iter().find(|node| node.def == "fx_map").unwrap(); let Value::PatternGraph(loaded_pattern) = &loaded_pattern.params["pattern"] else { panic!("missing saved root") }; + assert_eq!(loaded_pattern.nodes.len(), 1); assert_eq!(loaded_pattern.nodes[0].kind, PatternNodeKind::Quadrant); assert_eq!(loaded_pattern.root, Some(loaded_pattern.nodes[0].id)); for index in 0..6 { execute(&mut session, "graph.connect", &json!({"from": source.0, "to": owner.0, "toPort": format!("pattern{index}")})).unwrap(); + assert_eq!(session.input_ports(owner).last(), Some(&format!("pattern{}", index + 1))); } + let Value::PatternGraph(pattern) = &session.graph().unwrap().node(owner).unwrap().params["pattern"] else { panic!("missing pattern") }; let mut pattern = (**pattern).clone(); @@ -159,25 +165,36 @@ fn fx_map_starts_with_a_quadrant_and_grows_image_inputs_as_connected() { pattern.nodes[0].parameters.insert("imageindex".into(), Value::Int(6)); execute(&mut session, "graph.pattern.set", &json!({"id": owner.0, "pattern": pattern})).unwrap(); session.cook(); + assert!(session.cooked.nodes[&owner].error.is_none(), "{:?}", session.cooked.nodes[&owner].error); assert!((session.cooked.image(owner, 0).unwrap().mean()[0] - 0.5).abs() < 1e-5); let bytes = write_sbs(&session.pkg).unwrap(); + assert!(std::str::from_utf8(&bytes).unwrap().contains("")); + let read = read_sbs(&bytes, "image_inputs.sbs", &|_| None).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let mut loaded = Session::new(read.pkg); loaded.preview_size = Some(16); let loaded_owner = loaded.graph().unwrap().nodes.iter().find(|node| node.def == "fx_map").unwrap().id; + assert_eq!(loaded.input_ports(loaded_owner), session.input_ports(owner)); + loaded.cook(); + assert!((loaded.cooked.image(loaded_owner, 0).unwrap().mean()[0] - 0.5).abs() < 1e-5); execute(&mut session, "graph.disconnect", &json!({"to": owner.0, "toPort": "pattern5"})).unwrap(); + assert_eq!(session.input_ports(owner).last().map(String::as_str), Some("pattern5")); assert!(session.undo().is_some()); assert_eq!(session.input_ports(owner).last().map(String::as_str), Some("pattern6")); + let before = session.pkg.clone(); + assert!(execute(&mut session, "graph.connect", &json!({"from": source.0, "to": owner.0, "toPort": "pattern999999999"})).is_err()); assert_eq!(session.pkg, before); } @@ -220,6 +237,7 @@ fn processors_sample_dynamic_image_inputs_after_sbs_round_trip() { let processor = add(&mut session, definition); for index in 0..6 { execute(&mut session, "graph.connect", &json!({"from": source.0, "to": processor.0, "toPort": format!("input{index}")})).unwrap(); + assert_eq!(session.input_ports(processor).last(), Some(&format!("input{}", index + 1))); } @@ -228,15 +246,21 @@ fn processors_sample_dynamic_image_inputs_after_sbs_round_trip() { function.output = Some(function.add("samplelum", &[position], Some(Value::Int2([5, 1])))); execute(&mut session, "graph.node.param", &json!({"id": processor.0, "param": "function", "value": Value::Function(Box::new(function))})).unwrap(); let bytes = write_sbs(&session.pkg).unwrap(); + assert!(std::str::from_utf8(&bytes).unwrap().contains("")); + let read = read_sbs(&bytes, "processor_images.sbs", &|_| None).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let mut loaded = Session::new(read.pkg); loaded.preview_size = Some(16); loaded.cook(); let processor = loaded.graph().unwrap().nodes.iter().find(|node| node.def == definition).unwrap().id; + assert_eq!(loaded.input_ports(processor).last().map(String::as_str), Some("input6")); assert!(loaded.cooked.nodes[&processor].error.is_none(), "{:?}", loaded.cooked.nodes[&processor].error); + if definition == "value_processor" { assert_eq!(loaded.cooked.value(processor, 0), Some(&Value::Float(0.5))); } else { diff --git a/crates/engine/tests/it/linked_instances.rs b/crates/engine/tests/it/linked_instances.rs index f4c0dd9..58f6c19 100644 --- a/crates/engine/tests/it/linked_instances.rs +++ b/crates/engine/tests/it/linked_instances.rs @@ -42,23 +42,34 @@ fn source_edits_update_instances_and_dirty_dependents_before_saving() { let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap()); session.preview_size = Some(16); session.cook(); + assert_eq!(session.output_ports(instance), ["color"]); assert!(session.cooked.nodes[&instance].error.is_none(), "{:?}", session.cooked.nodes[&instance].error); assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.25, 0.5, 0.75, 1.0]); + let history = session.history(); session.modified = false; execute(&mut session, "package.activate", &json!({"document": 0})).unwrap(); execute(&mut session, "graph.select", &json!({"ids": [value.0]})).unwrap(); execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [0.8, 0.2, 0.1, 1.0]})).unwrap(); + assert!(session.open_packages().iter().find(|entry| entry.0 == target_id).unwrap().3); + execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap(); + assert_eq!(session.history(), history); + session.cook(); + assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.8, 0.2, 0.1, 1.0]); + execute(&mut session, "edit.undo", &json!({})).unwrap(); + assert!(session.graph().unwrap().nodes.is_empty()); + execute(&mut session, "edit.redo", &json!({})).unwrap(); session.cook(); + assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.8, 0.2, 0.1, 1.0]); } @@ -93,18 +104,25 @@ fn failed_edit_groups_do_not_publish_source_changes() { execute(session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [1.0, 0.0, 0.0, 1.0]})).unwrap(); execute(session, "graph.node.param", &json!({"id": 99999, "param": "color", "value": [0.0, 0.0, 0.0, 0.0]})) }); + assert!(result.is_err()); assert!(!session.open_packages().iter().find(|entry| entry.0 == target_id).unwrap().3); + execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap(); session.preview_size = Some(16); session.cook(); + assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.25, 0.5, 0.75, 1.0]); + execute(&mut session, "package.activate", &json!({"document": 0})).unwrap(); session.begin_group("Complete source edit on package switch"); execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [1.0, 0.0, 0.0, 1.0]})).unwrap(); execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap(); + assert!(session.modified); + session.cook(); + assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [1.0, 0.0, 0.0, 1.0]); } @@ -143,13 +161,20 @@ fn updates_propagate_through_three_packages_and_cycles_are_rejected() { session.modified = false; session.preview_size = Some(16); session.cook(); + assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.25, 0.5, 0.75, 1.0]); + execute(&mut session, "package.activate", &json!({"document": 0})).unwrap(); + assert!(execute(&mut session, "graph.instance.add", &json!({"document": outer, "graph": "Target"})).is_err()); + execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [0.1, 0.2, 0.3, 1.0]})).unwrap(); + assert!(session.open_packages().iter().filter(|entry| entry.0 != 0).all(|entry| entry.3)); + execute(&mut session, "package.activate", &json!({"document": outer})).unwrap(); session.cook(); + assert_eq!(session.cooked.image(instance, 0).unwrap().get(0, 0, false), [0.1, 0.2, 0.3, 1.0]); } @@ -168,13 +193,19 @@ fn closing_a_modified_source_discards_its_unsaved_changes_in_dependents() { let instance = NodeId(execute(&mut session, "graph.instance.add", &json!({"document": 0, "graph": "Source"})).unwrap()["id"].as_u64().unwrap()); execute(&mut session, "package.activate", &json!({"document": 0})).unwrap(); execute(&mut session, "graph.node.param", &json!({"id": value.0, "param": "color", "value": [1.0, 0.0, 0.0, 1.0]})).unwrap(); + assert!(execute(&mut session, "package.close", &json!({})).is_err()); + execute(&mut session, "package.close", &json!({"discard": true})).unwrap(); + assert_eq!(session.document_id, target_id); + session.preview_size = Some(16); session.cook(); let color = session.cooked.image(instance, 0).unwrap().get(0, 0, false); + assert!(color.into_iter().zip([0.25, 0.5, 0.75, 1.0]).all(|(actual, expected)| (actual - expected).abs() < 1e-6)); + std::fs::remove_dir_all(directory).unwrap(); } @@ -199,10 +230,12 @@ fn named_value_inputs_and_outputs_keep_their_type_across_files() { session.preview_size = Some(16); session.cook(); let cooked = &session.cooked.nodes[&instance]; + assert!(cooked.error.is_none(), "{:?}", cooked.error); assert_eq!(cooked.outputs[0].as_value(), Some(&Value::Float(0.75))); assert_eq!(session.output_ports(instance), ["result"]); assert!(!materialcraft_engine::MaterialSource { graph: session.graph, node: instance, port: "result".into() }.is_image(&session.pkg)); + let mut local = session.open_packages().into_iter().find(|entry| entry.0 == 0).unwrap().1.clone(); let mut caller = session.graph().unwrap().clone(); caller.node_mut(instance).unwrap().instance.as_mut().unwrap().package.clear(); @@ -215,6 +248,7 @@ fn named_value_inputs_and_outputs_keep_their_type_across_files() { reopened.cook(); let instance = reopened.graph().unwrap().nodes.iter().find(|node| node.def == "graph_instance").unwrap().id; let cooked = &reopened.cooked.nodes[&instance]; + assert!(cooked.error.is_none(), "{:?}", cooked.error); assert_eq!(cooked.outputs[0].as_value(), Some(&Value::Float(0.75))); } @@ -237,9 +271,12 @@ fn removing_a_source_output_does_not_erase_connected_instance_wires_on_save() { execute(&mut session, "package.activate", &json!({"document": target_id})).unwrap(); let bytes = file_cmds::save_bytes(&session).unwrap(); let read = formats::sbs::read_sbs(&bytes, "target.sbs", &|_| None).unwrap(); + assert_eq!(read.pkg.graphs[0].connections.len(), 1); + session.preview_size = Some(16); session.cook(); + assert!(session.cooked.nodes[&instance].error.is_some()); } @@ -285,28 +322,38 @@ fn saved_dependencies_reopen_without_source_files_in_explorer_and_missing_source execute(&mut session, "graph.connect", &json!({"from": instance, "fromPort": "color", "to": output})).unwrap(); execute(&mut session, "file.save", &json!({"path": target_path})).unwrap(); let saved = std::fs::read_to_string(&target_path).unwrap(); + assert!(saved.contains("filename v=\"source.sbs\"")); assert!(!saved.contains(&directory.to_string_lossy().to_string())); + let mut reopened = Session::new(Package::new("Initial")); file_cmds::open_file(&mut reopened, target_path.to_str().unwrap()).unwrap(); + assert!(reopened.load_warnings.is_empty(), "{:?}", reopened.load_warnings); assert_eq!(reopened.open_packages().len(), 2); + execute(&mut reopened, "graph.open", &json!({"graph": "Target"})).unwrap(); reopened.preview_size = Some(16); reopened.cook(); let graph = reopened.graph().unwrap(); let output = graph.nodes.iter().find(|node| node.def == "output").unwrap().id; let color = reopened.cooked.image(output, 0).unwrap().get(0, 0, false); + assert!(color.into_iter().zip([0.25, 0.5, 0.75, 1.0]).all(|(actual, expected)| (actual - expected).abs() < 1e-6)); + std::fs::remove_file(&source_path).unwrap(); let mut missing = Session::new(Package::new("Initial")); file_cmds::open_file(&mut missing, target_path.to_str().unwrap()).unwrap(); execute(&mut missing, "graph.open", &json!({"graph": "Target"})).unwrap(); + assert_eq!(missing.graph().unwrap().connections.len(), 1); + let resaved = file_cmds::save_bytes(&missing).unwrap(); let resaved = formats::sbs::read_sbs(&resaved, "target.sbs", &|_| None).unwrap(); + assert_eq!(resaved.pkg.graph_by_name("Target").unwrap().1.connections.len(), 1); assert!(missing.path.is_some()); + std::fs::remove_dir_all(directory).unwrap(); } @@ -352,6 +399,7 @@ fn instance_defaults_follow_current_source_inputs_before_and_after_save() { assert_eq!(cooked.nodes[&instance].error, None); assert_eq!(cooked.nodes[&instance].parameters["tint"], Value::Color(expected)); + assert_instance_color(&mut session, instance, expected); } @@ -360,7 +408,9 @@ fn instance_defaults_follow_current_source_inputs_before_and_after_save() { std::fs::write(&target_path, file_cmds::save_bytes(&session).unwrap()).unwrap(); let mut reopened = Session::new(Package::new("Initial")); file_cmds::open_file(&mut reopened, target_path.to_str().unwrap()).unwrap(); + assert!(reopened.load_warnings.is_empty(), "{:?}", reopened.load_warnings); + reopened.graph = target_graph; reopened.preview_size = Some(16); let instance = reopened.graph().unwrap().nodes.iter().find(|node| node.def == "graph_instance").unwrap().id; diff --git a/crates/engine/tests/it/linked_resources.rs b/crates/engine/tests/it/linked_resources.rs index 7c66792..4fe2024 100644 --- a/crates/engine/tests/it/linked_resources.rs +++ b/crates/engine/tests/it/linked_resources.rs @@ -13,17 +13,23 @@ fn bundled_voronoi_bitmaps_load_the_shared_resource_and_survive_portable_saving( let path = "sbs://noise_voronoi.sbs"; let bytes = sbs::library::read(path).unwrap(); let read = sbs::read_sbs(&bytes, path, &file_cmds::linked_files(path)).unwrap(); + assert!(!read.warnings.iter().any(|warning| warning.contains("resource")), "{:?}", read.warnings); + let bitmaps: Vec<_> = read.pkg.graphs.iter().flat_map(|graph| &graph.nodes).filter(|node| node.def == "bitmap").cloned().collect(); + assert_eq!(bitmaps.len(), 4); + let expected = sbs::library::read("sbs://resources/perm.png").unwrap(); for bitmap in &bitmaps { let Value::Text(name) = &bitmap.params["resource"] else { panic!("missing resource") }; let resource = read.pkg.resource(name).unwrap(); + assert_eq!(resource.bytes.as_slice(), expected); assert_eq!(resource.source, "resources/perm.png"); } - // Exercise the repaired bitmaps independently of remaining Voronoi function limitations. + + // Exercise the repaired bitmaps independently of remaining Voronoi function limitations let mut package = Package::new("Resource regression"); package.resources = read.pkg.resources; let mut graph = Graph::new("Bitmaps"); @@ -37,13 +43,17 @@ fn bundled_voronoi_bitmaps_load_the_shared_resource_and_survive_portable_saving( let cooked = session.cook().clone(); for node in &session.pkg.graphs[0].nodes { assert!(cooked.nodes[&node.id].error.is_none()); + let image = cooked.image(node.id, 0).unwrap(); let low = image.data.iter().copied().fold(f32::INFINITY, f32::min); let high = image.data.iter().copied().fold(f32::NEG_INFINITY, f32::max); + assert!(low.is_finite() && high.is_finite() && high > low); } + let saved = file_cmds::save_bytes(&session).unwrap(); let reopened = sbs::read_sbs(&saved, "relocated.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert_eq!(reopened.pkg.resources[0].bytes.as_slice(), expected); } @@ -121,7 +131,7 @@ fn verify_library_resources(root: &std::path::Path) { assert!(read("../../private.svg").is_none()); assert!(read("resources/absent.svg").is_none()); - // Native filesystem opens use the same shared root as alias opens, including nested packages. + // Native filesystem opens use the same shared root as alias opens, including nested packages let native = root.join("assets/sbs/nested/source.sbs"); let native_read = file_cmds::linked_files(native.to_str().unwrap()); assert_eq!(native_read("../resources/tile.svg").unwrap(), TILE); diff --git a/crates/engine/tests/it/material_preview.rs b/crates/engine/tests/it/material_preview.rs index 55db290..52b081d 100644 --- a/crates/engine/tests/it/material_preview.rs +++ b/crates/engine/tests/it/material_preview.rs @@ -192,12 +192,17 @@ fn multiple_graph_channels_and_multi_output_instances_use_the_correct_ports() { execute(&mut session, "graph.open", &json!({"graph": 1})).unwrap(); let instance = instance(&mut session, "Colors"); execute(&mut session, "view.3d.assign", &json!({"node": instance, "port": "blue", "channel": "baseColor"})).unwrap(); + assert_eq!(color(&mut session, "baseColor"), [0.1, 0.1, 0.8, 1.0]); + let key = session.material_key(); execute(&mut session, "view.3d.assign", &json!({"node": instance, "port": 0, "channel": "baseColor"})).unwrap(); + assert_eq!(color(&mut session, "baseColor"), [0.8, 0.1, 0.1, 1.0]); assert_ne!(session.material_key(), key); + execute(&mut session, "view.3d.assign", &json!({"graph": "Colors", "node": 1, "channel": "emissive"})).unwrap(); + assert_eq!(color(&mut session, "emissive"), [0.8, 0.1, 0.1, 1.0]); assert_eq!(session.graph, 1); } diff --git a/crates/engine/tests/it/node_library.rs b/crates/engine/tests/it/node_library.rs index 0c7597c..c54ddd0 100644 --- a/crates/engine/tests/it/node_library.rs +++ b/crates/engine/tests/it/node_library.rs @@ -12,15 +12,20 @@ fn bundled_cloud_and_white_noise_parameter_scopes_cook() { for file in ["clouds_3.sbs", "noise_clouds_3_v2.sbs", "white_noise_v2.sbs"] { materialcraft_engine::file_cmds::open_file(&mut session, &format!("sbs://{file}")).unwrap(); let warnings = materialcraft_engine::function_inspection::warnings(session.graph().unwrap()); + assert!(warnings.is_empty(), "{file}: {warnings:?}"); + let nodes = session.graph().unwrap().nodes.clone(); let cooked = session.cook(); let failures: Vec<_> = cooked.nodes.iter().filter_map(|(id, result)| result.error.as_ref().map(|error| (*id, error))).collect(); + assert!(failures.is_empty(), "{file}: {failures:?}"); + for node in nodes.iter().filter(|node| matches!(node.def.as_str(), "output" | "fx_map")) { let image = cooked.image(node.id, 0).unwrap(); let low = image.data.iter().copied().fold(f32::INFINITY, f32::min); let high = image.data.iter().copied().fold(f32::NEG_INFINITY, f32::max); + assert!(high - low > 0.05, "{file} lost variation: {low}..{high}"); } } @@ -35,25 +40,35 @@ fn bundled_brick_material_cooks_without_missing_functions_or_budget_errors() { session.preview_size = Some(64); materialcraft_engine::file_cmds::open_file(&mut session, "sbs://materials/pbr/bricks_001.sbs").unwrap(); let outputs: Vec<_> = session.graph().unwrap().nodes.iter().filter(|node| node.def == "output").map(|node| node.id).collect(); + assert!(!outputs.is_empty()); + let cooked = session.cook(); let failures: Vec<_> = cooked.nodes.iter().filter_map(|(id, result)| result.error.as_ref().map(|error| (*id, error))).collect(); + assert!(failures.is_empty(), "{failures:?}"); + for output in outputs { let image = cooked.image(output, 0).unwrap(); + assert!(image.data.iter().all(|value| value.is_finite())); } - // Finite output alone missed flat noise, identical tile values and a half-turn hue error. + + // Finite output alone missed flat noise, identical tile values and a half-turn hue error for id in [24, 34, 48, 91] { let image = cooked.image(materialcraft_engine::core::NodeId(id), 0).unwrap(); let low = image.data.iter().copied().fold(f32::INFINITY, f32::min); let high = image.data.iter().copied().fold(f32::NEG_INFINITY, f32::max); + assert!(high - low > 0.05, "node {id} lost its variation: {low}..{high}"); + if id == 24 { assert!((0.15..0.85).contains(&image.mean()[0]), "fractal noise has lost its neutral range"); } } + let color = cooked.image(materialcraft_engine::core::NodeId(99), 0).unwrap().mean(); + assert!(color[0] > color[2], "the default brick color shifted to blue: {color:?}"); } diff --git a/crates/engine/tests/it/optimization_perf.rs b/crates/engine/tests/it/optimization_perf.rs index c194590..2eb7b8d 100644 --- a/crates/engine/tests/it/optimization_perf.rs +++ b/crates/engine/tests/it/optimization_perf.rs @@ -53,6 +53,7 @@ fn optimization_baseline() { resource: &|_| None, vars: &variables, }; + let ms = median_ms(|| { black_box((definition.cook)(&context).unwrap()); }); @@ -81,11 +82,15 @@ fn optimization_baseline() { let options = Options::new(&|_| None); let cache = Mutex::new(Cache::default()); let initial = cook(&package, 0, &options, &cache); + assert_eq!(initial.nodes.len(), count); assert!(initial.nodes.values().all(|result| result.error.is_none())); + let ms = median_ms(|| { let cooked = cook(&package, 0, &options, &cache); + assert!(cooked.nodes.values().all(|result| result.cached)); + black_box(cooked); }); println!("warm_cook,{count},{branching},{ms:.3}"); diff --git a/crates/engine/tests/it/parameter_functions.rs b/crates/engine/tests/it/parameter_functions.rs index b459240..7c68023 100644 --- a/crates/engine/tests/it/parameter_functions.rs +++ b/crates/engine/tests/it/parameter_functions.rs @@ -274,6 +274,7 @@ fn instance_parameter_functions_work_without_stored_literals_and_use_instance_si let loaded = formats::sbs::read_sbs(&bytes, "instance.sbs", &|_| None).unwrap(); let mut reopened = Session::new(loaded.pkg); let owner = reopened.graph().unwrap().nodes[0].id; + assert!((reopened.cook().image(owner, 0).unwrap().get_gray(0, 0, false) - 0.75).abs() < 1e-6); } @@ -291,22 +292,30 @@ fn color_boolean_enum_and_text_parameters_use_typed_function_outputs() { command(&mut session, "graph.node.function", json!({"id": owner, "param": parameter, "function": FunctionGraph::constant(value.clone())})); let result = &session.cook().nodes[&owner]; + assert_eq!(result.error, None, "{parameter}"); + let expected = materialcraft_engine::nodes::find(definition).unwrap().param(parameter).unwrap().default.coerce_json(&json!(value)).unwrap(); + assert_eq!(result.parameters[parameter], expected); } let owner = node(&mut session, "uniform"); command(&mut session, "graph.node.function", json!({"id": owner, "param": "color", "function": FunctionGraph::constant(Value::Float(0.5))})); + // A gray level fills the color channels, as the library's uniform colors rely on let result = &session.cook().nodes[&owner]; + assert_eq!(result.error, None); assert_eq!(result.parameters["color"].as_vec4(), Some([0.5, 0.5, 0.5, 1.0])); let owner = node(&mut session, "uniform"); command(&mut session, "graph.node.function", json!({"id": owner, "param": "color", "function": FunctionGraph::constant(Value::Int(1))})); + assert!(session.cook().nodes[&owner].error.as_ref().unwrap().contains("Float4")); + let bitmap = node(&mut session, "bitmap"); + assert!(execute(&mut session, "graph.node.function", &json!({"id": bitmap, "param": "resource"})).is_err()); } @@ -320,22 +329,32 @@ fn native_channel_parameters_accept_scalar_functions_without_changing_their_bodi let function = FunctionGraph::constant(Value::Float(0.25)); command(&mut session, "graph.node.function", json!({"id": owner, "param": parameter, "function": function})); let target = materialcraft_engine::FunctionGraphTarget::Dynamic { node: owner, parameter: (*parameter).into() }; + assert!(materialcraft_engine::function_inspection::warnings_for(session.graph().unwrap(), &target, &function).is_empty()); + let result = &session.cook().nodes[&owner]; + assert_eq!(result.error, None, "{definition}.{parameter}"); + let expected = if definition == "fx_map" { Value::Color([0.25, 0.25, 0.25, 1.0]) } else { Value::Float4([0.25; 4]) }; + assert_eq!(result.parameters[*parameter], expected); let bytes = formats::sbs::write_sbs(&session.pkg).unwrap(); let loaded = formats::sbs::read_sbs(&bytes, "scalar-channel.sbs", &|_| None).unwrap(); + assert!(loaded.warnings.is_empty(), "{:?}", loaded.warnings); + let reopened_node = loaded.pkg.graphs[0].nodes.last().unwrap(); let reopened_id = reopened_node.id; let saved_function = &reopened_node.functions[*parameter]; + assert_eq!(saved_function.nodes.len(), 1); assert_eq!(saved_function.nodes[0].value, Some(Value::Float(0.25))); + let mut reopened = Session::new(loaded.pkg); reopened.preview_size = Some(16); + assert_eq!(reopened.cook().nodes[&reopened_id].parameters[*parameter], expected); } } diff --git a/crates/engine/tests/it/pattern_graph.rs b/crates/engine/tests/it/pattern_graph.rs index e6453ca..37895bf 100644 --- a/crates/engine/tests/it/pattern_graph.rs +++ b/crates/engine/tests/it/pattern_graph.rs @@ -63,9 +63,13 @@ fn dynamic_roughness_survives_native_saving_and_invalidates_the_cook_cache() { for _ in 0..2 { let saved = write_sbs(&package).unwrap(); + assert!(std::str::from_utf8(&saved).unwrap().contains("")); + let loaded = read_sbs(&saved, "roughness.sbs", &|_| None).unwrap(); + assert!(loaded.warnings.is_empty(), "{:?}", loaded.warnings); + package = loaded.pkg; let owner = package.graphs[0].nodes[0].id; let cache = std::sync::Mutex::new(materialcraft_engine::eval::Cache::default()); @@ -73,10 +77,13 @@ fn dynamic_roughness_survives_native_saving_and_invalidates_the_cook_cache() { for (time, expected) in [(0.0, 1.0), (0.5, 1.0 / 3.0), (1.0, 0.5), (2.0, 2.0 / 3.0)] { options.environment.time_seconds = time; let result = materialcraft_engine::eval::cook(&package, 0, &options, &cache); + assert_eq!(result.nodes[&owner].error, None); assert!(!result.nodes[&owner].cached); assert!((result.image(owner, 0).unwrap().get(0, 0, false)[0] - expected).abs() < 1e-6); + let cached = materialcraft_engine::eval::cook(&package, 0, &options, &cache); + assert!(cached.nodes[&owner].cached); assert_eq!(cached.image(owner, 0).unwrap().data, result.image(owner, 0).unwrap().data); } diff --git a/crates/engine/tests/it/sbs_functions.rs b/crates/engine/tests/it/sbs_functions.rs index 3573db5..f0f1a9c 100644 --- a/crates/engine/tests/it/sbs_functions.rs +++ b/crates/engine/tests/it/sbs_functions.rs @@ -4,7 +4,7 @@ use serde_json::json; #[test] fn imported_expression_cooks_follows_inputs_and_survives_undo_and_save() { - // Original structural XML: in_low = amount - 0.25, with no embedded native package. + // Original structural XML: in_low = amount - 0.25, with no embedded native package let xml = r#" @@ -22,7 +22,9 @@ fn imported_expression_cooks_follows_inputs_and_survives_undo_and_save() { "#; let imported = sbs::read_sbs(xml.as_bytes(), "math.sbs", &|_| None).unwrap(); + assert!(imported.warnings.is_empty(), "{:?}", imported.warnings); + let mut s = new_session(); s.preview_size = Some(64); s.pkg = imported.pkg; @@ -33,19 +35,30 @@ fn imported_expression_cooks_follows_inputs_and_survives_undo_and_save() { let mean = |s: &mut materialcraft_engine::Session| { let levels = s.graph().unwrap().nodes.iter().find(|node| node.def == "levels").unwrap().id; s.cook(); + assert!(s.cooked.nodes[&levels].error.is_none()); + s.cooked.image(levels, 0).unwrap().mean()[0] }; + assert!(mean(&mut s).abs() < 1e-5); + execute(&mut s, "graph.exposed.set", &json!({"id": "amount", "value": 0.5})).unwrap(); + assert!((mean(&mut s) - 1.0 / 3.0).abs() < 1e-5); + execute(&mut s, "edit.undo", &json!({})).unwrap(); + assert!(mean(&mut s).abs() < 1e-5); + execute(&mut s, "edit.redo", &json!({})).unwrap(); + assert!((mean(&mut s) - 1.0 / 3.0).abs() < 1e-5); + let saved = sbs::write_sbs(&s.pkg).unwrap(); s.pkg = sbs::read_sbs(&saved, "saved.sbs", &|_| None).unwrap().pkg; s.cache.lock().unwrap().clear(); + assert!((mean(&mut s) - 1.0 / 3.0).abs() < 1e-5); assert!(s.graph().unwrap().nodes.iter().find(|node| node.def == "levels").unwrap().functions.contains_key("in_low")); } diff --git a/crates/engine/tests/it/system_variables.rs b/crates/engine/tests/it/system_variables.rs index d47d129..1b6c918 100644 --- a/crates/engine/tests/it/system_variables.rs +++ b/crates/engine/tests/it/system_variables.rs @@ -139,17 +139,21 @@ fn pixel_ratio_and_tiling_inherit_and_preserve_native_relative_modes() { execute(&mut session, "graph.node.base", &json!({"pixelRatio": 0, "tiling": false, "pixelRatioMode": mode, "tilingMode": mode})).unwrap(); let bytes = write_sbs(&session.pkg).unwrap(); let read = read_sbs(&bytes, "inheritance.sbs", &|_| None).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); // SBS represents Inherit as a format value plus a relative mode let mut expected_base = session.pkg.graphs[0].node(pixel).unwrap().base.clone(); expected_base.format = OutputFormat::U16; expected_base.format_mode = ParameterInheritance::RelativeToInput; + assert_eq!(read.pkg.graphs[0].node(pixel).unwrap().base, expected_base); + let mut reopened = Session::new(read.pkg); for session in [&mut session, &mut reopened] { let cooked = session.cook(); + assert_eq!(cooked.nodes[&pixel].error, None); assert!((cooked.image(pixel, 0).unwrap().mean()[0] - expected).abs() < 1e-6, "{mode}"); } @@ -180,13 +184,16 @@ fn pixel_processor_default_format_preserves_input_precision_on_sbs_round_trip() package.graphs.push(graph); let mut original = Session::new(package); let before = original.cook(); + assert_eq!(before.nodes[&pixel].error, None); assert_eq!(before.nodes[&pixel].base.format, expected_format); assert_eq!(before.image(pixel, 0).unwrap().get(0, 0, false)[0], expected_value); let bytes = write_sbs(&original.pkg).unwrap(); let read = read_sbs(&bytes, "format-inheritance.sbs", &|_| None).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let mut reopened = Session::new(read.pkg); let after = reopened.cook(); @@ -273,7 +280,9 @@ fn nested_function_instances_use_the_processing_callers_variable_scope() { assert_eq!(cooked.nodes[&pixel].error, None); assert!(cooked.nodes[&value].error.as_deref().unwrap().contains("undefined variable `$pos`")); + let image = cooked.image(pixel, 0).unwrap(); + assert_eq!(&image.get(0, 0, false)[..2], &[0.03125; 2]); assert_eq!(&image.get(15, 15, false)[..2], &[0.96875; 2]); } diff --git a/crates/engine/tests/package_resources.rs b/crates/engine/tests/package_resources.rs index e7d7f62..3c37530 100644 --- a/crates/engine/tests/package_resources.rs +++ b/crates/engine/tests/package_resources.rs @@ -21,10 +21,13 @@ fn byte_only_sbs_import_resolves_library_resource_substitutes() { let font_file = if fallback.is_ok() { "LiberationMono-Regular.ttf" } else { "cour.ttf" }; for (name, filename) in [("noise", "bluenoise_256_c.exr"), ("font", font_file)] { let resource = session.pkg.resource(name).unwrap(); + assert_eq!(resource.file, filename); assert_eq!(resource.bytes.as_slice(), materialcraft_engine::formats::sbs::library::read(&format!("sbs://resources/{filename}")).unwrap_or_default()); } + let saved = String::from_utf8(file_cmds::save_bytes(&session).unwrap()).unwrap(); + assert!(!saved.contains(".surface")); assert_eq!(saved.contains("old/cour.ttf"), fallback.is_err()); } diff --git a/crates/eval/src/cache.rs b/crates/eval/src/cache.rs index e9fe735..2621a8f 100644 --- a/crates/eval/src/cache.rs +++ b/crates/eval/src/cache.rs @@ -2,6 +2,9 @@ use crate::NodeOutput; use std::collections::HashMap; use std::sync::Arc; +const DEFAULT_LIMIT_BYTES: usize = 256 << 20; +const MAX_ENTRIES: usize = 8192; + /// LRU cache of node outputs, bounded by bytes pub struct Cache { entries: HashMap>, usize, u64)>, @@ -17,8 +20,8 @@ mod tests; impl Default for Cache { fn default() -> Self { - // Active graph results are retained separately; history must not reserve a gigabyte. - Cache::new(256 << 20) + // Active graph results are retained separately; history must not reserve a gigabyte + Cache::new(DEFAULT_LIMIT_BYTES) } } @@ -47,31 +50,34 @@ impl Cache { pub(crate) fn get(&mut self, key: u64) -> Option>> { self.tick = self.tick.saturating_add(1); - let t = self.tick; - self.entries.get_mut(&key).map(|e| { - e.2 = t; - e.0.clone() + let tick = self.tick; + self.entries.get_mut(&key).map(|entry| { + entry.2 = tick; + entry.0.clone() }) } - pub(crate) fn put(&mut self, key: u64, v: Arc>) { - let size = v.iter().fold(0_usize, |total, output| total.saturating_add(output.memory_bytes())); + pub(crate) fn put(&mut self, key: u64, outputs: Arc>) { + let size = outputs.iter().fold(0_usize, |total, output| total.saturating_add(output.memory_bytes())); if let Some(old) = self.entries.remove(&key) { self.bytes = self.bytes.saturating_sub(old.1); } - // An oversized result remains owned by its cook, never by this cache. Empty results - // are not worth retaining and otherwise permit unbounded zero-byte metadata. + + // Oversized results remain owned by their cooks + // Empty results would permit unbounded zero-byte cache metadata if size == 0 || size > self.cap { return; } - while self.bytes > self.cap - size || self.entries.len() >= 8192 { - let Some((&k, _)) = self.entries.iter().min_by_key(|(_, e)| e.2) else { break }; - if let Some(e) = self.entries.remove(&k) { - self.bytes = self.bytes.saturating_sub(e.1); + + while self.bytes > self.cap - size || self.entries.len() >= MAX_ENTRIES { + let Some((&oldest_key, _)) = self.entries.iter().min_by_key(|(_, entry)| entry.2) else { break }; + if let Some(entry) = self.entries.remove(&oldest_key) { + self.bytes = self.bytes.saturating_sub(entry.1); } } + self.tick = self.tick.saturating_add(1); - self.entries.insert(key, (v, size, self.tick)); + self.entries.insert(key, (outputs, size, self.tick)); self.bytes += size; } } diff --git a/crates/eval/src/cache.test.rs b/crates/eval/src/cache.test.rs index 1a03c0d..bbbf4a3 100644 --- a/crates/eval/src/cache.test.rs +++ b/crates/eval/src/cache.test.rs @@ -12,6 +12,7 @@ fn oversized_results_are_not_retained_or_allowed_to_flush_useful_entries() { let large = output(8); let weak = Arc::downgrade(&large); cache.put(2, large); + assert!(weak.upgrade().is_none()); assert!(cache.get(1).is_some()); assert!(cache.get(2).is_none()); @@ -22,10 +23,13 @@ fn oversized_results_are_not_retained_or_allowed_to_flush_useful_entries() { fn disabled_and_empty_caches_do_not_retain_outputs_or_metadata() { let mut cache = Cache::new(0); cache.put(1, output(4)); + assert!(cache.is_empty()); + for key in 0..10_000 { cache.put(key, Arc::new(Vec::new())); } + assert!(cache.is_empty()); } @@ -36,17 +40,25 @@ fn repeated_edits_release_evicted_buffers_and_keep_recent_hits() { let first_weak = Arc::downgrade(&first); cache.put(0, first); cache.put(1, output(4)); + assert!(cache.get(0).is_some()); + cache.put(2, output(4)); + assert!(cache.get(1).is_none()); assert!(first_weak.upgrade().is_some()); + for key in 3..1_000 { cache.put(key, output(4)); + assert!(cache.bytes() <= 128); assert!(cache.len() <= 2); } + assert!(first_weak.upgrade().is_none()); + cache.clear(); + assert_eq!(cache.bytes(), 0); assert!(cache.is_empty()); } diff --git a/crates/eval/src/cache_key.rs b/crates/eval/src/cache_key.rs index 160065d..f910960 100644 --- a/crates/eval/src/cache_key.rs +++ b/crates/eval/src/cache_key.rs @@ -5,83 +5,92 @@ use std::collections::{BTreeMap, HashSet}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::sync::{Arc, Mutex}; -pub(crate) fn image_key(i: &Image, cache: &Mutex) -> u64 { - let ptr = Arc::as_ptr(&i.data) as usize; - let shape = (i.w, i.h, i.kind.channels()); +const MAX_INPUT_FINGERPRINTS: usize = 128; + +pub(crate) fn image_key(image: &Image, cache: &Mutex) -> u64 { + let buffer_id = Arc::as_ptr(&image.data) as usize; + let shape = (image.w, image.h, image.kind.channels()); if let Some(key) = cache .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .input_hashes - .get(&ptr) - .and_then(|saved| saved.buffer.upgrade().filter(|data| Arc::ptr_eq(data, &i.data) && saved.shape == shape).map(|_| saved.key)) + .get(&buffer_id) + .and_then(|saved| saved.buffer.upgrade().filter(|data| Arc::ptr_eq(data, &image.data) && saved.shape == shape).map(|_| saved.key)) { - return key ^ hash_of(&i.integer_sampling); + return key ^ hash_of(&image.integer_sampling); } - let mut h = DefaultHasher::new(); - (shape, i.data.len()).hash(&mut h); - for value in i.data.iter() { - value.to_bits().hash(&mut h); + + let mut hasher = DefaultHasher::new(); + (shape, image.data.len()).hash(&mut hasher); + for value in image.data.iter() { + value.to_bits().hash(&mut hasher); } - let key = h.finish(); + + let key = hasher.finish(); let mut cache = cache.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - if cache.input_hashes.len() >= 128 { + if cache.input_hashes.len() >= MAX_INPUT_FINGERPRINTS { cache.input_hashes.retain(|_, saved| saved.buffer.strong_count() > 0); - if cache.input_hashes.len() >= 128 { + if cache.input_hashes.len() >= MAX_INPUT_FINGERPRINTS { cache.input_hashes.clear(); } } - cache.input_hashes.insert(ptr, crate::input_fingerprint::InputFingerprint { buffer: Arc::downgrade(&i.data), shape, key }); - key ^ hash_of(&i.integer_sampling) + + cache.input_hashes.insert(buffer_id, crate::input_fingerprint::InputFingerprint { buffer: Arc::downgrade(&image.data), shape, key }); + key ^ hash_of(&image.integer_sampling) } -pub(crate) fn hash_of(v: &impl Hash) -> u64 { - let mut h = DefaultHasher::new(); - v.hash(&mut h); - h.finish() +pub(crate) fn hash_of(value: &impl Hash) -> u64 { + let mut hasher = DefaultHasher::new(); + value.hash(&mut hasher); + hasher.finish() } #[cfg(test)] -pub(crate) fn graph_tree_hash(pkg: &Package, gi: usize) -> u64 { - graph_tree_hash_with_registry(pkg, pkg, gi) +pub(crate) fn graph_tree_hash(package: &Package, graph_index: usize) -> u64 { + graph_tree_hash_with_registry(package, package, graph_index) } -pub(crate) fn graph_tree_hash_with_registry(registry: &Package, pkg: &Package, gi: usize) -> u64 { - let mut h = DefaultHasher::new(); - let mut pending = vec![(pkg, gi)]; +pub(crate) fn graph_tree_hash_with_registry(registry: &Package, package: &Package, graph_index: usize) -> u64 { + let mut hasher = DefaultHasher::new(); + let mut pending = vec![(package, graph_index)]; let mut seen = HashSet::new(); - while let Some((pkg, index)) = pending.pop() { - if !seen.insert((std::ptr::from_ref(pkg) as usize, index)) { + while let Some((package, index)) = pending.pop() { + if !seen.insert((std::ptr::from_ref(package) as usize, index)) { continue; } - let Some(graph) = pkg.graphs.get(index) else { continue }; - index.hash(&mut h); - serde_json::to_string(graph).unwrap_or_default().hash(&mut h); - for (id, res) in &graph.input_images { - pkg.resource(res).map(|r| r.bytes.content_hash()).hash(&mut h); - id.hash(&mut h); - graph.input_images_linear.contains(id).hash(&mut h); + let Some(graph) = package.graphs.get(index) else { continue }; + + index.hash(&mut hasher); + serde_json::to_string(graph).unwrap_or_default().hash(&mut hasher); + for (id, resource_name) in &graph.input_images { + package.resource(resource_name).map(|resource| resource.bytes.content_hash()).hash(&mut hasher); + id.hash(&mut hasher); + graph.input_images_linear.contains(id).hash(&mut hasher); } + for instance in graph.functions().flat_map(|function| &function.nodes).filter_map(|node| node.instance.as_ref()) { - let source = if instance.package.is_empty() { Some(pkg) } else { registry.dependencies.get(&instance.package).map(AsRef::as_ref) }; + let source = if instance.package.is_empty() { Some(package) } else { registry.dependencies.get(&instance.package).map(AsRef::as_ref) }; if let Some(source) = source && let Some((index, _)) = source.graph_by_name(&instance.graph) { pending.push((source, index)); } } - for n in &graph.nodes { - if let Some(Value::Text(name)) = n.params.get("resource") { - pkg.resource(name).map(|r| r.bytes.content_hash()).hash(&mut h); + + for node in &graph.nodes { + if let Some(Value::Text(name)) = node.params.get("resource") { + package.resource(name).map(|resource| resource.bytes.content_hash()).hash(&mut hasher); } - if n.def == "graph_instance" - && let Some((source, index, _)) = crate::graph_instance::resolve(registry, pkg, n) + if node.def == "graph_instance" + && let Some((source, index, _)) = crate::graph_instance::resolve(registry, package, node) { pending.push((source, index)); } } } - h.finish() + + hasher.finish() } pub(crate) fn variable_key(node: &Node, parameters: &BTreeMap, variables: &BTreeMap) -> u64 { diff --git a/crates/eval/src/node_execution.rs b/crates/eval/src/node_execution.rs index dd4f181..4a098b4 100644 --- a/crates/eval/src/node_execution.rs +++ b/crates/eval/src/node_execution.rs @@ -75,6 +75,7 @@ pub(crate) fn cook_node( if output.as_ref().is_some_and(|output| output.as_value().is_some()) && !matches!(node.def.as_str(), "output" | "dot") { input_error = Some(format!("input `{}` requires an image", port.id)); } + inputs.push(output.and_then(NodeOutput::as_image).cloned()); } @@ -86,6 +87,7 @@ pub(crate) fn cook_node( Ok((base, values)) => (base, values, None), Err(error) => (crate::ResolvedBaseParameters::resolve(&node.base, input_base, prepared.parent_base), BTreeMap::new(), Some(error)), }; + if node.disabled && let Some(input) = input_base { @@ -103,6 +105,7 @@ pub(crate) fn cook_node( key, cached: false, }; + if let Some(error) = base_error { return fail(error, 0); } @@ -173,8 +176,7 @@ pub(crate) fn cook_node( incoming.first().copied().flatten().cloned().map(|output| vec![output]).ok_or("missing value input".into()) } else if let Some(image) = input_image { let mut image = if node.def == "input_grayscale" && image.kind == Kind::Color { - // Grayscale input bridges use the default equal RGB weights, not - // display luminance. Preserve the fused order for float resources. + // Grayscale bridges use equal RGB weights and preserve fused float order let weight = 1.0 / 3.0; image.map(Kind::Gray, |pixel| [pixel[2].mul_add(weight, pixel[1].mul_add(weight, pixel[0] * weight)), 0.0, 0.0, 0.0]) } else if node.def == "input_color" { @@ -182,16 +184,19 @@ pub(crate) fn cook_node( } else { image.clone() }; - // Bound resources clamp at their edges and resize through stored factor-two levels. - // A single resize changes the edge filter; skipping storage conversion leaks HDR - // values through integer input bridges and changes subsequent function samples. + + // Bound resources clamp edges through stored factor-two levels + // Preserve resampling and storage conversion for HDR bridges + let resized = image.w != w || image.h != h; while image.w != w || image.h != h { let next = |current: u32, target: u32| { if current < target { current.saturating_mul(2).min(target) } else { current.div_ceil(2).max(target) } }; image = crate::output_precision::apply(image.resize(next(image.w, w), next(image.h, h), false), base.format); } - let image = crate::output_precision::apply(image, base.format); + + // Resized inputs already store the final level at the declared precision + let image = if resized { image } else { crate::output_precision::apply(image, base.format) }; Ok(vec![NodeOutput::Image(image)]) } else if node.def == materialcraft_core::MISSING_NODE { let name = if node.label.is_empty() { "this node" } else { node.label.as_str() }; @@ -205,7 +210,7 @@ pub(crate) fn cook_node( images .into_iter() .map(|image| { - // Bridges retain their source pixels; filters store their declared precision. + // Bridges retain their source pixels; filters store their declared precision let image = if matches!(node.def.as_str(), "output" | "dot") { image } else { crate::output_precision::apply(image, base.format) }; NodeOutput::Image(image) }) diff --git a/crates/eval/src/output_precision.rs b/crates/eval/src/output_precision.rs index 5b8d7d6..ec82772 100644 --- a/crates/eval/src/output_precision.rs +++ b/crates/eval/src/output_precision.rs @@ -3,31 +3,38 @@ use materialcraft_image::Image; use std::sync::Arc; pub(crate) fn apply(mut image: Image, format: OutputFormat) -> Image { + const HALF_FLOAT_MAX: f32 = 65504.0; + const U8_FIXED_SCALE: f64 = 4096.0; + const U16_FIXED_SCALE: f64 = 1_048_576.0; + image.integer_sampling = integer_sampling(format); if format == OutputFormat::F16 { for pixel in Arc::make_mut(&mut image.data) { - // Half-float storage saturates finite overflow and truncates toward zero. - // Leaving the f32 unconverted changes every downstream read and export. - let value = pixel.clamp(-65504.0, 65504.0); + // Half-float storage saturates finite overflow and truncates toward zero + // Leaving the f32 unconverted changes every downstream read and export + let value = pixel.clamp(-HALF_FLOAT_MAX, HALF_FLOAT_MAX); let rounded = half::f16::from_f32(value); *pixel = if rounded.to_f32().abs() > value.abs() { half::f16::from_bits(rounded.to_bits().saturating_sub(1)).to_f32() } else { rounded.to_f32() }; } + return image; } + let (fraction, maximum) = match format { - OutputFormat::U8 => (4096.0_f64, 255.0_f64), - OutputFormat::Inherit | OutputFormat::U16 => (1_048_576.0, 65535.0), + OutputFormat::U8 => (U8_FIXED_SCALE, f64::from(u8::MAX)), + OutputFormat::Inherit | OutputFormat::U16 => (U16_FIXED_SCALE, f64::from(u16::MAX)), OutputFormat::F16 | OutputFormat::F32 => return image, }; + for pixel in Arc::make_mut(&mut image.data) { - // Integer outputs retain four fractional guard bits before conversion. - // This truncation and halfway-down rounding are observable even for constants; - // rounding directly from the float changes noise and every downstream blend. + // Integer outputs retain four fractional guard bits before conversion + // Halfway-down rounding affects constants and downstream blends let value = if pixel.is_nan() { 0.0 } else { f64::from(*pixel).clamp(0.0, 1.0) }; let fixed = (value * fraction).floor() / fraction; let integer = (fixed * maximum - 0.5).ceil() as u32; *pixel = integer as f32 / maximum as f32; } + image } diff --git a/crates/eval/src/parameter.rs b/crates/eval/src/parameter.rs index b8ca2d1..f578411 100644 --- a/crates/eval/src/parameter.rs +++ b/crates/eval/src/parameter.rs @@ -18,6 +18,7 @@ pub(crate) fn resolve_parameters( parameters } }; + for parameter in node.bindings.keys().chain(node.functions.keys()) { if !parameters.contains_key(parameter) && let Some(descriptor) = ParameterDescriptor::for_node(package, node, parameter) diff --git a/crates/eval/src/parameter_function.rs b/crates/eval/src/parameter_function.rs index 32a04d0..e09ce2f 100644 --- a/crates/eval/src/parameter_function.rs +++ b/crates/eval/src/parameter_function.rs @@ -12,7 +12,8 @@ pub(crate) fn evaluate( ) -> Result { let program = compile(function, seed)?; let scalar_channel = allow_scalar && expected == ValueType::Float4 && program.output_type() == ValueType::Float as u32; - // A color parameter also takes a gray level or an opaque RGB triple. + + // A color parameter also takes a gray level or an opaque RGB triple let widened = expected == ValueType::Float4 && [ValueType::Float as u32, ValueType::Float3 as u32].contains(&program.output_type()); if program.output_type() != expected as u32 && !scalar_channel && !widened { return Err(format!("output must be {}", expected.label())); @@ -28,6 +29,7 @@ pub(crate) fn evaluate( Value::Float4(values) | Value::Color(values) => values.iter().all(|value| value.is_finite()), _ => true, }; + if !finite { return Err("function returned a non-finite value".into()); } diff --git a/crates/eval/src/resolved_base_parameters.rs b/crates/eval/src/resolved_base_parameters.rs index 9642149..a258846 100644 --- a/crates/eval/src/resolved_base_parameters.rs +++ b/crates/eval/src/resolved_base_parameters.rs @@ -27,12 +27,12 @@ impl ResolvedBaseParameters { pub(crate) fn resolve(base: &BaseParameters, input: Option, parent: Self) -> Self { let size_base = BaseParameter::OutputSize.inheritance(base).resolve([1; 2], input.map(|base| base.size), parent.size); - // Internal reductions and lookup images can reach one texel. Applying - // the graph editor's 16-pixel minimum here breaks their size functions. + + // Internal images may be one texel; the editor minimum would break size functions let size = std::array::from_fn(|axis| (f64::from(size_base[axis]) * 2f64.powi(base.size[axis].clamp(-12, 13))).round().clamp(1.0, 8192.0) as u32); - // Images carry size and format, but do not carry a random stream. - // Both relative seed modes use the containing graph's seed; chaining - // image filters must not accumulate the seeds of upstream nodes. + + // Images carry size and format, but do not carry a random stream + // Relative seed modes use the graph seed without accumulating upstream seeds let inherited_seed = base.seed_mode.resolve(0, None, parent.seed); let inherited_pixel_size = base.pixel_size_mode.resolve([1.0; 2], input.map(|base| base.pixel_size), parent.pixel_size); Self { @@ -96,6 +96,7 @@ pub(crate) fn evaluate( } else { resolved.size }; + let value = crate::parameter_function::evaluate(function, parameter.value_type(), variables.to_mut(), resolved.seed, size, false) .map_err(|error| format!("function of `{}`: {error}", parameter.identifier()))?; parameter.set_value(&mut evaluated, &value)?; diff --git a/crates/eval/src/tests.rs b/crates/eval/src/tests.rs index 9b38fc0..3f6eb6a 100644 --- a/crates/eval/src/tests.rs +++ b/crates/eval/src/tests.rs @@ -30,14 +30,19 @@ fn cooks_and_caches() { let cache = Mutex::new(Cache::default()); let opts = Options::new(&no_res); let a = cook(&p, 0, &opts, &cache); + assert!(a.nodes.values().all(|r| r.error.is_none() && !r.cached)); assert_eq!(a.image(out, 0).map(|i| (i.w, i.h)), Some((64, 64))); + // Same graph: everything from the cache let b = cook(&p, 0, &opts, &cache); + assert!(b.nodes.values().all(|r| r.cached)); + // Editing the blur re-cooks the blur and the output, not the noise p.graphs[0].node_mut(blur).unwrap().params.insert("intensity".into(), Value::Float(5.0)); let c = cook(&p, 0, &opts, &cache); + assert!(c.nodes[&noise].cached && !c.nodes[&blur].cached && !c.nodes[&out].cached); assert_eq!(outputs(&p.graphs[0], &c).len(), 1); } @@ -70,13 +75,15 @@ fn processor_tiling_variable_tracks_both_axes_and_invalidates_cached_values() { #[test] fn base_parameter_assignments_reach_pixel_functions_without_leaking_to_other_nodes() { use materialcraft_core::{BaseParameter, FunctionGraph}; - let mut init = FunctionGraph::default(); - let value = init.add_constant(Value::Float(0.375)); - let assign = init.add("set", &[value], Some(Value::Text("prepared_value".into()))); - let size = init.add_constant(Value::Int2([0; 2])); - init.output = Some(init.add("sequence", &[assign, size], None)); + + let mut initialization = FunctionGraph::default(); + let value = initialization.add_constant(Value::Float(0.375)); + let assignment = initialization.add("set", &[value], Some(Value::Text("prepared_value".into()))); + let output_size = initialization.add_constant(Value::Int2([0; 2])); + initialization.output = Some(initialization.add("sequence", &[assignment, output_size], None)); let mut function = FunctionGraph::default(); function.output = Some(function.add("get_float1", &[], Some(Value::Text("prepared_value".into())))); + let mut graph = Graph::new("Parameter scope"); graph.size = [4; 2]; graph.format = materialcraft_core::OutputFormat::F32; @@ -86,18 +93,23 @@ fn base_parameter_assignments_reach_pixel_functions_without_leaking_to_other_nod graph.node_mut(id).unwrap().params.insert("function".into(), Value::Function(Box::new(function.clone()))); graph.node_mut(id).unwrap().params.insert("color_mode".into(), Value::Bool(false)); } - graph.node_mut(initialized).unwrap().base.functions.insert(BaseParameter::OutputSize, init); + graph.node_mut(initialized).unwrap().base.functions.insert(BaseParameter::OutputSize, initialization); let mut package = Package::new("Parameter scope"); package.graphs.push(graph); let cache = Mutex::new(Cache::default()); for expected in [0.375, 0.625] { package.graphs[0].node_mut(initialized).unwrap().base.functions.get_mut(&BaseParameter::OutputSize).unwrap().nodes[0].value = Some(Value::Float(expected)); + let cooked = cook(&package, 0, &Options::new(&no_res), &cache); + assert_eq!(cooked.nodes[&initialized].error, None); assert!(!cooked.nodes[&initialized].cached); assert!((cooked.image(initialized, 0).unwrap().mean()[0] - expected).abs() < 1e-6); - assert!(cooked.nodes[&isolated].error.as_deref().unwrap().contains("undefined variable `prepared_value`")); + + // Unassigned user variables start at typed zero in each node's own scope + assert_eq!(cooked.nodes[&isolated].error, None); + assert_eq!(cooked.image(isolated, 0).unwrap().mean()[0], 0.0); } } @@ -129,6 +141,7 @@ fn preview_cap_and_unknown_nodes() { #[test] fn graph_instances_cook_their_subgraph() { let (mut p, ..) = sample(); + // A second graph: input → invert → output, instanced from the first let mut sub = Graph::new("inv"); sub.size = [6, 6]; @@ -147,12 +160,14 @@ fn graph_instances_cook_their_subgraph() { let inst = g.add_node("graph_instance", [100.0, 100.0]); g.node_mut(inst).unwrap().params.insert("graph".into(), Value::Text("inv".into())); g.connect(u, "output", inst, "input").unwrap(); + // A graph instancing itself is refused, not a stack overflow let selfi = g.add_node("graph_instance", [0.0, 200.0]); g.node_mut(selfi).unwrap().params.insert("graph".into(), Value::Text("g".into())); let cache = Mutex::new(Cache::default()); let c = cook(&p, 0, &Options::new(&no_res), &cache); let img = c.image(inst, 0).unwrap(); + assert!((img.data[0] - (1.0 - 0.0722 * 0.25 - 0.2126 * 0.25 - 0.7152 * 0.25)).abs() < 1e-4, "{}", img.data[0]); assert!(c.nodes[&selfi].error.as_deref().is_some_and(|e| e.contains("uses itself")), "{:?}", c.nodes[&selfi].error); } @@ -200,6 +215,7 @@ fn deep_shared_instance_walks_stay_bounded() { let mut pkg = Package::new("deep"); for index in 0..2000 { let mut graph = Graph::new(&format!("g{index}")); + // Repeated references must visit the next graph only once for _ in 0..4 { let id = graph.add_node("graph_instance", [0.0; 2]); @@ -207,13 +223,18 @@ fn deep_shared_instance_walks_stay_bounded() { } pkg.graphs.push(graph); } + assert!(instances_reach(&pkg, 0, 1999)); assert!(!instances_reach(&pkg, 0, 2000)); + let before = graph_tree_hash(&pkg, 0); pkg.graphs[1999].seed = 42; + assert_ne!(before, graph_tree_hash(&pkg, 0)); + // A back edge must terminate both traversals too pkg.graphs[1999].nodes[0].params.insert("graph".into(), Value::Text("g0".into())); + assert!(instances_reach(&pkg, 1, 0)); assert!(!instances_reach(&pkg, 0, 2000)); assert_ne!(before, graph_tree_hash(&pkg, 0)); @@ -245,6 +266,7 @@ fn same_size_resources_key_differently() { let mut p = Package::new("p"); p.graphs.push(g); p.resources.push(Resource::new("tex", "tex.bin", ResourceBytes::new(vec![1; 100]))); + // Stands in for decoding: the first byte picks the shade let shade = AtomicU8::new(1); let res = |_: &str| Some(Image::filled(4, 4, Kind::Gray, [f32::from(shade.load(Ordering::Relaxed)) / 10.0; 4])); @@ -255,6 +277,7 @@ fn same_size_resources_key_differently() { p.resources[0].bytes = ResourceBytes::new(vec![2; 100]); shade.store(2, Ordering::Relaxed); let second = cook(&p, 0, &opts, &cache); + assert!(!second.nodes[&bm].cached); assert!((second.image(bm, 0).unwrap().mean()[0] - mean).abs() > 0.05); } @@ -274,11 +297,14 @@ fn input_nodes_read_bound_preview_images() { let cache = Mutex::new(Cache::default()); let c = cook(&p, 0, &Options::new(&res), &cache); let img = c.image(o, 0).unwrap(); + assert!(img.is_gray() && (img.data[0] - 0.8).abs() < 1e-4, "bound image, as gray"); + // The caller's image wins over the bound one let mut opts = Options::new(&res); opts.inputs.insert("curvature".into(), Image::filled(4, 4, Kind::Gray, [0.1; 4])); let c = cook(&p, 0, &opts, &cache); + assert!((c.image(o, 0).unwrap().data[0] - 0.1).abs() < 1e-4); } @@ -414,11 +440,12 @@ fn changed_unsampled_input_recooks() { } #[test] fn linear_input_images_and_bitmap_nodes_request_data_reads() { - // The decoder returns 0.5 for a linear request and 0.2 for an sRGB one. + // The decoder returns 0.5 for a linear request and 0.2 for an sRGB one let res = |k: &str| { let (name, linear) = materialcraft_image::split_resource_key(k); (name == "m").then(|| Image::filled(4, 4, Kind::Color, [if linear { 0.5 } else { 0.2 }; 4])) }; + let mut p = Package::new("p"); let mut g = Graph::new("g"); g.size = [4, 4]; @@ -434,9 +461,12 @@ fn linear_input_images_and_bitmap_nodes_request_data_reads() { let c = cook(p, 0, &Options::new(&res), &cache); (c.image(i, 0).unwrap().data[0], c.image(b, 0).unwrap().data[0]) }; + assert_eq!(first(&p), (0.2, 0.2), "default is sRGB, as before"); + p.graphs[0].input_images_linear.insert("n".into()); p.graphs[0].node_mut(b).unwrap().params.insert("colour_space".into(), Value::Enum(1)); + assert_eq!(first(&p), (0.5, 0.5)); } @@ -455,19 +485,25 @@ fn bound_input_image_resource_change_recooks_instance() { let mut p = Package::new("p"); p.graphs = vec![top, sub]; p.resources.push(Resource::new("curv", "curv.bin", ResourceBytes::new(vec![1; 100]))); - // Stands in for decoding: the first byte picks the shade. + + // Stands in for decoding: the first byte picks the shade let shade = AtomicU8::new(1); let res = |_: &str| Some(Image::filled(4, 4, Kind::Gray, [f32::from(shade.load(Ordering::Relaxed)) / 10.0; 4])); let cache = Mutex::new(Cache::default()); let opts = Options::new(&res); let first = cook(&p, 0, &opts, &cache); + assert!(first.nodes[&inst].error.is_none(), "{:?}", first.nodes[&inst].error); assert!(!first.nodes[&inst].cached); + let unchanged = cook(&p, 0, &opts, &cache); + assert!(unchanged.nodes[&inst].cached, "an unchanged instance should be served from the cache"); + p.resources[0].bytes = ResourceBytes::new(vec![2; 100]); shade.store(2, Ordering::Relaxed); let second = cook(&p, 0, &opts, &cache); + assert!(!second.nodes[&inst].cached, "changing the bound input image resource must recook the instance"); } @@ -590,6 +626,7 @@ fn enabled_generators_use_parent_size_and_seed_with_input_image_properties() { assert_eq!(result.base.tiling, [false, true]); assert_eq!(result.base.pixel_ratio, 1); assert_eq!(result.base.pixel_size, [0.25, 0.5]); + if !dynamic { assert_eq!( node_size(&package.graphs[0], materialcraft_nodes::find(definition), package.graphs[0].node(generator).unwrap(), Some((32, 64)), Some(16)), @@ -631,6 +668,7 @@ fn variables_set_by_a_base_parameter_function_reach_the_nodes_other_functions() let cooked = cook(&package, 0, &Options::new(&no_res), &Mutex::new(Cache::default())); let result = &cooked.nodes[&generator]; + assert_eq!(result.error, None); assert_eq!(result.parameters.get("roughness"), Some(&Value::Float(2.0))); } diff --git a/crates/formats/build.rs b/crates/formats/build.rs index 7d9cabd..db8b796 100644 --- a/crates/formats/build.rs +++ b/crates/formats/build.rs @@ -8,13 +8,18 @@ use std::{ }; type Result = std::result::Result>; +const MAX_DIRECTORY_DEPTH: usize = 6; +const MAX_BUNDLED_FILES: usize = 4096; +const MAX_ASSET_BYTES: usize = 256 << 20; + #[path = "build/sbs_font.rs"] mod sbs_font; fn scan(root: &Path, folder: &Path, files: &mut Vec) -> Result<()> { - if folder.components().count() > 6 || files.len() > 4096 { + if folder.components().count() > MAX_DIRECTORY_DEPTH || files.len() > MAX_BUNDLED_FILES { return Err("bundled library exceeds its scan limits".into()); } + let path = root.join(folder); println!("cargo:rerun-if-changed={}", path.display()); for entry in std::fs::read_dir(path)? { @@ -24,19 +29,22 @@ fn scan(root: &Path, folder: &Path, files: &mut Vec) -> Result<()> { if name.starts_with('.') { continue; } + let kind = entry.file_type()?; let relative = folder.join(name); if kind.is_dir() { scan(root, &relative, files)?; } else if kind.is_file() && (name.ends_with(".sbs") || relative.starts_with("resources")) { - if files.len() >= 4096 { + if files.len() >= MAX_BUNDLED_FILES { return Err("bundled library exceeds its file limit".into()); } + files.push(relative); } else if kind.is_symlink() { return Err("symlinks are not bundled library assets".into()); } } + Ok(()) } @@ -54,10 +62,11 @@ fn main() -> Result<()> { let mut packages = String::from("pub const BUNDLED: &[&str] = &[\n"); for (index, (relative, file)) in files.iter().enumerate() { let mut bytes = Vec::new(); - std::fs::File::open(file)?.take((256 << 20) + 1).read_to_end(&mut bytes)?; - if bytes.len() > 256 << 20 { + std::fs::File::open(file)?.take(MAX_ASSET_BYTES as u64 + 1).read_to_end(&mut bytes)?; + if bytes.len() > MAX_ASSET_BYTES { return Err("bundled asset exceeds 256 MiB".into()); } + let mut encoder = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::best()); encoder.write_all(&bytes)?; std::fs::write(compressed.join(format!("{index}.deflate")), encoder.finish()?)?; diff --git a/crates/formats/build/sbs_font.rs b/crates/formats/build/sbs_font.rs index f8c6b66..d376d78 100644 --- a/crates/formats/build/sbs_font.rs +++ b/crates/formats/build/sbs_font.rs @@ -7,24 +7,31 @@ use std::{ type Result = std::result::Result>; +const MAX_MANIFEST_BYTES: u64 = 1 << 20; +const MAX_FONT_BYTES: u64 = 16 << 20; +const MAX_LICENSE_BYTES: u64 = 64 << 10; + fn read(path: &Path, limit: u64) -> Result> { let mut bytes = Vec::new(); std::fs::File::open(path)?.take(limit + 1).read_to_end(&mut bytes)?; if bytes.is_empty() || bytes.len() as u64 > limit { return Err(format!("{} is empty or exceeds its size limit", path.display()).into()); } + Ok(bytes) } fn checkout_file(root: &Path, relative: &str) -> Result { - // A manifest must not embed arbitrary files from the build host, including through symlinks. + // A manifest must not embed arbitrary files from the build host, including through symlinks if relative.is_empty() || relative.contains(['\\', ':']) || !Path::new(relative).components().all(|part| matches!(part, Component::Normal(_))) { return Err("font manifest path is not checkout-relative".into()); } + let path = root.join(relative).canonicalize()?; if !path.starts_with(root) { return Err("font manifest path escapes the checkout".into()); } + println!("cargo:rerun-if-changed={}", path.display()); Ok(path) } @@ -32,7 +39,7 @@ fn checkout_file(root: &Path, relative: &str) -> Result { fn select(directory: &Path) -> Result> { let root = directory.canonicalize()?; let manifest = checkout_file(&root, "fonts/latin-manifest.txt")?; - let bytes = read(&manifest, 1 << 20)?; + let bytes = read(&manifest, MAX_MANIFEST_BYTES)?; let entries = craft_fonts_build::parse_manifest(std::str::from_utf8(&bytes)?)?; let face = entries .iter() @@ -41,13 +48,15 @@ fn select(directory: &Path) -> Result> { if face.licence != "OFL-1.1" || face.index != 0 { return Err("the SBS fallback must be the OFL-1.1 Liberation Mono Regular single font".into()); } + let font = checkout_file(&root, &face.file)?; - let bytes = read(&font, 16 << 20)?; + let bytes = read(&font, MAX_FONT_BYTES)?; if !bytes.starts_with(b"\0\x01\0\0") || format!("{:x}", Sha256::digest(&bytes)) != face.sha256 { return Err("Liberation Mono Regular does not match its catalog checksum or TrueType format".into()); } + let license = checkout_file(&root, &face.licence_file)?; - read(&license, 64 << 10)?; + read(&license, MAX_LICENSE_BYTES)?; Ok(vec![("resources/LiberationMono-Regular.ttf".into(), font), ("resources/OFL-LiberationMono.txt".into(), license)]) } @@ -61,6 +70,7 @@ pub fn assets() -> Result> { None if required => Err("CRAFT_FONTS_REQUIRED=1 requires CRAFT_FONTS_DIR for the SBS fallback font".into()), None => return Ok(Vec::new()), }; + match selected { Ok(assets) => Ok(assets), Err(error) if required => Err(error), diff --git a/crates/formats/examples/sbs_library_build/convert.rs b/crates/formats/examples/sbs_library_build/convert.rs index 62357d9..ed02479 100644 --- a/crates/formats/examples/sbs_library_build/convert.rs +++ b/crates/formats/examples/sbs_library_build/convert.rs @@ -1,16 +1,7 @@ -use super::{ - Result, layout, resources, - xml::{self, Edit}, -}; +use super::{Result, converted_package::ConvertedPackage, layout, resources, xml, xml_edit::XmlEdit}; use roxmltree::Node; use std::collections::{BTreeMap, BTreeSet}; -pub struct Converted { - pub xml: String, - pub resources: Vec<&'static str>, - pub report: serde_json::Value, -} - fn presentation(node: Node<'_, '_>) -> bool { matches!(node.tag_name().name(), "GUILayout" | "GUIObjects" | "GUIName" | "desc") || (node.parent().is_some_and(|p| p.has_tag_name("attributes")) @@ -29,12 +20,14 @@ pub fn semantics(document: &roxmltree::Document<'_>) -> String { if node.ancestors().any(|n| presentation(n) || replaced_resource_field(n)) { continue; } + if node.has_tag_name("fileUID") || (matches!(node.tag_name().name(), "formatVersion" | "updaterVersion") && node.parent().is_some_and(|p| p.has_tag_name("package"))) || (node.has_tag_name("filename") && node.parent().is_some_and(|p| p.has_tag_name("dependency"))) { continue; } + if node.is_element() && !node.has_tag_name("attributes") { { result.push_str(&node.ancestors().filter(|n| !n.has_tag_name("attributes")).count().to_string()); @@ -53,6 +46,7 @@ pub fn semantics(document: &roxmltree::Document<'_>) -> String { result.push_str(node.text().unwrap_or("")); } } + result } @@ -95,20 +89,22 @@ fn attributes(owner: Node<'_, '_>, text: &str, description: &str) -> Result) -> Result { +pub fn package(text: &str, name: &str, identities: &BTreeMap) -> Result { let document = xml::parse(text)?; let root = document.root_element(); if !root.has_tag_name("package") { return Err("expected a native SBS package".into()); } + let before = semantics(&document); let mut edits = Vec::new(); for tag in ["formatVersion", "updaterVersion"] { if xml::child(root, tag).is_none() { let offset = root.first_element_child().ok_or("package has no content")?.range().start; - edits.push(Edit { range: offset..offset, replacement: xml::leaf(tag, env!("CARGO_PKG_VERSION")) }); + edits.push(XmlEdit { range: offset..offset, replacement: xml::leaf(tag, env!("CARGO_PKG_VERSION")) }); } } + let mut resource_keys = Vec::new(); let mut graphs = 0; let mut functions = 0; @@ -119,6 +115,7 @@ pub fn package(text: &str, name: &str, identities: &BTreeMap) -> if node.ancestors().skip(1).any(|n| n.has_tag_name("GUIObjects") || n.has_tag_name("resource") || n.has_tag_name("attributes")) { continue; } + match node.tag_name().name() { "graph" => graphs += 1, "function" if node.parent().is_some_and(|p| p.has_tag_name("content")) => functions += 1, @@ -128,9 +125,9 @@ pub fn package(text: &str, name: &str, identities: &BTreeMap) -> } "attributes" => { let owner = node.parent().ok_or("attribute owner is missing")?; - edits.push(Edit { range: node.range(), replacement: attributes(owner, text, &description(owner))? }); + edits.push(XmlEdit { range: node.range(), replacement: attributes(owner, text, &description(owner))? }); } - "GUIObjects" => edits.push(Edit { range: node.range(), replacement: String::new() }), + "GUIObjects" => edits.push(XmlEdit { range: node.range(), replacement: String::new() }), "GUIName" => xml::set(&mut edits, node, ""), "desc" => { let owner = node.parent().ok_or("description owner is missing")?; @@ -169,13 +166,15 @@ pub fn package(text: &str, name: &str, identities: &BTreeMap) -> if !font_replacement && std::path::Path::new(original).file_stem() != output.file_stem() { return Err(format!("{name}: resource `{key}` would change its filename stem").into()); } + let extension = std::path::Path::new(original).extension(); - // Surface inputs are replaced with a standard image container; - // the requested font substitute is the only filename-stem exception. + + // Only the requested font substitute may change the filename stem if !font_replacement && extension != Some(std::ffi::OsStr::new("surface")) && extension != output.extension() { return Err(format!("{name}: resource `{key}` would change its extension").into()); } } + let mut replacement = String::from(""); for child in node.children().filter(Node::is_element) { if !matches!(child.tag_name().name(), "source" | "filepath" | "format" | "attributes") { @@ -186,21 +185,24 @@ pub fn package(text: &str, name: &str, identities: &BTreeMap) -> replacement.push_str(&xml::leaf("filepath", &xml::relative(name, recipe.path)?)); replacement.push_str(&attributes(node, text, recipe.description)?); replacement.push_str(""); - edits.push(Edit { range: node.range(), replacement }); + edits.push(XmlEdit { range: node.range(), replacement }); resource_keys.push(recipe.key); } _ => {} } } + let output = xml::apply(text, edits)?; let after = xml::parse(&output)?; if semantics(&after) != before { return Err(format!("{name}: conversion changed native computational data").into()); } + if after.descendants().any(|n| n.has_tag_name("binembedded") || n.has_tag_name("icon")) { return Err(format!("{name}: source asset payload survived conversion").into()); } - Ok(Converted { + + Ok(ConvertedPackage { xml: output, resources: resource_keys, report: serde_json::json!({ @@ -218,21 +220,26 @@ pub fn verify_references(text: &str, owner: &str, available: &BTreeSet) if !is_dependency && !is_resource { continue; } + let reference = node.attribute("v").ok_or("missing reference value")?; if reference == "?himself" { continue; } + if reference.contains("://") { return Err("converted references must be package-relative".into()); } + let resolved = materialcraft_formats::sbs::library::resolve(&format!("sbs://{owner}"), reference)?; let relative = materialcraft_formats::sbs::library::relative_path(&resolved)?; if !available.contains(relative) { return Err(format!("{owner}: reference does not resolve: {reference}").into()); } + if is_resource && !relative.starts_with("resources/") { return Err("resource is outside the shared resources folder".into()); } } + Ok(()) } diff --git a/crates/formats/examples/sbs_library_build/converted_package.rs b/crates/formats/examples/sbs_library_build/converted_package.rs new file mode 100644 index 0000000..5a7fc07 --- /dev/null +++ b/crates/formats/examples/sbs_library_build/converted_package.rs @@ -0,0 +1,5 @@ +pub struct ConvertedPackage { + pub xml: String, + pub resources: Vec<&'static str>, + pub report: serde_json::Value, +} diff --git a/crates/formats/examples/sbs_library_build/layout.rs b/crates/formats/examples/sbs_library_build/layout.rs index 4f0d1c0..b2dadaa 100644 --- a/crates/formats/examples/sbs_library_build/layout.rs +++ b/crates/formats/examples/sbs_library_build/layout.rs @@ -1,15 +1,13 @@ -use super::{ - Result, - xml::{self, Edit}, -}; +use super::{Result, xml, xml_edit::XmlEdit}; use roxmltree::Node; use std::collections::{BTreeMap, BTreeSet}; -pub fn reposition(list: Node<'_, '_>, edits: &mut Vec) -> Result { +pub fn reposition(list: Node<'_, '_>, edits: &mut Vec) -> Result { let nodes: Vec<_> = list.children().filter(Node::is_element).collect(); if nodes.len() > 100_000 { return Err("canvas exceeds the node limit".into()); } + let mut ids = BTreeMap::new(); for node in &nodes { let id = xml::value(*node, "uid").ok_or("canvas node has no uid")?; @@ -17,6 +15,7 @@ pub fn reposition(list: Node<'_, '_>, edits: &mut Vec) -> Result { return Err("duplicate canvas node uid".into()); } } + let mut pending = BTreeMap::new(); let mut successors = BTreeMap::<&str, Vec<&str>>::new(); let mut columns = BTreeMap::<&str, usize>::new(); @@ -31,6 +30,7 @@ pub fn reposition(list: Node<'_, '_>, edits: &mut Vec) -> Result { if sources.iter().any(|source| !ids.contains_key(source)) { return Err("canvas connection references an absent node".into()); } + pending.insert(*id, sources.len()); if sources.is_empty() { ready.insert(*id); @@ -39,6 +39,7 @@ pub fn reposition(list: Node<'_, '_>, edits: &mut Vec) -> Result { successors.entry(source).or_default().push(id); } } + let mut visited = 0; while let Some(id) = ready.pop_first() { visited += 1; @@ -53,9 +54,11 @@ pub fn reposition(list: Node<'_, '_>, edits: &mut Vec) -> Result { } } } + if visited != nodes.len() { return Err("canvas contains a connection cycle".into()); } + let mut rows = BTreeMap::::new(); for (id, node) in ids { let column = columns.get(id).copied().unwrap_or(0); @@ -69,7 +72,8 @@ pub fn reposition(list: Node<'_, '_>, edits: &mut Vec) -> Result { let end = node.range().end.checked_sub(node.tag_name().name().len() + 3).ok_or("invalid node end")?; end..end }; - edits.push(Edit { range, replacement }); + edits.push(XmlEdit { range, replacement }); } + Ok(nodes.len()) } diff --git a/crates/formats/examples/sbs_library_build/main.rs b/crates/formats/examples/sbs_library_build/main.rs index 03b5adc..6a37159 100644 --- a/crates/formats/examples/sbs_library_build/main.rs +++ b/crates/formats/examples/sbs_library_build/main.rs @@ -4,48 +4,64 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod convert; +mod converted_package; mod layout; mod references; +mod resource_random; +mod resource_recipe; mod resources; #[cfg(test)] mod tests; mod xml; +mod xml_edit; use std::{collections::BTreeMap, io::Read, path::Path}; type Result = std::result::Result>; const MAX_FILE_BYTES: u64 = 64 << 20; +const MAX_PACKAGE_FILES: usize = 2048; +const MAX_DIRECTORY_DEPTH: usize = 6; +const MAX_CONVERTED_BYTES: usize = 512 << 20; fn read(path: &Path) -> Result> { let metadata = std::fs::symlink_metadata(path)?; if !metadata.is_file() || metadata.len() > MAX_FILE_BYTES { return Err(format!("{} is not a regular file within the 64 MiB limit", path.display()).into()); } + let mut bytes = Vec::new(); std::fs::File::open(path)?.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes)?; if bytes.len() as u64 > MAX_FILE_BYTES { return Err("file grew beyond the read limit".into()); } + Ok(bytes) } fn inventory(root: &Path, relative: &Path, files: &mut Vec) -> Result<()> { - if relative.components().count() > 6 || files.len() > 2048 { + if relative.components().count() > MAX_DIRECTORY_DEPTH { return Err("package inventory exceeds its depth or file limit".into()); } - let mut entries = std::fs::read_dir(root.join(relative))?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { + + // The caller sorts package names once after traversal; directory entries need not be retained + for entry in std::fs::read_dir(root.join(relative))? { + let entry = entry?; let name = entry.file_name().into_string().map_err(|_| "non-UTF-8 package filename")?; let kind = entry.file_type()?; - // Resource directories are deliberately never opened: replacements have independent recipes. + + // Resource directories are deliberately never opened: replacements have independent recipes if name.eq_ignore_ascii_case("resources") || name.ends_with(".resources") || name.starts_with('.') { continue; } + let path = relative.join(&name); if kind.is_dir() { inventory(root, &path, files)?; } else if kind.is_file() && name.ends_with(".sbs") { + if files.len() >= MAX_PACKAGE_FILES { + return Err("package inventory exceeds its depth or file limit".into()); + } + let path = path.to_string_lossy().replace('\\', "/"); materialcraft_formats::sbs::library::relative_path(&format!("sbs://{path}"))?; files.push(path); @@ -53,6 +69,7 @@ fn inventory(root: &Path, relative: &Path, files: &mut Vec) -> Result<() return Err(format!("symlink refused in package inventory: {}", path.display()).into()); } } + Ok(()) } @@ -61,11 +78,13 @@ fn main() -> Result<()> { let [source, destination, report] = args.as_slice() else { return Err("usage: sbs_library_build ".into()); }; + let source = Path::new(source).canonicalize()?; let destination = Path::new(destination).canonicalize()?; if source == destination || destination.starts_with(&source) || source.starts_with(&destination) { return Err("source and destination trees must be separate".into()); } + let baseline = read(&destination.join("functions.sbs"))?; let baseline_text = std::str::from_utf8(&baseline)?; let baseline_doc = xml::parse(baseline_text)?; @@ -86,13 +105,15 @@ fn main() -> Result<()> { let mut converted = convert::package(&source, name, &identities)?; converted.report["qualified_standard_function_references"] = serde_json::json!(qualified); total = total.checked_add(converted.xml.len()).ok_or("library size overflow")?; - if total > 512 << 20 { + if total > MAX_CONVERTED_BYTES { return Err("converted library exceeds 512 MiB".into()); } + needed.extend(converted.resources.iter().copied()); reports.push(converted.report); outputs.insert(name.clone(), converted.xml.into_bytes()); } + let mut assets = Vec::new(); for key in needed { let recipe = resources::recipe(key)?; @@ -105,27 +126,32 @@ fn main() -> Result<()> { outputs.insert(path, bytes); } } - // Validate the complete dependency closure before writing any package or asset. + + // Validate the complete dependency closure before writing any package or asset let available = outputs.keys().cloned().chain(std::iter::once("functions.sbs".into())).collect(); for (name, bytes) in outputs.iter().filter(|(name, _)| name.ends_with(".sbs")) { convert::verify_references(std::str::from_utf8(bytes)?, name, &available)?; } + for (name, bytes) in &outputs { let target = destination.join(name); if target.exists() && read(&target)? != *bytes { return Err(format!("refusing to overwrite different existing content: {}", target.display()).into()); } + if let Some(parent) = target.parent() { - // Refuse existing symlink parents before creating output directories. + // Refuse existing symlink parents before creating output directories let mut ancestor = parent; while ancestor != destination { if ancestor.exists() && !ancestor.canonicalize()?.starts_with(&destination) { return Err("output directory escapes the destination".into()); } + ancestor = ancestor.parent().ok_or("invalid output directory")?; } } } + for (name, bytes) in &outputs { let target = destination.join(name); if let Some(parent) = target.parent() { @@ -133,9 +159,11 @@ fn main() -> Result<()> { } materialcraft_formats::write_atomic(&target, bytes)?; } + if read(&destination.join("functions.sbs"))? != baseline { return Err("the existing function library changed during conversion".into()); } + let report_data = serde_json::json!({ "version": 1, "converted_packages": reports.len(), "preserved_packages": ["functions.sbs"], "graph_implementations": "retained from the supplied packages; presentation changes do not establish redistribution rights", diff --git a/crates/formats/examples/sbs_library_build/references.rs b/crates/formats/examples/sbs_library_build/references.rs index 57eda47..2b1b7c7 100644 --- a/crates/formats/examples/sbs_library_build/references.rs +++ b/crates/formats/examples/sbs_library_build/references.rs @@ -40,10 +40,12 @@ pub fn qualify(text: &str, standard: &BTreeSet) -> Result<(String, usize if local.contains(path) || !standard.contains(path) { continue; } + let [uid] = dependencies.as_slice() else { return Err("bare standard function requires one declared functions.sbs dependency".into()) }; xml::set(&mut edits, value, &format!("pkg:///{path}?dependency={uid}")); } } + let count = edits.len(); Ok((xml::apply(text, edits)?, count)) } @@ -59,19 +61,28 @@ mod tests { "#; let library = format!("{body}"); let standard = function_paths(&xml::parse(&library).unwrap()); + assert_eq!(standard, BTreeSet::from(["Functions/Random/Sample_[A,B[".into()])); + let source = r#" "#; let (qualified, count) = qualify(source, &standard).unwrap(); + assert_eq!(count, 1); assert_eq!(qualified, source.replace("pkg:///Functions/Random/Sample_[A,B[", "pkg:///Functions/Random/Sample_[A,B[?dependency=9")); + let explicit = source.replace("Sample_[A,B[", "Sample_[A,B[?dependency=7"); + assert_eq!(qualify(&explicit, &standard).unwrap(), (explicit, 0)); + let unknown = source.replace("Sample_[A,B[", "Unknown"); + assert_eq!(qualify(&unknown, &standard).unwrap(), (unknown, 0)); + let local = source.replacen("", &format!("{body}"), 1); + assert_eq!(qualify(&local, &standard).unwrap(), (local, 0)); assert!(qualify(&source.replace("sbs://functions.sbs", "sbs://other.sbs"), &standard).is_err()); } diff --git a/crates/formats/examples/sbs_library_build/resource_random.rs b/crates/formats/examples/sbs_library_build/resource_random.rs new file mode 100644 index 0000000..5e5a348 --- /dev/null +++ b/crates/formats/examples/sbs_library_build/resource_random.rs @@ -0,0 +1,11 @@ +pub(super) struct ResourceRandom(pub(super) u64); + +impl ResourceRandom { + pub(super) fn next(&mut self) -> u64 { + // Xorshift64 uses the standard 13, 7, 17 shift sequence + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } +} diff --git a/crates/formats/examples/sbs_library_build/resource_recipe.rs b/crates/formats/examples/sbs_library_build/resource_recipe.rs new file mode 100644 index 0000000..f0f91d9 --- /dev/null +++ b/crates/formats/examples/sbs_library_build/resource_recipe.rs @@ -0,0 +1,6 @@ +pub struct ResourceRecipe { + pub key: &'static str, + pub path: &'static str, + pub extension: &'static str, + pub description: &'static str, +} diff --git a/crates/formats/examples/sbs_library_build/resources.rs b/crates/formats/examples/sbs_library_build/resources.rs index aee2be9..8025d22 100644 --- a/crates/formats/examples/sbs_library_build/resources.rs +++ b/crates/formats/examples/sbs_library_build/resources.rs @@ -1,47 +1,55 @@ //! Original procedural recipes and attributed library resources; never reads source assets. -use super::Result; +use super::{Result, resource_random::ResourceRandom, resource_recipe::ResourceRecipe}; use image::{DynamicImage, ImageBuffer, ImageFormat}; use std::io::Cursor; -pub struct Recipe { - pub key: &'static str, - pub path: &'static str, - pub extension: &'static str, - pub description: &'static str, -} - -const RECIPES: &[Recipe] = &[ - Recipe { +const RECIPES: &[ResourceRecipe] = &[ + ResourceRecipe { key: "perm", path: "resources/perm.png", extension: "png", description: "Sixteen independently shuffled 1024-entry permutation rows. Original seeded lookup substitute; external sampling semantics require validation.", }, - Recipe { + ResourceRecipe { key: "bluenoise_256", path: "resources/bluenoise_256.png", extension: "png", description: "256 by 256 grayscale blue noise for dithering and sampling.", }, - Recipe { + ResourceRecipe { key: "bluenoise_256_c", path: "resources/bluenoise_256_c.exr", extension: "exr", description: "256 by 256 color blue noise for multichannel sampling.", }, - Recipe { key: "scratched_metal", path: "resources/scratched_metal.png", extension: "png", description: "Scratched metal texture for surface wear." }, - Recipe { key: "leather", path: "resources/leather.png", extension: "png", description: "Leather grain texture for surface detail." }, - Recipe { key: "scratch_damage", path: "resources/scratch_damage.png", extension: "png", description: "Scratch damage texture for worn surfaces." }, - Recipe { key: "shuf4_64_32", path: "resources/shuf4_64_32.exr", extension: "exr", description: "Four-channel 64 by 32 floating-point sampling lookup." }, - Recipe { key: "sun", path: "resources/sun.exr", extension: "exr", description: "512 by 16 solar coefficient lookup. All RGBA channels are numeric data." }, - Recipe { key: "moss01_bmp", path: "resources/moss01_bmp.bmp", extension: "bmp", description: "Moss texture for coverage and surface detail." }, - Recipe { + ResourceRecipe { + key: "scratched_metal", + path: "resources/scratched_metal.png", + extension: "png", + description: "Scratched metal texture for surface wear.", + }, + ResourceRecipe { key: "leather", path: "resources/leather.png", extension: "png", description: "Leather grain texture for surface detail." }, + ResourceRecipe { key: "scratch_damage", path: "resources/scratch_damage.png", extension: "png", description: "Scratch damage texture for worn surfaces." }, + ResourceRecipe { + key: "shuf4_64_32", + path: "resources/shuf4_64_32.exr", + extension: "exr", + description: "Four-channel 64 by 32 floating-point sampling lookup.", + }, + ResourceRecipe { + key: "sun", + path: "resources/sun.exr", + extension: "exr", + description: "512 by 16 solar coefficient lookup. All RGBA channels are numeric data.", + }, + ResourceRecipe { key: "moss01_bmp", path: "resources/moss01_bmp.bmp", extension: "bmp", description: "Moss texture for coverage and surface detail." }, + ResourceRecipe { key: "courier_new", path: "resources/LiberationMono-Regular.ttf", extension: "ttf", description: "Liberation Mono Regular fallback from craft-fonts for missing point-preview fonts.", }, - Recipe { + ResourceRecipe { key: "DoorFrame", path: "resources/DoorFrame.svg", extension: "svg", @@ -49,27 +57,18 @@ const RECIPES: &[Recipe] = &[ }, ]; -pub fn recipe(key: &str) -> Result<&'static Recipe> { +pub fn recipe(key: &str) -> Result<&'static ResourceRecipe> { RECIPES.iter().find(|recipe| recipe.key == key).ok_or_else(|| format!("no resource recipe for `{key}`").into()) } -struct Random(u64); -impl Random { - fn next(&mut self) -> u64 { - self.0 ^= self.0 << 13; - self.0 ^= self.0 >> 7; - self.0 ^= self.0 << 17; - self.0 - } -} - fn permutation(length: usize, seed: u64) -> Vec { let mut values: Vec<_> = (0..length).collect(); - let mut random = Random(seed); + let mut random = ResourceRandom(seed); for index in (1..length).rev() { let other = (random.next() % (index as u64 + 1)) as usize; values.swap(index, other); } + values } @@ -78,13 +77,16 @@ pub fn load(key: &str) -> Result> { if !notices(key).is_empty() { return Ok(materialcraft_formats::sbs::library::read(&format!("sbs://{}", recipe.path))?); } + if key == "DoorFrame" { return Ok(br#""#.to_vec()); } - // Every other recipe must explicitly select its source rather than invent replacement art. + + // Every other recipe must explicitly select its source rather than invent replacement art if key != "perm" { return Err(format!("no procedural generator for `{key}`").into()); } + let values: Vec = (0..16) .flat_map(|row| permutation(1024, 0x6d6174657269616c ^ (row * 313 + 1)).into_iter().map(|v| ((v as f32 / 1023.0) * 65535.0).round() as u16)) .collect(); @@ -99,7 +101,7 @@ pub fn notices(key: &str) -> &'static [&'static str] { match key { "scratch_damage" | "leather" | "scratched_metal" | "moss01_bmp" => &["LICENSE_NICK_ROSE.txt"], "bluenoise_256" | "bluenoise_256_c" => &["LICENSE_BLUE_NOISE.txt"], - "shuf4_64_32" | "sun" => &["LICENSE_HOSEK_WILKIE.txt", "sun_lookup_readme.txt"], + "shuf4_64_32" | "sun" => &["LICENSE_HOSEK_WILKIE.txt"], "courier_new" => &["OFL-LiberationMono.txt"], _ => &[], } diff --git a/crates/formats/examples/sbs_library_build/tests.rs b/crates/formats/examples/sbs_library_build/tests.rs index 9b213fd..37aa4aa 100644 --- a/crates/formats/examples/sbs_library_build/tests.rs +++ b/crates/formats/examples/sbs_library_build/tests.rs @@ -20,6 +20,24 @@ fn identities(name: &str) -> BTreeMap { BTreeMap::from([(name.into(), xml::identity(name)), ("functions.sbs".into(), "0".into())]) } +#[test] +fn flat_package_inventory_enforces_the_total_file_limit() { + let directory = (0..1_000) + .map(|suffix| std::env::temp_dir().join(format!("materialcraft-inventory-{}-{suffix}", std::process::id()))) + .find(|path| std::fs::create_dir(path).is_ok()) + .unwrap(); + for index in 0..=2048 { + std::fs::write(directory.join(format!("package-{index}.sbs")), b"").unwrap(); + } + + let mut files = Vec::new(); + let result = inventory(&directory, Path::new(""), &mut files); + std::fs::remove_dir_all(directory).unwrap(); + + assert!(result.is_err(), "a flat directory must not bypass the total package limit"); + assert!(files.len() <= 2048); +} + #[test] fn conversion_starts_both_versions_at_the_workspace_release() { let saved = materialcraft_formats::sbs::write_sbs(&materialcraft_core::Package::new("Version check")).unwrap(); @@ -28,6 +46,7 @@ fn conversion_starts_both_versions_at_the_workspace_release() { for source in [PACKAGE.to_string(), PACKAGE.replacen("", "", 1)] { let converted = convert::package(&source, "example.sbs", &identities("example.sbs")).unwrap(); let converted = xml::parse(&converted.xml).unwrap(); + assert_eq!(xml::value(converted.root_element(), "updaterVersion"), Some(expected)); assert_eq!(xml::value(converted.root_element(), "formatVersion"), Some(expected)); assert_eq!(converted.root_element().children().filter(|n| n.has_tag_name("updaterVersion")).count(), 1); @@ -38,18 +57,24 @@ fn conversion_starts_both_versions_at_the_workspace_release() { fn native_conversion_keeps_unknown_computations_and_replaces_embedded_art_without_decoding_it() { let owner = "materials/pbr/example.sbs"; let converted = convert::package(PACKAGE, owner, &identities(owner)).unwrap(); + assert_eq!(convert::semantics(&xml::parse(PACKAGE).unwrap()), convert::semantics(&xml::parse(&converted.xml).unwrap())); + for retained in ["futureOperation", "future_function", "retain verbatim", "physicalSize v=\"1 2 3\""] { assert!(converted.xml.contains(retained)); } for removed in ["Old prose", "Old author", "payload", "Old annotation", "binembedded", "sbs://", "docked"] { assert!(!converted.xml.contains(removed), "{removed}"); } + assert!(converted.xml.contains("../../resources/DoorFrame.svg")); assert!(converted.xml.contains("../../functions.sbs")); + let document = xml::parse(&converted.xml).unwrap(); + assert_eq!(document.descendants().filter(|n| n.has_tag_name("gpos")).count(), 3); assert_eq!(converted.xml, convert::package(PACKAGE, owner, &identities(owner)).unwrap().xml); + let available = BTreeSet::from([owner.into(), "functions.sbs".into(), "resources/DoorFrame.svg".into()]); convert::verify_references(&converted.xml, owner, &available).unwrap(); } @@ -57,10 +82,13 @@ fn native_conversion_keeps_unknown_computations_and_replaces_embedded_art_withou #[test] fn unknown_assets_missing_dependencies_and_broken_graphs_are_errors() { let ids = identities("example.sbs"); + assert!(convert::package(&PACKAGE.replace("DoorFrame", "unknown_asset"), "example.sbs", &ids).is_err()); assert!(convert::package(&PACKAGE.replace("sbs://functions.sbs", "sbs://absent.sbs"), "example.sbs", &ids).is_err()); assert!(convert::package(&PACKAGE.replace("connRef v=\"10\"", "connRef v=\"99\""), "example.sbs", &ids).is_err()); + let cycle = PACKAGE.replace("", ""); + assert!(convert::package(&cycle, "example.sbs", &ids).is_err()); } @@ -69,9 +97,13 @@ fn references_are_relative_portable_and_confined_to_the_library() { assert_eq!(xml::relative("materials/pbr/a.sbs", "resources/a.png").unwrap(), "../../resources/a.png"); assert_eq!(xml::relative("materials/pbr/a.sbs", "materials/pbr/b.sbs").unwrap(), "b.sbs"); assert!(xml::relative("a.sbs", "../private.png").is_err()); + let converted = convert::package(PACKAGE, "example.sbs", &identities("example.sbs")).unwrap(); + assert!(convert::verify_references(&converted.xml, "example.sbs", &BTreeSet::new()).is_err()); + let escaped = converted.xml.replace("resources/DoorFrame.svg", "../outside.svg"); + assert!(convert::verify_references(&escaped, "example.sbs", &BTreeSet::new()).is_err()); } @@ -86,6 +118,7 @@ fn resource_names_survive_and_surface_replacements_declare_their_actual_formats( ] { let xml = format!(""); let output = convert::package(&xml, "example.sbs", &identities("example.sbs")).unwrap(); + assert!(output.xml.contains(&format!(""))); assert!(output.xml.contains(&format!(""))); assert!(convert::package(&xml.replace(original, "different.png"), "example.sbs", &identities("example.sbs")).is_err()); @@ -95,15 +128,21 @@ fn resource_names_survive_and_surface_replacements_declare_their_actual_formats( #[test] fn generated_permutation_has_uniform_ranks_and_blue_noise_has_the_expected_shape() { let permutation = image::load_from_memory(&resources::load("perm").unwrap()).unwrap().into_luma16(); + assert_eq!(permutation.dimensions(), (1024, 16)); + for row in permutation.as_raw().as_chunks::<1024>().0.iter() { assert_eq!(row.iter().collect::>().len(), 1024); assert_eq!(row.iter().copied().min(), Some(0)); assert_eq!(row.iter().copied().max(), Some(u16::MAX)); } + let noise = resources::load("bluenoise_256").unwrap(); + assert_eq!(noise, resources::load("bluenoise_256").unwrap()); + let noise = image::load_from_memory(&noise).unwrap().into_luma16(); + assert_eq!(noise.dimensions(), (256, 256)); } @@ -112,10 +151,13 @@ fn library_art_is_decodable_without_reading_source_assets() { for key in ["DoorFrame", "scratched_metal", "leather", "scratch_damage", "moss01_bmp", "sun", "bluenoise_256_c"] { let bytes = resources::load(key).unwrap(); let image = materialcraft_formats::decode(&bytes, true).unwrap(); + assert!(image.w > 0 && image.h > 0); assert!(image.data.iter().all(|value| value.is_finite())); + let min = image.data.iter().copied().fold(f32::INFINITY, f32::min); let max = image.data.iter().copied().fold(f32::NEG_INFINITY, f32::max); + assert!(max > min, "constant replacement {key}"); } } @@ -126,20 +168,27 @@ fn curated_resources_keep_the_supplied_bytes_and_required_notices() { let recipe = resources::recipe(key).unwrap(); if key == "courier_new" && materialcraft_formats::sbs::library::read(&format!("sbs://{}", recipe.path)).is_err() { assert!(resources::load(key).is_err(), "a build without craft-fonts cannot export its font"); + continue; } + let bytes = resources::load(key).unwrap(); let supplied = materialcraft_formats::sbs::library::read(&format!("sbs://{}", recipe.path)).unwrap(); + assert!(bytes == supplied, "converter replaced the supplied bytes for {key}"); assert!(!resources::notices(key).is_empty(), "missing license notice for {key}"); + for notice in resources::notices(key) { assert!(!materialcraft_formats::sbs::library::read(&format!("sbs://resources/{notice}")).unwrap().is_empty()); } } + if let Ok(font) = resources::load("courier_new") { assert!(font.starts_with(b"\0\x01\0\0")); } + let shuffled = image::load_from_memory(&resources::load("shuf4_64_32").unwrap()).unwrap().into_rgba32f(); + assert_eq!(shuffled.dimensions(), (64, 32)); assert!(shuffled.as_raw().iter().all(|v| v.is_finite())); } diff --git a/crates/formats/examples/sbs_library_build/xml.rs b/crates/formats/examples/sbs_library_build/xml.rs index 82815d5..696dca0 100644 --- a/crates/formats/examples/sbs_library_build/xml.rs +++ b/crates/formats/examples/sbs_library_build/xml.rs @@ -1,9 +1,10 @@ -use super::Result; +use super::{Result, xml_edit::XmlEdit}; use roxmltree::{Document, Node}; -use std::ops::Range; pub fn parse(text: &str) -> Result> { - Ok(Document::parse_with_options(text, roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: 8_000_000 })?) + const MAX_XML_NODES: u32 = 8_000_000; + + Ok(Document::parse_with_options(text, roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES })?) } pub fn child<'a, 'b>(node: Node<'a, 'b>, tag: &str) -> Option> { @@ -34,22 +35,18 @@ pub fn title(identifier: &str) -> String { } pub fn identity(path: &str) -> String { + // Two FNV-style streams fill the UUID; masks set its version and variant bits let hash = |seed: u64| path.bytes().fold(seed, |hash, byte| (hash ^ u64::from(byte)).wrapping_mul(0x100000001b3)); let bits = (u128::from(hash(0xcbf29ce484222325)) << 64) | u128::from(hash(0x9e3779b97f4a7c15)); let id = format!("{:032x}", (bits & !(0xf000_u128 << 64) & !(0xc000_u128 << 48)) | (0x4000_u128 << 64) | (0x8000_u128 << 48)); format!("{{{}-{}-{}-{}-{}}}", &id[..8], &id[8..12], &id[12..16], &id[16..20], &id[20..]) } -pub struct Edit { - pub range: Range, - pub replacement: String, +pub fn set(edits: &mut Vec, node: Node<'_, '_>, value: &str) { + edits.push(XmlEdit { range: node.range(), replacement: leaf(node.tag_name().name(), value) }); } -pub fn set(edits: &mut Vec, node: Node<'_, '_>, value: &str) { - edits.push(Edit { range: node.range(), replacement: leaf(node.tag_name().name(), value) }); -} - -pub fn apply(text: &str, mut edits: Vec) -> Result { +pub fn apply(text: &str, mut edits: Vec) -> Result { edits.sort_by_key(|edit| (edit.range.start, edit.range.end)); let mut output = String::with_capacity(text.len()); let mut cursor = 0; @@ -57,6 +54,7 @@ pub fn apply(text: &str, mut edits: Vec) -> Result { if edit.range.start < cursor || edit.range.end < edit.range.start { return Err("overlapping XML edits".into()); } + output.push_str(text.get(cursor..edit.range.start).ok_or("invalid XML edit boundary")?); output.push_str(&edit.replacement); cursor = edit.range.end; @@ -69,6 +67,7 @@ pub fn relative(owner: &str, target: &str) -> Result { for path in [owner, target] { materialcraft_formats::sbs::library::relative_path(&format!("sbs://{path}"))?; } + let mut from: Vec<_> = owner.split('/').collect(); from.pop(); let to: Vec<_> = target.split('/').collect(); diff --git a/crates/formats/examples/sbs_library_build/xml_edit.rs b/crates/formats/examples/sbs_library_build/xml_edit.rs new file mode 100644 index 0000000..2014349 --- /dev/null +++ b/crates/formats/examples/sbs_library_build/xml_edit.rs @@ -0,0 +1,6 @@ +use std::ops::Range; + +pub struct XmlEdit { + pub range: Range, + pub replacement: String, +} diff --git a/crates/formats/src/dds_image.rs b/crates/formats/src/dds_image.rs index cfe8eb0..f83d4ca 100644 --- a/crates/formats/src/dds_image.rs +++ b/crates/formats/src/dds_image.rs @@ -14,11 +14,12 @@ pub(crate) fn decode(bytes: &[u8], linear: bool) -> Result { dds::ImageViewMut::new(bytemuck::cast_slice_mut(&mut data), size, dds::ColorFormat::RGBA_F32).ok_or_else(|| error("Invalid DDS dimensions".into()))?; decoder.read_surface(view).map_err(|e| error(e.to_string()))?; if !linear && decoder.format().precision() != dds::Precision::F32 { - for pixel in data.chunks_exact_mut(4) { + for pixel in data.as_chunks_mut::<4>().0 { for value in pixel.iter_mut().take(3) { *value = srgb_to_linear(*value); } } } + Ok(Image { w: size.width, h: size.height, kind: Kind::Color, integer_sampling: false, data: std::sync::Arc::new(data) }) } diff --git a/crates/formats/src/image.rs b/crates/formats/src/image.rs index b017df1..edac3d0 100644 --- a/crates/formats/src/image.rs +++ b/crates/formats/src/image.rs @@ -30,6 +30,7 @@ pub fn decode(bytes: &[u8], linear: bool) -> Result { image::ImageError::Limits(_) => FormatError::Image(format!("image is larger than {MAX_SIDE} pixels")), e => FormatError::Image(e.to_string()), }; + let (w, h) = guard(|| open()?.into_dimensions().map_err(image_error), failed)?; if w == 0 || h == 0 || w > MAX_SIDE || h > MAX_SIDE { return Err(FormatError::Image(format!("{w}x{h} is outside 1..={MAX_SIDE} pixels"))); @@ -46,12 +47,13 @@ pub fn decode(bytes: &[u8], linear: bool) -> Result { let mut data = img.into_rgba32f().into_raw(); if !(float || linear) { - for px in data.chunks_exact_mut(4) { + for px in data.as_chunks_mut::<4>().0 { for c in px.iter_mut().take(3) { *c = srgb_to_linear(*c); } } } + Ok(Image { w, h, kind: Kind::Color, integer_sampling: false, data: Arc::new(data) }) } diff --git a/crates/formats/src/project.rs b/crates/formats/src/project.rs index 6950073..e6e7a90 100644 --- a/crates/formats/src/project.rs +++ b/crates/formats/src/project.rs @@ -13,6 +13,13 @@ use std::io::{Cursor, Read}; pub const EXT: &str = "mcraft"; pub const SBS_ENTRY: &str = "package.sbs"; +/// Most members one archive may hold; a header count is checked before any member is read. +const MAX_ENTRIES: usize = 100_000; +/// Largest member read. +const MAX_ENTRY_BYTES: u64 = 256 << 20; +/// Most bytes all members may expand to together. Without a total, many individually allowed +/// members could expand far past what one open should spend (audit: MaterialCraft #1). +const MAX_TOTAL_EXPANDED_BYTES: u64 = 1 << 30; fn bad(message: impl Into) -> FormatError { FormatError::BadPackage(message.into()) @@ -76,10 +83,15 @@ fn resolve(owner: &str, reference: &str) -> Result { pub fn read(bytes: &[u8], identity: &str) -> Result { let mut zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|error| bad(error.to_string()))?; + if zip.len() > MAX_ENTRIES { + return Err(bad(format!("archive has more than {MAX_ENTRIES} entries"))); + } + let mut files = BTreeMap::new(); let mut seen = BTreeSet::new(); + let mut total_expanded_bytes = 0u64; for index in 0..zip.len() { - let mut entry = zip.by_index(index).map_err(|error| bad(error.to_string()))?; + let entry = zip.by_index(index).map_err(|error| bad(error.to_string()))?; let name = entry.name().to_string(); let path = name.trim_end_matches('/'); if path.is_empty() @@ -96,9 +108,19 @@ pub fn read(bytes: &[u8], identity: &str) -> Result { } let mut content = Vec::new(); - entry.read_to_end(&mut content).map_err(|error| bad(error.to_string()))?; + entry.take(MAX_ENTRY_BYTES + 1).read_to_end(&mut content).map_err(|error| bad(error.to_string()))?; + if content.len() as u64 > MAX_ENTRY_BYTES { + return Err(bad(format!("archive entry `{name}` is too large"))); + } + + total_expanded_bytes = total_expanded_bytes.saturating_add(content.len() as u64); + if total_expanded_bytes > MAX_TOTAL_EXPANDED_BYTES { + return Err(bad("archive expands past its total size limit")); + } + files.insert(name, content); } + let roots: Vec<_> = files.keys().filter(|name| !name.contains('/') && name.to_ascii_lowercase().ends_with(".sbs")).cloned().collect(); if roots.len() != 1 { return Err(bad("A MaterialCraft archive must contain exactly one SBS file at its root")); @@ -139,9 +161,11 @@ pub fn read(bytes: &[u8], identity: &str) -> Result { names.insert(reference, format!("{prefix}{target}")); pending.push(target); } + remap(&mut read.pkg, &names); packages.insert(file, read.pkg); } + let mut root_package = packages.get(&root).cloned().ok_or_else(|| bad("Missing SBS package"))?; root_package.dependencies = packages.into_iter().map(|(path, package)| (format!("{prefix}{path}"), std::sync::Arc::new(package))).collect(); Ok(sbs::SbsRead { pkg: root_package, warnings }) diff --git a/crates/formats/src/sbs/base_parameters.rs b/crates/formats/src/sbs/base_parameters.rs index 9017f44..c1b8d81 100644 --- a/crates/formats/src/sbs/base_parameters.rs +++ b/crates/formats/src/sbs/base_parameters.rs @@ -13,7 +13,8 @@ pub(super) fn read_base_params(params: Option, warnings: &mut Warnings) let mut base = BaseParameters::default(); for element in params.into_iter().flat_map(|parameters| children_named(parameters, "parameter")) { let Some(parameter) = child_value(element, "name").and_then(BaseParameter::named) else { continue }; - // SBS stores parent as 1 and input as 2, independently of our enum order. + + // SBS stores parent as 1 and input as 2, independently of our enum order let inheritance = match child_value(element, "relativeTo").unwrap_or("2") { "0" => ParameterInheritance::Absolute, "1" => ParameterInheritance::RelativeToParent, diff --git a/crates/formats/src/sbs/constant.rs b/crates/formats/src/sbs/constant.rs index 322e8f9..94e6ebf 100644 --- a/crates/formats/src/sbs/constant.rs +++ b/crates/formats/src/sbs/constant.rs @@ -8,6 +8,12 @@ pub(super) const UPDATER_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const SBS_EXT: &str = "sbs"; pub(super) const MAX_WARNINGS: usize = 600; + +/// Largest SBS document read (audit: MaterialCraft #2). +pub(super) const MAX_SBS_BYTES: u64 = 256 << 20; + +/// Most wires one SBS may carry, across all graphs (audit: MaterialCraft #2). +pub(super) const MAX_WIRES: usize = 1_000_000; pub(super) const SNIFF_BYTES: usize = 4096; pub(super) const SELF_DEPENDENCY: &str = "?himself"; pub(super) const FIRST_UID: u64 = 1_000_000_000; diff --git a/crates/formats/src/sbs/embedded.rs b/crates/formats/src/sbs/embedded.rs index cb5d0a9..d3f9431 100644 --- a/crates/formats/src/sbs/embedded.rs +++ b/crates/formats/src/sbs/embedded.rs @@ -13,6 +13,7 @@ pub(super) fn decode_embedded(strdata: &str) -> std::result::Result, Str if length as u64 > crate::MAX_RESOURCE { return Err("embedded resource exceeds the 256 MiB limit".into()); } + let mut out = Vec::new(); // Reading one extra byte detects data that contradicts the declared format length @@ -49,7 +50,9 @@ mod tests { assert!(decode_embedded(&base64_engine().encode(&raw)).unwrap_err().contains("declared")); } + raw[..4].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode_embedded(&base64_engine().encode(&raw)).unwrap_err().contains("256 MiB limit")); } } diff --git a/crates/formats/src/sbs/function_graph.rs b/crates/formats/src/sbs/function_graph.rs index 06391e4..1a2304f 100644 --- a/crates/formats/src/sbs/function_graph.rs +++ b/crates/formats/src/sbs/function_graph.rs @@ -18,10 +18,12 @@ pub(super) fn read(element: XmlNode<'_, '_>) -> Result { if graph.nodes.len() >= 4096 { return Err("function exceeds the 4096-node limit".into()); } + let id = child_value(element, "uid").and_then(|value| value.parse().ok()).ok_or("function node has no valid uid")?; if !ids.insert(id) { return Err(format!("duplicate function uid {id}")); } + let op = child_value(element, "function").ok_or("function node has no operation")?; let value = child_element(element, "funcDatas") .into_iter() @@ -46,6 +48,7 @@ pub(super) fn read(element: XmlNode<'_, '_>) -> Result { if names.len() >= 4096 { return Err("Function instance exceeds the 4096-input limit".into()); } + if let Some(instance) = &mut instance { instance.inputs.push(materialcraft_core::GraphPort { identifier: port.into(), type_code: 0 }); } else { @@ -62,6 +65,7 @@ pub(super) fn read(element: XmlNode<'_, '_>) -> Result { return Err(format!("{op}: duplicate input `{port}`")); } } + let position = child_element(element, "GUILayout").and_then(|layout| child_value(layout, "gpos")).map(parse_floats).unwrap_or_default(); graph.nodes.push(FunctionNode { missing: missing.then_some(names), @@ -84,13 +88,16 @@ pub(super) fn read(element: XmlNode<'_, '_>) -> Result { if graph.output.is_some_and(|id| !ids.contains(&id)) { return Err("function result references a missing node".into()); } + if graph.nodes.iter().flat_map(|node| node.inputs.iter().flatten()).any(|id| !ids.contains(id)) { return Err("function connection references a missing node".into()); } + // Earlier files may designate a Dot; keep the graph editable without promoting another node if graph.output.and_then(|id| graph.node(id)).is_some_and(|node| node.missing.is_none() && !function::can_be_output(&node.op)) { graph.output = None; } + Ok(graph) } @@ -106,6 +113,7 @@ pub(super) fn write(graph: &FunctionGraph, writer: &mut SbsWriter) -> Result 4096 { return Err("function exceeds the 4096-node limit".into()); } + let types = function::node_types(graph)?; let identifiers: BTreeMap<_, _> = graph.nodes.iter().map(|node| (node.id, writer.next_uid())).collect(); let mut output = String::from(""); diff --git a/crates/formats/src/sbs/image_input.rs b/crates/formats/src/sbs/image_input.rs index 56ff79e..68a3eb3 100644 --- a/crates/formats/src/sbs/image_input.rs +++ b/crates/formats/src/sbs/image_input.rs @@ -3,7 +3,7 @@ use materialcraft_nodes::{MAX_IMAGE_INPUTS, image_input_prefix}; pub(super) fn read(definition: &str, identifier: &str) -> Option { let prefix = image_input_prefix(definition)?; - // Native FX-Map sockets use inputpattern; accept the earlier writer's input spelling too. + // Native FX-Map sockets use inputpattern; accept the earlier writer's input spelling too let native_identifier = if definition == "fx_map" { identifier.strip_prefix("inputpattern").map(|suffix| format!("input{suffix}")) } else { None }; let identifier = native_identifier.as_deref().unwrap_or(identifier); let index = if identifier == "input" { 0 } else { identifier.strip_prefix("input:")?.parse::().ok()? }; diff --git a/crates/formats/src/sbs/library.rs b/crates/formats/src/sbs/library.rs index 21dbea2..a279881 100644 --- a/crates/formats/src/sbs/library.rs +++ b/crates/formats/src/sbs/library.rs @@ -79,16 +79,19 @@ pub fn contains_file(path: &std::path::Path) -> bool { if let Ok(file) = path.canonicalize() { return file.starts_with(root); } + path.parent().and_then(|parent| parent.canonicalize().ok()).is_some_and(|parent| parent.starts_with(root)) } /// Read a validated library alias from the configured directory or the embedded fallback. pub fn read(uri: &str) -> Result, String> { let relative = relative_path(uri)?; - // These assets live in craft-fonts, so a source checkout's SBS directory cannot supply them. + + // These assets live in craft-fonts, so a source checkout's SBS directory cannot supply them if is_shared_font_asset(relative) { return read_embedded(relative).map_err(|error| format!("{error}; build with CRAFT_FONTS_DIR containing Liberation Mono Regular")); } + #[cfg(not(target_arch = "wasm32"))] { let root = root(); @@ -114,11 +117,13 @@ fn read_embedded(relative: &str) -> Result, String> { if *size > 256 << 20 { return Err("bundled asset exceeds 256 MiB".into()); } + let mut bytes = Vec::new(); flate2::read::DeflateDecoder::new(*compressed).take(*size as u64 + 1).read_to_end(&mut bytes).map_err(|error| error.to_string())?; if bytes.len() != *size { return Err("bundled asset has an invalid decompressed length".into()); } + Ok(bytes) } @@ -180,8 +185,10 @@ mod tests { for (relative, _, _) in EMBEDDED_ASSETS { if is_shared_font_asset(relative) { assert_eq!(read(&format!("sbs://{relative}")).unwrap(), read_embedded(relative).unwrap()); + continue; } + assert_eq!(read_embedded(relative).unwrap(), std::fs::read(root.join(relative)).unwrap(), "{relative}"); } } diff --git a/crates/formats/src/sbs/library_layout.rs b/crates/formats/src/sbs/library_layout.rs index 8dff9d6..2125961 100644 --- a/crates/formats/src/sbs/library_layout.rs +++ b/crates/formats/src/sbs/library_layout.rs @@ -5,7 +5,6 @@ use super::{ LayoutFrame, LayoutItem, - constant::MAX_XML_NODES, filter_map::FILTERS, xml::{child_element, child_value, children_named, parse_floats}, }; @@ -19,14 +18,11 @@ const MAX_ITEMS: usize = 65_536; /// The frames of every graph and parameter function in `bytes`, in document order pub fn read_layout(bytes: &[u8], uri: &str) -> Result> { - let text = std::str::from_utf8(bytes).map_err(|e| FormatError::BadPackage(format!("the SBS file is not UTF-8 text: {e}")))?; - let text = text.trim_start_matches('\u{feff}'); - let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES }; - let doc = roxmltree::Document::parse_with_options(text, options).map_err(|e| FormatError::BadPackage(format!("the SBS file is not valid XML: {e}")))?; + super::SbsDocument::parse(bytes)?.layout(uri) +} + +pub(super) fn read_document(doc: &roxmltree::Document<'_>, uri: &str) -> Result> { let root = doc.root_element(); - if root.tag_name().name() != "package" { - return Err(FormatError::BadPackage("not an SBS package (the root element is not )".into())); - } let mut dependencies = HashMap::new(); for dependency in child_element(root, "dependencies").into_iter().flat_map(|d| children_named(d, "dependency")) { @@ -77,6 +73,7 @@ pub fn read_layout(bytes: &[u8], uri: &str) -> Result> { items += 1; } } + placed.sort_by(|a, b| a.0.cmp(&b.0).then(a.1[1].total_cmp(&b.1[1])).then(a.1[0].total_cmp(&b.1[0]))); let mut grouped: Vec> = vec![Vec::new(); scope_frames.len()]; for (frame, _, item) in placed { @@ -84,6 +81,7 @@ pub fn read_layout(bytes: &[u8], uri: &str) -> Result> { list.push(item); } } + for ((title, _), items) in scope_frames.drain(..).zip(grouped) { if frames.len() >= MAX_FRAMES { break; @@ -92,6 +90,7 @@ pub fn read_layout(bytes: &[u8], uri: &str) -> Result> { frames.push(LayoutFrame { title, function, items }); } } + Ok(frames) } diff --git a/crates/formats/src/sbs/linked_resources.rs b/crates/formats/src/sbs/linked_resources.rs index 98beff4..3899fa9 100644 --- a/crates/formats/src/sbs/linked_resources.rs +++ b/crates/formats/src/sbs/linked_resources.rs @@ -13,6 +13,7 @@ fn relative_to(owner: &str, file: &str) -> String { if file.contains(':') || file.starts_with('/') { return file; } + let owner = owner.replace('\\', "/"); owner.rsplit_once('/').map_or_else(|| file.clone(), |(folder, _)| format!("{folder}/{file}")) } @@ -25,6 +26,10 @@ fn resource_path(resource: Node<'_, '_>) -> String { } pub(super) fn read(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, pkg: &mut Package, linked: &dyn Fn(&str) -> Option>) { + const MAX_SOURCE_BYTES: usize = 64 * 1024 * 1024; + const MAX_SOURCE_PACKAGES: usize = 64; + const MAX_IMPORTED_RESOURCES: usize = 1024; + let mut needed: BTreeMap> = BTreeMap::new(); for parameter in content .descendants() @@ -36,13 +41,15 @@ pub(super) fn read(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, pkg: & needed.entry(file).or_default().insert(path); } } - // Dependency resources cannot trigger recursive graph loads or unbounded XML work. - let mut remaining = 64 * 1024 * 1024_usize; + + // Dependency resources cannot trigger recursive graph loads or unbounded XML work + let mut remaining = MAX_SOURCE_BYTES; let mut remaining_data = crate::MAX_RESOURCE as usize; - if needed.len() > 64 { + if needed.len() > MAX_SOURCE_PACKAGES { reader.warnings.add("Only the first 64 resource dependencies were read"); } - for (file, paths) in needed.into_iter().take(64) { + + for (file, paths) in needed.into_iter().take(MAX_SOURCE_PACKAGES) { let result = (|| -> Result<(), String> { let bytes = linked(&file).ok_or("source package is unavailable")?; remaining = remaining.checked_sub(bytes.len()).ok_or("linked resource XML exceeds 64 MiB")?; @@ -52,14 +59,17 @@ pub(super) fn read(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, pkg: & if !document.root_element().has_tag_name("package") { return Err("source is not an SBS package".into()); } + for resource in document.descendants().filter(|node| node.has_tag_name("resource")) { let path = resource_path(resource); if !paths.contains(&path) || reader.external_resource_names.contains_key(&(file.clone(), path.clone())) { continue; } - if reader.external_resource_names.len() >= 1024 { + + if reader.external_resource_names.len() >= MAX_IMPORTED_RESOURCES { return Err("linked resources exceed the 1024-resource limit".into()); } + let identifier = child_value(resource, "identifier").unwrap_or("resource"); let name = reader.read_resource(resource, identifier, &path, pkg, &|reference| { linked(&relative_to(&file, reference)).filter(|bytes| bytes.len() <= remaining_data) @@ -68,6 +78,7 @@ pub(super) fn read(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, pkg: & if !imported.source.is_empty() { imported.source = relative_to(&file, &imported.source); } + if let Some(left) = remaining_data.checked_sub(imported.bytes.len()) { remaining_data = left; } else { @@ -77,6 +88,7 @@ pub(super) fn read(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, pkg: & } reader.external_resource_names.insert((file.clone(), path), name); } + Ok(()) })(); if let Err(error) = result { diff --git a/crates/formats/src/sbs/mod.rs b/crates/formats/src/sbs/mod.rs index 19cfa5d..242beee 100644 --- a/crates/formats/src/sbs/mod.rs +++ b/crates/formats/src/sbs/mod.rs @@ -24,6 +24,7 @@ mod parameter_widget; mod pattern_graph; mod placeholder; mod read_node; +mod sbs_document; mod sbs_read; mod sbs_reader; mod sbs_value; @@ -38,6 +39,7 @@ pub use layout_frame::LayoutFrame; pub use layout_item::LayoutItem; pub use library_layout::read_layout; pub use read::{is_sbs, read_sbs}; +pub use sbs_document::SbsDocument; pub use sbs_read::SbsRead; pub use write::write_sbs; diff --git a/crates/formats/src/sbs/parameter_array.rs b/crates/formats/src/sbs/parameter_array.rs index a67c84b..1019302 100644 --- a/crates/formats/src/sbs/parameter_array.rs +++ b/crates/formats/src/sbs/parameter_array.rs @@ -32,14 +32,16 @@ pub(super) fn read(node: &mut Node, implementation: XmlNode<'_, '_>) { None => [0.0; 4], }; let midpoint = parameters.get("midpoint").and_then(Value::as_f32).unwrap_or(0.5); - // Legacy gradient arrays use zero for their default linear segments. - // Treating it as a near-zero interpolation fraction clips successive ramps to white. + + // Legacy gradient arrays use zero for their default linear segments + // Treating it as a near-zero interpolation fraction clips successive ramps to white let midpoint = if midpoint == 0.0 { 0.5 } else { midpoint }; gradient.keys.push(GradientKey { pos: position, color, midpoint }); } _ => {} } } + if !curve.points.is_empty() { curve.points.sort_by(|left, right| left.x.total_cmp(&right.x)); node.params.insert("curve".into(), Value::Curve(curve)); diff --git a/crates/formats/src/sbs/read.rs b/crates/formats/src/sbs/read.rs index b4c6d8b..b043078 100644 --- a/crates/formats/src/sbs/read.rs +++ b/crates/formats/src/sbs/read.rs @@ -1,25 +1,23 @@ use super::{ - constant::{MAX_GRAPHS, MAX_XML_NODES, SNIFF_BYTES}, + constant::{MAX_GRAPHS, SNIFF_BYTES}, placeholder::report_placeholders, sbs_read::SbsRead, sbs_reader::SbsReader, warnings::Warnings, xml::{child_element, child_value, children_named}, }; -use crate::{FormatError, Result}; +use crate::Result; use materialcraft_core::Package; use roxmltree::Node as XmlNode; use std::collections::HashMap; pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option>) -> Result { - let text = std::str::from_utf8(bytes).map_err(|e| FormatError::BadPackage(format!("the SBS file is not UTF-8 text: {e}")))?; - let text = text.trim_start_matches('\u{feff}'); - let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES }; - let doc = roxmltree::Document::parse_with_options(text, options).map_err(|e| FormatError::BadPackage(format!("the SBS file is not valid XML: {e}")))?; + super::SbsDocument::parse(bytes)?.read(name, linked) +} + +pub(super) fn read_document(doc: &roxmltree::Document<'_>, name: &str, linked: &dyn Fn(&str) -> Option>) -> Result { + let text = doc.input_text(); let root = doc.root_element(); - if root.tag_name().name() != "package" { - return Err(FormatError::BadPackage("not an SBS package (the root element is not )".into())); - } let mut warnings = Warnings::default(); let file_stem = name.rsplit(['/', '\\']).next().unwrap_or(name); @@ -39,7 +37,9 @@ pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option Option MAX_GRAPHS { reader.warnings.add(format!("only the first {MAX_GRAPHS} graphs were read")); } @@ -78,6 +79,7 @@ pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option bool { let head = String::from_utf8_lossy(&bytes[..bytes.len().min(SNIFF_BYTES)]); let mut rest = head.trim_start_matches('\u{feff}'); diff --git a/crates/formats/src/sbs/resource_replacement.rs b/crates/formats/src/sbs/resource_replacement.rs index 2eacca6..6c4f09b 100644 --- a/crates/formats/src/sbs/resource_replacement.rs +++ b/crates/formats/src/sbs/resource_replacement.rs @@ -35,7 +35,7 @@ pub(super) fn load( target: &str, linked: &dyn Fn(&str) -> Option>, ) -> String { - // A missing replacement must never fall back to the old container or embedded payload. + // A missing replacement must never fall back to the old container or embedded payload let bytes = library::relative_path(target).ok().and_then(|_| linked(target)).filter(|bytes| bytes.len() as u64 <= crate::MAX_RESOURCE).unwrap_or_else(|| { reader.warnings.add(format!("resource `{path}` could not be loaded: replacement `{target}` is invalid, missing or exceeds 256 MiB")); @@ -60,15 +60,18 @@ pub(super) fn read_direct(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, if reference.trim().is_empty() || reader.direct_resource_names.contains_key(reference) { continue; } + let font = child_value(parameter, "name") == Some("fontdata"); let package_reference = reference.starts_with("pkg://"); if package_reference && (!font || reader.resource_name(reference).is_some()) { continue; } + if reader.direct_resource_names.len() >= 1024 { reader.warnings.add("Only the first 1024 direct resource references were read"); break; } + if font { let original = if package_reference { reader.resolve_reference(reference).0 } else { reference.to_string() }; let filename = original.rsplit(['/', '\\']).next().unwrap_or("font"); @@ -81,11 +84,13 @@ pub(super) fn read_direct(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, name } else { reader.warnings.add(format!("font `{reference}` and fallback `{}` are unavailable", library::FALLBACK_FONT)); - // Without a declaration there is no filename to invent: keep the unresolved SBS reference. + + // Without a declaration there is no filename to invent: keep the unresolved SBS reference if package_reference { reader.direct_resource_names.insert(reference.into(), reference.into()); continue; } + store(pkg, identifier, &path, reference, Vec::new()) }; if let Some(resource) = pkg.resources.last() { @@ -94,6 +99,7 @@ pub(super) fn read_direct(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, reader.direct_resource_names.insert(reference.into(), name); continue; } + let Some(target) = target(reference) else { continue }; let name = if let Some(name) = targets.get(&target) { name.clone() diff --git a/crates/formats/src/sbs/sbs_document.rs b/crates/formats/src/sbs/sbs_document.rs new file mode 100644 index 0000000..cb8d2e3 --- /dev/null +++ b/crates/formats/src/sbs/sbs_document.rs @@ -0,0 +1,69 @@ +use super::{ + LayoutFrame, SbsRead, + constant::{MAX_SBS_BYTES, MAX_XML_NODES}, +}; +use crate::{FormatError, Result}; + +/// A validated SBS XML document that can supply layout and graph data without reparsing. +pub struct SbsDocument<'input> { + document: roxmltree::Document<'input>, +} + +impl<'input> SbsDocument<'input> { + /// Parse borrowed input with the same byte, XML-node, and DTD limits as the SBS reader. + pub fn parse(bytes: &'input [u8]) -> Result { + if bytes.len() as u64 > MAX_SBS_BYTES { + return Err(FormatError::BadPackage(format!("the SBS file is {} bytes, over the {MAX_SBS_BYTES} byte limit", bytes.len()))); + } + + let text = std::str::from_utf8(bytes).map_err(|error| FormatError::BadPackage(format!("the SBS file is not UTF-8 text: {error}")))?; + let text = text.trim_start_matches('\u{feff}'); + let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES }; + let document = roxmltree::Document::parse_with_options(text, options) + .map_err(|error| FormatError::BadPackage(format!("the SBS file is not valid XML: {error}")))?; + + if document.root_element().tag_name().name() != "package" { + return Err(FormatError::BadPackage("not an SBS package (the root element is not )".into())); + } + + Ok(Self { document }) + } + + /// Read the layout independently of whether its frames name library categories. + pub fn layout(&self, uri: &str) -> Result> { + super::library_layout::read_document(&self.document, uri) + } + + /// Read owned package data; the result does not retain the XML document or input bytes. + pub fn read(&self, name: &str, linked: &dyn Fn(&str) -> Option>) -> Result { + super::read::read_document(&self.document, name, linked) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn package_data_outlives_the_shared_document() { + let bytes = b"".to_vec(); + let document = SbsDocument::parse(&bytes).unwrap(); + + let frames = document.layout("sbs://example.sbs").unwrap(); + let read = document.read("example.sbs", &|_| None).unwrap(); + drop(document); + drop(bytes); + + assert!(frames.is_empty()); + assert_eq!(read.pkg.graphs.len(), 1); + assert_eq!(read.pkg.graphs[0].name, "Example"); + assert!(read.warnings.is_empty()); + } + + #[test] + fn malformed_documents_fail_before_either_reader_runs() { + for bytes in [b"".as_slice(), b"", b"\xff", b"]>"] { + assert!(SbsDocument::parse(bytes).is_err()); + } + } +} diff --git a/crates/formats/src/sbs/sbs_reader.rs b/crates/formats/src/sbs/sbs_reader.rs index fe5a062..f520552 100644 --- a/crates/formats/src/sbs/sbs_reader.rs +++ b/crates/formats/src/sbs/sbs_reader.rs @@ -1,7 +1,7 @@ use super::base_parameters::read_base_params; use super::component_usage; use super::connection::connect_all; -use super::constant::MAX_NODES; +use super::constant::{MAX_NODES, MAX_WIRES}; use super::constant::{NODE_HALF_SIZE, SELF_DEPENDENCY}; use super::embedded::decode_embedded; use super::filter_map::{BASE_PARAMS, FilterMap, HANDLED_PARAMS, filter_by_sbs_name}; @@ -30,6 +30,7 @@ pub(super) struct SbsReader<'text, 'warnings> { pub(super) external_resource_names: HashMap<(String, String), String>, pub(super) direct_resource_names: HashMap, pub(super) node_count: usize, + pub(super) wire_count: usize, } impl<'text> SbsReader<'text, '_> { @@ -67,6 +68,7 @@ impl<'text> SbsReader<'text, '_> { if let Some(description) = child_value(item, "desc").filter(|text| !text.is_empty()) { pkg.folder_descriptions.insert(path.clone(), description.into()); } + if let Some(group_content) = child_element(item, "content") { pending.push((path, group_content)); } @@ -93,6 +95,7 @@ impl<'text> SbsReader<'text, '_> { { return super::resource_replacement::load(self, pkg, identifier, path, &target, linked); } + let external = source .and_then(|source| child_element(source, "externalcopy")) .and_then(|copy| child_value(copy, "filename")) @@ -131,11 +134,12 @@ impl<'text> SbsReader<'text, '_> { } } - // A supplied font, including cour.ttf or embedded data, always wins. Only absence uses the shared fallback. + // A supplied font, including cour.ttf or embedded data, always wins. Only absence uses the shared fallback if font && bytes.is_none() { if let Some(name) = super::resource_replacement::load_font(pkg, identifier, path, linked) { return name; } + let why = if why_missing.is_empty() { "it has no font data" } else { &why_missing }; why_missing = format!("{why}; fallback `{}` is unavailable", super::library::FALLBACK_FONT); } @@ -147,7 +151,8 @@ impl<'text> SbsReader<'text, '_> { let ext = if kind == "svg" { "svg".to_string() } else { format }; let mut resource = Resource::new(name.clone(), crate::safe_file_name(&format!("{name}.{ext}")), ResourceBytes::new(data)); - // Keep the link as well as the loaded bytes so library saves can retain shared assets. + + // Keep the link as well as the loaded bytes so library saves can retain shared assets resource.source = loaded_source.into(); resource.folder = path.rsplit_once('/').map_or("", |(folder, _)| folder).into(); pkg.resources.push(resource); @@ -168,6 +173,7 @@ impl<'text> SbsReader<'text, '_> { self.warnings.add(format!("resource `{path}` could not be loaded: {why}")); } } + name } @@ -232,6 +238,7 @@ impl<'text> SbsReader<'text, '_> { if let Some(dynamic) = dynamic { super::canvas::read_canvas_items(dynamic, &mut graph, self.warnings); } + return graph; } @@ -273,6 +280,7 @@ impl<'text> SbsReader<'text, '_> { super::canvas::read_canvas_items(element, &mut graph, self.warnings); graph } + pub(super) fn read_graph_inputs(&mut self, element: XmlNode, graph: &mut Graph) -> HashMap { let mut image_inputs = HashMap::new(); for input in child_element(element, "paraminputs").into_iter().flat_map(|p| children_named(p, "paraminput")) { @@ -289,6 +297,7 @@ impl<'text> SbsReader<'text, '_> { ); continue; } + if graph.exposed.iter().any(|e| e.id == id) { continue; } @@ -301,6 +310,7 @@ impl<'text> SbsReader<'text, '_> { ); continue; } + let label = child_element(input, "attributes").and_then(|a| child_value(a, "label")).filter(|l| !l.is_empty()).unwrap_or(&id).to_string(); let (mut min, mut max) = (0.0, 1.0); let widget_options = child_element(input, "defaultWidget").and_then(|w| child_element(w, "options")); @@ -331,8 +341,10 @@ impl<'text> SbsReader<'text, '_> { super::parameter_widget::read(input, &mut exposed); graph.exposed.push(exposed); } + image_inputs } + pub(super) fn read_wires(&mut self, graph: &mut Graph, comp_nodes: &[XmlNode], read_nodes: &HashMap) -> Vec { let name = graph.name.clone(); let mut wires = Vec::new(); @@ -368,6 +380,7 @@ impl<'text> SbsReader<'text, '_> { add_placeholder_input(node, sbs_input); } } + sbs_input.to_string() } None => { @@ -377,9 +390,16 @@ impl<'text> SbsReader<'text, '_> { } }; + if self.wire_count >= MAX_WIRES { + self.warnings.add(format!("graph `{name}`: the {MAX_WIRES} wire limit was reached; later wires were dropped")); + return wires; + } + + self.wire_count += 1; wires.push(Connection { from: from.id, from_port, to: to.id, to_port }); } } + wires } @@ -405,6 +425,7 @@ impl<'text> SbsReader<'text, '_> { if let Some(node) = node { fill_placeholder(node, "other", "empty node", "", None, &output_uids, None); } + return ReadNode { id, index, inputs: HashMap::new(), outputs: Vec::new(), open_inputs: true }; }; @@ -428,8 +449,10 @@ impl<'text> SbsReader<'text, '_> { if let Some(input) = input { node.params.insert("default".into(), input.default.clone()); } + node.base = read_base_params(params, self.warnings); } + ReadNode { id, index, inputs: HashMap::new(), outputs: all_outputs_to("output"), open_inputs: false } } "compOutputBridge" => { @@ -443,6 +466,7 @@ impl<'text> SbsReader<'text, '_> { node.params.insert("color_mode".into(), Value::Bool(channels == 1)); } } + let inputs = HashMap::from([("inputNodeOutput".to_string(), "input".to_string())]); ReadNode { id, index, inputs, outputs: Vec::new(), open_inputs: false } } @@ -454,8 +478,8 @@ impl<'text> SbsReader<'text, '_> { let (id, index, node) = push_node(graph, filter.def, pos); if let Some(node) = node { self.read_filter_params(node, filter, params, &graph_name); - // Native files can omit the color switch while declaring a color - // output. Retain that compiled image type instead of the editor preset. + + // Preserve a declared color output even when the color switch is omitted if let [(_, gray)] = output_uids.as_slice() && find(filter.def).is_some_and(|definition| { matches!(definition.param("color_mode").map(|parameter| ¶meter.default), Some(Value::Bool(_))) @@ -474,7 +498,7 @@ impl<'text> SbsReader<'text, '_> { .flat_map(|parameters| children_named(parameters, "parameter")) .any(|p| child_value(p, "name") == Some("format")) { - // A processor defaults to its parent format, even with an image input. + // A processor defaults to its parent format, even with an image input node.base.format = OutputFormat::U8; node.base.format_mode = ParameterInheritance::RelativeToParent; } @@ -489,6 +513,7 @@ impl<'text> SbsReader<'text, '_> { } } } + let inputs = filter.inputs.iter().map(|(sbs, port)| (sbs.to_string(), port.to_string())).collect(); ReadNode { id, index, inputs, outputs: all_outputs_to("output"), open_inputs: false } } @@ -497,6 +522,7 @@ impl<'text> SbsReader<'text, '_> { if let Some(node) = node { fill_placeholder(node, "filter", filter_name, "", params.map(|p| &text[p.range()]), &output_uids, None); } + ReadNode { id, index, inputs: HashMap::new(), outputs: placeholder_outputs(&output_uids, None), open_inputs: true } } } @@ -507,10 +533,12 @@ impl<'text> SbsReader<'text, '_> { if let Some(node) = node { fill_placeholder(node, "other", other, "", None, &output_uids, None); } + ReadNode { id, index, inputs: HashMap::new(), outputs: placeholder_outputs(&output_uids, None), open_inputs: true } } } } + pub(super) fn read_instance(&mut self, graph: &mut Graph, implementation: XmlNode<'_, 'text>, pos: [f32; 2], output_uids: &[(String, bool)]) -> ReadNode { let graph_name = graph.name.clone(); let (path, dependency) = self.resolve_reference(child_value(implementation, "path").unwrap_or("")); @@ -577,6 +605,7 @@ impl<'text> SbsReader<'text, '_> { ReadNode { id, index, inputs: HashMap::new(), outputs: placeholder_outputs(output_uids, Some(&bridgings)), open_inputs: true } } + pub(super) fn set_param(&mut self, node: &mut Node, id: &str, like: &Value, options: usize, param: XmlNode, graph_name: &str) { let Some(param_value) = child_element(param, "paramValue") else { return }; @@ -607,11 +636,11 @@ impl<'text> SbsReader<'text, '_> { let Some(def) = find(filter.def) else { return }; if filter.sbs == "uniform" { - // An omitted SBS output color is black; the editor's new-node preset is gray. + // An omitted SBS output color is black; the editor's new-node preset is gray node.params.insert("color".into(), Value::Color([0.0, 0.0, 0.0, 1.0])); } if filter.sbs == "normal" { - // Native files omit these defaults; the editor's new-node preset differs. + // Native files omit these defaults; the editor's new-node preset differs node.params.insert("opengl".into(), Value::Bool(false)); node.params.insert("height_alpha".into(), Value::Bool(true)); } @@ -620,7 +649,8 @@ impl<'text> SbsReader<'text, '_> { params.into_iter().flat_map(|p| children_named(p, "parameter")).filter(|p| !BASE_PARAMS.contains(&child_value(*p, "name").unwrap_or(""))).collect(); for param in &filter_params { let param_name = child_value(*param, "name").unwrap_or(""); - // Read the earlier writer's alias without emitting it alongside the native parameter. + + // Read the earlier writer's alias without emitting it alongside the native parameter let param_name = match (filter.sbs, param_name) { ("hsl", "luminance") => "luminosity", ("dyngradient", "position") => "coordinate", @@ -667,10 +697,12 @@ impl<'text> SbsReader<'text, '_> { if let Some(name) = self.direct_resource_names.get(url) { return Some(name); } + let dependency_uid = url.split_once('?').and_then(|(_, query)| query.split('&').find_map(|part| part.strip_prefix("dependency="))); if dependency_uid.is_some_and(|uid| !self.dependencies.contains_key(uid)) { return None; } + let (path, dependency) = self.resolve_reference(url); match dependency.filter(|file| file != SELF_DEPENDENCY) { Some(file) => self.external_resource_names.get(&(file, path)), @@ -682,6 +714,7 @@ impl<'text> SbsReader<'text, '_> { if let Some(name) = self.resource_name(url) { return Some(name.clone()); } + let dependency_uid = url.split_once('?').and_then(|(_, query)| query.split('&').find_map(|part| part.strip_prefix("dependency="))); if let Some(uid) = dependency_uid.filter(|uid| !self.dependencies.contains_key(*uid)) { self.warnings.add(format!("graph `{graph_name}`: resource reference has unknown dependency `{uid}`")); @@ -689,21 +722,26 @@ impl<'text> SbsReader<'text, '_> { let (path, _) = self.resolve_reference(url); self.warnings.add(format!("graph `{graph_name}`: Node uses resource `{path}`, which is not in the package")); } + None } } + pub(super) fn unique_name(taken: &[String], base: &str) -> String { let free = |name: &str| !taken.iter().any(|t| t == name); if free(base) { return base.to_string(); } + (1..).map(|i| format!("{base}_{i}")).find(|name| free(name)).unwrap_or_else(|| base.to_string()) } + pub(super) fn push_node<'g>(graph: &'g mut Graph, def: &str, pos: [f32; 2]) -> (NodeId, usize, Option<&'g mut Node>) { let id = graph.add_node(def, pos); let index = graph.nodes.len() - 1; (id, index, graph.nodes.last_mut()) } + pub(super) fn read_graph_outputs(element: XmlNode) -> HashMap { let mut outputs = HashMap::new(); for output in child_element(element, "graphOutputs").into_iter().flat_map(|o| children_named(o, "graphoutput")) { @@ -712,5 +750,6 @@ pub(super) fn read_graph_outputs(element: XmlNode) -> HashMap 0.0, } } + pub(super) fn int(&self, i: usize) -> i32 { match self { SbsValue::Ints(v) => v.get(i).or(v.first()).copied().unwrap_or(0), @@ -45,6 +46,7 @@ impl SbsValue { } } } + pub(super) fn sbs_constant(holder: XmlNode) -> Option { sbs_constant_element(holder.children().find(|child| child.is_element())?) } @@ -65,6 +67,7 @@ pub(super) fn sbs_constant_element(element: XmlNode) -> Option { None } } + pub(super) fn to_param_value(like: &Value, value: &SbsValue, options: usize) -> Option { let f = |i| value.float(i); @@ -73,8 +76,8 @@ pub(super) fn to_param_value(like: &Value, value: &SbsValue, options: usize) -> Value::Float2(_) => Value::Float2([f(0), f(1)]), Value::Float3(_) => Value::Float3([f(0), f(1), f(2)]), Value::Float4(_) => Value::Float4([f(0), f(1), f(2), f(3)]), - // These are graph components, including grayscale data and function inputs. - // Applying a display transfer here changes the arithmetic performed by nodes. + // These are graph components, including grayscale data and function inputs + // Applying a display transfer here changes the arithmetic performed by nodes Value::Color(_) if value.component_count() >= 3 => { let alpha = if value.component_count() >= 4 { f(3) } else { 1.0 }; @@ -100,6 +103,7 @@ pub(super) fn to_param_value(like: &Value, value: &SbsValue, options: usize) -> Value::Curve(_) | Value::Gradient(_) | Value::Function(_) | Value::PatternGraph(_) => return None, }) } + pub(super) fn to_natural_value(value: &SbsValue) -> Value { let f = |i| value.float(i); @@ -118,6 +122,7 @@ pub(super) fn to_natural_value(value: &SbsValue) -> Value { }, } } + pub(super) fn exposed_default(code: i64, value: Option<&SbsValue>) -> Value { use type_code::*; diff --git a/crates/formats/src/sbs/sbs_writer.rs b/crates/formats/src/sbs/sbs_writer.rs index 783cb84..827173d 100644 --- a/crates/formats/src/sbs/sbs_writer.rs +++ b/crates/formats/src/sbs/sbs_writer.rs @@ -36,6 +36,7 @@ impl SbsWriter { self.external_packages.insert(file.to_string(), uid); uid } + pub(super) fn output_ports(pkg: &Package, graph: &Graph, node: &Node) -> Vec<(String, u32)> { match node.def.as_str() { "output" => Vec::new(), @@ -43,6 +44,7 @@ impl SbsWriter { if let Some(instance) = &node.instance { return instance.outputs.iter().map(|port| (port.identifier.clone(), port.type_code)).collect(); } + let sub_graph = match node.params.get("graph") { Some(Value::Text(name)) => pkg.graph_by_name(name).map(|(_, g)| g), _ => None, @@ -82,6 +84,7 @@ impl SbsWriter { for wire in &graph.connections { wires_into.entry(wire.to).or_default().push(wire); } + out.push_str(""); for node in &graph.nodes { let uid = node_uids.get(&node.id).copied().unwrap_or(0); @@ -141,6 +144,7 @@ impl SbsWriter { self.write_implementation(out, node, outputs, ids, pkg)?; out.push_str(""); } + out.push_str(""); out.push_str(¶m_xml("randomseed", &Value::Int(graph.seed)).unwrap_or_default()); let base = materialcraft_core::BaseParameters { @@ -166,6 +170,7 @@ impl SbsWriter { out.push_str(""); Ok(()) } + pub(super) fn write_graph_inputs(&mut self, out: &mut String, graph: &Graph) -> HashMap { let mut entries = HashMap::new(); out.push_str(""); @@ -212,6 +217,7 @@ impl SbsWriter { out.push_str(""); entries } + pub(super) fn write_graph_outputs(&mut self, out: &mut String, graph: &Graph) -> HashMap { let mut output_uids = HashMap::new(); out.push_str(""); diff --git a/crates/formats/src/sbs/tests.rs b/crates/formats/src/sbs/tests.rs index 9a63f7f..8a8f397 100644 --- a/crates/formats/src/sbs/tests.rs +++ b/crates/formats/src/sbs/tests.rs @@ -81,6 +81,7 @@ fn first_sbs_save_assigns_the_workspace_version_to_both_fields() { let unversioned = read_sbs(b"", "unversioned.sbs", &no_linked_files).unwrap().pkg; for package in [Package::new("New file"), unversioned] { assert!(package.format_version.is_none()); + let text = String::from_utf8(write_sbs(&package).unwrap()).unwrap(); let document = roxmltree::Document::parse(&text).unwrap(); for field in ["formatVersion", "updaterVersion"] { @@ -96,12 +97,15 @@ fn later_sbs_saves_keep_the_original_format_version_and_refresh_the_updater() { let mut package = read_sbs(old.as_bytes(), "older.sbs", &no_linked_files).unwrap().pkg; for _ in 0..3 { assert_eq!(package.format_version.as_deref(), Some(baseline)); - // Snapshot/restore and subsequent saves must retain the baseline too. + + // Snapshot/restore and subsequent saves must retain the baseline too package = serde_json::from_slice(&serde_json::to_vec(&package).unwrap()).unwrap(); let bytes = write_sbs(&package).unwrap(); let document = roxmltree::Document::parse(std::str::from_utf8(&bytes).unwrap()).unwrap(); + assert_eq!(super::xml::child_value(document.root_element(), "formatVersion"), Some(baseline)); assert_eq!(super::xml::child_value(document.root_element(), "updaterVersion"), Some(env!("CARGO_PKG_VERSION"))); + package = read_sbs(&bytes, "saved.sbs", &no_linked_files).unwrap().pkg; } } diff --git a/crates/formats/src/sbs/warnings.rs b/crates/formats/src/sbs/warnings.rs index 86406ac..c1efd04 100644 --- a/crates/formats/src/sbs/warnings.rs +++ b/crates/formats/src/sbs/warnings.rs @@ -5,6 +5,8 @@ use std::collections::HashMap; pub(super) struct Warnings { pub(super) lines: Vec<(String, usize)>, pub(super) line_index: HashMap, + /// Messages over the limit: they are counted but never stored (audit: MaterialCraft #2). + dropped: usize, } impl Warnings { @@ -15,17 +17,44 @@ impl Warnings { return; } + if self.lines.len() >= MAX_WARNINGS { + self.dropped += 1; + return; + } + self.line_index.insert(message.clone(), self.lines.len()); self.lines.push((message, 1)); } pub(super) fn finish(self) -> Vec { - let total = self.lines.len(); - let mut out: Vec = + let total_messages = self.lines.len() + self.dropped; + let mut messages: Vec = self.lines.into_iter().take(MAX_WARNINGS).map(|(line, count)| if count > 1 { format!("{line} (×{count})") } else { line }).collect(); - if total > MAX_WARNINGS { - out.push(format!("…and {} more warnings", total - MAX_WARNINGS)); + if total_messages > MAX_WARNINGS { + messages.push(format!("…and {} more warnings", total_messages - MAX_WARNINGS)); } - out + + messages + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn warning_messages_are_capped_as_they_arrive() { + let mut warnings = Warnings::default(); + + for i in 0..MAX_WARNINGS + 5 { + warnings.add(format!("warning {i}")); + } + + assert_eq!(warnings.lines.len(), MAX_WARNINGS); + + let messages = warnings.finish(); + + assert_eq!(messages.len(), MAX_WARNINGS + 1); + assert_eq!(messages.last().unwrap(), "…and 5 more warnings"); } } diff --git a/crates/formats/tests/font_fallback.rs b/crates/formats/tests/font_fallback.rs index 6aeb4b4..de10dd9 100644 --- a/crates/formats/tests/font_fallback.rs +++ b/crates/formats/tests/font_fallback.rs @@ -31,15 +31,20 @@ fn existing_relative_fonts_including_cour_take_precedence_over_the_fallback() { if path == "empty.ttf" { return Some(Vec::new()); } + assert_eq!(path, file, "an available font must not read the fallback"); + Some(vec![1, 2, 3]) }) .unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(read.pkg.resources.len(), 1); assert_eq!(read.pkg.resources[0].source, file); assert_eq!(read.pkg.resources[0].bytes.as_slice(), &[1, 2, 3]); + let reopened = read_sbs(&write_sbs(&read.pkg).unwrap(), "moved.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert_eq!(reopened.pkg.resources[0].bytes.as_slice(), &[1, 2, 3]); } @@ -51,29 +56,38 @@ fn every_missing_font_uses_liberation_mono_and_survives_a_portable_save() { for file in ["cour.ttf", "fonts/Other.otf", "fonts/Collection.ttc", "sbs://resources/cursor.otf"] { for xml in [resource(file), direct(file)] { let read = read_sbs(xml.as_bytes(), "example.sbs", &|path| (path == FALLBACK).then(|| vec![4, 5, 6])).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(read.pkg.resources.len(), 1); + let font = &read.pkg.resources[0]; + assert_eq!(font.source, FALLBACK); assert_eq!(font.file, "LiberationMono-Regular.ttf"); assert_eq!(font.bytes.as_slice(), &[4, 5, 6]); + if let Some(graph) = read.pkg.graphs.first() { assert_eq!(graph.nodes[0].params["fontdata"], Value::Text(font.name.clone())); } + let reopened = read_sbs(&write_sbs(&read.pkg).unwrap(), "moved.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert_eq!(reopened.pkg.resources[0].bytes.as_slice(), &[4, 5, 6]); - // An unavailable fallback must keep the SBS reference repairable after saving. + // An unavailable fallback must keep the SBS reference repairable after saving for fallback in [None, Some(Vec::new())] { let missing = read_sbs(xml.as_bytes(), "example.sbs", &|path| if path == FALLBACK { fallback.clone() } else { None }).unwrap(); let original = &missing.pkg.resources[0]; + assert_eq!(original.source, file); assert_eq!(original.file, file.rsplit(['/', '\\']).next().unwrap()); assert!(original.bytes.is_empty()); assert!(!missing.warnings.is_empty()); + let saved = write_sbs(&missing.pkg).unwrap(); let repaired = read_sbs(&saved, "example.sbs", &|path| (path == file).then(|| vec![7, 8, 9])).unwrap(); + assert!(repaired.warnings.is_empty(), "{:?}", repaired.warnings); assert_eq!(repaired.pkg.resources[0].name, original.name); assert_eq!(repaired.pkg.resources[0].bytes.as_slice(), &[7, 8, 9]); @@ -91,16 +105,22 @@ fn every_missing_font_uses_liberation_mono_and_survives_a_portable_save() { _ => None, }) .unwrap(); + assert_eq!(read.pkg.resources.len(), 1); + let font = &read.pkg.resources[0]; + assert_eq!(font.name, "Original"); assert_eq!(font.bytes.as_slice(), &[4, 5, 6]); assert_eq!(read.pkg.graphs[0].nodes[0].params["fontdata"], Value::Text(font.name.clone())); + let reopened = read_sbs(&write_sbs(&read.pkg).unwrap(), "moved.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert_eq!(reopened.pkg.resources[0].name, "Original"); let missing = read_sbs(xml.as_bytes(), "example.sbs", &|_| None).unwrap(); + assert!(missing.pkg.resources.is_empty()); assert_eq!(missing.pkg.graphs[0].nodes[0].params["fontdata"], Value::Text(reference.into())); } diff --git a/crates/formats/tests/foreign_resources.rs b/crates/formats/tests/foreign_resources.rs index eca1eff..d2a1d74 100644 --- a/crates/formats/tests/foreign_resources.rs +++ b/crates/formats/tests/foreign_resources.rs @@ -38,9 +38,11 @@ fn foreign_resource_uses_its_owner_without_shadowing_local_names_and_round_trips } }) .unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(reads.borrow().iter().filter(|path| *path == "nested/source.sbs").count(), 1); assert_eq!(read.pkg.resources.len(), 2); + let names: Vec<_> = read.pkg.graphs[0] .nodes .iter() @@ -49,14 +51,19 @@ fn foreign_resource_uses_its_owner_without_shadowing_local_names_and_round_trips _ => panic!("resource should be text"), }) .collect(); + assert_ne!(names[0], names[1]); assert_eq!(names[1], names[2]); assert_eq!(read.pkg.resource(names[0]).unwrap().bytes.as_slice(), &[1, 2, 3]); + let foreign = read.pkg.resource(names[1]).unwrap(); + assert_eq!(foreign.bytes.as_slice(), &[4, 5, 6]); assert_eq!(foreign.source, "nested/../resources/tile.png"); + let saved = write_sbs(&read.pkg).unwrap(); let reopened = read_sbs(&saved, "relocated.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert_eq!(reopened.pkg.resource(names[1]).unwrap().bytes.as_slice(), &[4, 5, 6]); assert_eq!(reopened.pkg.graphs[0].nodes[1].params["resource"], Value::Text(names[1].into())); @@ -75,12 +82,15 @@ fn missing_or_invalid_foreign_resource_never_falls_back_to_a_local_name() { } }) .unwrap(); + assert!(!read.warnings.is_empty()); assert!(!read.pkg.graphs[0].nodes[1].params.contains_key("resource")); assert_eq!(read.pkg.resources.len(), 1); } + let unknown = package().replace("?dependency=2", "?dependency=99"); let read = read_sbs(unknown.as_bytes(), "caller.sbs", &|path| (path == "resources/local.png").then(|| vec![1])).unwrap(); + assert!(read.warnings.iter().any(|warning| warning.contains("unknown dependency"))); assert!(!read.pkg.graphs[0].nodes[1].params.contains_key("resource")); } diff --git a/crates/formats/tests/function_library.rs b/crates/formats/tests/function_library.rs index 9811ad5..0c85755 100644 --- a/crates/formats/tests/function_library.rs +++ b/crates/formats/tests/function_library.rs @@ -161,16 +161,21 @@ fn bundled_library_has_complete_bodies_and_current_export_metadata() { let bytes = include_bytes!("../../../assets/sbs/functions.sbs"); let read = read_sbs(bytes, "functions.sbs", &|_| None).unwrap(); let xml = roxmltree::Document::parse(std::str::from_utf8(bytes).unwrap()).unwrap(); + // The SBS schema version is independent of the MaterialCraft release version for (field, expected) in [("formatVersion", "1.1.0.202401"), ("updaterVersion", env!("CARGO_PKG_VERSION"))] { let version = xml.root_element().children().find(|element| element.has_tag_name(field)).unwrap().attribute("v").unwrap(); + assert_eq!(version, expected, "bundled functions {field}"); } + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(read.pkg.graphs.len(), 171); assert_eq!(read.pkg.graphs.iter().flat_map(|graph| graph.functions()).map(|function| function.nodes.len()).sum::(), 2334); + for graph in &read.pkg.graphs { let function = graph.function.as_ref().unwrap(); + assert!(function.output.is_some(), "{}", graph.sbs_path()); assert!(function.nodes.iter().all(|node| node.missing.is_none()), "{}", graph.sbs_path()); assert!(!graph.attributes.description.is_empty()); @@ -178,15 +183,19 @@ fn bundled_library_has_complete_bodies_and_current_export_metadata() { assert!(graph.frames.is_empty() && graph.comments.is_empty() && graph.pins.is_empty()); } let pi = &read.pkg.graph_by_name("Functions/Math/Pi").unwrap().1.attributes; + assert!(pi.category.is_empty()); assert_eq!(pi.tags, "Constant"); + let saved = write_sbs(&read.pkg).unwrap(); let reopened = read_sbs(&saved, "functions.sbs", &|_| None).unwrap(); for graph in &read.pkg.graphs { let reloaded = reopened.pkg.graph_by_name(&graph.sbs_path()).unwrap().1; + assert_eq!(reloaded.attributes.category, graph.attributes.category); assert_eq!(reloaded.attributes.tags, graph.attributes.tags); } + assert!(!xml.descendants().any(|element| element.has_tag_name("icon"))); } diff --git a/crates/formats/tests/fx_map_roughness.rs b/crates/formats/tests/fx_map_roughness.rs index ac3f7a6..ad1b67a 100644 --- a/crates/formats/tests/fx_map_roughness.rs +++ b/crates/formats/tests/fx_map_roughness.rs @@ -19,19 +19,24 @@ fn native_hurst_constants_and_functions_survive_saving() { let mut read = read_sbs(xml.as_bytes(), "roughness.sbs", &|_| None).unwrap(); for _ in 0..2 { assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let node = &read.pkg.graphs[0].nodes[0]; if value.contains("dynamicValue") { let function = &node.functions["roughness"]; + assert_eq!(function.nodes.len(), 1); assert_eq!(function.output, Some(function.nodes[0].id)); assert_eq!(function.nodes[0].value, Some(Value::Float(0.75))); } else { assert_eq!(node.params["roughness"], Value::Float(0.75)); } + let bytes = write_sbs(&read.pkg).unwrap(); let text = std::str::from_utf8(&bytes).unwrap(); + assert!(text.contains("")); assert!(!text.contains("")); + read = read_sbs(&bytes, "resaved.sbs", &|_| None).unwrap(); } } diff --git a/crates/formats/tests/gradient_values.rs b/crates/formats/tests/gradient_values.rs index 93ef5c4..14186ad 100644 --- a/crates/formats/tests/gradient_values.rs +++ b/crates/formats/tests/gradient_values.rs @@ -7,9 +7,12 @@ fn an_omitted_native_uniform_color_is_black() { "#; let read = read_sbs(xml, "noise.sbs", &|_| None).unwrap(); + assert_eq!(read.pkg.graphs[0].nodes[0].params["color"], Value::Color([0.0, 0.0, 0.0, 1.0])); + let saved = write_sbs(&read.pkg).unwrap(); let reopened = read_sbs(&saved, "saved.sbs", &|_| None).unwrap(); + assert_eq!(reopened.pkg.graphs[0].nodes[0].params["color"], read.pkg.graphs[0].nodes[0].params["color"]); } @@ -39,11 +42,14 @@ fn native_gradient_default_midpoints_are_linear_and_components_are_preserved() { let mut read = read_sbs(xml.as_bytes(), "ramp.sbs", &|_| None).unwrap(); for _ in 0..3 { assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let Value::Gradient(gradient) = &read.pkg.graphs[0].nodes[0].params["gradient"] else { panic!("missing gradient") }; let end = 0.5; + assert!((gradient.eval(1.0)[0] - end).abs() < 1e-6); assert!((gradient.eval(0.5)[0] - end * 0.5).abs() < 1e-6); assert!((gradient.eval(0.5)[3] - if color { 0.4 } else { 1.0 }).abs() < 1e-6); + let saved = write_sbs(&read.pkg).unwrap(); read = read_sbs(&saved, "saved.sbs", &|_| None).unwrap(); } diff --git a/crates/formats/tests/graph_attributes.rs b/crates/formats/tests/graph_attributes.rs index dbab271..bd48ee7 100644 --- a/crates/formats/tests/graph_attributes.rs +++ b/crates/formats/tests/graph_attributes.rs @@ -18,12 +18,15 @@ fn native_graph_attributes_load_and_survive_sbs_save() { "tags": "math utility", "hideInLibrary": true, "group": "Utilities", "userData": "pipeline=example"}); assert_eq!(serde_json::to_value(&imported.graphs[0].attributes).unwrap(), expected, "{kind} import"); + let bytes = write_sbs(&imported).unwrap(); let saved = read_sbs(&bytes, "saved.sbs", &|_| None).unwrap().pkg; assert_eq!(saved.graphs[0].name, "Stable_identifier"); assert_eq!(serde_json::to_value(&saved.graphs[0].attributes).unwrap(), expected, "{kind} save"); + let xml = String::from_utf8(bytes).unwrap(); + assert!(xml.contains("")); diff --git a/crates/formats/tests/library_native_ports.rs b/crates/formats/tests/library_native_ports.rs index 000fd82..1805584 100644 --- a/crates/formats/tests/library_native_ports.rs +++ b/crates/formats/tests/library_native_ports.rs @@ -13,12 +13,17 @@ fn fx_map_render_and_tiling_regions_keep_native_names_and_functions() { let mut read = read_sbs(xml, "regions.sbs", &|_| None).unwrap(); for _ in 0..2 { assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let node = &read.pkg.graphs[0].nodes[0]; + assert_eq!(node.params["tiling_region"], Value::Float4([0.0, 2.0, 0.0, 1.0])); assert_eq!(node.functions["render_region"].nodes[0].value, Some(Value::Float4([0.0, 0.5, 0.0, 1.0]))); + let bytes = write_sbs(&read.pkg).unwrap(); let saved = std::str::from_utf8(&bytes).unwrap(); + assert!(saved.contains("") && saved.contains("")); + read = read_sbs(&bytes, "saved.sbs", &|_| None).unwrap(); } } @@ -35,15 +40,22 @@ fn native_dynamic_gradient_coordinates_and_orientation_survive_saving() { let mut read = read_sbs(xml, "gradient.sbs", &|_| None).unwrap(); for _ in 0..2 { assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let node = &read.pkg.graphs[0].nodes[0]; + assert_eq!(node.params["orientation"], materialcraft_core::Value::Enum(1)); assert_eq!(node.functions["position"].nodes[0].value, Some(materialcraft_core::Value::Float(0.25))); + let bytes = write_sbs(&read.pkg).unwrap(); let saved = std::str::from_utf8(&bytes).unwrap(); + assert!(saved.contains("") && saved.contains("")); + read = read_sbs(&bytes, "saved.sbs", &|_| None).unwrap(); } + let legacy = std::str::from_utf8(xml).unwrap().replace("coordinate", "position").replace("uvselector", "orientation"); + assert!(read_sbs(legacy.as_bytes(), "legacy.sbs", &|_| None).unwrap().warnings.is_empty()); } @@ -53,13 +65,18 @@ fn linked_resources_retain_the_successful_path_but_embedded_resources_do_not_lin "#; let read = read_sbs(xml, "example.sbs", &|path| (path == "resources/example.ttf").then(|| vec![1, 2, 3])).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(read.pkg.resources[0].source, "resources/example.ttf"); + let saved = write_sbs(&read.pkg).unwrap(); let saved = String::from_utf8(saved).unwrap(); + assert!(saved.contains("")); + let saved = saved.replace("", ""); let embedded = read_sbs(saved.as_bytes(), "saved.sbs", &|_| None).unwrap(); + assert!(embedded.warnings.is_empty(), "{:?}", embedded.warnings); assert!(embedded.pkg.resources[0].source.is_empty()); assert_eq!(embedded.pkg.resources[0].bytes.as_slice(), &[1, 2, 3]); @@ -78,20 +95,29 @@ fn native_pattern_inputs_and_hsl_luminosity_round_trip() { "#; let read = read_sbs(xml.as_bytes(), "example.sbs", &|_| None).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let graph = &read.pkg.graphs[0]; + assert_eq!(graph.connections.len(), 1); assert_eq!(graph.connections[0].to_port, "pattern2"); assert_eq!(graph.nodes.iter().find(|node| node.def == "hsl").unwrap().params["lightness"], materialcraft_core::Value::Float(0.75)); + let saved = write_sbs(&read.pkg).unwrap(); + assert!(std::str::from_utf8(&saved).unwrap().contains("inputpattern:2")); assert!(std::str::from_utf8(&saved).unwrap().contains("")); assert!(!std::str::from_utf8(&saved).unwrap().contains("")); + let reopened = read_sbs(&saved, "saved.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert_eq!(reopened.pkg.graphs[0].connections.len(), 1); + let legacy = xml.replace("luminosity", "luminance").replace("inputpattern:2", "input:2"); let legacy = read_sbs(legacy.as_bytes(), "legacy.sbs", &|_| None).unwrap(); + assert!(legacy.warnings.is_empty(), "{:?}", legacy.warnings); assert_eq!(legacy.pkg.graphs[0].connections.len(), 1); assert_eq!(legacy.pkg.graphs[0].nodes.iter().find(|node| node.def == "hsl").unwrap().params["lightness"], materialcraft_core::Value::Float(0.75)); diff --git a/crates/formats/tests/project_packages.rs b/crates/formats/tests/project_packages.rs index 3989cff..3b4cb69 100644 --- a/crates/formats/tests/project_packages.rs +++ b/crates/formats/tests/project_packages.rs @@ -12,10 +12,13 @@ fn archived_sbs_replaces_surface_references_from_the_shared_library() { let xml = br#""#; let archive = zip(&[("package.sbs", xml), ("old/sun.surface", b"old container must never be read")]); let read = project::read(&archive, "example.mcraft").unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(read.pkg.resources[0].file, "sun.exr"); assert_eq!(read.pkg.resources[0].bytes.as_slice(), materialcraft_formats::sbs::library::read("sbs://resources/sun.exr").unwrap()); + let files = ProjectFiles::build(&read.pkg, "saved.sbs", false).unwrap(); + assert!(files.files.keys().any(|path| path.ends_with(".exr"))); assert!(!String::from_utf8_lossy(files.xml().unwrap()).contains(".surface")); } diff --git a/crates/formats/tests/resource_replacements.rs b/crates/formats/tests/resource_replacements.rs index 94a765b..b5fa891 100644 --- a/crates/formats/tests/resource_replacements.rs +++ b/crates/formats/tests/resource_replacements.rs @@ -25,7 +25,7 @@ fn surface_resources_use_library_replacements_before_embedded_or_external_data() ("sbs://resources/sun.surface", "surface", "sun.exr", "bitmap", "bitmapresourcepath", "resource"), ("../textures/arbitrary.SURFACE", "surface", "arbitrary.exr", "bitmap", "bitmapresourcepath", "resource"), ] { - // Valid old embedded data must not take precedence over the requested replacement. + // Valid old embedded data must not take precedence over the requested replacement let mut original = materialcraft_core::Package::new("Old"); original.resources.push(materialcraft_core::Resource::new("asset", format!("asset.{format}"), materialcraft_core::ResourceBytes::new(vec![99]))); let embedded = String::from_utf8(write_sbs(&original).unwrap()).unwrap(); @@ -39,15 +39,20 @@ fn surface_resources_use_library_replacements_before_embedded_or_external_data() (path == target).then(|| vec![1, 2, 3]) }) .unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(reads.borrow().as_slice(), std::slice::from_ref(&target)); + let imported = &read.pkg.resources[0]; + assert_eq!(imported.file, expected); assert_eq!(imported.source, target); assert_eq!(imported.bytes.as_slice(), &[1, 2, 3]); assert_eq!(read.pkg.graphs[0].nodes[0].params[key], Value::Text(imported.name.clone())); + let saved = write_sbs(&read.pkg).unwrap(); let reopened = read_sbs(&saved, "moved.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert!(reopened.pkg.resources[0].file.ends_with(expected.rsplit_once('.').unwrap().1)); assert_eq!(reopened.pkg.resources[0].bytes.as_slice(), &[1, 2, 3]); @@ -60,14 +65,18 @@ fn missing_replacement_keeps_repairable_target_and_never_reads_the_old_file() { let xml = package(&resource("old/sun.surface", "surface", ""), ""); let read = read_sbs(xml.as_bytes(), "example.sbs", &|path| { assert_eq!(path, "sbs://resources/sun.exr"); + None }) .unwrap(); + assert_eq!(read.pkg.resources[0].file, "sun.exr"); assert_eq!(read.pkg.resources[0].source, "sbs://resources/sun.exr"); assert!(read.pkg.resources[0].bytes.is_empty()); assert!(read.warnings.iter().any(|w| w.contains("sbs://resources/sun.exr"))); + let saved = String::from_utf8(write_sbs(&read.pkg).unwrap()).unwrap(); + assert!(!saved.contains(".surface")); assert!(saved.contains("sbs://resources/sun.exr")); } @@ -78,13 +87,17 @@ fn external_copy_and_direct_bitmap_references_are_replaced_and_invalid_targets_a let xml = package(&(resource("", "surface", external) + &node("bitmap", "bitmapresourcepath", "old/shuf4_64_32.surface")), ""); let read = read_sbs(xml.as_bytes(), "example.sbs", &|path| { assert_eq!(path, "sbs://resources/shuf4_64_32.exr"); + Some(vec![8]) }) .unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert!(read.pkg.resources.iter().all(|r| r.file == "shuf4_64_32.exr" && r.bytes.as_slice() == [8])); + let xml = package(&resource("old/invalid%name.surface", "surface", ""), ""); let read = read_sbs(xml.as_bytes(), "example.sbs", &|_| panic!("invalid target must not be read")).unwrap(); + assert!(read.pkg.resources[0].bytes.is_empty()); assert!(read.pkg.resources[0].file.ends_with(".exr")); assert!(read.warnings.iter().any(|w| w.contains("invalid"))); @@ -101,11 +114,14 @@ fn foreign_font_binding_is_imported_once_and_round_trips() { _ => None, }) .unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); assert_eq!(read.pkg.resources.len(), 1); assert_eq!(read.pkg.resources[0].file, "LiberationMono-Regular.ttf"); assert_eq!(read.pkg.graphs[0].nodes[0].params["fontdata"], Value::Text("asset".into())); + let reopened = read_sbs(&write_sbs(&read.pkg).unwrap(), "moved.sbs", &|_| None).unwrap(); + assert!(reopened.warnings.is_empty(), "{:?}", reopened.warnings); assert_eq!(reopened.pkg.graphs[0].nodes[0].params["fontdata"], Value::Text("asset".into())); assert_eq!(reopened.pkg.resources[0].bytes.as_slice(), &[4, 5, 6]); diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs index 025de8b..000f628 100644 --- a/crates/geom/src/mesh.rs +++ b/crates/geom/src/mesh.rs @@ -59,6 +59,7 @@ impl Mesh { if self.positions.is_empty() { return (Vec3::ZERO, Vec3::ZERO); } + self.positions.iter().fold((Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)), |(lo, hi), p| (lo.min(*p), hi.max(*p))) } @@ -97,33 +98,40 @@ impl Mesh { self.tangents.len() ))); } + if !self.colors.is_empty() && self.colors.len() != n { return Err(GeomError::Invalid(format!("{} vertex colors for {n} vertices", self.colors.len()))); } + if !self.indices.len().is_multiple_of(3) { return Err(GeomError::Invalid("index count is not a multiple of 3".into())); } + if self.triangle_count() > MAX_TRIANGLES { return Err(GeomError::Invalid(format!("mesh has more than {MAX_TRIANGLES} triangles"))); } + if let Some(bad) = self.indices.iter().find(|i| **i as usize >= n) { return Err(GeomError::Invalid(format!("index {bad} out of range ({n} vertices)"))); } + if self.positions.iter().any(|p| !p.is_finite()) { return Err(GeomError::Invalid("mesh has non-finite positions".into())); } + for s in &self.submeshes { if s.start as usize + s.count as usize > self.indices.len() || s.material >= self.materials.len().max(1) { return Err(GeomError::Invalid(format!("submesh {} is out of range", s.name))); } } + Ok(()) } /// Area-weighted smooth normals over the index buffer. pub fn compute_normals(&mut self) { let mut n = vec![Vec3::ZERO; self.positions.len()]; - for t in self.indices.chunks_exact(3) { + for t in self.indices.as_chunks::<3>().0 { let (a, b, c) = (t[0] as usize, t[1] as usize, t[2] as usize); let (Some(pa), Some(pb), Some(pc)) = (self.positions.get(a), self.positions.get(b), self.positions.get(c)) else { continue }; let face = (*pb - *pa).cross(*pc - *pa); @@ -133,6 +141,7 @@ impl Mesh { } } } + self.normals = n.into_iter().map(|v| v.try_normalize().unwrap_or(Vec3::Y)).collect(); } @@ -145,14 +154,16 @@ impl Mesh { if self.uvs.len() != self.positions.len() { self.uvs = vec![Vec2::ZERO; self.positions.len()]; } - // mikktspace crashes (out-of-bounds) when every triangle is degenerate; skip it then. + + // mikktspace crashes (out-of-bounds) when every triangle is degenerate; skip it then let usable = !crate::unwrap::all_triangles_degenerate(self); let mut g = Mikk { mesh: self }; if !usable || !mikktspace::generate_tangents(&mut g) { self.simple_tangents(); } + for (t, n) in self.tangents.iter_mut().zip(&self.normals) { - // Orthonormalize against the normal; repair degenerate results. + // Orthonormalize against the normal; repair degenerate results let v = t.truncate(); let o = (v - *n * n.dot(v)).try_normalize().unwrap_or_else(|| n.any_orthonormal_vector()); *t = o.extend(if t.w < 0.0 { -1.0 } else { 1.0 }); @@ -161,7 +172,7 @@ impl Mesh { fn simple_tangents(&mut self) { let mut acc = vec![Vec3::ZERO; self.positions.len()]; - for t in self.indices.chunks_exact(3) { + for t in self.indices.as_chunks::<3>().0 { let (a, b, c) = (t[0] as usize, t[1] as usize, t[2] as usize); let (Some(pa), Some(pb), Some(pc)) = (self.positions.get(a), self.positions.get(b), self.positions.get(c)) else { continue }; let (Some(ua), Some(ub), Some(uc)) = (self.uvs.get(a), self.uvs.get(b), self.uvs.get(c)) else { continue }; @@ -171,6 +182,7 @@ impl Mesh { if r.abs() < 1e-12 { continue; } + let tan = (e1 * d2.y - e2 * d1.y) / r; for v in [a, b, c] { if let Some(x) = acc.get_mut(v) { @@ -178,6 +190,7 @@ impl Mesh { } } } + self.tangents = acc.into_iter().map(|t| t.try_normalize().unwrap_or(Vec3::X).extend(1.0)).collect(); } @@ -210,14 +223,16 @@ impl Mesh { *x = true; } } + let mut shared = vec![false; self.positions.len()]; - for (_, tri) in self.indices.chunks_exact(3).enumerate().filter(|(t, _)| !is_mine.get(*t).copied().unwrap_or(false)) { + for (_, tri) in self.indices.as_chunks::<3>().0.iter().enumerate().filter(|(t, _)| !is_mine.get(*t).copied().unwrap_or(false)) { for v in tri { if let Some(s) = shared.get_mut(*v as usize) { *s = true; } } } + let mut copies = std::collections::HashMap::new(); for k in mine.iter().flat_map(|t| t * 3..t * 3 + 3) { let Some(&v) = self.indices.get(k) else { continue }; @@ -226,13 +241,15 @@ impl Mesh { *i = v; } } - // (vertex, centroid of its first triangle), each vertex once. + + // (vertex, centroid of its first triangle), each vertex once let mut verts = Vec::with_capacity(mine.len() * 3); for t in &mine { let Some(tri) = self.tri(*t) else { continue }; let centroid = tri.iter().map(|v| self.uvs.get(*v).copied().unwrap_or_default()).sum::() / 3.0; verts.extend(tri.map(|v| (v, centroid))); } + verts.sort_by_key(|(v, _)| *v); verts.dedup_by_key(|(v, _)| *v); for (v, centroid) in verts { @@ -266,7 +283,7 @@ impl Mesh { self.tangents.extend_from_slice(&other.tangents); self.uvs.extend_from_slice(&other.uvs); if !(self.colors.is_empty() && other.colors.is_empty()) { - // Vertices of the side without colors become white. + // Vertices of the side without colors become white self.colors.resize(base as usize, Vec4::ONE); self.colors.extend(other.colors.iter().copied().chain(std::iter::repeat(Vec4::ONE)).take(other.positions.len())); } @@ -304,21 +321,26 @@ impl Mesh { if has_c { m.colors.push((self.colors[i] + self.colors[j]) * 0.5); } + (m.positions.len() - 1) as u32 }) }; - for t in self.indices.chunks_exact(3) { + + for t in self.indices.as_chunks::<3>().0 { if t.iter().any(|&i| i as usize >= self.positions.len()) { continue; } + let (a, b, c) = (t[0], t[1], t[2]); let (ab, bc, ca) = (mid(&mut m, a, b), mid(&mut m, b, c), mid(&mut m, c, a)); m.indices.extend_from_slice(&[a, ab, ca, ab, b, bc, ca, bc, c, ab, bc, ca]); } + for s in &mut m.submeshes { s.start *= 4; s.count *= 4; } + m } @@ -368,21 +390,25 @@ impl mikktspace::Geometry for Mikk<'_> { fn num_faces(&self) -> usize { self.mesh.indices.len() / 3 } + fn num_vertices_of_face(&self, _face: usize) -> usize { 3 } + fn position(&self, face: usize, vert: usize) -> [f32; 3] { self.mesh.positions.get(self.vi(face, vert)).copied().unwrap_or_default().to_array() } + fn normal(&self, face: usize, vert: usize) -> [f32; 3] { self.mesh.normals.get(self.vi(face, vert)).copied().unwrap_or(Vec3::Y).to_array() } + fn tex_coord(&self, face: usize, vert: usize) -> [f32; 2] { - // MikkTSpace expects OpenGL texture coordinates (v up); our UVs are glTF (v down). With - // v flipped, the bitangent `cross(N, T) * w` points up the image, as glTF requires. + // Flip glTF V coordinates for MikkTSpace so the bitangent points up let uv = self.mesh.uvs.get(self.vi(face, vert)).copied().unwrap_or_default(); [uv.x, 1.0 - uv.y] } + fn set_tangent_encoded(&mut self, tangent: [f32; 4], face: usize, vert: usize) { let i = self.vi(face, vert); if let Some(t) = self.mesh.tangents.get_mut(i) { @@ -422,10 +448,12 @@ mod tests { #[test] fn subdivision_shares_edge_midpoints() { let m = quad().subdivided(); - // 4 corners + 5 edge midpoints (the diagonal is shared), 8 triangles. + + // 4 corners + 5 edge midpoints (the diagonal is shared), 8 triangles assert_eq!((m.positions.len(), m.triangle_count()), (9, 8)); assert_eq!(m.uvs.len(), 9); assert_eq!((m.submeshes[0].start, m.submeshes[0].count), (0, 24)); + m.validate().unwrap(); } @@ -444,40 +472,54 @@ mod tests { let mut a = quad(); let b = quad(); a.append(&b); + assert_eq!(a.indices[6], 4); assert_eq!(a.submeshes[1].material, 1); assert_eq!(a.submeshes[1].start, 6); + a.validate().unwrap(); - // Colors on one side only: the other side's vertices become white. + + // Colors on one side only: the other side's vertices become white let mut red = quad(); red.colors = vec![Vec4::new(1.0, 0.0, 0.0, 1.0); 4]; a.append(&red); + assert_eq!(a.colors.len(), 12); assert_eq!((a.colors[0], a.colors[8]), (Vec4::ONE, Vec4::new(1.0, 0.0, 0.0, 1.0))); + a.append(&quad()); + assert_eq!(a.colors[15], Vec4::ONE); + a.validate().unwrap(); a.colors.pop(); + assert!(a.validate().is_err()); } #[test] fn material_uvs_remap_and_split_shared_vertices() { - // quad(): two triangles sharing an edge; put the second in its own material. + // quad(): two triangles sharing an edge; put the second in its own material let mut m = quad(); m.materials = vec!["a".into(), "b".into()]; m.submeshes = vec![SubMesh { name: "a".into(), material: 0, start: 0, count: 3 }, SubMesh { name: "b".into(), material: 1, start: 3, count: 3 }]; let before = m.clone(); m.map_material_uvs(1, |uv| uv + Vec2::X); m.validate().unwrap(); + assert_eq!(m.vertex_count(), 6, "the two shared vertices are split"); assert_eq!(&m.indices[..3], &before.indices[..3]); + for k in 0..3 { let (old, new) = (before.indices[k] as usize, m.indices[k] as usize); + assert_eq!(m.uvs[new], before.uvs[old], "material a keeps its UVs"); + let (old, new) = (before.indices[3 + k] as usize, m.indices[3 + k] as usize); + assert_eq!((m.uvs[new], m.positions[new]), (before.uvs[old] + Vec2::X, before.positions[old])); } + assert_eq!(m.uv_tiles(1).into_iter().collect::>(), vec![(1, 0)]); assert_eq!(m.uv_tiles(0).into_iter().collect::>(), vec![(0, 0)]); } diff --git a/crates/image/src/lib.rs b/crates/image/src/lib.rs index 5e54bad..3314854 100644 --- a/crates/image/src/lib.rs +++ b/crates/image/src/lib.rs @@ -76,6 +76,7 @@ impl Image { Kind::Gray => vec![v[0]; n], Kind::Color => v.iter().copied().cycle().take(n * 4).collect(), }; + Image { w, h, kind, integer_sampling: false, data: Arc::new(data) } } @@ -163,15 +164,17 @@ impl Image { if !fx.is_finite() || !fy.is_finite() { return [0.0, 0.0, 0.0, 1.0]; } - // Huge but finite coordinates would saturate the i64 cast and overflow at `+ 1`. + + // Huge but finite coordinates would saturate the i64 cast and overflow at `+ 1` let (fx, fy) = (fx.clamp(-1.0e9, 1.0e9), fy.clamp(-1.0e9, 1.0e9)); let (x0, y0) = (fx.floor(), fy.floor()); let (mut tx, mut ty) = (fx - x0, fy - y0); if fixed_weights { - // Function texture reads use eight fractional bits for filter weights. + // Function texture reads use eight fractional bits for filter weights tx = (tx * 256.0).round() / 256.0; ty = (ty * 256.0).round() / 256.0; } + let (x0, y0) = (x0 as i64, y0 as i64); let a = self.get_axes(x0, y0, tiling); let b = self.get_axes(x0 + 1, y0, tiling); @@ -183,6 +186,7 @@ impl Image { let bot = c[k] + (d[k] - c[k]) * tx; o[k] = top + (bot - top) * ty; } + o } @@ -200,7 +204,7 @@ impl Image { pub fn sample_stored(&self, u: f32, v: f32, tile: bool) -> [f32; 4] { let pixel = self.sample_filtered(u, v, [tile; 2], true); if self.integer_sampling { - // Eight-bit sources also retain sixteen bits after interpolation. + // Eight-bit sources also retain sixteen bits after interpolation pixel.map(|value| (value.clamp(0.0, 1.0) * 65535.0).round() / 65535.0) } else { pixel @@ -221,6 +225,7 @@ impl Image { if !x.is_finite() || !y.is_finite() { return [0.0, 0.0, 0.0, 1.0]; } + self.get_axes(x as i64, y as i64, tiling) } @@ -258,6 +263,7 @@ impl Image { if w == self.w && h == self.h { return self.clone(); } + if w <= self.w && h <= self.h && self.w.is_multiple_of(w) && self.h.is_multiple_of(h) { let (fx, fy) = ((self.w / w) as i64, (self.h / h) as i64); let inv = 1.0 / (fx * fy) as f32; @@ -271,9 +277,11 @@ impl Image { } } } + s.map(|v| v * inv) }); } + Image::generate_uv(w, h, self.kind, |u, v| self.sample(u, v, tile)) } @@ -310,10 +318,12 @@ impl Image { s[k] += *v as f64; } } + if c == 1 { let m = (s[0] / n) as f32; return [m, m, m, 1.0]; } + s.map(|v| (v / n) as f32) } @@ -398,9 +408,11 @@ mod tests { #[test] fn sample_wraps_and_clamps() { let img = Image::gray(4, 1, |x, _| x as f32); + assert_eq!(img.get(-1, 0, true)[0], 3.0); assert_eq!(img.get(-1, 0, false)[0], 0.0); - // Halfway between the last and the (wrapped) first pixel. + + // Halfway between the last and the (wrapped) first pixel assert!((img.sample(1.0, 0.5, true)[0] - 1.5).abs() < 1e-5); } diff --git a/crates/nodes/src/atomic/gradient_map.rs b/crates/nodes/src/atomic/gradient_map.rs index 4ab0a55..0ae8e67 100644 --- a/crates/nodes/src/atomic/gradient_map.rs +++ b/crates/nodes/src/atomic/gradient_map.rs @@ -16,29 +16,31 @@ pub(super) fn definition() -> NodeDefinition { fn gradient_map(context: &CookContext) -> CookResult { let input = context.gray_or(0, 0.0); - let g = context.gradient("gradient"); + let gradient = context.gradient("gradient"); if matches!(context.format, OutputFormat::Inherit | OutputFormat::U16) { - return Ok(vec![integer_map(&input, g, !context.b("color_mode"))]); + return Ok(vec![integer_map(&input, gradient, !context.b("color_mode"))]); } - let lut: Vec<[f32; 4]> = (0..=512).map(|i| g.eval(i as f32 / 512.0)).collect(); + + const LOOKUP_INTERVALS: usize = 512; + let lookup_table: Vec<[f32; 4]> = (0..=LOOKUP_INTERVALS).map(|index| gradient.eval(index as f32 / LOOKUP_INTERVALS as f32)).collect(); let gray = !context.b("color_mode"); - let look = |v: f32| lut.get((v.clamp(0.0, 1.0) * 512.0).round() as usize).copied().unwrap_or([0.0, 0.0, 0.0, 1.0]); + let look = |v: f32| lookup_table.get((v.clamp(0.0, 1.0) * LOOKUP_INTERVALS as f32).round() as usize).copied().unwrap_or([0.0, 0.0, 0.0, 1.0]); Ok(vec![if gray { input.map(Kind::Gray, |p| [luminance(look(p[0])), 0.0, 0.0, 0.0]) } else { input.map(Kind::Color, |p| look(p[0])) }]) } fn integer_map(input: &materialcraft_image::Image, mut gradient: Gradient, gray: bool) -> materialcraft_image::Image { - // The 16-bit path evaluates a 256-entry table in integer position space. - // Keeping normalized positions or sampling a coarse table by nearest value - // shifts narrow transitions and changes the subsequent noise octaves. + // The 16-bit path evaluates a 256-entry table in integer position space + // Coarse nearest sampling shifts transitions and downstream noise octaves for key in &mut gradient.keys { key.pos = (key.pos.clamp(0.0, 1.0) * 65535.0).round(); } + let table: Vec<[u32; 4]> = (0..256).map(|index| gradient.eval(index as f32 * 257.0).map(|value| (value.clamp(0.0, 1.0) * 65535.0).round() as u32)).collect(); let lookup = |value: f32| { let value = if value.is_finite() { value.clamp(0.0, 1.0) } else { 0.0 }; - // The half-texel conversion precedes eight-bit interpolation weights; - // its float rounding is visible at the middle of a 16-bit ramp. + + // Half-texel conversion before eight-bit weights preserves ramp rounding let position = (value * 255.0 + 0.5) - 0.5; let fixed = (position * 256.0).round() as usize; let index = fixed / 256; @@ -47,5 +49,6 @@ fn integer_map(input: &materialcraft_image::Image, mut gradient: Gradient, gray: let right = table.get(index.saturating_add(1)).copied().unwrap_or(left); std::array::from_fn(|channel| ((left[channel] * (256 - weight) + right[channel] * weight + 128) >> 8) as f32 / 65535.0) }; + if gray { input.map(Kind::Gray, |pixel| [luminance(lookup(pixel[0])), 0.0, 0.0, 0.0]) } else { input.map(Kind::Color, |pixel| lookup(pixel[0])) } } diff --git a/crates/nodes/src/atomic/grayscale_conversion.rs b/crates/nodes/src/atomic/grayscale_conversion.rs index 540f1ce..4c37f36 100644 --- a/crates/nodes/src/atomic/grayscale_conversion.rs +++ b/crates/nodes/src/atomic/grayscale_conversion.rs @@ -18,16 +18,16 @@ pub(super) fn definition() -> NodeDefinition { } fn grayscale_conversion(context: &CookContext) -> CookResult { - let img = context.any_or(0, Kind::Color, [0.0, 0.0, 0.0, 1.0]).to_color(); - let w = [context.f("red"), context.f("green"), context.f("blue"), context.f("alpha")]; + let image = context.any_or(0, Kind::Color, [0.0, 0.0, 0.0, 1.0]).to_color(); + let weights = [context.f("red"), context.f("green"), context.f("blue"), context.f("alpha")]; let flatten = context.b("flatten_alpha"); let matte = context.f("matte"); - Ok(vec![img.map(Kind::Gray, |p| { - // Preserve the fused, ordered dot product through float image storage. - let mut v = p[3].mul_add(w[3], p[2].mul_add(w[2], p[1].mul_add(w[1], p[0] * w[0]))); + Ok(vec![image.map(Kind::Gray, |pixel| { + // Preserve the fused, ordered dot product through float image storage + let mut value = pixel[3].mul_add(weights[3], pixel[2].mul_add(weights[2], pixel[1].mul_add(weights[1], pixel[0] * weights[0]))); if flatten { - v = v * p[3] + matte * (1.0 - p[3]); + value = value * pixel[3] + matte * (1.0 - pixel[3]); } - [v, 0.0, 0.0, 0.0] + [value, 0.0, 0.0, 0.0] })]) } diff --git a/crates/nodes/src/atomic/hsl.rs b/crates/nodes/src/atomic/hsl.rs index 3aa341b..f5ff683 100644 --- a/crates/nodes/src/atomic/hsl.rs +++ b/crates/nodes/src/atomic/hsl.rs @@ -16,14 +16,15 @@ pub(super) fn definition() -> NodeDefinition { } fn hsl(context: &CookContext) -> CookResult { - let img = context.any_or(0, Kind::Color, [0.5, 0.5, 0.5, 1.0]).to_color(); - // The normalized slider spans one full turn in either direction from its midpoint. - let (dh, sat, lig) = ((context.f("hue") - 0.5) * 2.0, context.f("saturation"), context.f("lightness")); - Ok(vec![img.map(Kind::Color, |p| { - let [h, s, l] = ops::rgb_to_hsl(p); - let s = if sat <= 0.5 { s * sat * 2.0 } else { s + (1.0 - s) * (sat - 0.5) * 2.0 }; - let l = if lig <= 0.5 { l * lig * 2.0 } else { l + (1.0 - l) * (lig - 0.5) * 2.0 }; - let c = ops::hsl_to_rgb(h + dh, s.clamp(0.0, 1.0), l.clamp(0.0, 1.0)); - [c[0], c[1], c[2], p[3]] + let image = context.any_or(0, Kind::Color, [0.5, 0.5, 0.5, 1.0]).to_color(); + + // The normalized slider spans one full turn in either direction from its midpoint + let (hue_shift, saturation, lightness) = ((context.f("hue") - 0.5) * 2.0, context.f("saturation"), context.f("lightness")); + Ok(vec![image.map(Kind::Color, |pixel| { + let [h, s, l] = ops::rgb_to_hsl(pixel); + let s = if saturation <= 0.5 { s * saturation * 2.0 } else { s + (1.0 - s) * (saturation - 0.5) * 2.0 }; + let l = if lightness <= 0.5 { l * lightness * 2.0 } else { l + (1.0 - l) * (lightness - 0.5) * 2.0 }; + let color = ops::hsl_to_rgb(h + hue_shift, s.clamp(0.0, 1.0), l.clamp(0.0, 1.0)); + [color[0], color[1], color[2], pixel[3]] })]) } diff --git a/crates/nodes/src/atomic/levels.rs b/crates/nodes/src/atomic/levels.rs index 70d51fc..ecc27ec 100644 --- a/crates/nodes/src/atomic/levels.rs +++ b/crates/nodes/src/atomic/levels.rs @@ -32,11 +32,13 @@ fn levels(context: &CookContext) -> CookResult { pub fn levels_fn(v: f32, in_low: f32, in_mid: f32, in_high: f32, out_low: f32, out_high: f32) -> f32 { let span = in_high - in_low; let t = if span.abs() < 1e-6 { if v >= in_high { 1.0 } else { 0.0 } } else { ((v - in_low) / span).clamp(0.0, 1.0) }; + // NaN (from an input or a hostile parameter) would otherwise spread through the graph let t = if t.is_finite() { t } else { 0.0 }; let mid = if in_mid.is_finite() { in_mid.clamp(0.0, 1.0) } else { 0.5 }; - // The midpoint slider controls reciprocal gamma below neutral and gamma above it. - // Treating it as an input value that maps to 0.5 changes authored levels curves. + + // The midpoint slider controls reciprocal gamma below neutral and gamma above it + // Treating it as an input value that maps to 0.5 changes authored levels curves let g = if mid <= 0.5 { 1.0 / (9.0 - 16.0 * mid) } else { 16.0 * mid - 7.0 }; out_low + t.powf(g) * (out_high - out_low) } @@ -48,20 +50,25 @@ mod levels_tests { #[test] fn levels_remaps_the_input_range_midtones_and_output_range() { let close = |a: f32, b: f32| (a - b).abs() < 1e-5; + // Identity by default for v in [0.0, 0.25, 0.5, 1.0] { assert!(close(levels_fn(v, 0.0, 0.5, 1.0, 0.0, 1.0), v)); } + // The input range stretches and clips assert!(close(levels_fn(0.4, 0.2, 0.5, 0.6, 0.0, 1.0), 0.5)); assert!(close(levels_fn(0.1, 0.2, 0.5, 0.6, 0.0, 1.0), 0.0)); assert!(close(levels_fn(0.9, 0.2, 0.5, 0.6, 0.0, 1.0), 1.0)); - // The same gamma curve applies after normalizing the input range. + + // The same gamma curve applies after normalizing the input range assert!(close(levels_fn(0.25, 0.0, 0.25, 1.0, 0.0, 1.0), 0.7578583)); assert!(close(levels_fn(0.3, 0.2, 0.25, 0.6, 0.0, 1.0), 0.7578583)); + // The output range, inverted when low > high assert!(close(levels_fn(0.5, 0.0, 0.5, 1.0, 0.2, 0.6), 0.4)); assert!(close(levels_fn(0.0, 0.0, 0.5, 1.0, 1.0, 0.0), 1.0)); + // A collapsed input range is a threshold; NaN never escapes assert_eq!(levels_fn(0.49, 0.5, 0.5, 0.5, 0.0, 1.0), 0.0); assert_eq!(levels_fn(0.5, 0.5, 0.5, 0.5, 0.0, 1.0), 1.0); diff --git a/crates/nodes/src/atomic/normal.rs b/crates/nodes/src/atomic/normal.rs index a2779a2..99b4d83 100644 --- a/crates/nodes/src/atomic/normal.rs +++ b/crates/nodes/src/atomic/normal.rs @@ -15,21 +15,21 @@ pub(super) fn definition() -> NodeDefinition { } fn normal(context: &CookContext) -> CookResult { - let h = context.gray_or(0, 0.0); - let k = context.f("intensity") * context.w as f32 / 256.0; - Ok(vec![height_to_normal(&h, k, !context.b("opengl"), context.tile, context.b("height_alpha"))]) + let height_map = context.gray_or(0, 0.0); + let intensity = context.f("intensity") * context.w as f32 / 256.0; + Ok(vec![height_to_normal(&height_map, intensity, !context.b("opengl"), context.tile, context.b("height_alpha"))]) } -pub fn height_to_normal(h: &Image, k: f32, directx: bool, tile: bool, alpha_height: bool) -> Image { - Image::generate(h.w, h.h, Kind::Color, |x, y| { - // Backward differences place a wrapped discontinuity on its owning pixel. - let height = h.get_gray(x as i64, y as i64, tile); - let dx = height - h.get_gray(x as i64 - 1, y as i64, tile); - let dy = height - h.get_gray(x as i64, y as i64 - 1, tile); - let (nx, ny, nz) = (-dx * k, dy * k, 1.0); - let l = (nx * nx + ny * ny + nz * nz).sqrt(); +pub fn height_to_normal(height_map: &Image, intensity: f32, directx: bool, tile: bool, alpha_height: bool) -> Image { + Image::generate(height_map.w, height_map.h, Kind::Color, |x, y| { + // Backward differences place alpha wrapped discontinuity on its owning pixel + let height = height_map.get_gray(x as i64, y as i64, tile); + let dx = height - height_map.get_gray(x as i64 - 1, y as i64, tile); + let dy = height - height_map.get_gray(x as i64, y as i64 - 1, tile); + let (nx, ny, nz) = (-dx * intensity, dy * intensity, 1.0); + let length = (nx * nx + ny * ny + nz * nz).sqrt(); let ny = if directx { -ny } else { ny }; - let a = if alpha_height { height } else { 1.0 }; - [nx / l * 0.5 + 0.5, ny / l * 0.5 + 0.5, nz / l * 0.5 + 0.5, a] + let alpha = if alpha_height { height } else { 1.0 }; + [nx / length * 0.5 + 0.5, ny / length * 0.5 + 0.5, nz / length * 0.5 + 0.5, alpha] }) } diff --git a/crates/nodes/src/atomic/output.rs b/crates/nodes/src/atomic/output.rs index 9d38816..bfcf847 100644 --- a/crates/nodes/src/atomic/output.rs +++ b/crates/nodes/src/atomic/output.rs @@ -13,8 +13,7 @@ pub(super) fn definition() -> NodeDefinition { } fn output(context: &CookContext) -> CookResult { - // Unconnected outputs cook as transparent RGBA, even when their saved - // graph interface declares grayscale. Preserve that declaration separately. + // Keep saved grayscale declarations even when an unconnected output cooks as RGBA Ok(vec![context.sized(0).unwrap_or_else(|| Image::filled(context.w, context.h, Kind::Color, [0.0; 4]))]) } diff --git a/crates/nodes/src/atomic/text.rs b/crates/nodes/src/atomic/text.rs index 1938a6a..10c2fcc 100644 --- a/crates/nodes/src/atomic/text.rs +++ b/crates/nodes/src/atomic/text.rs @@ -104,7 +104,7 @@ fn glyph(c: char) -> &'static str { fn strokes(s: &str) -> impl Iterator> + '_ { s.split(' ') .filter(|t| !t.is_empty()) - .map(|t| t.as_bytes().chunks_exact(2).map(|p| [p[0].saturating_sub(b'0') as f32, p[1].saturating_sub(b'0') as f32]).collect()) + .map(|t| t.as_bytes().as_chunks::<2>().0.iter().map(|p| [p[0].saturating_sub(b'0') as f32, p[1].saturating_sub(b'0') as f32]).collect()) } /// Longest text drawn (an allocation cap) @@ -136,6 +136,7 @@ fn layout(context: &CookContext, text: &str) -> Vec { let matrix = context.v4("matrix"); let anchor = [(0.5 + position[0]) * context.w as f32, (0.5 + position[1]) * context.h as f32]; let lines: Vec<&str> = text.split('\n').collect(); + // The block is centered vertically on the anchor (cap height of the first line to baseline of the last) let top = -((lines.len().max(1) - 1) as f32 * line_height + 6.0) * 0.5; let mut segments = Vec::new(); @@ -166,6 +167,7 @@ fn layout(context: &CookContext, text: &str) -> Vec { } } } + segments } @@ -217,9 +219,12 @@ mod tests { resource: &no_res, vars: &vars, }; + let img = &text(&context).unwrap()[0]; let lit = img.data.iter().filter(|value| **value > 0.5).count(); + assert!(lit > 300, "{lit} pixels"); + // Centered: nothing in the top and bottom quarters assert!((0..256).all(|x| img.get(x, 20, false)[0] == 0.0 && img.get(x, 236, false)[0] == 0.0)); } diff --git a/crates/nodes/src/function/arithmetic.rs b/crates/nodes/src/function/arithmetic.rs index be5f509..5b0fff3 100644 --- a/crates/nodes/src/function/arithmetic.rs +++ b/crates/nodes/src/function/arithmetic.rs @@ -38,6 +38,7 @@ pub(crate) fn evaluate(op: &str, args: &[Value], random: &mut u32, scope: super: if args.get(1).is_some_and(|value| dimensions(value) != count && !(op == "mulscalar" && dimensions(value) == 1)) { return Err("float operands must have the same dimensions".into()); } + if args.get(2).is_some_and(|value| dimensions(value) != count && dimensions(value) != 1) { return Err("interpolation factor must be a scalar or match its operands".into()); } @@ -45,16 +46,20 @@ pub(crate) fn evaluate(op: &str, args: &[Value], random: &mut u32, scope: super: if op == "dot" { return float_value(&[left.iter().zip(right).take(count).map(|(a, b)| a * b).sum()]); } + if op == "atan2" { if count != 2 { return Err("arc tangent requires a float2".into()); } + return float_value(&[left[1].atan2(left[0])]); } + if op == "cartesian" { if count != 1 { return Err("Cartesian requires scalar radius and angle".into()); } + return float_value(&[left[0] * right[0].cos(), left[0] * right[0].sin()]); } @@ -65,7 +70,7 @@ pub(crate) fn evaluate(op: &str, args: &[Value], random: &mut u32, scope: super: "sub" => left - right, "mul" | "mulscalar" => left * right, "div" => left / right, - // Floored modulo wraps negative coordinates; remainder keeps their sign. + // Floored modulo wraps negative coordinates; remainder keeps their sign "mod" => left - right * (left / right).floor(), "neg" => -left, "abs" => left.abs(), diff --git a/crates/nodes/src/function/compile.rs b/crates/nodes/src/function/compile.rs index 1759973..f8040f4 100644 --- a/crates/nodes/src/function/compile.rs +++ b/crates/nodes/src/function/compile.rs @@ -11,9 +11,11 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu if graph.nodes.len() > 4096 { return Err("function exceeds the 4096-node graph limit".into()); } + if depth > super::MAX_CALL_DEPTH || graph.nodes.len() > *remaining { return Err(format!("function exceeds the {}-level or {}-node expanded execution limit", super::MAX_CALL_DEPTH, super::MAX_EXPANDED_NODES)); } + *remaining -= graph.nodes.len(); let output = super::output_node(graph).ok_or("function has no result node")?.id; let mut nodes = BTreeMap::new(); @@ -36,12 +38,14 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu if node.missing.is_some() { return Err(format!("Missing function operation `{}`", node.op)); } + let definition = if node.instance.is_some() { &super::function_call::DEFINITION } else { catalog::definition(&node.op).ok_or_else(|| format!("unknown function `{}`", node.op))? }; let names: Vec<_> = super::input_identifiers(node).collect(); + // An operation computes with zero on an input nothing is connected to; a call needs a source default instead let missing_input = node .instance @@ -70,7 +74,7 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu active.remove(&id); let connected: Vec = - sources.iter().map(|input| indices.get(input).copied().ok_or("unresolved function input".to_string())).collect::>()?; + sources.iter().map(|input| indices.get(input).copied().ok_or_else(|| "unresolved function input".to_string())).collect::>()?; let inputs = if node.instance.is_some() || ports.iter().all(|(_, input)| input.is_some()) { connected } else { @@ -97,8 +101,10 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu }; inputs.push(index); } + inputs }; + let call = if let Some(instance) = &node.instance { let source = node.source.as_ref().ok_or_else(|| format!("unresolved Function Graph `{}`", instance.graph))?.as_ref().map_err(Clone::clone)?; let program = compile_nested(source, seed, depth + 1, remaining)?; @@ -121,6 +127,7 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu } else { None }; + let value = node.value.clone().or_else(|| catalog::default_value(&node.op)); let input_type = |index: usize| inputs.get(index).and_then(|index| instructions.get(*index)).map_or(256, |instruction| instruction.type_code); let inferred = super::inferred_type::inferred_type(&node.op, value.as_ref(), input_type); diff --git a/crates/nodes/src/function/mod.rs b/crates/nodes/src/function/mod.rs index 9ae250f..bf58c57 100644 --- a/crates/nodes/src/function/mod.rs +++ b/crates/nodes/src/function/mod.rs @@ -1,7 +1,7 @@ //! Native function primitives, compilation and bounded evaluation -// Individual graphs stay capped at 4096 nodes. The volume-light graph expands to -// 72,973 nodes through shared calls; retain a global ceiling to bound hostile inputs. +// Individual graphs stay capped at 4096 nodes +// Shared calls can expand to 72,973 nodes, so retain a global ceiling const MAX_EXPANDED_NODES: usize = 131_072; const MAX_CALL_DEPTH: usize = 16; diff --git a/crates/nodes/src/function/operation.rs b/crates/nodes/src/function/operation.rs index eafc20c..6f01b22 100644 --- a/crates/nodes/src/function/operation.rs +++ b/crates/nodes/src/function/operation.rs @@ -35,8 +35,7 @@ pub(crate) fn evaluate( let missing = || format!("undefined variable `{name}` in this context"); let value = match context.variable(name, variables)? { Some(value) => value, - // User counters can be read before their first Set. They start at - // typed zero; unavailable system variables must still report errors. + // User counters start at typed zero; unavailable system variables still error None if !name.starts_with('$') => materialcraft_core::ValueType::from_code(instruction.type_code) .filter(|kind| *kind != materialcraft_core::ValueType::String) .map(materialcraft_core::ValueType::default_value) @@ -44,8 +43,8 @@ pub(crate) fn evaluate( None => return Err(missing()), }; - // Older authored graphs read an exposed integer option as a Boolean. - // Keep system-variable type checks strict; user options use zero/nonzero. + // Older authored graphs read an exposed integer option as a Boolean + // Keep system-variable type checks strict; user options use zero/nonzero if op == "get_bool" && !name.starts_with('$') && matches!(value, Value::Int(_) | Value::Enum(_)) @@ -89,6 +88,7 @@ pub(crate) fn evaluate( if value_type::type_code(left) != value_type::type_code(right) { return Err("comparison inputs must have the same type".into()); } + Value::Bool((left == right) == (op == "eq")) } "gt" | "gteq" | "lr" | "lreq" => { @@ -96,14 +96,16 @@ pub(crate) fn evaluate( if dimensions(left) != 1 || value_type::type_code(left) != value_type::type_code(right) { return Err("comparison requires matching scalars".into()); } + let ordering = match (left, right) { (Value::Int(a), Value::Int(b)) => a.partial_cmp(b), (Value::Float(a), Value::Float(b)) => a.partial_cmp(b), (Value::Bool(a), Value::Bool(b)) => a.partial_cmp(b), _ => return Err("comparison requires numeric inputs".into()), }; - // Ordered IEEE comparisons are false for NaN, including <= and >=. - // Undefined intermediates can therefore select a finite fallback. + + // Ordered IEEE comparisons are false for NaN, including <= and >= + // Undefined intermediates can therefore select a finite fallback Value::Bool(ordering.is_some_and(|ordering| match op { "gt" => ordering.is_gt(), "gteq" => ordering.is_ge(), @@ -116,6 +118,7 @@ pub(crate) fn evaluate( if dimensions(first()?) != 2 { return Err("sample position must be float2".into()); } + let setting = instruction.value.as_ref().map(integers).transpose()?.unwrap_or([0, 0, 0, 0]); let index = usize::try_from(setting[0]).map_err(|_| "negative image index")?; let pixel = match context.images.get(index).and_then(Option::as_ref) { diff --git a/crates/nodes/src/function/processor.rs b/crates/nodes/src/function/processor.rs index 9b5ad95..f31654b 100644 --- a/crates/nodes/src/function/processor.rs +++ b/crates/nodes/src/function/processor.rs @@ -8,19 +8,22 @@ use std::sync::Arc; fn pixel(value: Value, gray: bool, format: materialcraft_core::OutputFormat) -> Result<[f32; 4], String> { let values = super::value_type::floats(&value)?; let integer = matches!(format, materialcraft_core::OutputFormat::U8 | materialcraft_core::OutputFormat::U16 | materialcraft_core::OutputFormat::Inherit); - // Integer texture storage converts NaN to black and saturates infinities. - // Rejecting those values before storage breaks zero-softness masks. + + // Integer texture storage converts NaN to black and saturates infinities + // Rejecting those values before storage breaks zero-softness masks if !integer && values.iter().take(super::value_type::dimensions(&value)).any(|value| !value.is_finite()) { return Err("pixel function produced a non-finite output".into()); } + Ok(if gray { let intensity = if super::value_type::dimensions(&value) == 4 { - // RGBA results on a grayscale processor use equal RGB weights. - // Preserve the fused accumulation order at floating-point outputs. + // RGBA results on a grayscale processor use equal RGB weights + // Preserve the fused accumulation order at floating-point outputs values[2].mul_add(1.0 / 3.0, values[0].mul_add(1.0 / 3.0, values[1] * (1.0 / 3.0))) } else { values[0] }; + [intensity, 0.0, 0.0, 0.0] } else { match super::value_type::dimensions(&value) { @@ -55,6 +58,7 @@ pub(crate) fn pixel_processor(context: &CookContext) -> CookResult { let result = pixel(program.evaluate_pixel(&evaluation, state)?, gray, context.format)?; target.copy_from_slice(result.get(..channels).ok_or("invalid output channels")?); } + Ok(()) })?; Ok(vec![Image { w: width, h: height, kind, integer_sampling: false, data: Arc::new(data) }]) diff --git a/crates/nodes/src/function/program.rs b/crates/nodes/src/function/program.rs index e39e5b4..2d8282d 100644 --- a/crates/nodes/src/function/program.rs +++ b/crates/nodes/src/function/program.rs @@ -40,18 +40,19 @@ impl Program { let result = self.run(context, state, &mut remaining); *random = state.random; let value = result?; - // Nested calls can return undefined intermediates to a caller that clamps - // them, but a completed parameter/value function must publish finite data. + + // Callers may clamp undefined intermediates; completed functions must be finite if value.as_vec4().is_some_and(|components| components.iter().any(|value| !value.is_finite())) { return Err("function produced a non-finite output".into()); } + Ok(value) } fn random_seed(&self, context: &EvaluationContext<'_>) -> u32 { let seed = context.variables.get("$randomseed").and_then(Value::as_i32).map_or(self.seed, |seed| seed as u32); if context.scope == super::FunctionScope::PixelProcessor { - // Each pixel owns a deterministic stream, independent of worker scheduling. + // Each pixel owns a deterministic stream, independent of worker scheduling let x = (context.position[0] * context.size[0]).floor() as u32; let y = (context.position[1] * context.size[1]).floor() as u32; crate::random::hash(seed ^ crate::random::hash(x.wrapping_add(0x9e37_79b9)) ^ crate::random::hash(y.wrapping_add(0x85eb_ca6b))) @@ -119,15 +120,17 @@ impl Program { for (name, value) in call.inputs.iter().zip(arguments) { storage.variables.insert(name.clone(), value.clone()); } - // A function call consumes its caller's stream instead of restarting it. + + // A function call consumes its caller's stream instead of restarting it storage.random = state.random; let result = call.program.run(context, storage, remaining_steps); state.random = storage.random; if call.exports && result.is_ok() { - // Set publishes into the caller's scope; arguments and defaults stay local. + // Set publishes into the caller's scope; arguments and defaults stay local state.variables.extend(storage.assignments.clone()); state.assignments.extend(storage.assignments.clone()); } + result } else { operation::evaluate(instruction, arguments, &mut state.variables, &mut state.random, context) @@ -194,13 +197,14 @@ impl Program { } fn clear_loop_results(&self, index: usize, state: &mut EvaluationState) -> Result<(), String> { - // A node's outgoing wires share its computed value, including reads, branches and side effects. - // Only the current loop's condition and body run again; unrelated cached values stay intact. + // Outgoing wires share a node value across reads, branches and effects + // Only the active loop condition and body run again for repeated in &self.instruction(index)?.repeated { if let Some(register) = state.registers.get_mut(*repeated) { *register = None; } } + Ok(()) } } diff --git a/crates/nodes/src/function/resolve_instances.rs b/crates/nodes/src/function/resolve_instances.rs index c433a40..22a75d4 100644 --- a/crates/nodes/src/function/resolve_instances.rs +++ b/crates/nodes/src/function/resolve_instances.rs @@ -29,6 +29,7 @@ fn resolve(function: &mut FunctionGraph, local: &Package, sources: &BTreeMap 4096 { return Err("Function Graph exceeds the 4096-node graph limit".into()); } + if stack.len() >= super::MAX_CALL_DEPTH || body.nodes.len() > *remaining { return Err(format!("Function instances exceed the {}-level or {}-node expanded limit", super::MAX_CALL_DEPTH, super::MAX_EXPANDED_NODES)); } diff --git a/crates/nodes/src/function/tests.rs b/crates/nodes/src/function/tests.rs index be1ed45..e2aaed6 100644 --- a/crates/nodes/src/function/tests.rs +++ b/crates/nodes/src/function/tests.rs @@ -116,6 +116,7 @@ fn integer_math_preserves_all_32_bits_and_rejects_overflow() { #[test] fn vectors_concatenate_swizzle_and_cast_without_broadcasting() { let vector = operation("vector4", &[Value::Float2([1.0, 2.0]), Value::Float2([3.0, 4.0])], None).unwrap(); + assert_eq!(vector, Value::Float4([1.0, 2.0, 3.0, 4.0])); assert_eq!(operation("swizzle3", &[vector], Some(Value::Int3([3, 0, 3]))).unwrap(), Value::Float3([4.0, 1.0, 4.0])); assert_eq!(operation("toint2", &[Value::Float2([-1.9, 2.9])], None).unwrap(), Value::Int2([-2, 2])); @@ -284,6 +285,7 @@ fn function_calls_consume_the_callers_random_stream() { caller.output = Some(caller.add("vector2", &calls, None)); let expected = evaluate(&random, &BTreeMap::new()).unwrap(); let Value::Float2(values) = expected else { panic!("expected two random values") }; + assert_ne!(values[0], values[1]); assert_eq!(evaluate(&caller, &BTreeMap::new()).unwrap(), expected); } @@ -297,6 +299,7 @@ fn shared_conditions_keep_the_value_computed_before_a_sequence_assignment() { let assign = graph.add("set", &[one], Some(Value::Text("counter".into()))); let after = graph.add("sequence", &[assign, condition], None); graph.output = Some(graph.add("sequence", &[condition, after], None)); + assert_eq!(evaluate(&graph, &BTreeMap::from([("counter".into(), Value::Int(0))])).unwrap(), Value::Bool(true)); } @@ -308,6 +311,7 @@ fn shared_assignment_nodes_run_once_per_function_evaluation() { let next = graph.add("add", &[current, one], None); let assign = graph.add("set", &[next], Some(Value::Text("counter".into()))); graph.output = Some(graph.add("ivector2", &[assign, assign], None)); + assert_eq!(evaluate(&graph, &BTreeMap::from([("counter".into(), Value::Int(0))])).unwrap(), Value::Int2([1, 1])); } @@ -330,13 +334,18 @@ fn pixel_random_samples_vary_spatially_and_repeat_for_the_same_seed() { tile: true, variables: &variables, }; + let value = program.evaluate_pixel(&context, state).unwrap(); + assert_eq!(value, program.evaluate(&context, &mut EvaluationState::default()).unwrap()); + value }) .collect::>() }; + let first = samples(&mut state); + assert!(first.windows(2).all(|pair| pair[0] != pair[1])); assert_eq!(first, samples(&mut state)); } @@ -347,8 +356,11 @@ fn exposed_choices_are_integers_inside_functions() { let choice = graph.add("get_integer1", &[], Some(Value::Text("choice".into()))); let one = graph.add_constant(Value::Int(1)); graph.output = Some(graph.add("lr", &[choice, one], None)); + assert_eq!(evaluate(&graph, &BTreeMap::from([("choice".into(), Value::Enum(0))])).unwrap(), Value::Bool(true)); + graph.output = Some(graph.add("add", &[choice, one], None)); + assert_eq!(evaluate(&graph, &BTreeMap::from([("choice".into(), Value::Enum(2))])).unwrap(), Value::Int(3)); assert!(evaluate(&graph, &BTreeMap::from([("choice".into(), Value::Enum(u32::MAX))])).is_err()); } @@ -374,6 +386,7 @@ fn nested_function_assignments_escape_but_arguments_stay_local() { let argument = caller.add("get_integer1", &[], Some(Value::Text("argument".into()))); let pair = caller.add("ivector2", &[exported, argument], None); caller.output = Some(caller.add("sequence", &[call, pair], None)); + assert_eq!(evaluate(&caller, &BTreeMap::from([("argument".into(), Value::Int(7))])).unwrap(), Value::Int2([42, 7])); } @@ -387,6 +400,7 @@ fn one_random_node_reuses_its_sample_across_vector_connections() { let Value::Float4(channels) = evaluate(&graph, &BTreeMap::new()).unwrap() else { panic!("expected color"); }; + assert!(channels.iter().all(|value| *value == channels[0]), "{channels:?}"); } @@ -400,6 +414,7 @@ fn loops_refresh_random_samples_but_keep_each_iterations_fanout_equal() { let Value::Float3(expected) = evaluate(&samples, &BTreeMap::new()).unwrap() else { panic!("expected samples"); }; + assert_ne!(expected[0], expected[2]); let mut graph = FunctionGraph::default(); @@ -418,6 +433,7 @@ fn loops_refresh_random_samples_but_keep_each_iterations_fanout_equal() { let pair = graph.add("vector2", &[random, random], None); let body = graph.add("sequence", &[advance, pair], None); graph.output = Some(graph.add("while", &[init, exit, body], Some(Value::Int(3)))); + assert_eq!(evaluate(&graph, &BTreeMap::new()).unwrap(), Value::Float2([expected[2]; 2])); } @@ -431,11 +447,13 @@ fn a_node_runs_once_so_readers_before_and_after_a_set_share_its_value() { graph.output = Some(graph.add("sequence", &[store, read], None)); let variables = BTreeMap::from([("count".to_string(), Value::Int(4))]); + assert_eq!(evaluate(&graph, &variables).unwrap(), Value::Int(4), "the Get node read 4 before the Set and keeps it"); // A second Get node reads the variable when it runs, after the Set let later = graph.add("get_integer1", &[], Some(Value::Text("count".into()))); graph.output = Some(graph.add("sequence", &[store, later], None)); + assert_eq!(evaluate(&graph, &variables).unwrap(), Value::Int(5)); } @@ -476,10 +494,12 @@ fn variables_a_called_graph_sets_reach_its_caller_but_its_inputs_do_not() { node.source = Some(Ok(std::sync::Arc::new(called))); let shared = caller.add("get_integer1", &[], Some(Value::Text("shared".into()))); caller.output = Some(caller.add("sequence", &[call, shared], None)); + assert_eq!(evaluate(&caller, &BTreeMap::new()).unwrap(), Value::Int(9)); let leaked = caller.add("get_integer1", &[], Some(Value::Text("amount".into()))); caller.output = Some(caller.add("sequence", &[call, leaked], None)); + assert_eq!(evaluate(&caller, &BTreeMap::new()).unwrap(), Value::Int(0)); } @@ -500,6 +520,7 @@ fn an_unconnected_input_computes_with_zero_and_a_loop_without_a_condition_runs_i let half = graph.add_constant(Value::Float(0.5)); graph.output = Some(graph.add("lerp", &[high, high, half], None)); graph.nodes.last_mut().unwrap().inputs = vec![None, Some(high), Some(half)]; + assert_eq!(evaluate(&graph, &BTreeMap::new()).unwrap(), Value::Float(4.0), "halfway between zero and 8"); let mut graph = FunctionGraph::default(); @@ -511,6 +532,7 @@ fn an_unconnected_input_computes_with_zero_and_a_loop_without_a_condition_runs_i let step = graph.add("set", &[next], Some(Value::Text("count".into()))); graph.output = Some(graph.add("while", &[start, start, step], Some(Value::Int(3)))); graph.nodes.last_mut().unwrap().inputs = vec![Some(start), None, Some(step)]; + assert_eq!(evaluate(&graph, &BTreeMap::new()).unwrap(), Value::Int(3)); } @@ -544,5 +566,6 @@ fn every_pixel_of_a_processor_draws_its_own_random_numbers() { let first = image.get(0, 0, false)[0]; let different = (0..8).flat_map(|y| (0..8).map(move |x| (x, y))).filter(|(x, y)| (image.get(*x, *y, false)[0] - first).abs() > 1e-4).count(); + assert!(different > 56, "{different} of 64 pixels differ from the first"); } diff --git a/crates/nodes/src/function/value_type.rs b/crates/nodes/src/function/value_type.rs index 00748ee..0875018 100644 --- a/crates/nodes/src/function/value_type.rs +++ b/crates/nodes/src/function/value_type.rs @@ -45,12 +45,13 @@ pub fn float_value(values: &[f32]) -> Result { if values.iter().any(|v| !v.is_finite()) { return Err("operation produced a non-finite number".into()); } + float_components(values) } pub(crate) fn intermediate_float_value(values: &[f32]) -> Result { - // All runtime function scopes can clamp an infinite/NaN intermediate. - // Public evaluation boundaries validate the final result instead. + // All runtime function scopes can clamp an infinite/NaN intermediate + // Public evaluation boundaries validate the final result instead float_components(values) } diff --git a/crates/nodes/src/function/vector.rs b/crates/nodes/src/function/vector.rs index 5b0adb7..f25f29f 100644 --- a/crates/nodes/src/function/vector.rs +++ b/crates/nodes/src/function/vector.rs @@ -7,6 +7,7 @@ pub(crate) fn evaluate(op: &str, args: &[Value], setting: Option<&Value>) -> Res if !(1..=4).contains(&count) { return Err("invalid vector size".into()); } + let integer = op.starts_with('i') || op.starts_with("toint"); let mut result_float = [0.0; 4]; let mut result_int = [0; 4]; @@ -18,8 +19,7 @@ pub(crate) fn evaluate(op: &str, args: &[Value], setting: Option<&Value>) -> Res let index = usize::try_from(*index).ok().filter(|index| *index < 4).ok_or("swizzle component is outside its input")?; if integer { if let Some(result) = result_int.get_mut(lane) { - // Swizzles read zero in undeclared lanes, including scalar - // inputs. Arithmetic's scalar broadcast does not apply here. + // Swizzles read zero in undeclared lanes without scalar broadcast *result = if index < dimensions(first) { *integers.get(index).ok_or("invalid swizzle")? } else { 0 }; } } else if let Some(result) = result_float.get_mut(lane) { @@ -49,14 +49,15 @@ pub(crate) fn evaluate(op: &str, args: &[Value], setting: Option<&Value>) -> Res if dimensions(first) != count { return Err("casts preserve the number of components".into()); } + if integer { let values = floats(first)?; for (result, value) in result_int.iter_mut().zip(values).take(count) { if !value.is_finite() || !(-2147483648.0..2147483648.0).contains(&value) { return Err("float is outside the integer range".into()); } - // Integer conversion rounds down; truncating toward zero shifts - // negative texture coordinates and lattice indices by one cell. + + // Round down because truncation shifts negative texture and lattice indices *result = value.floor() as i32; } } else { diff --git a/crates/nodes/src/graph_interface.rs b/crates/nodes/src/graph_interface.rs index f7ea89f..9e78a51 100644 --- a/crates/nodes/src/graph_interface.rs +++ b/crates/nodes/src/graph_interface.rs @@ -30,12 +30,14 @@ pub fn output_type(graph: &Graph, id: NodeId, port: &str) -> u32 { if let Ok(Some(kind)) = crate::output_port_value_type(graph, id, port) { return kind as u32; } + let mut pending = vec![(id, port.to_string())]; let mut visited = std::collections::BTreeSet::new(); while let Some((id, port)) = pending.pop() { if !visited.insert((id, port.clone())) { continue; } + let Ok(node) = graph.node(id) else { continue }; if node.def == "output" && graph.input_of(id, "input").is_none() @@ -43,24 +45,30 @@ pub fn output_type(graph: &Graph, id: NodeId, port: &str) -> u32 { { return if color { 1 } else { 2 }; } + if let Some(instance) = &node.instance { if instance.outputs.iter().any(|output| output.identifier == port && output.type_code == 1) { return 1; } + continue; } + let Some(definition) = find(&node.def) else { return 1 }; if let Some(parameter) = definition.param("color_mode") { if node.params.get("color_mode").unwrap_or(¶meter.default).as_bool() == Some(true) { return 1; } + continue; } + match definition.outputs.iter().find(|output| output.id == port).map(|output| output.kind) { Some(PortKind::Color) => return 1, Some(PortKind::Gray) => continue, _ => {} } + for source in definition.inputs.iter().filter(|input| input.kind != PortKind::Gray).filter_map(|input| graph.input_of(id, input.id)) { pending.push((source.from, source.from_port.clone())); if node.def != "blend" { @@ -68,5 +76,6 @@ pub fn output_type(graph: &Graph, id: NodeId, port: &str) -> u32 { } } } + 2 } diff --git a/crates/nodes/src/ops.rs b/crates/nodes/src/ops.rs index 751ecbe..96f2d8d 100644 --- a/crates/nodes/src/ops.rs +++ b/crates/nodes/src/ops.rs @@ -12,6 +12,7 @@ pub fn gaussian(img: &Image, sigma: f32, tile: bool) -> Image { if sigma.is_nan() || sigma <= 0.05 || !sigma.is_finite() { return img.clone(); } + let sigma = sigma.min(img.w.max(img.h) as f32); if sigma < 3.0 { let r = (sigma * 3.0).ceil() as i64; @@ -21,7 +22,8 @@ pub fn gaussian(img: &Image, sigma: f32, tile: bool) -> Image { let pass = |src: &Image, horizontal: bool| convolve_1d(src, &k, r, horizontal, tile); return pass(&pass(img, true), false); } - // Box widths for three passes (Kovesi, "Fast almost-Gaussian filtering"). + + // Box widths for three passes (Kovesi, "Fast almost-Gaussian filtering") let n = 3.0f32; let wl = ((12.0 * sigma * sigma / n + 1.0).sqrt()).floor() as i64; let wl = if wl % 2 == 0 { wl - 1 } else { wl }; @@ -32,6 +34,7 @@ pub fn gaussian(img: &Image, sigma: f32, tile: bool) -> Image { let r = (if i < m { wl } else { wu } - 1) / 2; out = box_1d(&box_1d(&out, r.max(0), true, tile), r.max(0), false, tile); } + out } @@ -55,6 +58,7 @@ fn box_1d(src: &Image, r: i64, horizontal: bool, tile: bool) -> Image { if r <= 0 { return src.clone(); } + let (w, h) = (src.w as usize, src.h as usize); let c = src.channels(); let inv = 1.0 / (2 * r + 1) as f32; @@ -67,6 +71,7 @@ fn box_1d(src: &Image, r: i64, horizontal: bool, tile: bool) -> Image { let (x, y) = if horizontal { (i, line) } else { (line, i) }; src.data.get((y * w + x) * c + ch).copied().unwrap_or(0.0) }; + let results: Vec> = (0..lines) .into_par_iter() .map(|line| { @@ -78,6 +83,7 @@ fn box_1d(src: &Image, r: i64, horizontal: bool, tile: bool) -> Image { sum += get(line, i as i64 + r + 1, ch) - get(line, i as i64 - r, ch); } } + out }) .collect(); @@ -88,6 +94,7 @@ fn box_1d(src: &Image, r: i64, horizontal: bool, tile: bool) -> Image { data[dst..dst + c].copy_from_slice(&vals[i * c..i * c + c]); } } + Image { w: src.w, h: src.h, kind: src.kind, integer_sampling: false, data: Arc::new(data) } } @@ -110,9 +117,11 @@ fn edt_1d(f: &[f32]) -> (Vec, Vec) { if n == 0 { return (Vec::new(), Vec::new()); } + let f: Vec = f.iter().map(|v| *v as f64).collect(); let mut d = vec![0.0f32; n]; - // Distance-only callers do not allocate or propagate site indices. + + // Distance-only callers do not allocate or propagate site indices let mut sites = if SITES { vec![usize::MAX; n] } else { Vec::new() }; let mut v = vec![0usize; n]; let mut z = vec![0.0f64; n + 1]; @@ -131,6 +140,7 @@ fn edt_1d(f: &[f32]) -> (Vec, Vec) { z[k] = s; z[k + 1] = DISTANCE_INF as f64; } + k = 0; for (q, dq) in d.iter_mut().enumerate() { while k + 1 < n && z[k + 1] < q as f64 { @@ -142,6 +152,7 @@ fn edt_1d(f: &[f32]) -> (Vec, Vec) { sites[q] = p; } } + (d, sites) } @@ -150,9 +161,11 @@ fn distance_transform(w: u32, h: u32, inside: impl Fn(usize) if w == 0 || h == 0 { return (Vec::new(), Vec::new()); } - // Tiling: run on a 3x tiled line so distances cross the edges. + + // Tiling: run on a 3x tiled line so distances cross the edges let rep = if tile { 3 } else { 1 }; - // Columns. Site indices are vertical coordinates at this stage. + + // Columns. Site indices are vertical coordinates at this stage let cols: Vec<_> = (0..w) .into_par_iter() .map(|x| { @@ -162,7 +175,8 @@ fn distance_transform(w: u32, h: u32, inside: impl Fn(usize) (d[off..off + h].to_vec(), if SITES { sites[off..off + h].to_vec() } else { Vec::new() }) }) .collect(); - // Rows. Carry the chosen column's vertical site through the horizontal transform. + + // Rows. Carry the chosen column's vertical site through the horizontal transform let rows: Vec<_> = (0..h) .into_par_iter() .map(|y| { @@ -195,6 +209,7 @@ fn distance_transform(w: u32, h: u32, inside: impl Fn(usize) distances.extend(row); sites.extend(row_sites); } + (distances, sites) } @@ -214,6 +229,7 @@ pub fn rgb_to_hsl(c: [f32; 4]) -> [f32; 3] { if (mx - mn).abs() < 1e-7 { return [0.0, 0.0, l]; } + let d = mx - mn; let s = if l > 0.5 { d / (2.0 - mx - mn) } else { d / (mx + mn) }; let h = if mx == r { @@ -223,6 +239,7 @@ pub fn rgb_to_hsl(c: [f32; 4]) -> [f32; 3] { } else { (r - g) / d + 4.0 }; + [h / 6.0, s, l] } @@ -230,6 +247,7 @@ pub fn hsl_to_rgb(h: f32, s: f32, l: f32) -> [f32; 3] { if s <= 0.0 { return [l, l, l]; } + let q = if l < 0.5 { l * (1.0 + s) } else { l + s - l * s }; let p = 2.0 * l - q; let f = |mut t: f32| { @@ -292,7 +310,7 @@ pub fn blend_channel(mode: i32, b: f32, f: f32) -> f32 { } 10 => 1.0 - (1.0 - b) * (1.0 - f), 11 => { - // W3C soft light. + // W3C soft light if f <= 0.5 { b - (1.0 - 2.0 * f) * b * (1.0 - b) } else { @@ -324,15 +342,14 @@ pub fn blend_channel(mode: i32, b: f32, f: f32) -> f32 { /// `blend_alpha`: composite coverage; false preserves background alpha. #[inline] pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blending: i32, blend_alpha: bool, clamp: bool) -> [f32; 4] { - // Switch interpolates the RGBA tuple. Weighting it by source alpha again - // composites an already blended result a second time. + // Switch already interpolates RGBA; another alpha weight composites twice let t = opacity * if mode != 7 { fg[3] } else { 1.0 }; let coverage = bg[3] * (1.0 - t) + t; let mut o = [0.0; 4]; for k in 0..3 { let v = blend_channel(mode, bg[k], fg[k]); - // Weight both terms before adding. Subtracting first changes rounding at - // integer output boundaries, which compounds through noise remapping. + + // Weight before adding to preserve integer-boundary rounding o[k] = match color_blending { 1 if mode != 7 => bg[k] * (1.0 - opacity) + v * opacity, 2 if mode != 7 && coverage > 0.0 => { @@ -347,6 +364,7 @@ pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blend o[k] = o[k].clamp(0.0, 1.0); } } + o[3] = if mode == 7 { bg[3] * (1.0 - opacity) + fg[3] * opacity } else if blend_alpha || color_blending == 2 || (color_blending == 3 && mode == 0) { @@ -354,6 +372,7 @@ pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blend } else { bg[3] }; + o } @@ -370,10 +389,12 @@ mod tests { fn color_dodge_and_burn_keep_black_and_white_backgrounds() { const DODGE: i32 = 13; const BURN: i32 = 14; + // (background, foreground, expected), W3C compositing formulas for (b, f, expected) in [(0.0, 1.0, 0.0), (0.0, 0.5, 0.0), (0.5, 1.0, 1.0), (0.25, 0.5, 0.5), (1.0, 0.75, 1.0)] { assert_eq!(blend_channel(DODGE, b, f), expected, "dodge b={b} f={f}"); } + for (b, f, expected) in [(1.0, 0.0, 1.0), (1.0, 0.5, 1.0), (0.5, 0.0, 0.0), (0.75, 0.5, 0.5), (0.0, 0.25, 0.0)] { assert_eq!(blend_channel(BURN, b, f), expected, "burn b={b} f={f}"); } @@ -400,8 +421,7 @@ mod tests { #[test] fn edt_sites_match_brute_force() { - // Exhaust all small masks, including ties and empty/full masks. Larger rectangles use - // deterministic pseudo-random masks; the oracle measures every site independently. + // Exhaust small masks; use deterministic masks and per-site checks for larger ones for (w, h) in [(1usize, 1usize), (1, 7), (7, 1), (2, 3), (3, 3), (5, 7), (8, 6)] { let cases = if w * h <= 9 { 1usize << (w * h) } else { 128 }; for case in 0..cases { @@ -418,7 +438,9 @@ mod tests { .collect(); for tile in [false, true] { let (d, sites) = distance_sq_with_sites(w as u32, h as u32, |i| mask[i], tile); + assert_eq!(d, distance_sq(w as u32, h as u32, |i| mask[i], tile)); + for i in 0..w * h { let distance = |site: usize| { let dx = (i % w).abs_diff(site % w); @@ -439,6 +461,7 @@ mod tests { } } } + assert!(distance_sq_with_sites(0, 3, |_| true, true).0.is_empty()); assert!(distance_sq_with_sites(3, 0, |_| true, false).1.is_empty()); } diff --git a/crates/nodes/src/parameter.rs b/crates/nodes/src/parameter.rs index cc70009..fd6f5fd 100644 --- a/crates/nodes/src/parameter.rs +++ b/crates/nodes/src/parameter.rs @@ -28,17 +28,19 @@ pub fn clamp_params(def: &NodeDefinition, params: &mut BTreeMap) }; continue; } + if let Value::Color(c) = v { for x in c { *x = clamp_finite(*x, -HARD_SPAN, 1.0 + HARD_SPAN, 0.0); } continue; } - // No range declared (an Input Value's default takes any number): only a value that isn't - // a number goes + + // An unconstrained Input Value accepts any number; reject only nonnumeric values let ranged = p.min < p.max; let span = p.max - p.min; let (lo, hi) = if ranged { (p.min - HARD_SPAN * span, p.max + HARD_SPAN * span) } else { (f32::MIN, f32::MAX) }; + // The default's component `i`, or 0 when the default is a different variant let fallback = |i: usize| match &p.default { Value::Float(d) => [*d].get(i).copied(), diff --git a/crates/nodes/src/pattern/evaluation.rs b/crates/nodes/src/pattern/evaluation.rs index a6aaefd..48116bf 100644 --- a/crates/nodes/src/pattern/evaluation.rs +++ b/crates/nodes/src/pattern/evaluation.rs @@ -5,8 +5,8 @@ use super::{catalog, pattern_stamp::PatternStamp, pattern_visit::PatternVisit, r use crate::function::{EvaluationContext, EvaluationState, compile}; use crate::{CookContext, CookResult}; -// A 512-by-512 quadtree needs 349,525 visits including its internal quadrants. -// Keep a hard ceiling for deeper or repeated trees; the rasterizer separately bounds pixel work. +// A 512-by-512 quadtree needs 349,525 visits including internal quadrants +// Cap deeper trees; the rasterizer separately bounds pixel work const MAX_VISITS: usize = 1_048_576; pub(crate) fn cook(context: &CookContext) -> CookResult { @@ -31,8 +31,8 @@ pub(crate) fn cook(context: &CookContext) -> CookResult { } let mut stamps = Vec::new(); - // Set variables belong to this FX-Map cook. In particular, iterations must see - // preceding iterations' assignments rather than resetting to the iterator's initial state. + + // Keep Set assignments across iterations within this FX-Map cook let mut variables = context.vars.clone(); let mut max_depth = 0; let mut state = EvaluationState::default(); @@ -149,6 +149,7 @@ pub(crate) fn cook(context: &CookContext) -> CookResult { if count > remaining || count > 10_000 { return Err("pattern iteration count exceeds the execution budget".into()); } + if let Some(Some(child)) = node.children.first() { let mut first = visit.clone(); first.node = *child; @@ -171,6 +172,7 @@ pub(crate) fn cook(context: &CookContext) -> CookResult { if children.len().saturating_add(pending.len()) > remaining { return Err("pattern graph exceeded its execution budget".into()); } + pending.extend(children.into_iter().rev()); } diff --git a/crates/nodes/src/pattern/raster.rs b/crates/nodes/src/pattern/raster.rs index d5e3de8..a6139a4 100644 --- a/crates/nodes/src/pattern/raster.rs +++ b/crates/nodes/src/pattern/raster.rs @@ -15,6 +15,7 @@ pub(super) fn render(context: &CookContext, stamps: &[PatternStamp], max_depth: if stamps.is_empty() { return Ok(image); } + let channels = kind.channels(); let opacity = context.f("opacity").max(0.0); let weights = depth_weights(context.f("roughness"), max_depth)?; @@ -28,12 +29,15 @@ pub(super) fn render(context: &CookContext, stamps: &[PatternStamp], max_depth: if !stamp.center.iter().chain(&stamp.size).chain(&stamp.color).chain([&stamp.rotation, &stamp.variation]).all(|value| value.is_finite()) { return Err("pattern parameters must be finite".into()); } + if stamp.size.iter().any(|size| size.abs() < 1e-8) { continue; } + if !matches!(stamp.blend, 0..=2) { return Err(format!("unsupported pattern blending mode {}", stamp.blend)); } + if !(2..=18).contains(&stamp.pattern) && !(256..256 + crate::MAX_IMAGE_INPUTS as i32).contains(&stamp.pattern) { return Err(format!("unsupported pattern {}", stamp.pattern)); } @@ -42,8 +46,8 @@ pub(super) fn render(context: &CookContext, stamps: &[PatternStamp], max_depth: if !opacity.is_finite() { return Err("pattern roughness overflowed its depth opacity".into()); } - // A scalar luminosity/opacity weights alpha as well as RGB. Its RGB - // already includes that weight, unlike a straight RGBA tint. + + // Scalar luminosity weights both RGB and alpha, unlike straight RGBA tint let source_alpha = if stamp.scalar_color { stamp.color[0] } else { stamp.color[3] }; let (sine, cosine) = (stamp.rotation * std::f32::consts::TAU).sin_cos(); @@ -67,8 +71,9 @@ pub(super) fn render(context: &CookContext, stamps: &[PatternStamp], max_depth: let point = render_region.point([x, y], [width, height]); let delta = [point[0] - center[0], point[1] - center[1]]; let local = [(delta[0] * cosine + delta[1] * sine) * 2.0 / stamp.size[0], (-delta[0] * sine + delta[1] * cosine) * 2.0 / stamp.size[1]]; - // The preceding quadrant owns a shared edge, including subpixel quadrants. - // Including both sides can add four adjacent stamps to the same pixel. + + // The preceding quadrant owns shared edges, including subpixel ones + // Counting both sides can stamp a pixel four extra times if local.iter().any(|coordinate| *coordinate <= -1.0 || *coordinate > 1.0) { continue; } @@ -99,7 +104,7 @@ pub(super) fn render(context: &CookContext, stamps: &[PatternStamp], max_depth: let tint = if channel == 3 { source_alpha } else { stamp.color[channel] }; let value = pattern[channel] * tint * alpha * opacity; *target = match stamp.blend { - // Grayscale legacy maximum blends have no alpha channel to composite. + // Grayscale legacy maximum blends have no alpha channel to composite 1 if kind == Kind::Gray => target.max(value), 1 => value + *target * (1.0 - (pattern[3] * source_alpha * opacity).clamp(0.0, 1.0)), 2 => target.max(value), @@ -114,21 +119,25 @@ pub(super) fn render(context: &CookContext, stamps: &[PatternStamp], max_depth: } } } + Ok(image) } fn depth_weights(roughness: f32, max_depth: u32) -> Result, String> { - // Traversal is bounded by the 4096-node acyclic pattern graph. Keep this allocation bounded too. + // Traversal is bounded by the 4096-node acyclic pattern graph. Keep this allocation bounded too if max_depth >= 4096 || !roughness.is_finite() || roughness < 0.0 { return Err("pattern roughness must be finite and nonnegative, with depth below 4096".into()); } + let mut weights = vec![1.0_f64; max_depth as usize + 1]; - // Zero is the native no-attenuation mode, not the limit of the normalized geometric weights. + + // Zero is the native no-attenuation mode, not the limit of the normalized geometric weights if roughness == 0.0 { return Ok(weights.into_iter().map(|value| value as f32).collect()); } - // Normalize r^depth over all visited quadrant depths, including non-drawing ancestors. - // Build from the largest weight so large roughness/depth cannot overflow the geometric series. + + // Normalize depth weights across drawing and non-drawing ancestors + // Start at the largest weight to avoid overflow let mut weight = 1.0; let ratio = f64::from(roughness); if roughness <= 1.0 { @@ -142,6 +151,7 @@ fn depth_weights(roughness: f32, max_depth: u32) -> Result, String> { weight /= ratio; } } + let sum: f64 = weights.iter().sum(); Ok(weights.into_iter().map(|value| (value / sum) as f32).collect()) } diff --git a/crates/nodes/src/pattern/region.rs b/crates/nodes/src/pattern/region.rs index 433fa50..981b098 100644 --- a/crates/nodes/src/pattern/region.rs +++ b/crates/nodes/src/pattern/region.rs @@ -11,6 +11,7 @@ impl Region { if value.iter().any(|value| !value.is_finite()) || span.iter().any(|value| !value.is_finite() || value.abs() < f32::EPSILON) { return Err("pattern regions must be finite with nonzero width and height".into()); } + Ok(Self { start, span }) } @@ -39,12 +40,15 @@ impl Region { let end = self.start[axis] + self.span[axis]; let first = ((self.start[axis].min(end) - center[axis] - radius[axis]) / period[axis]).ceil(); let last = ((self.start[axis].max(end) - center[axis] + radius[axis]) / period[axis]).floor(); - // A tiny period or huge pattern cannot create unbounded work, even outside the image. + + // A tiny period or huge pattern cannot create unbounded work, even outside the image if !first.is_finite() || !last.is_finite() || first < i32::MIN as f32 || last >= i32::MAX as f32 { return Err("pattern repetition exceeds its coordinate budget".into()); } + result[axis] = [first as i32, last as i32]; } + Ok(result) } } diff --git a/crates/nodes/src/pattern/shape.rs b/crates/nodes/src/pattern/shape.rs index f658e66..7776360 100644 --- a/crates/nodes/src/pattern/shape.rs +++ b/crates/nodes/src/pattern/shape.rs @@ -42,5 +42,6 @@ pub(super) fn coverage(pattern: i32, x: f32, y: f32, variation: f32) -> Result ((x + 1.0) * 0.5 + variation).fract(), _ => return Err(format!("unknown built-in pattern {pattern}")), }; + Ok(value) } diff --git a/crates/nodes/src/pattern/tests.rs b/crates/nodes/src/pattern/tests.rs index 7b8d844..e3b61a1 100644 --- a/crates/nodes/src/pattern/tests.rs +++ b/crates/nodes/src/pattern/tests.rs @@ -20,6 +20,7 @@ fn cook_with_variables(graph: PatternGraph, variables: &BTreeMap) resource: &|_| None, vars: variables, }; + (definition.cook)(&context) } @@ -85,6 +86,7 @@ fn iterate_preserves_assignments_from_previous_iterations() { graph.nodes[1].parameters.insert("patterntype".into(), Value::Int(2)); graph.nodes[1].parameters.insert("opacity".into(), Value::Function(Box::new(increment))); let image = cook(graph).unwrap().remove(0); + assert!(image.data.iter().all(|value| *value == 6.0), "expected 1 + 2 + 3, got {:?}", image.mean()); } @@ -160,6 +162,7 @@ fn roughness_sample(roughness: f32, depth: usize, blend: i32, color: bool, repea resource: &|_| None, vars: &BTreeMap::new(), }; + (definition.cook)(&context) } @@ -178,6 +181,7 @@ fn roughness_attenuates_quadrants_by_depth_and_keeps_background() { 2 => value.max(0.1), _ => value + 0.1, }; + assert!((pixel[channel] - expected).abs() < 1e-6, "roughness={roughness}, depth={depth}, blend={blend}, channel={channel}: {pixel:?}"); } if depth > 0 { @@ -191,6 +195,7 @@ fn roughness_attenuates_quadrants_by_depth_and_keeps_background() { #[test] fn repetition_switches_and_dots_do_not_add_roughness_depth() { let image = roughness_sample(1.0, 2, 0, false, true).unwrap().remove(0); + assert!((image.get(0, 0, false)[0] - (0.1 + 0.8 / 3.0)).abs() < 1e-6); assert!((image.get(15, 15, false)[0] - 0.1).abs() < 1e-6); } @@ -221,7 +226,9 @@ fn a_full_nine_level_quadtree_renders_one_pattern_per_pixel() { resource: &|_| None, vars: &BTreeMap::new(), }; + let images = (definition.cook)(&context).unwrap(); + assert!(images[0].data.iter().all(|value| (*value - 1.0).abs() < 1e-6)); } @@ -298,6 +305,7 @@ fn every_visit_draws_its_own_random_numbers() { let image = cook(graph).unwrap().remove(0); let quarters = [image.get(4, 4, false)[0], image.get(12, 4, false)[0], image.get(4, 12, false)[0], image.get(12, 12, false)[0]]; + assert!(quarters.windows(2).all(|pair| (pair[0] - pair[1]).abs() > 1e-4), "{quarters:?}"); } @@ -316,6 +324,7 @@ fn a_pixel_on_the_seam_of_two_patterns_is_drawn_once() { let definition = find("fx_map").unwrap(); let parameters = resolve_params(definition, &BTreeMap::from([("pattern".into(), Value::PatternGraph(Box::new(graph)))])); let variables = BTreeMap::new(); + // Two pixels a side put every pixel center on the corner of four of the sixteen squares let context = CookContext { format: materialcraft_core::OutputFormat::F32, diff --git a/crates/nodes/src/tests.rs b/crates/nodes/src/tests.rs index f30301d..b9104c7 100644 --- a/crates/nodes/src/tests.rs +++ b/crates/nodes/src/tests.rs @@ -20,6 +20,7 @@ fn cook(def: &NodeDefinition, inputs: &[Option], size: u32) -> CookResult resource: &no_res, vars: &vars, }; + (def.cook)(&ctx) } @@ -94,6 +95,7 @@ fn every_node_survives_hostile_params() { if matches!(def.id, "graph_instance" | "value_processor" | "input_value") { continue; } + for p in &def.params { for h in [1e30_f32, -1e30, f32::NAN] { let v = match p.default { @@ -120,13 +122,17 @@ fn every_node_survives_hostile_params() { resource: &no_res, vars: &vars, }; + let t = Instant::now(); let out = (def.cook)(&ctx).unwrap_or_else(|e| panic!("{}.{}={h}: {e}", def.id, p.id)); let dt = t.elapsed(); + assert!(dt < Duration::from_secs(2), "{}.{}={h} took {dt:?}", def.id, p.id); + if dt > Duration::from_millis(250) { slow.push((format!("{}.{}={h}", def.id, p.id), dt)); } + for img in &out { assert_eq!((img.w, img.h), (16, 16), "{}.{}={h}", def.id, p.id); assert!(img.data.iter().all(|x| x.is_finite()), "{}.{}={h} produced a non-finite value", def.id, p.id); @@ -134,6 +140,7 @@ fn every_node_survives_hostile_params() { } } } + eprintln!("hostile params: total {:?}; cooks over 250 ms: {slow:?}", total.elapsed()); } @@ -158,7 +165,9 @@ fn blend_and_levels_math() { resource: &no_res, vars: &vars, }; + let out = (def.cook)(&ctx).unwrap(); + assert!((out[0].data[0] - 0.125).abs() < 1e-6); assert!((atomic::levels_fn(0.5, 0.0, 0.5, 1.0, 0.0, 1.0) - 0.5).abs() < 1e-5); assert_eq!(atomic::levels_fn(0.2, 0.4, 0.5, 0.8, 0.0, 1.0), 0.0); diff --git a/crates/nodes/tests/brick_primitives.rs b/crates/nodes/tests/brick_primitives.rs index 78d46fa..e25f65a 100644 --- a/crates/nodes/tests/brick_primitives.rs +++ b/crates/nodes/tests/brick_primitives.rs @@ -17,6 +17,7 @@ fn cook(def: &str, values: BTreeMap, inputs: &[Option]) -> resource: &|_| None, vars: &BTreeMap::new(), }; + (definition.cook)(&context).unwrap().remove(0) } @@ -30,7 +31,9 @@ fn hsl_hue_endpoints_complete_a_full_turn() { assert!((actual - expected).abs() < 1e-5, "hue {hue}: {actual} != {expected}"); } } + let image = cook("hsl", BTreeMap::from([("hue".into(), Value::Float(0.75))]), &inputs); + assert!((image.get(0, 0, false)[2] - 0.7).abs() < 1e-5); } @@ -47,6 +50,7 @@ fn repeated_quadrants_have_distinct_reproducible_random_values() { let values = BTreeMap::from([("pattern".into(), Value::PatternGraph(Box::new(graph)))]); let image = cook("fx_map", values.clone(), &[]); let samples: Vec<_> = [(16, 16), (48, 16), (16, 48), (48, 48)].map(|(x, y)| image.get_gray(x, y, false)).into_iter().collect(); + assert!(samples.windows(2).all(|pair| pair[0] != pair[1]), "{samples:?}"); assert_eq!(image.data, cook("fx_map", values, &[]).data); } @@ -60,6 +64,7 @@ fn brick_variation_bevels_both_axes_without_narrowing_the_plateau_to_a_strip() { let center = image.get_gray(32, 32, false); let horizontal_edge = image.get_gray(3, 32, false); let vertical_edge = image.get_gray(32, 3, false); + assert_eq!(center, 1.0); assert!(horizontal_edge > 0.0 && horizontal_edge < center); assert!((horizontal_edge - vertical_edge).abs() < 1e-5); @@ -76,6 +81,7 @@ fn subpixel_quadrants_do_not_accumulate_at_shared_edges() { leaf.parameters.insert("patterntype".into(), Value::Int(2)); leaf.parameters.insert("opacity".into(), Value::Float(0.25)); let image = cook("fx_map", BTreeMap::from([("pattern".into(), Value::PatternGraph(Box::new(graph)))]), &[]); + assert!(image.data.iter().all(|value| (*value - 0.25).abs() < 1e-6), "mean {}", image.mean()[0]); } @@ -91,5 +97,6 @@ fn grayscale_max_patterns_do_not_accumulate_opacity() { let single = cook("fx_map", values(graph.clone()), &[]); graph.nodes[0].parameters.insert("numberadded".into(), Value::Int(2)); let repeated = cook("fx_map", values(graph), &[]); + assert!(single.data == repeated.data, "repeating a maximum blend must not brighten the image"); } diff --git a/crates/nodes/tests/distance.rs b/crates/nodes/tests/distance.rs index 5e73ea1..c9f2baa 100644 --- a/crates/nodes/tests/distance.rs +++ b/crates/nodes/tests/distance.rs @@ -20,8 +20,8 @@ fn cook(mask: Image, source: Option, tile: bool, combine: bool, distance: fn tiled_distance_spreads_source_across_stationary_gradient() { let mask = Image::gray(32, 32, |x, y| if x == 0 && y == 0 { 1.0 } else { 0.0 }); let output = cook(mask.clone(), Some(mask), true, false, 256.0); - // This is within range of the only mask pixel, but the symmetric distance field has zero - // slope here. A gradient walk stopped on the black source background instead of the seed. + + // A zero slope at the only mask pixel must not stop the walk on background assert_eq!(output.get_gray(16, 16, false), 1.0); assert!(output.data.iter().all(|&v| v == 1.0)); } diff --git a/crates/nodes/tests/fx_map_opacity.rs b/crates/nodes/tests/fx_map_opacity.rs index 0ae61b2..d3d6544 100644 --- a/crates/nodes/tests/fx_map_opacity.rs +++ b/crates/nodes/tests/fx_map_opacity.rs @@ -34,13 +34,14 @@ fn render(depth: usize, roughness: f32, stack: bool, color: Option<[f32; 4]>) -> resource: &|_| None, vars: &BTreeMap::new(), }; + (definition.cook)(&context).unwrap().remove(0) } #[test] fn quadrant_opacity_matches_measured_depth_and_stack_values() { - // Pixel values measured from an original constant-square SBS diagnostic at 32-bit float precision. - // Empty ancestor quadrants still count toward the depth normalization; zero roughness bypasses it. + // Pixel values come from an original constant-square 32-bit float diagnostic + // Empty ancestors affect depth normalization; zero roughness bypasses it for (roughness, depth, expected) in [ (0.0, 0, 0.125), (0.0, 1, 0.125), @@ -56,14 +57,17 @@ fn quadrant_opacity_matches_measured_depth_and_stack_values() { (1.0, 4, 0.025), ] { let image = render(depth, roughness, false, None); + assert!( image.data.iter().all(|value| (*value - expected).abs() < 2e-7), "roughness {roughness}, depth {depth}: {:?}, expected {expected}", image.mean() ); } + for roughness in [0.42, 1.0] { let image = render(4, roughness, true, None); + assert!(image.data.iter().all(|value| (*value - 0.125).abs() < 2e-7), "stack roughness {roughness}: {:?}", image.mean()); } } diff --git a/crates/nodes/tests/fx_map_regions.rs b/crates/nodes/tests/fx_map_regions.rs index 4887dac..5b28606 100644 --- a/crates/nodes/tests/fx_map_regions.rs +++ b/crates/nodes/tests/fx_map_regions.rs @@ -33,6 +33,7 @@ fn render_region_samples_a_cropped_area() { let identity = [0.0, 1.0, 0.0, 1.0]; let full = render(identity, identity, false).unwrap().remove(0); let crop = render([0.25, 0.75, 0.25, 0.75], identity, false).unwrap().remove(0); + assert_eq!(full.mean()[0], 0.25); assert!(crop.data.iter().all(|value| *value == 1.0)); } @@ -43,6 +44,7 @@ fn tiling_region_sets_repeat_distance_independently_of_render_region() { let repeated = render(region, [0.0, 1.0, 0.0, 1.0], true).unwrap().remove(0); let wider = render(region, region, true).unwrap().remove(0); let untiled = render(region, [0.0, 1.0, 0.0, 1.0], false).unwrap().remove(0); + assert_eq!(repeated.get(48, 32, false)[0], 1.0); assert_eq!(wider.get(48, 32, false)[0], 0.0); assert_eq!(wider.data, untiled.data); @@ -53,6 +55,7 @@ fn tiling_region_sets_repeat_distance_independently_of_render_region() { #[test] fn degenerate_regions_and_excessive_repetition_return_errors() { let identity = [0.0, 1.0, 0.0, 1.0]; + assert!(render([0.0; 4], identity, true).is_err()); assert!(render(identity, [0.0; 4], true).is_err()); assert!(render(identity, [0.0, 1e-9, 0.0, 1e-9], true).is_err()); diff --git a/crates/render/src/preview.rs b/crates/render/src/preview.rs index a7fca3b..a013e61 100644 --- a/crates/render/src/preview.rs +++ b/crates/render/src/preview.rs @@ -89,6 +89,7 @@ impl Scene { if mesh.normals.len() != mesh.positions.len() { mesh.compute_normals(); } + while mesh.triangle_count() > 0 && mesh.triangle_count() * 4 <= displacement.max_triangles { mesh = mesh.subdivided(); } @@ -108,6 +109,7 @@ impl Scene { *height_sum += height.sample_gray(uv.x * tiling, uv.y * tiling, true); *count += 1.0; } + let offsets: Vec = spots.iter().map(|spot| spot_sums.get(spot).map_or(Vec3::ZERO, |(n, h, count)| n.normalize_or_zero() * depth * (h / count - level))).collect(); @@ -115,24 +117,27 @@ impl Scene { // Normals of the displaced surface, shared per spot so seams don't show let moved = |i: u32| mesh.positions.get(i as usize).zip(offsets.get(i as usize)).map(|(p, o)| *p + *o); let mut face_sums: HashMap<[i32; 3], Vec3> = HashMap::new(); - for triangle in mesh.indices.chunks_exact(3) { + for triangle in mesh.indices.as_chunks::<3>().0 { let (Some(a), Some(b), Some(c)) = (moved(triangle[0]), moved(triangle[1]), moved(triangle[2])) else { continue }; let face = (b - a).cross(c - a); for spot in triangle.iter().filter_map(|&i| spots.get(i as usize)) { *face_sums.entry(*spot).or_default() += face; } } + for (spot, n) in spots.iter().zip(mesh.normals.iter_mut()) { if let Some(sum) = face_sums.get(spot).and_then(|s| s.try_normalize()) { *n = sum; } } + mesh.tangents.clear(); } for (p, offset) in mesh.positions.iter_mut().zip(&offsets) { *p += *offset; } + Scene::new(mesh) } @@ -156,9 +161,10 @@ pub const VIEW_TRIANGLES: usize = 500_000; pub fn builtin_mesh(id: &str) -> Mesh { let mut m = materialcraft_geom::primitives::builtin(id).unwrap_or_else(|| materialcraft_geom::primitives::rounded_box(Vec3::ONE, 0.15)); if id == "sphere" { - // The texture wraps twice around, so texels are square at the equator. + // The texture wraps twice around, so texels are square at the equator m.uvs.iter_mut().for_each(|uv| uv.x *= 2.0); } + m } @@ -189,6 +195,7 @@ impl Lighting { if levels == 0 { return Vec3::ZERO; } + let l = rough.clamp(0.0, 1.0) * (levels - 1) as f32; let i = (l.floor() as usize).min(levels - 1); let j = (i + 1).min(levels - 1); @@ -349,6 +356,7 @@ fn direct(n: Vec3, v: Vec3, l: Vec3, base: Vec3, f0: Vec3, metal: f32, rough: f3 if ndl <= 0.0 { return Vec3::ZERO; } + let ndv = n.dot(v).max(1e-4); let h = (v + l).normalize_or_zero(); let (ndh, vdh) = (n.dot(h).max(0.0), v.dot(h).max(0.0)); @@ -379,7 +387,8 @@ fn shade(scene: &Scene, maps: &Maps, light: &Lighting, s: &Settings, origin: Vec let w0 = 1.0 - w1 - w2; let lerp3 = |a: Vec3, b: Vec3, c: Vec3| a * w0 + b * w1 + c * w2; let nrm = lerp3(*m.normals.get(i0)?, *m.normals.get(i1)?, *m.normals.get(i2)?).normalize_or_zero(); - // A back face (seen through a transparent front, or under an open mesh) faces the viewer. + + // A back face (seen through a transparent front, or under an open mesh) faces the viewer let nrm = if nrm.dot(dir) > 0.0 { -nrm } else { nrm }; let (t0, t1, t2) = (*m.tangents.get(i0)?, *m.tangents.get(i1)?, *m.tangents.get(i2)?); let tan = (t0.truncate() * w0 + t1.truncate() * w1 + t2.truncate() * w2).normalize_or_zero(); @@ -393,6 +402,7 @@ fn shade(scene: &Scene, maps: &Maps, light: &Lighting, s: &Settings, origin: Vec if s.alpha_view { return Some(Shaded { color: Vec3::splat(a), alpha: 1.0, t: hit.t }); } + let base = tex(&maps.base_color, uv).map_or(Vec3::splat(0.5), |c| Vec3::new(c[0], c[1], c[2])); let rough = tex_gray(&maps.roughness, uv, 0.5).clamp(0.02, 1.0); let metal = tex_gray(&maps.metallic, uv, 0.0).clamp(0.0, 1.0); @@ -403,8 +413,8 @@ fn shade(scene: &Scene, maps: &Maps, light: &Lighting, s: &Settings, origin: Vec if s.normal_y_flip { t.y = -t.y; } - // OpenGL convention: green points along +v in texture space, which is -bitangent - // with top-left UVs. + + // Green points along texture +v, opposite the bitangent for top-left UVs (tan * t.x - bit * t.y + nrm * t.z.max(0.05)).normalize_or_zero() } None => nrm, @@ -420,7 +430,7 @@ fn shade(scene: &Scene, maps: &Maps, light: &Lighting, s: &Settings, origin: Vec let diff = light.diffuse(rot(n)) * diffuse_col; let mut c = (diff + spec) * ao; if s.ao_rays > 0 || s.key_light > 0.0 { - // The true surface (a displaced scene's facets), offset to avoid self-hits. + // The true surface (a displaced scene's facets), offset to avoid self-hits let (p0, p1, p2) = (*m.positions.get(i0)?, *m.positions.get(i1)?, *m.positions.get(i2)?); let face = (p1 - p0).cross(p2 - p0).normalize_or_zero(); let face = if face.dot(nrm) < 0.0 { -face } else { face }; @@ -435,6 +445,7 @@ fn shade(scene: &Scene, maps: &Maps, light: &Lighting, s: &Settings, origin: Vec if let Some(e) = tex(&maps.emissive, uv) { c += Vec3::new(e[0], e[1], e[2]); } + Some(Shaded { color: c, alpha: if s.transparency { a } else { 1.0 }, t: hit.t }) } @@ -453,8 +464,10 @@ fn trace(scene: &Scene, maps: &Maps, light: &Lighting, s: &Settings, origin: Vec if through < 1e-3 { return acc; } + o += dir * (h.t + step); } + acc + miss * through } @@ -489,7 +502,8 @@ pub fn render_cancellable(scene: &Scene, maps: &Maps, light: &Lighting, s: &Sett let ss = s.supersample.clamp(1, 4); let k = 2f32.powf(s.exposure); let bg_env = light.specular.get(3).or(light.specular.last()); - // Key light from the camera's upper left, a little in front of the subject. + + // Key light from the camera's upper left, a little in front of the subject let fwd = cam.ray(Vec2::ZERO, aspect).dir; let right = fwd.cross(Vec3::Y).normalize_or(Vec3::X); let key = (right * -0.6 + right.cross(fwd) * 0.75 - fwd * 0.5).normalize_or_zero(); @@ -498,6 +512,7 @@ pub fn render_cancellable(scene: &Scene, maps: &Maps, light: &Lighting, s: &Sett if cancel.load(Ordering::Relaxed) { return; } + for x in 0..w as usize { let mut acc = Vec3::ZERO; for sy in 0..ss { @@ -512,7 +527,7 @@ pub fn render_cancellable(scene: &Scene, maps: &Maps, light: &Lighting, s: &Sett _ => bg, }; acc += if s.alpha_view { - // Display values: the alpha as is, the background tone mapped. + // Display values: the alpha as is, the background tone mapped match shade(scene, maps, light, s, ray.origin, ray.dir, &p) { Some(h) => h.color, None => miss.map(|v| linear_to_srgb(aces(v))), @@ -522,6 +537,7 @@ pub fn render_cancellable(scene: &Scene, maps: &Maps, light: &Lighting, s: &Sett }; } } + let c = acc / (ss * ss) as f32; let q = |v: f32| { let d = if s.alpha_view { v } else { linear_to_srgb(aces(v)) }; @@ -549,12 +565,16 @@ mod tests { height: Some(Image::gray(16, 16, |x, _| (x % 4) as f32 / 4.0)), ..Maps::default() }; + for id in ["rounded_cube", "sphere", "plane", "cylinder", "torus", "material_ball", "cube"] { let scene = Scene::builtin(id); let px = render(&scene, &maps, &light, &Settings::default(), 64, 48); + assert_eq!(px.len(), 64 * 48 * 4); - // The center hits the mesh (the torus' hole is left of it): reddish. + + // The center hits the mesh (the torus' hole is left of it): reddish let c = if id == "torus" { ((24 * 64 + 20) * 4) as usize } else { ((24 * 64 + 32) * 4) as usize }; + assert!(px[c] > px[c + 1], "{id}: center is not red ({:?})", &px[c..c + 3]); } } @@ -567,18 +587,26 @@ mod tests { let s = Settings::default(); let opaque = render(&scene, &red(1.0), &light, &s, 64, 48); let clear = render(&scene, &red(0.0), &light, &s, 64, 48); + assert!(opaque[c] > opaque[c + 1] + 40, "opaque center is red: {:?}", &opaque[c..c + 3]); - // Fully clear: the backdrop shows through both the front and the back of the sphere. + + // Fully clear: the backdrop shows through both the front and the back of the sphere let bg = backdrop(Vec2::new(32.5 / 64.0 * 2.0 - 1.0, 1.0 - 24.5 / 48.0 * 2.0)); let bg = (linear_to_srgb(aces(bg.x)) * 255.0 + 0.5) as u8; + assert!(clear[c..c + 3].iter().all(|v| v.abs_diff(bg) <= 1), "clear {:?} vs backdrop {bg}", &clear[c..c + 3]); - // Transparency off: alpha is ignored. + + // Transparency off: alpha is ignored assert_eq!(render(&scene, &red(0.0), &light, &Settings { transparency: false, ..s }, 64, 48), opaque); - // An opacity output wins over the base color's alpha. + + // An opacity output wins over the base color's alpha let half = Maps { opacity: Some(Image::filled(16, 16, Kind::Gray, [0.5; 4])), ..red(1.0) }; let px = render(&scene, &half, &light, &Settings { alpha_view: true, ..s }, 64, 48); + assert!(px[c].abs_diff(128) <= 1 && px[c] == px[c + 1], "alpha view: {:?}", &px[c..c + 3]); + let px = render(&scene, &red(1.0), &light, &Settings { alpha_view: true, ..s }, 64, 48); + assert_eq!(&px[c..c + 3], &[255, 255, 255]); } @@ -669,25 +697,36 @@ mod tests { let scene = Scene::builtin("sphere"); let s = Settings::default(); let at = |s: &Settings| camera(&scene, s).project(scene.center, 1.0).unwrap(); - // Dragging right and down by a tenth of the view moves the center right and down by - // the same fraction (NDC spans two view heights, y up). + + // Drag fractions move the center by the same fraction in y-up NDC let right = Settings { target: pan(&scene, &s, 0.1, 0.0), ..s }; let p = at(&right); + assert!((p.x - 0.2).abs() < 1e-3 && p.y.abs() < 1e-3, "{p:?}"); + let down = Settings { target: pan(&scene, &s, 0.0, 0.1), ..s }; let p = at(&down); + assert!(p.x.abs() < 1e-3 && (p.y + 0.2).abs() < 1e-3, "{p:?}"); - // Orbiting turns around the panned target, not the scene center. + + // Orbiting turns around the panned target, not the scene center let turned = Settings { yaw: s.yaw + 90.0, ..right }; let cam = camera(&scene, &turned); + assert!((cam.target - camera(&scene, &right).target).length() < 1e-5); - // Panning back returns home; hostile inputs stay finite. + + // Panning back returns home; hostile inputs stay finite let back = pan(&scene, &right, -0.1, 0.0); + assert!(back.iter().all(|v| v.abs() < 1e-4), "{back:?}"); + let far = pan(&scene, &s, 1e9, f32::NAN); + assert!(far.iter().all(|v| v.is_finite()), "{far:?}"); + for target in [[f32::NAN, 0.0, 0.0], [0.0, f32::INFINITY, 0.0], [0.0, 0.0, f32::MAX]] { let cam = camera(&scene, &Settings { target, ..s }); + assert_eq!(cam.target, scene.center, "{target:?}"); assert!(cam.eye().is_finite()); } diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index b0d3a48..44fbb0a 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -56,11 +56,12 @@ pub fn poll(app: &mut MaterialCraftApp, ctx: &egui::Context) { let reqs: Vec = rx.try_iter().collect(); for r in reqs { if r.method == "ui.screenshot" { - // Captured from the UI pass (`request_screenshots`), where viewport commands land. + // Captured from the UI pass (`request_screenshots`), where viewport commands land app.pending_screenshots.push(r.reply); ctx.request_repaint(); continue; } + let reply = handle(app, ctx, &r.method, &r.params); let _ = r.reply.send(reply); ctx.request_repaint(); @@ -72,6 +73,7 @@ pub fn request_screenshots(app: &mut MaterialCraftApp, ctx: &egui::Context) { if app.pending_screenshots.is_empty() { return; } + let replies = std::mem::take(&mut app.pending_screenshots); let repaint = ctx.clone(); let callback = egui::ScreenshotCallback::new(move |img| { @@ -81,6 +83,7 @@ pub fn request_screenshots(app: &mut MaterialCraftApp, ctx: &egui::Context) { for r in &replies { let _ = r.send(answer.clone()); } + repaint.request_repaint(); }); ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot(callback)); @@ -140,6 +143,7 @@ fn modifiers(v: &Value) -> Modifiers { /// Frames of input for a list of event descriptions. Each press, release and move lands on its /// own frame, so egui sees real clicks and drags. pub fn parse_events(list: &[Value]) -> Result, String> { + const MAX_INPUT_FRAMES: usize = 10_000; let mut frames = Vec::new(); let frame = |events: Vec, _modifiers: Modifiers| Scripted { events }; for (i, e) in list.iter().enumerate() { @@ -204,10 +208,12 @@ pub fn parse_events(list: &[Value]) -> Result, String> { } other => return Err(format!("event {i}: unknown type `{other}`")), } - if frames.len() > 10_000 { + + if frames.len() > MAX_INPUT_FRAMES { return Err("too many input events".into()); } } + Ok(frames) } @@ -268,6 +274,7 @@ fn handle_inner(app: &mut MaterialCraftApp, ctx: &egui::Context, method: &str, p if id == "session.history" { app.ui.prefs.history = app.session.history_prefs; } + app.close_subgraph_after(id); ok(v) } @@ -339,6 +346,7 @@ fn handle_inner(app: &mut MaterialCraftApp, ctx: &egui::Context, method: &str, p if !crate::menus::implemented(id) { return err(format!("menu item `{id}` is not implemented")); } + let modality = crate::shell::modality::Modality::capture(app, ctx); if let crate::shell::modality::Modality::Blocked(blocker) = modality && !modality.allows(id) @@ -354,6 +362,7 @@ fn handle_inner(app: &mut MaterialCraftApp, ctx: &egui::Context, method: &str, p if p.is_object() { merge_json(&mut cur, p); } + match serde_json::from_value::(cur) { Ok(mut ui) => { ui.graph_camera = ui.graph_camera.sanitized(); @@ -402,7 +411,8 @@ mod tests { json!({"type": "scroll", "pos": [5, 5], "delta": [0, -60]}), ]; let f = parse_events(&list).unwrap(); - // click: move, press, release; drag: move, press, 4 moves, release; key: 2; text 1; scroll 1. + + // click: move, press, release; drag: move, press, 4 moves, release; key: 2; text 1; scroll 1 assert_eq!(f.len(), 3 + 7 + 2 + 1 + 1); assert!(parse_events(&[json!({"type": "key", "key": "NoSuchKey"})]).is_err()); assert!(parse_events(&[json!({"type": "click"})]).is_err()); @@ -479,19 +489,30 @@ mod tests { 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); } diff --git a/crates/ui-egui/src/function/node_face.rs b/crates/ui-egui/src/function/node_face.rs index e9efa0c..09a7637 100644 --- a/crates/ui-egui/src/function/node_face.rs +++ b/crates/ui-egui/src/function/node_face.rs @@ -141,11 +141,13 @@ mod tests { #[test] fn a_body_grows_with_the_components_of_its_value() { let single = height(&node("const_float1")); + assert_eq!(height(&node("const_float4")), single + 3.0 * (ROW_HEIGHT + ROW_GAP)); assert_eq!(height(&node("add")), SYMBOL_HEIGHT); let mut unavailable = node("const_float4"); unavailable.missing = Some(Vec::new()); + assert_eq!(height(&unavailable), SYMBOL_HEIGHT); } } diff --git a/crates/ui-egui/src/function/render.rs b/crates/ui-egui/src/function/render.rs index c9f3542..ad73c11 100644 --- a/crates/ui-egui/src/function/render.rs +++ b/crates/ui-egui/src/function/render.rs @@ -48,6 +48,7 @@ pub(super) fn paint(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, ui: destination = destination.union(Rect::from_min_size(pos2(frame.rect[0], frame.rect[1] - 32.0), vec2(frame.rect[2], frame.rect[3] + 32.0))); } } + let camera = crate::graph::GraphCamera::fit(canvas_bounds, destination); editor.pan = camera.pan; editor.zoom = camera.zoom; diff --git a/crates/ui-egui/src/graph/canvas.rs b/crates/ui-egui/src/graph/canvas.rs index 2e75b6e..ae90ef8 100644 --- a/crates/ui-egui/src/graph/canvas.rs +++ b/crates/ui-egui/src/graph/canvas.rs @@ -60,7 +60,9 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { app.graph.selected_wires.clear(); frame(app, rect, false); } + let painter = ui.painter_at(rect); + // Background and grid painter.rect_filled(rect, 0.0, Color32::from_rgb(0x2b, 0x2d, 0x31)); let mut camera = GraphCamera { origin: rect.min, pan: Vec2::from(app.ui.graph_camera.pan), zoom: app.ui.graph_camera.zoom }; @@ -99,6 +101,7 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { outs: outs.iter().enumerate().map(|(i, p)| (p.id.clone(), p.label.clone(), p.kind, port_position(i, true, outs.len()))).collect(), }); } + app.graph.rects = layouts.iter().map(|layout| (layout.id, Rect::from_min_max(camera.to_screen(layout.rect.min), camera.to_screen(layout.rect.max)))).collect(); app.graph.ports = layouts @@ -148,12 +151,14 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { if detached.as_ref().is_some_and(|(node, p)| *node == c.to && *p == c.to_port) { continue; } + let a = layout_indices.get(&c.from).and_then(|index| layouts.get(*index)).and_then(|layout| layout.outs.iter().find(|p| p.0 == c.from_port)); let b = layout_indices.get(&c.to).and_then(|index| layouts.get(*index)).and_then(|layout| layout.ins.iter().find(|p| p.0 == c.to_port)); if let (Some(a), Some(b)) = (a, b) { wires.push(((c.to, c.to_port.clone()), bezier_points(camera.to_screen(a.3), camera.to_screen(b.3)))); } } + // Hover: not where a node card or a port sits on top of the wire let over_card = |p: Pos2| app.graph.rects.iter().any(|(_, r)| r.contains(p)) || nearest_port(&app.graph.ports, p, 9.0).is_some(); let wire_hover = match &app.graph.drag { @@ -181,6 +186,7 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { painter.add(egui::epaint::CubicBezierShape::from_points_stroke(*ctrl, false, Color32::TRANSPARENT, stroke)); } + app.graph.wires = wires; // Nodes @@ -194,14 +200,17 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { if !screen_rect.intersects(rect) { continue; } + let selected = selection.contains(&node.id); let result = cooked.nodes.get(&node.id); let error = result.and_then(|r| r.error.as_ref()); + // A dot is tiny, so its grab area keeps a minimum on-screen size let grab = if node.def == "dot" { node_card::dot_grab(screen_rect) } else { screen_rect }; if node.def == "dot" { dot_grabs.push(grab); } + let resp = ui.interact(grab, ui.scope_id().with(("node", node.id.0)), Sense::click_and_drag()); crate::widgets::name(&resp, egui::Role::Button, &node_label(node)); if node.def == "dot" { @@ -228,6 +237,7 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { } else if result.is_none() { node_card::paint_symbol(&painter, body, camera.zoom, icon::HOURGLASS_MEDIUM, theme.text_dim); } + // Footer: size and format let footer = Rect::from_min_max(pos2(screen_rect.left(), body.bottom()), screen_rect.max); if let Some(img) = result.and_then(|r| r.outputs.first().and_then(materialcraft_engine::eval::NodeOutput::as_image)) { @@ -271,6 +281,7 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { resp.clone().on_hover_text(e); } } + node_responses.push((node.id, resp)); } @@ -279,13 +290,13 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { let pin_hits = super::annotation_canvas::pins(app, ui, &graph.pins, &camera); app.graph.pins = pin_hits.clone(); - // Ports (drawn last, interactable over the node cards) - // except on a dot's body: its port hit circles would otherwise cover it and it could never be moved + // A dot body stays movable when port hit circles overlap it let port_hover = pointer.filter(|p| !dot_grabs.iter().any(|grab| grab.contains(*p))).and_then(|p| nearest_port(&app.graph.ports, p, 9.0)).cloned(); for h in &app.graph.ports { if !rect.contains(h.pos) { continue; } + let hot = port_hover.as_ref().is_some_and(|x| x.node == h.node && x.port == h.port && x.output == h.output); let r = (PORT_RADIUS * camera.zoom).clamp(2.5, 7.0) * if hot { 1.4 } else { 1.0 }; @@ -309,6 +320,7 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { if wire_hover.is_some() { ctx.set_cursor_icon(egui::CursorIcon::PointingHand); } + app.graph.inspected_node = super::node_selection::inspected(&app.session.selection, app.graph.inspected_node); super::input::handle(app, ui, &resp, rect, &camera, &node_responses, &frame_hits, &comment_hits, &pin_hits, port_hover, wire_hover); app.graph.inspected_node = super::node_selection::inspected(&app.session.selection, app.graph.inspected_node); @@ -340,6 +352,7 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) { let text = format!("{} {} node(s) failed", icon::WARNING, failed.len()); at.y = painter.text(at, Align2::RIGHT_BOTTOM, text, FontId::proportional(11.0), theme.danger).top() - 2.0; } + // Placeholders for missing SBS nodes are a soft warning, not a failure if placeholders > 0 { let text = format!("{} {placeholders} missing node(s), see the Console", icon::WARNING); diff --git a/crates/ui-egui/src/graph/interaction_tests.rs b/crates/ui-egui/src/graph/interaction_tests.rs index 05e5dc2..b85c944 100644 --- a/crates/ui-egui/src/graph/interaction_tests.rs +++ b/crates/ui-egui/src/graph/interaction_tests.rs @@ -404,12 +404,16 @@ fn function_dots_wire_move_and_save_with_regular_card_geometry() { let zoom = frames.app.ui.graph_camera.zoom; let dot_bounds = frames.app.graph.rects.iter().find(|(id, _)| id.0 == u64::from(dot)).unwrap().1; let card = frames.app.graph.rects.iter().find(|(id, _)| id.0 == u64::from(multiply)).unwrap().1; + assert!((dot_bounds.width() / zoom - 14.0).abs() < 0.01); assert!((card.width() / zoom - 100.0).abs() < 0.01); + // An operator shows its symbol in a body that only fits its two inputs assert!((card.height() / zoom - 76.0).abs() < 0.01); assert!(!frames.canvas_text.iter().any(|text| text.contains("Reroute"))); + let dot_ports: Vec<_> = frames.app.graph.ports.iter().filter(|port| port.node.0 == u64::from(dot)).collect(); + assert_eq!(dot_ports.len(), 2); assert!((dot_ports[0].pos.y - dot_ports[1].pos.y).abs() < 0.01); assert!(dot_ports[0].pos.x < dot_ports[1].pos.x); @@ -420,26 +424,39 @@ fn function_dots_wire_move_and_save_with_regular_card_geometry() { let end = frames.app.graph.ports.iter().find(|port| port.node.0 == u64::from(to) && port.port == input).unwrap().pos; drag(&mut frames, start, end); } + let graph = frames.app.session.graph().unwrap().function.as_ref().unwrap(); + assert_eq!(graph.node(dot).unwrap().inputs, vec![Some(source)]); assert_eq!(graph.node(multiply).unwrap().inputs, vec![Some(dot), Some(dot)]); + let before_move = frames.app.session.pkg.clone(); let center = frames.center(NodeId(u64::from(dot))); drag(&mut frames, center, center + vec2(64.0, 32.0) * zoom); let graph = frames.app.session.graph().unwrap().function.as_ref().unwrap(); + assert_eq!(graph.node(dot).unwrap().pos, [288.0, 96.0]); assert_eq!(graph.node(multiply).unwrap().inputs, vec![Some(dot), Some(dot)]); let bytes = write_sbs(&frames.app.session.pkg).unwrap(); + assert!(std::str::from_utf8(&bytes).unwrap().contains("")); + let read = read_sbs(&bytes, "dot_sample.sbs", &|_| None).unwrap(); + assert!(read.warnings.is_empty(), "{:?}", read.warnings); + let saved = read.pkg.graphs.iter().find(|graph| graph.name == "Dot_sample").unwrap().function.as_ref().unwrap(); let saved_dot = saved.nodes.iter().find(|node| node.op == "passthrough").unwrap(); + assert_eq!(saved_dot.pos, [288.0, 96.0]); + let saved_multiply = saved.nodes.iter().find(|node| node.op == "mul").unwrap(); + assert_eq!(saved_multiply.inputs, vec![Some(saved_dot.id), Some(saved_dot.id)]); + frames.app.session.undo().unwrap(); + assert_eq!(frames.app.session.pkg, before_move); } @@ -824,6 +841,7 @@ fn wires_pulled_from_an_unconnected_input_connect_on_an_output() { let ports = |frames: &Frames, node: NodeId, output: bool| { frames.app.graph.ports.iter().filter(|port| port.node == node && port.output == output).cloned().collect::>() }; + let (inputs, output) = (ports(&frames, target, false), ports(&frames, source, true)[0].clone()); let wires = |frames: &Frames| { frames @@ -837,21 +855,29 @@ fn wires_pulled_from_an_unconnected_input_connect_on_an_output() { .collect::>() }; let empty = frames.app.graph.view.left_bottom() + vec2(24.0, -24.0); + assert!(!frames.app.graph.rects.iter().any(|(_, rect)| rect.expand(12.0).contains(empty)), "the drop point must be empty canvas"); // A click, and drops on another input and on the node's own output leave the input unconnected let history = frames.app.session.history(); frames.click(inputs[0].pos, Modifiers::NONE); frames.time += 1.0; + assert!(frames.app.graph.search.is_none(), "a click on an input opens nothing"); + for end in [inputs[1].pos, ports(&frames, target, true)[0].pos] { drag(&mut frames, inputs[0].pos, end); } + assert!(frames.app.graph.search.is_none()); + // Dropped on empty canvas, it searches for a node to feed the input, as an output wire does drag(&mut frames, inputs[0].pos, empty); + assert!(frames.app.graph.search.as_ref().is_some_and(|search| search.connect_to == Some((target, inputs[0].port.clone())))); + frames.app.graph.search = None; + assert!(wires(&frames).is_empty()); assert_eq!(frames.app.session.history(), history, "a missed wire must not add an undo step"); @@ -860,17 +886,25 @@ fn wires_pulled_from_an_unconnected_input_connect_on_an_output() { frames.run(vec![Event::PointerMoved(inputs[0].pos)], Modifiers::NONE); frames.run(vec![Event::PointerButton { pos: inputs[0].pos, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }], Modifiers::NONE); frames.run(vec![Event::PointerMoved(midway)], Modifiers::NONE); + assert!(matches!(&frames.app.graph.drag, Some(super::GraphDrag::InputWire { to }) if *to == (target, inputs[0].port.clone()))); + frames.run(vec![Event::PointerMoved(output.pos)], Modifiers::NONE); frames.run(vec![Event::PointerButton { pos: output.pos, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }], Modifiers::NONE); + assert_eq!(wires(&frames), [(source, output.port.clone(), target, inputs[0].port.clone())]); // The second input too; then the connected first input is unplugged by pulling its wire, as before drag(&mut frames, inputs[1].pos, output.pos); + assert_eq!(wires(&frames)[1], (source, output.port.clone(), target, inputs[1].port.clone())); + drag(&mut frames, inputs[0].pos, empty); + assert_eq!(wires(&frames), [(source, output.port.clone(), target, inputs[1].port.clone())]); + frames.app.session.undo().unwrap(); + assert_eq!(wires(&frames).len(), 2); } diff --git a/crates/ui-egui/src/graph/navigation.rs b/crates/ui-egui/src/graph/navigation.rs index 1452a45..60818a6 100644 --- a/crates/ui-egui/src/graph/navigation.rs +++ b/crates/ui-egui/src/graph/navigation.rs @@ -49,6 +49,7 @@ pub(super) fn frame(app: &mut MaterialCraftApp, view: Rect, only_selection: bool r = r.union(Rect::from_min_size(pos2(f.rect[0], f.rect[1] - 32.0), vec2(f.rect[2], f.rect[3] + 32.0))); } } + app.ui.graph_camera = super::GraphCamera::fit(view, r).state(); } diff --git a/crates/ui-egui/src/graph/node_style.rs b/crates/ui-egui/src/graph/node_style.rs index e841be5..b51469c 100644 --- a/crates/ui-egui/src/graph/node_style.rs +++ b/crates/ui-egui/src/graph/node_style.rs @@ -38,9 +38,11 @@ pub fn node_label(n: &Node) -> String { if !n.label.is_empty() { return n.label.clone(); } + if let Some(instance) = &n.instance { return if instance.label.is_empty() { instance.graph.clone() } else { instance.label.clone() }; } + let label = match n.def.as_str() { "graph_instance" => match n.params.get("graph") { Some(Value::Text(g)) => g.clone(), @@ -55,6 +57,7 @@ pub fn node_label(n: &Node) -> String { d.to_string() } }; + label.split(['_', ' ']).filter(|word| !word.is_empty()).fold(String::with_capacity(label.len()), |mut display, word| { if !display.is_empty() { display.push(' '); @@ -64,6 +67,7 @@ pub fn node_label(n: &Node) -> String { display.extend(first.to_uppercase()); display.extend(letters); } + display }) } diff --git a/crates/ui-egui/src/graph_preview/active_graph_preview.rs b/crates/ui-egui/src/graph_preview/active_graph_preview.rs new file mode 100644 index 0000000..23d9987 --- /dev/null +++ b/crates/ui-egui/src/graph_preview/active_graph_preview.rs @@ -0,0 +1,21 @@ +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::mpsc::Receiver; + +use materialcraft_engine::NodeLibrary; + +use super::GraphPreviewKey; + +pub(super) struct ActiveGraphPreview { + pub(super) library: Arc, + pub(super) key: GraphPreviewKey, + pub(super) cancel: Arc, + pub(super) result: Receiver>>, +} + +impl Drop for ActiveGraphPreview { + fn drop(&mut self) { + // Dropping preview state cancels its background cook + self.cancel.store(true, Ordering::Relaxed); + } +} diff --git a/crates/ui-egui/src/graph_preview/graph_preview_key.rs b/crates/ui-egui/src/graph_preview/graph_preview_key.rs new file mode 100644 index 0000000..39cc4ab --- /dev/null +++ b/crates/ui-egui/src/graph_preview/graph_preview_key.rs @@ -0,0 +1,5 @@ +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub(super) enum GraphPreviewKey { + Graph(String, String), + Native(String), +} diff --git a/crates/ui-egui/src/graph_previews.rs b/crates/ui-egui/src/graph_previews.rs index edb05c0..1be71bb 100644 --- a/crates/ui-egui/src/graph_previews.rs +++ b/crates/ui-egui/src/graph_previews.rs @@ -1,75 +1,70 @@ use std::collections::HashMap; use std::sync::Arc; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::mpsc::{Receiver, TryRecvError}; +#[cfg(not(target_arch = "wasm32"))] +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::sync::mpsc::TryRecvError; use egui::{ColorImage, TextureHandle, TextureOptions}; use materialcraft_engine::NodeLibrary; use super::LIBRARY_PREVIEW_SIZE; -#[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. +#[path = "graph_preview/active_graph_preview.rs"] +mod active; +#[path = "graph_preview/graph_preview_key.rs"] +mod key; + +use active::ActiveGraphPreview; +use key::GraphPreviewKey; + +// Bound GPU texture storage independently of library size +const MAX_TEXTURES: usize = 256; const MAX_VISIBLE_REQUESTS: usize = 64; /// Only one cook may run; every frame replaces the pending work with its visible tiles. #[derive(Default)] pub(super) struct GraphPreviews { #[cfg(not(target_arch = "wasm32"))] - pub persistence: Option, + pub persistence: Option, library: Option>, - textures: HashMap, u64)>, - wanted: Vec, - active: Option, + textures: HashMap, u64)>, + wanted: Vec, + active: Option, frame: u64, } -struct Active { - library: Arc, - key: Key, - cancel: Arc, - result: Receiver>>, -} - -impl Drop for Active { - fn drop(&mut self) { - // Closing the app or its preview state must not leave background cooks running. - self.cancel.store(true, Ordering::Relaxed); - } -} - impl GraphPreviews { pub fn get(&mut self, _ctx: &egui::Context, library: &Arc, package: &str, graph: &str) -> Option { self.set_library(library); - self.request(Key::Graph(package.to_string(), graph.to_string())) + self.request(GraphPreviewKey::Graph(package.to_string(), graph.to_string())) } pub fn native(&mut self, _ctx: &egui::Context, id: &str) -> Option { - self.request(Key::Native(id.into())) + self.request(GraphPreviewKey::Native(id.into())) } - fn request(&mut self, key: Key) -> Option { + fn request(&mut self, key: GraphPreviewKey) -> Option { if let Some((texture, used)) = self.textures.get_mut(&key) { *used = self.frame; return texture.as_ref().map(TextureHandle::id); } + if self.wanted.len() < MAX_VISIBLE_REQUESTS && !self.wanted.contains(&key) { self.wanted.push(key); } + None } pub fn clear(&mut self) { self.textures.clear(); self.wanted.clear(); + + // Keep the receiver until the worker exits to prevent overlapping renders 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) { @@ -77,16 +72,17 @@ impl GraphPreviews { self.library = Some(library.clone()); self.textures.clear(); self.wanted.clear(); + + // Keep the receiver until the old cook ends to prevent overlapping workers if let Some(active) = &self.active { active.cancel.store(true, Ordering::Relaxed); } - // Keep the receiver until the old cook ends: repeated reloads cannot pile up workers. } } /// Called after drawing the panels, including frames where the Library is hidden. pub fn collect(&mut self, ctx: &egui::Context, library: &Arc, foreground_busy: bool) { - // A hidden panel must also release the previous library after a reload. + // Hidden panels must also release the previous library after a reload self.set_library(library); self.frame = self.frame.saturating_add(1); let finished = self.active.as_ref().and_then(|active| match active.result.try_recv() { @@ -106,6 +102,7 @@ impl GraphPreviews { } ctx.request_repaint(); } + if let Some(active) = &self.active { if foreground_busy || !self.wanted.contains(&active.key) { active.cancel.store(true, Ordering::Relaxed); @@ -117,10 +114,11 @@ impl GraphPreviews { { self.start(ctx, library, key); } + self.wanted.clear(); } - fn remember(&mut self, key: Key, texture: Option) { + fn remember(&mut self, key: GraphPreviewKey, texture: Option) { self.textures.insert(key, (texture, self.frame)); while self.textures.len() > MAX_TEXTURES { let Some(oldest) = self.textures.iter().min_by_key(|(_, (_, used))| *used).map(|(key, _)| key.clone()) else { break }; @@ -129,7 +127,7 @@ impl GraphPreviews { } #[cfg(not(target_arch = "wasm32"))] - fn start(&mut self, ctx: &egui::Context, library: Arc, key: Key) { + fn start(&mut self, ctx: &egui::Context, library: Arc, key: GraphPreviewKey) { 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()); @@ -139,29 +137,34 @@ impl GraphPreviews { 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()), + GraphPreviewKey::Native(id) => super::render_native(id), + GraphPreviewKey::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 = 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) => { + GraphPreviewKey::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), + GraphPreviewKey::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(), }; + let _ = sender.send(rgba); context.request_repaint(); }); if spawned.is_ok() { - self.active = Some(Active { library, key, cancel, result }); + self.active = Some(ActiveGraphPreview { library, key, cancel, result }); } else { self.remember(key, None); } @@ -172,16 +175,18 @@ impl GraphPreviews { 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, key: Key) { - // Native previews remain available in the browser without a filesystem or cook worker. + fn start(&mut self, ctx: &egui::Context, _library: Arc, key: GraphPreviewKey) { + // Native previews remain available in the browser without filesystem access let rgba = match &key { - Key::Native(id) => super::render_native(id), - Key::Graph(_, _) => None, + GraphPreviewKey::Native(id) => super::render_native(id), + GraphPreviewKey::Graph(_, _) => None, }; + let texture = rgba.map(|rgba| { ctx.load_texture( format!("library-preview-{key:?}"), @@ -198,9 +203,9 @@ impl GraphPreviews { mod tests { use super::*; - fn active(library: Arc, key: Key) -> (Active, std::sync::mpsc::Sender>>) { + fn active(library: Arc, key: GraphPreviewKey) -> (ActiveGraphPreview, std::sync::mpsc::Sender>>) { let (sender, result) = std::sync::mpsc::channel(); - (Active { library, key, cancel: Arc::new(AtomicBool::new(false)), result }, sender) + (ActiveGraphPreview { library, key, cancel: Arc::new(AtomicBool::new(false)), result }, sender) } #[test] @@ -208,7 +213,7 @@ mod tests { let context = egui::Context::default(); let library = Arc::new(NodeLibrary::default()); let mut previews = GraphPreviews::default(); - let key = Key::Graph("test".into(), "graph".into()); + let key = GraphPreviewKey::Graph("test".into(), "graph".into()); let (job, _sender) = active(library.clone(), key.clone()); let cancelled = job.cancel.clone(); previews.active = Some(job); @@ -216,8 +221,11 @@ mod tests { for graph in 0..1_000 { previews.get(&context, &library, "test", &graph.to_string()); } + assert_eq!(previews.wanted.len(), MAX_VISIBLE_REQUESTS); + previews.collect(&context, &library, false); + assert!(cancelled.load(Ordering::Relaxed)); assert!(previews.wanted.is_empty()); assert_eq!(previews.active.as_ref().unwrap().key, key, "wait for cancellation before starting another worker"); @@ -228,14 +236,17 @@ mod tests { let context = egui::Context::default(); let library = Arc::new(NodeLibrary::default()); let old = Arc::downgrade(&library); - let (job, sender) = active(library.clone(), Key::Graph("test".into(), "graph".into())); + let (job, sender) = active(library.clone(), GraphPreviewKey::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"); + assert!(previews.active.as_ref().unwrap().cancel.load(Ordering::Relaxed)); + previews.wanted.clear(); sender.send(None).unwrap(); previews.collect(&context, &new_library, false); + assert!(previews.active.is_none()); assert!(previews.textures.is_empty(), "a cancelled result must be eligible for another request"); assert!(old.upgrade().is_none()); @@ -247,11 +258,12 @@ 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(Key::Graph("test".into(), "old".into()), Some(texture)); + previews.remember(GraphPreviewKey::Graph("test".into(), "old".into()), Some(texture)); for index in 0..MAX_TEXTURES { previews.frame += 1; - previews.remember(Key::Graph("test".into(), index.to_string()), None); + previews.remember(GraphPreviewKey::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"); } @@ -262,7 +274,7 @@ mod tests { let library = Arc::new(NodeLibrary::default()); let mut previews = GraphPreviews::default(); previews.set_library(&library); - let key = Key::Native("blur".into()); + let key = GraphPreviewKey::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)); @@ -271,18 +283,23 @@ mod tests { 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_eq!(previews.active.as_ref().unwrap().key, GraphPreviewKey::Native("levels".into())); assert!(previews.wanted.is_empty(), "clearing must not schedule the entire native catalog"); } @@ -293,11 +310,14 @@ mod tests { let mut previews = GraphPreviews::default(); previews.get(&context, &library, "test", "graph"); previews.collect(&context, &library, true); + assert!(previews.active.is_none()); - let (job, _sender) = active(library.clone(), Key::Graph("test".into(), "graph".into())); + + let (job, _sender) = active(library.clone(), GraphPreviewKey::Graph("test".into(), "graph".into())); let cancelled = job.cancel.clone(); previews.active = Some(job); drop(previews); + assert!(cancelled.load(Ordering::Relaxed)); } } diff --git a/crates/ui-egui/src/library.rs b/crates/ui-egui/src/library.rs index 444545d..40dc9bb 100644 --- a/crates/ui-egui/src/library.rs +++ b/crates/ui-egui/src/library.rs @@ -12,10 +12,12 @@ use materialcraft_engine::{LibraryCanvas, LibraryEntry, LibraryItem}; use crate::MaterialCraftApp; use crate::function::library::FunctionLibraryDrag; +mod library_divider_action; mod library_target; use crate::graph::{LibraryDrag, header_color}; use crate::i18n::tr; use crate::theme::Tokens; +use library_divider_action::LibraryDividerAction; pub(crate) use library_target::LibraryTarget; /// The category key of the Favorites row @@ -139,10 +141,11 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { egui::ScrollArea::vertical().id_salt("lib-tree").auto_shrink([false, false]).show(ui, |ui| category_tree(app, ui, &all)); }); match divider(ui, tree_width, available_size.y) { - Some(Divider::Dragged(width)) => app.ui.library_tree_width = self::tree_width(width, available_size.x), - Some(Divider::Reset) => app.ui.library_tree_width = 0.0, + Some(LibraryDividerAction::Dragged(width)) => app.ui.library_tree_width = self::tree_width(width, available_size.x), + Some(LibraryDividerAction::Reset) => app.ui.library_tree_width = 0.0, None => {} } + ui.allocate_ui_with_layout(vec2(ui.available_width(), available_size.y), top_down, |ui| { egui::ScrollArea::vertical().id_salt("lib-items").auto_shrink([false, false]).show(ui, |ui| items(app, ui, &all, &target)); }); @@ -157,16 +160,8 @@ fn tree_width(chosen: f32, available: f32) -> f32 { chosen.min(available - NARROWEST_TILES).max(NARROWEST_TREE) } -/// What the pointer did to the Library's divider -enum Divider { - /// Dragged to this tree width - Dragged(f32), - /// Double-clicked: back to the automatic width - Reset, -} - /// The draggable bar between the tree and the tiles -fn divider(ui: &mut Ui, tree_width: f32, height: f32) -> Option { +fn divider(ui: &mut Ui, tree_width: f32, height: f32) -> Option { let tokens = Tokens::get(ui.ctx()); let (rect, response) = ui.allocate_exact_size(vec2(9.0, height), Sense::click_and_drag()); crate::widgets::name(&response, egui::Role::Splitter, "Library divider"); @@ -180,9 +175,10 @@ fn divider(ui: &mut Ui, tree_width: f32, height: f32) -> Option { ui.painter().rect_filled(line, 0.0, if active { tokens.accent } else { tokens.text_dim.gamma_multiply(0.35) }); if response.double_clicked() { - return Some(Divider::Reset); + return Some(LibraryDividerAction::Reset); } - response.dragged().then(|| Divider::Dragged(tree_width + response.drag_delta().x)) + + response.dragged().then(|| LibraryDividerAction::Dragged(tree_width + response.drag_delta().x)) } /// Expose availability and canvas filters without changing the selected category. @@ -420,6 +416,7 @@ fn preview(app: &mut MaterialCraftApp, ui: &Ui, ctx: &egui::Context, entry: &Lib } _ => (None, category_color(&entry.category)), }; + match texture { Some(texture) => { painter.image(texture, rect, Rect::from_min_max(egui::Pos2::ZERO, egui::pos2(1.0, 1.0)), Color32::WHITE.gamma_multiply(fade)); @@ -442,6 +439,7 @@ fn preview(app: &mut MaterialCraftApp, ui: &Ui, ctx: &egui::Context, entry: &Lib ); } } + let strip = Rect::from_min_size(rect.left_bottom() - vec2(0.0, 3.0), vec2(rect.width(), 3.0)); painter.rect_filled(strip, 0.0, color.gamma_multiply(fade)); if entry.missing.is_some() && rect.height() > 30.0 { @@ -500,6 +498,7 @@ pub(crate) fn add_entry(app: &mut MaterialCraftApp, entry: &LibraryEntry) { } return; } + match &entry.item { LibraryItem::Operation { operation } => crate::function::library::add(app, &FunctionLibraryDrag::Operation(operation.clone())), LibraryItem::FunctionInstance { package, graph } => { diff --git a/crates/ui-egui/src/library/library_divider_action.rs b/crates/ui-egui/src/library/library_divider_action.rs new file mode 100644 index 0000000..c22092f --- /dev/null +++ b/crates/ui-egui/src/library/library_divider_action.rs @@ -0,0 +1,6 @@ +pub(super) enum LibraryDividerAction { + /// Dragged to this tree width + Dragged(f32), + /// Double-clicked: back to the automatic width + Reset, +} diff --git a/crates/ui-egui/src/material_craft_app.rs b/crates/ui-egui/src/material_craft_app.rs index 8ec44ac..1c01a55 100644 --- a/crates/ui-egui/src/material_craft_app.rs +++ b/crates/ui-egui/src/material_craft_app.rs @@ -79,6 +79,7 @@ impl MaterialCraftApp { app.dialogs = crate::native_dialogs::attached(&cc.egui_ctx); app.thumbs.enable_persistence(app.ui.prefs.library_cache); } + app } @@ -150,6 +151,7 @@ impl MaterialCraftApp { if self.session.camera != self.ui.prefs.camera.sanitized() { self.session.set_camera_prefs(self.ui.prefs.camera); } + let p = if self.ui.prefs.preview_size == 0 { None } else { Some(self.ui.prefs.preview_size.clamp(64, 8192)) }; if self.session.preview_size != p { let _ = materialcraft_engine::execute(&mut self.session, "view.previewSize", &serde_json::json!({"size": p.unwrap_or(0)})); @@ -173,6 +175,7 @@ impl MaterialCraftApp { } } } + if id.starts_with("file.") || id.starts_with("package.") || matches!(id, "graph.open" | "graph.delete" | "graph.duplicate" | "folder.remove" | "explorer.paste" | "edit.undo" | "edit.redo") @@ -218,6 +221,7 @@ impl MaterialCraftApp { { self.system_clipboard = Some(clip.to_string()); } + if matches!(id, "graph.pin.add" | "library.add") && let Some(materialcraft_engine::AnnotationSelection::Pin(pin)) = self.session.annotation_selection { @@ -226,6 +230,7 @@ impl MaterialCraftApp { self.inspected_graph = None; self.ui.dock.focus(docking::Panel::Properties); } + if id == "view.previewSize" { self.ui.prefs.preview_size = self.session.preview_size.unwrap_or(0); } @@ -235,6 +240,7 @@ impl MaterialCraftApp { if id == "session.history" { self.ui.prefs.history = self.session.history_prefs; } + self.close_subgraph_after(id); if id == "graph.node.function.edit" && let Ok(target) = serde_json::from_value::(v["target"].clone()) @@ -248,9 +254,11 @@ impl MaterialCraftApp { crate::graph::subgraph_navigation::open_function(self, node, target); } } + if matches!(id, "view.3d.assign" | "view.3d.outputs") { self.ui.dock.focus(docking::Panel::View3d); } + Some(v) } Err(e) => { @@ -393,6 +401,7 @@ impl MaterialCraftApp { ctx.request_repaint(); return; } + i18n::set_language(&self.ui.prefs.language); self.fonts.update(ctx, &self.ui.prefs.language, self.ui.windows.preferences); let now = ctx.input(|i| i.time); @@ -406,13 +415,16 @@ impl MaterialCraftApp { theme::apply(ctx, style.0, style.1); self.styled = Some(style); } + let scale = self.ui.prefs.ui_scale.clamp(0.5, 3.0); if (ctx.zoom_factor() - scale).abs() > 1e-3 && !ctx.input(|input| input.pointer.any_down()) { ctx.set_zoom_factor(scale); } + if ctx.input(|input| input.pointer.any_pressed()) { self.session.finish_edit(); } + crate::unsaved::handle_close(self, ctx); control::poll(self, ctx); web_files::poll(self); @@ -423,6 +435,7 @@ impl MaterialCraftApp { web_files::dropped(self, ctx, &f); } } + self.apply_prefs(); cook::drive(self, ctx); #[cfg(not(target_arch = "wasm32"))] @@ -452,8 +465,10 @@ impl MaterialCraftApp { self.fonts_ready = true; return; } + let ctx = ui.ctx().clone(); let t0 = now_ms(); + // Tab can focus a menu or toolbar button while drawing; preserve keyboard ownership self.graph.keyboard_was_focused = ctx.egui_wants_keyboard_input(); menus::menu_bar(self, ui); @@ -466,6 +481,7 @@ impl MaterialCraftApp { if self.window_layout.is_some() { crate::window_controls::resize_zones(ui); } + let gesture_active = ctx.input(|input| input.pointer.any_down()) || ctx.egui_wants_keyboard_input(); if !gesture_active && (self.ui_gesture_active || ctx.input(|input| input.pointer.any_released())) { self.session.finish_edit(); diff --git a/crates/ui-egui/src/preview_cache/preview_cache_limits.rs b/crates/ui-egui/src/preview_cache/preview_cache_limits.rs new file mode 100644 index 0000000..21242b4 --- /dev/null +++ b/crates/ui-egui/src/preview_cache/preview_cache_limits.rs @@ -0,0 +1,14 @@ +use materialcraft_engine::LibraryCachePrefs; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct PreviewCacheLimits { + pub budget_bytes: u64, + pub max_entries: usize, +} + +impl From for PreviewCacheLimits { + fn from(prefs: LibraryCachePrefs) -> Self { + let prefs = prefs.sanitized(); + Self { budget_bytes: u64::from(prefs.disk_mib) * 1024 * 1024, max_entries: prefs.max_previews as usize } + } +} diff --git a/crates/ui-egui/src/preview_cache/preview_cache_maintenance.rs b/crates/ui-egui/src/preview_cache/preview_cache_maintenance.rs new file mode 100644 index 0000000..cb888b7 --- /dev/null +++ b/crates/ui-egui/src/preview_cache/preview_cache_maintenance.rs @@ -0,0 +1,148 @@ +use std::sync::mpsc::{Receiver, TryRecvError}; +use std::time::{Duration, Instant}; + +use materialcraft_engine::LibraryCachePrefs; + +use super::{PreviewCacheSettings, PreviewCacheState}; + +const BUSY_RETRY_DELAY: Duration = Duration::from_millis(100); + +pub(crate) struct PreviewCacheMaintenance { + pub settings: PreviewCacheSettings, + pub(super) active: Option<(PreviewCacheState, Receiver>)>, + pub(super) applied: Option, + retry_at: Option, +} + +impl PreviewCacheMaintenance { + pub fn new(prefs: LibraryCachePrefs) -> Self { + Self { settings: PreviewCacheSettings::new(prefs), active: None, applied: None, retry_at: None } + } + + pub fn tick(&mut self, ctx: &egui::Context) -> Option { + let mut error = None; + if let Some((state, active)) = &self.active { + let state = *state; + 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(true) => { + self.applied = Some(state.revision); + self.settings.finish(state, false); + } + Ok(false) => self.retry_at = Some(Instant::now() + BUSY_RETRY_DELAY), + Err(reason) => { + self.applied = Some(state.revision); + self.settings.finish(state, 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 state = settings.get(); + 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 result = match super::super::preview_disk_cache::PreviewDiskCache::configured(settings) { + Some(cache) => cache.maintain(state), + None => Ok(true), + }; + + let _ = sender.send(result); + context.request_repaint(); + }) { + Ok(_) => self.active = Some((state, receiver)), + Err(reason) => { + self.applied = Some(state.revision); + self.settings.finish(state, true); + error = Some(format!("Library preview cache worker unavailable: {reason}")); + } + } + } + + error + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn stale_failure(clear: bool) { + let context = egui::Context::default(); + let mut maintenance = PreviewCacheMaintenance::new(LibraryCachePrefs::default()); + let previous = maintenance.settings.get(); + let (sender, receiver) = std::sync::mpsc::sync_channel(1); + maintenance.active = Some((previous, receiver)); + maintenance.retry_at = Some(Instant::now() + Duration::from_secs(60)); + maintenance.settings.update(LibraryCachePrefs { disk_mib: 128, max_previews: 8192 }.into(), clear); + sender.send(Err(std::io::Error::other("failure from the previous request"))).unwrap(); + + let error = maintenance.tick(&context); + + let latest = maintenance.settings.get(); + assert!(error.is_some()); + assert_eq!(latest.clear_pending, clear, "an old error must not consume a newer clear request"); + assert!(!latest.disabled, "an old error must not disable a newer configuration"); + assert_eq!(maintenance.applied, Some(previous.revision)); + assert!(maintenance.active.is_none(), "the retry delay keeps this test independent of disk workers"); + } + + #[test] + fn old_worker_failure_preserves_a_new_clear_request() { + stale_failure(true); + } + + #[test] + fn old_worker_failure_preserves_new_limits_without_a_clear() { + stale_failure(false); + } + + #[test] + fn rapid_changes_keep_one_maintenance_worker_and_coalesce_the_latest_request() { + let context = egui::Context::default(); + let mut maintenance = PreviewCacheMaintenance::new(LibraryCachePrefs::default()); + let previous = maintenance.settings.get(); + let (sender, receiver) = std::sync::mpsc::sync_channel(1); + maintenance.active = Some((previous, receiver)); + maintenance.retry_at = Some(Instant::now() + Duration::from_secs(60)); + + 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()); + } + + // Completing the old request leaves the latest clear queued + sender.send(Ok(true)).unwrap(); + assert!(maintenance.tick(&context).is_none()); + assert!(maintenance.active.is_none()); + assert_eq!(maintenance.applied, Some(previous.revision)); + assert!(maintenance.settings.get().clear_pending); + + let latest = maintenance.settings.get(); + let (sender, receiver) = std::sync::mpsc::sync_channel(1); + maintenance.active = Some((latest, receiver)); + sender.send(Ok(true)).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); + } +} diff --git a/crates/ui-egui/src/preview_cache/preview_cache_settings.rs b/crates/ui-egui/src/preview_cache/preview_cache_settings.rs new file mode 100644 index 0000000..d176482 --- /dev/null +++ b/crates/ui-egui/src/preview_cache/preview_cache_settings.rs @@ -0,0 +1,43 @@ +use std::sync::{Arc, Mutex}; + +use materialcraft_engine::LibraryCachePrefs; + +use super::{PreviewCacheLimits, PreviewCacheState}; + +#[derive(Clone)] +pub(crate) struct PreviewCacheSettings(Arc>); + +impl PreviewCacheSettings { + pub fn new(prefs: LibraryCachePrefs) -> Self { + Self(Arc::new(Mutex::new(PreviewCacheState { limits: prefs.into(), revision: 0, epoch: 0, clear_pending: false, disabled: false }))) + } + + pub fn get(&self) -> PreviewCacheState { + *self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) + } + + pub fn update(&self, limits: PreviewCacheLimits, 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, completed: PreviewCacheState, failed: bool) { + let mut state = self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + + // An old failure must not disable newer limits or consume a newer clear request + if state.epoch == completed.epoch && (!failed || state.revision == completed.revision) { + state.clear_pending = false; + + // Failed cleanup must bypass stale files and rebuild from source + state.disabled = failed; + } + } +} diff --git a/crates/ui-egui/src/preview_cache/preview_cache_state.rs b/crates/ui-egui/src/preview_cache/preview_cache_state.rs new file mode 100644 index 0000000..7706ec9 --- /dev/null +++ b/crates/ui-egui/src/preview_cache/preview_cache_state.rs @@ -0,0 +1,10 @@ +use super::PreviewCacheLimits; + +#[derive(Clone, Copy)] +pub(crate) struct PreviewCacheState { + pub limits: PreviewCacheLimits, + pub revision: u64, + pub epoch: u64, + pub clear_pending: bool, + pub disabled: bool, +} diff --git a/crates/ui-egui/src/preview_cache_settings.rs b/crates/ui-egui/src/preview_cache_settings.rs index 0b7bc9d..7df8978 100644 --- a/crates/ui-egui/src/preview_cache_settings.rs +++ b/crates/ui-egui/src/preview_cache_settings.rs @@ -1,171 +1,38 @@ //! 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, -} +#[path = "preview_cache/preview_cache_limits.rs"] +mod limits; +#[path = "preview_cache/preview_cache_maintenance.rs"] +mod maintenance; +#[path = "preview_cache/preview_cache_settings.rs"] +mod settings; +#[path = "preview_cache/preview_cache_state.rs"] +mod state; -impl From 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>); - -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>>>, - applied: Option, - retry_at: Option, -} - -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 { - 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 - } -} +pub(super) use limits::PreviewCacheLimits; +pub(super) use maintenance::PreviewCacheMaintenance; +pub(super) use settings::PreviewCacheSettings; +pub(super) use state::PreviewCacheState; #[cfg(test)] mod tests { use super::*; + use materialcraft_engine::LibraryCachePrefs; #[test] fn an_old_cleanup_cannot_finish_a_new_clear_request() { - let settings = Settings::new(LibraryCachePrefs::default()); + let settings = PreviewCacheSettings::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); + settings.finish(old, false); + assert!(settings.get().clear_pending); + let latest = settings.get(); - settings.finish(latest.epoch, true); + settings.finish(latest, 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); - } } diff --git a/crates/ui-egui/src/preview_disk_cache.rs b/crates/ui-egui/src/preview_disk_cache.rs index 578561d..e0830aa 100644 --- a/crates/ui-egui/src/preview_disk_cache.rs +++ b/crates/ui-egui/src/preview_disk_cache.rs @@ -10,29 +10,30 @@ use std::time::{Duration, SystemTime}; use image::{ImageDecoder, ImageEncoder}; use sha2::{Digest, Sha256}; -use super::preview_cache_settings::{Limits, Settings, State}; +use super::preview_cache_settings::{PreviewCacheLimits, PreviewCacheSettings, PreviewCacheState}; const MAX_SCAN_ENTRIES: usize = materialcraft_engine::LibraryCachePrefs::MAX_PREVIEWS as usize * 2; const MAX_FILE_BYTES: u64 = 128 * 1024; +const MAX_EXECUTABLE_BYTES: u64 = 1 << 30; +const MAX_DECODE_BYTES: u64 = 1024 * 1024; +const FILE_TOUCH_INTERVAL_SECS: u64 = 3600; const MAX_SIDE: u32 = 128; pub(super) struct PreviewDiskCache { root: PathBuf, renderer: [u8; 32], - settings: Settings, + settings: PreviewCacheSettings, epoch: u64, } impl PreviewDiskCache { - pub fn configured(settings: Settings) -> Option { - // 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. + pub fn configured(settings: PreviewCacheSettings) -> Option { + // Only an explicit override may enable persistence in scripted runs let root = match std::env::var_os("MATERIALCRAFT_CACHE_DIR") { Some(path) if path.is_empty() => return None, Some(path) => PathBuf::from(path), None if cfg!(test) || std::env::var_os("MATERIALCRAFT_NO_PREFS").is_some() => return None, - // The existing shared path resolver follows the suite's cache-folder policy, - // including redirected Windows profiles and absolute XDG paths. + // The shared resolver honors redirected Windows profiles and absolute XDG paths None => craft_update::Paths::per_user("materialcraft").ok()?.cache, }; Self::open(root.join("previews"), settings) @@ -40,13 +41,13 @@ impl PreviewDiskCache { /// 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, settings: Settings) -> Option { + pub fn open(root: PathBuf, settings: PreviewCacheSettings) -> Option { static RENDERER: OnceLock> = OnceLock::new(); let renderer = *RENDERER.get_or_init(|| { - let mut file = File::open(std::env::current_exe().ok()?).ok()?.take((1 << 30) + 1); + let mut file = File::open(std::env::current_exe().ok()?).ok()?.take(MAX_EXECUTABLE_BYTES + 1); let mut hash = Sha256::new(); let bytes = io::copy(&mut file, &mut hash).ok()?; - (bytes <= 1 << 30).then(|| hash.finalize().into()) + (bytes <= MAX_EXECUTABLE_BYTES).then(|| hash.finalize().into()) }); let epoch = settings.get().epoch; Some(Self { root, renderer: renderer?, settings, epoch }) @@ -56,18 +57,22 @@ impl PreviewDiskCache { if cancel.load(Ordering::Relaxed) { return None; } + let path = self.path(key, side); if let Some(pixels) = self.read(&path, side) { return (!cancel.load(Ordering::Relaxed)).then_some(pixels); } + let pixels = render()?; if cancel.load(Ordering::Relaxed) { return None; } - // A cache is optional: unavailable storage must not hide a successfully rendered tile. + + // Optional storage failure must not hide a rendered tile if let Err(error) = self.write(&path, side, &pixels) { log::debug!("Library preview persistence skipped: {error}"); } + Some(pixels) } @@ -81,125 +86,146 @@ impl PreviewDiskCache { } fn read(&self, path: &Path, side: u32) -> Option> { - if side == 0 || side > MAX_SIDE || self.limits().is_none_or(|limits| limits.budget == 0) { + if side == 0 || side > MAX_SIDE || self.limits().is_none_or(|limits| limits.budget_bytes == 0) { return None; } + let metadata = fs::symlink_metadata(path).ok()?; if !metadata.is_file() || metadata.len() > MAX_FILE_BYTES { return None; } + let mut file = File::open(path).ok()?; let mut bytes = Vec::new(); Read::by_ref(&mut file).take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes).ok()?; if bytes.len() as u64 > MAX_FILE_BYTES { return None; } + let mut limits = image::Limits::default(); limits.max_image_width = Some(MAX_SIDE); limits.max_image_height = Some(MAX_SIDE); - limits.max_alloc = Some(1024 * 1024); + limits.max_alloc = Some(MAX_DECODE_BYTES); let decoder = image::codecs::png::PngDecoder::with_limits(Cursor::new(bytes), limits).ok()?; if decoder.dimensions() != (side, side) || decoder.color_type() != image::ColorType::Rgba8 { return None; } + let mut pixels = vec![0; (side * side * 4) as usize]; decoder.read_image(&mut pixels).ok()?; - // Approximate LRU across launches, without a metadata write on every tile lookup. - if metadata.modified().ok()?.elapsed().is_ok_and(|age| age >= Duration::from_secs(3600)) { + + // Refresh file age at most once per hour to approximate LRU across launches + if metadata.modified().ok()?.elapsed().is_ok_and(|age| age >= Duration::from_secs(FILE_TOUCH_INTERVAL_SECS)) { let _ = file.set_modified(SystemTime::now()); } + Some(pixels) } 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.limits().is_none_or(|limits| limits.budget == 0) { + if side == 0 || side > MAX_SIDE || pixels.len() != (side * side * 4) as usize || self.limits().is_none_or(|limits| limits.budget_bytes == 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 { return Ok(()); } + fs::create_dir_all(&self.root)?; - // Coordinate the budget across windows/processes. A busy writer is a harmless cache - // miss; do not block a preview worker waiting for another process to finish. + + // A busy writer is a cache miss; preview workers must not wait on another process let lock = File::options().read(true).write(true).create(true).truncate(false).open(self.root.join("cache.lock"))?; lock.try_lock().map_err(io::Error::other)?; - // craft-atomic deliberately follows document symlinks. Cache entries are disposable - // files owned by this cache, so never follow one into an unrelated document instead. + + // Refuse symlinks because atomic writes otherwise follow them into unrelated files 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")); } + let Some(limits) = self.limits() else { return Ok(()) }; - if bytes.len() as u64 > limits.budget { + + if bytes.len() as u64 > limits.budget_bytes { 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) } pub fn enabled(&self) -> bool { - self.limits().is_some_and(|limits| limits.budget > 0) + self.limits().is_some_and(|limits| limits.budget_bytes > 0) } - fn limits(&self) -> Option { + fn limits(&self) -> Option { 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 { + pub fn maintain(&self, state: PreviewCacheState) -> io::Result { 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 }; + + let limits = if state.clear_pending || state.limits.budget_bytes == 0 { PreviewCacheLimits { budget_bytes: 0, max_entries: 0 } } else { state.limits }; self.reserve(None, 0, limits)?; Ok(true) } - fn reserve(&self, target: Option<&Path>, incoming: u64, limits: Limits) -> io::Result<()> { + fn reserve(&self, target: Option<&Path>, incoming: u64, limits: PreviewCacheLimits) -> io::Result<()> { let mut entries = Vec::new(); let mut total = 0u64; - // Bound both scan time and metadata allocations in an externally populated directory. + + // Bound scans and metadata allocations for externally populated directories for (index, entry) in fs::read_dir(&self.root)?.enumerate() { if index >= MAX_SCAN_ENTRIES { return Err(io::Error::other("preview cache directory exceeds its entry limit")); } + let entry = entry?; if !entry.file_type()?.is_file() { continue; } + let name = entry.file_name(); let name = name.to_string_lossy(); - // The cache lock excludes every live writer. Matching staging files can only be - // leftovers from an interrupted atomic save, so reclaim them before growing again. + + // The lock excludes live writers, so matching staging files are abandoned if staging_name(&name) { fs::remove_file(entry.path())?; continue; } + if !cache_name(&name) || target.is_some_and(|target| entry.path() == target) { continue; } + let metadata = entry.metadata()?; total = total.saturating_add(metadata.len()); entries.push((metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH), entry.path(), metadata.len())); } + 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) <= limits.budget && count.saturating_add(usize::from(target.is_some())) <= limits.entries { + if total.saturating_add(incoming) <= limits.budget_bytes && count.saturating_add(usize::from(target.is_some())) <= limits.max_entries { break; } + fs::remove_file(path)?; total = total.saturating_sub(size); count = count.saturating_sub(1); } + Ok(()) } } @@ -216,6 +242,7 @@ fn staging_name(name: &str) -> bool { let Some((stem, suffix)) = name.strip_prefix('.').and_then(|name| name.strip_suffix(".craft_tmp")).and_then(|name| name.split_once('.')) else { return false; }; + digest_name(stem) && suffix.split_once('-').is_some_and(|(pid, counter)| pid.parse::().is_ok_and(|pid| pid > 0) && counter.parse::().is_ok()) } diff --git a/crates/ui-egui/src/preview_disk_cache.test.rs b/crates/ui-egui/src/preview_disk_cache.test.rs index 658f2d2..269906e 100644 --- a/crates/ui-egui/src/preview_disk_cache.test.rs +++ b/crates/ui-egui/src/preview_disk_cache.test.rs @@ -1,12 +1,12 @@ use super::*; const SIDE: u32 = 8; -// A fork can briefly inherit another test's file lock before exec closes it. Keep this -// module's subprocess launches apart from its nonblocking-writer tests. + +// Keep subprocess launches separate from nonblocking lock tests static TESTS: std::sync::Mutex<()> = std::sync::Mutex::new(()); fn cache(root: &Path) -> PreviewDiskCache { - PreviewDiskCache { root: root.to_owned(), renderer: [1; 32], settings: Settings::new(Default::default()), epoch: 0 } + PreviewDiskCache { root: root.to_owned(), renderer: [1; 32], settings: PreviewCacheSettings::new(Default::default()), epoch: 0 } } fn pixels() -> Vec { @@ -21,11 +21,15 @@ fn previews_survive_process_restarts_without_rendering() { let cache = cache(Path::new(&root)); let actual = cache.get_or_render([2; 32], SIDE, &AtomicBool::new(false), || { assert_eq!(std::env::var("MATERIALCRAFT_TEST_DISK_PHASE").unwrap(), "cold", "warm launch must not cook"); + Some(pixels()) }); + assert_eq!(actual, Some(pixels())); + return; } + let root = tempfile::tempdir().unwrap(); for phase in ["cold", "warm"] { let start = std::time::Instant::now(); @@ -35,7 +39,9 @@ fn previews_survive_process_restarts_without_rendering() { .env("MATERIALCRAFT_TEST_DISK_PHASE", phase) .status() .unwrap(); + assert!(status.success()); + eprintln!("{phase} preview subprocess: {:?}", start.elapsed()); } } @@ -48,7 +54,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), Settings::new(Default::default())).unwrap(); + let cache = PreviewDiskCache::open(PathBuf::from(root), PreviewCacheSettings::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")] @@ -57,14 +63,18 @@ fn real_library_previews_survive_relaunch() { let key = library.preview_fingerprint(package, graph, &cancel).unwrap(); let image = cache.get_or_render(key, 72, &cancel, || { assert_eq!(phase, "cold", "warm launch must load {graph} without cooking"); + materialcraft_engine::NodeLibrary::preview_cancellable(&library, package, graph, 72, &cancel) .map(|image| image.resize(72, 72, true).to_rgba8_display()) }); + assert_eq!(image.unwrap().len(), 72 * 72 * 4); + eprintln!("{phase} {graph}: {:?}", started.elapsed()); } return; } + let root = tempfile::tempdir().unwrap(); for phase in ["cold", "warm"] { let status = std::process::Command::new(std::env::current_exe().unwrap()) @@ -73,6 +83,7 @@ fn real_library_previews_survive_relaunch() { .env("MATERIALCRAFT_TEST_DISK_PHASE", phase) .status() .unwrap(); + assert!(status.success()); } } @@ -83,9 +94,12 @@ fn keys_separate_sources_sizes_and_renderer_builds() { let root = tempfile::tempdir().unwrap(); let mut cache = cache(root.path()); let original = cache.path([1; 32], SIDE); + assert_ne!(original, cache.path([2; 32], SIDE)); assert_ne!(original, cache.path([1; 32], SIDE + 1)); + cache.renderer = [2; 32]; + assert_ne!(original, cache.path([1; 32], SIDE)); } @@ -95,8 +109,11 @@ fn cancelled_failed_and_invalid_previews_never_persist() { let root = tempfile::tempdir().unwrap(); let cache = cache(root.path()); let cancel = AtomicBool::new(true); + assert!(cache.get_or_render([1; 32], SIDE, &cancel, || panic!("cancelled cook ran")).is_none()); + cancel.store(false, Ordering::Relaxed); + assert!(cache.get_or_render([1; 32], SIDE, &cancel, || None).is_none()); assert!( cache @@ -106,7 +123,9 @@ fn cancelled_failed_and_invalid_previews_never_persist() { }) .is_none() ); + cache.write(&cache.path([2; 32], SIDE), SIDE, &[1]).unwrap(); + assert_eq!(fs::read_dir(root.path()).unwrap().count(), 0); } @@ -118,13 +137,16 @@ fn corrupt_oversized_and_wrong_size_files_are_misses_and_repaired() { let path = cache.path([1; 32], SIDE); for bytes in [b"broken PNG".to_vec(), vec![0; MAX_FILE_BYTES as usize + 1]] { fs::write(&path, bytes).unwrap(); + assert!(cache.read(&path, SIDE).is_none()); assert_eq!(cache.get_or_render([1; 32], SIDE, &AtomicBool::new(false), || Some(pixels())), Some(pixels())); assert_eq!(cache.read(&path, SIDE), Some(pixels())); assert!(cache.read(&path, SIDE + 1).is_none()); } + let bytes = fs::read(&path).unwrap(); fs::write(&path, &bytes[..bytes.len() / 2]).unwrap(); + assert!(cache.read(&path, SIDE).is_none()); } @@ -136,28 +158,37 @@ fn cache_budget_evicts_old_files_and_preserves_unrelated_files() { let old = cache.path([1; 32], SIDE); cache.write(&old, SIDE, &pixels()).unwrap(); let file_bytes = fs::metadata(&old).unwrap().len(); - cache.settings.update(Limits { budget: file_bytes * 2, entries: 4096 }, false); + cache.settings.update(PreviewCacheLimits { budget_bytes: file_bytes * 2, max_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())); fs::write(&leftover, b"interrupted write").unwrap(); fs::write(root.path().join("unrelated.craft_tmp"), b"keep").unwrap(); cache.write(&cache.path([2; 32], SIDE), SIDE, &pixels()).unwrap(); + assert!(!leftover.exists()); assert!(root.path().join("unrelated.craft_tmp").exists()); + cache.write(&cache.path([3; 32], SIDE), SIDE, &pixels()).unwrap(); + assert!(!old.exists()); 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.settings.get().limits.budget); - cache.settings.update(Limits { budget: 64 * 1024 * 1024, entries: 1 }, false); + + assert!(total <= cache.settings.get().limits.budget_bytes); + + cache.settings.update(PreviewCacheLimits { budget_bytes: 64 * 1024 * 1024, max_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.settings.update(Limits { budget: file_bytes - 1, entries: 1 }, false); + + cache.settings.update(PreviewCacheLimits { budget_bytes: file_bytes - 1, max_entries: 1 }, false); let oversized = cache.path([5; 32], SIDE); cache.write(&oversized, SIDE, &pixels()).unwrap(); + assert!(!oversized.exists()); } @@ -167,10 +198,13 @@ fn unavailable_or_busy_cache_still_returns_rendered_pixels() { let root = tempfile::tempdir().unwrap(); let blocked = root.path().join("file"); fs::write(&blocked, b"not a directory").unwrap(); + assert_eq!(cache(&blocked).get_or_render([1; 32], SIDE, &AtomicBool::new(false), || Some(pixels())), Some(pixels())); + let lock = File::options().read(true).write(true).create_new(true).open(root.path().join("cache.lock")).unwrap(); lock.lock().unwrap(); let cache = cache(root.path()); + assert_eq!(cache.get_or_render([1; 32], SIDE, &AtomicBool::new(false), || Some(pixels())), Some(pixels())); assert!(!cache.path([1; 32], SIDE).exists()); } @@ -185,8 +219,11 @@ fn cache_does_not_read_or_evict_symlink_targets() { let cache = cache(root.path()); let path = cache.path([1; 32], SIDE); std::os::unix::fs::symlink(outside.path(), &path).unwrap(); + assert!(cache.read(&path, SIDE).is_none()); + let _ = cache.write(&path, SIDE, &pixels()); + assert_eq!(fs::read(outside.path()).unwrap(), b"keep"); } @@ -198,15 +235,21 @@ fn changed_limits_trim_a_warm_cache_without_another_render() { 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); + cache.settings.update(PreviewCacheLimits { budget_bytes: 64 * 1024 * 1024, max_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); + cache.settings.update(PreviewCacheLimits { budget_bytes: file_bytes, max_entries: 4096 }, false); + assert!(cache.maintain(cache.settings.get()).unwrap()); assert_eq!(count(), 1); - cache.settings.update(Limits { budget: 0, entries: 4096 }, false); + + cache.settings.update(PreviewCacheLimits { budget_bytes: 0, max_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())); @@ -223,15 +266,22 @@ fn clear_removes_previews_and_prevents_old_workers_from_repopulating_them() { 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); + + cache.settings.finish(state, 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"); } @@ -240,9 +290,10 @@ 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); + cache.settings.update(PreviewCacheLimits { budget_bytes: 0, max_entries: 4096 }, false); Some(pixels()) }), Some(pixels()) diff --git a/crates/ui-egui/src/state/prefs.rs b/crates/ui-egui/src/state/prefs.rs index cc86ce8..518a894 100644 --- a/crates/ui-egui/src/state/prefs.rs +++ b/crates/ui-egui/src/state/prefs.rs @@ -57,18 +57,22 @@ mod tests { fn preferences_persist_and_default_when_missing() { // Missing preferences use their defaults let old: Prefs = serde_json::from_value(serde_json::json!({"uiScale": 1.25})).unwrap(); + assert_eq!(old.camera, CameraPrefs::default()); assert_eq!(old.ui_scale, 1.25); assert!(old.snap_to_grid); assert_eq!(old.history, HistoryPrefs::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})); diff --git a/crates/ui-egui/src/thumbs.rs b/crates/ui-egui/src/thumbs.rs index c0e8aab..c14900a 100644 --- a/crates/ui-egui/src/thumbs.rs +++ b/crates/ui-egui/src/thumbs.rs @@ -10,13 +10,15 @@ pub type Previews = Vec<(String, Vec)>; /// Side of node thumbnails and library previews, in pixels pub const THUMB: u32 = 96; const LIBRARY_PREVIEW_SIZE: u32 = 72; +const NODE_TEXTURE_TRIM_THRESHOLD: usize = 256; +const NODE_TEXTURE_IDLE_FRAMES: u64 = 120; #[derive(Default)] pub struct Thumbs { /// (cook key, port) → texture and the frame it was last used nodes: HashMap<(u64, usize), (TextureHandle, u64)>, #[cfg(not(target_arch = "wasm32"))] - persistence: Option, + persistence: Option, clear_epoch: u64, graphs: GraphPreviews, frame: u64, @@ -42,7 +44,7 @@ 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, prefs: materialcraft_engine::LibraryCachePrefs) { - let persistence = preview_cache_settings::Maintenance::new(prefs); + let persistence = preview_cache_settings::PreviewCacheMaintenance::new(prefs); self.graphs.persistence = Some(persistence.settings.clone()); self.persistence = Some(persistence); } @@ -54,6 +56,7 @@ impl Thumbs { if let Some(persistence) = &self.persistence { persistence.settings.update(_prefs.into(), clear); } + if clear { self.graphs.clear(); } @@ -66,23 +69,25 @@ impl Thumbs { /// The thumbnail of a node output, uploaded on first use pub fn node(&mut self, ctx: &egui::Context, key: u64, port: usize, img: &Image) -> egui::TextureId { let frame = self.frame; - if let Some((t, f)) = self.nodes.get_mut(&(key, port)) { - *f = frame; - return t.id(); + if let Some((texture, last_used_frame)) = self.nodes.get_mut(&(key, port)) { + *last_used_frame = frame; + return texture.id(); } - let tex = ctx.load_texture(format!("node-{key}-{port}"), color_image(img, THUMB), TextureOptions::LINEAR); - let id = tex.id(); - self.nodes.insert((key, port), (tex, frame)); + + let texture = ctx.load_texture(format!("node-{key}-{port}"), color_image(img, THUMB), TextureOptions::LINEAR); + let id = texture.id(); + self.nodes.insert((key, port), (texture, frame)); id } /// 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, foreground_busy: bool) -> Option { self.frame += 1; - let f = self.frame; - if self.nodes.len() > 256 { - self.nodes.retain(|_, (_, used)| f - *used < 120); + let frame = self.frame; + if self.nodes.len() > NODE_TEXTURE_TRIM_THRESHOLD { + self.nodes.retain(|_, (_, used)| frame - *used < NODE_TEXTURE_IDLE_FRAMES); } + #[cfg(not(target_arch = "wasm32"))] let error = self.persistence.as_mut().and_then(|persistence| persistence.tick(ctx)); #[cfg(target_arch = "wasm32")] @@ -109,41 +114,47 @@ impl Thumbs { } } -/// Sample inputs for previews: a wave height, a colored version and its normal map -fn sample_inputs() -> (Image, Image, Image) { - let gray = Image::generate_uv(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, Kind::Gray, |u, v| { +/// Wave height used by native node previews +fn sample_height() -> Image { + Image::generate_uv(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, Kind::Gray, |u, v| { [0.5 + 0.25 * (u * std::f32::consts::TAU).sin() + 0.25 * (v * std::f32::consts::TAU).cos(), 0.0, 0.0, 0.0] - }); - let color = gray.map(Kind::Color, |p| [0.15 + 0.7 * p[0], 0.35 + 0.3 * p[0], 0.8 - 0.5 * p[0], 1.0]); - let normal = materialcraft_engine::nodes::atomic::height_to_normal(&gray, 4.0, false, true, false); - (gray, color, normal) + }) } fn render_native(id: &str) -> Option> { + // Strong relief keeps normal inputs legible in small previews + const NORMAL_INTENSITY: f32 = 4.0; + 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 gray = (!def.inputs.is_empty()).then(sample_height); + let mut color = None; + let mut normal = None; + let no_resource = |_: &str| None; + let variables = BTreeMap::new(); let inputs: Vec> = 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") { + .map(|(index, port)| { + // Masks and secondary inputs stay empty so the main effect shows + if index > 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 { + + let gray = gray.as_ref()?; + Some(if port.id.contains("normal") || def.category.contains("Normal") && port.kind == materialcraft_engine::nodes::PortKind::Color { + normal.get_or_insert_with(|| materialcraft_engine::nodes::atomic::height_to_normal(gray, NORMAL_INTENSITY, false, true, false)).clone() + } else if port.kind == materialcraft_engine::nodes::PortKind::Color { + // A blue-to-sand palette distinguishes color inputs from the grayscale height + color.get_or_insert_with(|| gray.map(Kind::Color, |pixel| [0.15 + 0.7 * pixel[0], 0.35 + 0.3 * pixel[0], 0.8 - 0.5 * pixel[0], 1.0])).clone() + } else if index == 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 { format: materialcraft_engine::core::OutputFormat::U16, @@ -153,9 +164,10 @@ fn render_native(id: &str) -> Option> { tile: true, inputs: &inputs, params: ¶ms, - resource: &no_res, - vars: &vars, + resource: &no_resource, + vars: &variables, }; - let imgs = (def.cook)(&ctx).ok()?; - Some(imgs.first()?.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display()) + + let images = (def.cook)(&ctx).ok()?; + Some(images.first()?.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display()) } diff --git a/crates/ui-egui/src/value_editor.rs b/crates/ui-egui/src/value_editor.rs index bf505cc..5f831b3 100644 --- a/crates/ui-egui/src/value_editor.rs +++ b/crates/ui-egui/src/value_editor.rs @@ -74,6 +74,7 @@ fn floats(ui: &mut Ui, values: &mut [f32], labels: &[&str], stacked: bool) -> bo for (value, label) in values.iter_mut().zip(labels) { changed |= float(ui, value, label, stacked); } + changed } @@ -86,5 +87,6 @@ fn integers(ui: &mut Ui, values: &mut [i32], stacked: bool) -> bool { for (value, label) in values.iter_mut().zip(["X", "Y", "Z", "W"]) { changed |= integer(ui, value, label, stacked); } + changed } diff --git a/crates/ui-egui/src/view2d.rs b/crates/ui-egui/src/view2d.rs index 33d4c11..7971ca0 100644 --- a/crates/ui-egui/src/view2d.rs +++ b/crates/ui-egui/src/view2d.rs @@ -24,11 +24,12 @@ const BINS: usize = 64; /// Histograms of the displayed 8-bit values: one for gray or isolated channels, else R, G and B. fn histogram(px: &[u8], channels: usize) -> Vec<[u32; BINS]> { let mut h = vec![[0u32; BINS]; channels]; - for p in px.chunks_exact(4) { + for p in px.as_chunks::<4>().0 { for (c, bins) in h.iter_mut().enumerate() { bins[p[c] as usize * BINS / 256] += 1; } } + h } @@ -55,7 +56,8 @@ fn rgba8(img: &Image, channel: &str) -> Vec { pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { let tk = Tokens::get(ui.ctx()); let ctx = ui.ctx().clone(); - // Toolbar. + + // Toolbar egui::Frame::NONE.fill(tk.panel).inner_margin(egui::Margin::symmetric(6, 3)).show(ui, |ui| { ui.horizontal(|ui| { crate::preview_resolution::show(app, ui); @@ -92,6 +94,7 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { ui.painter().text(r.center(), egui::Align2::CENTER_CENTER, tr("Click a node to show it here"), egui::FontId::proportional(12.0), tk.viewport_text); return; }; + let texture_limit = ctx.input(|i| i.max_texture_side).max(1); let key = app.session.cooked.nodes.get(&id).map_or(0, |r| r.key) ^ (texture_limit as u64).rotate_left(32) @@ -102,7 +105,8 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { ^ app.session.view2d.channel.bytes().next().unwrap_or(0) as u64; if app.view2d.tex.as_ref().is_none_or(|(k, _)| *k != key) { let px = rgba8(&img, &app.session.view2d.channel); - // Keep source pixels for readout/screenshots, but respect the renderer's upload limit. + + // Keep source pixels for readout/screenshots, but respect the renderer's upload limit let ci = if img.w.max(img.h) as usize > texture_limit { let scale = texture_limit as f64 / f64::from(img.w.max(img.h)); let display = img.resize((f64::from(img.w) * scale).floor().max(1.0) as u32, (f64::from(img.h) * scale).floor().max(1.0) as u32, false); @@ -110,15 +114,18 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { } else { ColorImage::from_rgba_unmultiplied([img.w as usize, img.h as usize], &px) }; + app.view2d.tex = Some((key, ctx.load_texture("view2d", ci, TextureOptions::NEAREST))); app.view2d.shown = Some((img.w, img.h, px)); } + let avail = ui.available_size(); let (rect, resp) = ui.allocate_exact_size(vec2(avail.x, (avail.y - 20.0).max(40.0)), Sense::click_and_drag()); crate::widgets::name(&resp, egui::Role::Image, "2D view"); let p = ui.painter_at(rect); p.rect_filled(rect, 0.0, tk.viewport); - // Camera: fit, wheel zoom around the pointer, drag to pan. + + // Camera: fit, wheel zoom around the pointer, drag to pan app.ui.image_camera = app.ui.image_camera.sanitized(); let cam = &mut app.ui.image_camera; let tiles = if app.session.view2d.tiled { 3.0 } else { 1.0 }; @@ -127,6 +134,7 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { cam.zoom = fit_zoom; cam.offset = [0.0, 0.0]; } + if resp.hovered() { let scroll = ui.input(|i| i.smooth_scroll_delta.y); if scroll.abs() > 0.0 { @@ -141,15 +149,18 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { } } } + if resp.dragged() { cam.fit = false; let d = resp.drag_delta(); cam.offset = [cam.offset[0] + d.x, cam.offset[1] + d.y]; } + let size = vec2(img.w as f32, img.h as f32) * cam.zoom; let center = rect.center() + vec2(cam.offset[0], cam.offset[1]); let full = Rect::from_center_size(center, size * tiles); - // A checker behind transparent images. + + // A checker behind transparent images if img.kind == Kind::Color && app.session.view2d.channel == "rgba" { let cell = 8.0; let vis = full.intersect(rect); @@ -165,6 +176,7 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { row += 1; } } + if let Some((_, tex)) = &app.view2d.tex { let uv = Rect::from_min_max(pos2(0.0, 0.0), pos2(tiles, tiles)); let mut mesh = egui::Mesh::with_texture(tex.id()); @@ -174,11 +186,13 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { p.rect_stroke(Rect::from_center_size(center, size), 0.0, egui::Stroke::new(1.0, Color32::from_white_alpha(60)), egui::StrokeKind::Outside); } } + if app.session.view2d.histogram { let single = img.is_gray() || app.session.view2d.channel != "rgba"; draw_histogram(&mut app.view2d, &p, rect, key, single); } - // Info line: size, format, zoom and the pixel under the pointer. + + // Info line: size, format, zoom and the pixel under the pointer let info = Rect::from_min_size(pos2(rect.left(), rect.bottom()), vec2(rect.width(), 20.0)); let bits = app.session.cooked.nodes.get(&id).map_or("16", |result| result.base.format.bits()); let mut text = format!("{} x {} ({}, {}bpc) {:.0}%", img.w, img.h, if img.is_gray() { "L" } else { "RGBA" }, bits, cam.zoom * 100.0); diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index bab31cb..3b8b203 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -79,6 +79,7 @@ pub fn show(app: &mut MaterialCraftApp, ctx: &egui::Context) { 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); } @@ -97,6 +98,7 @@ pub fn show(app: &mut MaterialCraftApp, ctx: &egui::Context) { if app.ui.windows.home { home(app, ctx); } + crate::unsaved::show(app, ctx); crate::explorer::dialogs(app, ctx); crate::properties::ExposeParameterDialog::show(app, ctx); @@ -235,6 +237,7 @@ fn preferences(app: &mut MaterialCraftApp, ctx: &egui::Context) { app.run("library.cache.clear", serde_json::json!({})); app.apply_prefs(); } + if !open { app.ui.windows.preferences = false; } diff --git a/crates/ui-egui/tests/it/node_look.rs b/crates/ui-egui/tests/it/node_look.rs index 6f71983..2dab4a6 100644 --- a/crates/ui-egui/tests/it/node_look.rs +++ b/crates/ui-egui/tests/it/node_look.rs @@ -32,7 +32,9 @@ fn divider(harness: &Harness<'static, MaterialCraftApp>) -> Pos2 { #[test] fn the_library_divider_drags_the_tree_narrower_and_a_double_click_resets_it() { let mut harness = harness(MaterialCraftApp::headless(materialcraft_engine::new_session(), UiState::default())); + assert_eq!(harness.state().ui.library_tree_width, 0.0, "the tree starts at its automatic width"); + let start = divider(&harness); harness.event(Event::PointerMoved(start)); @@ -46,8 +48,11 @@ fn the_library_divider_drags_the_tree_narrower_and_a_double_click_resets_it() { harness.run_steps(3); let dragged = harness.state().ui.library_tree_width; + assert!(dragged > 0.0, "a drag chooses a width"); + let moved = start.x - divider(&harness).x; + assert!((40.0..=62.0).contains(&moved), "the divider follows the pointer, moved {moved}"); let at = divider(&harness); @@ -57,6 +62,7 @@ fn the_library_divider_drags_the_tree_narrower_and_a_double_click_resets_it() { press(&mut harness, at, pressed); } harness.run_steps(3); + assert_eq!(harness.state().ui.library_tree_width, 0.0, "a double click returns to the automatic width"); assert!((divider(&harness).x - start.x).abs() < 1.0); } @@ -72,10 +78,12 @@ fn a_constant_node_fits_a_field_for_each_component() { let card = harness.get(By::new().role(Role::Button).label("Float4")).rect(); for component in ["X", "Y", "Z", "W"] { let field = harness.get(By::new().role(Role::SpinButton).label(component)).rect(); + assert!(card.contains_rect(field), "{component} lies inside its node: {field:?} in {card:?}"); } let operator = harness.get(By::new().role(Role::Button).label("Add")).rect(); + assert!(operator.height() < card.height(), "an operator is a compact node"); assert!(operator.height() < operator.width(), "an operator has no empty preview square"); } diff --git a/crates/ui-egui/tests/snapshot.rs b/crates/ui-egui/tests/snapshot.rs index 93f5d7d..93dfd96 100644 --- a/crates/ui-egui/tests/snapshot.rs +++ b/crates/ui-egui/tests/snapshot.rs @@ -84,6 +84,7 @@ fn save(h: &mut Harness<'static, MaterialCraftApp>, dir: &std::path::Path, name: fn assert_node_menu_order(harness: &Harness<'static, MaterialCraftApp>, labels: &[&str]) { let button = |label| By::new().role(egui::accesskit::Role::Button).label(label); + // Canvas cards share these labels; compare the buttons within the submenu's own parent let menu = harness .get_all(button(labels[0])) @@ -151,44 +152,60 @@ fn explorer_drags_a_linked_graph_between_open_packages() { } harness.event(egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); settle(&mut harness, 8); + assert_eq!(harness.state().session.document_id, target_document); assert_eq!(harness.state().session.graph().unwrap().nodes.len(), 1); + let instance = harness.state().session.graph().unwrap().nodes[0].id; + assert_eq!(harness.state().session.graph().unwrap().node(instance).unwrap().instance.as_ref().unwrap().graph, "Reusable tint"); assert!(harness.state().session.cooked.nodes[&instance].error.is_none()); + save(&mut harness, &directory, "linked-instance-explorer-drop"); let position = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Reusable tint")).rect().center(); double_click(&mut harness, position); + assert_eq!(harness.state().session.document_id, 0); + harness.state_mut().run("graph.select", json!({"ids": [color.0]})).unwrap(); harness.state_mut().run("graph.node.param", json!({"id": color.0, "param": "color", "value": [0.9, 0.3, 0.1, 1.0]})).unwrap(); let position = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Target canvas")).rect().center(); double_click(&mut harness, position); settle(&mut harness, 8); + assert_eq!(harness.state().session.document_id, target_document); assert_eq!(harness.state().session.history(), ["Add graph instance"]); + let value = harness.state().session.cooked.image(instance, 0).unwrap().get(0, 0, false); + assert!(value.into_iter().zip([0.9, 0.3, 0.1, 1.0]).all(|(actual, expected)| (actual - expected).abs() <= 1.0 / 255.0)); assert!(harness.state().session.modified); + save(&mut harness, &directory, "linked-instance-source-edit"); let position = harness.get(By::new().role(egui::accesskit::Role::Button).label("Reusable tint")).rect().center(); double_click(&mut harness, position); + assert_eq!(harness.state().session.document_id, target_document, "double-click must not open the instance source"); assert_eq!(harness.state().session.graph().unwrap().name, "Target canvas"); assert_eq!(harness.state().session.history(), ["Add graph instance"]); + harness.key_press_modifiers(egui::Modifiers::CTRL | egui::Modifiers::COMMAND, egui::Key::E); settle(&mut harness, 3); + assert_eq!(harness.state().session.document_id, target_document, "Ctrl+E must not open the instance source"); assert_eq!(harness.state().session.graph().unwrap().name, "Target canvas"); assert_eq!(harness.state().session.history(), ["Add graph instance"]); + harness.get(By::new().role(egui::accesskit::Role::Button).label("Reusable tint")).click_secondary(); settle(&mut harness, 2); for label in ["View output in 2D view", "Copy info to clipboard", "Open reference", "Create graph from selection (1)", "Duplicate selection Ctrl+D"] { assert!(harness.query(By::new().label(label)).is_some(), "missing instance action: {label}"); } + save(&mut harness, &directory, "linked-instance-context-menu"); harness.get(By::new().label("Open reference")).click(); settle(&mut harness, 4); + assert_eq!(harness.state().session.document_id, 0); assert_eq!(harness.state().session.graph().unwrap().name, "Reusable tint"); } @@ -600,8 +617,10 @@ fn output_menu_and_package_warnings_follow_parameter_types() { let context = harness.ctx.clone(); materialcraft_ui_egui::menus::invoke(harness.state_mut(), &context, "graph.node.edit"); settle(&mut harness, 3); + assert!(harness.query(By::new().label("Expected output: Float")).is_some()); assert!(harness.query(By::new().label(&package_warning_label)).is_some()); + let node = harness.get(By::new().role(egui::accesskit::Role::Button).label("Float3")).rect(); let header = egui::pos2(node.center().x, node.min.y + 12.0); harness.event(egui::Event::PointerMoved(header)); @@ -611,7 +630,9 @@ fn output_menu_and_package_warnings_follow_parameter_types() { harness.step(); } settle(&mut harness, 3); + assert!(harness.get(By::new().role(egui::accesskit::Role::Button).label("Set as output node")).accesskit_node().is_disabled()); + let directory = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").unwrap()); std::fs::create_dir_all(&directory).unwrap(); save(&mut harness, &directory, "incompatible-function-output"); @@ -619,15 +640,20 @@ fn output_menu_and_package_warnings_follow_parameter_types() { settle(&mut harness, 3); harness.state_mut().run("graph.function.edit", json!({"target": target, "edit": {"action": "output", "id": scalar}})).unwrap(); settle(&mut harness, 3); + assert!(harness.query(By::new().label(&package_warning_label)).is_none()); + harness.state_mut().run("graph.node.param", json!({"id": owner.0, "param": "color_mode", "value": true})).unwrap(); settle(&mut harness, 3); + assert!(harness.query(By::new().label("Expected output: Float4")).is_some()); assert!(harness.query(By::new().label_contains("Output node returns Float; expected Float4")).is_some()); assert!(harness.query(By::new().label(&package_warning_label)).is_some()); + save(&mut harness, &directory, "changed-function-output-type"); harness.state_mut().run("edit.undo", json!({})).unwrap(); settle(&mut harness, 3); + assert!(harness.query(By::new().label(&package_warning_label)).is_none()); } @@ -646,35 +672,49 @@ fn explorer_disclosure_tracks_input_and_output_entries() { let name = &harness.state().session.graph().unwrap().name; let row = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label(name)).rect(); let image = harness.render().unwrap(); + // The disclosure gutter contains a light triangle only when the graph has children ((row.left() as u32 - 16)..(row.left() as u32 - 2)) .any(|x| ((row.top() as u32 + 3)..(row.bottom() as u32 - 3)).any(|y| image.get_pixel(x, y).0[..3].iter().all(|channel| *channel > 150))) }; + assert!(!arrow_visible(&mut harness)); + save(&mut harness, &directory, "explorer-no-inputs-or-outputs"); for definition in ["input_color", "input_grayscale", "input_value", "output"] { harness.state_mut().run("graph.node.add", json!({"def": definition})).unwrap(); settle(&mut harness, 3); + assert!(arrow_visible(&mut harness), "{definition}"); + if definition == "output" { save(&mut harness, &directory, "explorer-with-output"); } + harness.state_mut().run("edit.delete", json!({})).unwrap(); settle(&mut harness, 3); + assert!(!arrow_visible(&mut harness), "{definition}"); + harness.state_mut().run("edit.undo", json!({})).unwrap(); settle(&mut harness, 3); + assert!(arrow_visible(&mut harness), "{definition}"); + harness.state_mut().run("edit.redo", json!({})).unwrap(); settle(&mut harness, 3); + assert!(!arrow_visible(&mut harness), "{definition}"); } + harness.state_mut().run("file.new", json!({"kind": "function", "name": "Function leaf"})).unwrap(); let function = materialcraft_engine::core::FunctionGraph::constant(materialcraft_engine::core::Value::Float(0.5)); harness.state_mut().run("graph.function.set", json!({"function": function})).unwrap(); settle(&mut harness, 3); + assert!(!arrow_visible(&mut harness)); + save(&mut harness, &directory, "explorer-function-leaf"); } @@ -689,12 +729,15 @@ fn explorer_open_targets_the_clicked_graph() { settle(&mut h, 5); h.get_by_label("Export target").click_secondary(); settle(&mut h, 3); + // Explorer menu items name their shortcut after the label let open = format!("Open {}", materialcraft_ui_egui::menus::shortcut_label(&h.ctx, "Enter")); h.get(By::new().role(egui::accesskit::Role::Button).label(&open)).click(); settle(&mut h, 3); + assert_eq!(h.state().session.graph, 1); assert!(!h.state().ui.windows.export); + 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, "explorer-open"); @@ -738,10 +781,12 @@ fn snapshots() { eprintln!("set MC_SNAPSHOT_DIR"); return; }; + std::fs::create_dir_all(&dir).unwrap(); let app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default()); let mut h = harness(app, 1600.0, 960.0); settle(&mut h, 40); + // Select a node so the properties panel shows parameters let id = h.state().session.graph().unwrap().nodes[0].id; h.state_mut().run("graph.select", json!({"ids": [id.0]})); @@ -1474,7 +1519,9 @@ fn material_assignment_previews_outputs_and_survives_function_editing() { .unwrap(); let mut harness = harness(app, 1600.0, 960.0); let before = wait_for_3d_frame(&mut harness, None); + assert!(harness.state().session.material_maps().is_empty()); + let directory = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").unwrap()); std::fs::create_dir_all(&directory).unwrap(); save(&mut harness, &directory, "material-unassigned"); @@ -1487,24 +1534,33 @@ fn material_assignment_previews_outputs_and_survives_function_editing() { save(&mut harness, &directory, "material-channel-menu"); harness.get(By::new().role(egui::accesskit::Role::Button).label("Base color")).click(); let red_frame = wait_for_3d_frame(&mut harness, Some(&before.2)); + assert_eq!(harness.state().session.selection, selection, "channel menu clicks must not reach the graph beneath them"); assert_eq!(harness.state().session.material_maps()[0].0, "baseColor"); - let red_pixels = red_frame.2.chunks_exact(4).filter(|pixel| u16::from(pixel[0]) > u16::from(pixel[1]) + 30).count(); + assert_ne!(before.2, red_frame.2, "assignment must invalidate the rendered frame"); + + let red_pixels = red_frame.2.as_chunks::<4>().0.iter().filter(|pixel| u16::from(pixel[0]) > u16::from(pixel[1]) + 30).count(); + assert!(red_pixels > 100, "the rendered mesh must show the assigned red image"); + save(&mut harness, &directory, "material-assigned-red"); harness.state_mut().run("graph.node.param", json!({"id": source, "param": "color", "value": [0.02, 0.04, 0.8, 1.0]})).unwrap(); let blue_frame = wait_for_3d_frame(&mut harness, Some(&red_frame.2)); - let blue_pixels = blue_frame.2.chunks_exact(4).filter(|pixel| u16::from(pixel[2]) > u16::from(pixel[0]) + 30).count(); + let blue_pixels = blue_frame.2.as_chunks::<4>().0.iter().filter(|pixel| u16::from(pixel[2]) > u16::from(pixel[0]) + 30).count(); + assert!(blue_pixels > 100, "edits must refresh the assigned material"); + save(&mut harness, &directory, "material-assigned-blue"); harness.state_mut().run("file.new", json!({"kind": "function", "name": "Function preview"})).unwrap(); settle(&mut harness, 45); let maps = harness.state().session.material_maps(); let color = maps[0].1.get(0, 0, false); + assert!(color.into_iter().zip([0.02, 0.04, 0.8, 1.0]).all(|(actual, expected)| (actual - expected).abs() <= 1.0 / 255.0)); assert_eq!(harness.state().session.material_preview.channels[&materialcraft_engine::core::MaterialChannel::BaseColor].node, NodeId(output)); + save(&mut harness, &directory, "material-retained-in-function"); } @@ -1539,26 +1595,36 @@ fn explorer_inputs_and_graph_drop_share_labels_and_material_assignment() { let graph_row = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Atomic_sample")).rect(); for (id, label) in inputs { let row = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label(label)); + assert!(row.rect().left() > graph_row.left()); + let position = row.rect().center(); double_click(&mut harness, position); + assert_eq!(harness.state().session.selection, vec![id]); assert_eq!(harness.get(By::new().role(egui::accesskit::Role::TextInput).label("Node label")).value().as_deref(), Some(label)); assert!(harness.query_all(By::new().label("Identifier")).next().is_some()); + let before = harness.state().session.graph().unwrap().node(id).unwrap().clone(); let default = &materialcraft_engine::nodes::find(&before.def).unwrap().param("usage").unwrap().default; + assert_eq!(before.params.get("usage").unwrap_or(default), &materialcraft_engine::core::Value::Enum(ComponentUsage::ANY_INDEX)); + harness.get(By::new().role(egui::accesskit::Role::ComboBox).label("Component Usage")).click(); settle(&mut harness, 2); harness.get_by_label("Base color").scroll_to_me(); settle(&mut harness, 2); harness.get_by_label("Base color").click(); settle(&mut harness, 2); + assert_eq!(harness.state().session.graph().unwrap().node(id).unwrap().params.get("usage"), Some(&materialcraft_engine::core::Value::Enum(0))); + save(&mut harness, &directory, &format!("component-usage-{}", before.def)); harness.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(harness.state().session.graph().unwrap().node(id).unwrap(), &before); } + let position = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Base Color")).rect().center(); double_click(&mut harness, position); harness.get(By::new().role(egui::accesskit::Role::ComboBox).label("Component Usage")).click(); @@ -1568,7 +1634,9 @@ fn explorer_inputs_and_graph_drop_share_labels_and_material_assignment() { harness.get_by_label("Glossiness").click(); settle(&mut harness, 3); let node = harness.state().session.graph().unwrap().node(NodeId(output)).unwrap(); + assert_eq!(materialcraft_engine::eval::output_usage(node), "glossiness"); + harness.state_mut().run("graph.node.param", json!({"id": output, "param": "usage", "value": ComponentUsage::index("diffuse")})).unwrap(); settle(&mut harness, 3); @@ -1589,16 +1657,23 @@ fn explorer_inputs_and_graph_drop_share_labels_and_material_assignment() { harness.event(egui::Event::PointerMoved(position)); harness.step(); } + save(&mut harness, &directory, "graph-drop-target"); harness.event(egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); settle(&mut harness, 45); + assert_eq!(harness.state().session.graph, graph, "dropping a graph must not open it"); assert_eq!(harness.state().session.view3d, camera, "dropping must not orbit the camera"); assert_eq!(harness.state().session.pkg, package); + let binding = &harness.state().session.material_preview.channels[&MaterialChannel::BaseColor]; + assert_eq!((binding.graph, binding.node), (0, NodeId(output))); + let frame = &harness.state().view3d.shown.as_ref().unwrap().2; - assert!(frame.chunks_exact(4).filter(|pixel| u16::from(pixel[0]) > u16::from(pixel[1]) + 30).count() > 100); + + assert!(frame.as_chunks::<4>().0.iter().filter(|pixel| u16::from(pixel[0]) > u16::from(pixel[1]) + 30).count() > 100); + save(&mut harness, &directory, "graph-dropped-on-material"); } @@ -1610,11 +1685,15 @@ fn library_cache_preferences() { 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"); diff --git a/crates/ui-egui/tests/snapshot/function_library.rs b/crates/ui-egui/tests/snapshot/function_library.rs index 98b6366..b4aebd7 100644 --- a/crates/ui-egui/tests/snapshot/function_library.rs +++ b/crates/ui-egui/tests/snapshot/function_library.rs @@ -41,11 +41,15 @@ fn bottom_library_drags_functions_into_function_and_processor_canvases() { drag(&mut harness, "Pi", vec2(-140.0, 0.0)); let function = target.current(&harness.state().session).unwrap(); + assert_eq!(function.nodes.len(), previous + 1, "{kind}"); + let node = function.nodes.iter().find(|node| node.instance.is_some()).unwrap(); + assert_eq!(node.instance.as_ref().unwrap().package, "sbs://functions.sbs"); assert_eq!(node.instance.as_ref().unwrap().graph, "Functions/Math/Pi"); assert!(matches!(node.source, Some(Ok(_)))); + save(&mut harness, &directory, &format!("library-drag-{kind}")); harness.state_mut().ui.library_search = "function:div".into(); @@ -54,8 +58,10 @@ fn bottom_library_drags_functions_into_function_and_processor_canvases() { let function = target.current(&harness.state().session).unwrap(); let division = function.nodes.iter().find(|node| node.op == "div").unwrap(); + assert!(division.instance.is_none(), "native operations must not become SBS instances"); assert_eq!(division.inputs.len(), 2); + harness.get(By::new().role(egui::accesskit::Role::Button).label("Division")); save(&mut harness, &directory, &format!("library-native-{kind}")); @@ -70,7 +76,9 @@ fn bottom_library_drags_functions_into_function_and_processor_canvases() { .iter() .find(|node| node.instance.as_ref().is_some_and(|instance| instance.graph.ends_with("GlobalRandom"))) .unwrap(); + assert!(matches!(random.source, Some(Ok(_)))); + save(&mut harness, &directory, &format!("library-random-{kind}")); let mut missing = harness.state().session.pkg.dependencies["sbs://functions.sbs"].graph_by_name("Functions/Random/GlobalRandom").unwrap().1.clone(); @@ -95,7 +103,9 @@ fn bottom_library_drags_functions_into_function_and_processor_canvases() { .iter() .find(|node| node.instance.as_ref().is_some_and(|instance| instance.graph.ends_with("MissingFunction"))) .unwrap(); + assert!(matches!(missing.source, Some(Err(_)))); + let bounds = harness.get(By::new().role(egui::accesskit::Role::Button).label("MissingFunction")).rect(); let image = harness.render().unwrap(); let magenta = (2..12).any(|y| { @@ -104,7 +114,9 @@ fn bottom_library_drags_functions_into_function_and_processor_canvases() { pixel[0] > 150 && pixel[1] < 80 && pixel[2] > 80 }) }); + assert!(magenta, "missing header must be magenta"); + save(&mut harness, &directory, &format!("library-missing-{kind}")); } } diff --git a/crates/ui-egui/tests/snapshot/library.rs b/crates/ui-egui/tests/snapshot/library.rs index 2cd43a1..f89accd 100644 --- a/crates/ui-egui/tests/snapshot/library.rs +++ b/crates/ui-egui/tests/snapshot/library.rs @@ -40,6 +40,7 @@ fn library_tree_shows_every_category_and_adds_what_the_graph_takes() { ] { assert!(harness.query(tree(label)).is_some(), "{label}"); } + assert!(harness.query(tree("SDF functions")).is_none(), "the category is MCF functions now"); // Graph items, then the atomic nodes with their previews @@ -48,11 +49,13 @@ fn library_tree_shows_every_category_and_adds_what_the_graph_takes() { for label in ["Comment", "Frame", "Pin", "Dot node"] { assert!(harness.query(tile(label)).is_some(), "{label}"); } + harness.get(tree("Atomic nodes")).click(); settle(&mut harness, 12); for label in ["Blend", "Blur", "Levels", "Output", "Input color"] { assert!(harness.query(tile(label)).is_some(), "{label}"); } + save(&mut harness, &directory, "library-atomic-nodes"); // Dragging a tile adds it where it drops @@ -70,6 +73,7 @@ fn library_tree_shows_every_category_and_adds_what_the_graph_takes() { harness.event(egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); settle(&mut harness, 5); let graph = harness.state().session.graph().unwrap(); + assert_eq!(graph.nodes.len(), before + 1); assert_eq!(graph.nodes.last().unwrap().def, "blur"); @@ -78,7 +82,9 @@ fn library_tree_shows_every_category_and_adds_what_the_graph_takes() { settle(&mut harness, 3); harness.get(tree("Noises")).click(); settle(&mut harness, 4); + assert!(harness.query(tile("Perlin noise")).is_some()); + harness.get(tree("Texture generators")).click(); settle(&mut harness, 2); harness.get(tree("Texture generators")).click(); @@ -92,11 +98,13 @@ fn library_tree_shows_every_category_and_adds_what_the_graph_takes() { for label in ["Pi", "2 Pi"] { assert!(harness.query(tile(label)).is_some(), "{label}"); } + save(&mut harness, &directory, "library-functions-constant"); harness.get(tree("Functions")).click(); settle(&mut harness, 2); harness.get(tree("Functions")).click(); settle(&mut harness, 2); + assert!(!harness.state().ui.library_expanded.contains("Functions")); for (top, subs) in [ @@ -117,6 +125,7 @@ fn library_tree_shows_every_category_and_adds_what_the_graph_takes() { // Clicking the selected, open category closes it again harness.get(tree(top)).click(); settle(&mut harness, 2); + assert!(!harness.state().ui.library_expanded.contains(top), "{top}"); } @@ -129,13 +138,17 @@ fn library_tree_shows_every_category_and_adds_what_the_graph_takes() { settle(&mut harness, 2); harness.get(tree("Favorites")).click(); settle(&mut harness, 3); + assert!(harness.query(tile("Blend")).is_some()); assert_eq!(harness.state().ui.library_favorites, ["node:blend"]); + save(&mut harness, &directory, "library-favorites"); // Search finds entries across the whole library, what the graph takes first harness.state_mut().ui.library_search = "blur".into(); settle(&mut harness, 4); + assert!(harness.query(tile("Blur")).is_some()); + save(&mut harness, &directory, "library-search"); } diff --git a/crates/ui-egui/tests/snapshot/node_look.rs b/crates/ui-egui/tests/snapshot/node_look.rs index 4198fff..991e8f7 100644 --- a/crates/ui-egui/tests/snapshot/node_look.rs +++ b/crates/ui-egui/tests/snapshot/node_look.rs @@ -34,6 +34,7 @@ fn function_nodes_show_their_values_and_symbols() { for (operation, at) in operations { app.run("graph.function.edit", json!({"edit": {"action": "add", "operation": operation, "at": at}})).unwrap(); } + let mut harness = harness(app, 1600.0, 960.0); settle(&mut harness, 8); @@ -58,7 +59,9 @@ fn library_nodes_and_tiles_show_previews() { for (entry, at) in entries { app.run("library.add", json!({"entry": entry, "at": at})).unwrap(); } + let mut harness = harness(app, 1600.0, 960.0); + // The graph cooks on a worker for _ in 0..480 { std::thread::sleep(std::time::Duration::from_millis(250)); @@ -68,6 +71,7 @@ fn library_nodes_and_tiles_show_previews() { break; } } + save(&mut harness, &directory(), "look-library-nodes"); for (top, sub, name) in @@ -76,11 +80,13 @@ fn library_nodes_and_tiles_show_previews() { harness.get(By::new().role(Role::TreeItem).label(top)).click(); settle(&mut harness, 3); harness.get_all(By::new().role(Role::TreeItem).label(sub)).next().unwrap().click(); + // Previews cook on a worker for _ in 0..40 { std::thread::sleep(std::time::Duration::from_millis(250)); settle(&mut harness, 2); } + save(&mut harness, &directory(), name); harness.get(By::new().role(Role::TreeItem).label(top)).click(); settle(&mut harness, 2); diff --git a/docs/helpful-pr-review-guidelines.md b/docs/helpful-pr-review-guidelines.md index 098b6ae..fbe4f69 100644 --- a/docs/helpful-pr-review-guidelines.md +++ b/docs/helpful-pr-review-guidelines.md @@ -8,7 +8,7 @@ Use these preferences when reviewing and improving a PR. Follow repository requi - Review-only requests call for findings; requests to fix or improve also authorize focused local edits - A review may conclude that no changes are needed. If the PR meets the applicable standards and no actionable issues are found, leave it unchanged. Do not invent findings, add unnecessary tests, or make edits merely to produce a diff. Report what was checked and any remaining validation limits - Apply all review standards across the entire affected method, including existing code and relevant tests -- Complete naming, spacing, and comment cleanup throughout it +- Read and apply [CODE_STYLE.md](../CODE_STYLE.md) throughout that scope - Make additional correctness or performance changes when supported by evidence and appropriate validation - Separate substantial changes that address an independent problem, explaining why - Apply the same scope to standalone functions and inspect relevant callers when verifying behavior @@ -27,6 +27,7 @@ Use these preferences when reviewing and improving a PR. Follow repository requi ## Optimization and simplicity - Prefer the smallest complete solution that is efficient and easy to understand +- Keep control flow direct and share repeated logic when it makes the code simpler - Look for repeated calculations, expensive iterations, unnecessary allocations, and redundant data access - For loops, batches, and streaming operations, trace allocations and buffer lifetimes across multiple iterations, including outputs retained from earlier iterations. Identify work that belongs once per operation rather than once per item, using the full selection when making retention decisions - Consider caching, batching, and parallelism when measurements justify their complexity and memory cost @@ -35,39 +36,6 @@ Use these preferences when reviewing and improving a PR. Follow repository requi - Check what the compiler already optimizes before replacing clear arithmetic with micro-optimizations - Avoid speculative optimization, unnecessary dependencies, and abstractions that add more code than clarity -## Clarity, naming, and spacing - -- Clean up the functions or methods being changed and their relevant tests within the scope above -- Use descriptive names that explain purpose; include units where useful -- Avoid cryptic abbreviations, misleading names, and unexplained production constants -- Keep control flow direct and share repeated logic when it makes the code simpler -- Use a single blank line to separate logical steps, such as input preparation, validation, computation, mutation, and result construction, when those steps are distinct -- Keep closely related statements together; avoid spacing that fragments a single operation or inserts a blank line after every declaration -- Put a blank line after a multiline early-exit guard, including `if` and `let ... else`, before the next statement -- Keep comments attached to the code they explain; put a blank line before a comment introducing a new step, with no blank line between the comment and that step -- Keep documentation and attributes attached to their declarations -- Separate function definitions with one blank line; avoid extra empty lines immediately inside braces or between every field, argument, or statement -- Group tests into preparation, action, and verification with blank lines where those phases are distinct; keep related assertions together -- Apply this spacing consistently throughout affected functions, methods, and tests, and follow the repository's formatter for mechanical formatting - -## Source organization - -- Keep each class, enum, or struct in its own source file, with its implementation beside it -- Spell out the full type name in the filename; use the language's filename convention, such as `mesh_curvature_settings.rs` for `MeshCurvatureSettings` in Rust -- Use singular folder names, such as `map`, `setting`, or `tracing` -- Group related types into focused modules and share helpers where useful without collecting unrelated types into one large file -- Apply these preferences to new types and source organization changes within scope; follow mandatory repository layout rules and leave unrelated existing structure alone - -## Comments - -- Use short, single-line comments; adjacent comment lines should each express a concise point -- Avoid block comments and wrapped prose in ordinary implementation comments -- Explain intent or a non-obvious constraint rather than narrating the code -- No trailing punctuation on ordinary code comments -- Preserve explanations of guards and invariants; remove stale or redundant comments without losing necessary rationale -- API documentation, required license headers, and examples may use their conventional multiline formatting and punctuation -- Apply these standards to both production code and tests - ## Validation and reporting - Run relevant tests and required repository checks with the project's expected toolchain diff --git a/docs/testing-ci.md b/docs/testing-ci.md index 34bff0a..ccedbde 100644 --- a/docs/testing-ci.md +++ b/docs/testing-ci.md @@ -12,6 +12,31 @@ Run a feature module with a test-name filter, for example: cargo test -p materialcraft-engine --test it graph_edit:: ``` +For faster optimized local verification, run the same workspace tests and doctests +with the `verify` profile: + +```sh +cargo test --workspace --profile verify +``` + +`verify` inherits the release profile's optimization level and runtime checks, but +uses 16 codegen units and `lto = false`. Cargo retains thin LTO within each crate +without repeating cross-crate LTO for every test executable. The existing test +selection and ignore attributes still apply. Keep the target directory between +runs so Cargo can reuse unchanged build artifacts. + +To compile the normal application binaries alongside the test binaries before +running them: + +```sh +cargo build --workspace --bins --tests --profile verify +cargo test --workspace --profile verify +``` + +This profile is for local verification. Use `--release` for release validation and +performance measurements; its compiler settings remain unchanged. The regular +debug test profile and CI test selection also remain unchanged. + The language-switching test stays in its own executable because it changes the process-global locale, which UI constructors also set. @@ -21,7 +46,8 @@ unchanged. Tests in the shared executable can run concurrently across modules; keep temporary paths unique and avoid changing process-wide state. CI continues to use the shared gate workflow and `cargo xtask ci` on Ubuntu and -Windows: formatting, Clippy, workspace tests (including doctests), layering and -isolated per-package WebAssembly checks. The caller disables the shared -workflow's optional tool installation because this gate uses `cargo test` and -does not use nextest. +Windows, using the shared workflow's stable Rust default: formatting, layering, +Clippy, workspace tests (including doctests) and isolated per-package WebAssembly +checks. Layering runs before Clippy and tests to reject structural failures early. +The caller disables the shared workflow's optional tool installation because this +gate uses `cargo test` and does not use nextest. diff --git a/packaging/windows/package.ps1 b/packaging/windows/package.ps1 index d13f380..9f81e9e 100644 --- a/packaging/windows/package.ps1 +++ b/packaging/windows/package.ps1 @@ -90,16 +90,17 @@ Copy-Item (Join-Path $Bin 'materialcraft.exe'), (Join-Path $Bin 'materialcraft-c & (Join-Path $PSScriptRoot 'sign.ps1') (Join-Path $Stage 'materialcraft.exe') (Join-Path $Stage 'materialcraft-cli.exe') -# Stage documentation once so the MSI and portable ZIP carry the same notices and licences. -$Docs = Join-Path $Stage 'docs' -New-Item -ItemType Directory -Force -Path $Docs | Out-Null -foreach ($f in 'README.md', 'LICENSE-MIT', 'LICENSE-APACHE', 'NOTICE', 'ATTRIBUTION.md') { - Copy-Item -LiteralPath (Join-Path $Root $f) -Destination $Docs +# Stage documentation once so the MSI and portable ZIP carry the same notices and licences +$DocumentationDirectory = Join-Path $Stage 'docs' +New-Item -ItemType Directory -Force -Path $DocumentationDirectory | Out-Null +foreach ($DocumentationFile in 'README.md', 'LICENSE-MIT', 'LICENSE-APACHE', 'NOTICE', 'ATTRIBUTION.md') { + Copy-Item -LiteralPath (Join-Path $Root $DocumentationFile) -Destination $DocumentationDirectory } -# Include craft-fonts notices for both the UI faces and the SBS fallback font. + +# Include craft-fonts notices for both the UI faces and the SBS fallback font if ($env:CRAFT_FONTS_DIR) { - foreach ($ofl in Get-ChildItem -Path (Join-Path $env:CRAFT_FONTS_DIR 'fonts\*\OFL.txt') -File -ErrorAction SilentlyContinue) { - Copy-Item -LiteralPath $ofl.FullName -Destination (Join-Path $Docs "OFL-$($ofl.Directory.Name).txt") + foreach ($FontNotice in Get-ChildItem -Path (Join-Path $env:CRAFT_FONTS_DIR 'fonts\*\OFL.txt') -File -ErrorAction SilentlyContinue) { + Copy-Item -LiteralPath $FontNotice.FullName -Destination (Join-Path $DocumentationDirectory "OFL-$($FontNotice.Directory.Name).txt") } } @@ -108,7 +109,7 @@ $Msi = Join-Path $Dist "materialcraft-$Version-windows-$Arch.msi" Invoke-Native 'wix build' { wix build (Join-Path $PSScriptRoot 'materialcraft.wxs') -arch $Arch ` -d "Version=$MsiVersion" -d "BinDir=$Stage" -d "IconPath=$(Join-Path $Root 'assets\app-icon\materialcraft.ico')" ` - -d "DocsDir=$Docs" -o $Msi + -d "DocsDir=$DocumentationDirectory" -o $Msi } Invoke-Native 'MSI shortcut icon validation (ICE50)' { wix msi validate $Msi -ice ICE50 -intermediateFolder (Join-Path $Stage 'msi-validation') @@ -122,7 +123,7 @@ $Portable = Join-Path $TargetDir "windows-package\materialcraft-$Version-windows Remove-Item -Recurse -Force $Portable -ErrorAction SilentlyContinue New-Item -ItemType Directory -Force -Path $Portable | Out-Null Copy-Item (Join-Path $Stage '*.exe') $Portable -Copy-Item -Recurse (Join-Path $Docs '*') $Portable +Copy-Item -Recurse (Join-Path $DocumentationDirectory '*') $Portable $Zip = Join-Path $Dist "materialcraft-$Version-windows-$Arch-portable.zip" Remove-Item -Force $Zip -ErrorAction SilentlyContinue Compress-Archive -Path $Portable -DestinationPath $Zip diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 23b5d1b..b4ca9c6 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -15,7 +15,7 @@ usage: cargo xtask commands: layers enforce the crate dependency layering (xtask/src/layers.rs) wasm cargo check --target wasm32-unknown-unknown for the wasm crates and the web app - ci fmt --check, clippy -D warnings, test, layers, wasm (stops at the first failure) + ci fmt --check, layers, clippy -D warnings, test, wasm (stops at the first failure) version [set X.Y.Z[-pre]] print the workspace version, or set it (Cargo.toml + Cargo.lock) ico ... @@ -73,6 +73,7 @@ fn metadata() -> Result { if !out.status.success() { return Err(format!("cargo metadata failed:\n{}", String::from_utf8_lossy(&out.stderr))); } + serde_json::from_slice(&out.stdout).map_err(|e| format!("cargo metadata: bad JSON: {e}")) } @@ -89,11 +90,13 @@ fn cmd_layers() -> Result<(), String> { } } } + println!("\n{:<28} {:<8} workspace deps", "crate", "layer"); for c in &crates { let ws: Vec<&str> = c.deps.iter().filter(|d| d.workspace).map(|d| d.name.as_str()).collect(); println!("{:<28} {:<8} {}", c.name, layers::describe(&c.name), ws.join(", ")); } + let violations = layers::check(&crates); println!(); if violations.is_empty() { @@ -104,6 +107,7 @@ fn cmd_layers() -> Result<(), String> { for v in &violations { println!(" - {v}"); } + Err(format!("{} layering violation(s)", violations.len())) } } @@ -130,12 +134,14 @@ fn cmd_wasm() -> Result<(), String> { println!("skip: {pkg} (not a workspace member yet)"); continue; } - // One package per invocation, so no crate passes only thanks to features another enables. + + // One package per invocation, so no crate passes only thanks to features another enables let mut c = cargo(); c.args(["check", "--target", "wasm32-unknown-unknown", "-p", pkg]); let ok = run(c, &format!("cargo check --target wasm32-unknown-unknown -p {pkg}")).is_ok(); results.push((*pkg, ok)); } + println!("\nwasm32-unknown-unknown check:"); for (p, ok) in &results { println!(" {:<6} {p}", if *ok { "ok" } else { "FAIL" }); @@ -153,11 +159,13 @@ fn cmd_ci() -> Result<(), String> { } }; type Step = (&'static str, Box Result<(), String>>); + + // Reject invalid dependency layering before spending time compiling and running the workspace let steps: Vec = vec![ ("fmt", Box::new(cargo_step(&["fmt", "--all", "--", "--check"]))), + ("layers", Box::new(cmd_layers)), ("clippy", Box::new(cargo_step(&["clippy", "--workspace", "--all-targets", "--", "-D", "warnings"]))), ("test", Box::new(cargo_step(&["test", "--workspace"]))), - ("layers", Box::new(cmd_layers)), ("wasm", Box::new(cmd_wasm)), ]; for (i, (name, step)) in steps.iter().enumerate() { @@ -174,6 +182,7 @@ fn cmd_ci() -> Result<(), String> { return Err(format!("ci failed at `{name}`")); } } + println!("\nCI summary: all {} steps passed", steps.len()); Ok(()) }