mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-25 03:53:53 +01:00
* LR-EDIT-DETAIL-DENOISE: AI Denoise works out of the box with a built-in denoiser Turning AI Denoise on no longer needs an ONNX model and a denoise-model.json. Without a model of the user's own, the engine runs a built-in pure-Rust denoiser (noise measured per tile, non-local means on the Bayer planes, edge-directed demosaic) through the same tiling, cache, queue, preview and export path as a model. Installing a model stays optional; Stop using returns to the built-in one, and the id `builtin` is reserved. The set-up prompt and its resume-after-install flow (model_setup.rs, PhotoState::NoModel) are removed. Tests updated for the new behaviour, with new tests for the built-in path; docs, trackers and locale catalogs updated. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: built-in denoiser cleans the colour planes together and adapts to the noise The built-in denoiser now measures the photo's noise once from the whole mosaic (the model prepares the photo before its tiles run) instead of per tile, stabilises the variance (generalised Anscombe), rotates the four Bayer planes with a Hadamard transform and runs a non-local-means filter whose patch distance spans all planes, so colour blotches are cleaned with the brightness. It cleans gently at low noise to keep fine detail, and harder, with a second pass that matches on the first pass's cleaner picture, as the noise grows. Cached pictures made by the first version are made again (builtin version 2). Adds an opt-in benchmark (tests/denoise_builtin_quality.rs, public CC0 raw plus noise of known level, scored against the original) and tests for the whole-photo noise estimate, strong noise, bad noise values and the strength ramp. On the Sony A7 III sample the result is 3.6, 7.0, 9.1 and 9.7 dB above the noisy picture at light, medium, heavy and extreme noise, against 2.9, 6.0, 6.5 and 5.8 dB for the first version. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * contributors: add esli1283 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: docs say how to run the built-in denoiser's quality benchmark Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: built-in denoiser cleans colour further, keeps more texture and leaves no isolated specks Colour-difference planes get a wider pass guided by the cleaned brightness plane, the first-pass threshold is lower (more texture kept), and cells with no matching patch lean on a looser average in heavy noise. Skips negligible weights for speed; bumps builtin::VERSION to 3. Benchmark adds a texture figure; on the public A7 III raw the gain over the noisy picture is +3.7 / +7.2 / +9.7 / +10.4 dB (was +3.6 / +7.0 / +9.1 / +9.7). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Windows: tests build paths one component at a time and doc_path has no unused variable On Windows `cargo xtask ci` failed before it reached any of the work under test: - doc_path.rs bound the gvfs path outside the cfg(not(windows)) block that uses it, an unused variable under -D warnings. - tests_import and the MCP headless discovery test joined "trip/day2" onto a temp folder, giving mixed separators, and compared them with the normalised paths the folder walk returns. They now join each component. Test and lint changes only. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: built-in denoiser filters with block-matching collaborative filtering, on the GPU The built-in denoiser's filter is now block matching with collaborative filtering (BM3D / CBM3D, written from the paper) instead of non-local means. On the public Sony A7 III benchmark with added noise it scores 0.25 to 0.6 dB higher at every noise level and keeps more texture at medium to extreme noise (light noise keeps slightly less: 0.58 against 0.62), as fast as before on the processor. docs/denoise.md records the comparison with non-local means and weighted nuclear norm minimisation (tried, not kept: about +0.03 texture for ten times the time) and what the tuning showed. - denoise-core: bm3d.rs (matching on the brightness plane, 2-D DCT plus Haar across the group, hard threshold then Wiener, hard threshold rising with the noise). Builtin::run_with takes the filter stage, so every other step is shared between devices. Tuning.bm3d selects it; non-local means stays as the fallback. builtin::VERSION is 4, so cached pictures are made again. - gpu: bm3d.rs and wgsl/bm3d_*.wgsl run the grouping, the filtering and the putting back on the graphics card, in the same order as the reference (the results agree to rounding: at most 0.1 noise deviation at a few tie blocks, 4e-6 on average, identical every run). Noise measurement, stabilising and the demosaic stay on the processor. FXC (DX12) needs the working arrays at module scope and no dynamically indexed vector: documented in docs/gpu-pipeline.md. - engine: the card is set up once per process for the first photo that may use it, checked against the processor on a check tile (in the filter's own domain: the demosaic's direction choice can flip a pixel on a tiny difference), timed, and used where it is faster. A tile it gets wrong runs on the processor and three stop it. Settings' Run on choice, device line and "Try the graphics card again" now work for the built-in denoiser. Its set-up marker lives in the models folder. - The benchmark takes LC_GPU=1, LC_NLM=1 and LC_B_* for sweeps. On an RTX 4080 SUPER a 256-cell tile takes 22 ms on the card against 408 ms on one processor thread, and the 1536 x 1024 benchmark crop 0.13 to 0.24 s against 1.0 to 2.1 s on four threads, with identical scores. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: the built-in denoiser measures the noise per colour plane and sets its strength from it The first noise estimator read a picture's texture as noise: it never reported less than about 0.003 at mid-grey whatever the real noise, so the denoiser always believed a photo was about as noisy as ISO 12800. The threshold curve was tuned on that over-estimate, and its levels (0.024 to 0.14) are far noisier than real Canon bodies produce (about 0.0025 at ISO 400 up to 0.038 above ISO 25600, measured on a private library; only aggregates are recorded in the docs). - estimate_noise: each colour plane is read on its own (they sit at different levels in a raw), a second-difference operator cancels flat and sloping signal, 12 x 12 blocks, the quietest 5 % at each brightness divided by the bias of that quantile of pure noise (0.67, measured), a constrained line fit. The first version stays as estimate_noise_v1 to compare with. On 30 public pictures with known added noise the 10th to 90th percentile of estimated / true deviation at mid-grey is 0.97 to 1.29 at the ISO 6400 level against 0.94 to 1.96. - bm3d::Params::lambda is a curve of the noise (1.2 at 0.0028 up to 3.5 at 0.14, straight against its logarithm) instead of two end values and a strength, and plan / filter take the noise level; the GPU path shares plan, so CPU and GPU agree as before. Fitted on clean low-ISO photos of a Canon EOS R and R6 Mark III with noise of their measured levels added: the lowest threshold within about a quarter of a decibel of the best at each level. - Result (PSNR, EOS R / R6 Mark III, 10 + 5 photos): ISO 25600 level 38.36 / 38.86 from 36.18 / 36.21, ISO 51200 level 33.83 / 34.45 from 28.42 / 28.78, ISO 6400 39.93 / 40.69 from 39.30 / 39.74, quiet photos no longer over-smoothed; the noise left in the result falls from 1.3 to 3.1 down to 0.73 to 0.99. - Tests: threshold curve, flat versus textured pictures, no-flat-area pictures, clipped / NaN mosaics; an opt-in accuracy harness on the public corpus (denoise_noise_estimate) with a calibration of the quantile bias; the benchmark takes LC_REAL, LC_BIN, LC_FULL, LC_EST and LC_B_LOFF. - builtin::VERSION is 5, so cached pictures are made again. docs/denoise.md has "How the noise is measured" and "Fitted on real cameras". Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: GPU workspace pool cannot miss a wake-up; benchmark path and parity date fixed Review findings (CodeRabbit) on the built-in denoiser pull request: - bm3d::Bm3dGpu::forget and nn::NetRunner::forget changed the workspace count without the free-list lock. A take() that had found no free workspace and read made == WORKSPACES, but had not gone to sleep yet, could miss the wake-up and wait for good on a slot that was already free. forget now changes the count under the free lock, in the same order as take(), then notifies. - A test for each runner holds the free lock and checks that forget waits for it; both fail on the old code and pass now. - docs/denoise.md: the benchmark command resolves the raw file from the repository root, since the test runs from the crate's folder. - docs/target-app-parity.md: Last updated matches its newest revision-history entry. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: doc comments for the functions this branch adds CodeRabbit's pre-merge docstring check counted 49.72 % of the functions this pull request touches as documented (threshold 80 %). Every function the branch adds, the built-in denoiser's block-matching and non-local-means filters, its noise estimator, the GPU runner and their tests, now says what it does or what the test proves. Older functions the branch only touched are left as they were. Comments only: no code line changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: make the GPU set-up marker's folder; fix the benchmark's default path The crash guard was lost when the models folder did not exist yet. The opt-in quality benchmark resolved its default raw file against crates/engine. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: enforce the cache limit without a models folder The built-in denoiser makes products when there is no models folder, but eviction returned early in that case, so the cache could outgrow its limit. It now applies the default limit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * LR-EDIT-DETAIL-DENOISE: the cache-limit test needs no 20 GiB file off Unix The no-models-folder test now checks the limit itself on every platform and keeps the oversized sparse file for Unix only. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * S0002: review fixes for the built-in denoiser Tell the runner a photo's noise only when it could be measured, run prepare() after the work gate with a cancel check, reword the Settings hint for builds without a models folder, drop untranslated placeholder locale entries (they fall back to English) and restore the cs.json spacing; rustfmt. Co-Authored-By: Claude Code <noreply@anthropic.com> * S0002: fix review findings: command descriptions, stale gpu test, self-test noise The denoise.models.select and denoise.toggle descriptions now describe the built-in fallback. Drop our workspace-forget test, which read fields that upstream's Workspaces pool (#2631, same fix) replaced and so did not compile; upstream's own tests cover it. The built-in self-test is told a noise instead of measuring a 16-cell tile too small to measure. Co-Authored-By: Claude Code <noreply@anthropic.com> --------- Co-authored-by: esli1283 <esli.aaron@outlook.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Claude Code <noreply@anthropic.com> Co-authored-by: FredApps <FredApps@users.noreply.github.com>
521 lines
24 KiB
Rust
521 lines
24 KiB
Rust
#[cfg(feature = "denoise")]
|
|
use super::builtin::{builtin_card, builtin_marker};
|
|
#[cfg(doc)]
|
|
use super::model::Model;
|
|
use super::model::STACK;
|
|
use super::products::{Active, JobSpec, MakeError, Progress, eligible, make_product, product_key, product_path, products_in};
|
|
use super::settings::{DIR, SETTINGS_TTL, installed_models, read_settings};
|
|
use super::state::{AUTO_LOOK, Failure, MAX_QUEUE, Outcome, Pace, PhotoState, Running};
|
|
use super::{
|
|
Arc, DenoiseSpec, DevelopSettings, HashMap, HashSet, MakeProduct, Mutex, Ordering, PairLoader, Path, PathBuf, Photo, PhotoId, PoisonError,
|
|
Selection, Session, Source, Value, json, product,
|
|
};
|
|
|
|
impl Session {
|
|
/// Where the active model's tiles run (see [`Model::device`]); `{kind: "none"}` while no model is loaded.
|
|
pub(crate) fn denoise_device(&self) -> Value {
|
|
let loaded = self.denoise.active.as_ref().and_then(|a| a.model.lock().unwrap_or_else(PoisonError::into_inner).clone());
|
|
match loaded {
|
|
Some(m) => m.device(self.denoise.settings.run_on()),
|
|
#[cfg(feature = "denoise")]
|
|
None if self.denoise.active.as_ref().is_some_and(|a| a.id == lightcraft_denoise::builtin::ID) => {
|
|
let onnx = self.denoise.active.as_ref().map(|a| a.onnx.clone()).unwrap_or_default();
|
|
builtin_card(&builtin_marker(&onnx)).device(self.denoise.settings.run_on())
|
|
}
|
|
None => json!({"kind": "none"}),
|
|
}
|
|
}
|
|
|
|
/// The folder the products of the open library are kept in; `None` without a library on disk.
|
|
pub(crate) fn denoise_products_dir(&self) -> Option<PathBuf> {
|
|
self.library.as_ref().filter(|l| l.on_disk).map(|l| l.dir.join(DIR))
|
|
}
|
|
|
|
/// Read which model is in use (at most every couple of seconds): the manifest and file of the model the settings
|
|
/// name, if it is installed.
|
|
pub(crate) fn denoise_refresh_active(&mut self, force: bool) {
|
|
let fresh = self.denoise.active_seen.is_some_and(|t| t.elapsed() < SETTINGS_TTL);
|
|
if fresh && !force {
|
|
return;
|
|
}
|
|
self.denoise.active_seen = Some(web_time::Instant::now());
|
|
self.denoise.settings = self.denoise.models_dir.as_deref().map(read_settings).unwrap_or_default();
|
|
let chosen = self.denoise.models_dir.as_deref().and_then(|dir| {
|
|
let id = self.denoise.settings.model.clone()?;
|
|
installed_models(dir).into_iter().find(|i| i.manifest.id == id)
|
|
});
|
|
// no model chosen (or the one chosen is gone): the built-in denoiser
|
|
let (fingerprint, manifest, onnx) = match chosen {
|
|
Some(i) => (i.fingerprint(), i.manifest, i.onnx),
|
|
None => {
|
|
let manifest = lightcraft_denoise::builtin::manifest();
|
|
let onnx = self.denoise.models_dir.as_deref().map(|d| d.join(lightcraft_denoise::builtin::ID)).unwrap_or_default();
|
|
(format!("{}@{}", manifest.id, manifest.version), manifest, onnx)
|
|
}
|
|
};
|
|
if self.denoise.active.as_ref().is_some_and(|a| a.fingerprint == fingerprint) {
|
|
return;
|
|
}
|
|
// another model: what was made with the old one is not this one's, and what failed may work now
|
|
self.denoise.active = Some(Active { id: manifest.id.clone(), manifest, onnx, fingerprint, model: Arc::new(Mutex::new(None)) });
|
|
if let Some(r) = self.denoise.running.take() {
|
|
r.progress.cancel_now();
|
|
}
|
|
self.denoise.queue.clear();
|
|
self.denoise.failed.clear();
|
|
self.denoise.dirty = true;
|
|
}
|
|
|
|
fn denoise_spec(&self, p: &Photo) -> Option<(DenoiseSpec, String)> {
|
|
let (dir, active) = (self.denoise_products_dir()?, self.denoise.active.as_ref()?);
|
|
let key = product_key(&active.fingerprint, p);
|
|
Some((DenoiseSpec { product: product_path(&dir, &key), key: key.clone(), make: None }, key))
|
|
}
|
|
|
|
/// Make the index of ready photos from the products on disk (a directory listing and a hash per photo: no file is
|
|
/// opened). Pictures that appeared or vanished drop the decoded sources they affect. The photo being made is its
|
|
/// job's to report (see [`Self::denoise_check_photo`]).
|
|
pub(crate) fn denoise_reindex(&mut self) {
|
|
self.denoise.dirty = false;
|
|
self.denoise.reindexed = Some((self.catalog.revision, web_time::Instant::now()));
|
|
let running = self.denoise.running.as_ref().map(|r| r.photo);
|
|
let mut now: HashMap<PhotoId, DenoiseSpec> = HashMap::new();
|
|
if let (Some(dir), Some(active)) = (self.denoise_products_dir(), self.denoise.active.as_ref()) {
|
|
let names: HashSet<String> =
|
|
products_in(&dir).into_iter().filter_map(|(p, _, _)| p.file_stem().and_then(|s| s.to_str()).map(str::to_string)).collect();
|
|
if !names.is_empty() {
|
|
for p in self.catalog.photos().filter(|p| eligible(p) && running != Some(p.id)) {
|
|
let key = product_key(&active.fingerprint, p);
|
|
if names.contains(&key) {
|
|
now.insert(p.id, DenoiseSpec { product: product_path(&dir, &key), key, make: None });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let mut changed = false;
|
|
for id in self.media.denoise.ids() {
|
|
if !now.contains_key(&id) {
|
|
self.media.denoise.remove(id);
|
|
self.media.forget(id);
|
|
changed = true;
|
|
}
|
|
}
|
|
for (id, spec) in now {
|
|
if self.media.denoise.spec(id).is_none_or(|old| old.key != spec.key) {
|
|
self.media.denoise.set(id, spec);
|
|
self.media.forget(id);
|
|
changed = true;
|
|
}
|
|
}
|
|
if changed {
|
|
self.denoise.generation += 1;
|
|
}
|
|
}
|
|
|
|
/// A photo whose file changed (its content key is not the one its index entry was made for) has no picture.
|
|
///
|
|
/// The photo whose picture is being made is left to its job: the worker saves the picture before it reports, so the
|
|
/// file is on disk while the job still counts as running. Finding it here would make the photo Ready while the session
|
|
/// is busy with it (#719); [`Self::denoise_poll`] makes it Ready in the pump that takes the job's result instead.
|
|
fn denoise_check_photo(&mut self, id: PhotoId) {
|
|
if self.denoise.running.as_ref().is_some_and(|r| r.photo == id) {
|
|
return;
|
|
}
|
|
let Some(p) = self.catalog.photo(id).cloned() else { return };
|
|
let Some((spec, _)) = self.denoise_spec(&p) else { return };
|
|
match self.media.denoise.spec(id) {
|
|
Some(have) if have.key != spec.key => {
|
|
self.media.denoise.remove(id);
|
|
self.media.forget(id);
|
|
self.denoise.generation += 1;
|
|
}
|
|
None if spec.product.is_file() && product::is_current(&spec.product, &spec.key) => {
|
|
// made by an export, or by another session
|
|
self.media.denoise.set(id, spec);
|
|
self.media.forget(id);
|
|
self.denoise.generation += 1;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
/// How photo `id` stands.
|
|
pub fn denoise_photo_state(&self, id: PhotoId) -> PhotoState {
|
|
let Some(p) = self.catalog.photo(id) else { return PhotoState::NotApplicable };
|
|
if !eligible(p) {
|
|
return PhotoState::NotApplicable;
|
|
}
|
|
if self.denoise.active.is_none() {
|
|
// the model in use has not been looked at yet (just started)
|
|
return PhotoState::Idle;
|
|
}
|
|
if self.denoise_products_dir().is_none() {
|
|
return PhotoState::Failed {
|
|
why: "AI Denoise keeps its pictures in the library folder, and this session has none.".into(),
|
|
unsupported: true,
|
|
};
|
|
}
|
|
if self.media.denoise.spec(id).is_some() {
|
|
return PhotoState::Ready;
|
|
}
|
|
if let Some(r) = self.denoise.running.as_ref().filter(|r| r.photo == id) {
|
|
return PhotoState::Running { done: r.progress.done.load(Ordering::Relaxed), total: r.progress.total.load(Ordering::Relaxed) };
|
|
}
|
|
if let Some(ahead) = self.denoise.queue.iter().position(|q| *q == id) {
|
|
return PhotoState::Queued { ahead: ahead + usize::from(self.denoise.running.is_some()) };
|
|
}
|
|
let key = self.denoise_spec(p).map(|s| s.1);
|
|
match self.denoise.failed.get(&id) {
|
|
Some(f) if Some(&f.key) == key.as_ref() => PhotoState::Failed { why: f.why.clone(), unsupported: f.unsupported },
|
|
_ => PhotoState::Idle,
|
|
}
|
|
}
|
|
|
|
/// Ask for photos' pictures to be made (the ones that are not made, queued or known to be impossible). With `retry`
|
|
/// a photo that failed before is tried again. A photo that is not raw is skipped. Returns how many were queued.
|
|
pub(crate) fn denoise_enqueue(&mut self, ids: &[PhotoId], front: bool, retry: bool) -> usize {
|
|
self.denoise_refresh_active(false);
|
|
if self.denoise.active.is_none() || self.denoise_products_dir().is_none() {
|
|
return 0;
|
|
}
|
|
let mut added = 0;
|
|
for id in ids {
|
|
let Some(p) = self.catalog.photo(*id).cloned() else { continue };
|
|
if !eligible(&p) || self.media.denoise.spec(*id).is_some() || self.denoise.queue.contains(id) {
|
|
continue;
|
|
}
|
|
if self.denoise.running.as_ref().is_some_and(|r| r.photo == *id) || self.denoise.queue.len() >= MAX_QUEUE {
|
|
continue;
|
|
}
|
|
let Some((_, key)) = self.denoise_spec(&p) else { continue };
|
|
if retry {
|
|
self.denoise.failed.remove(id);
|
|
} else if self.denoise.failed.get(id).is_some_and(|f| f.key == key) {
|
|
continue;
|
|
}
|
|
if front {
|
|
self.denoise.queue.push_front(*id);
|
|
} else {
|
|
self.denoise.queue.push_back(*id);
|
|
}
|
|
added += 1;
|
|
}
|
|
added
|
|
}
|
|
|
|
/// Stop the job that is running, and with `all` forget everything that waits.
|
|
pub(crate) fn denoise_cancel(&mut self, photo: Option<PhotoId>, all: bool) -> usize {
|
|
let mut n = 0;
|
|
if let Some(r) = &self.denoise.running
|
|
&& photo.is_none_or(|p| p == r.photo)
|
|
{
|
|
r.progress.cancel_now();
|
|
n += 1;
|
|
}
|
|
let before = self.denoise.queue.len();
|
|
match photo {
|
|
Some(p) => self.denoise.queue.retain(|q| *q != p),
|
|
None if all => self.denoise.queue.clear(),
|
|
None => {}
|
|
}
|
|
n + before - self.denoise.queue.len()
|
|
}
|
|
|
|
/// The photos the automatic queue looks at: the one open and the ones selected, those with a Denoise amount.
|
|
fn denoise_wanted(&self) -> Vec<PhotoId> {
|
|
let Selection { active, ids, .. } = &self.selection;
|
|
active
|
|
.iter()
|
|
.chain(ids.iter().take(AUTO_LOOK))
|
|
.copied()
|
|
.filter(|id| self.catalog.photo(*id).is_some_and(|p| p.develop.denoise_amount() > 0.0))
|
|
.collect()
|
|
}
|
|
|
|
fn denoise_job_spec(&self, p: &Photo, parallel: usize) -> Option<JobSpec> {
|
|
let Source::File { path } = &p.source else { return None };
|
|
let (spec, _) = self.denoise_spec(p)?;
|
|
let active = self.denoise.active.as_ref()?;
|
|
Some(JobSpec {
|
|
path: path.clone(),
|
|
read: self.media.file_bytes.clone(),
|
|
product: spec.product,
|
|
key: spec.key,
|
|
manifest: active.manifest.clone(),
|
|
onnx: active.onnx.clone(),
|
|
model: active.model.clone(),
|
|
loader: self.denoise.loader.clone(),
|
|
parallel,
|
|
run_on: self.denoise.settings.run_on(),
|
|
})
|
|
}
|
|
|
|
/// Start the next queued photo on its own thread.
|
|
fn denoise_start_next(&mut self, parallel: usize) {
|
|
while let Some(id) = self.denoise.queue.pop_front() {
|
|
let Some(p) = self.catalog.photo(id).cloned() else { continue };
|
|
if !eligible(&p) || self.media.denoise.spec(id).is_some() {
|
|
continue;
|
|
}
|
|
let Some(spec) = self.denoise_job_spec(&p, parallel) else { continue };
|
|
// another session or an export may have made it since it was queued
|
|
if product::is_current(&spec.product, &spec.key) {
|
|
self.denoise_check_photo(id);
|
|
continue;
|
|
}
|
|
let progress = Arc::new(Progress::default());
|
|
let outcome: Arc<Mutex<Option<Outcome>>> = Arc::new(Mutex::new(None));
|
|
let (key, product) = (spec.key.clone(), spec.product.clone());
|
|
let (worker_progress, worker_outcome) = (progress.clone(), outcome.clone());
|
|
let started = std::thread::Builder::new().name("denoise".into()).stack_size(STACK).spawn(move || {
|
|
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| make_product(&spec, Some(&worker_progress))));
|
|
let o = match result {
|
|
Ok(Ok(_)) => Outcome::Done,
|
|
Ok(Err(MakeError::Cancelled)) => Outcome::Cancelled,
|
|
Ok(Err(MakeError::Unsupported(why))) => Outcome::Failed(why, true),
|
|
Ok(Err(MakeError::Failed(why))) => Outcome::Failed(why, false),
|
|
Err(_) => Outcome::Failed("the denoiser stopped unexpectedly".into(), false),
|
|
};
|
|
*worker_outcome.lock().unwrap_or_else(PoisonError::into_inner) = Some(o);
|
|
});
|
|
match started {
|
|
Ok(_) => {
|
|
self.denoise.running = Some(Running { photo: id, key, product, progress, outcome, started: web_time::Instant::now() });
|
|
}
|
|
Err(e) => {
|
|
self.denoise.failed.insert(id, Failure { key, why: format!("could not start the denoiser: {e}"), unsupported: false });
|
|
continue;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// Take in the running job's result.
|
|
fn denoise_poll(&mut self) {
|
|
let Some(r) = &self.denoise.running else { return };
|
|
let Some(outcome) = r.outcome.lock().unwrap_or_else(PoisonError::into_inner).take() else { return };
|
|
let Some(r) = self.denoise.running.take() else { return };
|
|
match outcome {
|
|
Outcome::Done => {
|
|
self.denoise.made += 1;
|
|
self.denoise_check_photo(r.photo);
|
|
self.denoise_evict(Some(&r.product));
|
|
}
|
|
Outcome::Failed(why, unsupported) => {
|
|
self.denoise.failed.insert(r.photo, Failure { key: r.key, why, unsupported });
|
|
}
|
|
Outcome::Cancelled => {}
|
|
}
|
|
// the picture's state changed: the interface draws it again
|
|
self.denoise.generation += 1;
|
|
}
|
|
|
|
/// Keep the cache within its limit: the pictures nobody asks for go first, the oldest first; `keep` (the one just
|
|
/// made) never goes. The index forgets what was deleted.
|
|
pub(crate) fn denoise_evict(&mut self, keep: Option<&Path>) {
|
|
self.denoise_evict_to(self.denoise_cache_limit(), keep);
|
|
}
|
|
|
|
/// The cache's size limit in bytes (the default without a models folder).
|
|
pub(crate) fn denoise_cache_limit(&self) -> u64 {
|
|
self.denoise.models_dir.as_deref().map(read_settings).unwrap_or_default().cache_bytes()
|
|
}
|
|
|
|
/// [`Self::denoise_evict`] for a limit of `limit` bytes.
|
|
pub(crate) fn denoise_evict_to(&mut self, limit: u64, keep: Option<&Path>) {
|
|
let Some(dir) = self.denoise_products_dir() else { return };
|
|
let mut files = products_in(&dir);
|
|
let mut total: u64 = files.iter().map(|f| f.1).sum();
|
|
if total <= limit {
|
|
return;
|
|
}
|
|
// the pictures photos use now (a Denoise amount above 0) are the last to go
|
|
let wanted: HashSet<PathBuf> = self
|
|
.media
|
|
.denoise
|
|
.ids()
|
|
.into_iter()
|
|
.filter(|id| self.catalog.photo(*id).is_some_and(|p| p.develop.denoise_amount() > 0.0))
|
|
.filter_map(|id| self.media.denoise.spec(id).map(|s| s.product.clone()))
|
|
.collect();
|
|
files.sort_by_key(|(path, _, modified)| (wanted.contains(path), *modified));
|
|
let mut gone: HashSet<PathBuf> = HashSet::new();
|
|
for (path, size, _) in files {
|
|
if total <= limit {
|
|
break;
|
|
}
|
|
if keep == Some(path.as_path()) {
|
|
continue;
|
|
}
|
|
if std::fs::remove_file(&path).is_ok() {
|
|
total = total.saturating_sub(size);
|
|
gone.insert(path);
|
|
}
|
|
}
|
|
if gone.is_empty() {
|
|
return;
|
|
}
|
|
for id in self.media.denoise.ids() {
|
|
if self.media.denoise.spec(id).is_some_and(|s| gone.contains(&s.product)) {
|
|
self.media.denoise.remove(id);
|
|
self.media.forget(id);
|
|
}
|
|
}
|
|
self.denoise.generation += 1;
|
|
}
|
|
|
|
/// Delete every product of this library (they are made again when wanted).
|
|
pub(crate) fn denoise_clear(&mut self) -> (usize, u64) {
|
|
self.denoise_cancel(None, true);
|
|
let (mut files, mut bytes) = (0, 0);
|
|
if let Some(dir) = self.denoise_products_dir() {
|
|
for (path, size, _) in products_in(&dir) {
|
|
if std::fs::remove_file(&path).is_ok() {
|
|
files += 1;
|
|
bytes += size;
|
|
}
|
|
}
|
|
}
|
|
self.denoise.failed.clear();
|
|
self.denoise.touch();
|
|
(files, bytes)
|
|
}
|
|
|
|
/// Per frame: take in what finished, look at what the user is looking at, and keep one job going. Cheap when
|
|
/// nothing is happening (no file is opened unless a photo with a Denoise amount lacks its picture).
|
|
pub(crate) fn denoise_pump(&mut self, pace: Pace) {
|
|
crate::cmd::denoise::finish_downloads(self);
|
|
self.denoise_refresh_active(false);
|
|
// photos that changed (a replaced file has another key) must not keep the picture of what they were
|
|
let stale = !self.media.denoise.is_empty()
|
|
&& self.denoise.reindexed.is_none_or(|(rev, at)| rev != self.catalog.revision && at.elapsed() > SETTINGS_TTL);
|
|
if self.denoise.dirty || stale {
|
|
self.denoise_reindex();
|
|
}
|
|
self.denoise_poll();
|
|
if self.denoise.active.is_none() || self.denoise_products_dir().is_none() {
|
|
return;
|
|
}
|
|
let (cap, auto) = (self.denoise.settings.threads, self.denoise.settings.auto());
|
|
let wanted = self.denoise_wanted();
|
|
for id in &wanted {
|
|
self.denoise_check_photo(*id);
|
|
}
|
|
if auto {
|
|
// the photo being edited goes first
|
|
let (open, others): (Vec<PhotoId>, Vec<PhotoId>) = wanted.into_iter().partition(|id| self.selection.active == Some(*id));
|
|
self.denoise_enqueue(&others, false, false);
|
|
self.denoise_enqueue(&open, true, false);
|
|
}
|
|
if self.denoise.running.is_none() && pace != Pace::Pause {
|
|
let cores = std::thread::available_parallelism().map_or(1, |n| n.get());
|
|
self.denoise_start_next(pace.parallel(cores, cap));
|
|
}
|
|
}
|
|
|
|
/// The statistics `denoise.status` reports besides the queue (reads the cache folder).
|
|
pub(crate) fn denoise_cache_usage(&self) -> (usize, u64) {
|
|
match self.denoise_products_dir() {
|
|
Some(dir) => {
|
|
let files = products_in(&dir);
|
|
(files.len(), files.iter().map(|f| f.1).sum())
|
|
}
|
|
None => (0, 0),
|
|
}
|
|
}
|
|
|
|
/// Are pictures made without being asked for photos being looked at (Settings ▸ AI Denoise)?
|
|
pub fn denoise_auto(&self) -> bool {
|
|
self.denoise.settings.auto()
|
|
}
|
|
|
|
/// Is a picture being made, or waiting for its turn with a model to make it? (Headless runs wait for this.)
|
|
pub fn denoise_busy(&self) -> bool {
|
|
self.denoise.running.is_some() || (!self.denoise.queue.is_empty() && self.denoise.active.is_some())
|
|
}
|
|
|
|
/// Keep AI Denoise's row in the activity stack up to date (each `denoise.pump`): there while a photo is being made or
|
|
/// waits its turn, with the running photo's tiles and its file name; gone when the queue is idle. It has no ✕: what is
|
|
/// made follows the photos' Denoise amounts and Settings ▸ AI Denoise.
|
|
pub(crate) fn denoise_track(&mut self) {
|
|
if !self.denoise_busy() {
|
|
self.denoise.task = None;
|
|
return;
|
|
}
|
|
let task = self.denoise.task.get_or_insert_with(|| {
|
|
let t = self.activity.start("denoise", "AI Denoise", crate::activity::Cancel::No);
|
|
t.set_unit(crate::activity::Unit::Percent);
|
|
t
|
|
});
|
|
match &self.denoise.running {
|
|
Some(r) => {
|
|
task.progress(r.progress.done.load(Ordering::Relaxed) as u64, r.progress.total.load(Ordering::Relaxed) as u64);
|
|
task.detail(self.catalog.photo(r.photo).map_or("", |p| p.file_name.as_str()));
|
|
}
|
|
None => {
|
|
task.progress(0, 0);
|
|
task.detail("");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Seconds the running job has been going, and its progress.
|
|
pub(crate) fn denoise_running_json(&self) -> Value {
|
|
match &self.denoise.running {
|
|
Some(r) => json!({
|
|
"photo": r.photo.0,
|
|
"done": r.progress.done.load(Ordering::Relaxed),
|
|
"total": r.progress.total.load(Ordering::Relaxed),
|
|
"seconds": r.started.elapsed().as_secs_f64(),
|
|
}),
|
|
None => Value::Null,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn denoise_made(&self) -> u64 {
|
|
self.denoise.made
|
|
}
|
|
|
|
/// The running job's product path and key (tests stand in for the worker's save).
|
|
#[cfg(test)]
|
|
pub(crate) fn denoise_running_product(&self) -> Option<(PathBuf, String)> {
|
|
self.denoise.running.as_ref().map(|r| (r.product.clone(), r.key.clone()))
|
|
}
|
|
|
|
/// What an export of photo `id` with `settings` needs for its Denoise amount: its denoised picture, made now when the
|
|
/// queue has not got to it. `Ok(None)` when denoise does not apply (amount 0, not a Bayer raw), `Err` when it should
|
|
/// have and cannot (no model chosen, nowhere to keep the picture, the model failing).
|
|
pub(crate) fn denoise_for_export(&mut self, id: PhotoId, settings: &DevelopSettings) -> Result<Option<(DenoiseSpec, PairLoader)>, String> {
|
|
if settings.denoise_amount() <= 0.0 {
|
|
return Ok(None);
|
|
}
|
|
let Some(p) = self.catalog.photo(id).cloned() else { return Ok(None) };
|
|
let Some(loader) = self.media.denoise.loader.clone() else { return Ok(None) };
|
|
if !eligible(&p) {
|
|
return Ok(None);
|
|
}
|
|
self.denoise_refresh_active(false);
|
|
let name = &p.file_name;
|
|
if self.denoise.active.is_none() {
|
|
return Err(format!("{name}: Denoise is on for this photo but no denoise model is chosen; choose one in Settings, or set Denoise to 0"));
|
|
}
|
|
if self.denoise_products_dir().is_none() {
|
|
return Err(format!("{name}: Denoise needs a library on disk to keep its pictures in"));
|
|
}
|
|
self.denoise_check_photo(id);
|
|
let Some(job) = self.denoise_job_spec(&p, Pace::Full.parallel(std::thread::available_parallelism().map_or(1, |n| n.get()), None)) else {
|
|
return Ok(None);
|
|
};
|
|
let label = name.clone();
|
|
let spec = job.clone();
|
|
let make: MakeProduct = Arc::new(move || match make_product(&spec, None) {
|
|
Ok(_) => Ok(true),
|
|
Err(MakeError::Unsupported(_)) => Ok(false),
|
|
Err(MakeError::Failed(why)) => Err(format!("{label}: could not denoise: {why}")),
|
|
Err(MakeError::Cancelled) => Err(format!("{label}: denoise was cancelled")),
|
|
});
|
|
Ok(Some((DenoiseSpec { product: job.product, key: job.key, make: Some(make) }, loader)))
|
|
}
|
|
}
|