mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-21 15:00:42 +01:00
Merge pull request #69 from SteamSmith/feat/bake-auto-rebake
feat(bake): Auto-rebake keeps one mesh map up to date
This commit is contained in:
18 files changed
+391
-13
No files matched your search
@@ -0,0 +1,176 @@
|
||||
//! Auto-rebake: one mesh map that bakes itself again when what it was baked from changes.
|
||||
//!
|
||||
//! The map is chosen with `bake.autoRebake`. From then on, [`Session::auto_rebake_due`] names the
|
||||
//! texture sets whose bake inputs (the set's bake settings except the list of maps, the low poly,
|
||||
//! the high polys and the cage) differ from those of their last bake of that map.
|
||||
//! The apps poll it each frame and bake the due sets in the background; `bake.autoRebake.run`
|
||||
//! does the same thing once, blocking, for agents and scripts.
|
||||
//!
|
||||
//! A set only takes part once it has the map baked at its current output size: Auto-rebake
|
||||
//! keeps a bake up to date, it doesn't start one, and it never leaves a set's maps at two sizes.
|
||||
//! The choice isn't part of the project.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_bake::Progress;
|
||||
use texturecraft_core::BakedMap;
|
||||
|
||||
use crate::bake_cmds::BakeTask;
|
||||
use crate::commands::{CommandSpec, always, bad, boolp};
|
||||
use crate::{Result, Session, cmd};
|
||||
|
||||
/// Which map rebakes itself, and what each texture set was last baked from.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct AutoRebake {
|
||||
pub map: Option<BakedMap>,
|
||||
/// Fingerprint of each set's bake inputs when it last baked [`AutoRebake::map`].
|
||||
seen: BTreeMap<usize, u64>,
|
||||
/// Fingerprint taken when a set's bake started, kept when that bake finishes.
|
||||
pending: BTreeMap<usize, u64>,
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"bake.autoRebake",
|
||||
"Auto-rebake",
|
||||
[],
|
||||
None,
|
||||
"{map?: map id (e.g. \"normal_base\", \"height\") to keep up to date, or null / off: true to turn it off; no params: just report} → {map, due: [texture sets that need a rebake]}",
|
||||
always,
|
||||
auto_rebake
|
||||
),
|
||||
cmd!(
|
||||
"bake.autoRebake.run",
|
||||
"Run Auto-rebake",
|
||||
[],
|
||||
None,
|
||||
"{cpu?: true (skip the GPU)} → {rebaked: [texture sets]}: bakes the Auto-rebake map of every set whose bake inputs changed since it was last baked, now",
|
||||
always,
|
||||
auto_rebake_run
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// A fingerprint of everything a bake of texture set `ts` is made from, except which maps it
|
||||
/// makes.
|
||||
pub fn bake_inputs_key(&self, ts: usize) -> Option<u64> {
|
||||
let set = self.doc.project.texture_sets.get(ts)?;
|
||||
let mut settings = set.bake.clone();
|
||||
settings.maps.clear();
|
||||
settings.enabled = true;
|
||||
let mut h = DefaultHasher::new();
|
||||
serde_json::to_string(&settings).ok()?.hash(&mut h);
|
||||
(Arc::as_ptr(&self.mesh) as usize).hash(&mut h);
|
||||
for m in &self.bake_meshes.high {
|
||||
(Arc::as_ptr(&m.mesh) as usize).hash(&mut h);
|
||||
}
|
||||
self.bake_meshes.cage.as_ref().map(|c| Arc::as_ptr(&c.mesh) as usize).hash(&mut h);
|
||||
Some(h.finish())
|
||||
}
|
||||
|
||||
/// Texture sets whose Auto-rebake map is out of date. A set seen for the first time (a project
|
||||
/// just opened, a bake from before Auto-rebake was on) is taken as up to date.
|
||||
pub fn auto_rebake_due(&mut self) -> Vec<usize> {
|
||||
let Some(map) = self.auto_rebake.map else { return Vec::new() };
|
||||
let mut due = Vec::new();
|
||||
for ts in 0..self.doc.project.texture_sets.len() {
|
||||
let Some(set) = self.doc.project.texture_sets.get(ts).filter(|t| t.bake.enabled) else { continue };
|
||||
let columns = set.tile_grid().map_or(1, |g| g.cols);
|
||||
let baked = self.doc.baked.get(&ts).is_some_and(|b| b.maps.contains_key(&map) && b.resolution == set.bake.resolution.saturating_mul(columns));
|
||||
let Some(key) = self.bake_inputs_key(ts).filter(|_| baked) else { continue };
|
||||
match self.auto_rebake.seen.get(&ts) {
|
||||
None => {
|
||||
self.auto_rebake.seen.insert(ts, key);
|
||||
}
|
||||
Some(seen) if *seen != key => due.push(ts),
|
||||
Some(_) => {}
|
||||
}
|
||||
}
|
||||
due
|
||||
}
|
||||
|
||||
/// Call right before building the bake task of `ts`: remembers what it is baked from, so a
|
||||
/// finished bake of the Auto-rebake map marks the set up to date (and a change made while it
|
||||
/// ran still triggers another).
|
||||
pub fn begin_bake(&mut self, ts: usize) {
|
||||
if let Some(key) = self.bake_inputs_key(ts) {
|
||||
self.auto_rebake.pending.insert(ts, key);
|
||||
}
|
||||
}
|
||||
|
||||
/// The bake task of `ts` restricted to one map (Auto-rebake's: the other maps stay as they are).
|
||||
pub fn bake_task_for(&self, ts: usize, only: Option<BakedMap>) -> Result<BakeTask> {
|
||||
let mut task = self.bake_task(ts)?;
|
||||
if let Some(m) = only {
|
||||
task.settings.maps = std::iter::once(m).collect();
|
||||
}
|
||||
Ok(task)
|
||||
}
|
||||
|
||||
/// Records a finished bake: when it made the Auto-rebake map, the set is up to date.
|
||||
pub(crate) fn note_baked(&mut self, ts: usize, mut made: impl Iterator<Item = BakedMap>) {
|
||||
let key = self.auto_rebake.pending.remove(&ts);
|
||||
if let (Some(key), Some(map)) = (key, self.auto_rebake.map)
|
||||
&& made.any(|m| m == map)
|
||||
{
|
||||
self.auto_rebake.seen.insert(ts, key);
|
||||
}
|
||||
}
|
||||
|
||||
/// Records bakes of `sets` that were cancelled, failed or never started: Auto-rebake leaves
|
||||
/// those sets alone until their bake inputs change again, instead of retrying every frame.
|
||||
pub fn bake_abandoned(&mut self, sets: impl IntoIterator<Item = usize>) {
|
||||
for ts in sets {
|
||||
if let Some(key) = self.auto_rebake.pending.remove(&ts).or_else(|| self.bake_inputs_key(ts)) {
|
||||
self.auto_rebake.seen.insert(ts, key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bakes every due set's Auto-rebake map now (blocking); returns the sets.
|
||||
pub fn auto_rebake_now(&mut self, cpu: bool) -> Result<Vec<usize>> {
|
||||
let due = self.auto_rebake_due();
|
||||
let map = self.auto_rebake.map;
|
||||
for &ts in &due {
|
||||
self.begin_bake(ts);
|
||||
let mut task = self.bake_task_for(ts, map)?;
|
||||
if cpu {
|
||||
task.tracer = None;
|
||||
}
|
||||
let out = task.run(&Progress::new())?;
|
||||
self.apply_bake(ts, out)?;
|
||||
}
|
||||
Ok(due)
|
||||
}
|
||||
}
|
||||
|
||||
fn state(s: &mut Session) -> Value {
|
||||
json!({ "map": s.auto_rebake.map.map(BakedMap::id), "due": s.auto_rebake_due() })
|
||||
}
|
||||
|
||||
fn auto_rebake(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if boolp(p, "off") == Some(true) || p.get("map").is_some_and(Value::is_null) {
|
||||
s.auto_rebake = AutoRebake::default();
|
||||
} else if let Some(id) = p.get("map") {
|
||||
let map = id.as_str().and_then(BakedMap::parse).ok_or_else(|| bad("bake.autoRebake", format!("unknown mesh map {id}")))?;
|
||||
if s.auto_rebake.map != Some(map) {
|
||||
s.auto_rebake = AutoRebake { map: Some(map), ..AutoRebake::default() };
|
||||
}
|
||||
}
|
||||
Ok(state(s))
|
||||
}
|
||||
|
||||
fn auto_rebake_run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if s.auto_rebake.map.is_none() {
|
||||
return Err(bad("bake.autoRebake.run", "no Auto-rebake map is set; run bake.autoRebake {map} first"));
|
||||
}
|
||||
let rebaked = s.auto_rebake_now(boolp(p, "cpu") == Some(true))?;
|
||||
s.update();
|
||||
Ok(json!({ "rebaked": rebaked }))
|
||||
}
|
||||
@@ -177,6 +177,7 @@ impl Session {
|
||||
/// Store a finished bake (one undo step).
|
||||
pub fn apply_bake(&mut self, ts: usize, out: BakeOutput) -> Result<()> {
|
||||
self.log.push(format!("Baked texture set {ts} ({})", out.tracer));
|
||||
let made: Vec<BakedMap> = out.maps.keys().copied().collect();
|
||||
self.edit("Bake mesh maps", |s| {
|
||||
let maps: BTreeSet<BakedMap> = out.maps.keys().copied().collect();
|
||||
let entry = s.doc.baked.entry(ts).or_default();
|
||||
@@ -186,11 +187,14 @@ impl Session {
|
||||
}
|
||||
s.doc.project.texture_set_mut(ts)?.baked.extend(maps);
|
||||
Ok(())
|
||||
})
|
||||
})?;
|
||||
self.note_baked(ts, made.into_iter());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Bake texture set `ts` now (blocking), on the CPU when `cpu`; returns where the rays ran.
|
||||
pub fn bake_now(&mut self, ts: usize, cpu: bool) -> Result<String> {
|
||||
self.begin_bake(ts);
|
||||
let mut task = self.bake_task(ts)?;
|
||||
if cpu {
|
||||
task.tracer = None;
|
||||
|
||||
@@ -68,6 +68,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
|
||||
v.extend(crate::uv_tile_cmds::specs());
|
||||
v.extend(crate::template_cmds::specs());
|
||||
v.extend(crate::project_cmds::specs());
|
||||
v.extend(crate::auto_rebake::specs());
|
||||
v.extend(crate::bake_cmds::specs());
|
||||
v.extend(crate::materialx_cmds::specs());
|
||||
v.extend(crate::file_cmds::specs());
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
pub mod auto_rebake;
|
||||
pub mod bake_cmds;
|
||||
pub mod brush_preset_cmds;
|
||||
pub mod camera_cmds;
|
||||
|
||||
@@ -320,6 +320,8 @@ pub struct Session {
|
||||
pub mesh_source: Option<(String, Vec<u8>)>,
|
||||
/// High-definition meshes and cage for baking (saved inside the project).
|
||||
pub bake_meshes: crate::BakeMeshes,
|
||||
/// The map that bakes itself again when its inputs change (not saved).
|
||||
pub auto_rebake: crate::auto_rebake::AutoRebake,
|
||||
pub caches: Vec<SetCache>,
|
||||
pub(crate) undo: Vec<(String, Document)>,
|
||||
pub(crate) redo: Vec<(String, Document)>,
|
||||
@@ -375,6 +377,7 @@ impl Session {
|
||||
bvh: Arc::new(bvh),
|
||||
mesh_source,
|
||||
bake_meshes: Default::default(),
|
||||
auto_rebake: Default::default(),
|
||||
caches,
|
||||
undo: Vec::new(),
|
||||
redo: Vec::new(),
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
//! Auto-rebake: one mesh map bakes itself again when its bake inputs change.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_engine::core::BakedMap;
|
||||
use texturecraft_engine::geom::primitives;
|
||||
use texturecraft_engine::{Session, execute};
|
||||
|
||||
/// A plane with a high poly just above it, baked once with height and opacity.
|
||||
fn baked_session(tag: &str) -> (Session, std::path::PathBuf) {
|
||||
let mesh = primitives::builtin("plane").unwrap();
|
||||
let mut obj = String::from("o Part_high\n");
|
||||
for p in &mesh.positions {
|
||||
obj += &format!("v {} {} {}\n", p.x, p.y + 0.002, p.z);
|
||||
}
|
||||
for t in mesh.indices.chunks_exact(3) {
|
||||
obj += &format!("f {} {} {}\n", t[0] + 1, t[1] + 1, t[2] + 1);
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("tc_auto_rebake_{tag}_{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("hp.obj");
|
||||
std::fs::write(&path, obj).unwrap();
|
||||
let mut s = Session::new_project("t", mesh, "builtin:plane", None, 256).unwrap();
|
||||
execute(&mut s, "bake.highPoly.add", &json!({ "path": path.display().to_string() })).unwrap();
|
||||
execute(&mut s, "bake.settings", &json!({ "resolution": 128, "dilation": 0, "maps": ["height", "opacity"] })).unwrap();
|
||||
execute(&mut s, "bake.run", &json!({ "cpu": true })).unwrap();
|
||||
(s, dir)
|
||||
}
|
||||
|
||||
fn due(s: &mut Session) -> Vec<u64> {
|
||||
execute(s, "bake.autoRebake", &Value::Null).unwrap()["due"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect()
|
||||
}
|
||||
|
||||
fn image(s: &Session, map: BakedMap) -> Arc<Vec<f32>> {
|
||||
s.doc.baked[&0].maps[&map].clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_changed_bake_input_rebakes_only_the_chosen_map() {
|
||||
let (mut s, dir) = baked_session("rebake");
|
||||
// Off by default; nothing is due and the run command says what to do first.
|
||||
assert_eq!(execute(&mut s, "bake.autoRebake", &Value::Null).unwrap()["map"], Value::Null);
|
||||
assert!(execute(&mut s, "bake.autoRebake.run", &Value::Null).is_err());
|
||||
let r = execute(&mut s, "bake.autoRebake", &json!({ "map": "height" })).unwrap();
|
||||
assert_eq!(r["map"], "height");
|
||||
assert!(due(&mut s).is_empty(), "turning it on takes the current bake as up to date");
|
||||
|
||||
// Which maps are ticked is not a bake input; a setting is.
|
||||
execute(&mut s, "bake.settings", &json!({ "maps": ["height", "opacity", "ambient_occlusion"] })).unwrap();
|
||||
assert!(due(&mut s).is_empty());
|
||||
execute(&mut s, "bake.settings", &json!({ "maxFrontal": 0.2, "maps": ["height", "opacity"] })).unwrap();
|
||||
assert_eq!(due(&mut s), [0]);
|
||||
|
||||
let (height, opacity) = (image(&s, BakedMap::Height), image(&s, BakedMap::Opacity));
|
||||
let r = execute(&mut s, "bake.autoRebake.run", &json!({ "cpu": true })).unwrap();
|
||||
assert_eq!(r["rebaked"], json!([0]));
|
||||
assert!(!Arc::ptr_eq(&height, &image(&s, BakedMap::Height)), "the Auto-rebake map was baked again");
|
||||
assert!(Arc::ptr_eq(&opacity, &image(&s, BakedMap::Opacity)), "the other maps were left alone");
|
||||
assert!(due(&mut s).is_empty(), "and the set is up to date");
|
||||
assert_eq!(execute(&mut s, "bake.autoRebake.run", &Value::Null).unwrap()["rebaked"], json!([]));
|
||||
|
||||
// A manual bake of the map also counts; a changed high poly list is a change again.
|
||||
execute(&mut s, "bake.settings", &json!({ "antialiasing": 2 })).unwrap();
|
||||
assert_eq!(due(&mut s), [0]);
|
||||
s.begin_bake(0);
|
||||
let out = s.bake_task_for(0, None).unwrap().run(&texturecraft_engine::bake::Progress::new()).unwrap();
|
||||
s.apply_bake(0, out).unwrap();
|
||||
assert!(due(&mut s).is_empty(), "a manual bake of the map marks it up to date");
|
||||
execute(&mut s, "bake.highPoly.remove", &json!({ "all": true })).unwrap();
|
||||
assert_eq!(due(&mut s), [0]);
|
||||
|
||||
// A different output size is left to a manual bake: the maps would end up at two sizes.
|
||||
execute(&mut s, "bake.settings", &json!({ "resolution": 256 })).unwrap();
|
||||
assert!(due(&mut s).is_empty());
|
||||
|
||||
// Hostile input, and turning it off.
|
||||
for bad in [json!({ "map": "nonsense" }), json!({ "map": 3 })] {
|
||||
assert!(execute(&mut s, "bake.autoRebake", &bad).is_err(), "{bad}");
|
||||
}
|
||||
assert_eq!(execute(&mut s, "bake.autoRebake", &json!({ "map": null })).unwrap()["map"], Value::Null);
|
||||
assert!(due(&mut s).is_empty());
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cancelled_or_failed_bake_is_not_queued_again_until_the_inputs_change() {
|
||||
let (mut s, dir) = baked_session("cancel");
|
||||
execute(&mut s, "bake.autoRebake", &json!({ "map": "height" })).unwrap();
|
||||
assert!(due(&mut s).is_empty());
|
||||
execute(&mut s, "bake.settings", &json!({ "maxFrontal": 0.2 })).unwrap();
|
||||
assert_eq!(due(&mut s), [0]);
|
||||
|
||||
// What the app does: start the bake, cancel it, record the abandoned bake.
|
||||
s.begin_bake(0);
|
||||
let mut task = s.bake_task_for(0, Some(BakedMap::Height)).unwrap();
|
||||
task.tracer = None;
|
||||
let progress = texturecraft_engine::bake::Progress::new();
|
||||
progress.cancel();
|
||||
assert!(task.run(&progress).is_err(), "a cancelled bake fails");
|
||||
s.bake_abandoned([0]);
|
||||
assert!(due(&mut s).is_empty(), "not queued again on the next frame");
|
||||
|
||||
// The next change makes it due again; a bake that never started (queued behind a failed
|
||||
// one) is recorded the same way.
|
||||
execute(&mut s, "bake.settings", &json!({ "maxFrontal": 0.3 })).unwrap();
|
||||
assert_eq!(due(&mut s), [0]);
|
||||
s.bake_abandoned([0]);
|
||||
assert!(due(&mut s).is_empty());
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_set_without_the_map_baked_is_left_alone() {
|
||||
let mut s = Session::new_project("t", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap();
|
||||
execute(&mut s, "bake.autoRebake", &json!({ "map": "height" })).unwrap();
|
||||
execute(&mut s, "bake.settings", &json!({ "maxFrontal": 0.3 })).unwrap();
|
||||
assert!(due(&mut s).is_empty(), "Auto-rebake keeps a bake up to date, it doesn't start one");
|
||||
}
|
||||
Reference in new issue
Block a user