Auto: Integrate main and guard unavailable tool actions

Preserve the shared-catalog integration and saved review repairs. Reject saved path commits during quick-mask editing, hide the unavailable 2D brush cursor, and reduce dirty-tile selection work. Keep the Selection test fixture in its own module.
This commit is contained in:
chasse20 committed 2026-11-15 20:47:26 +00:00
commit 7a780244d9
78 files changed
+3160 -11254

No files matched your search

-1
View File
@@ -24,7 +24,6 @@ jobs:
# Same craft-fonts commit as release.yml.
craft-fonts-ref: aced4137c022f1f587624d48e81ec77e1914a9cc
os: '["ubuntu-24.04","windows-latest"]'
toolchain: "1.96.0"
# xtask ci runs cargo test (including doctests), not cargo nextest.
tools: ""
packages: >-
+5
View File
@@ -1,6 +1,10 @@
name: Release
on:
# A nightly dry run on main: every release build except macOS, nothing signed or released
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
schedule:
- cron: "44 2 * * *"
push:
branches: [release]
workflow_dispatch:
@@ -23,6 +27,7 @@ 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/release.yml@main
with:
dry-run: ${{ github.event_name == 'schedule' }}
app-name: texturecraft
app-title: "TextureCraft"
craft-fonts-ref: aced4137c022f1f587624d48e81ec77e1914a9cc
+77
View File
@@ -0,0 +1,77 @@
# Code style
Style conventions for TextureCraft'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.
Generated
+36 -34
View File
@@ -578,7 +578,7 @@ dependencies = [
"maybe-owned",
"rustix 1.1.5",
"rustix-linux-procfs",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
"winx",
]
@@ -758,7 +758,7 @@ dependencies = [
[[package]]
name = "craft-dialogs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"block2 0.6.2",
"objc2 0.6.5",
@@ -771,12 +771,12 @@ dependencies = [
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -799,7 +799,7 @@ dependencies = [
[[package]]
name = "craft-keymap"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"egui",
"serde",
@@ -809,7 +809,7 @@ dependencies = [
[[package]]
name = "craft-menu"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"craft-keymap",
"craft-quit",
@@ -822,7 +822,7 @@ dependencies = [
[[package]]
name = "craft-quit"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"dispatch2",
"egui",
@@ -833,7 +833,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -854,7 +854,7 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"chrono",
"dunce",
@@ -864,6 +864,7 @@ dependencies = [
"image",
"log",
"plist",
"rustix 1.1.5",
"semver",
"serde",
"serde_json",
@@ -880,7 +881,7 @@ dependencies = [
[[package]]
name = "craft-window"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"egui",
"objc2 0.6.5",
@@ -1319,7 +1320,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -2542,7 +2543,7 @@ dependencies = [
"objc2-foundation 0.3.2",
"png",
"thiserror 2.0.21",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -3066,7 +3067,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -3710,7 +3711,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -3777,7 +3778,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -4130,7 +4131,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -4245,7 +4246,7 @@ dependencies = [
"getrandom 0.4.3",
"once_cell",
"rustix 1.1.5",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -4259,7 +4260,7 @@ dependencies = [
[[package]]
name = "texturecraft"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"craft-menu",
"craft-update",
@@ -4280,7 +4281,7 @@ dependencies = [
[[package]]
name = "texturecraft-automation"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"base64 0.22.1",
"cap-std",
@@ -4301,7 +4302,7 @@ dependencies = [
[[package]]
name = "texturecraft-bake"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"bytemuck",
"rayon",
@@ -4313,7 +4314,7 @@ dependencies = [
[[package]]
name = "texturecraft-cli"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"image",
"serde_json",
@@ -4325,7 +4326,7 @@ dependencies = [
[[package]]
name = "texturecraft-compose"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"half",
"rayon",
@@ -4336,7 +4337,7 @@ dependencies = [
[[package]]
name = "texturecraft-core"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"serde",
"serde_json",
@@ -4345,7 +4346,7 @@ dependencies = [
[[package]]
name = "texturecraft-engine"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"epaint_default_fonts",
"image",
@@ -4365,7 +4366,7 @@ dependencies = [
[[package]]
name = "texturecraft-formats"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"base64 0.22.1",
"exr",
@@ -4390,7 +4391,7 @@ dependencies = [
[[package]]
name = "texturecraft-geom"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"glam 0.30.10",
"mikktspace",
@@ -4401,7 +4402,7 @@ dependencies = [
[[package]]
name = "texturecraft-paint"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"epaint_default_fonts",
"harfrust",
@@ -4416,7 +4417,7 @@ dependencies = [
[[package]]
name = "texturecraft-render"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"bytemuck",
"half",
@@ -4435,14 +4436,15 @@ dependencies = [
[[package]]
name = "texturecraft-texel"
version = "0.3.1"
version = "0.6.1"
[[package]]
name = "texturecraft-ui-egui"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"base64 0.22.1",
"craft-dialogs",
"craft-i18n",
"craft-keymap",
"craft-quit",
"craft-ui",
@@ -4473,7 +4475,7 @@ dependencies = [
[[package]]
name = "texturecraft-web"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"eframe",
"js-sys",
@@ -4765,7 +4767,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e"
dependencies = [
"memoffset",
"tempfile",
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -5360,7 +5362,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.60.2",
]
[[package]]
@@ -5865,7 +5867,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
[[package]]
name = "xtask"
version = "0.3.1"
version = "0.6.1"
dependencies = [
"serde_json",
]
+17 -8
View File
@@ -5,7 +5,7 @@ exclude = ["vendor"]
default-members = ["crates/core", "crates/texel", "crates/geom", "crates/compose", "crates/bake", "crates/formats", "crates/paint", "crates/engine", "crates/render", "crates/automation", "crates/ui-egui", "apps/texturecraft", "apps/texturecraft-cli", "xtask"]
[workspace.package]
version = "0.3.1"
version = "0.6.1"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
@@ -65,19 +65,21 @@ getrandom = "0.3"
rustix = { version = "1", default-features = false, features = ["std", "fs", "process"] }
# Shared Craft egui components (widget palette, icon buttons, panel docking): storytold/craft-libs,
# MIT OR Apache-2.0, pinned to one rev.
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["serde"] }
# Shared catalog lookup, pinned with the other Craft libraries.
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["serde"] }
# Who draws the window frame, and the app-drawn strip as the title bar (crates/ui-egui/src/titlebar.rs).
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
# Release checks, the update prompt and the update helper (crates/ui-egui/src/updates_ui.rs).
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
# File dialogs that never block the frame (crates/ui-egui/src/file_dialogs.rs).
craft-dialogs = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
craft-dialogs = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
# Shortcut chords, lookup and dispatch (crates/ui-egui/src/keymap.rs).
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
# Quitting the macOS way instead of closing the window (crates/ui-egui/src/windows.rs).
craft-quit = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
craft-quit = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
# The macOS app menu whose Quit asks about unsaved changes (apps/texturecraft/src/main.rs).
craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
# egui 0.37 and winit: the suite's forks (storytold/egui, storytold/winit), as craft-libs pins them.
[patch.crates-io]
@@ -115,6 +117,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"
+24 -2
View File
@@ -1,12 +1,34 @@
//! Drives the `texturecraft-cli` binary.
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use serde_json::Value;
// Nextest 0.9.130+ remaps this variable when an archive is extracted on another
// runner. Cargo's compile-time path still supports ordinary `cargo test`.
fn cli_binary() -> OsString {
cli_binary_path(std::env::var_os("CARGO_BIN_EXE_texturecraft-cli"))
}
fn cli_binary_path(runner_path: Option<OsString>) -> OsString {
runner_path.unwrap_or_else(|| env!("CARGO_BIN_EXE_texturecraft-cli").into())
}
#[test]
fn cli_binary_prefers_relocated_runner_path() {
let relocated = OsString::from("relocated archive/texturecraft-cli");
assert_eq!(cli_binary_path(Some(relocated.clone())), relocated);
}
#[test]
fn cli_binary_defaults_to_cargo_path() {
assert_eq!(cli_binary_path(None), OsString::from(env!("CARGO_BIN_EXE_texturecraft-cli")));
}
fn cli(args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_texturecraft-cli")).args(args).output().unwrap()
Command::new(cli_binary()).args(args).output().unwrap()
}
fn ok(args: &[&str]) -> String {
@@ -101,7 +123,7 @@ fn info_on_a_mesh_reports_uvs() {
fn serve_takes_the_automation_root_spelling_too() {
use std::io::Write as _;
let d = tmp("automation-root");
let mut child = Command::new(env!("CARGO_BIN_EXE_texturecraft-cli"))
let mut child = Command::new(cli_binary())
.args(["serve", "--automation-write-root", p(&d)])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
+1
View File
@@ -0,0 +1 @@
3cb11a36a58c5eabfd3a768448b55fdf086885bf
+113 -43
View File
@@ -10,10 +10,12 @@ use std::collections::{BTreeSet, HashMap, VecDeque};
use std::sync::Arc;
use texturecraft_core::project::MAX_DEPTH;
use texturecraft_core::{BakedMap, Channel, Layer, LayerId, LayerKind, Project, TextureSet};
use texturecraft_core::{BakedMap, Channel, EffectKind, Layer, LayerId, LayerKind, Project, Source, TextureSet};
use texturecraft_texel::TileImage;
use crate::eval::{Inputs, PaintTarget};
use crate::held_fields::HeldFields;
use crate::image_identity::ImageIdentity;
use crate::store::{BakedMaps, ImageStore, PixelStore};
/// Default byte budget of a [`BelowCache`], per nesting level it holds (see
@@ -40,11 +42,28 @@ pub struct BelowCache {
/// Tiles of the whole pass a caller splits into several composites (0: each composite is a
/// pass). A pass larger than the cache records only what fits.
pub pass: usize,
/// Tiles the caller composites next in this pass, after those of the coming composite (empty: none, or a pass that is one
/// composite). A stack with neighbourhood filters then filters the coming tiles and these at once and keeps the result for
/// the composites that follow, instead of filtering each small chunk's surroundings again.
pub ahead: Vec<(u32, u32)>,
pub(crate) held: Option<HeldFields>,
}
impl Default for BelowCache {
fn default() -> Self {
BelowCache { hot: None, key: None, tiles: HashMap::new(), order: VecDeque::new(), clock: 0, bytes: 0, budget: BELOW_BUDGET, levels: 1, pass: 0 }
BelowCache {
hot: None,
key: None,
tiles: HashMap::new(),
order: VecDeque::new(),
clock: 0,
bytes: 0,
budget: BELOW_BUDGET,
levels: 1,
pass: 0,
ahead: Vec::new(),
held: None,
}
}
}
@@ -78,48 +97,82 @@ pub struct StackKey {
layers: Vec<Layer>,
pixels: Vec<(LayerId, Option<Channel>, Shared)>,
baked: (u32, Vec<(BakedMap, usize)>),
images: Vec<(String, usize)>,
images: Vec<(String, ImageIdentity)>,
}
impl StackKey {
/// The key of `roots` (layers of texture set `ts`, bottom → top) and their effects.
pub fn new(project: &Project, pixels: &PixelStore, baked: &BakedMaps, images: &ImageStore, ts: usize, roots: &[LayerId]) -> Option<StackKey> {
StackKey::of_tree(project, pixels, baked, images, ts, roots.to_vec(), &project.render_tree(roots))
/// The key of `roots` (layers of a texture set, bottom → top) and their effects.
pub fn new(
project: &Project,
pixels: &PixelStore,
baked: &BakedMaps,
images: &ImageStore,
texture_set_index: usize,
roots: &[LayerId],
) -> Option<StackKey> {
StackKey::of_tree(project, pixels, baked, images, texture_set_index, roots.to_vec(), &project.render_tree(roots))
}
/// The key of the layers in `tree` (and their effects), drawn as `roots` arrange them.
/// The key of the layers in `render_tree` (and their effects), drawn as `roots` arrange them.
fn of_tree(
project: &Project,
pixels: &PixelStore,
baked: &BakedMaps,
images: &ImageStore,
ts: usize,
texture_set_index: usize,
roots: Vec<LayerId>,
tree: &BTreeSet<LayerId>,
render_tree: &BTreeSet<LayerId>,
) -> Option<StackKey> {
let mut set = project.texture_set(ts).ok()?.clone();
let mut set = project.texture_set(texture_set_index).ok()?.clone();
set.name.clear();
set.description.clear();
set.stack.clear();
set.bake = Default::default();
let effects: BTreeSet<LayerId> = tree.iter().filter_map(|id| project.layers.get(id)).flat_map(effect_ids).collect();
let pixels = tree
let effect_ids: BTreeSet<LayerId> = render_tree.iter().filter_map(|id| project.layers.get(id)).flat_map(effect_ids).collect();
let pixel_handles = render_tree
.iter()
.chain(&effects)
.chain(&effect_ids)
.flat_map(|id| {
let layer = Channel::ALL.into_iter().filter_map(move |c| pixels.layer(*id, c).map(|img| (*id, Some(c), Shared(img.clone()))));
layer.chain(pixels.masks.get(id).map(|img| (*id, None, Shared(img.clone()))))
let layer_channels =
Channel::ALL.into_iter().filter_map(move |channel| pixels.layer(*id, channel).map(|image| (*id, Some(channel), Shared(image.clone()))));
layer_channels.chain(pixels.masks.get(id).map(|image| (*id, None, Shared(image.clone()))))
})
.collect();
let mut images: Vec<(String, usize)> = images.iter().map(|(n, i)| (n.clone(), Arc::as_ptr(i) as usize)).collect();
images.sort();
let mut referenced_images = BTreeSet::new();
if !images.is_empty() {
let mut record_image_reference = |source: &Source| {
if let Source::Image { name, .. } = source
&& let Some((name, _)) = images.get_key_value(name)
{
referenced_images.insert(name);
}
};
for layer in render_tree.iter().filter_map(|id| project.layers.get(id)) {
if let LayerKind::Fill(fill) = &layer.kind {
fill.sources.values().for_each(&mut record_image_reference);
}
for effect in layer.mask.iter().flat_map(|mask| &mask.effects).chain(&layer.content_effects) {
match &effect.kind {
EffectKind::Fill { source, .. } => record_image_reference(source),
EffectKind::Material(fill) => fill.sources.values().for_each(&mut record_image_reference),
_ => {}
}
}
}
}
let image_identities =
referenced_images.into_iter().filter_map(|name| images.get(name).map(|image| (name.clone(), ImageIdentity::new(image)))).collect();
Some(StackKey {
set,
roots,
layers: tree.iter().filter_map(|id| project.layers.get(id)).map(render_view).collect(),
pixels,
baked: (baked.resolution, baked.maps.iter().map(|(m, d)| (*m, Arc::as_ptr(d) as usize)).collect()),
images,
layers: render_tree.iter().filter_map(|id| project.layers.get(id)).map(render_view).collect(),
pixels: pixel_handles,
baked: (baked.resolution, baked.maps.iter().map(|(map, data)| (*map, Arc::as_ptr(data) as usize)).collect()),
images: image_identities,
})
}
}
@@ -173,23 +226,30 @@ impl BelowCache {
pub fn clear(&mut self) {
let tiles = std::mem::take(&mut self.tiles);
// Freeing a full cache (up to the byte budget in small bands) can take most of a
// second: a thread does it so the frame that switched the hot layer doesn't stall.
// Native builds free cached bands off-thread to avoid a frame stall
#[cfg(not(target_arch = "wasm32"))]
if !tiles.is_empty() {
let _ = std::thread::Builder::new().name("texturecraft-free".into()).spawn(move || drop(tiles));
}
#[cfg(target_arch = "wasm32")]
drop(tiles);
self.order.clear();
self.bytes = 0;
self.key = None;
self.held = None;
}
pub fn bytes(&self) -> usize {
self.bytes
}
/// Whether neighbourhood-filter fields are kept for the composites that follow (see [`BelowCache::ahead`]).
pub fn holds_fields(&self) -> bool {
self.held.is_some()
}
/// The byte limit: the budget for the top level and as much again for a hot layer nested in
/// folders (each tile then holds the stack at two or more levels).
pub fn limit(&self) -> usize {
@@ -256,43 +316,53 @@ impl BelowCache {
/// live stroke). Empties the cache when what the cached part reads changed.
pub(crate) fn prepare(&mut self, inp: &Inputs) -> Option<Vec<Level>> {
let hot = self.hot?;
let p = inp.project;
let set = p.texture_set(inp.ts).ok()?;
// Anchor readers depend on the stack below them.
if p.render_tree(&set.stack).iter().any(|id| p.layers.get(id).is_some_and(Layer::reads_anchor)) {
let project = inp.project;
let set = project.texture_set(inp.ts).ok()?;
// Anchor readers depend on the stack below them
if project.render_tree(&set.stack).iter().any(|id| project.layers.get(id).is_some_and(Layer::reads_anchor)) {
return None;
}
let levels = path(p, &set.stack, hot)?;
let rest: Vec<LayerId> = levels.iter().flat_map(|l| l.below.iter().chain(&l.above)).copied().collect();
if rest.is_empty() && levels.len() == 1 {
let levels = path(project, &set.stack, hot)?;
let off_path_roots: Vec<LayerId> = levels.iter().flat_map(|level| level.below.iter().chain(&level.above)).copied().collect();
if off_path_roots.is_empty() && levels.len() == 1 {
return None;
}
let mut tree = p.render_tree(&rest);
// Folders holding the hot layer (and the folders their instances draw) count with their
// own state only, not their children.
for l in levels.iter().take(levels.len() - 1) {
let mut id = Some(l.at);
let mut cached_tree = project.render_tree(&off_path_roots);
// Cache containing folders by state, without their children
for level in levels.iter().take(levels.len() - 1) {
let mut id = Some(level.at);
for _ in 0..=MAX_DEPTH {
let Some(i) = id.filter(|i| tree.insert(*i)) else { break };
id = p.layers.get(&i).and_then(Layer::instance_source);
let Some(instance_id) = id.filter(|instance_id| cached_tree.insert(*instance_id)) else { break };
id = project.layers.get(&instance_id).and_then(Layer::instance_source);
}
}
if tree.contains(&hot) {
// Repeated hot instances cannot share the off-path cache
if cached_tree.contains(&hot) {
return None;
}
if let Some(pv) = inp.preview {
let (PaintTarget::Layer(t) | PaintTarget::Mask(t)) = pv.target;
if tree.contains(&t) || tree.iter().filter_map(|id| p.layers.get(id)).flat_map(effect_ids).any(|e| e == t) {
if let Some(preview) = inp.preview {
let (PaintTarget::Layer(target) | PaintTarget::Mask(target)) = preview.target;
if cached_tree.contains(&target)
|| cached_tree.iter().filter_map(|id| project.layers.get(id)).flat_map(effect_ids).any(|effect_id| effect_id == target)
{
return None;
}
}
let roots = set.stack.iter().chain(levels.iter().map(|l| &l.at)).copied().collect();
let key = StackKey::of_tree(p, inp.pixels, inp.baked, inp.images, inp.ts, roots, &tree)?;
let roots = set.stack.iter().chain(levels.iter().map(|level| &level.at)).copied().collect();
let key = StackKey::of_tree(project, inp.pixels, inp.baked, inp.images, inp.ts, roots, &cached_tree)?;
self.levels = levels.len();
if self.key.as_ref() != Some(&key) {
self.clear();
self.key = Some(key);
}
Some(levels)
}
}
+40 -10
View File
@@ -125,8 +125,9 @@ fn store(curvature: &Arc<Vec<f32>>, key: CacheKey, scales: &BlockScales, sizes:
sizes
};
// One layout per curvature map, with the most recently used map at the end
cache.retain(|(source, ..)| source.strong_count() > 0 && !std::ptr::eq(source.as_ptr(), Arc::as_ptr(curvature)));
// Keep distinct layouts: thumbnails and the viewport alternate on the same bake.
// Replacing only this variant prevents them from repeatedly rebuilding each other.
cache.retain(|entry| entry.0.strong_count() > 0 && !is_entry_for(entry, curvature, key, scales));
let sizes = Arc::new(sizes);
cache.push((Arc::downgrade(curvature), key, scales.clone(), sizes.clone()));
trim_cache(&mut cache, CACHE_BYTES);
@@ -338,6 +339,7 @@ mod tests {
#[test]
fn unused_maps_are_evicted_even_while_undo_keeps_the_sources_alive() {
let _guard = CACHE_TESTS.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let sources: Vec<_> = (0..=CACHE_MAPS).map(|_| Arc::new(vec![0.5; 16])).collect();
let scales = BlockScales::default();
let key = ((4, 4), (4, 4), (4, 4));
@@ -408,18 +410,46 @@ mod tests {
}
}
// Cache lifecycle tests must not evict each other's entries in parallel.
static CACHE_TESTS: Mutex<()> = Mutex::new(());
#[test]
fn a_new_layout_replaces_the_sizes_of_the_old_one() {
fn alternating_layouts_reuse_their_cached_sizes() {
let _guard = CACHE_TESTS.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let curvature = Arc::new(vec![0.5; 16]);
let scales = BlockScales::default();
let entries_for_curvature = || {
let cache = CACHE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
cache.iter().filter(|(source, ..)| std::ptr::eq(source.as_ptr(), Arc::as_ptr(&curvature))).count()
};
let viewport_key = ((4, 4), (4, 4), (4, 4));
let thumbnail_key = ((4, 4), (4, 4), (2, 2));
let viewport = store(&curvature, viewport_key, &scales, CurvatureSizes { levels: Vec::new() });
let thumbnail = store(&curvature, thumbnail_key, &scales, CurvatureSizes { levels: Vec::new() });
store(&curvature, ((4, 4), (4, 4), (4, 4)), &scales, CurvatureSizes { levels: Vec::new() });
store(&curvature, ((4, 4), (2, 2), (8, 8)), &scales, CurvatureSizes { levels: Vec::new() });
assert_eq!(entries_for_curvature(), 1, "only the latest layout of a curvature map stays cached");
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scales).unwrap(), &viewport), "a thumbnail must not evict the viewport layout");
assert!(Arc::ptr_eq(&cached(&curvature, thumbnail_key, &scales).unwrap(), &thumbnail), "both layouts stay reusable");
let mut set = texturecraft_core::TextureSet::new("Tiles", 0, 4);
set.uv_tiles = vec![1001, 1002];
set.tile_resolutions.insert(1002, 2);
let scaled = set.block_scales();
assert_ne!(scaled, scales);
assert!(cached(&curvature, viewport_key, &scaled).is_none(), "different tile scales cannot read the unscaled result");
let scaled_sizes = store(&curvature, viewport_key, &scaled, CurvatureSizes { levels: Vec::new() });
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scaled).unwrap(), &scaled_sizes));
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scales).unwrap(), &viewport), "scale variants also coexist");
let replacement = store(&curvature, viewport_key, &scales, CurvatureSizes { levels: Vec::new() });
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scales).unwrap(), &replacement));
assert!(Arc::ptr_eq(&cached(&curvature, thumbnail_key, &scales).unwrap(), &thumbnail), "updating one layout keeps the other");
}
#[test]
fn layout_variants_obey_the_entry_limit() {
let _guard = CACHE_TESTS.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let curvature = Arc::new(vec![0.5; 16]);
let scales = BlockScales::default();
let key = |size| ((4, 4), (4, 4), (size, size));
for size in 1..=CACHE_MAPS as u32 + 1 {
store(&curvature, key(size), &scales, CurvatureSizes { levels: Vec::new() });
}
assert!(cached(&curvature, key(1), &scales).is_none(), "layout variants cannot grow without bound");
assert!(cached(&curvature, key(CACHE_MAPS as u32 + 1), &scales).is_some(), "the newest layout stays cached");
}
#[test]
+278 -115
View File
@@ -18,8 +18,9 @@ use texturecraft_geom::glam::{Vec2, Vec3};
use texturecraft_geom::{Mesh, NO_TRI, UvGBuffer};
use texturecraft_texel::{DilationMap, TILE, TILE_AREA, TileImage, from_u16, noise, to_u16, to_u16_signed};
use crate::below::{Band, BelowCache, Level};
use crate::below::{Band, BelowCache, Level, StackKey};
use crate::curvature_sizes::CurvatureSizes;
use crate::held_fields::HeldFields;
use crate::pattern::{Coords, decal_uv, image_samples, procedural, world_noise};
use crate::store::{BakedMaps, ImageStore, PixelStore, decode};
@@ -434,15 +435,29 @@ fn gather(op: Op, orig: &[f32], cover: &[f32], comps: usize, (x0, y0, w, h): (u3
/// the region it is computed in, so tiles composited in any grouping agree exactly.
const FIX: f64 = (1u64 << 24) as f64;
/// Texels one parallel task of a neighbourhood filter handles at least. Compositing in frame slices filters regions of a few tiles: split per texel,
/// handing that little work to every thread costs far more than the work itself. Each texel is computed on its own, so the split does not change results.
const MIN_PARALLEL_TEXELS: usize = 1 << 14;
/// How many lines of `length` texels make a parallel task of at least [`MIN_PARALLEL_TEXELS`].
fn lines_per_task(length: usize) -> usize {
(MIN_PARALLEL_TEXELS / length.max(1)).max(1)
}
/// Three running-sum box blurs (≈ Gaussian) of a `width × height` image along its rows, then its columns, each in parallel.
pub(crate) fn blur(data: &mut [f32], width: usize, height: usize, radius: usize) {
let to_fixed = |value: &f32| if value.is_finite() { (f64::from(value.clamp(-1e6, 1e6)) * FIX).round() as i64 } else { 0 };
let mut fixed: Vec<i64> = data.par_iter().map(to_fixed).collect();
// Bound samples before fixed-point accumulation
const MAX_INPUT_MAGNITUDE: f32 = 1e6;
let to_fixed = |value: &f32| if value.is_finite() { (f64::from(value.clamp(-MAX_INPUT_MAGNITUDE, MAX_INPUT_MAGNITUDE)) * FIX).round() as i64 } else { 0 };
let mut fixed: Vec<i64> = data.par_iter().with_min_len(MIN_PARALLEL_TEXELS).map(to_fixed).collect();
blur_rows(&mut fixed, width, radius);
let mut columns = transpose(&fixed, width, height);
blur_rows(&mut columns, height, radius);
data.par_iter_mut().zip(transpose(&columns, height, width)).for_each(|(target, blurred)| *target = (blurred as f64 / FIX) as f32);
transpose_into(&columns, height, width, &mut fixed);
data.par_iter_mut().with_min_len(MIN_PARALLEL_TEXELS).zip(fixed).for_each(|(target, blurred)| *target = (blurred as f64 / FIX) as f32);
}
/// The box blurs `blur` runs along each axis
@@ -453,11 +468,12 @@ fn blur_rows(data: &mut [i64], w: usize, r: usize) {
if w == 0 {
return;
}
data.par_chunks_mut(w).for_each(|row| {
let mut tmp = vec![0i64; row.len()];
data.par_chunks_mut(w).with_min_len(lines_per_task(w)).for_each(|row| {
let mut scratch = vec![0i64; row.len()];
for _ in 0..BLUR_BOX_PASSES {
box_line(row, &mut tmp, r);
row.copy_from_slice(&tmp);
box_line(row, &mut scratch, r);
row.copy_from_slice(&scratch);
}
});
}
@@ -465,7 +481,8 @@ fn blur_rows(data: &mut [i64], w: usize, r: usize) {
/// One box blur of radius `r` of a line (texels past the ends count as 0).
fn box_line(src: &[i64], dst: &mut [i64], r: usize) {
let n = (2 * r + 1) as i64;
// Rounded division of a window sum.
// Rounded division of a window sum
let avg = |acc: i64| (2 * acc + n).div_euclid(2 * n);
let mut acc: i64 = src.iter().take(r + 1).sum();
for (i, d) in dst.iter_mut().enumerate() {
@@ -480,14 +497,18 @@ fn box_line(src: &[i64], dst: &mut [i64], r: usize) {
/// `src` (`w × h`, row-major) transposed to `h × w`.
fn transpose(src: &[i64], w: usize, h: usize) -> Vec<i64> {
let mut out = vec![0i64; src.len()];
transpose_into(src, w, h, &mut out);
out
}
fn transpose_into(src: &[i64], w: usize, h: usize, out: &mut [i64]) {
if h > 0 {
out.par_chunks_mut(h).enumerate().for_each(|(x, col)| {
out.par_chunks_mut(h).with_min_len(lines_per_task(h)).enumerate().for_each(|(x, col)| {
for (y, d) in col.iter_mut().enumerate() {
*d = src.get(y * w + x).copied().unwrap_or(0);
}
});
}
out
}
/// Covered texels of tile (tx, ty): (x, y, offset inside the tile).
@@ -607,9 +628,12 @@ fn filter_region(
(rx0, ry0, w, h): (u32, u32, u32, u32),
start: Option<(usize, &Field)>,
) -> Option<Field> {
// Tiny coverage sums make normalization unstable
const MIN_FILTER_WEIGHT: f32 = 1e-6;
let opacity = effects.iter().filter(|effect| effect.visible).nth(index).map(|effect| effect.opacity.clamp(0.0, 1.0)).unwrap_or(1.0);
let (radius_texels, _) = filter_extent(radius, op, inp.gbuf.width.max(inp.gbuf.height));
let comps = match job {
let component_count = match job {
Job::Mask(_) => 1,
Job::Content(_) => 4,
};
@@ -620,7 +644,7 @@ fn filter_region(
let parts: Vec<Vec<(u32, u32, [f32; 4])>> = region_tiles
.par_iter()
.map(|&(tx, ty)| {
let full = anchor_plan(inp, id, channels, tx, ty, Some(below));
let anchor_nodes = anchor_plan(inp, id, channels, tx, ty, Some(below));
let preview_tile = inp.preview.and_then(|preview| preview.alpha.tile(tx, ty));
let mut mask = None;
let mut nodes = Vec::new();
@@ -634,22 +658,23 @@ fn filter_region(
}
Job::Content(channel) => {
nodes = plan(inp, &[id], &[channel], tx, ty, 0, Some(below));
if let Some(chan) = nodes.first_mut().and_then(|node| node.chans.first_mut()) {
chan.effects.truncate(index);
chan.start = start;
if let Some(channel_plan) = nodes.first_mut().and_then(|node| node.chans.first_mut()) {
channel_plan.effects.truncate(index);
channel_plan.start = start;
}
}
}
tile_texels(inp.gbuf, tx, ty)
.map(|(x, y, offset)| {
let mut texel = Texel::new(inp, x, y, offset, stroke_at(preview_tile, offset));
if !full.is_empty() {
texel.anchors_below(&full, id);
if !anchor_nodes.is_empty() {
texel.anchors_below(&anchor_nodes, id);
}
let value = match (&mask, nodes.first()) {
(Some(mask_stack), _) => [texel.mask(mask_stack), 0.0, 0.0, 0.0],
(None, Some(node)) => match node.chans.first().and_then(|chan| texel.content(&node.content, chan)) {
Some((color, cov)) => [color[0] * cov, color[1] * cov, color[2] * cov, cov],
(None, Some(node)) => match node.chans.first().and_then(|channel_plan| texel.content(&node.content, channel_plan)) {
Some((color, alpha)) => [color[0] * alpha, color[1] * alpha, color[2] * alpha, alpha],
None => [0.0; 4],
},
(None, None) => [0.0; 4],
@@ -662,64 +687,77 @@ fn filter_region(
// Scatter into the region, a row of tiles at a time (the region is tile aligned)
let texel_count = (w * h) as usize;
let mut orig = vec![0.0f32; texel_count * comps];
let mut cover = vec![0.0f32; texel_count];
let mut original_values = vec![0.0f32; texel_count * component_count];
let mut coverage = vec![0.0f32; texel_count];
let row_tiles = (rx1.div_ceil(TILE) - rx0 / TILE).max(1) as usize;
let band = (TILE * w) as usize;
orig.par_chunks_mut(band * comps).zip(cover.par_chunks_mut(band)).zip(parts.par_chunks(row_tiles)).for_each(|((orig, cover), row)| {
for &(x, y, value) in row.iter().flatten() {
let texel = (((y - ry0) % TILE) * w + (x - rx0)) as usize;
if let (Some(covered), Some(pixel)) = (cover.get_mut(texel), orig.get_mut(texel * comps..(texel + 1) * comps)) {
*covered = 1.0;
pixel.copy_from_slice(&value[..comps]);
original_values.par_chunks_mut(band * component_count).zip(coverage.par_chunks_mut(band)).zip(parts.par_chunks(row_tiles)).for_each(
|((original_values, coverage), row)| {
for &(x, y, value) in row.iter().flatten() {
let texel = (((y - ry0) % TILE) * w + (x - rx0)) as usize;
if let (Some(covered), Some(pixel)) = (coverage.get_mut(texel), original_values.get_mut(texel * component_count..(texel + 1) * component_count))
{
*covered = 1.0;
pixel.copy_from_slice(&value[..component_count]);
}
}
}
});
},
);
// High pass centers on the channel's mid value (0 for signed channels like height)
let mid = match job {
// Release scattered texels before allocating filter buffers
drop(parts);
// High pass centers on the channel's midpoint value (0 for signed channels like height)
let midpoint = match job {
Job::Content(channel) if channel.kind() == ChannelKind::Signed => 0.0,
_ => 0.5,
};
// A smaller UV tile has fewer texels per UV unit, so its filters reach fewer texels
let filtered = |scale: f32| -> Vec<f32> {
let res = ((inp.tile_res() as f32 * scale).round() as u32).max(1);
let scaled_radius = neighbourhood_filters(effects, res)
let sampling_resolution = ((inp.tile_res() as f32 * scale).round() as u32).max(1);
let scaled_radius = neighbourhood_filters(effects, sampling_resolution)
.find(|filter| filter.0 == index)
.map_or(radius_texels, |filter| filter.1.round().clamp(1.0, radius_texels as f32) as u32);
if !is_box_blur(op) {
return gather(op, &orig, &cover, comps, (rx0, ry0, w, h), scaled_radius as f32, res, opacity);
return gather(op, &original_values, &coverage, component_count, (rx0, ry0, w, h), scaled_radius as f32, sampling_resolution, opacity);
}
// Weighted so uncovered texels don't bleed in
let (width, height, blur_radius) = (w as usize, h as usize, scaled_radius as usize);
let mut weights = cover.clone();
let mut weights = coverage.clone();
blur(&mut weights, width, height, blur_radius);
let mut data = orig.clone();
for component in 0..comps {
let mut sums: Vec<f32> = orig.par_chunks(comps).map(|pixel| pixel.get(component).copied().unwrap_or(0.0)).collect();
let mut data = original_values.clone();
for component in 0..component_count {
let mut sums: Vec<f32> = original_values
.par_chunks(component_count)
.with_min_len(MIN_PARALLEL_TEXELS)
.map(|pixel| pixel.get(component).copied().unwrap_or(0.0))
.collect();
blur(&mut sums, width, height, blur_radius);
data.par_chunks_mut(comps).zip(sums.par_iter()).zip(weights.par_iter()).for_each(|((pixel, sum), weight)| {
// Content is premultiplied, so the mid value is too (coverage itself is kept)
let cov = if comps == 4 { pixel.get(3).copied().unwrap_or(0.0) } else { 1.0 };
if *weight > 1e-6
&& let Some(slot) = pixel.get_mut(component)
{
let (original, blurred) = (*slot, sum / weight);
let result = match op {
Op::Blur(Some(amount)) => original + amount * (original - blurred),
Op::HighPass if component < 3 => original - blurred + mid * cov,
// Away from 0 by at most the value itself: 0 at the edge, full inside
Op::Bevel if component < 3 => original.signum() * original.abs().min((2.0 * blurred * original.signum() - original.abs()).max(0.0)),
Op::Glow(amount) if component < 3 => original + amount * blurred,
Op::HighPass | Op::Bevel | Op::Glow(_) => original,
_ => blurred,
};
*slot = original + (result - original) * opacity;
}
});
data.par_chunks_mut(component_count).with_min_len(MIN_PARALLEL_TEXELS).zip(sums.par_iter()).zip(weights.par_iter()).for_each(
|((pixel, sum), weight)| {
// Content is premultiplied, so the midpoint value is too (coverage itself is kept)
let alpha = if component_count == 4 { pixel.get(3).copied().unwrap_or(0.0) } else { 1.0 };
if *weight > MIN_FILTER_WEIGHT
&& let Some(slot) = pixel.get_mut(component)
{
let (original, blurred) = (*slot, sum / weight);
let result = match op {
Op::Blur(Some(amount)) => original + amount * (original - blurred),
Op::HighPass if component < 3 => original - blurred + midpoint * alpha,
// Away from 0 by at most the value itself: 0 at the edge, full inside
Op::Bevel if component < 3 => original.signum() * original.abs().min((2.0 * blurred * original.signum() - original.abs()).max(0.0)),
Op::Glow(amount) if component < 3 => original + amount * blurred,
Op::HighPass | Op::Bevel | Op::Glow(_) => original,
_ => blurred,
};
*slot = original + (result - original) * opacity;
}
},
);
}
data
};
@@ -733,9 +771,9 @@ fn filter_region(
let mut data = filtered(first);
for &scale in rest {
let other = filtered(scale);
let row = w as usize * comps;
let row = w as usize * component_count;
data.par_chunks_mut(row).zip(other.par_chunks(row)).enumerate().for_each(|(y, (dst, src))| {
for (x, (target, source)) in dst.chunks_mut(comps).zip(src.chunks(comps)).enumerate() {
for (x, (target, source)) in dst.chunks_mut(component_count).zip(src.chunks(component_count)).enumerate() {
if scales.at(rx0 + x as u32, ry0 + y as u32) == scale {
target.copy_from_slice(source);
}
@@ -743,17 +781,20 @@ fn filter_region(
});
}
unpremultiply(&mut data, comps);
Some(Field { x0: rx0, y0: ry0, w, h, comps, data })
unpremultiply(&mut data, component_count);
Some(Field { x0: rx0, y0: ry0, w, h, comps: component_count, data })
}
/// Clamp filtered masks to 0..1, and turn filtered content back into straight values.
fn unpremultiply(data: &mut [f32], comps: usize) {
data.par_chunks_mut(comps).for_each(|px| match px {
// Avoid amplifying rounding error near transparent texels
const MIN_COVERAGE: f32 = 1e-5;
data.par_chunks_mut(comps).with_min_len(MIN_PARALLEL_TEXELS).for_each(|px| match px {
[v] => *v = v.clamp(0.0, 1.0),
[r, g, b, cov] => {
*cov = cov.clamp(0.0, 1.0);
let inv = if *cov > 1e-5 { 1.0 / *cov } else { 0.0 };
let inv = if *cov > MIN_COVERAGE { 1.0 / *cov } else { 0.0 };
*r *= inv;
*g *= inv;
*b *= inv;
@@ -794,12 +835,14 @@ fn fields_of(inp: &Inputs, channels: &[Channel], list: &[LayerId], tiles: &[(u32
if layer.is_group() || neighbourhood(&layer.content_effects, working_width).is_none() {
continue;
}
for channel in channels.iter().filter(|channel| layer.channels.contains_key(channel)) {
if let Some(filtered) = field(inp, channels, &out, id, Job::Content(*channel), tiles) {
out.insert(FieldKey::Content(id, *channel), filtered);
}
}
}
out
}
@@ -1241,12 +1284,8 @@ impl<'a, 'b> Texel<'a, 'b> {
signal
};
// A smooth step from black to white over 2 × softness around 1 − level
let threshold = 1.0 - generator.level.clamp(0.0, 1.0);
let softness = generator.softness.max(1e-3);
let ramp = ((signal - (threshold - softness)) / (2.0 * softness)).clamp(0.0, 1.0);
let value = ramp * ramp * (3.0 - 2.0 * ramp);
if generator.invert { 1.0 - value } else { value }
// A smooth step from black to white over 2 × softness around 1 − level, then the offset
generator.shape(signal)
}
/// The curvature of `detail`'s mode at this texel: each detail size's edges and cavities, weighted and combined like a screen blend.
@@ -1665,7 +1704,7 @@ pub fn composite_tiles(inp: &Inputs, out: &mut Composite, tiles: &[(u32, u32)])
}
/// Tiles per side of the blocks a composite works through (bounds neighbourhood-filter memory).
const BLOCK: u32 = 16;
pub const BLOCK: u32 = 16;
/// [`composite_tiles`], serving everything but the hot layer from `below` where it can (and
/// filling it). Results are identical to [`composite_tiles`].
@@ -1674,9 +1713,10 @@ pub fn composite_tiles_cached(inp: &Inputs, out: &mut Composite, tiles: &[(u32,
if (inp.gbuf.width, inp.gbuf.height) != (out.resolution, out.height) {
return;
}
let split = below.as_deref_mut().and_then(|b| b.prepare(inp));
// A pass over more tiles than the cache holds records only what fits: recording the rest
// would just evict it again. Tiles hold the stack per level plus a tape of varying size.
// Avoid caching tiles that would immediately evict each other
let levels = split.as_ref().map_or(1, Vec::len);
let estimate = TILE_AREA * levels * set.channel_list().iter().map(|c| c.components()).sum::<usize>() * std::mem::size_of::<f32>();
let tile_bytes = below.as_deref().and_then(BelowCache::tile_bytes).unwrap_or(estimate).max(1);
@@ -1685,14 +1725,66 @@ pub fn composite_tiles_cached(inp: &Inputs, out: &mut Composite, tiles: &[(u32,
for t in tiles {
blocks.entry((t.1 / BLOCK, t.0 / BLOCK)).or_default().push(*t);
}
for block in blocks.values() {
let channels = set.channel_list();
let hot = split.as_deref().and_then(<[Level]>::last);
// A stroke preview is absent from StackKey, so it disables field retention
let retain_fields = below.is_some() && inp.preview.is_none() && has_neighbourhood(inp, &set.stack);
let key = if retain_fields { StackKey::new(inp.project, inp.pixels, inp.baked, inp.images, inp.ts, &set.stack).map(Arc::new) } else { None };
let size = (inp.gbuf.width, inp.gbuf.height);
// Release fields when a stroke or stack change disables retention
if key.is_none()
&& let Some(cache) = below.as_deref_mut()
{
cache.held = None;
}
for (block_index, block) in &blocks {
let uncached = |tile: &(u32, u32)| below.as_deref().zip(split.as_deref()).is_none_or(|(cache, _)| cache.tile(*tile).is_none());
let fresh: Vec<(u32, u32)> = block.iter().copied().filter(uncached).collect();
let held = below.as_deref().and_then(|cache| cache.held.as_ref()).filter(|held| {
key.as_ref().is_some_and(|key| **key == *held.key)
&& held.size == size
&& fresh.iter().all(|tile| held.stack_tiles.contains(tile))
&& block.iter().all(|tile| held.hot_tiles.contains(tile))
});
let ahead: Vec<(u32, u32)> = match (&key, below.as_deref()) {
(Some(_), Some(cache)) if held.is_none() => {
cache.ahead.iter().copied().filter(|tile| (tile.1 / BLOCK, tile.0 / BLOCK) == *block_index && !block.contains(tile)).collect()
}
_ => Vec::new(),
};
let fields: Arc<Fields> = match (held, &key) {
(Some(held), _) => held.fields.clone(),
(None, Some(key)) if !ahead.is_empty() => {
// Filter the current and following tiles as one run
let planned_tiles: Vec<(u32, u32)> = block.iter().chain(&ahead).copied().collect();
let uncached_planned_tiles: Vec<(u32, u32)> = planned_tiles.iter().copied().filter(uncached).collect();
let fields = Arc::new(block_fields(inp, &channels, &set.stack, hot, &uncached_planned_tiles, &planned_tiles));
let hot_tiles: std::collections::BTreeSet<(u32, u32)> = planned_tiles.iter().copied().collect();
let stack_tiles = if hot.is_some() && uncached_planned_tiles.len() < planned_tiles.len() {
uncached_planned_tiles.into_iter().collect()
} else {
hot_tiles.clone()
};
if let Some(cache) = below.as_deref_mut() {
cache.held = Some(HeldFields { key: key.clone(), size, stack_tiles, hot_tiles, fields: fields.clone() });
}
fields
}
_ => Arc::new(block_fields(inp, &channels, &set.stack, hot, &fresh, block)),
};
let quota = match below.as_deref() {
Some(_) if fits => usize::MAX,
Some(b) => b.limit().saturating_sub(b.bytes()) / tile_bytes,
None => 0,
};
let cached = below.as_deref().zip(split.as_deref());
let records = composite_block(inp, &set.stack, out, block, cached, quota);
let records = composite_block(inp, &set.stack, out, block, cached, quota, &fields);
if let Some(b) = below.as_deref_mut() {
for t in block {
b.touch(*t);
@@ -1702,6 +1794,35 @@ pub fn composite_tiles_cached(inp: &Inputs, out: &mut Composite, tiles: &[(u32,
}
}
}
// Release fields at the end of the pass
if let Some(cache) = below.filter(|cache| cache.ahead.is_empty()) {
cache.held = None;
}
}
/// Whether a layer drawn by `stack` has a neighbourhood filter in its mask or content effects.
fn has_neighbourhood(inp: &Inputs, stack: &[LayerId]) -> bool {
let working_width = inp.gbuf.width;
let mut order = Vec::new();
draw_order(inp.project, stack, 0, &mut order);
order.into_iter().any(|id| {
let masked = layer_mask(inp.project, id).is_some_and(|mask| mask.enabled && neighbourhood(&mask.effects, working_width).is_some());
masked || inp.project.content_layer(id).is_some_and(|layer| !layer.is_group() && neighbourhood(&layer.content_effects, working_width).is_some())
})
}
/// The neighbourhood fields compositing `tiles` reads: those of every layer of `stack` over the tiles the cache lacks (`fresh`),
/// and off the path to the `hot` layer only there; the hot layer's over all of `tiles`.
fn block_fields(inp: &Inputs, channels: &[Channel], stack: &[LayerId], hot: Option<&Level>, fresh: &[(u32, u32)], tiles: &[(u32, u32)]) -> Fields {
match hot {
Some(hot) if fresh.len() < tiles.len() => {
let mut fields = fields_of(inp, channels, stack, fresh);
fields.extend(fields_of(inp, channels, &[hot.at], tiles));
fields
}
_ => fields_of(inp, channels, stack, tiles),
}
}
/// Rows of a tile composited as one parallel task.
@@ -1723,6 +1844,7 @@ fn composite_block(
tiles: &[(u32, u32)],
below: Option<(&BelowCache, &[Level])>,
quota: usize,
fields: &Fields,
) -> Vec<((u32, u32), Vec<Band>)> {
let channels: Vec<Channel> = inp.project.texture_set(inp.ts).map(|s| s.channel_list()).unwrap_or_default();
let (w, h) = (out.resolution, out.height);
@@ -1730,20 +1852,12 @@ fn composite_block(
let cached = |t: (u32, u32)| below.and_then(|(b, _)| b.tile(t));
let fresh: Vec<(u32, u32)> = tiles.iter().copied().filter(|t| cached(*t).is_none()).collect();
let recorded: std::collections::BTreeSet<(u32, u32)> = if split.is_some() { fresh.iter().copied().take(quota).collect() } else { Default::default() };
// Off the path, layers are only evaluated on tiles the cache lacks.
let fields = match split.and_then(<[Level]>::last) {
Some(hot) if fresh.len() < tiles.len() => {
let mut fields = fields_of(inp, &channels, stack, &fresh);
fields.extend(fields_of(inp, &channels, &[hot.at], tiles));
fields
}
_ => fields_of(inp, &channels, stack, tiles),
};
let lanes: Vec<Lane> = channels.iter().map(|c| Lane { ch: c.index(), n: c.components(), signed: c.kind() == ChannelKind::Signed }).collect();
let stride: usize = lanes.iter().map(|l| l.n).sum();
let nlev = split.map_or(0, <[Level]>::len);
let level_count = split.map_or(0, <[Level]>::len);
let defaults: Acc = Channel::ALL.map(Channel::default_value);
// Tiles split into bands of rows, so a handful of tiles still keeps every thread busy.
// Tiles split into bands of rows, so a handful of tiles still keeps every thread busy
let bands = TILE / BAND;
let tasks: Vec<((u32, u32), u32)> = tiles.iter().flat_map(|t| (0..bands).map(move |b| (*t, b))).collect();
let mut results: Vec<BandResult> = tasks
@@ -1751,20 +1865,20 @@ fn composite_block(
.map(|&((tx, ty), band)| {
let warm = cached((tx, ty)).and_then(|b| b.get(band as usize));
let record = warm.is_none() && recorded.contains(&(tx, ty));
let levels = split.map(|l| plan_levels(inp, l, &channels, tx, ty, &fields, warm.is_some()));
let nodes = if levels.is_none() { plan(inp, stack, &channels, tx, ty, 0, Some(&fields)) } else { Vec::new() };
let levels = split.map(|l| plan_levels(inp, l, &channels, tx, ty, fields, warm.is_some()));
let nodes = if levels.is_none() { plan(inp, stack, &channels, tx, ty, 0, Some(fields)) } else { Vec::new() };
let rows = (BAND * TILE) as usize;
let mut accs = vec![defaults; nlev];
let mut masks = vec![0.0f32; nlev];
let mut level_values = vec![defaults; level_count];
let mut masks = vec![0.0f32; level_count];
let mut tape = match warm {
Some(b) => Tape::Replay(b, 0),
None if record => Tape::Record(Vec::new()),
None => Tape::Off,
};
// The levels whose stack the band stores (its header): a level with nothing drawn
// before the path layer repeats the level above it.
// Empty lower stacks reuse the enclosing level instead of storing a duplicate
let stored: Vec<bool> = match (&levels, warm) {
(Some(_), Some(_)) => tape.take(nlev).iter().map(|v| *v > 0.0).collect(),
(Some(_), Some(_)) => tape.take(level_count).iter().map(|v| *v > 0.0).collect(),
(Some(levels), None) => levels.iter().map(|l| !l.below.is_empty()).collect(),
(None, _) => Vec::new(),
};
@@ -1773,7 +1887,7 @@ fn composite_block(
}
let slots = stored.iter().filter(|s| **s).count() * stride;
let preview_tile = inp.preview.and_then(|p| p.alpha.tile(tx, ty));
let mut bufs: Vec<Vec<u16>> = channels.iter().map(|c| vec![0u16; rows * c.components()]).collect();
let mut channel_buffers: Vec<Vec<u16>> = channels.iter().map(|c| vec![0u16; rows * c.components()]).collect();
let mut anchors = Vec::new();
let mut any = false;
for ly in band * BAND..(band + 1) * BAND {
@@ -1786,20 +1900,20 @@ fn composite_block(
if x >= w {
break;
}
let gi = (y * w + x) as usize;
// Offset in the tile, and in this band.
let o = (ly * TILE + lx) as usize;
let ob = ((ly - band * BAND) * TILE + lx) as usize;
let covered = inp.gbuf.tri.get(gi).is_some_and(|t| *t != NO_TRI);
let gbuffer_index = (y * w + x) as usize;
let tile_offset = (ly * TILE + lx) as usize;
let band_offset = ((ly - band * BAND) * TILE + lx) as usize;
let covered = inp.gbuf.tri.get(gbuffer_index).is_some_and(|t| *t != NO_TRI);
let mut acc = defaults;
if covered {
any = true;
let mut tex = Texel::new(inp, x, y, o, stroke_at(preview_tile, o));
// Reuse one anchor buffer per band.
tex.anchors = std::mem::take(&mut anchors);
let mut texel = Texel::new(inp, x, y, tile_offset, stroke_at(preview_tile, tile_offset));
// Reuse one anchor buffer per band
texel.anchors = std::mem::take(&mut anchors);
match &levels {
Some(levels) => {
// The texel's stream starts with the stack at each level.
// The texel's stream starts with the stack at each level
let start = match &mut tape {
Tape::Record(t) => {
t.resize(t.len() + slots, 0.0);
@@ -1808,38 +1922,39 @@ fn composite_block(
_ => 0,
};
if warm.is_some() {
load_levels(tape.take(slots), &lanes, stride, &stored, &defaults, &mut accs);
load_levels(tape.take(slots), &lanes, stride, &stored, &defaults, &mut level_values);
}
acc = tex.eval_split(defaults, levels, &mut accs, &mut masks, warm.is_some(), &mut tape);
acc = texel.eval_split(defaults, levels, &mut level_values, &mut masks, warm.is_some(), &mut tape);
if let Tape::Record(t) = &mut tape
&& let Some(below) = t.get_mut(start..start + slots)
{
store_levels(below, &lanes, stride, &stored, &accs);
store_levels(below, &lanes, stride, &stored, &level_values);
}
}
None => {
tex.eval(&nodes, &mut acc);
texel.eval(&nodes, &mut acc);
}
}
anchors = tex.anchors;
anchors = texel.anchors;
anchors.clear();
}
for (l, buf) in lanes.iter().zip(&mut bufs) {
for (l, buf) in lanes.iter().zip(&mut channel_buffers) {
let v = acc.get(l.ch).copied().unwrap_or([0.0; 3]);
for (k, x) in v.iter().take(l.n).enumerate() {
let enc = if l.signed { to_u16_signed(*x) } else { to_u16(*x) };
if let Some(slot) = buf.get_mut(ob * l.n + k) {
*slot = enc;
let encoded = if l.signed { to_u16_signed(*x) } else { to_u16(*x) };
if let Some(slot) = buf.get_mut(band_offset * l.n + k) {
*slot = encoded;
}
}
}
}
}
let cache = if let Tape::Record(t) = tape { Some(t) } else { None };
(bufs, cache, any)
(channel_buffers, cache, any)
})
.collect();
// Join the bands of each tile (in parallel; uncovered tiles are freed).
// Join the bands of each tile (in parallel; uncovered tiles are freed)
let joined: Vec<Joined> = results
.par_chunks(bands as usize)
.map(|parts| {
@@ -1865,7 +1980,8 @@ fn composite_block(
img.set_tile(tx, ty, d);
}
}
// Tiles without covered texels cost nothing to composite: not worth caching.
// Tiles without covered texels cost nothing to composite: not worth caching
if any && let Some(bands) = parts.iter_mut().map(|p| p.1.take()).collect::<Option<Vec<Band>>>() {
records.push(((tx, ty), bands));
}
@@ -2327,6 +2443,53 @@ mod tests {
(0..3).all(|i| (a[i] - b[i]).abs() < 2e-4)
}
#[test]
fn retained_filter_buffers_release_after_the_last_tile() {
let mut fixture = fixture();
const RESOLUTION: u32 = 128;
fixture.gbuf = UvGBuffer::new(&fixture.mesh, 0, RESOLUTION, RESOLUTION);
fixture.project.texture_sets[0] = TextureSet::new("Plane", 0, RESOLUTION);
let layer = fill_layer(&mut fixture.project, [0.4; 3], 0.5);
let mut mask = Mask::new(MaskBackground::White);
mask.effects.push(Effect::new(fixture.project.alloc_id(), EffectKind::Filter(Filter::Blur { radius: 8.0 })));
fixture.project.layer_mut(layer).unwrap().mask = Some(mask);
let second_layer = fill_layer(&mut fixture.project, [0.2; 3], 0.7);
let mut second_mask = Mask::new(MaskBackground::White);
second_mask.effects.push(Effect::new(fixture.project.alloc_id(), EffectKind::Filter(Filter::Blur { radius: 4.0 })));
fixture.project.layer_mut(second_layer).unwrap().mask = Some(second_mask);
let images = ImageStore::default();
let input = Inputs {
project: &fixture.project,
ts: 0,
mesh: &fixture.mesh,
gbuf: &fixture.gbuf,
pixels: &fixture.pixels,
baked: &fixture.baked,
block_scales: fixture.project.texture_sets[0].block_scales(),
preview: None,
images: &images,
bounds: fixture.mesh.bounds(),
};
let mut output = Composite::new(&fixture.project.texture_sets[0].channel_list(), RESOLUTION);
let mut cache = BelowCache::default();
cache.ahead = vec![(1, 0), (0, 1), (1, 1)];
composite_tiles_cached(&input, &mut output, &[(0, 0)], Some(&mut cache));
let retained = cache.held.as_ref().expect("filter fields should be retained while tiles follow");
let field_bytes: usize = retained.fields.values().map(|field| field.data.capacity() * std::mem::size_of::<f32>()).sum();
let field_count = retained.fields.len();
let weak_fields = Arc::downgrade(&retained.fields);
assert_eq!(field_count, 2);
assert!(field_bytes > 0);
println!("retained fields: {field_count}, buffer capacity: {field_bytes} bytes");
cache.ahead.clear();
composite_tiles_cached(&input, &mut output, &[(1, 0)], Some(&mut cache));
assert!(weak_fields.upgrade().is_none(), "the last tile must release retained field buffers");
}
#[test]
fn histogram_samples_spread_over_every_column() {
assert_eq!(histogram_tiles(1024, 1024), all_tiles(1024, 1024), "small sets are read whole");
+24
View File
@@ -0,0 +1,24 @@
use std::collections::BTreeSet;
use std::sync::Arc;
use crate::below::StackKey;
use crate::eval::Fields;
/// Neighbourhood-filter fields kept between the composites of one pass.
pub(crate) struct HeldFields {
/// Everything the fields read.
pub key: Arc<StackKey>,
/// The working size they were built at.
pub size: (u32, u32),
/// The tiles every layer's fields hold.
pub stack_tiles: BTreeSet<(u32, u32)>,
/// The tiles the hot layer's fields hold (the same tiles when the stack was not split).
pub hot_tiles: BTreeSet<(u32, u32)>,
pub fields: Arc<Fields>,
}
impl std::fmt::Debug for HeldFields {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HeldFields").field("stack_tiles", &self.stack_tiles.len()).field("hot_tiles", &self.hot_tiles.len()).finish_non_exhaustive()
}
}
+19
View File
@@ -0,0 +1,19 @@
use std::sync::{Arc, Weak};
use crate::store::ImageRes;
/// Keeps an image's allocation identity stable without retaining pixels after resource removal.
#[derive(Debug)]
pub(crate) struct ImageIdentity(Weak<ImageRes>);
impl ImageIdentity {
pub(crate) fn new(image: &Arc<ImageRes>) -> Self {
Self(Arc::downgrade(image))
}
}
impl PartialEq for ImageIdentity {
fn eq(&self, other: &Self) -> bool {
self.0.ptr_eq(&other.0)
}
}
+4 -2
View File
@@ -13,6 +13,8 @@ mod curvature_size_level;
pub mod curvature_sizes;
pub mod display;
pub mod eval;
mod held_fields;
mod image_identity;
pub mod pattern;
pub mod store;
pub mod thumb;
@@ -20,8 +22,8 @@ pub mod thumb;
pub use below::{BelowCache, StackKey};
pub use display::{DisplayKind, tile_bytes};
pub use eval::{
Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, covered_tiles, effect_histogram, flatten_layer,
mask_at, mask_image, pad_tiles,
BLOCK, Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, covered_tiles, effect_histogram,
flatten_layer, mask_at, mask_image, pad_tiles,
};
pub use store::{BakedMaps, ImageRes, ImageStore, PixelStore};
pub use thumb::{THUMB_TEXELS, ThumbImage, ThumbTarget};
+226
View File
@@ -573,3 +573,229 @@ fn gathering_filters_composite_identically_in_any_grouping_and_cached() {
assert!(!check_cached(&f, 0, hot).is_empty());
}
}
/// Composite every tile in chunks of `chunk`, telling the cache the `ahead` tiles that follow each chunk, as a pass spread over frames does.
fn run_sliced(fixture: &Fixture, cache: &mut BelowCache, chunk: usize, ahead: usize) -> Composite {
let all = all_tiles(RES, RES);
let (images, baked) = (Default::default(), Default::default());
let mut out = Composite::new(&fixture.project.texture_sets[0].channel_list(), RES);
for (index, tiles) in all.chunks(chunk).enumerate() {
let done = (index * chunk + tiles.len()).min(all.len());
cache.ahead = all.iter().skip(done).take(ahead).copied().collect();
composite_tiles_cached(&inputs(fixture, 0, &images, &baked), &mut out, tiles, Some(&mut *cache));
assert_eq!(cache.holds_fields(), !cache.ahead.is_empty(), "fields are kept exactly while tiles follow (chunk {chunk}, ahead {ahead})");
}
out
}
#[test]
fn filter_fields_kept_across_the_chunks_of_a_pass_composite_exactly() {
let mut fixture = fixture();
let top = heavy_stack(&mut fixture);
let blurred_fill = fixture.project.texture_sets[0].stack[1];
let expected = reference(&fixture, 0);
for hot in [None, Some(top), Some(blurred_fill)] {
// Exercise reuse within and recomputation beyond retained runs
for (chunk, ahead) in [(1, 1), (1, 5), (3, 4), (5, 16), (2, 0)] {
let mut cache = BelowCache::default();
cache.set_hot(hot);
assert!(run_sliced(&fixture, &mut cache, chunk, ahead) == expected, "a cold sliced pass differs (hot {hot:?}, chunk {chunk}, ahead {ahead})");
// A warm hot-layer cache needs fewer fields
assert!(run_sliced(&fixture, &mut cache, chunk, ahead) == expected, "a warm sliced pass differs (hot {hot:?}, chunk {chunk}, ahead {ahead})");
}
}
// Evicted tiles need fields from every layer again
let mut full = BelowCache::default();
full.set_hot(Some(top));
run(&fixture, 0, &all_tiles(RES, RES), Some(&mut full));
let tile_bytes = full.tile_bytes().unwrap();
for tiles_held in [2, 5, 9] {
for (chunk, ahead) in [(1, 5), (3, 4), (2, 16)] {
let mut cache = BelowCache::default();
cache.budget = tiles_held * tile_bytes;
cache.set_hot(Some(top));
for pass in 0..3 {
assert!(
run_sliced(&fixture, &mut cache, chunk, ahead) == expected,
"pass {pass} differs with {tiles_held} cached tiles (chunk {chunk}, ahead {ahead})"
);
}
}
}
}
#[test]
fn kept_filter_fields_are_not_used_after_an_edit_or_under_a_live_stroke() {
let mut fixture = fixture();
let top = heavy_stack(&mut fixture);
let all = all_tiles(RES, RES);
let (first, rest) = all.split_at(3);
let mut cache = BelowCache::default();
cache.set_hot(Some(top));
// Keep fields for the remaining tiles
cache.ahead = rest.to_vec();
let mut out = run(&fixture, 0, first, Some(&mut cache));
assert!(cache.holds_fields());
// Changing the hot mask must invalidate the retained fields
let mask = fixture.project.layer_mut(top).unwrap().mask.as_mut().unwrap();
mask.effects[1].kind = EffectKind::Filter(Filter::Blur { radius: 6.0 });
let (images, baked) = (Default::default(), Default::default());
cache.ahead = Vec::new();
composite_tiles_cached(&inputs(&fixture, 0, &images, &baked), &mut out, &all, Some(&mut cache));
assert!(out == reference(&fixture, 0), "fields kept from before an edit were used after it");
assert!(!cache.holds_fields());
// A live stroke must bypass retained fields
let preview = stroke(top);
cache.ahead = rest.to_vec();
let mut live = run_with(&fixture, 0, first, Some(&mut cache), Some(&preview));
assert!(!cache.holds_fields());
let mut inp = inputs(&fixture, 0, &images, &baked);
inp.preview = Some(&preview);
composite_tiles_cached(&inp, &mut live, rest, Some(&mut cache));
assert!(live == run_with(&fixture, 0, &all, None, Some(&preview)), "a live stroke over a sliced pass differs");
}
/// Kept fields hold a layer off the hot path only over the tiles the cache lacked when they were built. A tile the cache drops
/// while they are in use needs that layer's fields again.
#[test]
fn tiles_the_cache_drops_during_a_kept_run_are_filtered_again() {
const SIZE: u32 = 512;
let mesh = primitives::sphere(1.0, 32, 16);
let gbuf = UvGBuffer::new(&mesh, 0, SIZE, SIZE);
let mut project = Project::new("t", "builtin:sphere");
project.texture_sets.push(TextureSet::new("A", 0, SIZE));
let mut fixture = Fixture { project, mesh, gbuf, pixels: PixelStore::default() };
add(&mut fixture, 0, fill(&[(Channel::BaseColor, procedural(ProceduralKind::Clouds, 0.1, 0.9))]), None);
// Pattern detail exposes any missing blur field
let mut blurred = Mask::new(MaskBackground::Black);
blurred.effects.push(effect(&mut fixture, EffectKind::Fill { source: procedural(ProceduralKind::Checker, 0.0, 1.0), projection: Projection::default() }));
blurred.effects.push(effect(&mut fixture, EffectKind::Filter(Filter::Blur { radius: 40.0 })));
add(&mut fixture, 0, fill(&[(Channel::Roughness, procedural(ProceduralKind::Grunge, 0.0, 1.0))]), Some(blurred));
// Keep the blurred layer visible under the hot layer
let hot = add(&mut fixture, 0, LayerKind::Paint, None);
paint_rect_at(&mut fixture, hot, SIZE, (10, 10), (40, 40), 0.2);
let mut all = all_tiles(SIZE, SIZE);
all.sort_by_key(|&(x, y)| (y, x));
let channels = fixture.project.texture_sets[0].channel_list();
let (images, baked) = (Default::default(), Default::default());
let mut expected = Composite::new(&channels, SIZE);
composite_tiles(&inputs(&fixture, 0, &images, &baked), &mut expected, &all);
// Prime the cache with only the two bottom rows
let rows = (SIZE / TILE) as usize;
let (above, bottom) = all.split_at(all.len() - 2 * rows);
let mut cache = BelowCache::default();
cache.set_hot(Some(hot));
let mut out = Composite::new(&channels, SIZE);
composite_tiles_cached(&inputs(&fixture, 0, &images, &baked), &mut out, bottom, Some(&mut cache));
assert_eq!(cache.len(), bottom.len());
cache.budget = cache.bytes();
// Cache eviction during the run must rebuild missing lower-layer fields
let mut out = Composite::new(&channels, SIZE);
for (index, tile) in all.iter().enumerate() {
cache.ahead = all.iter().skip(index + 1).copied().collect();
composite_tiles_cached(&inputs(&fixture, 0, &images, &baked), &mut out, &[*tile], Some(&mut cache));
if index == 0 {
assert!(cache.holds_fields());
}
}
assert!(above.len() > bottom.len());
assert!(out == expected, "a tile dropped from the cache was composited with fields that lack the layers off the hot path");
}
/// A stroke that starts while fields are kept must not leave them allocated for as long as it lasts, even though tiles still follow.
#[test]
fn a_live_stroke_releases_the_fields_kept_before_it_started() {
let mut fixture = fixture();
let top = heavy_stack(&mut fixture);
let all = all_tiles(RES, RES);
let (first, rest) = all.split_at(3);
let mut cache = BelowCache::default();
cache.set_hot(Some(top));
cache.ahead = rest.to_vec();
run(&fixture, 0, first, Some(&mut cache));
assert!(cache.holds_fields());
// A stroke must release fields even while tiles remain
let preview = stroke(top);
let mut live = run_with(&fixture, 0, first, Some(&mut cache), Some(&preview));
assert!(!cache.holds_fields(), "fields kept before the stroke stay allocated while it lasts");
let (images, baked) = (Default::default(), Default::default());
let mut inp = inputs(&fixture, 0, &images, &baked);
inp.preview = Some(&preview);
composite_tiles_cached(&inp, &mut live, rest, Some(&mut cache));
assert!(live == run_with(&fixture, 0, &all, None, Some(&preview)), "a stroke that starts during a sliced pass differs");
}
#[test]
fn cached_stack_tracks_referenced_images_only() {
use std::sync::Arc;
use texturecraft_compose::{ImageRes, ImageStore};
let mut fixture = fixture();
let hot_layer = heavy_stack(&mut fixture);
let bottom_layer = fixture.project.texture_sets[0].stack[0];
let LayerKind::Fill(fill) = &mut fixture.project.layer_mut(bottom_layer).unwrap().kind else { panic!("expected fill") };
fill.sources.insert(Channel::Roughness, Source::Image { name: "used".into(), normal_format: Default::default() });
let image_with_value = |value| Arc::new(ImageRes { width: RES, height: RES, rgba: vec![[value; 4]; (RES * RES) as usize], srgb: false });
let mut images = ImageStore::default();
let baked = Default::default();
let tiles = all_tiles(RES, RES);
let mut cache = BelowCache::default();
cache.set_hot(Some(hot_layer));
let mut output = Composite::new(&fixture.project.texture_sets[0].channel_list(), RES);
let mut previous_image = None;
for stage in ["cold", "resolve", "unused1", "unused2", "unused3", "replace", "remove"] {
match stage {
"resolve" => {
let image = image_with_value(1.0);
previous_image = Some(Arc::downgrade(&image));
images.insert("used".into(), image);
}
"replace" => {
let replacement = image_with_value(0.4);
let replacement_weak = Arc::downgrade(&replacement);
images.insert("used".into(), replacement);
assert!(previous_image.as_ref().is_some_and(|old| old.upgrade().is_none()), "cached key must not retain replaced image pixels");
previous_image = Some(replacement_weak);
}
"remove" => {
images.remove("used");
assert!(previous_image.as_ref().is_some_and(|old| old.upgrade().is_none()), "cached key must not retain removed image pixels");
}
"cold" => {}
_ => {
images.insert(stage.into(), image_with_value(0.5));
}
}
fixture.pixels.layer_mut(hot_layer, Channel::BaseColor, (RES, RES)).set(5, 5, &[stage.len() as u16, 0, 0, u16::MAX]);
let input = inputs(&fixture, 0, &images, &baked);
composite_tiles_cached(&input, &mut output, &tiles[..1], Some(&mut cache));
assert!(!cache.is_empty(), "{stage}: first tile should be cached");
let retained = cache.len() - 1;
composite_tiles_cached(&input, &mut output, &tiles[1..], Some(&mut cache));
assert_eq!(retained, if stage.starts_with("unused") { tiles.len() - 1 } else { 0 }, "{stage}");
let mut expected = Composite::new(&fixture.project.texture_sets[0].channel_list(), RES);
composite_tiles(&input, &mut expected, &tiles);
assert!(output == expected, "{stage}");
}
}
+40 -1
View File
@@ -543,6 +543,9 @@ pub struct Generator {
pub noise_amount: f32,
pub noise_scale: f32,
pub seed: u32,
/// Added to the finished mask value and clamped to 0..1 (−1..1; 0 = unchanged).
#[serde(default)]
pub offset: f32,
/// Direction for `Position` / `Direction` generators (world space).
pub direction: [f32; 3],
pub invert: bool,
@@ -565,7 +568,19 @@ impl Generator {
GeneratorKind::EdgeWear => Some(CurvatureDetail { mode: CurvatureMode::Edges, weights: CurvatureDetail::EDGE_WEIGHTS }),
_ => None,
};
Generator { kind, level, softness, noise_amount, noise_scale: 8.0, seed: 7, direction: [0.0, 1.0, 0.0], invert: false, detail }
Generator { kind, level, softness, noise_amount, noise_scale: 8.0, seed: 7, offset: 0.0, direction: [0.0, 1.0, 0.0], invert: false, detail }
}
/// The mask value for a (noise-broken) signal `s`: the smooth threshold at `level` over
/// `softness`, inverted if asked, then shifted by `offset` (a non-finite offset counts as 0).
pub fn shape(&self, s: f32) -> f32 {
let th = 1.0 - self.level.clamp(0.0, 1.0);
let soft = self.softness.max(1e-3);
let t = ((s - (th - soft)) / (2.0 * soft)).clamp(0.0, 1.0);
let v = t * t * (3.0 - 2.0 * t);
let v = if self.invert { 1.0 - v } else { v };
let offset = if self.offset.is_finite() { self.offset } else { 0.0 };
(v + offset).clamp(0.0, 1.0)
}
}
@@ -1065,6 +1080,30 @@ impl Effect {
mod tests {
use super::*;
#[test]
fn generator_offset_shifts_the_mask_value() {
let mut g = Generator::new(GeneratorKind::Position);
let base = g.shape(0.2);
assert!(base < 0.5);
g.offset = 0.3;
assert!((g.shape(0.2) - (base + 0.3)).abs() < 1e-6, "the offset is added");
g.offset = 1.0;
assert_eq!(g.shape(0.2), 1.0, "and the result is clamped");
g.offset = -1.0;
assert_eq!(g.shape(0.9), 0.0);
}
#[test]
fn generator_non_finite_offset_counts_as_zero() {
let mut g = Generator::new(GeneratorKind::Position);
let base: Vec<f32> = [0.0, 0.2, 0.5, 0.9].iter().map(|&s| g.shape(s)).collect();
for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
g.offset = bad;
let got: Vec<f32> = [0.0, 0.2, 0.5, 0.9].iter().map(|&s| g.shape(s)).collect();
assert_eq!(got, base, "offset {bad} behaves like 0");
}
}
#[test]
fn curvature_generators_saved_before_detail_sizes_load_without_them() {
let mut saved = serde_json::to_value(Generator::new(GeneratorKind::Curvature)).unwrap();
+62 -4
View File
@@ -195,16 +195,26 @@ pub(crate) fn blend_param(cmd: &str, p: &Value, key: &str) -> Result<Option<Blen
}
/// A color: `[r, g, b]` (linear 0..1) or `"#rrggbb"` (sRGB hex, converted to linear).
pub(crate) fn color_param(p: &Value, key: &str) -> Option<[f32; 3]> {
color_of(p.get(key)?)
pub(crate) fn color_param(cmd: &str, p: &Value, key: &str) -> Result<Option<[f32; 3]>> {
match p.get(key) {
None | Some(Value::Null) => Ok(None),
Some(v) => color_of(v).map(Some).ok_or_else(|| bad(cmd, format!("`{key}` must be a color ([r, g, b] linear or \"#rrggbb\")"))),
}
}
/// A color value (see [`color_param`]).
pub(crate) fn color_of(v: &Value) -> Option<[f32; 3]> {
match v {
Value::Array(a) if a.len() >= 3 => {
let v: Vec<f32> = a.iter().take(3).map(|x| x.as_f64().unwrap_or(0.0) as f32).collect();
if v.iter().all(|x| x.is_finite()) { Some([v[0], v[1], v[2]]) } else { None }
let mut out = [0.0f32; 3];
for (i, x) in a.iter().take(3).enumerate() {
let f = x.as_f64()? as f32;
if !f.is_finite() {
return None;
}
out[i] = f;
}
Some(out)
}
Value::Number(n) => n.as_f64().map(|v| [v as f32; 3]).filter(|v| v[0].is_finite()),
Value::String(s) => {
@@ -218,3 +228,51 @@ pub(crate) fn color_of(v: &Value) -> Option<[f32; 3]> {
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_param_accepts_valid_forms() {
let p = json!({
"hex": "#336699",
"hex_plain": "336699",
"rgb": [0.2, 0.4, 0.6],
"grey": 0.5,
});
assert!(color_param("cmd", &p, "hex").unwrap().is_some());
assert!(color_param("cmd", &p, "hex_plain").unwrap().is_some());
assert_eq!(color_param("cmd", &p, "rgb").unwrap(), Some([0.2, 0.4, 0.6]));
assert_eq!(color_param("cmd", &p, "grey").unwrap(), Some([0.5, 0.5, 0.5]));
assert_eq!(color_param("cmd", &p, "missing").unwrap(), None);
assert_eq!(color_param("cmd", &json!({"c": null}), "c").unwrap(), None);
}
#[test]
fn color_param_rejects_invalid_forms() {
let bad_cases = [
("#fff", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
("red", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
("#12345", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
("#gggggg", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
];
for (input, expected_err) in bad_cases {
let p = json!({ "color": input });
let err = color_param("test.cmd", &p, "color").unwrap_err();
assert_eq!(err.to_string(), format!("`test.cmd`: {expected_err}"));
}
let bad_array = json!({ "color": ["a", "b", "c"] });
assert!(color_param("test.cmd", &bad_array, "color").is_err());
let short_array = json!({ "color": [0.5, 0.5] });
assert!(color_param("test.cmd", &short_array, "color").is_err());
let overflow_array = json!({ "color": [1e40, 0.0, 0.0] });
assert!(color_param("test.cmd", &overflow_array, "color").is_err());
let overflow_scalar = json!({ "color": 1e40 });
assert!(color_param("test.cmd", &overflow_scalar, "color").is_err());
}
}
+3 -4
View File
@@ -253,8 +253,8 @@ pub(crate) fn apply(p: &Value, f: &mut Filter, cmd: &str) -> Result<()> {
}
Filter::ColorBalance { shadows, midtones, highlights } => {
for (key, slot) in [("shadows", shadows), ("midtones", midtones), ("highlights", highlights)] {
if p.get(key).is_some() {
let v = color_param(p, key).ok_or_else(|| bad(cmd, format!("`{key}` must be [r, g, b] offsets in -1..1")))?;
if let Some(val) = p.get(key).filter(|v| !v.is_null()) {
let v = color_of(val).ok_or_else(|| bad(cmd, format!("`{key}` must be [r, g, b] offsets in -1..1")))?;
*slot = v.map(|x| x.clamp(-1.0, 1.0));
}
}
@@ -305,8 +305,7 @@ pub(crate) fn apply(p: &Value, f: &mut Filter, cmd: &str) -> Result<()> {
}
Filter::ReplaceColor { from, to, tolerance, softness } => {
for (key, slot) in [("from", from), ("to", to)] {
if p.get(key).is_some() {
let v = color_param(p, key).ok_or_else(|| bad(cmd, format!("`{key}` must be a color ([r, g, b] linear or \"#rrggbb\")")))?;
if let Some(v) = color_param(cmd, p, key)? {
*slot = v.map(|x| x.clamp(0.0, 1.0));
}
}
+1 -1
View File
@@ -71,7 +71,7 @@ fn effect_json(session: &Session, effect: &Effect, content: bool) -> Value {
/// A generator's settings, with its curvature mode and detail size weights when it has them.
fn generator_json(generator: &Generator) -> Value {
let mut out = json!({ "type": format!("{:?}", generator.kind), "level": generator.level, "softness": generator.softness, "noise": generator.noise_amount, "invert": generator.invert });
let mut out = json!({ "type": format!("{:?}", generator.kind), "level": generator.level, "softness": generator.softness, "noise": generator.noise_amount, "offset": generator.offset, "invert": generator.invert });
if let Some(detail) = &generator.detail {
out["mode"] = json!(format!("{:?}", detail.mode));
out["sizes"] = json!(detail.weights);
+10 -6
View File
@@ -71,9 +71,9 @@ fn add_paint(s: &mut Session, p: &Value) -> Result<Value> {
}
/// A uniform fill from the `color`, `roughness`, `metallic`, `height` and `opacity` params.
pub(crate) fn fill_from(p: &Value) -> Fill {
pub(crate) fn fill_from(cmd: &str, p: &Value) -> Result<Fill> {
let mut fill = Fill::default();
if let Some(c) = color_param(p, "color") {
if let Some(c) = color_param(cmd, p, "color")? {
fill.sources.insert(Channel::BaseColor, Source::Uniform(c));
}
for (key, ch) in [("roughness", Channel::Roughness), ("metallic", Channel::Metallic), ("height", Channel::Height), ("opacity", Channel::Opacity)] {
@@ -81,13 +81,13 @@ pub(crate) fn fill_from(p: &Value) -> Fill {
fill.sources.insert(ch, Source::Uniform([v; 3]));
}
}
fill
Ok(fill)
}
fn add_fill(s: &mut Session, p: &Value) -> Result<Value> {
let ts = s.ts();
let name = strp(p, "name").map(str::to_string).unwrap_or_else(|| s.doc.project.next_layer_name(ts, "Fill layer"));
let fill = fill_from(p);
let fill = fill_from("layer.add.fill", p)?;
let id = s.edit("Add fill layer", |s| {
let id = s.doc.project.alloc_id();
insert_selected(s, Layer::new(id, name, LayerKind::Fill(fill), &channels_of(s)))
@@ -316,6 +316,10 @@ fn fill_set(s: &mut Session, p: &Value) -> Result<Value> {
let solo = boolp(p, "solo") == Some(true);
let enabled = if solo { Some(true) } else { boolp(p, "enabled") };
let normal_format = normal_format_param("layer.fill.set", p)?;
let low = color_param("layer.fill.set", p, "low")?.unwrap_or([0.0; 3]);
let high = color_param("layer.fill.set", p, "high")?.unwrap_or([1.0; 3]);
let color = color_param("layer.fill.set", p, "color")?;
let value = color_param("layer.fill.set", p, "value")?;
let source = if let Some(kind) = strp(p, "procedural") {
let kind = ProceduralKind::parse(kind).ok_or_else(|| bad("layer.fill.set", format!("unknown procedural `{kind}`")))?;
let mut pr = Procedural::new(kind);
@@ -332,8 +336,8 @@ fn fill_set(s: &mut Session, p: &Value) -> Result<Value> {
pr.balance = v.clamp(-1.0, 1.0);
}
pr.invert = boolp(p, "invert").unwrap_or(false);
Some(Source::Procedural { pattern: pr, low: color_param(p, "low").unwrap_or([0.0; 3]), high: color_param(p, "high").unwrap_or([1.0; 3]) })
} else if let Some(c) = color_param(p, "color").or_else(|| color_param(p, "value")) {
Some(Source::Procedural { pattern: pr, low, high })
} else if let Some(c) = color.or(value) {
Some(Source::Uniform(c))
} else if let Some(name) = strp(p, "image") {
Some(Source::Image { name: name.to_string(), normal_format: normal_format.unwrap_or_default() })
+1 -1
View File
@@ -41,7 +41,7 @@ mod path_draft;
pub mod path_tool;
mod polygon_fill;
mod polygon_fill_drag;
// Mesh reimport reads files: native only.
// Mesh reimport reads files on native targets only
#[cfg(not(target_arch = "wasm32"))]
mod preserve;
pub mod preset_cmds;
+27 -20
View File
@@ -383,38 +383,45 @@ impl Session {
/// With `merge`, this folds into the previous undo step when that was an edit of the same kind
/// (a parameter set while the cook ran).
pub fn apply_link_cook(&mut self, id: LayerId, bytes: &[u8], merge: bool) -> Result<()> {
let m = read_mcmat(bytes).map_err(|e| EngineError::Other(e.to_string()))?;
let maps = decoded_maps(&m)?;
let names = self.edit_merging(EDIT, merge, |s| {
let LayerKind::Fill(mut fill) = s.doc.project.layer(id)?.kind.clone() else {
let material = read_mcmat(bytes).map_err(|error| EngineError::Other(error.to_string()))?;
let maps = decoded_maps(&material)?;
let names = self.edit_merging(EDIT, merge, |session| {
let LayerKind::Fill(mut fill) = session.doc.project.layer(id)?.kind.clone() else {
return Err(EngineError::Other(format!("layer {} is not a fill", id.0)));
};
let link = fill.link.as_mut().ok_or_else(|| EngineError::Other(format!("layer {} is not a MaterialCraft material", id.0)))?;
for p in &mut link.params {
if let Some(cooked) = m.manifest.parameters.iter().find(|c| c.id == p.id) {
p.value = cooked.value.clone();
for parameter in &mut link.params {
if let Some(cooked) = material.manifest.parameters.iter().find(|candidate| candidate.id == parameter.id) {
parameter.value = cooked.value.clone();
}
}
link.size = m.manifest.size[0];
link.size = material.manifest.size[0];
let stem = texturecraft_formats::sanitize_resource_name(&link.name);
let old: Vec<String> =
fill.sources.values().filter_map(|src| if let Source::Image { name, .. } = src { Some(name.clone()) } else { None }).collect();
let names = put_maps(s, &stem, &maps, &mut fill);
s.doc.project.layer_mut(id)?.kind = LayerKind::Fill(fill);
// Old maps go unless another layer (a duplicate) still shows them.
let mut used = Vec::new();
for set in 0..s.doc.project.texture_sets.len() {
for root in s.doc.project.texture_sets.get(set).map(|t| t.stack.clone()).unwrap_or_default() {
crate::material_lib::images_used(&s.doc.project, root, &mut used, 0);
let old_names: Vec<String> =
fill.sources.values().filter_map(|source| if let Source::Image { name, .. } = source { Some(name.clone()) } else { None }).collect();
let names = put_maps(session, &stem, &maps, &mut fill);
session.doc.project.layer_mut(id)?.kind = LayerKind::Fill(fill);
// Duplicated layers may still use the earlier maps
let mut used_names = Vec::new();
for set in 0..session.doc.project.texture_sets.len() {
for root in session.doc.project.texture_sets.get(set).map(|texture_set| texture_set.stack.clone()).unwrap_or_default() {
crate::material_lib::images_used(&session.doc.project, root, &mut used_names, 0);
}
}
for name in old.iter().filter(|n| !used.contains(n)) {
s.doc.resources.remove(name);
for name in old_names.iter().filter(|name| !used_names.contains(name)) {
session.doc.resources.remove(name);
}
Ok(names)
})?;
self.sync_images();
// Reuse the validated maps before synchronization considers decoding missing images
self.cache_decoded(names, maps);
self.sync_images();
Ok(())
}
+8 -5
View File
@@ -49,7 +49,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Add generator",
["Layers", "Effects"],
None,
"{layer?, name?, generator?: edgeWear|dirt|thinAreas|curvature|ambientOcclusion|position|direction, level?, softness?, noise?, invert?, mode?: edges|cavities|both|raw, sizes?: [7 weights 0..1, finest first] (curvature and edgeWear)}",
"{layer?, name?, generator?: edgeWear|dirt|thinAreas|curvature|ambientOcclusion|position|direction, level?, softness?, noise?, offset?: -1..1 added to the result, invert?, mode?: edges|cavities|both|raw, sizes?: [7 weights 0..1, finest first] (curvature and edgeWear)}",
has_layer,
add_generator
),
@@ -94,7 +94,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Edit effect",
[],
None,
"{effect, visible?, blend?, opacity?, name?, filter?: id from filter.list (a filter effect only: swaps its type, keeping position, blend and opacity; its current id edits it in place), level?, softness?, noise?, noiseScale?, invert?, mode?, sizes?, value?, anchor?, anchorChannel?, inLow?, inHigh?, gamma?, outLow?, outHigh?, tolerance?, colors?: [[r,g,b], ...] (a color selection's ID colors), filter parameters (see effect.add.filter)}",
"{effect, visible?, blend?, opacity?, name?, filter?: id from filter.list (a filter effect only: swaps its type, keeping position, blend and opacity; its current id edits it in place), level?, softness?, noise?, noiseScale?, offset?, invert?, mode?, sizes?, value?, anchor?, anchorChannel?, inLow?, inHigh?, gamma?, outLow?, outHigh?, tolerance?, colors?: [[r,g,b], ...] (a color selection's ID colors), filter parameters (see effect.add.filter)}",
has_layer,
set
),
@@ -141,7 +141,7 @@ fn add_mask(s: &mut Session, p: &Value, bg: MaskBackground) -> Result<Value> {
}
fn add_color_mask(s: &mut Session, p: &Value) -> Result<Value> {
let color = color_param(p, "color").unwrap_or([1.0, 0.0, 0.0]);
let color = color_param("mask.add.colorSelection", p, "color")?.unwrap_or([1.0, 0.0, 0.0]);
let tolerance = f32p(p, "tolerance").unwrap_or(0.1).clamp(0.0, 1.0);
with_mask(s, p, "mask.add.colorSelection", "Add mask with color selection", |s, id| {
let eid = s.doc.project.alloc_id();
@@ -285,7 +285,7 @@ fn add_fill(s: &mut Session, p: &Value) -> Result<Value> {
let blend = blend_param("effect.add.fill", p, "blend")?.unwrap_or(BlendMode::Normal);
let id = layer_param(s, "effect.add.fill", p)?;
if content_target(p, "effect.add.fill")? {
let mut fill = crate::layer_cmds::fill_from(p);
let mut fill = crate::layer_cmds::fill_from("effect.add.fill", p)?;
if fill.sources.is_empty() {
fill.sources = s.doc.project.layer(id)?.channels.keys().map(|c| (*c, Source::uniform_of(*c))).collect();
}
@@ -325,6 +325,9 @@ fn generator_from(command: &str, params: &Value, generator: &mut Generator) -> R
if let Some(noise) = f32p(params, "noise") {
generator.noise_amount = noise.clamp(0.0, 2.0);
}
if let Some(offset) = f32p(params, "offset") {
generator.offset = offset.clamp(-1.0, 1.0);
}
if let Some(noise_scale) = f32p(params, "noiseScale") {
generator.noise_scale = noise_scale.clamp(0.01, 1000.0);
}
@@ -334,7 +337,7 @@ fn generator_from(command: &str, params: &Value, generator: &mut Generator) -> R
if let Some(invert) = boolp(params, "invert") {
generator.invert = invert;
}
if let Some(direction) = color_param(params, "direction") {
if let Some(direction) = color_param(command, params, "direction")? {
generator.direction = direction;
}
+1 -3
View File
@@ -343,9 +343,7 @@ impl Session {
let Some(cache) = self.caches.get_mut(active.ts) else { return };
let settings = &active.stroke.settings;
let pressure = pressure.clamp(0.0, 1.0);
let size_scale = if pressure < 1.0 { settings.min_size + (1.0 - settings.min_size) * pressure } else { 1.0 };
let radius = settings.size * 2.0 * size_scale;
let radius = settings.uv_stamp_radius(pressure);
let step = (settings.spacing * 2.0 * radius).max(MIN_UV_STAMP_STEP);
// Follow path: turn the stamp with the stroke direction (y up on screen, v grows down)
+17 -9
View File
@@ -368,6 +368,10 @@ impl Session {
/// Store the Path tool's curve (new, or the path being edited) and repaint its sublayer as one undo step, then clear the tool
pub fn commit_path(&mut self, viewport: Vec2) -> Result<Value> {
if self.quick_mask.editing {
return Err(EngineError::Other("paths can't edit the quick mask: stop editing it first".into()));
}
if let Some((effect, index)) = self.tool.path.editing
&& self.paths(effect).and_then(|paths| paths.get(index)).is_some_and(|stored| self.same_as_stored(stored))
{
@@ -396,20 +400,20 @@ impl Session {
let shape = self.path_shape(self.camera, viewport);
let editing = self.tool.path.editing;
let label = editing.map_or(NEW_PATH_LABEL.to_string(), |(_, i)| format!("Edit path {}", i + 1));
let out = self.edit_merging(&label, editing.is_some() && self.tool.path.edited, |s| {
let (eid, index) = match editing {
let report = self.edit_merging(&label, editing.is_some() && self.tool.path.edited, |session| {
let (effect_id, index) = match editing {
Some(at) => at,
None => (s.path_effect()?, usize::MAX),
None => (session.path_effect()?, usize::MAX),
};
s.put_path(eid, index, shape)
session.put_path(effect_id, index, shape)
})?;
if let Some(eid) = out.get("effect").and_then(Value::as_u64) {
self.target = StackTarget::Effect(LayerId(eid));
if let Some(effect_id) = report.get("effect").and_then(Value::as_u64) {
self.target = StackTarget::Effect(LayerId(effect_id));
}
self.tool.path.clear();
Ok(out)
Ok(report)
}
/// Repaint the edited path (one merged undo step) or a new path's preview (no step) when its points, brush or material changed; called every frame
@@ -429,9 +433,13 @@ impl Session {
/// Preview a command's points in its supplied viewport, or the UI's current viewport
fn path_live_in_view(&mut self, viewport: Vec2) -> Result<bool> {
if self.quick_mask.editing {
return Ok(false);
}
let stored = match self.tool.path.editing {
Some((eid, index)) => match self.paths(eid).and_then(|v| v.get(index)).cloned() {
Some(shape) => Some((eid, index, shape)),
Some((effect_id, index)) => match self.paths(effect_id).and_then(|paths| paths.get(index)).cloned() {
Some(shape) => Some((effect_id, index, shape)),
None => {
self.stop_path_edit();
return Ok(false);
+11 -11
View File
@@ -47,18 +47,18 @@ fn mask_enabled(session: &Session) -> std::result::Result<(), String> {
if session.quick_mask.enabled { Ok(()) } else { Err("enable the quick mask first".into()) }
}
fn state(s: &Session) -> Value {
let q = &s.quick_mask;
fn state(session: &Session) -> Value {
let mask = &session.quick_mask;
json!({
"enabled": q.enabled, "editing": q.editing, "inverted": q.inverted,
"paintedTiles": q.masks.get(&s.ts()).map(TileImage::allocated_count).unwrap_or(0),
"enabled": mask.enabled, "editing": mask.editing, "inverted": mask.inverted,
"paintedTiles": mask.masks.get(&session.ts()).map(TileImage::allocated_count).unwrap_or(0),
})
}
fn changed(s: &mut Session) -> Result<Value> {
s.quick_mask.revision = s.quick_mask.revision.wrapping_add(1);
s.revision = s.revision.wrapping_add(1);
Ok(state(s))
fn changed(session: &mut Session) -> Result<Value> {
session.quick_mask.revision = session.quick_mask.revision.wrapping_add(1);
session.revision = session.revision.wrapping_add(1);
Ok(state(session))
}
fn enable(session: &mut Session, params: &Value) -> Result<Value> {
@@ -79,9 +79,9 @@ fn edit(session: &mut Session, params: &Value) -> Result<Value> {
changed(session)
}
fn invert(s: &mut Session, _: &Value) -> Result<Value> {
s.quick_mask.inverted = !s.quick_mask.inverted;
changed(s)
fn invert(session: &mut Session, _: &Value) -> Result<Value> {
session.quick_mask.inverted = !session.quick_mask.inverted;
changed(session)
}
impl Session {
+49 -26
View File
@@ -21,7 +21,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Add fill layer from image",
[],
None,
"{name, channel?: baseColor, projection?: uv|triplanar|planar, scale?}",
"{name (exact resource name with extension or unique stem), channel?: baseColor, projection?: uv|triplanar|planar, scale?}",
always,
add_image_fill
),
@@ -38,7 +38,7 @@ pub(crate) fn decoded(d: texturecraft_formats::DecodedImage) -> Arc<ImageRes> {
impl Session {
/// Add an image resource from bytes (one undo step); returns its unique, sanitized name
pub fn import_resource_bytes(&mut self, file_name: &str, bytes: Vec<u8>) -> Result<String> {
self.edit("Import resource", |s| s.add_image_resource(file_name, bytes, u64::MAX))
self.edit("Import resource", |session| session.add_image_resource(file_name, bytes, texturecraft_formats::MAX_RESOURCE_TEXELS))
}
/// Add an image within a decoded texel budget, inside the caller's edit
@@ -47,39 +47,38 @@ impl Session {
return Err(EngineError::Other(format!("{file_name} is larger than {} MB", MAX_BYTES >> 20)));
}
let image = texturecraft_formats::decode_image_within(&bytes, max_texels).map_err(|e| EngineError::Other(format!("{file_name}: {e}")))?;
let leaf = std::path::Path::new(file_name).file_name().and_then(|n| n.to_str()).unwrap_or("image");
let image = texturecraft_formats::decode_image_within(&bytes, max_texels).map_err(|error| EngineError::Other(format!("{file_name}: {error}")))?;
let leaf = std::path::Path::new(file_name).file_name().and_then(|name| name.to_str()).unwrap_or("image");
let base = texturecraft_formats::sanitize_resource_name(leaf);
let mut name = base.clone();
let mut n = 2;
let mut duplicate_index = 2;
while self.doc.resources.contains_key(&name) {
name = format!("{n}_{base}");
n += 1;
name = format!("{duplicate_index}_{base}");
duplicate_index += 1;
}
self.doc.resources.insert(name.clone(), Arc::new(bytes));
self.images.insert(name.clone(), decoded(image));
Ok(name)
}
/// Decode resources that are not decoded yet and forget removed ones (after undo, redo, load).
pub fn sync_images(&mut self) {
let keep: Vec<String> = self.doc.resources.keys().cloned().collect();
self.images.retain(|k, _| keep.contains(k));
// Linked materials' packages are resources too, not images.
let missing: Vec<(String, Arc<Vec<u8>>)> = self
.doc
.resources
.iter()
.filter(|(k, _)| !self.images.contains_key(*k) && !k.ends_with(&format!(".{}", texturecraft_formats::mcmat::MCRAFT_EXT)))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
for (k, bytes) in missing {
match texturecraft_formats::decode_image(&bytes) {
Ok(d) => {
self.images.insert(k, decoded(d));
self.images.retain(|name, _| self.doc.resources.contains_key(name));
// Linked material packages are resources but cannot be decoded as images
let package_suffix = format!(".{}", texturecraft_formats::mcmat::MCRAFT_EXT);
for (name, bytes) in &self.doc.resources {
if self.images.contains_key(name) || name.ends_with(&package_suffix) {
continue;
}
match texturecraft_formats::decode_image(bytes) {
Ok(image) => {
self.images.insert(name.clone(), decoded(image));
}
Err(e) => self.log.push(format!("resource {k}: {e}")),
Err(error) => self.log.push(format!("resource {name}: {error}")),
}
}
}
@@ -179,11 +178,35 @@ fn remove_unused(s: &mut Session, _: &Value) -> Result<Value> {
Ok(json!({ "removed": unused }))
}
fn add_image_fill(s: &mut Session, p: &Value) -> Result<Value> {
let name = strp(p, "name").ok_or_else(|| bad("layer.add.image", "`name` is required"))?.to_string();
if !s.doc.resources.contains_key(&name) {
return Err(bad("layer.add.image", format!("no resource `{name}`")));
#[cfg(not(target_arch = "wasm32"))]
const IMPORT_HINT: &str = "resource.import";
#[cfg(target_arch = "wasm32")]
const IMPORT_HINT: &str = "File > Import resources";
/// Resolve an image resource by exact name or unique file stem.
fn resolve_resource_name(s: &Session, name: &str) -> Result<String> {
if s.images.contains_key(name) {
return Ok(name.to_string());
}
let matches: Vec<&String> = s.images.keys().filter(|k| std::path::Path::new(k).file_stem().and_then(|stem| stem.to_str()) == Some(name)).collect();
match matches.as_slice() {
[single] => Ok((*single).clone()),
[] => {
let available: Vec<&str> = s.images.keys().map(String::as_str).collect();
let hint =
if available.is_empty() { format!(" (no resources imported: use {IMPORT_HINT})") } else { format!(" (available: {})", available.join(", ")) };
Err(bad("layer.add.image", format!("no resource `{name}`{hint}")))
}
multiple => {
let matches_str = multiple.iter().map(|m| format!("`{m}`")).collect::<Vec<_>>().join(", ");
Err(bad("layer.add.image", format!("ambiguous resource `{name}` matches {matches_str}: give the full name with extension")))
}
}
}
fn add_image_fill(s: &mut Session, p: &Value) -> Result<Value> {
let raw_name = strp(p, "name").ok_or_else(|| bad("layer.add.image", "`name` is required"))?;
let name = resolve_resource_name(s, raw_name)?;
let ch = channel_param("layer.add.image", p, "channel")?.unwrap_or(Channel::BaseColor);
let projection = crate::layer_cmds::projection_from(p, "layer.add.image", None)?;
let ts = s.ts();
+126 -52
View File
@@ -571,23 +571,55 @@ impl Session {
self.invalidate_changed(other);
}
/// Mark dirty every texture set whose composite differs between `other` and the current
/// Mark dirty every texture set whose composite differs between the previous and current
/// document (after an edit, undo or redo).
pub(crate) fn invalidate_changed(&mut self, other: &Document) {
self.follow_uv_layouts(&other.project);
pub(crate) fn invalidate_changed(&mut self, previous_document: &Document) {
self.follow_uv_layouts(&previous_document.project);
self.sync_caches();
let resources = |d: &Document| d.resources.iter().map(|(n, b)| (n.clone(), Arc::as_ptr(b))).collect::<Vec<_>>();
let all = resources(other) != resources(&self.doc);
let (empty, images) = (BakedMaps::default(), Default::default());
let key = |d: &Document, ts: usize| {
let stack = d.project.texture_sets.get(ts).map(|s| s.stack.as_slice()).unwrap_or_default();
StackKey::new(&d.project, &d.pixels, d.baked.get(&ts).unwrap_or(&empty), &images, ts, stack)
// Removed or replaced resources may change any set's rendered content
let removed_or_replaced_resource =
previous_document.resources.iter().any(|(name, previous)| !self.doc.resources.get(name).is_some_and(|current| Arc::ptr_eq(previous, current)));
let added_resource_names: BTreeSet<_> = self.doc.resources.keys().filter(|name| !previous_document.resources.contains_key(*name)).collect();
let uses_added_resource = |texture_set_index: usize| {
use texturecraft_core::{EffectKind, Fill, Source};
let uses_source = |source: &Source| matches!(source, Source::Image { name, .. } if added_resource_names.contains(name));
let uses_fill =
|fill: &Fill| fill.sources.values().any(uses_source) || fill.link.as_ref().is_some_and(|link| added_resource_names.contains(&link.package));
let project = &self.doc.project;
!added_resource_names.is_empty()
&& project.texture_sets.get(texture_set_index).is_some_and(|set| {
project.render_tree(&set.stack).iter().filter_map(|id| project.layers.get(id)).any(|layer| {
matches!(&layer.kind, LayerKind::Fill(fill) if uses_fill(fill))
|| layer.mask.iter().flat_map(|mask| &mask.effects).chain(&layer.content_effects).any(|effect| match &effect.kind {
EffectKind::Fill { source, .. } => uses_source(source),
EffectKind::Material(fill) => uses_fill(fill),
_ => false,
})
})
})
};
for ts in 0..self.caches.len() {
if (all || key(other, ts) != key(&self.doc, ts))
&& let Some(c) = self.caches.get_mut(ts)
let (empty_baked_maps, empty_images) = (BakedMaps::default(), Default::default());
let stack_key = |document: &Document, texture_set_index: usize| {
let stack = document.project.texture_sets.get(texture_set_index).map(|set| set.stack.as_slice()).unwrap_or_default();
StackKey::new(
&document.project,
&document.pixels,
document.baked.get(&texture_set_index).unwrap_or(&empty_baked_maps),
&empty_images,
texture_set_index,
stack,
)
};
for texture_set_index in 0..self.caches.len() {
if (removed_or_replaced_resource
|| stack_key(previous_document, texture_set_index) != stack_key(&self.doc, texture_set_index)
|| uses_added_resource(texture_set_index))
&& let Some(cache) = self.caches.get_mut(texture_set_index)
{
c.invalidate_all();
cache.invalidate_all();
}
}
}
@@ -732,7 +764,8 @@ impl Session {
self.modified = true;
self.revision = self.revision.wrapping_add(1);
self.invalidate_changed(other);
// An undone add or duplicate takes the selected effect away: select the stack it was in.
// An undone add or duplicate takes the selected effect away
if let StackTarget::Effect(e) = self.target
&& self.doc.project.effect(e).is_none()
{
@@ -740,6 +773,10 @@ impl Session {
let content = p.effect(e).and_then(|(owner, _)| p.layers.get(&owner)).is_some_and(|l| l.content_effects.iter().any(|x| x.id == e));
self.target = if content { StackTarget::Content } else { StackTarget::Mask };
}
let has_mask = self.selected_layer().and_then(|lid| self.doc.project.layer(self.doc.project.mask_owner(lid)).ok()).is_some_and(|l| l.mask.is_some());
if self.target == StackTarget::Mask && !has_mask {
self.target = StackTarget::Content;
}
}
// -----------------------------------------------------------------------------------------
@@ -759,22 +796,24 @@ impl Session {
/// of a finite budget, after the dirty ones.
pub fn update_for(&mut self, budget_ms: f64, clock: impl Fn() -> f64) -> bool {
let start = clock();
let first = if budget_ms.is_finite() { rayon::current_num_threads().clamp(1, CHUNK) } else { CHUNK };
// The layer being edited: everything else is cached per tile.
let hot = self.stroke.as_ref().and_then(|s| s.target).map(|t| self.paint_owner(t)).or_else(|| self.selected_layer());
let stroke_size = self.stroke.as_ref().and_then(|s| self.caches.get(s.ts)).map(|c| c.size);
let preview = self.stroke.as_ref().and_then(|s| {
let initial_chunk_size = if budget_ms.is_finite() { rayon::current_num_threads().clamp(1, CHUNK) } else { CHUNK };
// The layer being edited: everything else is cached per tile
let hot = self.stroke.as_ref().and_then(|stroke| stroke.target).map(|target| self.paint_owner(target)).or_else(|| self.selected_layer());
let stroke_size = self.stroke.as_ref().and_then(|stroke| self.caches.get(stroke.ts)).map(|cache| cache.size);
let preview = self.stroke.as_ref().and_then(|stroke| {
Some(StrokePreview {
target: s.target?,
alpha: s.canvas.alpha.clone(),
opacity: s.stroke.settings.opacity,
erase: s.erase,
values: s.values.clone(),
target: stroke.target?,
alpha: stroke.canvas.alpha.clone(),
opacity: stroke.stroke.settings.opacity,
erase: stroke.erase,
values: stroke.values.clone(),
mask_value: self.tool.mask_value,
stencil: s.stencil.clone(),
carried: s.canvas.pickup.as_ref().map(|p| p.values.clone()).unwrap_or_default(),
stencil: stroke.stencil.clone(),
carried: stroke.canvas.pickup.as_ref().map(|pickup| pickup.values.clone()).unwrap_or_default(),
})
});
let active = self.ts();
let order = std::iter::once(active).chain((0..self.caches.len()).filter(|ts| *ts != active));
let mut changed = false;
@@ -782,24 +821,27 @@ impl Session {
let Some(cache) = self.caches.get_mut(ts) else { continue };
cache.below.set_hot(hot);
cache.below.pass = cache.dirty.len() + cache.settle.len();
// Instances in sets of the stroke's size show the live stroke too.
// Instances in sets of the stroke's size show the live stroke too
let preview = preview.as_ref().filter(|_| Some(cache.size) == stroke_size);
// Start at the size that fit the budget last time: with a heavy stack (smart
// materials at 8K) one tile per thread alone would take several frames' budget.
let mut chunk = if budget_ms.is_finite() && cache.chunk > 0 { cache.chunk } else { first };
// Reuse the last affordable chunk size for heavy stacks
let mut chunk = if budget_ms.is_finite() && cache.chunk > 0 { cache.chunk } else { initial_chunk_size };
loop {
if cache.dirty.is_empty() {
if cache.settle.is_empty() || clock() - start >= budget_ms.min(SETTLE_BUDGET_MS) {
break;
}
// Small chunks: the settle slice of the budget is short.
cache.dirty.extend(next_chunk(&mut cache.settle, chunk.min(first)));
// Small chunks: the settle slice of the budget is short
cache.dirty.extend(next_chunk(&mut cache.settle, chunk.min(initial_chunk_size)));
}
let t0 = clock();
let chunk_started = clock();
let tiles = next_chunk(&mut cache.dirty, chunk);
for t in &tiles {
cache.settle.remove(t);
for tile in &tiles {
cache.settle.remove(tile);
}
let empty = BakedMaps::default();
let inputs = texturecraft_compose::Inputs {
project: &self.doc.project,
@@ -813,30 +855,39 @@ impl Session {
images: &self.images,
bounds: self.mesh.bounds(),
};
// Look ahead so sliced updates filter a run of tiles once
cache.below.ahead = if budget_ms.is_finite() { tiles_ahead(&cache.dirty, &tiles) } else { Vec::new() };
texturecraft_compose::composite_tiles_cached(&inputs, &mut cache.composite, &tiles, Some(&mut cache.below));
// The composited tiles and the neighbours their padding reached change.
// The composited tiles and the neighbours their padding reached change
let padded = pad_tiles(&mut cache.composite, &cache.dilation, &tiles);
cache.upload.extend(tiles.iter().copied().chain(padded));
cache.revision = cache.revision.wrapping_add(1);
changed = true;
let now = clock();
let left = budget_ms - (now - start);
// As many tiles as fit in what is left, at the pace of this chunk.
let per_tile = (now - t0) / tiles.len().max(1) as f64;
if budget_ms.is_finite() && per_tile > 0.0 {
// Never above one tile per thread: work can turn heavy from one frame to the next.
cache.chunk = ((budget_ms / per_tile) as usize).clamp(1, first);
let remaining_ms = budget_ms - (now - start);
// Estimate how many tiles fit in the remaining frame budget
let ms_per_tile = (now - chunk_started) / tiles.len().max(1) as f64;
if budget_ms.is_finite() && ms_per_tile > 0.0 {
// Never above one tile per thread: work can turn heavy from one frame to the next
cache.chunk = ((budget_ms / ms_per_tile) as usize).clamp(1, initial_chunk_size);
}
if left <= 0.0 {
if remaining_ms <= 0.0 {
return changed;
}
let fit = if per_tile > 0.0 { (left / per_tile) as usize } else { CHUNK };
if fit == 0 {
let fitting_tiles = if ms_per_tile > 0.0 { (remaining_ms / ms_per_tile) as usize } else { CHUNK };
if fitting_tiles == 0 {
return changed;
}
chunk = fit.min(CHUNK);
chunk = fitting_tiles.min(CHUNK);
}
}
changed
}
@@ -959,19 +1010,42 @@ pub const SETTLE_BUDGET_MS: f64 = 6.0;
/// Take up to `n` dirty tiles, block by block (16 × 16 tiles) so neighbourhood filters
/// evaluate compact regions.
fn next_chunk(dirty: &mut BTreeSet<(u32, u32)>, n: usize) -> Vec<(u32, u32)> {
if dirty.len() <= n {
fn next_chunk(dirty: &mut BTreeSet<(u32, u32)>, max_tiles: usize) -> Vec<(u32, u32)> {
// A zero-sized request cannot select a pivot
if max_tiles == 0 {
return Vec::new();
}
if dirty.len() <= max_tiles {
return std::mem::take(dirty).into_iter().collect();
}
let mut tiles: Vec<(u32, u32)> = dirty.iter().copied().collect();
tiles.sort_by_key(|&(x, y)| (y / 16, x / 16, y, x));
tiles.truncate(n);
for t in &tiles {
dirty.remove(t);
// Partition by block order so only the requested chunk needs sorting
tiles.select_nth_unstable_by_key(max_tiles - 1, |&(x, y)| (y / texturecraft_compose::BLOCK, x / texturecraft_compose::BLOCK, y, x));
tiles.truncate(max_tiles);
tiles.sort_by_key(|&(x, y)| (y / texturecraft_compose::BLOCK, x / texturecraft_compose::BLOCK, y, x));
for tile in &tiles {
dirty.remove(tile);
}
tiles
}
/// Dirty tiles a neighbourhood filter runs over ahead of the chunk being composited.
const FILTER_AHEAD: usize = 64;
/// The dirty tiles composited after `chunk` in its 16 × 16 tile blocks, in the order [`next_chunk`] takes them, at most [`FILTER_AHEAD`].
fn tiles_ahead(dirty: &BTreeSet<(u32, u32)>, chunk: &[(u32, u32)]) -> Vec<(u32, u32)> {
let block = |&(x, y): &(u32, u32)| (y / texturecraft_compose::BLOCK, x / texturecraft_compose::BLOCK);
let blocks: BTreeSet<(u32, u32)> = chunk.iter().map(block).collect();
let mut ahead: Vec<(u32, u32)> = dirty.iter().copied().filter(|tile| blocks.contains(&block(tile))).collect();
ahead.sort_by_key(|tile| (block(tile), tile.1, tile.0));
ahead.truncate(FILTER_AHEAD);
ahead
}
/// Valid texture set resolutions: powers of two from 128 to 8192.
pub fn check_resolution(r: u32) -> Result<u32> {
if (128..=8192).contains(&r) && r.is_power_of_two() {
+1 -1
View File
@@ -100,7 +100,7 @@ fn add_text(s: &mut Session, p: &Value) -> Result<Value> {
let (font, index) = font_bytes(strp(p, "font"), strp(p, "style"))?;
let size = f32p(p, "size").unwrap_or(256.0).clamp(8.0, 1024.0);
let img = render_text(&font, index, text, size).map_err(|e| bad(cmd, e))?;
let rgb = color_param(p, "color").unwrap_or([1.0; 3]).map(|c| (texturecraft_texel::linear_to_srgb(c.clamp(0.0, 1.0)) * 255.0).round() as u8);
let rgb = color_param(cmd, p, "color")?.unwrap_or([1.0; 3]).map(|c| (texturecraft_texel::linear_to_srgb(c.clamp(0.0, 1.0)) * 255.0).round() as u8);
let png = texturecraft_formats::coverage_png(img.width, img.height, &img.coverage, rgb).map_err(|e| bad(cmd, e.to_string()))?;
// The resource is named after the text (its first line, shortened) unless a name is given.
let label: String = strp(p, "name").unwrap_or_else(|| text.lines().next().unwrap_or_default()).chars().take(32).collect();
+38 -31
View File
@@ -6,7 +6,7 @@ use texturecraft_core::Channel;
use texturecraft_geom::glam::Vec2;
use texturecraft_paint::{Alignment, BrushAlpha, MAX_RADIAL_COPIES, alphas};
use crate::commands::{CommandSpec, always, bad, boolp, channel_param, color_param, f32p, strp, vec3};
use crate::commands::{CommandSpec, always, bad, boolp, channel_param, color_of, color_param, f32p, strp, vec3};
use crate::fill_hit_test::FillHitTest;
use crate::fill_mode::FillMode;
use crate::session::{CloneFrom, CloneSource, StackTarget, Tool};
@@ -115,6 +115,7 @@ pub fn specs() -> Vec<CommandSpec> {
pub(crate) fn set_tool(session: &mut Session, tool: Tool) -> Result<Value> {
session.cancel_stroke();
// A box fill under way keeps what it filled as one undo step
session.end_polygon_fill_box();
session.tool.tool = tool;
@@ -285,11 +286,14 @@ pub(crate) fn brush_json(session: &Session) -> Value {
}
fn material(s: &mut Session, p: &Value) -> Result<Value> {
let channel = channel_param("tool.material", p, "channel")?;
let color = color_param("tool.material", p, "color")?;
let value = color_param("tool.material", p, "value")?;
if let Some(v) = f32p(p, "maskValue") {
s.tool.mask_value = v.clamp(0.0, 1.0);
}
if let Some(c) = channel_param("tool.material", p, "channel")? {
if let Some(v) = color_param(p, "color").or_else(|| color_param(p, "value")) {
if let Some(c) = channel {
if let Some(v) = color.or(value) {
let v = if c == Channel::Height { v.map(|x| x.clamp(-1.0, 1.0)) } else { v.map(|x| x.clamp(0.0, 1.0)) };
s.tool.values.insert(c, v);
s.tool.enabled.insert(c);
@@ -339,50 +343,53 @@ fn swap_mask_value(session: &mut Session, _: &Value) -> Result<Value> {
Ok(json!({ "maskValue": session.tool.mask_value }))
}
fn stencil(s: &mut Session, p: &Value) -> Result<Value> {
if boolp(p, "clear") == Some(true) {
s.tool.stencil = None;
fn stencil(session: &mut Session, params: &Value) -> Result<Value> {
if boolp(params, "clear") == Some(true) {
session.tool.stencil = None;
return Ok(Value::Null);
}
if let Some(name) = strp(p, "image") {
if !s.doc.resources.contains_key(name) {
if let Some(name) = strp(params, "image") {
if !session.doc.resources.contains_key(name) {
return Err(bad("tool.stencil", format!("no resource `{name}` (import it first)")));
}
let keep = s.tool.stencil.take();
let mut st = crate::session::StencilState::new(name);
if let Some(k) = keep {
st = crate::session::StencilState { image: name.to_string(), ..k };
let previous_stencil = session.tool.stencil.take();
let mut stencil_state = crate::session::StencilState::new(name);
if let Some(previous_stencil) = previous_stencil {
stencil_state = crate::session::StencilState { image: name.to_string(), ..previous_stencil };
}
s.tool.stencil = Some(st);
session.tool.stencil = Some(stencil_state);
}
let st = s.tool.stencil.as_mut().ok_or_else(|| bad("tool.stencil", "no stencil: give `image`"))?;
if let Some(c) = color_param(p, "center").map(|c| [c[0], c[1]]).or_else(|| pair(p, "center")) {
st.center = c.map(|v| v.clamp(-4.0, 4.0));
let stencil_state = session.tool.stencil.as_mut().ok_or_else(|| bad("tool.stencil", "no stencil: give `image`"))?;
if let Some(center) = pair(params, "center").or_else(|| params.get("center").and_then(color_of).map(|color| [color[0], color[1]])) {
stencil_state.center = center.map(|value| value.clamp(-4.0, 4.0));
}
if let Some(v) = f32p(p, "scale") {
st.scale = v.clamp(0.01, 10.0);
if let Some(scale) = f32p(params, "scale") {
stencil_state.scale = scale.clamp(0.01, 10.0);
}
if let Some(v) = f32p(p, "rotation") {
st.rotation = v.rem_euclid(360.0);
if let Some(rotation) = f32p(params, "rotation") {
stencil_state.rotation = rotation.rem_euclid(360.0);
}
if let Some(v) = boolp(p, "tile") {
st.tile = v;
if let Some(tile) = boolp(params, "tile") {
stencil_state.tile = tile;
}
if let Some(v) = f32p(p, "opacity") {
st.opacity = v.clamp(0.0, 1.0);
if let Some(opacity) = f32p(params, "opacity") {
stencil_state.opacity = opacity.clamp(0.0, 1.0);
}
s.revision = s.revision.wrapping_add(1);
Ok(serde_json::to_value(&s.tool.stencil).unwrap_or(Value::Null))
session.revision = session.revision.wrapping_add(1);
Ok(serde_json::to_value(&session.tool.stencil).unwrap_or(Value::Null))
}
/// A `[a, b]` param of two finite numbers.
fn pair(p: &Value, key: &str) -> Option<[f32; 2]> {
let a = p.get(key)?.as_array()?;
let v: Vec<f32> = a.iter().filter_map(Value::as_f64).map(|x| x as f32).filter(|x| x.is_finite()).collect();
match v.as_slice() {
[x, y] => Some([*x, *y]),
_ => None,
}
let [x, y] = a.as_slice() else { return None };
let x = x.as_f64()? as f32;
let y = y.as_f64()? as f32;
(x.is_finite() && y.is_finite()).then_some([x, y])
}
fn clone_source(s: &mut Session, p: &Value) -> Result<Value> {
+2 -1
View File
@@ -211,6 +211,7 @@ fn display(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(e) = env.as_deref().filter(|e| !e.starts_with(texturecraft_core::project::ENV_RESOURCE_PREFIX) && !ENVIRONMENTS.contains(e)) {
return Err(bad("display.set", format!("unknown environment `{e}`: one of {}, or resource:<image>", ENVIRONMENTS.join(", "))));
}
let scatter_color = color_param("display.set", p, "scatterColor")?;
let d = &mut s.doc.project.display;
if let Some(e) = env {
d.environment = e;
@@ -236,7 +237,7 @@ fn display(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(t) = tm {
d.tone_mapping = t;
}
if let Some(c) = color_param(p, "scatterColor") {
if let Some(c) = scatter_color {
d.scatter_color = c.map(|v| v.clamp(0.0, 1.0));
}
if let Some(b) = boolp(p, "shadows") {
+47 -1
View File
@@ -2,7 +2,7 @@
//! frame-budgeted compositor, through engine commands.
use serde_json::{Value, json};
use texturecraft_engine::core::{Channel, LayerId, LayerKind};
use texturecraft_engine::core::{Channel, Fill, LayerId, LayerKind, Source};
use texturecraft_engine::geom::glam::Vec2;
use texturecraft_engine::geom::primitives;
use texturecraft_engine::{Session, execute};
@@ -206,6 +206,52 @@ fn edits_invalidate_only_the_sets_they_change() {
assert!((rough(&mut s, 0) - 0.4).abs() < 1e-3);
}
#[test]
fn importing_images_invalidates_only_sets_with_newly_resolved_references() {
use std::sync::Arc;
let mut session = session();
let referenced_layer = session.doc.project.texture_sets[1].stack[0];
session
.edit("Reference missing image", |session| {
let mut fill = Fill::default();
fill.sources.insert(Channel::BaseColor, Source::image("later.png"));
session.doc.project.layer_mut(referenced_layer).unwrap().kind = LayerKind::Fill(fill);
Ok(())
})
.unwrap();
session.update();
let mut image_bytes = Vec::new();
let image = image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255]));
image.write_to(&mut std::io::Cursor::new(&mut image_bytes), image::ImageFormat::Png).unwrap();
session.import_resource_bytes("later.png", image_bytes.clone()).unwrap();
assert!(session.caches[0].dirty.is_empty(), "unrelated set stays clean");
assert!(!session.caches[1].dirty.is_empty(), "newly resolved image invalidates its set");
session.update();
let (x, y) = covered(&session, 1);
assert!(session.caches[1].composite.value(Channel::BaseColor, x, y)[0] > 0.9);
let imported_image = Arc::downgrade(&session.images["later.png"]);
session.undo().unwrap();
assert!(!session.caches[0].dirty.is_empty(), "resource removal retains broad invalidation");
assert!(!session.caches[1].dirty.is_empty());
assert!(imported_image.upgrade().is_none(), "undo releases the decoded image even while encoded bytes remain in history");
session.update();
session.redo().unwrap();
assert!(session.caches[0].dirty.is_empty());
assert!(!session.caches[1].dirty.is_empty());
assert!(!imported_image.ptr_eq(&Arc::downgrade(&session.images["later.png"])), "redo decodes a new image allocation");
session.update();
session.import_resource_bytes("unused.png", image_bytes).unwrap();
assert!(session.caches.iter().all(|cache| cache.dirty.is_empty()), "unused image leaves every set clean");
let resolved_image = Arc::downgrade(&session.images["later.png"]);
session.sync_images();
assert!(resolved_image.ptr_eq(&Arc::downgrade(&session.images["later.png"])), "sync reuses already decoded images");
}
#[test]
fn budgeted_updates_spread_work_and_end_identical() {
let mut s = session();
+39 -31
View File
@@ -108,47 +108,55 @@ fn imports_as_a_linked_fill_with_its_maps() {
/// An image marked DirectX keeps the mark when a re-cook replaces it.
#[test]
fn recooks_keep_the_normal_format_of_a_map() {
let mut s = session();
let id = s.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
execute(&mut s, "layer.fill.set", &json!({"layer": id.0, "channel": "roughness", "normalFormat": "directX"})).unwrap();
s.apply_link_cook(id, &mcmat(0.75, 0.5), false).unwrap();
let LayerKind::Fill(fill) = &s.doc.project.layer(id).unwrap().kind else { panic!("a fill") };
let mut session = session();
let id = session.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
execute(&mut session, "layer.fill.set", &json!({"layer": id.0, "channel": "roughness", "normalFormat": "directX"})).unwrap();
session.apply_link_cook(id, &mcmat(0.75, 0.5), false).unwrap();
let LayerKind::Fill(fill) = &session.doc.project.layer(id).unwrap().kind else { panic!("a fill") };
assert!(matches!(fill.sources.get(&Channel::Roughness), Some(Source::Image { normal_format: NormalFormat::DirectX, .. })));
}
#[test]
fn parameters_are_checked_stored_and_recooked_in_one_undo_step() {
let mut s = session();
let id = s.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
let set = |s: &mut Session, p: Value| execute(s, "material.link.set", &p);
assert!(set(&mut s, json!({"id": "nope", "value": 1, "cook": false})).is_err());
assert!(set(&mut s, json!({"id": "wear", "value": "soft", "cook": false})).is_err());
assert!(set(&mut s, json!({"id": "wear", "value": f64::NAN, "cook": false})).is_err());
set(&mut s, json!({"id": "tint", "value": "#ff0000", "cook": false})).unwrap();
assert_eq!(link(&s, id).params[1].value, json!({"t": "color", "v": [1.0, 0.0, 0.0, 1.0]}));
set(&mut s, json!({"id": "wear", "value": 0.9, "cook": false})).unwrap();
assert_eq!(link(&s, id).params[0].value, json!({"t": "float", "v": 0.9}));
let mut session = session();
let id = session.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
let set = |session: &mut Session, params: Value| execute(session, "material.link.set", &params);
assert!(set(&mut session, json!({"id": "nope", "value": 1, "cook": false})).is_err());
assert!(set(&mut session, json!({"id": "wear", "value": "soft", "cook": false})).is_err());
assert!(set(&mut session, json!({"id": "wear", "value": f64::NAN, "cook": false})).is_err());
set(&mut session, json!({"id": "tint", "value": "#ff0000", "cook": false})).unwrap();
assert_eq!(link(&session, id).params[1].value, json!({"t": "color", "v": [1.0, 0.0, 0.0, 1.0]}));
set(&mut session, json!({"id": "wear", "value": 0.9, "cook": false})).unwrap();
assert_eq!(link(&session, id).params[0].value, json!({"t": "float", "v": 0.9}));
// Cooks follow the texture set resolution
let job = session.link_job(id, &serde_json::Map::new()).unwrap();
// The job carries every value and the texture set's resolution.
let job = s.link_job(id, &serde_json::Map::new()).unwrap();
assert_eq!(job.size, 128);
assert_eq!(job.values["wear"], json!({"t": "float", "v": 0.9}));
// What the job cooked (here: built by hand) replaces the maps, merged into the edit.
let old = map_name(&s, id, Channel::Roughness);
s.apply_link_cook(id, &mcmat(0.75, 0.9), true).unwrap();
assert!((roughness(&mut s) - 0.75).abs() < 2e-3);
assert!(!s.doc.resources.contains_key(&old), "old maps are dropped");
execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert!((roughness(&mut s) - 0.25).abs() < 2e-3, "undo restores the earlier maps");
assert_eq!(link(&s, id).params[0].value, json!({"t": "float", "v": 0.5}));
execute(&mut s, "edit.redo", &Value::Null).unwrap();
assert!((roughness(&mut s) - 0.75).abs() < 2e-3);
// Merge the cooked maps into the parameter edit
let old = map_name(&session, id, Channel::Roughness);
session.apply_link_cook(id, &mcmat(0.75, 0.9), true).unwrap();
// Presets skip inputs the painter can't set; unknown presets are errors.
assert!(execute(&mut s, "material.link.preset", &json!({"name": "Nope", "cook": false})).is_err());
execute(&mut s, "material.link.preset", &json!({"name": "Worn", "cook": false})).unwrap();
assert_eq!(link(&s, id).params[0].value, json!({"t": "float", "v": 1.0}));
assert!((roughness(&mut session) - 0.75).abs() < 2e-3);
assert!(!session.doc.resources.contains_key(&old), "old maps are dropped");
execute(&mut session, "edit.undo", &Value::Null).unwrap();
assert!((roughness(&mut session) - 0.25).abs() < 2e-3, "undo restores the earlier maps");
assert_eq!(link(&session, id).params[0].value, json!({"t": "float", "v": 0.5}));
execute(&mut session, "edit.redo", &Value::Null).unwrap();
assert!((roughness(&mut session) - 0.75).abs() < 2e-3);
// Presets skip unsupported inputs and reject unknown names
assert!(execute(&mut session, "material.link.preset", &json!({"name": "Nope", "cook": false})).is_err());
execute(&mut session, "material.link.preset", &json!({"name": "Worn", "cook": false})).unwrap();
assert_eq!(link(&session, id).params[0].value, json!({"t": "float", "v": 1.0}));
}
#[test]
+40
View File
@@ -397,3 +397,43 @@ fn bench_layer_thumbs_4k() {
}
eprintln!(" every target once: {:.1} ms ({} pictures)", total.as_secs_f64() * 1e3, targets.len());
}
/// A full recomposite in one call against the same one spread over frames, as the UI runs it, with and without a blurred mask
/// in the stack: neighbourhood filters are what a frame slice pays for apart from its own tiles.
#[test]
#[ignore]
fn bench_sliced_filters_4k() {
for blur in [false, true] {
let mut session = Session::new_project("sliced", primitives::sphere(1.0, 64, 32), "builtin:sphere", None, 4096).unwrap();
session.viewport = Vec2::new(800.0, 800.0);
let run = |session: &mut Session, id: &str, params: Value| execute(session, id, &params).unwrap();
run(&mut session, "layer.add.fill", json!({"color": [0.6, 0.5, 0.4], "roughness": 0.6}));
run(&mut session, "layer.add.fill", json!({"color": [0.1, 0.3, 0.6]}));
run(&mut session, "mask.add.black", Value::Null);
run(&mut session, "effect.add.generator", json!({"generator": "position", "level": 0.5}));
if blur {
run(&mut session, "effect.add.filter", json!({"filter": "blur", "radius": 8}));
}
session.update();
session.caches[0].invalidate_all();
let started = Instant::now();
session.update();
let direct = started.elapsed();
session.caches[0].invalidate_all();
let clock = Instant::now();
let elapsed_ms = || clock.elapsed().as_secs_f64() * 1000.0;
let (mut frames, mut longest_frame) = (0, Duration::ZERO);
while session.pending_tiles() > 0 {
let started = Instant::now();
session.update_for(30.0, elapsed_ms);
longest_frame = longest_frame.max(started.elapsed());
frames += 1;
}
let label = if blur { "blurred mask" } else { "no filter" };
println!("[sliced 4096², {label}] one call {direct:.2?}; in 30 ms slices {frames} frames, worst {longest_frame:.2?}, total {:.2?}", clock.elapsed());
}
}
+35 -82
View File
@@ -1,5 +1,8 @@
//! Which tools can act on each selection, and that every usable tool acts there in both views
mod selection;
use selection::Selection;
use serde_json::{Value, json};
use texturecraft_engine::core::{Effect, EffectKind, LayerId};
use texturecraft_engine::geom::glam::{Vec2, Vec3};
@@ -11,86 +14,6 @@ o Body\nv 2 2 0\nv 4 2 0\nv 2 4 0\nvt 0.5 0.5\nvt 1 0.5\nvt 0.5 1\nf 4/4 5/5 6/6
const VIEWPORT_PIXELS: f32 = 400.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Selection {
PaintContent,
FillContent,
PaintMask,
FillMask,
FolderMask,
InstanceOwnMask,
PaintSublayer,
PaintSublayerInMask,
PaintSublayerInFill,
PathSublayer,
FilterInPaint,
FilterInFill,
FilterInMask,
FillEffectInPaint,
GeneratorInFillMask,
FolderContent,
InstanceOfPaint,
InstanceOfFill,
Nothing,
QuickMaskOverPaint,
QuickMaskOverFill,
QuickMaskOverNothing,
}
impl Selection {
const ALL: [Selection; 22] = [
Selection::PaintContent,
Selection::FillContent,
Selection::PaintMask,
Selection::FillMask,
Selection::FolderMask,
Selection::InstanceOwnMask,
Selection::PaintSublayer,
Selection::PaintSublayerInMask,
Selection::PaintSublayerInFill,
Selection::PathSublayer,
Selection::FilterInPaint,
Selection::FilterInFill,
Selection::FilterInMask,
Selection::FillEffectInPaint,
Selection::GeneratorInFillMask,
Selection::FolderContent,
Selection::InstanceOfPaint,
Selection::InstanceOfFill,
Selection::Nothing,
Selection::QuickMaskOverPaint,
Selection::QuickMaskOverFill,
Selection::QuickMaskOverNothing,
];
/// Whether the shelf offers `tool` for this selection
fn offers(self, tool: Tool) -> bool {
let reads_content = matches!(tool, Tool::Smudge | Tool::Clone);
match tool {
Tool::ColorPicker => true,
Tool::GeometryMask => !matches!(self, Selection::Nothing | Selection::QuickMaskOverNothing),
_ => match self {
Selection::PaintContent | Selection::PaintSublayer => true,
Selection::PaintSublayerInFill => tool != Tool::Path,
Selection::PaintMask | Selection::FillMask | Selection::FolderMask | Selection::InstanceOwnMask | Selection::PaintSublayerInMask => {
!reads_content
}
Selection::PathSublayer => tool == Tool::Path,
Selection::QuickMaskOverPaint | Selection::QuickMaskOverFill | Selection::QuickMaskOverNothing => tool != Tool::Path,
_ => false,
},
}
}
/// Greyed in the shelf, but scripts still paint here: below the effect, or the instance's source
fn scripts_still_paint(self) -> bool {
matches!(
self,
Selection::FilterInPaint | Selection::FillEffectInPaint | Selection::FilterInMask | Selection::GeneratorInFillMask | Selection::InstanceOfPaint
)
}
}
fn stencil_png() -> Vec<u8> {
let mut out = Vec::new();
let green = image::RgbaImage::from_fn(8, 8, |_, _| image::Rgba([0, 255, 0, 255]));
@@ -133,13 +56,13 @@ fn select_nothing(session: &mut Session) {
}
/// Adds an effect with `command` to the layer's content or mask and selects it
fn select_new_effect(session: &mut Session, layer: u64, mask: bool, command: &str, params: Value) {
fn select_new_effect(session: &mut Session, layer: u64, mask: bool, command: &str, effect_params: Value) {
if mask {
execute(session, "mask.add.black", &json!({"layer": layer})).unwrap();
}
let target = if mask { "mask" } else { "content" };
let mut params = params;
let mut params = effect_params;
params["layer"] = json!(layer);
params["target"] = json!(target);
execute(session, command, &params).unwrap();
@@ -241,6 +164,7 @@ fn session_with(selection: Selection) -> Session {
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
}
}
session
}
@@ -284,6 +208,7 @@ fn use_tool(session: &mut Session, tool: Tool, in_2d_view: bool) -> texturecraft
session.continue_stroke(*point, 1.0);
}
}
session.end_stroke()
}
@@ -381,3 +306,31 @@ fn quick_mask_edit_and_invert_need_the_mask_on() {
assert!(!session.quick_mask.editing, "turning the mask off stops editing it");
}
#[test]
fn a_saved_path_cannot_be_committed_while_editing_the_quick_mask() {
let mut session = session_with(Selection::PaintContent);
commit_path(&mut session).unwrap();
let StackTarget::Effect(path_sublayer) = session.target else { panic!("the committed path is not selected") };
let points_before = match session.doc.project.effect(path_sublayer).map(|(_, effect)| &effect.kind) {
Some(EffectKind::Path(paths)) => paths[0].points.clone(),
_ => panic!("the committed path is missing"),
};
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
execute(&mut session, "path.edit", &json!({"effect": path_sublayer.0, "index": 0})).unwrap();
assert!(!session.tool_usable(Tool::Path));
execute(&mut session, "tool.path", &json!({"uvPoints": [[0.2, 0.8], [0.3, 0.7]]})).unwrap();
assert!(!session.path_live().unwrap(), "live preview must leave the stored path alone");
let result = execute(&mut session, "tool.path", &json!({"commit": true}));
assert!(result.is_err(), "a greyed Path tool must not commit a stored path into the quick mask edit state");
let points_after = match session.doc.project.effect(path_sublayer).map(|(_, effect)| &effect.kind) {
Some(EffectKind::Path(paths)) => paths[0].points.clone(),
_ => panic!("the stored path is missing"),
};
assert_eq!(points_after, points_before);
}
@@ -0,0 +1,83 @@
//! Selection cases for the tool availability tests
use texturecraft_engine::Tool;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Selection {
PaintContent,
FillContent,
PaintMask,
FillMask,
FolderMask,
InstanceOwnMask,
PaintSublayer,
PaintSublayerInMask,
PaintSublayerInFill,
PathSublayer,
FilterInPaint,
FilterInFill,
FilterInMask,
FillEffectInPaint,
GeneratorInFillMask,
FolderContent,
InstanceOfPaint,
InstanceOfFill,
Nothing,
QuickMaskOverPaint,
QuickMaskOverFill,
QuickMaskOverNothing,
}
impl Selection {
pub(super) const ALL: [Selection; 22] = [
Selection::PaintContent,
Selection::FillContent,
Selection::PaintMask,
Selection::FillMask,
Selection::FolderMask,
Selection::InstanceOwnMask,
Selection::PaintSublayer,
Selection::PaintSublayerInMask,
Selection::PaintSublayerInFill,
Selection::PathSublayer,
Selection::FilterInPaint,
Selection::FilterInFill,
Selection::FilterInMask,
Selection::FillEffectInPaint,
Selection::GeneratorInFillMask,
Selection::FolderContent,
Selection::InstanceOfPaint,
Selection::InstanceOfFill,
Selection::Nothing,
Selection::QuickMaskOverPaint,
Selection::QuickMaskOverFill,
Selection::QuickMaskOverNothing,
];
/// Whether the shelf offers `tool` for this selection
pub(super) fn offers(self, tool: Tool) -> bool {
let reads_content = matches!(tool, Tool::Smudge | Tool::Clone);
match tool {
Tool::ColorPicker => true,
Tool::GeometryMask => !matches!(self, Selection::Nothing | Selection::QuickMaskOverNothing),
_ => match self {
Selection::PaintContent | Selection::PaintSublayer => true,
Selection::PaintSublayerInFill => tool != Tool::Path,
Selection::PaintMask | Selection::FillMask | Selection::FolderMask | Selection::InstanceOwnMask | Selection::PaintSublayerInMask => {
!reads_content
}
Selection::PathSublayer => tool == Tool::Path,
Selection::QuickMaskOverPaint | Selection::QuickMaskOverFill | Selection::QuickMaskOverNothing => tool != Tool::Path,
_ => false,
},
}
}
/// Greyed in the shelf, but scripts still paint here: below the effect, or the instance's source
pub(super) fn scripts_still_paint(self) -> bool {
matches!(
self,
Selection::FilterInPaint | Selection::FillEffectInPaint | Selection::FilterInMask | Selection::GeneratorInFillMask | Selection::InstanceOfPaint
)
}
}
+51
View File
@@ -251,3 +251,54 @@ fn swap_mask_value_inverts_only_while_a_mask_is_the_target() {
assert_eq!(session.tool.mask_value, 0.0, "a mask effect paints the mask too");
}
#[test]
fn stroke_after_undoing_mask_is_paint_stroke() {
let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:random", None, 256).unwrap();
s.viewport = Vec2::new(400.0, 400.0);
execute(&mut s, "layer.add.fill", &json!({"color": [0.2, 0.5, 0.3], "roughness": 0.7})).unwrap();
execute(&mut s, "layer.add.paint", &Value::Null).unwrap();
s.update();
execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 50, "opacity": 0.8})).unwrap();
execute(&mut s, "tool.paint", &Value::Null).unwrap();
execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [0.9, 0.3, 0.9]})).unwrap();
execute(&mut s, "tool.stroke", &json!({"points": [[130, 130], [170, 150]]})).unwrap();
s.update();
execute(&mut s, "mask.add.white", &Value::Null).unwrap();
s.update();
execute(&mut s, "edit.undo", &Value::Null).unwrap();
s.update();
execute(&mut s, "tool.stroke", &json!({"points": [[167, 183], [207, 203]]})).unwrap();
s.update();
let undone = execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert_eq!(undone["undone"], "Paint stroke", "the stroke after undoing the mask was recorded as {}", undone["undone"]);
execute(&mut s, "edit.redo", &Value::Null).unwrap();
s.update();
let settled = s.caches[0].composite.clone();
s.caches[0].below.clear();
s.caches[0].invalidate_all();
s.update();
assert_eq!(settled, s.caches[0].composite, "the composite after the stroke matches a full recomposite");
}
#[test]
fn undoing_after_painting_on_instance_source_mask_keeps_mask_target() {
let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:random", None, 256).unwrap();
s.viewport = Vec2::new(400.0, 400.0);
execute(&mut s, "layer.add.paint", &Value::Null).unwrap();
execute(&mut s, "mask.add.white", &Value::Null).unwrap();
execute(&mut s, "layer.instance", &Value::Null).unwrap();
s.update();
s.target = StackTarget::Mask;
execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 50, "opacity": 0.8})).unwrap();
execute(&mut s, "tool.stroke", &json!({"points": [[170, 180], [210, 200]]})).unwrap();
execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert_eq!(s.target, StackTarget::Mask);
execute(&mut s, "tool.stroke", &json!({"points": [[180, 180], [210, 200]]})).unwrap();
let undone = execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert_eq!(undone["undone"], "Mask stroke");
}
+188
View File
@@ -54,6 +54,84 @@ fn layers_masks_materials_and_bake() {
assert_eq!(s.doc.project.walk(0).len(), 1, "back to the initial paint layer");
}
#[test]
fn generator_offset_is_settable_and_clamped() {
let mut s = session();
execute(&mut s, "layer.add.fill", &json!({})).unwrap();
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
let r = execute(&mut s, "effect.add.generator", &json!({"generator": "position", "offset": 0.25})).unwrap();
let eff = r["effect"].as_u64().unwrap();
let offset = |s: &Session| {
let layer = s.doc.project.layers.values().find(|l| l.mask.is_some()).unwrap();
layer
.mask
.as_ref()
.unwrap()
.effects
.iter()
.find_map(|e| match &e.kind {
texturecraft_engine::core::EffectKind::Generator(g) => Some(g.offset),
_ => None,
})
.unwrap()
};
assert_eq!(offset(&s), 0.25);
execute(&mut s, "effect.set", &json!({"effect": eff, "offset": 5.0})).unwrap();
assert_eq!(offset(&s), 1.0, "clamped to 1");
}
fn generator_offset_of(s: &Session) -> f32 {
let layer = s.doc.project.layers.values().find(|l| l.mask.is_some()).unwrap();
layer
.mask
.as_ref()
.unwrap()
.effects
.iter()
.find_map(|e| match &e.kind {
texturecraft_engine::core::EffectKind::Generator(g) => Some(g.offset),
_ => None,
})
.unwrap()
}
#[test]
fn generator_offset_survives_save_and_reopen_and_undo_restores_it() {
let mut s = session();
execute(&mut s, "layer.add.fill", &json!({})).unwrap();
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
let r = execute(&mut s, "effect.add.generator", &json!({"generator": "position", "offset": 0.25})).unwrap();
let eff = r["effect"].as_u64().unwrap();
execute(&mut s, "effect.set", &json!({"effect": eff, "offset": -0.6})).unwrap();
assert_eq!(generator_offset_of(&s), -0.6);
let path = std::env::temp_dir().join(format!("tc-generator-offset-{}.tcraft", std::process::id()));
let path = path.to_str().unwrap();
execute(&mut s, "file.saveCopy", &json!({"path": path})).unwrap();
let reopened = texturecraft_engine::file_cmds::open_path(path);
let _ = std::fs::remove_file(path);
assert_eq!(generator_offset_of(&reopened.unwrap()), -0.6, "the offset is saved and read back");
execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert_eq!(generator_offset_of(&s), 0.25, "undo restores the previous offset");
}
#[test]
fn generator_offset_works_alongside_curvature_mode_and_sizes() {
let mut s = session();
execute(&mut s, "layer.add.fill", &json!({})).unwrap();
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
let r = execute(&mut s, "effect.add.generator", &json!({"generator": "curvature", "mode": "cavities", "offset": 0.5})).unwrap();
let eff = r["effect"].as_u64().unwrap();
assert_eq!(generator_offset_of(&s), 0.5);
execute(&mut s, "effect.set", &json!({"effect": eff, "offset": -0.5, "sizes": [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5]})).unwrap();
assert_eq!(generator_offset_of(&s), -0.5);
let inspect = execute(&mut s, "project.inspect", &Value::Null).unwrap().to_string();
assert!(inspect.contains(r#""offset":-0.5"#) && inspect.contains(r#""mode":"Cavities""#), "{inspect}");
assert!(inspect.contains(r#""sizes":[0.0,0.0,0.0,1.0,0.0,0.0,0.5]"#), "{inspect}");
}
#[test]
fn layer_rename_and_undo() {
let mut s = session();
@@ -230,6 +308,67 @@ fn image_resources_fill_layers_and_round_trip() {
assert!(s.import_resource_bytes("bad.png", vec![1, 2, 3]).is_err());
}
#[test]
fn image_resource_lookup_accepts_extension_and_stem() {
let mut s = session();
// No resources imported yet
let err = execute(&mut s, "layer.add.image", &json!({"name": "missing"})).unwrap_err();
assert!(err.to_string().contains("no resource `missing` (no resources imported: use resource.import)"));
let mut png = Vec::new();
let img = image::RgbaImage::from_fn(4, 4, |_, _| image::Rgba([0, 255, 0, 255]));
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
s.import_resource_bytes("tile.png", png.clone()).unwrap();
// Lookup by stem
let r = execute(&mut s, "layer.add.image", &json!({"name": "tile"})).unwrap();
assert!(r["layer"].as_u64().is_some());
// Lookup by full name
let r2 = execute(&mut s, "layer.add.image", &json!({"name": "tile.png"})).unwrap();
assert!(r2["layer"].as_u64().is_some());
// Missing resource lists available
let err = execute(&mut s, "layer.add.image", &json!({"name": "wood"})).unwrap_err();
assert!(err.to_string().contains("no resource `wood` (available: tile.png)"));
// Ambiguous stem resolution
s.import_resource_bytes("tile.jpg", png).unwrap();
let err = execute(&mut s, "layer.add.image", &json!({"name": "tile"})).unwrap_err();
assert!(err.to_string().contains("ambiguous resource `tile`"));
}
#[test]
fn image_resource_lookup_ignores_linked_packages() {
use std::sync::Arc;
use texturecraft_engine::core::{LayerId, LayerKind, Source};
let mut s = session();
s.doc.resources.insert("wood.mcraft".into(), Arc::new(b"package bytes".to_vec()));
s.sync_images();
assert!(!s.images.contains_key("wood.mcraft"));
let err = execute(&mut s, "layer.add.image", &json!({"name": "wood"})).unwrap_err();
assert!(err.to_string().contains("no resources imported"), "{err}");
let err = execute(&mut s, "layer.add.image", &json!({"name": "wood.mcraft"})).unwrap_err();
assert!(err.to_string().contains("no resource `wood.mcraft` (no resources imported"), "{err}");
let mut png = Vec::new();
let img = image::RgbaImage::from_fn(4, 4, |_, _| image::Rgba([0, 255, 0, 255]));
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
s.import_resource_bytes("wood.png", png).unwrap();
let err = execute(&mut s, "layer.add.image", &json!({"name": "missing"})).unwrap_err();
assert!(err.to_string().contains("no resource `missing` (available: wood.png)"), "{err}");
let res = execute(&mut s, "layer.add.image", &json!({"name": "wood"})).unwrap();
let id = LayerId(res["layer"].as_u64().unwrap());
let LayerKind::Fill(fill) = &s.doc.project.layer(id).unwrap().kind else { panic!("expected fill layer") };
assert_eq!(fill.sources.get(&Channel::BaseColor), Some(&Source::image("wood.png")));
}
#[test]
fn brush_presets_apply_save_and_load() {
let mut s = session();
@@ -446,3 +585,52 @@ fn curvature_generators_take_a_mode_and_detail_sizes() {
assert!(dirt.is_err(), "only curvature generators have a mode");
assert_eq!(execute(&mut state, "project.inspect", &Value::Null).unwrap().to_string(), inspect, "failed commands leave the project unchanged");
}
#[test]
fn unparseable_color_param_returns_error() {
let mut s = session();
let err = execute(&mut s, "layer.add.fill", &json!({"color": "#fff"})).unwrap_err();
assert_eq!(err.to_string(), "`layer.add.fill`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "layer.fill.set", &json!({"channel": "baseColor", "color": "#123"})).unwrap_err();
assert_eq!(err.to_string(), "`layer.fill.set`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`tool.material`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "mask.add.colorSelection", &json!({"color": "#xyz"})).unwrap_err();
assert_eq!(err.to_string(), "`mask.add.colorSelection`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "display.set", &json!({"scatterColor": "#12345"})).unwrap_err();
assert_eq!(err.to_string(), "`display.set`: `scatterColor` must be a color ([r, g, b] linear or \"#rrggbb\")");
let rev_before = s.revision;
let exposure_before = s.doc.project.display.env_exposure;
let err = execute(&mut s, "display.set", &json!({"exposure": 2.0, "scatterColor": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`display.set`: `scatterColor` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.doc.project.display.env_exposure, exposure_before);
assert_eq!(s.revision, rev_before);
let mask_before = s.tool.mask_value;
let err = execute(&mut s, "tool.material", &json!({"maskValue": 0.25, "channel": "baseColor", "color": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`tool.material`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.tool.mask_value, mask_before);
assert_eq!(s.revision, rev_before);
let err = execute(&mut s, "tool.material", &json!({"color": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`tool.material`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.revision, rev_before);
let err = execute(&mut s, "layer.add.fill", &json!({"color": [1e40, 0.0, 0.0]})).unwrap_err();
assert_eq!(err.to_string(), "`layer.add.fill`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.revision, rev_before);
let err = execute(&mut s, "layer.fill.set", &json!({"channel": "baseColor", "procedural": "noise", "color": "red"})).unwrap_err();
assert_eq!(err.to_string(), "`layer.fill.set`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.revision, rev_before);
assert!(execute(&mut s, "tool.cloneSource", &json!({"uv": [0.2, 0.8, 0.4]})).is_err());
execute(&mut s, "tool.polygonFill", &Value::Null).unwrap();
let err = execute(&mut s, "tool.click", &json!({"uv": [0.2, 0.8, 0.4]})).unwrap_err();
assert_eq!(err.to_string(), "`tool.click`: a 2D view point needs `uv` as [u, v]");
}
+41 -16
View File
@@ -1,6 +1,6 @@
//! Decoding imported image resources (fills, alphas, stencils).
use crate::{FormatError, MAX_IMAGE_SIZE, Result};
use crate::{FormatError, MAX_IMAGE_SIZE, MAX_RESOURCE_TEXELS, Result};
/// A decoded image: RGBA as 0..1 floats in the file's own encoding (8/16-bit files stay
/// sRGB-encoded; `srgb` says so), row-major, top row first.
@@ -15,33 +15,37 @@ pub struct DecodedImage {
/// Decode PNG, JPEG, TGA, TIFF, BMP, WEBP, EXR or HDR bytes.
pub fn decode_image(bytes: &[u8]) -> Result<DecodedImage> {
decode_image_within(bytes, u64::MAX)
decode_image_within(bytes, MAX_RESOURCE_TEXELS)
}
/// [`decode_image`] for an image of at most `max_texels` (width × height), checked from the
/// header, before any pixel is decoded or allocated.
pub fn decode_image_within(bytes: &[u8], max_texels: u64) -> Result<DecodedImage> {
let mut reader =
image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?;
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes))
.with_guessed_format()
.map_err(|error| FormatError::Malformed { kind: "image", msg: error.to_string() })?;
// TGA has no magic signature; its decoder validates the header when no format was recognized
let format = reader.format().unwrap_or(image::ImageFormat::Tga);
reader.set_format(format);
let (width, height) = reader.into_dimensions().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?;
let (width, height) = reader.into_dimensions().map_err(|error| FormatError::Malformed { kind: "image", msg: error.to_string() })?;
if width == 0 || height == 0 || width > MAX_IMAGE_SIZE || height > MAX_IMAGE_SIZE {
return Err(FormatError::TooLarge(format!("image is {width}x{height} (limit {MAX_IMAGE_SIZE})")));
}
if u64::from(width) * u64::from(height) > max_texels {
return Err(FormatError::TooLarge(format!("image is {width}x{height}, more than {max_texels} texels")));
}
let image =
crate::guard("image", || image::load_from_memory_with_format(bytes, format).map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() }))?;
let image = crate::guard("image", || {
image::load_from_memory_with_format(bytes, format).map_err(|error| FormatError::Malformed { kind: "image", msg: error.to_string() })
})?;
let srgb = !matches!(image, image::DynamicImage::ImageRgb32F(_) | image::DynamicImage::ImageRgba32F(_));
let float_image = image.into_rgba32f();
let (width, height) = float_image.dimensions();
let rgba = float_image.pixels().map(|pixel| pixel.0).collect();
Ok(DecodedImage { width, height, rgba, srgb })
}
@@ -63,15 +67,19 @@ mod tests {
#[test]
fn decodes_png_and_rejects_garbage() {
let mut png = Vec::new();
let img = image::RgbaImage::from_fn(4, 2, |x, _| image::Rgba([x as u8 * 60, 0, 0, 255]));
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let d = decode_image(&png).unwrap();
assert_eq!((d.width, d.height), (4, 2));
assert!(d.srgb);
assert!((d.rgba[1][0] - 60.0 / 255.0).abs() < 1e-3);
let image = image::RgbaImage::from_fn(4, 2, |x, _| image::Rgba([x as u8 * 60, 0, 0, 255]));
image.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let decoded = decode_image(&png).unwrap();
assert_eq!((decoded.width, decoded.height), (4, 2));
assert!(decoded.srgb);
assert!((decoded.rgba[1][0] - 60.0 / 255.0).abs() < 1e-3);
assert!(decode_image(b"not an image").is_err());
assert!(decode_image(&png[..png.len() / 2]).is_err());
let text = decode_image(&coverage_png(2, 1, &[0, 255], [255; 3]).unwrap()).unwrap();
assert_eq!(text.rgba, vec![[1.0, 1.0, 1.0, 0.0], [1.0; 4]]);
assert!(coverage_png(2, 2, &[0], [255; 3]).is_err());
}
@@ -80,11 +88,15 @@ mod tests {
fn tga_without_a_magic_signature_decodes_within_the_texel_budget() {
let mut bytes = Vec::new();
image::RgbaImage::from_pixel(4, 4, image::Rgba([128, 51, 255, 255])).write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Tga).unwrap();
let image = decode_image_within(&bytes, 16).unwrap();
assert_eq!((image.width, image.height), (4, 4));
assert!((image.rgba[0][1] - 0.2).abs() < 1e-6);
assert!(image.srgb);
let header = &bytes[..18];
assert!(matches!(decode_image_within(header, 15), Err(FormatError::TooLarge(_))));
assert!(decode_image(header).is_err());
}
@@ -92,12 +104,25 @@ mod tests {
#[test]
fn decoding_within_a_texel_limit_refuses_big_images_from_the_header() {
let png = {
let mut b = Vec::new();
image::RgbImage::from_pixel(8, 8, image::Rgb([1, 2, 3])).write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png).unwrap();
b
let mut bytes = Vec::new();
image::RgbImage::from_pixel(8, 8, image::Rgb([1, 2, 3])).write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
bytes
};
assert_eq!(decode_image_within(&png, 64).unwrap().width, 8);
assert!(matches!(decode_image_within(&png, 63), Err(FormatError::TooLarge(_))));
assert!(decode_image(&png).is_ok());
}
#[test]
fn the_default_decoder_refuses_more_than_the_resource_texel_budget_from_the_header() {
// The edge limit permits 16384² but the default resource budget does not
let mut header = [0u8; 18];
header[2] = 2;
header[12..14].copy_from_slice(&16_384u16.to_le_bytes());
header[14..16].copy_from_slice(&16_384u16.to_le_bytes());
header[16] = 24;
assert!(matches!(decode_image(&header), Err(FormatError::TooLarge(_))));
}
}
+61 -8
View File
@@ -95,6 +95,10 @@ pub(crate) fn guard<T>(kind: &'static str, f: impl FnOnce() -> Result<T>) -> Res
/// Largest image edge accepted or produced anywhere in this crate.
pub const MAX_IMAGE_SIZE: u32 = 16_384;
/// Most texels an imported image resource may decode to by default (8192 × 8192).
/// Larger images are refused from their header before any pixels are decoded.
pub const MAX_RESOURCE_TEXELS: u64 = 1 << 26;
/// Read a whole file, refusing files over `cap` bytes.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn read_capped(path: &std::path::Path, cap: u64) -> Result<Vec<u8>> {
@@ -108,15 +112,64 @@ pub(crate) fn read_capped(path: &std::path::Path, cap: u64) -> Result<Vec<u8>> {
Ok(std::fs::read(path)?)
}
/// Write `bytes` to `path` atomically: `<path>.tmp` first, then rename over the target.
/// Create a new temporary file beside `path`, refusing to reuse an existing (possibly symlinked) name.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn create_temp(path: &std::path::Path) -> Result<(std::path::PathBuf, std::fs::File)> {
let parent = path.parent().unwrap_or_else(|| std::path::Path::new("."));
let name = path.file_name().and_then(|name| name.to_str()).unwrap_or("out");
// Bound retries when stale files or concurrent writers occupy candidate names
const MAX_TEMP_ATTEMPTS: u32 = 100;
for attempt in 0..MAX_TEMP_ATTEMPTS {
let temporary_path = parent.join(format!(".{name}.{}.{attempt}.tmp", std::process::id()));
match std::fs::OpenOptions::new().write(true).create_new(true).open(&temporary_path) {
Ok(file) => return Ok((temporary_path, file)),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(error.into()),
}
}
Err(FormatError::Io(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "could not create a temporary file")))
}
/// Write `bytes` to `path` atomically: a new temporary file first, then rename over the target.
#[cfg(not(target_arch = "wasm32"))]
pub fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<()> {
let mut tmp = path.as_os_str().to_owned();
tmp.push(".tmp");
let tmp = std::path::PathBuf::from(tmp);
let res = std::fs::write(&tmp, bytes).and_then(|_| std::fs::rename(&tmp, path));
if res.is_err() {
let _ = std::fs::remove_file(&tmp);
use std::io::Write;
let (temporary_path, mut file) = create_temp(path)?;
let result = file.write_all(bytes).and_then(|_| file.sync_all());
drop(file);
let result = result.and_then(|_| std::fs::rename(&temporary_path, path));
if result.is_err() {
let _ = std::fs::remove_file(&temporary_path);
}
Ok(result?)
}
#[cfg(all(test, unix))]
mod atomic_tests {
use super::write_atomic;
#[test]
fn write_atomic_does_not_follow_a_planted_temp_symlink() -> std::io::Result<()> {
let directory = std::env::temp_dir().join(format!("tc-atomic-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&directory);
std::fs::create_dir_all(&directory)?;
let target = directory.join("out.png");
let victim = directory.join("victim.txt");
std::fs::write(&victim, b"safe")?;
std::os::unix::fs::symlink(&victim, directory.join("out.png.tmp"))?;
write_atomic(&target, b"new").map_err(std::io::Error::other)?;
assert_eq!(std::fs::read(&victim)?, b"safe");
assert_eq!(std::fs::read(&target)?, b"new");
let _ = std::fs::remove_dir_all(&directory);
Ok(())
}
Ok(res?)
}
+95 -19
View File
@@ -20,6 +20,10 @@ const FORMAT: &str = "craft-material";
const VERSION: u32 = 1;
/// Largest entry read (maps are at most 8192² × 16 bits, compressed).
const MAX_ENTRY: u64 = 512 << 20;
/// Most map entries one manifest may name (a material has at most nine usages).
const MAX_MAPS: usize = 64;
/// Largest total payload read from one `.mcmat`: maps and source together (audit: TextureCraft #4).
const MAX_TOTAL: u64 = 1 << 30;
const KIND: &str = "MaterialCraft material";
#[derive(Clone, Debug, PartialEq, Deserialize)]
@@ -66,27 +70,53 @@ fn malformed(msg: impl Into<String>) -> FormatError {
FormatError::Malformed { kind: KIND, msg: msg.into() }
}
fn read_entry(name: &str, entry: impl Read, total_bytes: &mut u64) -> Result<Vec<u8>> {
// Earlier maps remain live while the next entry is read
let remaining_bytes = MAX_TOTAL.saturating_sub(*total_bytes);
let entry_limit = MAX_ENTRY.min(remaining_bytes);
let mut bytes = Vec::new();
entry.take(entry_limit + 1).read_to_end(&mut bytes).map_err(|error| malformed(format!("{name}: {error}")))?;
if bytes.len() as u64 > MAX_ENTRY {
return Err(FormatError::TooLarge(name.to_string()));
}
let next_total = total_bytes.saturating_add(bytes.len() as u64);
if next_total > MAX_TOTAL {
return Err(FormatError::TooLarge(format!("{KIND} payload (over {MAX_TOTAL} bytes)")));
}
// Keep spare read capacity from accumulating across retained map entries
bytes.shrink_to_fit();
*total_bytes = next_total;
Ok(bytes)
}
/// Read `.mcmat` bytes.
pub fn read_mcmat(bytes: &[u8]) -> Result<PortableMaterial> {
let mut z = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| malformed(e.to_string()))?;
let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|error| malformed(error.to_string()))?;
let mut total_bytes = 0u64;
let mut read = |name: &str| -> Result<Vec<u8>> {
let mut out = Vec::new();
let f = z.by_name(name).map_err(|_| malformed(format!("{name} is missing")))?;
f.take(MAX_ENTRY + 1).read_to_end(&mut out).map_err(|e| malformed(format!("{name}: {e}")))?;
if out.len() as u64 > MAX_ENTRY {
return Err(FormatError::TooLarge(name.to_string()));
}
Ok(out)
let entry = archive.by_name(name).map_err(|_| malformed(format!("{name} is missing")))?;
read_entry(name, entry, &mut total_bytes)
};
let manifest: Manifest = serde_json::from_slice(&read("material.json")?).map_err(|e| malformed(format!("material.json: {e}")))?;
let manifest: Manifest = serde_json::from_slice(&read("material.json")?).map_err(|error| malformed(format!("material.json: {error}")))?;
if manifest.format != FORMAT {
return Err(malformed(format!("format `{}`", manifest.format)));
}
if manifest.version > VERSION {
return Err(FormatError::Unsupported(format!("{KIND} version {} (this version reads up to {VERSION})", manifest.version)));
}
let maps = manifest.maps.iter().map(|m| Ok((m.usage.clone(), read(&m.file)?))).collect::<Result<Vec<_>>>()?;
if manifest.maps.len() > MAX_MAPS {
return Err(malformed(format!("{} maps, over the {MAX_MAPS} map limit", manifest.maps.len())));
}
let maps = manifest.maps.iter().map(|map| Ok((map.usage.clone(), read(&map.file)?))).collect::<Result<Vec<_>>>()?;
let source = read(&manifest.source)?;
Ok(PortableMaterial { manifest, maps, source })
}
@@ -153,20 +183,66 @@ mod tests {
#[test]
fn reads_manifest_maps_and_source() {
let bytes = sample(1);
assert!(is_mcmat(&bytes) && !is_mcraft(&bytes));
let m = read_mcmat(&bytes).unwrap();
assert_eq!(m.manifest.graph, "Brick_wall");
assert_eq!(m.manifest.parameters[0].id, "wear");
assert_eq!(m.manifest.parameters[0].value, serde_json::json!({"t": "float", "v": 0.25}));
assert_eq!(m.manifest.presets[0].name, "Worn");
assert_eq!(m.manifest.inputs[0].identifier, "curvature");
assert_eq!(m.maps[0].0, "baseColor");
assert_eq!(crate::decode_image(&m.maps[0].1).unwrap().width, 4);
assert_eq!(m.source, b"pkg");
let material = read_mcmat(&bytes).unwrap();
assert_eq!(material.manifest.graph, "Brick_wall");
assert_eq!(material.manifest.parameters[0].id, "wear");
assert_eq!(material.manifest.parameters[0].value, serde_json::json!({"t": "float", "v": 0.25}));
assert_eq!(material.manifest.presets[0].name, "Worn");
assert_eq!(material.manifest.inputs[0].identifier, "curvature");
assert_eq!(material.maps[0].0, "baseColor");
assert_eq!(crate::decode_image(&material.maps[0].1).unwrap().width, 4);
assert_eq!(material.source, b"pkg");
assert!(matches!(read_mcmat(&sample(2)), Err(FormatError::Unsupported(_))));
assert!(read_mcmat(b"junk").is_err());
}
#[test]
fn an_unbounded_map_list_is_refused() {
let maps: Vec<_> = (0..=MAX_MAPS).map(|_| serde_json::json!({"usage": "baseColor", "file": "maps/baseColor.png"})).collect();
let manifest =
serde_json::json!({"format": FORMAT, "version": VERSION, "name": "n", "graph": "g", "size": [4, 4], "maps": maps, "source": "source.mcraft"});
let mut archive = zip::ZipWriter::new(Cursor::new(Vec::new()));
let options = zip::write::SimpleFileOptions::default();
archive.start_file("material.json", options).unwrap();
archive.write_all(manifest.to_string().as_bytes()).unwrap();
archive.start_file("maps/baseColor.png", options).unwrap();
archive.write_all(&mesh_map_png(4, 3, &[0.5; 48]).unwrap()).unwrap();
archive.start_file("source.mcraft", options).unwrap();
archive.write_all(b"pkg").unwrap();
let result = read_mcmat(&archive.finish().unwrap().into_inner());
assert!(matches!(result, Err(FormatError::Malformed { .. })));
}
#[test]
fn entry_read_stops_at_the_remaining_payload_budget() {
let payload = vec![7u8; 1024];
let mut input = Cursor::new(&payload);
let mut total_bytes = MAX_TOTAL - 8;
let result = read_entry("maps/extra.png", &mut input, &mut total_bytes);
assert!(matches!(result, Err(FormatError::TooLarge(_))));
assert_eq!(input.position(), 9, "only one byte beyond the remaining budget is needed to reject the entry");
assert_eq!(total_bytes, MAX_TOTAL - 8, "a rejected entry does not consume the payload budget");
}
#[test]
fn payload_budget_accepts_the_exact_limit_and_empty_entries() {
let mut total_bytes = MAX_TOTAL - 4;
let bytes = read_entry("map.png", Cursor::new([1, 2, 3, 4]), &mut total_bytes).unwrap();
assert_eq!(bytes, [1, 2, 3, 4]);
assert_eq!(total_bytes, MAX_TOTAL);
assert!(read_entry("empty.mcraft", Cursor::new([]), &mut total_bytes).unwrap().is_empty());
assert!(matches!(read_entry("extra.png", Cursor::new([1]), &mut total_bytes), Err(FormatError::TooLarge(_))));
}
#[test]
fn mesh_maps_encode_as_16_bit() {
let png = mesh_map_png(2, 1, &[0.0, 0.25, 0.5, 1.0]).unwrap();
+19 -7
View File
@@ -217,7 +217,7 @@ impl<'a> Ogawa<'a> {
let value_bytes = match property.pod {
0..=2 => 1,
3 | 4 => 2,
3 | 4 | 9 => 2,
5 | 6 | 10 => 4,
7 | 8 | 11 => 8,
other => return Err(FormatError::Unsupported(format!("Alembic property \"{}\" of data type {other}", property.name))),
@@ -233,6 +233,7 @@ impl<'a> Ogawa<'a> {
6 => raw.as_chunks::<4>().0.iter().map(|c| i32::from_le_bytes(*c) as f64).collect(),
7 => raw.as_chunks::<8>().0.iter().map(|c| u64::from_le_bytes(*c) as f64).collect(),
8 => raw.as_chunks::<8>().0.iter().map(|c| i64::from_le_bytes(*c) as f64).collect(),
9 => raw.as_chunks::<2>().0.iter().map(|c| half::f16::from_bits(u16::from_le_bytes(*c)).to_f64()).collect(),
10 => raw.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c) as f64).collect(),
_ => raw.as_chunks::<8>().0.iter().map(|c| f64::from_le_bytes(*c)).collect(),
})
@@ -280,19 +281,30 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
if root.len() < 6 || !(0..6).all(|i| is_data(i) != (i == 2)) {
return Err(malformed(KIND, "the archive group does not have the Alembic layout"));
}
// Indexed metadata: a u8 length before each string.
let block = o.data(root[5])?;
let mut c = Cursor { b: block, pos: 0 };
while c.pos < block.len() {
let n = c.u8()? as usize;
o.indexed_meta.push(c.string(n)?);
}
read_indexed_meta(block, &mut o.indexed_meta)?;
let mut sink = PolygonSink::default();
let mut walk = Walk { o, sink: &mut sink, visits: 0 };
walk.object(root[2], Mat4::IDENTITY, "", 0)?;
Ok(sink.finish())
}
/// The most indexed metadata entries that can ever be used: objects and properties refer to one
/// with a single byte, and 0xff already means "stored inline", so only 0 to 254 are reachable.
const MAX_INDEXED_META: usize = 255;
/// Indexed metadata: a u8 length before each string. `out` starts with the empty entry 0. Reading
/// stops once `out` holds [`MAX_INDEXED_META`] entries, because no later entry can be referred to
/// and an empty one costs a whole `String` in memory for one byte of file.
fn read_indexed_meta(block: &[u8], out: &mut Vec<String>) -> Result<()> {
let mut c = Cursor { b: block, pos: 0 };
while c.pos < block.len() && out.len() < MAX_INDEXED_META {
let n = c.u8()? as usize;
out.push(c.string(n)?);
}
Ok(())
}
struct Walk<'a, 'b> {
o: Ogawa<'a>,
sink: &'b mut PolygonSink,
@@ -243,6 +243,38 @@ fn strip_alone_keeps_uvs_flipped_to_v_down() {
}
}
#[test]
fn half_float_uvs_import_without_losing_values() {
let mut a = Abc { w: Writer::new() };
let pp = a.array("P", "", F32, 3, &f32s(&[0.0, 0.0, 0.0, 0.5, 1.0, 0.0, 1.0, 0.0, 0.0]));
let fi = a.array(".faceIndices", "", I32, 1, &i32s(&[0, 1, 2]));
let fc = a.array(".faceCounts", "", I32, 1, &i32s(&[3]));
let uv_bytes: Vec<u8> = [0u16, 0, 0x3800, 0x3c00, 0x3c00, 0].iter().flat_map(|x| x.to_le_bytes()).collect();
let uv = a.array("uv", "", 9, 2, &uv_bytes);
let geom = a.compound(".geom", "schema=AbcGeom_PolyMesh_v1", vec![pp, fi, fc, uv]);
let mesh = a.object(vec![geom], &[]);
let top = a.object(vec![], &[("m", "", mesh)]);
let imported = crate::import_mesh_bytes("m.abc", &a.archive(top, &["schema=AbcGeom_PolyMesh_v1"])).unwrap();
assert!(!imported.report.uvs_generated);
for (p, uv) in imported.mesh.positions.iter().zip(&imported.mesh.uvs) {
assert_eq!(*uv, texturecraft_geom::glam::Vec2::new(p.x, 1.0 - p.y));
}
}
#[test]
fn half_float_values_preserve_special_values() {
let mut a = Abc { w: Writer::new() };
let raw: Vec<u8> = [0xc000u16, 0x0001, 0x7c00, 0x7e00].iter().flat_map(|x| x.to_le_bytes()).collect();
let p = a.array("values", "", 9, 1, &raw);
let property = Prop { name: p.name.into(), meta: p.meta, kind: PropKind::Array, pod: 9, extent: 1, group: p.group };
let mut ogawa = Ogawa { b: &a.w.b, budget: MAX_DECODED_BYTES, indexed_meta: Vec::new() };
let values = ogawa.values(&property).unwrap();
assert_eq!(values[0], -2.0);
assert_eq!(values[1], 2f64.powi(-24));
assert_eq!(values[2], f64::INFINITY);
assert!(values[3].is_nan());
}
#[test]
fn hdf5_and_garbage_are_clear_errors() {
let e = read(b"\x89HDF\r\n\x1a\n rest of an hdf5 file").err().unwrap().to_string();
@@ -291,3 +323,29 @@ fn self_referencing_groups_are_bounded() {
let bytes = a.archive(at, &[]);
assert!(read(&bytes).is_err());
}
#[test]
fn indexed_metadata_stops_at_the_entries_a_byte_can_index() {
// One real entry followed by a million empty ones: a byte each in the file, a `String` each
// in memory, and no index past 254 can be used anyway.
let mut block = vec![3, b'a', b'b', b'c'];
block.extend(std::iter::repeat_n(0u8, 1_000_000));
let mut kept = vec![String::new()];
read_indexed_meta(&block, &mut kept).unwrap();
assert_eq!(kept.len(), MAX_INDEXED_META);
assert_eq!(kept.get(1).map(String::as_str), Some("abc"));
// The mesh schema comes from metadata index 1 even when the table has unreachable entries.
let mut a = Abc { w: Writer::new() };
let pp = a.array("P", "", F32, 3, &f32s(&[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]));
let fi = a.array(".faceIndices", "", I32, 1, &i32s(&[0, 1, 2]));
let fc = a.array(".faceCounts", "", I32, 1, &i32s(&[3]));
let geom = a.compound(".geom", "schema=AbcGeom_PolyMesh_v1", vec![pp, fi, fc]);
let mesh = a.object(vec![geom], &[]);
let top = a.object(vec![], &[("tri", "", mesh)]);
let mut metas = vec!["schema=AbcGeom_PolyMesh_v1"];
metas.extend(std::iter::repeat_n("", 5_000));
let raw = read(&a.archive(top, &metas)).unwrap();
assert_eq!(raw.positions, [Vec3::X, Vec3::Y, Vec3::ZERO]);
assert_eq!(raw.groups, vec![(DEFAULT_MATERIAL.into(), "tri".into(), vec![0, 1, 2])]);
}
+119 -4
View File
@@ -32,6 +32,8 @@ const MIN_VERSION: i64 = 6000;
const MAX_DEPTH: usize = 64;
/// Memory budget for parsed nodes and properties (arrays are held as f64).
const MAX_DECODED_BYTES: usize = 1 << 31;
/// Maximum control points transformed across all mesh instances in one import.
const MAX_INSTANCE_POINTS: usize = 200_000_000;
/// Approximate memory charged per node and per scalar property.
const NODE_COST: usize = 96;
const PROP_COST: usize = 32;
@@ -128,14 +130,15 @@ impl Node {
if let Some(a) = self.child("a") {
return a.arr();
}
Cow::Owned(self.props.iter().filter_map(Prop::num).collect())
Cow::Owned(self.props.iter().map(|p| p.num().unwrap_or(f64::NAN)).collect())
}
/// The numbers of entry `name` in Properties70 (`P: name, type, subtype, flags, values`) or
/// Properties60 (`Property: name, type, flags, values`).
fn property(&self, name: &str) -> Option<Vec<f64>> {
let list = self.child("Properties70").or_else(|| self.child("Properties60"))?;
list.children.iter().find(|p| p.str_at(0) == name).map(|p| p.props.iter().filter_map(Prop::num).collect())
let first_value = if list.name == "Properties70" { 4 } else { 3 };
list.children.iter().find(|p| p.str_at(0) == name).map(|p| p.props.iter().skip(first_value).map(|v| v.num().unwrap_or(f64::NAN)).collect())
}
fn vec3(&self, name: &str, default: Vec3) -> Vec3 {
@@ -542,6 +545,9 @@ fn read_inner(bytes: &[u8], files: &mut Files) -> Result<RawMesh> {
attribute_of.insert(parent, *a);
}
}
// Repeated connections describe the same instance; keep distinct model placements in order.
let mut seen_instances = HashSet::new();
instances.retain(|instance| seen_instances.insert(instance.clone()));
let placed: HashSet<&String> = instances.iter().map(|(g, _)| g).collect();
let mut unplaced: Vec<String> = geometries.keys().filter(|g| !placed.contains(g)).cloned().collect();
unplaced.sort_unstable();
@@ -550,6 +556,7 @@ fn read_inner(bytes: &[u8], files: &mut Files) -> Result<RawMesh> {
let base_colors = base_colors(&materials, &textures, &videos, &connections, files);
let global = global_transform(root("GlobalSettings").or_else(|| objects.child("GlobalSettings")));
let mut sink = PolygonSink::default();
let mut remaining_points = MAX_INSTANCE_POINTS;
let no_slots = Vec::new();
for (geom, model) in instances {
let Some(g) = geometries.get(&geom) else { continue };
@@ -564,12 +571,15 @@ fn read_inner(bytes: &[u8], files: &mut Files) -> Result<RawMesh> {
}
};
let names = model.as_ref().and_then(|m| slots.get(m)).unwrap_or(&no_slots);
add_geometry(&mut sink, g, global * m, names)?;
add_geometry(&mut sink, g, global * m, names, &mut remaining_points)?;
}
let mut raw = sink.finish();
raw.base_colors = base_colors;
// In file order; FBX 6's built-in "Producer" views are not the scene's cameras.
for o in objects.children.iter().filter(|o| o.name == "Model" && o.class() == "Camera" && !o.object_name().starts_with("Producer ")) {
if raw.cameras.len() >= super::MAX_CAMERAS {
break;
}
let Some(id) = o.id_at(0) else { continue };
if let Ok(m) = world(&models, &parents, &id) {
raw.cameras.push(camera(o, attribute_of.get(&id).copied().unwrap_or(o), global * m));
@@ -783,14 +793,25 @@ impl Layer<'_> {
}
}
fn add_geometry(sink: &mut PolygonSink, geometry: &Node, transform: Mat4, slots: &[String]) -> Result<()> {
fn add_geometry(sink: &mut PolygonSink, geometry: &Node, transform: Mat4, slots: &[String], remaining_points: &mut usize) -> Result<()> {
let vertices = geometry.child("Vertices").map(Node::arr).unwrap_or_default();
check_vertices(vertices.len() / 3)?;
*remaining_points = remaining_points
.checked_sub(vertices.len() / 3)
.ok_or_else(|| FormatError::TooLarge(format!("FBX instances exceed the {MAX_INSTANCE_POINTS} control-point work limit")))?;
let points: Vec<Vec3> = vertices.as_chunks::<3>().0.iter().map(|c| transform.transform_point3(Vec3::new(c[0] as f32, c[1] as f32, c[2] as f32))).collect();
let polygon_indices = geometry.child("PolygonVertexIndex").map(Node::arr).unwrap_or_default();
let normals = Layer::read(geometry, "LayerElementNormal", "Normals", "NormalsIndex");
let uvs = Layer::read(geometry, "LayerElementUV", "UV", "UVIndex");
let materials = Layer::read(geometry, "LayerElementMaterial", "Materials", "");
if vertices.iter().chain(polygon_indices.iter()).any(|v| !v.is_finite())
|| [&normals, &uvs, &materials]
.into_iter()
.flatten()
.any(|layer| layer.data.iter().any(|v| !v.is_finite()) || layer.index.as_ref().is_some_and(|index| index.iter().any(|v| !v.is_finite())))
{
return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value"));
}
let normal_transform = Mat3::from_mat4(transform).inverse().transpose();
let flip = transform.determinant() < 0.0;
let (mut corners, mut polygon_index, mut bad_normals, mut bad_uvs) = (Vec::new(), 0usize, 0usize, 0usize);
@@ -839,3 +860,97 @@ fn add_geometry(sink: &mut PolygonSink, geometry: &Node, transform: Mat4, slots:
Ok(())
}
#[cfg(test)]
mod instance_budget_tests {
use super::*;
#[test]
fn ascii_array_keeps_invalid_values_in_place() {
let (nodes, _) = parse_ascii(
br#"FBXHeaderExtension: { FBXVersion: 7400 }
Vertices: 1, 1.#INF, 3, "x", 5
"#,
)
.unwrap();
let values = nodes[1].arr();
assert_eq!(values.len(), 5);
assert!(values[1].is_nan() && values[3].is_nan());
assert_eq!((values[0], values[2], values[4]), (1.0, 3.0, 5.0));
}
#[test]
fn ascii_bad_vertex_is_rejected() {
let (nodes, _) = parse_ascii(
br#"FBXHeaderExtension: { FBXVersion: 7400 }
Geometry: { Vertices: 0, 0, 0, 1, -1.#IND, 0, 0, 1, 0
PolygonVertexIndex: 0, 1, -3 }
"#,
)
.unwrap();
let mut remaining = usize::MAX;
let result = add_geometry(&mut PolygonSink::default(), &nodes[1], Mat4::IDENTITY, &[], &mut remaining);
assert!(result.is_err());
}
#[test]
fn ascii_bad_index_and_uv_are_rejected() {
for payload in [
"PolygonVertexIndex: 0, oops, -3",
"PolygonVertexIndex: 0, 1, -3\nLayerElementUV: 0 { MappingInformationType: \"ByPolygonVertex\"\nReferenceInformationType: \"Direct\"\nUV: 0, 0, oops, 1, 1, 0 }",
] {
let source = format!("FBXHeaderExtension: {{ FBXVersion: 7400 }}\nGeometry: {{ Vertices: 0, 0, 0, 1, 0, 0, 0, 1, 0\n{payload} }}");
let (nodes, _) = parse_ascii(source.as_bytes()).unwrap();
let mut remaining = usize::MAX;
assert!(add_geometry(&mut PolygonSink::default(), &nodes[1], Mat4::IDENTITY, &[], &mut remaining).is_err());
}
}
#[test]
fn ascii_transform_metadata_does_not_shift_values() {
let (nodes, _) = parse_ascii(
br#"FBXHeaderExtension: { FBXVersion: 7400 }
Model: { Properties70: { P: "Lcl Translation", "Lcl Translation", "", "A", 1, 2, 3 } }
"#,
)
.unwrap();
assert_eq!(nodes[1].property("Lcl Translation"), Some(vec![1.0, 2.0, 3.0]));
let (nodes, _) = parse_ascii(
br#"FBXHeaderExtension: { FBXVersion: 7400 }
Model: { Properties60: { Property: "Lcl Translation", "Lcl Translation", "A", 1, 2, 3 } }
"#,
)
.unwrap();
assert_eq!(nodes[1].property("Lcl Translation"), Some(vec![1.0, 2.0, 3.0]));
}
#[test]
fn malformed_transform_value_keeps_its_position_in_both_property_formats() {
for (list, entry) in [
("Properties70", "P: \"Lcl Translation\", \"Lcl Translation\", \"\", \"A\", 1, oops, 3"),
("Properties60", "Property: \"Lcl Translation\", \"Lcl Translation\", \"A\", 1, oops, 3"),
] {
let source = format!("FBXHeaderExtension: {{ FBXVersion: 7400 }}\nModel: {{ {list}: {{ {entry} }} }}");
let (nodes, _) = parse_ascii(source.as_bytes()).unwrap();
let values = nodes[1].property("Lcl Translation").unwrap();
assert_eq!(values.len(), 3);
assert_eq!((values[0], values[2]), (1.0, 3.0));
assert!(values[1].is_nan());
}
}
#[test]
fn instance_points_are_charged_before_transforming_each_copy() {
let geometry = Node {
name: "Geometry".into(),
props: vec![],
children: vec![Node { name: "Vertices".into(), props: vec![Prop::Arr(vec![0.0; 9])], children: vec![] }],
};
let mut sink = PolygonSink::default();
let mut remaining = 5;
add_geometry(&mut sink, &geometry, Mat4::IDENTITY, &[], &mut remaining).unwrap();
assert_eq!(remaining, 2);
assert!(matches!(add_geometry(&mut sink, &geometry, Mat4::IDENTITY, &[], &mut remaining), Err(FormatError::TooLarge(_))));
assert_eq!(remaining, 2);
}
}
+27 -1
View File
@@ -20,8 +20,34 @@ pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
crate::guard(KIND, || read_inner(bytes))
}
/// Loads and validates the document like `gltf::Gltf::from_slice`, except that a scene without a
/// `nodes` array is an empty scene. The `gltf` crate declares the field as required, but the
/// glTF 2.0 schema lists no required properties for a scene, and exporters write such scenes
/// for scenes that contain no objects.
fn parse(bytes: &[u8]) -> Result<(gltf::Document, Option<Vec<u8>>)> {
let (json, blob) = if bytes.starts_with(b"glTF") {
let mut glb = gltf::Glb::from_slice(bytes).map_err(|e| malformed(KIND, e))?;
let blob = glb.bin.take().map(std::borrow::Cow::into_owned);
(glb.json.into_owned(), blob)
} else {
(bytes.to_vec(), None)
};
let mut value: serde_json::Value = serde_json::from_slice(&json).map_err(|e| malformed(KIND, e))?;
if let Some(scenes) = value.get_mut("scenes").and_then(serde_json::Value::as_array_mut) {
for scene in scenes {
if let Some(scene) = scene.as_object_mut() {
scene.entry("nodes").or_insert_with(|| serde_json::Value::Array(Vec::new()));
}
}
}
let json = serde_json::to_vec(&value).map_err(|e| malformed(KIND, e))?;
let root = gltf::json::Root::from_slice(&json).map_err(|e| malformed(KIND, e))?;
let document = gltf::Document::from_json(root).map_err(|e| malformed(KIND, e))?;
Ok((document, blob))
}
fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
let gltf::Gltf { document, blob } = gltf::Gltf::from_slice(bytes).map_err(|e| malformed(KIND, e))?;
let (document, blob) = parse(bytes)?;
check_buffer_uris(&document)?;
let buffers = gltf::import_buffers(&document, None, blob).map_err(|e| match e {
gltf::Error::ExternalReferenceInSliceImport => FormatError::Unsupported(
+12 -8
View File
@@ -36,10 +36,14 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
let materials = materials.unwrap_or_default();
let mut raw = RawMesh::default();
let (mut normals, mut uvs, mut colors) = (Some(Vec::new()), Some(Vec::new()), Vec::new());
let any_uvs = models.iter().any(|m| !m.mesh.indices.is_empty() && m.mesh.texcoords.len() == (m.mesh.positions.len() / 3) * 2);
let (mut normals, mut uvs, mut colors) = (Some(Vec::new()), any_uvs.then(Vec::new), Vec::new());
let mut partial_uvs = false;
for model in &models {
let mesh = &model.mesh;
if mesh.indices.is_empty() {
continue;
}
let vertex_count = mesh.positions.len() / 3;
check_vertices(raw.positions.len().saturating_add(vertex_count))?;
@@ -56,14 +60,14 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
} else {
normals = None;
}
if mesh.texcoords.len() == vertex_count * 2 {
if let Some(v) = &mut uvs {
if let Some(v) = &mut uvs {
if mesh.texcoords.len() == vertex_count * 2 {
// OBJ UVs have their origin bottom-left
v.extend(mesh.texcoords.as_chunks::<2>().0.iter().map(|t| Vec2::new(t[0], 1.0 - t[1])));
} else {
v.extend(std::iter::repeat_n(Vec2::ZERO, vertex_count));
partial_uvs = true;
}
} else {
partial_uvs |= vertex_count > 0 && !mesh.indices.is_empty();
uvs = None;
}
// `v x y z r g b`: objects without colors are white
@@ -94,8 +98,8 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
}
}
if partial_uvs && models.iter().any(|model| !model.mesh.texcoords.is_empty()) {
raw.warnings.push("some objects have no UVs; the whole mesh was unwrapped automatically".into());
if partial_uvs {
raw.warnings.push("some objects have no UVs; default UVs were assigned".into());
}
raw.normals = normals;
+71 -2
View File
@@ -299,6 +299,18 @@ fn fbx_binary_32_and_64_bit_compressed_or_not() {
}
/// [`scene`] with a diffuse color on Red and an embedded diffuse texture on Blue.
#[test]
fn fbx_camera_models_are_capped_during_import() {
let mut nodes = scene(false);
let objects = nodes.iter_mut().find(|node| node.name == "Objects").unwrap();
for id in 100..199 {
objects.children.push(n("Model", vec![P::L(id), P::S("Camera\\u{0}\\u{1}Model"), P::S("Camera")], vec![]));
}
let raw = fbx::read(&binary_fbx(&nodes, 7500, true)).unwrap();
assert_eq!(raw.cameras.len(), MAX_CAMERAS);
}
fn textured_scene(png: &[u8]) -> Vec<N> {
let mut nodes = scene(false);
let objects = nodes.iter_mut().find(|n| n.name == "Objects").unwrap();
@@ -415,6 +427,24 @@ fn fbx_ascii_and_z_up() {
check_fbx_scene(&import_mesh_bytes("z.fbx", &binary_fbx(&scene(true), 7700, true)).unwrap().mesh, true);
}
#[test]
fn fbx_duplicate_geometry_connections_do_not_duplicate_instances() {
let mut nodes = scene(false);
let connections = nodes.iter_mut().find(|node| node.name == "Connections").unwrap();
for _ in 0..50 {
connections.children.push(n("C", vec![P::S("OO"), P::L(10), P::L(21)], vec![]));
}
let imported = import_mesh_bytes("scene.fbx", ascii_fbx(&nodes).as_bytes()).unwrap();
assert_eq!(imported.report.triangles, 3);
let objects = nodes.iter_mut().find(|node| node.name == "Objects").unwrap();
objects.children.push(n("Model", vec![P::L(22), P::S("Other\u{0}\u{1}Model"), P::S("Mesh")], vec![]));
let connections = nodes.iter_mut().find(|node| node.name == "Connections").unwrap();
connections.children.push(n("C", vec![P::S("OO"), P::L(10), P::L(22)], vec![]));
let imported = import_mesh_bytes("scene.fbx", ascii_fbx(&nodes).as_bytes()).unwrap();
assert_eq!(imported.report.triangles, 6, "a different model must remain a distinct instance");
}
/// The scene of [`scene`] (Y up) as FBX 6.1 writes it: geometry inside the Model, objects named
/// `Class::Name`, `Properties60`, `Connect` records, GlobalSettings inside Objects.
const FBX61: &str = r#"; FBX 6.1.0 project file
@@ -589,19 +619,55 @@ fn fbx_old_versions_and_garbage_are_errors() {
assert!(import_mesh_bytes("h.fbx", &huge).is_err());
}
// Keep these fixtures independently schedulable: one serial test took 24 minutes in
// instrumented coverage. Splitting cases keeps every mutation and assertion intact.
#[test]
fn malformed_fbx_and_dae_never_panic() {
fn malformed_fbx_and_dae_never_panic_binary_7400() {
hammer("x.fbx", &binary_fbx(&scene(false), 7400, false));
}
#[test]
fn malformed_fbx_and_dae_never_panic_binary_7500() {
hammer("x.fbx", &binary_fbx(&scene(true), 7500, true));
}
#[test]
fn malformed_fbx_and_dae_never_panic_ascii() {
hammer("x.fbx", ascii_fbx(&scene(false)).as_bytes());
}
#[test]
fn malformed_fbx_and_dae_never_panic_collada() {
hammer("x.dae", DAE.as_bytes());
}
#[test]
fn malformed_fbx_and_dae_never_panic_random_binary() {
random_scene_corruptions(0);
}
#[test]
fn malformed_fbx_and_dae_never_panic_random_ascii() {
random_scene_corruptions(1);
}
#[test]
fn malformed_fbx_and_dae_never_panic_random_collada() {
random_scene_corruptions(2);
}
fn random_scene_corruptions(selected_fixture: usize) {
// Random multi-byte corruptions (a fixed LCG keeps failures reproducible).
let mut seed = 0x2545_f491_4f6c_dd1du64;
let mut next = move || {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
(seed >> 33) as usize
};
for good in [binary_fbx(&scene(false), 7500, true), ascii_fbx(&scene(true)).into_bytes(), DAE.as_bytes().to_vec()] {
// Replay earlier mutations without parsing them so each fixture receives exactly
// its original LCG stream, not a fresh seed. Fixture sizes and all 1,500 cases
// per fixture stay unchanged; generating these bytes is cheap next to parsing.
let fixtures = [binary_fbx(&scene(false), 7500, true), ascii_fbx(&scene(true)).into_bytes(), DAE.as_bytes().to_vec()];
for (fixture, good) in fixtures.into_iter().enumerate().take(selected_fixture + 1) {
let name = if good.starts_with(b"<?xml") { "r.dae" } else { "r.fbx" };
for _ in 0..1500 {
let mut b = good.clone();
@@ -609,6 +675,9 @@ fn malformed_fbx_and_dae_never_panic() {
let i = next() % b.len();
b[i] = next() as u8;
}
if fixture != selected_fixture {
continue;
}
if let Err(e) = import_mesh_bytes(name, &b) {
assert!(!e.to_string().contains("parser failed"), "{name}: a parser panicked");
}
+69
View File
@@ -118,6 +118,48 @@ fn glb_two_materials_mirrored_node() {
m.validate().unwrap();
}
/// A scene without a `nodes` array is legal glTF (Blender writes one for a scene with no exported
/// objects); it must not stop the default scene from importing.
#[test]
fn gltf_scene_without_nodes_imports_the_default_scene() {
let mut json: serde_json::Value = serde_json::from_slice(&embedded_gltf()).unwrap();
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }, { "nodes": [0] }]);
json["scene"] = 1.into();
let im = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap();
assert_eq!(im.report.triangles, 2);
// Only an empty scene: nothing to import, but a clear error rather than a parse failure.
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }]);
json["scene"] = 0.into();
let err = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap_err();
assert!(!err.to_string().contains("missing field"), "{err}");
}
#[test]
fn glb_scene_without_nodes_keeps_binary_mesh_in_default_scene() {
let original = glb(true, true);
let jlen = u32::from_le_bytes(original[12..16].try_into().unwrap()) as usize;
let mut json: serde_json::Value = serde_json::from_slice(&original[20..20 + jlen]).unwrap();
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }, { "nodes": [0] }]);
json["scene"] = 1.into();
let mut js = serde_json::to_vec(&json).unwrap();
while !js.len().is_multiple_of(4) {
js.push(b' ');
}
let bin_chunk = &original[20 + jlen..];
let mut glb = Vec::new();
glb.extend(b"glTF");
glb.extend(2u32.to_le_bytes());
glb.extend(((20 + js.len() + bin_chunk.len()) as u32).to_le_bytes());
glb.extend((js.len() as u32).to_le_bytes());
glb.extend(b"JSON");
glb.extend(js);
glb.extend(bin_chunk);
let im = import_mesh_bytes("scene.glb", &glb).unwrap();
assert_eq!(im.report.triangles, 2);
assert_eq!(im.mesh.materials, ["Red", "Material_1"]);
assert_outward(&im.mesh);
}
#[test]
fn gltf_embedded_buffers_and_missing_attributes() {
let im = import_mesh_bytes("scene.gltf", &embedded_gltf()).unwrap();
@@ -235,6 +277,33 @@ fn obj_without_uvs_or_normals_is_completed() {
assert!(n0.len() > 1 && n0.iter().all(|n| (*n - n0[0]).length() < 1e-5));
}
#[test]
fn obj_with_some_objects_having_uvs_preserves_uvs_without_full_unwrap() {
let obj = "o Body
v 0 0 0
v 1 0 0
v 1 1 0
vt 0 0
vt 1 0
vt 1 1
f 1/1 2/2 3/3
o Helper
v 2 0 0
v 3 0 0
v 3 1 0
f 4 5 6
";
let im = import_mesh_bytes("character.obj", obj.as_bytes()).unwrap();
assert!(!im.report.uvs_generated);
assert_eq!(im.mesh.uvs[0], Vec2::new(0.0, 1.0));
assert_eq!(im.mesh.uvs[1], Vec2::new(1.0, 1.0));
assert_eq!(im.mesh.uvs[2], Vec2::new(1.0, 0.0));
assert_eq!(im.mesh.uvs[3], Vec2::ZERO);
assert_eq!(im.mesh.uvs[4], Vec2::ZERO);
assert_eq!(im.mesh.uvs[5], Vec2::ZERO);
assert!(im.report.warnings.iter().any(|w| w.contains("some objects have no UVs")));
}
#[test]
fn polygons_keep_their_edges_for_the_wireframe() {
// An OBJ quad and pentagon (with UVs, so no unwrap splits them), a triangle, and a PLY quad.
+88 -34
View File
@@ -15,6 +15,9 @@ const MIN_VERSION: (u8, u8, u8) = (0, 4, 0);
const MAX_SECTIONS: usize = 64;
/// Items in one table or array.
const MAX_ITEMS: usize = 1 << 28;
/// Entries in the PATHS table. The declared count is tiny in the file but each entry is a
/// materialised path, so it needs its own ceiling as well as the decoded-byte budget.
const MAX_PATHS: usize = 4_000_000;
/// Spec types.
const SPEC_ATTRIBUTE: i64 = 1;
@@ -242,58 +245,73 @@ impl<'a> Crate<'a> {
/// Paths are stored as a pre-order tree: element token (negative for properties) and a
/// jump per node (-2 leaf, -1 child only, 0 sibling only, >0 child and sibling at +jump).
fn read_paths(&mut self, mut s: Cur, budget: &mut Budget) -> Result<()> {
let total = s.count(0)?;
let n = s.count(0)?;
let index = s.ints(n, 4, budget)?;
let elements = s.ints(n, 4, budget)?;
let jumps = s.ints(n, 4, budget)?;
budget.take(total.saturating_mul(64))?;
self.paths = vec![None; total];
let mut work: Vec<(usize, Option<usize>)> = vec![(0, None)];
let mut steps = 0usize;
let bad = || malformed(KIND, "corrupt path table");
while let Some((mut i, mut parent)) = work.pop() {
fn read_paths(&mut self, mut section: Cur, budget: &mut Budget) -> Result<()> {
let path_count = section.count(0)?;
let encoded_count = section.count(0)?;
let path_indices = section.ints(encoded_count, 4, budget)?;
let elements = section.ints(encoded_count, 4, budget)?;
let jumps = section.ints(encoded_count, 4, budget)?;
if path_count > MAX_PATHS {
return Err(FormatError::TooLarge(format!("the USD path table has {path_count} entries (limit {MAX_PATHS})")));
}
// Charge slot storage before allocating the table
budget.take(path_count.saturating_mul(std::mem::size_of::<Option<PathEl>>()))?;
self.paths = vec![None; path_count];
let mut pending_branches: Vec<(usize, Option<usize>)> = vec![(0, None)];
let mut visited_count = 0usize;
let path_error = || malformed(KIND, "corrupt path table");
while let Some((mut cursor_index, mut parent)) = pending_branches.pop() {
loop {
steps += 1;
if steps > n {
return Err(bad());
visited_count += 1;
if visited_count > encoded_count {
return Err(path_error());
}
let this = i;
i += 1;
let at = index.get(this).map(|v| *v as u32 as usize).filter(|p| *p < total).ok_or_else(bad)?;
let el = match parent {
let node_index = cursor_index;
cursor_index += 1;
let path_index =
path_indices.get(node_index).map(|v| *v as u32 as usize).filter(|parent_index| *parent_index < path_count).ok_or_else(path_error)?;
let path_element = match parent {
None => PathEl { text: "/".into(), parent: None, name: String::new(), prop: false },
Some(p) => {
let e = *elements.get(this).ok_or_else(bad)?;
let name = self.token(e.unsigned_abs())?.to_string();
let base = self.paths.get(p).and_then(Option::as_ref).map(|b| b.text.as_str()).ok_or_else(bad)?;
let text = match (e < 0, base) {
(true, _) => format!("{base}.{name}"),
(false, _) if name.starts_with('{') => format!("{base}{name}"),
Some(parent_index) => {
let element_token = *elements.get(node_index).ok_or_else(path_error)?;
let name = self.token(element_token.unsigned_abs())?;
let parent_path = self.paths.get(parent_index).and_then(Option::as_ref).map(|b| b.text.as_str()).ok_or_else(path_error)?;
let separator_bytes = usize::from(element_token < 0 || (!name.starts_with('{') && parent_path != "/"));
let text_bytes = parent_path.len().saturating_add(name.len()).saturating_add(separator_bytes);
// Charge both retained strings before allocating either one
budget.take(text_bytes.saturating_add(name.len()))?;
let name = name.to_string();
let text = match (element_token < 0, parent_path) {
(true, _) => format!("{parent_path}.{name}"),
(false, _) if name.starts_with('{') => format!("{parent_path}{name}"),
(false, "/") => format!("/{name}"),
(false, _) => format!("{base}/{name}"),
(false, _) => format!("{parent_path}/{name}"),
};
budget.take(text.len())?;
PathEl { text, parent: Some(p), name, prop: e < 0 }
PathEl { text, parent: Some(parent_index), name, prop: element_token < 0 }
}
};
if let Some(slot) = self.paths.get_mut(at) {
*slot = Some(el);
if let Some(slot) = self.paths.get_mut(path_index) {
*slot = Some(path_element);
}
let jump = *jumps.get(this).ok_or_else(bad)?;
let jump = *jumps.get(node_index).ok_or_else(path_error)?;
let (child, sibling) = (jump > 0 || jump == -1, jump >= 0);
if child {
if sibling {
work.push((this.checked_add(usize::try_from(jump).map_err(|_| bad())?).ok_or_else(bad)?, parent));
pending_branches.push((node_index.checked_add(usize::try_from(jump).map_err(|_| path_error())?).ok_or_else(path_error)?, parent));
}
parent = Some(at);
parent = Some(path_index);
}
if !child && !sibling {
break;
}
}
}
Ok(())
}
@@ -647,6 +665,42 @@ mod tests {
out.extend_from_slice(&comp);
}
#[test]
fn repeated_path_names_count_toward_the_decode_budget() {
let name = "long_name_reused";
let mut section = Vec::new();
section.extend_from_slice(&3u64.to_le_bytes());
section.extend_from_slice(&3u64.to_le_bytes());
compressed_ints(&mut section, &[0, 1, 2]);
compressed_ints(&mut section, &[0, 0, 0]);
compressed_ints(&mut section, &[-1, 0, -2]);
let mut crate_data = crate_over(&section, (0, 8, 0));
crate_data.tokens.push(name.into());
let decoded_arrays_bytes = 3 * 3 * std::mem::size_of::<i64>();
let path_slots_bytes = 3 * std::mem::size_of::<Option<PathEl>>();
let path_text_bytes = 2 * (1 + name.len());
let one_name_budget = decoded_arrays_bytes + path_slots_bytes + path_text_bytes + name.len();
let mut insufficient_budget = Budget(one_name_budget);
let rejected = crate_data.read_paths(Cur { b: &section, pos: 0 }, &mut insufficient_budget);
assert!(matches!(rejected, Err(FormatError::TooLarge(_))), "two path names cannot fit a one-name budget");
drop(crate_data);
let mut complete_budget = Budget(one_name_budget + name.len());
let mut crate_data = crate_over(&section, (0, 8, 0));
crate_data.tokens.push(name.into());
crate_data.read_paths(Cur { b: &section, pos: 0 }, &mut complete_budget).unwrap();
assert_eq!(complete_budget.0, 0);
assert_eq!(crate_data.path(1).unwrap().text, format!("/{name}"));
assert_eq!(crate_data.path(2).unwrap().text, format!("/{name}"));
let retained_name_bytes: usize = crate_data.paths.iter().filter_map(Option::as_ref).map(|path| path.name.capacity()).sum();
assert!(retained_name_bytes >= 2 * name.len());
println!("retained names: 2, buffer capacity: {retained_name_bytes} bytes");
}
#[test]
fn compressed_float_and_int_arrays() {
let n = 20usize;
+13 -13
View File
@@ -98,23 +98,23 @@ pub fn save_project_bytes(pf: &ProjectFile) -> Result<Vec<u8>> {
Ok(out.into_inner())
}
/// Save atomically: `<path>.tmp` first, then rename over `path`.
/// Save atomically through a newly created temporary file beside `path`.
#[cfg(not(target_arch = "wasm32"))]
pub fn save_project(path: &std::path::Path, pf: &ProjectFile) -> Result<()> {
let mut tmp = path.as_os_str().to_owned();
tmp.push(".tmp");
let tmp = std::path::PathBuf::from(tmp);
let res = (|| -> Result<()> {
let mut file = std::io::BufWriter::new(std::fs::File::create(&tmp)?);
write_project(&mut file, pf)?;
file.into_inner().map_err(|e| e.into_error())?.sync_all()?;
std::fs::rename(&tmp, path)?;
pub fn save_project(path: &std::path::Path, project_file: &ProjectFile) -> Result<()> {
let (temporary_path, file) = crate::create_temp(path)?;
let result = (|| -> Result<()> {
let mut file = std::io::BufWriter::new(file);
write_project(&mut file, project_file)?;
file.into_inner().map_err(|error| error.into_error())?.sync_all()?;
std::fs::rename(&temporary_path, path)?;
Ok(())
})();
if res.is_err() {
let _ = std::fs::remove_file(&tmp);
if result.is_err() {
let _ = std::fs::remove_file(&temporary_path);
}
res
result
}
fn zip_err(e: zip::result::ZipError) -> FormatError {
+16
View File
@@ -353,6 +353,13 @@ impl Default for BrushSettings {
}
impl BrushSettings {
/// Radius of a 2D (UV) stamp as a fraction of the UV square, for pen `pressure` 0..1: twice the
/// brush size, shrunk towards `min_size` by a light pen. Painting and the 2D cursor share it.
pub fn uv_stamp_radius(&self, pressure: f32) -> f32 {
let p = pressure.clamp(0.0, 1.0);
self.size * 2.0 * if p < 1.0 { self.min_size + (1.0 - self.min_size) * p } else { 1.0 }
}
/// Clamp every value into its valid range (input from UI, CLI or agents)
pub fn sanitized(&self) -> BrushSettings {
let clamp_finite = |value: f32, min: f32, max: f32, default: f32| if value.is_finite() { value.clamp(min, max) } else { default };
@@ -392,6 +399,15 @@ impl BrushSettings {
#[cfg(test)]
mod tests {
#[test]
fn uv_stamp_radius_follows_pen_pressure() {
let b = BrushSettings { size: 0.1, min_size: 0.5, ..BrushSettings::default() };
assert!((b.uv_stamp_radius(1.0) - 0.2).abs() < 1e-6);
assert!((b.uv_stamp_radius(0.5) - 0.15).abs() < 1e-6);
assert!((b.uv_stamp_radius(0.0) - 0.1).abs() < 1e-6);
assert!((b.uv_stamp_radius(7.0) - 0.2).abs() < 1e-6, "pressure clamps to 1");
}
use super::*;
#[test]
+2
View File
@@ -14,6 +14,7 @@ texturecraft-render = { workspace = true }
eframe = { workspace = true, features = ["default_fonts", "wgpu", "persistence"] }
egui = { workspace = true }
craft-ui = { workspace = true }
craft-i18n = { workspace = true }
craft-window = { workspace = true }
craft-keymap = { workspace = true }
craft-quit = { workspace = true }
@@ -39,6 +40,7 @@ craft-dialogs = { workspace = true }
# build.rs: the interface faces lose their Private Use Area, where the icon font draws (the
# versions craft-libs' craft-fonts-build subsets with).
[build-dependencies]
craft-i18n = { workspace = true }
skera = "0.8"
skrifa = { version = "0.48", default-features = false, features = ["std"] }
+10 -1
View File
@@ -4,6 +4,8 @@
//! recipe follows craft-fonts' `docs/integration.md`; no font file is committed here. The Latin
//! faces are embedded without their Private Use Area ([`without_private_use`]).
mod i18n_build;
use std::fmt::Write as _;
use std::path::PathBuf;
@@ -11,7 +13,7 @@ use std::path::PathBuf;
/// has a download budget: of the CJK faces only the Japanese one, ~4.5 MB).
const MANIFESTS: &[(&str, Faces, Faces)] = &[
("fonts/manifest.txt", &[("BIZ UDPGothic", "Regular"), ("Noto Sans CJK SC", "Regular"), ARABIC], &[("BIZ UDPGothic", "Regular"), ARABIC]),
// The interface font, regular and semibold (0.8 MB).
// The interface font has regular and semibold faces
(LATIN_MANIFEST, LATIN, LATIN),
];
/// The Arabic face (0.8 MB, so the web build has it too).
@@ -23,6 +25,7 @@ const LATIN: Faces = &[("Inter", "Regular"), ("Inter", "SemiBold")];
type Faces = &'static [(&'static str, &'static str)];
fn main() {
i18n_build::embed();
println!("cargo::rerun-if-env-changed=CRAFT_FONTS_DIR");
println!("cargo::rerun-if-env-changed=CRAFT_FONTS_REQUIRED");
let mut src = String::from("pub static CRAFT_FONTS: &[CraftFont] = &[\n");
@@ -35,6 +38,7 @@ fn main() {
Err(e) => println!("cargo::warning=building without craft-fonts: CRAFT_FONTS_DIR={}: {e}", dir.display()),
}
}
src.push_str("];\n");
let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap_or_default()).join("craft_fonts.rs");
if let Err(e) = std::fs::write(&out, src) {
@@ -49,6 +53,7 @@ fn craft_fonts(dir: &std::path::Path, rel: &str, desktop: &[(&str, &str)], web:
if rel == LATIN_MANIFEST && !manifest.exists() {
return Ok(String::new());
}
let text = std::fs::read_to_string(&manifest).map_err(|e| format!("{}: {e}", manifest.display()))?;
let wasm = std::env::var("CARGO_CFG_TARGET_ARCH").is_ok_and(|a| a == "wasm32");
let wanted = if wasm { web } else { desktop };
@@ -58,9 +63,11 @@ fn craft_fonts(dir: &std::path::Path, rel: &str, desktop: &[(&str, &str)], web:
let [family, style, file, scripts, ..] = f.as_slice() else {
return Err(format!("malformed manifest line: {line}"));
};
if !wanted.contains(&(*family, *style)) {
continue;
}
let mut path = dir.join(file).canonicalize().map_err(|e| format!("{file}: {e}"))?;
println!("cargo::rerun-if-changed={}", path.display());
if rel == LATIN_MANIFEST {
@@ -69,6 +76,7 @@ fn craft_fonts(dir: &std::path::Path, rel: &str, desktop: &[(&str, &str)], web:
std::fs::write(&out, without_private_use(&bytes).map_err(|e| format!("{file}: {e}"))?).map_err(|e| format!("{}: {e}", out.display()))?;
path = out;
}
let scripts: Vec<String> = scripts.split(',').map(|s| format!("{:?}", s.trim())).collect();
let _ = writeln!(
out,
@@ -77,6 +85,7 @@ fn craft_fonts(dir: &std::path::Path, rel: &str, desktop: &[(&str, &str)], web:
path.display().to_string(),
);
}
Ok(out)
}
+98
View File
@@ -0,0 +1,98 @@
//! Optional shared translation catalogs, embedded at build time.
use std::fmt::Write as _;
use std::path::Path;
/// Generate Rust includes for the optional catalog checkout; required input must contain app catalogs.
/// Paths are escaped as Rust strings so Windows separators and spaces remain valid.
pub fn generate(dir: Option<&Path>, required: bool) -> Result<String, String> {
let mut source = String::from("pub static CRAFT_I18N: &craft_i18n::Embedded = &[\n");
let Some(dir) = dir else {
if required {
return Err("CRAFT_I18N_REQUIRED=1 requires CRAFT_I18N_DIR".into());
}
source.push_str("];\n");
return Ok(source);
};
if !dir.is_absolute() {
return Err("CRAFT_I18N_DIR must be an absolute path".into());
}
let locales = dir.join("locales");
let entries = std::fs::read_dir(&locales).map_err(|e| format!("{}: {e}", locales.display()))?;
println!("cargo::rerun-if-changed={}", locales.display());
let mut paths = Vec::new();
for entry in entries {
let entry = entry.map_err(|e| e.to_string())?;
if entry.file_type().map_err(|e| e.to_string())?.is_dir() {
paths.push(entry.path());
}
}
paths.sort();
let mut count = 0;
let mut catalogs = Vec::new();
for path in paths {
// Watch directories so adding or removing an app catalog rebuilds the embedding
println!("cargo::rerun-if-changed={}", path.display());
let app = path.join("texturecraft.tsv");
if !app.is_file() {
continue;
}
let locale = path.file_name().and_then(|n| n.to_str()).ok_or("non-UTF-8 locale")?;
let common = path.join("common.tsv");
let embed = |file: &Path| -> Result<(String, &'static str), String> {
let text = std::fs::read_to_string(file).map_err(|e| format!("{}: {e}", file.display()))?;
let name = file.to_str().ok_or("non-UTF-8 catalog path")?;
println!("cargo::rerun-if-changed={name}");
// The shared runtime needs static inputs for this short-lived build process
Ok((format!("include_str!({name:?})"), Box::leak(text.into_boxed_str())))
};
let (shared, shared_text) = if common.is_file() { embed(&common)? } else { ("\"\"".into(), "") };
let (app, app_text) = embed(&app)?;
let code: &'static str = Box::leak(locale.to_owned().into_boxed_str());
catalogs.push((code, shared_text, app_text));
let _ = writeln!(source, " ({locale:?}, {shared}, {app}),");
count += 1;
}
if required && count == 0 {
return Err("no TextureCraft translation catalogs found".into());
}
// Reject rows the runtime would skip before required builds can ship English fallbacks
let (runtime, mut problems) = craft_i18n::I18n::new(Box::leak(catalogs.into_boxed_slice()), &[]);
problems.extend(runtime.check_all());
if !problems.is_empty() {
return Err(format!("invalid translation catalogs in {}: {}", locales.display(), problems.join("; ")));
}
source.push_str("];\n");
Ok(source)
}
/// Write the embedding for both desktop and wasm, reporting optional failures as warnings.
/// Required input errors stop Cargo instead of silently shipping an English-only release.
pub fn embed() {
println!("cargo::rerun-if-env-changed=CRAFT_I18N_DIR");
println!("cargo::rerun-if-env-changed=CRAFT_I18N_REQUIRED");
let dir = std::env::var_os("CRAFT_I18N_DIR").map(std::path::PathBuf::from);
let required = std::env::var("CRAFT_I18N_REQUIRED").is_ok_and(|v| v == "1");
let source = match generate(dir.as_deref(), required) {
Ok(source) => source,
Err(error) => {
if required {
println!("cargo::error={error}");
} else {
println!("cargo::warning=building without craft-i18n: {error}");
}
"pub static CRAFT_I18N: &craft_i18n::Embedded = &[];\n".into()
}
};
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap_or_default()).join("craft_i18n.rs");
if let Err(error) = std::fs::write(&out, source) {
println!("cargo::error=writing {}: {error}", out.display());
}
}
+5 -4
View File
@@ -2365,17 +2365,18 @@ fn generator_properties(app: &mut TextureCraftApp, ui: &mut Ui, id: u64, generat
}
});
let (mut level, mut softness, mut noise, mut noise_scale, mut invert) =
(generator.level, generator.softness, generator.noise_amount, generator.noise_scale, generator.invert);
let (mut level, mut softness, mut noise, mut noise_scale, mut offset, mut invert) =
(generator.level, generator.softness, generator.noise_amount, generator.noise_scale, generator.offset, generator.invert);
let level_edit = slider_edit(ui, tr("Level"), &mut level, 0.0..=1.0, 2);
let softness_edit = slider_edit(ui, tr("Softness"), &mut softness, 0.0..=1.0, 2);
let noise_edit = slider_edit(ui, tr("Noise amount"), &mut noise, 0.0..=2.0, 2);
let noise_scale_edit = slider_edit(ui, tr("Noise scale"), &mut noise_scale, 0.1..=64.0, 1);
let offset_edit = slider_edit(ui, tr("Offset"), &mut offset, -1.0..=1.0, 2);
let invert_edit = ui.checkbox(&mut invert, tr("Invert")).changed().then_some(false);
if let Some(merge) = level_edit.or(softness_edit).or(noise_edit).or(noise_scale_edit).or(invert_edit) {
if let Some(merge) = level_edit.or(softness_edit).or(noise_edit).or(noise_scale_edit).or(offset_edit).or(invert_edit) {
app.run(
"effect.set",
json!({"effect": id, "level": level, "softness": softness, "noise": noise, "noiseScale": noise_scale, "invert": invert, "merge": merge}),
json!({"effect": id, "level": level, "softness": softness, "noise": noise, "noiseScale": noise_scale, "offset": offset, "invert": invert, "merge": merge}),
);
}
+139 -115
View File
@@ -1,22 +1,32 @@
//! UI text lookup. Keys are the English strings, so English needs no table and a missing
//! translation falls back to English. Every user-visible literal goes through [`tr`] (or [`trf`]
//! for text with `{name}` placeholders); each language is a static `(English, translation)`
//! table, indexed into a hash map the first time the language is used. `Prefs::language` picks it.
//! UI text lookup over the optional shared craft-i18n catalogs.
//! Missing translations and builds without catalogs use the English source text.
use std::collections::HashMap;
use std::fmt::Display;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{LazyLock, OnceLock};
mod ar;
mod de;
mod es;
mod fr;
mod it;
mod ja;
mod ko;
mod pt_br;
mod zh_hans;
include!(concat!(env!("OUT_DIR"), "/craft_i18n.rs"));
static I18N: LazyLock<craft_i18n::I18n> = LazyLock::new(|| {
let offered: Vec<_> = LANGUAGES.iter().map(|l| l.code).collect();
let (i18n, problems) = craft_i18n::I18n::new(CRAFT_I18N, &offered);
for problem in problems {
log::warn!("translation catalog: {problem}");
}
i18n
});
static SHARED_LANGUAGES: OnceLock<[craft_i18n::Lang; COUNT]> = OnceLock::new();
/// Resolve the fixed picker codes once; hot text lookups only load the cached language handle.
fn shared_languages() -> &'static [craft_i18n::Lang; COUNT] {
SHARED_LANGUAGES.get_or_init(|| LANGUAGES.each_ref().map(|l| I18N.from_code(l.code).unwrap_or(craft_i18n::Lang::EN)))
}
/// Whether this build embeds a catalog for a supported language.
pub fn available(code: &str) -> bool {
I18N.from_code(code).is_some()
}
pub struct Language {
/// BCP 47 code ("en", "pt-BR", "zh-Hans").
@@ -25,26 +35,23 @@ pub struct Language {
pub name: &'static str,
/// Its English name (the picker's fallback when no font can draw `name`).
pub english: &'static str,
/// `(English, translation)` pairs, in any order.
pub table: &'static [(&'static str, &'static str)],
}
const COUNT: usize = 10;
pub static LANGUAGES: [Language; COUNT] = [
Language { code: "en", name: "English", english: "English", table: &[] },
Language { code: "fr", name: "Français", english: "French", table: fr::TABLE },
Language { code: "de", name: "Deutsch", english: "German", table: de::TABLE },
Language { code: "es", name: "Español", english: "Spanish", table: es::TABLE },
Language { code: "pt-BR", name: "Português (Brasil)", english: "Portuguese (Brazil)", table: pt_br::TABLE },
Language { code: "it", name: "Italiano", english: "Italian", table: it::TABLE },
Language { code: "ja", name: "日本語", english: "Japanese", table: ja::TABLE },
Language { code: "zh-Hans", name: "简体中文", english: "Chinese (Simplified)", table: zh_hans::TABLE },
Language { code: "ko", name: "한국어", english: "Korean", table: ko::TABLE },
Language { code: "ar", name: "العربية", english: "Arabic", table: ar::TABLE },
Language { code: "en", name: "English", english: "English" },
Language { code: "fr", name: "Français", english: "French" },
Language { code: "de", name: "Deutsch", english: "German" },
Language { code: "es", name: "Español", english: "Spanish" },
Language { code: "pt-BR", name: "Português (Brasil)", english: "Portuguese (Brazil)" },
Language { code: "it", name: "Italiano", english: "Italian" },
Language { code: "ja", name: "日本語", english: "Japanese" },
Language { code: "zh-Hans", name: "简体中文", english: "Chinese (Simplified)" },
Language { code: "ko", name: "한국어", english: "Korean" },
Language { code: "ar", name: "العربية", english: "Arabic" },
];
static MAPS: [OnceLock<HashMap<&'static str, &'static str>>; COUNT] = [const { OnceLock::new() }; COUNT];
static CURRENT: AtomicUsize = AtomicUsize::new(0);
/// Display labels belong to the UI; the model exposes only map identities
@@ -67,7 +74,7 @@ pub(crate) fn baked_map_label(map: texturecraft_engine::core::BakedMap) -> &'sta
/// Switch the UI language (unknown codes fall back to English).
pub fn set_language(code: &str) {
CURRENT.store(LANGUAGES.iter().position(|l| l.code == code).unwrap_or(0), Ordering::Relaxed);
CURRENT.store(LANGUAGES.iter().position(|l| l.code.eq_ignore_ascii_case(code) && available(l.code)).unwrap_or(0), Ordering::Relaxed);
}
/// The current UI language.
@@ -77,12 +84,8 @@ pub fn current() -> &'static Language {
/// `text` in the current language (unchanged when it has no translation).
pub fn tr(text: &str) -> &str {
let i = CURRENT.load(Ordering::Relaxed);
let (Some(lang), Some(map)) = (LANGUAGES.get(i), MAPS.get(i)) else { return text };
if lang.table.is_empty() {
return text;
}
map.get_or_init(|| lang.table.iter().copied().collect()).get(text).copied().unwrap_or(text)
let lang = shared_languages().get(CURRENT.load(Ordering::Relaxed)).copied().unwrap_or(craft_i18n::Lang::EN);
I18N.t(lang, text)
}
/// Marks a static string for translation where it is defined (gettext's `N_`); it is translated
@@ -93,25 +96,9 @@ pub const fn n_(text: &'static str) -> &'static str {
/// `template` translated, with each `{name}` replaced by its value in `args`.
pub fn trf(template: &str, args: &[(&str, &dyn Display)]) -> String {
let mut out = String::with_capacity(template.len() + 16);
let mut rest = tr(template);
while let Some(open) = rest.find('{') {
out.push_str(&rest[..open]);
let after = &rest[open + 1..];
match after.find('}').and_then(|close| args.iter().find(|(k, _)| *k == &after[..close]).map(|(_, v)| (close, v))) {
Some((close, v)) => {
use std::fmt::Write as _;
let _ = write!(out, "{v}");
rest = &after[close + 1..];
}
None => {
out.push('{');
rest = after;
}
}
}
out.push_str(rest);
out
let values: Vec<_> = args.iter().map(|(name, value)| (*name, value.to_string())).collect();
let args: Vec<_> = values.iter().map(|(name, value)| (*name, value.as_str())).collect();
craft_i18n::fmt(tr(template), &args)
}
/// The supported language for a locale such as "de-AT", "pt_BR.UTF-8" or "zh-Hant-TW" ("en" when
@@ -142,6 +129,7 @@ pub(crate) static TEST_LANGUAGE: std::sync::Mutex<()> = std::sync::Mutex::new(()
mod tests {
use super::TEST_LANGUAGE as LANGUAGE;
use super::*;
use std::collections::{BTreeSet, HashMap};
/// Every key the UI looks up: `tr("…")` / `trf("…"` literals in the sources, plus the
/// static catalogs whose labels are translated where they are shown.
@@ -170,35 +158,36 @@ mod tests {
fn catalog_keys() -> Vec<&'static str> {
use texturecraft_engine::core::{BlendMode, Channel, EffectKind, GeneratorKind, ProceduralKind};
use texturecraft_engine::filter_lib::{FILTERS, build, scalar_params};
let mut k: Vec<&'static str> = Vec::new();
k.extend(crate::menus::TOP);
for i in crate::menus::CATALOG.iter().filter(|i| i.label != "-") {
k.push(i.label);
k.extend(i.sub);
let mut keys: Vec<&'static str> = Vec::new();
keys.extend(crate::menus::TOP);
for item in crate::menus::CATALOG.iter().filter(|item| item.label != "-") {
keys.push(item.label);
keys.extend(item.sub);
}
k.extend(crate::docking::VIEWS.iter().map(|(_, p)| p.name()));
for b in crate::keymap::bindings() {
k.push(b.label);
k.push(b.group);
keys.extend(crate::docking::VIEWS.iter().map(|(_, panel)| panel.name()));
for binding in crate::keymap::bindings() {
keys.push(binding.label);
keys.push(binding.group);
}
k.extend(crate::shelf::TABS.iter().map(|t| t.2));
k.extend(Channel::ALL.iter().flat_map(|c| [c.label(), c.short()]));
k.extend(BlendMode::all().map(|m| m.label()));
k.extend(texturecraft_engine::formats::export::VirtualMap::ALL.iter().map(|m| m.label()));
k.extend(texturecraft_engine::Tool::ALL.iter().map(|t| t.label()));
k.extend(ProceduralKind::ALL.iter().map(|p| p.label()));
k.extend(GeneratorKind::ALL.iter().map(|g| g.label()));
k.extend(texturecraft_engine::paint::BrushAlpha::ALL.iter().map(|a| a.label()));
k.extend(texturecraft_engine::geom::primitives::BUILTINS.iter().map(|(_, l)| *l));
for f in FILTERS {
k.extend([f.label, f.group]);
if let Ok(EffectKind::Filter(x)) = build(f.id, &serde_json::Value::Null) {
k.push(x.label());
k.extend(scalar_params(&x).into_iter().map(|p| p.1));
keys.extend(crate::shelf::TABS.iter().map(|tab| tab.2));
keys.extend(Channel::ALL.iter().flat_map(|channel| [channel.label(), channel.short()]));
keys.extend(BlendMode::all().map(|mode| mode.label()));
keys.extend(texturecraft_engine::formats::export::VirtualMap::ALL.iter().map(|map| map.label()));
keys.extend(texturecraft_engine::Tool::ALL.iter().map(|tool| tool.label()));
keys.extend(ProceduralKind::ALL.iter().map(|kind| kind.label()));
keys.extend(GeneratorKind::ALL.iter().map(|kind| kind.label()));
keys.extend(texturecraft_engine::paint::BrushAlpha::ALL.iter().map(|alpha| alpha.label()));
keys.extend(texturecraft_engine::geom::primitives::BUILTINS.iter().map(|(_, label)| *label));
for filter in FILTERS {
keys.extend([filter.label, filter.group]);
if let Ok(EffectKind::Filter(effect)) = build(filter.id, &serde_json::Value::Null) {
keys.push(effect.label());
keys.extend(scalar_params(&effect).into_iter().map(|parameter| parameter.1));
}
}
// Effect, view and camera names the engine gives.
k.extend([
// Include effect, view and camera names supplied by the engine
keys.extend([
"Paint",
"Path",
"Fill",
@@ -211,8 +200,8 @@ mod tests {
texturecraft_engine::camera_cmds::DEFAULT_CAMERA,
crate::dock::SOLO_HINT,
]);
k.retain(|s| !s.is_empty() && !UNTRANSLATED.contains(s));
k
keys.retain(|key| !key.is_empty() && !UNTRANSLATED.contains(key));
keys
}
/// String literals passed straight to `tr(` or `trf(` in `src`.
@@ -252,43 +241,52 @@ mod tests",
}
}
fn placeholders(s: &str) -> Vec<&str> {
let mut v: Vec<&str> = s.split('{').skip(1).filter_map(|p| p.split_once('}').map(|(name, _)| name)).collect();
v.sort_unstable();
v
#[test]
fn embedded_catalogs_are_valid() {
assert!(I18N.check_all().is_empty(), "{:?}", I18N.check_all());
}
/// Translation work never blocks a feature: report missing/stale keys without failing.
#[test]
fn every_key_is_translated_in_every_language() {
fn translation_coverage_report() {
let keys = keys();
assert!(keys.len() > 300, "found only {} keys", keys.len());
let mut missing = Vec::new();
for l in LANGUAGES.iter().skip(1) {
let map: HashMap<&str, &str> = l.table.iter().copied().collect();
assert_eq!(map.len(), l.table.len(), "{}: duplicate keys", l.code);
for k in &keys {
if !map.contains_key(k.as_str()) {
missing.push(format!("{}: {k:?}", l.code));
let used: BTreeSet<_> = keys.iter().map(String::as_str).collect();
for &(locale, common, app) in CRAFT_I18N {
let Some(lang) = I18N.from_code(locale) else { continue };
let missing: Vec<_> = keys.iter().filter(|key| I18N.get(lang, "", key).is_none()).collect();
// Decode TSV escapes to compare with literal source strings
let mut rows = HashMap::new();
for text in [common, app] {
for line in text.lines().filter(|l| !l.starts_with('#')) {
let columns: Vec<_> = line.split('\t').collect();
if let ["", source, _] = columns.as_slice() {
let mut decoded = String::new();
let mut chars = source.chars();
while let Some(c) = chars.next() {
decoded.push(if c == '\\' {
match chars.next() {
Some('n') => '\n',
Some('t') => '\t',
Some(c) => c,
None => '\\',
}
} else {
c
});
}
rows.insert(decoded, ());
}
}
}
let stale: Vec<_> = rows.keys().filter(|key| !used.contains(key.as_str())).collect();
eprintln!("{locale}: missing {} {missing:?}; unused/shared {} {stale:?}", missing.len(), stale.len());
}
assert!(missing.is_empty(), "{} missing translations:\n{}", missing.len(), missing.join("\n"));
}
/// Catalog labels that read the same in every language (names and formats).
const UNTRANSLATED: &[&str] = &["RGB", "RGBA"];
#[test]
fn translations_keep_placeholders_and_are_not_empty() {
for l in &LANGUAGES {
for (en, t) in l.table {
assert!(!t.trim().is_empty(), "{}: empty translation of {en:?}", l.code);
assert_eq!(placeholders(en), placeholders(t), "{}: placeholders of {en:?} in {t:?}", l.code);
assert_eq!(en.ends_with("..."), t.ends_with("...") || t.ends_with('…'), "{}: ellipsis of {en:?} in {t:?}", l.code);
}
}
}
#[test]
fn lookup_formatting_and_fallbacks() {
let _lock = LANGUAGE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
@@ -304,27 +302,53 @@ mod tests",
assert_eq!(language_for_locale(locale), code, "{locale}");
}
set_language("de");
assert_eq!(current().code, "de");
assert_eq!(current().code, if available("de") { "de" } else { "en" });
assert_eq!(tr("not a key"), "not a key");
set_language("en");
}
#[test]
fn shared_lookup_overrides_and_missing_translations() {
static CATALOGS: &craft_i18n::Embedded = &[("es", "\tSave\tGuardar\n\tOpen\tAbrir\n", "\tSave\tGuardar proyecto\n")];
let (runtime, problems) = craft_i18n::I18n::new(CATALOGS, &["es"]);
assert!(problems.is_empty());
assert!(runtime.check_all().is_empty());
let lang = runtime.from_code("es").unwrap();
assert_eq!(runtime.t(lang, "Save"), "Guardar proyecto");
assert_eq!(runtime.t(lang, "Open"), "Abrir");
assert_eq!(runtime.t(lang, "New feature"), "New feature");
assert_eq!(runtime.t(craft_i18n::Lang::EN, "Save"), "Save");
}
#[test]
fn saved_language_codes_and_spanish_lookup() {
let _lock = LANGUAGE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
for language in &LANGUAGES {
set_language(language.code);
assert_eq!(current().code, if available(language.code) { language.code } else { "en" });
}
set_language("es");
assert_eq!(tr("Layers"), if available("es") { "Capas" } else { "Layers" });
set_language("en");
}
#[test]
fn lookup_is_fast() {
let _lock = LANGUAGE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let keys = keys();
set_language("ja");
let t = std::time::Instant::now();
let mut n = 0usize;
let started = std::time::Instant::now();
let mut total_chars = 0usize;
for _ in 0..100 {
for k in &keys {
n += tr(k).len();
for key in &keys {
total_chars += tr(key).len();
}
}
let per = t.elapsed().as_secs_f64() * 1e9 / (100 * keys.len()) as f64;
let ns_per_lookup = started.elapsed().as_secs_f64() * 1e9 / (100 * keys.len()) as f64;
set_language("en");
assert!(n > 0);
// A frame looks up a few hundred strings: well under a microsecond each.
assert!(per < 1000.0, "{per:.0} ns per lookup");
assert!(total_chars > 0);
// A frame looks up a few hundred strings, each expected below one microsecond
assert!(ns_per_lookup < 1000.0, "{ns_per_lookup:.0} ns per lookup");
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+1
View File
@@ -46,6 +46,7 @@ pub fn paint_bar(app: &mut TextureCraftApp, ui: &mut egui::Ui) {
let button_size = ui.spacing().interact_size.y;
let mode_width = 3.0 * button_size + 2.0 * BAR_SPACING;
let separator_width = BAR_SEPARATOR_WIDTH + 2.0 * BAR_SPACING;
// Leave room for both scroll arrows and at least one button of content
let minimum_strip_width = 3.0 * button_size + 2.0 * BAR_SPACING;
let scroll_views =
+87 -30
View File
@@ -256,6 +256,7 @@ fn shade(app: &TextureCraftApp) -> Shade {
};
let set_index = app.session.ts();
let height_strength = app.session.doc.project.texture_sets.get(set_index).map(|texture_set| texture_set.atlas_height_strength()).unwrap_or(1.0);
// Polygon fill shows the polygons it fills without changing the saved wireframe toggles
let polygon_fill_tool = tool_acts(app) && app.session.tool.tool == Tool::PolygonFill;
Shade {
@@ -391,14 +392,16 @@ const VIEW_HEADER_TOP_GAP: f32 = 8.0;
/// Width of each of the two pickers
const VIEW_PICKER_WIDTH: f32 = 130.0;
/// Where `view_header` draws its pickers in a view of `rect`; `None` when the view is too narrow.
fn view_header_rect(rect: Rect) -> Option<Rect> {
(rect.width() >= VIEW_HEADER_WIDTH).then(|| {
Rect::from_min_size(rect.min + vec2((rect.width() - VIEW_HEADER_WIDTH) * 0.5, VIEW_HEADER_TOP_GAP), vec2(VIEW_HEADER_WIDTH, VIEW_HEADER_HEIGHT))
})
}
/// The camera and view pickers at the top of a view, left out when the view is too narrow to hold them
fn view_header(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, id: &str) {
if rect.width() < VIEW_HEADER_WIDTH {
return;
}
let header_min = rect.min + vec2((rect.width() - VIEW_HEADER_WIDTH) * 0.5, VIEW_HEADER_TOP_GAP);
let header = Rect::from_min_size(header_min, vec2(VIEW_HEADER_WIDTH, VIEW_HEADER_HEIGHT));
let Some(header) = view_header_rect(rect) else { return };
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(header).layout(egui::Layout::left_to_right(egui::Align::Center)));
let current = texturecraft_engine::camera_cmds::current(&app.session);
@@ -515,6 +518,7 @@ fn view_3d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin
// Navigation; S + drag moves the projection stencil instead, below
let delta = ui.input(|input| input.pointer.delta());
let s_held = app.ui.mode == crate::state::Mode::Painting && ui.input(|input| input.key_down(egui::Key::S)) && !ui.ctx().egui_wants_keyboard_input();
// Only an equipped Projection tool that can act moves its stencil: it isn't drawn otherwise
let stencil_moves = tool_acts(app) && app.session.tool.tool == Tool::Projection;
let stencil_drag = s_held && stencil_moves && app.session.tool.stencil.is_some();
@@ -1525,13 +1529,14 @@ fn clone_source_key(app: &TextureCraftApp, ui: &Ui) -> bool {
/// Crosshair where the Clone tool reads from.
fn clone_marker(app: &TextureCraftApp, ui: &Ui, rect: Rect, pos: Vec3) {
let tk = Tokens::get(ui.ctx());
let Some(c) = project(app, rect, pos) else { return };
let p = ui.painter_at(rect);
for (w, color) in [(3.0, Color32::from_black_alpha(140)), (1.2, tk.viewport_accent)] {
p.circle_stroke(c, 7.0, Stroke::new(w, color));
p.line_segment([c - vec2(11.0, 0.0), c + vec2(11.0, 0.0)], Stroke::new(w, color));
p.line_segment([c - vec2(0.0, 11.0), c + vec2(0.0, 11.0)], Stroke::new(w, color));
let tokens = Tokens::get(ui.ctx());
let Some(center) = project(app, rect, pos) else { return };
let painter = ui.painter_at(rect);
for (stroke_width, color) in [(3.0, Color32::from_black_alpha(140)), (1.2, tokens.viewport_accent)] {
painter.circle_stroke(center, 7.0, Stroke::new(stroke_width, color));
painter.line_segment([center - vec2(11.0, 0.0), center + vec2(11.0, 0.0)], Stroke::new(stroke_width, color));
painter.line_segment([center - vec2(0.0, 11.0), center + vec2(0.0, 11.0)], Stroke::new(stroke_width, color));
}
}
@@ -1563,23 +1568,28 @@ fn brush_drag_button(resp: &egui::Response) -> Option<bool> {
/// While a Ctrl+drag changes the brush, its values beside where the drag began (drawn over the
/// view; the tool bar and Properties show them too).
fn brush_readout(app: &TextureCraftApp, ui: &Ui, resp: &egui::Response, adjusting: bool) {
let Some(right) = brush_drag_button(resp).filter(|_| adjusting) else { return };
let Some(at) = ui.input(|i| i.pointer.press_origin()) else { return };
// The pointer would cover the values; the brush cursor stays where the drag began.
ui.ctx().set_cursor_icon(egui::CursorIcon::None);
let b = &app.session.tool.brush;
let text = if right {
format!("{} {:.1} {} {:.0}", tr("Size"), b.size / 0.25 * 100.0, tr("Hardness"), b.hardness * 100.0)
fn brush_readout(app: &TextureCraftApp, ui: &Ui, response: &egui::Response, adjusting: bool) {
let Some(right_drag) = brush_drag_button(response).filter(|_| adjusting) else { return };
let Some(press_origin) = ui.input(|input| input.pointer.press_origin()) else { return };
// Keep the brush cursor at the drag origin so the pointer does not cover the values
if ui.input(|input| input.pointer.latest_pos()).is_some_and(|pointer| response.rect.contains(pointer)) {
ui.ctx().set_cursor_icon(egui::CursorIcon::None);
}
let brush = &app.session.tool.brush;
let text = if right_drag {
format!("{} {:.1} {} {:.0}", tr("Size"), brush.size / 0.25 * 100.0, tr("Hardness"), brush.hardness * 100.0)
} else {
format!("{} {:.0} {} {:.0}°", tr("Flow"), b.flow * 100.0, tr("Angle"), b.angle)
format!("{} {:.0} {} {:.0}°", tr("Flow"), brush.flow * 100.0, tr("Angle"), brush.angle)
};
let tk = Tokens::get(ui.ctx());
let p = ui.painter_at(resp.rect);
let g = p.layout_no_wrap(text, egui::FontId::proportional(12.0), tk.text_strong);
let r = Rect::from_min_size(at + vec2(18.0, -30.0), g.size()).expand2(vec2(6.0, 3.0));
p.rect_filled(r, 3.0, tk.panel);
p.galley(r.min + vec2(6.0, 3.0), g, tk.text_strong);
let tokens = Tokens::get(ui.ctx());
let painter = ui.painter_at(response.rect);
let galley = painter.layout_no_wrap(text, egui::FontId::proportional(12.0), tokens.text_strong);
let readout_rect = Rect::from_min_size(press_origin + vec2(18.0, -30.0), galley.size()).expand2(vec2(6.0, 3.0));
painter.rect_filled(readout_rect, 3.0, tokens.panel);
painter.galley(readout_rect.min + vec2(6.0, 3.0), galley, tokens.text_strong);
}
/// Size fraction, hardness and flow change per point of a Ctrl+drag
@@ -1655,9 +1665,9 @@ fn pen_pressure(ui: &Ui) -> f32 {
ui.data_mut(|d| d.insert_temp(id, f));
f
}
// A pen lifts with no pressure, and a tap paints on that frame: keep its contact pressure.
// A pen lifts with no pressure, so keep its contact pressure for a tap
Some((egui::TouchPhase::End, None)) => ui.data(|d| d.get_temp(id)).unwrap_or(1.0),
// A touch without pressure (a finger) must not inherit an earlier pen's.
// A finger must not inherit an earlier pen's pressure
Some((egui::TouchPhase::Start, None)) => {
ui.data_mut(|d| d.remove_temp::<f32>(id));
1.0
@@ -1834,6 +1844,28 @@ fn view_2d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin
}
}
// Brush cursor shows the stamp's size and hardness at the pointer
let show_brush_cursor = tool_acts(app) && !alt && !v_held;
let pointer_in_view = ui.input(|i| i.pointer.latest_pos()).is_some_and(|p| rect.contains(p));
if show_brush_cursor
&& !path_tool
&& brush_tool(app.session.tool.tool)
&& !response.dragged_by(PointerButton::Middle)
// A drag can retain stale hover while the pointer is outside the view
&& (adjusting || pointer_in_view)
// Ctrl+drag keeps the brush outline at the press origin
&& let Some(cursor_center) = if adjusting { ui.input(|input| input.pointer.press_origin()) } else { response.hover_pos().or_else(|| response.interact_pointer_pos()) }
// View header pickers keep the normal pointer
&& !view_header_rect(rect).is_some_and(|header| header.expand(2.0).contains(cursor_center))
{
// Match the stamp radius used by `continue_stroke_uv`, including pen pressure
let radius_points =
(to_screen(app.session.tool.brush.uv_stamp_radius(pen_pressure(ui)), 0.0).x - to_screen(0.0, 0.0).x).abs().max(BRUSH_CURSOR_MIN_RADIUS_POINTS);
if pointer_in_view {
ui.ctx().set_cursor_icon(egui::CursorIcon::None);
}
brush_cursor_2d(&painter, cursor_center, radius_points, app.session.tool.brush.hardness);
}
brush_readout(app, ui, &response, adjusting);
view_header(app, ui, rect, "v2d");
axis_gizmo(ui, rect, app, true);
@@ -1842,6 +1874,31 @@ fn view_2d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin
ui.painter().text(name_position, egui::Align2::RIGHT_BOTTOM, name, egui::FontId::proportional(SET_NAME_TEXT_SIZE), tokens.viewport_text);
}
/// Smallest visible 2D brush outline, in points
const BRUSH_CURSOR_MIN_RADIUS_POINTS: f32 = 1.5;
/// Hardness ring segments, balancing smoothness and per-frame shape work
const BRUSH_CURSOR_RING_SEGMENTS: usize = 48;
/// Smaller hardness rings overlap the center dot
const BRUSH_CURSOR_MIN_RING_HARDNESS: f32 = 0.05;
/// The brush outline and hardness ring around `center`, with `radius_points` in screen points
fn brush_cursor_2d(painter: &egui::Painter, center: egui::Pos2, radius_points: f32, hardness: f32) {
painter.circle_stroke(center, radius_points, Stroke::new(2.5, Color32::from_black_alpha(140)));
painter.circle_stroke(center, radius_points, Stroke::new(1.2, Color32::from_white_alpha(230)));
if hardness > BRUSH_CURSOR_MIN_RING_HARDNESS {
let inner_ring: Vec<egui::Pos2> = (0..=BRUSH_CURSOR_RING_SEGMENTS)
.map(|point_index| {
let angle = point_index as f32 / BRUSH_CURSOR_RING_SEGMENTS as f32 * std::f32::consts::TAU;
center + vec2(angle.cos(), angle.sin()) * radius_points * hardness
})
.collect();
painter.add(egui::Shape::dashed_line(&inner_ring, Stroke::new(1.0, Color32::from_white_alpha(120)), 3.0, 3.0));
}
painter.circle_filled(center, 1.5, Color32::WHITE);
}
/// Paint in the 2D view: a press starts a stroke, a drag continues it, Shift+click draws a line from the last stroke.
fn paint_2d_input(app: &mut TextureCraftApp, ui: &Ui, response: &egui::Response, shift: bool, to_uv: &dyn Fn(egui::Pos2) -> Vec2) {
let pressure = pen_pressure(ui);
+3 -1
View File
@@ -611,7 +611,7 @@ fn settings(app: &mut TextureCraftApp, ctx: &egui::Context) {
ui.label(tr("Language"));
let current = crate::i18n::current();
egui::ComboBox::from_id_salt("language").width(180.0).height(320.0).selected_text(current.name).show_ui(ui, |ui| {
for (l, ok) in LANGUAGES.iter().zip(drawable) {
for (l, ok) in LANGUAGES.iter().zip(drawable).filter(|(l, _)| crate::i18n::available(l.code)) {
let item = egui::Button::selectable(l.code == current.code, if ok { l.name } else { l.english });
if ui.add_enabled(ok, item).on_disabled_hover_text(tr("No font on this system can show this language")).clicked() {
p.language = l.code.to_owned();
@@ -1303,6 +1303,8 @@ mod tests {
fn harness() -> Harness<'static, TextureCraftApp> {
let session = texturecraft_engine::sample_session(256, 64).unwrap();
let mut h = Harness::builder().with_size(vec2(1600.0, 960.0)).build_eframe(|cc| TextureCraftApp::new(cc, session));
// These dialog tests query English labels, independently of the system locale.
h.state_mut().ui.prefs.language = "en".into();
h.run_steps(2);
h
}
+158
View File
@@ -0,0 +1,158 @@
//! The 2D view hides the system pointer and draws the brush outline under it while painting.
use egui::{CursorIcon, Event, Pos2, Shape, TouchDeviceId, TouchId, TouchPhase};
use egui_kittest::Harness;
use serde_json::json;
use texturecraft_engine::execute;
use texturecraft_ui_egui::TextureCraftApp;
use texturecraft_ui_egui::state::{Mode, ViewLayout};
fn harness() -> Harness<'static, TextureCraftApp> {
let session = texturecraft_engine::sample_session(256, 64).unwrap();
let mut harness = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|context| TextureCraftApp::new(context, session));
harness.state_mut().ui.layout = ViewLayout::Only2D;
harness.run_steps(4);
harness
}
fn cursor(harness: &Harness<'_, TextureCraftApp>) -> CursorIcon {
harness.output().platform_output.cursor_icon
}
#[test]
fn painting_in_the_2d_view_replaces_the_pointer_with_the_brush_outline() {
let mut harness = harness();
let center = egui::pos2(800.0, 480.0);
harness.hover_at(center);
harness.run_steps(3);
assert_eq!(cursor(&harness), CursorIcon::None, "the brush outline replaces the pointer");
harness.hover_at(egui::pos2(2.0, 2.0));
harness.run_steps(3);
assert_ne!(cursor(&harness), CursorIcon::None, "outside the view the pointer is back");
}
#[test]
fn outside_painting_the_2d_view_keeps_the_normal_pointer() {
let mut harness = harness();
harness.state_mut().ui.mode = Mode::Baking;
harness.run_steps(3);
let center = egui::pos2(800.0, 480.0);
harness.hover_at(center);
harness.run_steps(3);
assert_ne!(cursor(&harness), CursorIcon::None);
}
#[test]
fn unavailable_paint_tool_keeps_the_pointer_and_hides_the_2d_brush_outline() {
let mut harness = harness();
let fill = execute(&mut harness.state_mut().session, "layer.add.fill", &json!({})).unwrap()["layer"].as_u64().unwrap();
execute(&mut harness.state_mut().session, "layer.select", &json!({"layer": fill, "target": "content"})).unwrap();
let pointer = egui::pos2(800.0, 480.0);
harness.hover_at(pointer);
harness.run_steps(3);
assert_ne!(cursor(&harness), CursorIcon::None, "a fill layer cannot take the paint tool");
assert!(rings_at(&harness, pointer).is_empty(), "an unavailable brush has no outline");
}
/// Radii of the white brush-outline rings (stroke 1.2) around `center` in the last frame.
fn rings_at(harness: &Harness<'_, TextureCraftApp>, center: Pos2) -> Vec<f32> {
fn walk(shape: &Shape, center: Pos2, out: &mut Vec<f32>) {
match shape {
Shape::Vec(shapes) => shapes.iter().for_each(|s| walk(s, center, out)),
Shape::Circle(circle) if (circle.stroke.width - 1.2).abs() < 1e-3 && circle.center.distance(center) < 0.5 => out.push(circle.radius),
_ => {}
}
}
let mut out = Vec::new();
harness.output().shapes.iter().for_each(|clipped| walk(&clipped.shape, center, &mut out));
out
}
#[test]
fn the_view_header_keeps_the_normal_pointer_and_gets_no_brush_outline() {
let mut harness = harness();
// The header starts 8 points below the view edge and is 22 points high
let view_top = (0..200).map(|y| y as f32).find(|&y| {
harness.hover_at(egui::pos2(300.0, y));
harness.run_steps(2);
cursor(&harness) == CursorIcon::None
});
let top = view_top.expect("the view has a top edge");
let header = egui::pos2(800.0, top + 8.0 + 11.0);
harness.hover_at(header);
harness.run_steps(3);
assert_ne!(cursor(&harness), CursorIcon::None, "over the pickers the pointer stays");
assert!(rings_at(&harness, header).is_empty(), "no brush outline over the pickers");
let below = egui::pos2(800.0, top + 120.0);
harness.hover_at(below);
harness.run_steps(3);
assert_eq!(cursor(&harness), CursorIcon::None, "below the header the brush outline is back");
assert!(!rings_at(&harness, below).is_empty());
}
#[test]
fn a_light_pen_shows_the_smaller_footprint_it_paints() {
let mut harness = harness();
harness.state_mut().session.tool.brush.size = 0.05;
harness.state_mut().session.tool.brush.min_size = 0.5;
let center = egui::pos2(800.0, 600.0);
harness.hover_at(center);
harness.run_steps(3);
let full = rings_at(&harness, center);
assert_eq!(full.len(), 1, "one outline at full pressure");
// Half pressure gives 0.75 of the full radius with this minimum size
let events = &mut harness.input_mut().events;
events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Move, pos: center, force: Some(0.5) });
events.push(Event::PointerMoved(center));
harness.step();
let light = rings_at(&harness, center);
assert_eq!(light.len(), 1);
assert!((light[0] / full[0] - 0.75).abs() < 1e-3, "half pressure shows 0.75 of the radius: {} vs {}", light[0], full[0]);
}
#[test]
fn dragging_out_of_the_2d_view_brings_the_system_pointer_back() {
let mut harness = harness();
let center = egui::pos2(800.0, 480.0);
harness.hover_at(center);
harness.run_steps(3);
assert_eq!(cursor(&harness), CursorIcon::None, "inside the view the brush outline replaces the pointer");
// Drag from the view to its upper-left outside corner
harness.event(Event::PointerButton { pos: center, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
harness.run_steps(2);
assert_eq!(cursor(&harness), CursorIcon::None, "still inside while dragging");
let outside = egui::pos2(2.0, 2.0);
harness.hover_at(outside);
harness.run_steps(3);
assert_ne!(cursor(&harness), CursorIcon::None, "outside the view the pointer is visible even during a drag");
assert!(rings_at(&harness, outside).is_empty(), "no brush outline outside the view");
harness.event(Event::PointerButton { pos: outside, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
harness.run_steps(2);
}
#[test]
fn a_ctrl_right_drag_out_of_the_2d_view_keeps_its_outline_at_the_anchor() {
let mut harness = harness();
let center = egui::pos2(800.0, 480.0);
let ctrl = egui::Modifiers::COMMAND;
harness.hover_at(center);
harness.run_steps(2);
harness.event(Event::ModifiersChanged(ctrl));
harness.event(Event::PointerButton { pos: center, button: egui::PointerButton::Secondary, pressed: true, modifiers: ctrl });
harness.run_steps(2);
// The outline stays anchored while the pointer is hidden inside the view
harness.hover_at(center + egui::vec2(10.0, 0.0));
harness.run_steps(3);
assert_eq!(rings_at(&harness, center).len(), 1, "anchored outline while the pointer is in the view");
assert_eq!(cursor(&harness), CursorIcon::None);
// Outside the view, the outline stays anchored and the system pointer returns
harness.hover_at(egui::pos2(2.0, 2.0));
harness.run_steps(3);
assert_eq!(rings_at(&harness, center).len(), 1, "the outline stays at the press origin outside the view");
assert_ne!(cursor(&harness), CursorIcon::None, "outside the view the pointer is visible");
harness.event(Event::PointerButton { pos: egui::pos2(2.0, 2.0), button: egui::PointerButton::Secondary, pressed: false, modifiers: ctrl });
harness.event(Event::ModifiersChanged(egui::Modifiers::NONE));
harness.run_steps(2);
}
+27
View File
@@ -0,0 +1,27 @@
//! Release and source Flatpaks must allow release checks and host update helpers.
fn finish_args(manifest: &str) -> Vec<&str> {
manifest
.split("finish-args:")
.nth(1)
.unwrap_or_default()
.lines()
.take_while(|line| line.is_empty() || line.starts_with(' ') || line.starts_with('#'))
.filter_map(|line| line.trim().strip_prefix("- "))
.map(|arg| arg.trim_matches(['\'', '"']))
.collect()
}
#[test]
fn flatpak_update_permissions() {
let manifests = [
include_str!("../../../packaging/linux/flatpak/ai.storyteller.TextureCraft.bundle.yml"),
include_str!("../../../packaging/linux/flatpak/ai.storyteller.TextureCraft.yml"),
];
let args = manifests.map(finish_args);
assert_eq!(args[0], args[1], "source and release permissions must agree");
for permissions in args {
for required in ["--share=network", "--talk-name=org.freedesktop.Flatpak", "--filesystem=xdg-data/ArtCraft/versions:ro"] {
assert!(permissions.contains(&required), "missing update permission: {required}");
}
}
}
+100
View File
@@ -0,0 +1,100 @@
//! Optional catalog embedding must work without network or application dependencies.
#[path = "../i18n_build.rs"]
#[allow(dead_code)] // The production build entry point is not invoked by these pure-input tests.
mod i18n_build;
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
struct Fixture(PathBuf);
impl Fixture {
fn new() -> Self {
static NEXT: AtomicUsize = AtomicUsize::new(0);
let path = std::env::temp_dir().join(format!("texturecraft-i18n-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed)));
std::fs::create_dir_all(path.join("locales/es")).unwrap();
Self(path)
}
}
impl Drop for Fixture {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn no_input_is_english_but_required_input_errors() {
assert!(!i18n_build::generate(None, false).unwrap().contains("include_str!"));
assert!(i18n_build::generate(None, true).is_err());
}
#[test]
fn invalid_and_relative_checkouts_are_rejected() {
let fixture = Fixture::new();
assert!(i18n_build::generate(Some(&fixture.0.join("missing")), false).is_err());
assert!(i18n_build::generate(Some(std::path::Path::new("relative")), false).is_err());
}
#[test]
fn shared_only_locale_is_not_offered_and_required_needs_app_catalog() {
let fixture = Fixture::new();
std::fs::write(fixture.0.join("locales/es/common.tsv"), "\tSave\tGuardar\n").unwrap();
assert!(!i18n_build::generate(Some(&fixture.0), false).unwrap().contains("include_str!"));
assert!(i18n_build::generate(Some(&fixture.0), true).is_err());
}
#[test]
fn app_catalog_is_embedded_with_optional_shared_catalog() {
let fixture = Fixture::new();
let app = fixture.0.join("locales/es/texturecraft.tsv");
std::fs::write(&app, "\tLayers\tCapas\n").unwrap();
let source = i18n_build::generate(Some(&fixture.0), true).unwrap();
assert!(source.contains("texturecraft.tsv"));
assert!(source.contains("(\"es\", \"\","));
let common = fixture.0.join("locales/es/common.tsv");
std::fs::write(&common, "\tSave\tGuardar\n").unwrap();
assert!(i18n_build::generate(Some(&fixture.0), true).unwrap().contains("common.tsv"));
}
#[test]
fn unreadable_catalog_fails_before_generating_rust_source() {
let fixture = Fixture::new();
std::fs::write(fixture.0.join("locales/es/texturecraft.tsv"), [0xff, 0xfe]).unwrap();
assert!(i18n_build::generate(Some(&fixture.0), true).is_err());
}
#[test]
fn generated_paths_compile_and_embed_the_catalog_contents() {
let fixture = Fixture::new();
std::fs::write(fixture.0.join("locales/es/texturecraft.tsv"), "\tLayers\tCapas\n").unwrap();
let generated = i18n_build::generate(Some(&fixture.0), true).unwrap();
let program = format!(
"extern crate self as craft_i18n;\npub type Embedded = [(&'static str, &'static str, &'static str)];\n{generated}\nfn main() {{ assert_eq!(CRAFT_I18N[0].2, \"\\tLayers\\tCapas\\n\"); }}"
);
let source = fixture.0.join("check.rs");
let binary = fixture.0.join(format!("check{}", std::env::consts::EXE_SUFFIX));
std::fs::write(&source, program).unwrap();
assert!(std::process::Command::new("rustc").arg(&source).arg("-o").arg(&binary).status().unwrap().success());
assert!(std::process::Command::new(binary).status().unwrap().success());
}
#[test]
fn malformed_utf8_catalogs_and_invalid_rows_fail_during_generation() {
for file in ["texturecraft.tsv", "common.tsv"] {
for invalid in [
"not a TSV row\n",
"\tLayers\t\n",
"\tSave {name}\tGuardar\n",
"\tOpen…\tAbrir\n",
"\tSave\tGuardar\n\tSave\tSalvar\n",
"@plural\t{n} layer|{n} layers\t{n} capa\n",
"@unknown\tSave\tGuardar\n",
] {
let fixture = Fixture::new();
std::fs::write(fixture.0.join("locales/es/texturecraft.tsv"), "\tLayers\tCapas\n").unwrap();
std::fs::write(fixture.0.join("locales/es").join(file), invalid).unwrap();
for required in [false, true] {
assert!(i18n_build::generate(Some(&fixture.0), required).is_err(), "accepted {file}: {invalid:?}");
}
}
}
}
+18
View File
@@ -497,3 +497,21 @@ fn rail_window_snapshots() {
texturecraft_ui_egui::menus::invoke(harness.state_mut(), &context, "ui.view.history");
save(&mut harness, &output_dir, "rail-history-short");
}
/// Visual check of the migrated catalogs and the live language selector.
#[test]
#[ignore]
fn i18n_snapshots() {
let Some(output_dir) = std::env::var_os("TC_UI_SNAPSHOTS").map(PathBuf::from) else { return };
std::fs::create_dir_all(&output_dir).unwrap();
for code in ["en", "es", "ar"] {
assert!(texturecraft_ui_egui::i18n::available(code), "build with migrated catalogs for {code}");
let session = texturecraft_engine::sample_session(128, 32).unwrap();
let mut harness =
Harness::builder().with_size(egui::vec2(1600.0, 960.0)).with_pixels_per_point(1.0).wgpu().build_eframe(|cc| TextureCraftApp::new(cc, session));
harness.state_mut().ui.prefs.language = code.into();
harness.state_mut().ui.windows.settings = true;
save(&mut harness, &output_dir, &format!("i18n-{code}"));
}
texturecraft_ui_egui::i18n::set_language("en");
}
+2 -34
View File
@@ -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
+29
View File
@@ -0,0 +1,29 @@
# Interface translations
TextureCraft uses the `craft-i18n` runtime from craft-libs and optional catalogs from
[craft-i18n](https://github.com/storytold/craft-i18n).
Set `CRAFT_I18N_DIR` to the absolute path of a craft-i18n checkout before building.
The build embeds `locales/<locale>/common.tsv` and `texturecraft.tsv`. Without catalog input,
the application uses English. `CRAFT_I18N_REQUIRED=1` makes missing/invalid input a build error.
The same catalogs are embedded for native and WebAssembly builds; no runtime files are needed.
Feature changes add English strings through `tr`, `n_` or `trf`. Translations belong in
craft-i18n PRs. A missing translation displays English and does not block a feature or release.
Run `cargo test -p texturecraft-ui-egui translation_coverage_report -- --nocapture` with
catalog input to inspect untranslated and unused/shared keys. Shared catalogs contain other
apps' strings, so unused shared keys are informational, not candidates for automatic deletion.
The initial migration is tracked in storytold/texturecraft#419. Catalog changes must merge
before the application cutover, and translations landing meanwhile need a final sync.
Saved `pt-BR` and `zh-Hans` preferences retain their original spelling and meaning.
## CI and release catalog revision
`craft-i18n.rev` pins the catalog commit. The shared craft-common-actions gate and release
workflows read this file, check out storytold/craft-i18n and set CRAFT_I18N_DIR together with
CRAFT_I18N_REQUIRED=1. No application workflow changes are needed.
The catalog commit must be available in storytold/craft-i18n before merging the app cutover.
After a squash or rebase merge of the companion catalog PR, update the pin to the resulting
upstream commit and rerun validation before merging TextureCraft.
+8 -5
View File
@@ -2,20 +2,23 @@
TextureCraft's interface text goes through one lookup, `tr("English text")` in
`crates/ui-egui/src/i18n.rs`: the English string is the key, so English needs no table and a
missing translation falls back to English. Each language is a static `(English, translation)`
table in `crates/ui-egui/src/i18n/<code>.rs`, indexed into a hash map the first time the
language is used. Text with values uses `trf("Saved {path}", &[("path", &p)])`; static labels
that are translated where they are shown are marked with `n_("…")`.
missing translation falls back to English. Translations are embedded from the optional shared
craft-i18n catalogs; see [integration and contribution instructions](internationalization.md).
Text with values uses `trf("Saved {path}", &[("path", &p)])`; static labels translated where
they are shown are marked with `n_("…")`.
Pick the language in **Edit ▸ Settings ▸ Language** (it switches live). The first run follows
the operating system's language (the browser's in the web build).
## Status
The table records coverage before the initial catalog migration. Actual coverage depends on
the craft-i18n catalogs embedded in a build. Without catalogs, only English is offered.
UI strings: every `tr`/`trf`/`n_` literal in `crates/ui-egui/src` plus the menu catalog, panel
names, shortcut editor rows and the engine's channel, blend mode, mesh map, tool, procedural,
generator, filter and brush alpha names (**KEYS** strings, counted by the
`every_key_is_translated_in_every_language` test, which fails when one is missing).
`translation_coverage_report` test, which reports missing and unused keys without failing).
| Language | Code | UI strings | Dialogs, tooltips, help | Script support | Native review | Status | To `full` |
|---|---|---|---|---|---|---|---|
+25
View File
@@ -12,6 +12,31 @@ Run a feature module with a test-name filter, for example:
cargo test -p texturecraft-engine --test it bake::
```
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 gates also remain unchanged.
The standalone engine `session` target and UI `ui_snapshots` target remain
available for the commands in `AGENTS.md`. Existing ignore attributes are
unchanged. Tests in the shared executable can run concurrently across modules;
@@ -17,6 +17,12 @@ runtime-version: '26.08'
sdk: org.freedesktop.Sdk
command: texturecraft
finish-args:
# Honor the launcher's pinned and nightly version choices before offering updates.
- --filesystem=xdg-data/ArtCraft/versions:ro
# Release checks and downloads use HTTPS.
- --share=network
# The shared updater inspects and updates this installation via flatpak-spawn --host.
- --talk-name=org.freedesktop.Flatpak
# Display: Wayland first; X11 only when no Wayland compositor is running.
- --socket=wayland
- --socket=fallback-x11
@@ -30,9 +36,6 @@ finish-args:
# line, Open Recent, "Save" next to the original) work in the usual places.
- --filesystem=xdg-pictures
- --filesystem=xdg-documents
# Deliberately not granted: network (the optional --control server listens on 127.0.0.1 in
# the sandbox; `flatpak override --user --share=network ai.storyteller.texturecraft` exposes it to
# host agents) and home/host filesystem access.
modules:
- name: texturecraft
buildsystem: simple
@@ -18,6 +18,12 @@ sdk-extensions:
- org.freedesktop.Sdk.Extension.rust-stable
command: texturecraft
finish-args:
# Honor the launcher's pinned and nightly version choices before offering updates.
- --filesystem=xdg-data/ArtCraft/versions:ro
# Release checks and downloads use HTTPS.
- --share=network
# The shared updater inspects and updates this installation via flatpak-spawn --host.
- --talk-name=org.freedesktop.Flatpak
# Display: Wayland first; X11 only when no Wayland compositor is running.
- --socket=wayland
- --socket=fallback-x11
@@ -31,9 +37,6 @@ finish-args:
# line, Open Recent, "Save" next to the original) work in the usual places.
- --filesystem=xdg-pictures
- --filesystem=xdg-documents
# Deliberately not granted: network (the optional --control server listens on 127.0.0.1 in
# the sandbox; `flatpak override --user --share=network ai.storyteller.texturecraft` exposes it to
# host agents) and home/host filesystem access.
build-options:
append-path: /usr/lib/sdk/rust-stable/bin
modules:
+3 -2
View File
@@ -15,7 +15,7 @@ usage: cargo xtask <command>
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 <out.ico> <in.png>...
@@ -154,11 +154,12 @@ fn cmd_ci() -> Result<(), String> {
}
};
type Step = (&'static str, Box<dyn Fn() -> Result<(), String>>);
// Reject invalid dependency layering before spending time compiling and running the workspace.
let steps: Vec<Step> = 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() {