mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-22 02:12:32 +01:00
* GPU-FULL-1: LUT profiles, Refine Edges and soft proofs render on the GPU These three sent the whole loupe / compare / export render back to the CPU pipeline (media::develop and lightcraft_gpu::render declined them). - LUT profiles: the .cube table rides in the finish kernel's aux buffer (LUT_* parameters); lut_apply mirrors Lut3d::apply (trilinear, domain), applied after grain and blended by the profile amount, as on the CPU. - Refine Edges: the cross guided filter Auto Mask already ran on the device, now with a combine mode (max for strokes, mix by sqrt(refine) for Refine), applied to the combined alpha before the mask's invert and amount. - Soft proofing: proof-space gamut mapping and the destination / display gamut warnings in the kernel (PROOF_* parameters, GAMUT_WARN and warning colours generated from the CPU constants), for output spaces and displays. Equivalence tests (RTX 5070, DX12): LUT profiles at three amounts and a non-default domain, Refine 15/60/100 on two masks, proofs in 16 combinations of space, output, display and warnings - all max 1 LSB, mean <= 0.0005 LSB. The tests also check that strong refine and the destination warning change the image (so they aren't comparing two unchanged renders). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL-2: spot removal, defringe and the histogram on the GPU Spots and defringe made every render that had them (and every white balance or spot edit) read the resampled image back, run the scene-linear stage on the CPU and upload it again. Now only the ProRAW local tone map (off by default) does. - Defringe: OkLab pass, separable lightness min/max filter, per-pixel apply. Parameters come from the new optics::defringe_params, which the CPU stage now uses too. - Spots: new spot module (spot_prep / spot_apply over each spot's box, the Heal tone correction as device Gaussians), spot by spot in place so each reads what the earlier ones left, as on the CPU. Remove's automatic source: spot_score rates the 48 candidates on the device and only the scores come back. Layout, candidates, spoke directions, radii and ranking are shared helpers in pipeline::spots (spots::apply uses them; behaviour unchanged, pipeline tests green). - Histogram: counted with atomics on the device on Histogram::of_srgb8's sampling grid (exact), instead of a CPU pass over the readback. Tests: spots (clone, heal, auto remove incl. near the edge where no candidate fits, brushed strokes, feather 0, cut by the edge, overlapping) and defringe (purple/green, hue ranges, preview and full size) max 1 LSB; the device histogram equals the CPU count of the returned image for small, sampled (>1 MP) and windowed renders; the scoring kernel picks the CPU's candidate. (The edge case caught a real bug: "doesn't fit" was written as a decimal literal that rounds below f32::MAX.) Unit tests that create the device now take turns with the crash-sentinel test, which swaps a global. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL-2: fix the spot kernels' build; GPU tests fail when kernels don't build 07d4680b's spot module declared `const F32_MAX = bitcast<f32>(...)`, which naga can't evaluate as a constant: the kernels failed to build, the device was unavailable, and the equivalence tests skipped as if there were no adapter - so that commit's GPU tests didn't actually run. f32::MAX is now a function, and the equivalence suite (and the device unit test) fail instead of skipping when an adapter exists but the kernels don't build. Re-verified on the RTX 5070 (DX12), all on the device: 21 equivalence tests incl. the 8 spot cases (max 1 LSB), defringe, the exact device histogram, and the auto-source unit test. Adds an ignored thumbnail throughput bench (tests/thumbs_bench.rs). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL-3: thumbnails render on the GPU once the device is ready Every cached job (grid and filmstrip thumbnails, preset and profile previews, faces, quick stand-ins) was forced onto the CPU. They now use the GPU when lightcraft_gpu::ready() - so they never block on device creation at startup (1.8 s incl. kernels here) and render on the CPU until then. Views and exports keep waiting for the device as before. tests/thumbs_bench.rs (ignored): six workers rendering 256 px thumbnails of 512 px sources with preset-like settings (NR, sharpening, clarity, grading, a gradient mask): RTX 5070 3.6 ms each vs 4.3-5.2 ms on 32 CPU threads, with the cores left free for raw decoding. Engine and UI suites: unchanged (only the 12 failures that predate this branch on Windows: tests_sync paths, tests_model_setup). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL-4: the loupe's drafts are drawn straight from the GPU Rendering and drawing now share one device: the desktop app creates the window's wgpu device with the adapter's limits and hands it to the GPU renderer (lightcraft_gpu::adopt_window_device, on a background thread at startup, under the crash sentinel; a device that doesn't suit falls back to one of the renderer's own, as before). A drag's drafts of the views are RenderJob::on_screen: the render ends in an Rgba8Unorm texture (lightcraft_gpu::render_display -> Output::Display, new `present` kernels: unwritten-pixel count, row padding for the copy) and egui-wgpu draws it as a native texture (ui-egui ScreenTextures, implemented by the app) - no readback, no conversion to egui pixels, no re-upload. The readback's checks run on the device (atomic unwritten count, device histogram). Renders that need pixels keep the readback: the settled full render (view cache, overlays, screenshots), thumbnails, exports, overlays, windows that spots grew, a monitor profile. - engine: RenderJob::on_screen, RenderResult::frame (ScreenFrame). - ui-egui: Tex::native / Tex::id() (21 draw sites), Renderer set_screen_textures, natives per slot (freed when pixels return), ui.inspect perf.gpuWindowDevice / screenTextures / mainOnGpu. - app: WindowTextures, window device limits, LIGHTCRAFT_GPU_WINDOW=0 to opt out. Tests: crates/gpu/tests/display.rs adopts a device in its own process and checks the texture holds exactly the readback's pixels (padded and unpadded widths, draft and full), the histogram matches, and overlays / grown windows come back as pixels. In the app (RTX 5070, a 45 MP CR3 from the user's library copy): all 90 drafts of a drag drawn from GPU textures, 1.69 ms vs 2.05 ms median per draft render with the readback; the frame screenshot shows the image correctly; pixels return after the drag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL-2: the denoise runner tests take turns with the crash-sentinel tests Building a denoise runner creates a device and compiles kernels under the init marker. Running in parallel with backend::tests, it could arm the marker the test had just set, so "is removed afterwards" failed now and then. The runner tests now hold DEVICE_TEST_LOCK while they build, like the render tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL-4: fix the build after merging upstream main Upstream's QuickJob panic guard builds a RenderResult (add `frame: None`), its idle-trim test builds a Tex (add `native: None`), and its on_exit test builds App (add the WindowTextures slot). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL-2: upstream's blur tests take turns with the crash-sentinel tests gaussian_matches_cpu and bench_kernels create the device, so they hold DEVICE_TEST_LOCK like every other device test in lightcraft-gpu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Tests: the same-folder export test compares paths separator-blind (Windows) The written path is the original's folder (`\` on Windows) joined with `Exports/IMG_1.jpg`; the expected one had `\Exports`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Tests: the folder-drop tests find folder widgets separator-blind (Windows) The folder tree spells its paths with `/`; the tests built widget ids from Windows paths with `\`, so none was found and every test failed on Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Tests: the Windows path fixes take the same form as in the other PR branches So the branches merge into main without conflicts between their copies of the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: headless screenshots read back GPU-drawn views; LIGHTCRAFT_GPU_LIMITS caps the adopted device Review of PR #1065: - A headless `ui.screenshot` taken mid-drag showed the photo as a white rectangle: the GPU-resident draft left no CPU copy. With keep_pixels (control channel) the renderer now keeps each view's frame, and `cpu_textures` reads it back through `ScreenTextures::read` (`lightcraft_gpu::read_texture`). Drawing still never reads back, so drags keep the speed-up. - Adopting the window's device skipped LIGHTCRAFT_GPU_LIMITS; `Gpu::adopt` now caps its limits the same way `Gpu::new` does. Tests: a_headless_screenshot_reads_back_a_view_drawn_from_the_gpu (ui-egui), the_limits_override_caps_an_adopted_device (gpu); the display test uses read_texture and skips through lightcraft_testkit::skip!. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: review fixes (CodeRabbit 2026-10-21) and slideshow/print draw GPU-drawn views - the window-device adoption is announced before its thread starts: until it finishes, the settings' warm-up and renders don't create a device of their own (they raced it) - the device error / device-lost handlers go on only after the kernels build, so a declined window device keeps the window's own handler - releasing a slot frees its GPU-resident texture and kept frame - a soft proof into a colour space drafts full size on the GPU; only one to a device profile counts as proofed for `handles` - docs: Fill spots and device-profile soft proofs render on the CPU - slideshow, slide layout and print draw `Tex::id()` (the GPU texture when there is one) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: the display test skips when LIGHTCRAFT_GPU=0 switches the GPU off It panicked ("GPU rendering is off") instead of skipping like the other GPU tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: a view readback never panics on a lost device read_texture (headless screenshots of GPU-drawn views) polled the device directly. On a lost device wgpu 30 panics in Device::poll; it now goes through the same guard as the renderer's own polls (poll_device): a lost device returns an error, stops the GPU and is not polled again. Review of PR #1065. Also quiets a browser-build dead-code warning on OnScreen::Frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: an HDR edit's histogram shows the HDR range on the on-screen GPU path too develop_on_screen returned the GPU's SDR histogram for both its Display and Image arms; it now applies lightcraft_pipeline::hdr_histogram as develop_reporting does (None for drafts, windows, proofs and non-HDR edits, which keep the GPU's histogram). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: the Navigator draws the photo's texture as it is drawn in the loupe The Navigator falls back to the main view's texture before the photo has a thumbnail. It drew `t.tex`, which is stale (or the 1x1 placeholder) when the main view holds a GPU-resident draft; it now draws `Tex::id()`, like every other draw site. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: the egui 0.37 lifecycle test's texture names no native id Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: rustfmt the lifecycle test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: the second window fits the frame's size; freed GPU frames drop their views The second window sized its image from the texture handle, which is only a 1x1 placeholder for a GPU-resident render; it now uses the frame's size. When a GPU-resident render can't be shown and every native texture is freed, the views pointing at them (Before, Region, Compare tiles) are dropped too, so they render again as pixels instead of drawing a freed id. Test: freeing_gpu_frames_drops_the_views_drawn_from_them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(gpu): LIGHTCRAFT_GPU_ADAPTER picks the card for rendering too The variable chose the card only for AI Denoise and the face detector (two copies of the same enumerate-and-match code); photo rendering always took the first high-performance adapter, on a two-card machine the one driving the screens. One helper, backend::pick_adapter, now serves the render device (Gpu::new), the denoise network and the face detector: the first hardware card whose name contains the value (any case), else the high-performance one. A name that matches no card is logged and falls back instead of failing. backend::may_adopt_window_device says whether rendering may share the window's device (only for the window's card). Name matching is unit-tested without a GPU; the equivalence tests print the card they ran on. Documented in docs/gpu-pipeline.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: a view switching to a GPU texture releases its pixel image Review of #1065 (CodeRabbit nitpick): when a slot that had pixels switched to a GPU-resident texture, its egui texture kept the full-size image, never drawn while the native texture is set. Tex::show_native now swaps it for a 1x1 placeholder on the switch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(gpu): LIGHTCRAFT_GPU_ADAPTER picks among several matches in a defined order and logs once Review of #3428: - several cards match: the one whose whole name it is, then dedicated before built-in before virtual cards, then wgpu's order (backend::choose_named_adapter); the log names every match and the one used; - no match: the default choice, with a warning that lists the cards; - a name matching only a software renderer: never used, and the warning says so; - each line is logged once (backend::log_once), though the render device, AI Denoise and the face models each ask; - tests on fake adapter lists: a_fake_adapter_list_picks_in_a_defined_order, a_choice_is_logged_once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: the release note gives the GPU's documented bound against the CPU (3 levels, mean under half a level) Review of PR #3544. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU-FULL: the window's device isn't adopted when LIGHTCRAFT_GPU_ADAPTER names another card With #3428 merged, Gpu::new already makes the render device on the named card; the window's device was still adopted first, so the app rendered on the screen's card whatever the variable said. backend::may_adopt_window_device (tested on names in backend.rs) now decides: another card named renders on a device of its own, and the window stays on the screen's card. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
540 lines
21 KiB
Rust
540 lines
21 KiB
Rust
//! Render workers: dedicated Web Workers, each running a second instance of this wasm module
|
|
//! (`worker.js` → [`worker_main`]). The main thread compiles the module once and posts the
|
|
//! compiled `WebAssembly.Module` to every worker, so a worker starts in a few milliseconds.
|
|
//!
|
|
//! This doesn't need wasm threads (shared memory, `+atomics`, a nightly `build-std`), only
|
|
//! module workers: the main thread posts a [`WireJob`] as JSON, the worker renders it and
|
|
//! transfers the RGBA bytes back (no copy across the boundary).
|
|
//!
|
|
//! Main side: [`Workers`] implements the UI's [`RenderOffload`]: one job per worker at a time,
|
|
//! with affinity (a photo goes back to the worker that already decoded it), the rendered-
|
|
//! thumbnail memory cache checked before dispatch, and the thumbnail storage index
|
|
//! ([`ThumbIndex`]) kept and pruned here. If no worker comes up, jobs run inline.
|
|
|
|
use std::cell::RefCell;
|
|
use std::collections::HashMap;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use js_sys::{Array, Object, Reflect, Uint8Array, Uint32Array};
|
|
use lightcraft_engine::catalog::PhotoId;
|
|
use lightcraft_engine::media::{RenderJob, RenderResult, SourceLevel};
|
|
use lightcraft_engine::pipeline::Rendered;
|
|
use lightcraft_preview::{Hash128, PreviewCache};
|
|
use lightcraft_raster::{Histogram, Rgba8};
|
|
use lightcraft_ui_egui::render::{RenderOffload, Slot};
|
|
use wasm_bindgen::JsCast;
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use crate::backend::Backend;
|
|
use crate::wire::{CacheWatch, ThumbIndex, WireJob, WorkerCore, WorkerSlots, thumb_storage_key};
|
|
|
|
/// Storage key of the thumbnail index.
|
|
pub const THUMB_INDEX: &str = "thumbs/index.json";
|
|
|
|
fn set(o: &Object, k: &str, v: &JsValue) {
|
|
let _ = Reflect::set(o, &k.into(), v);
|
|
}
|
|
|
|
fn get(o: &JsValue, k: &str) -> JsValue {
|
|
Reflect::get(o, &k.into()).unwrap_or(JsValue::UNDEFINED)
|
|
}
|
|
|
|
fn now() -> f64 {
|
|
let perf = get(&js_sys::global(), "performance");
|
|
let f = get(&perf, "now");
|
|
f.dyn_ref::<js_sys::Function>().and_then(|f| f.call0(&perf).ok()).and_then(|v| v.as_f64()).unwrap_or(0.0)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// worker side
|
|
|
|
thread_local! {
|
|
static CORE: RefCell<WorkerCore> = RefCell::new(WorkerCore::default());
|
|
}
|
|
|
|
/// Entry point of a render worker (called by `worker.js` after instantiating the module).
|
|
/// `store` is the backend kind the page uses ("opfs", "idb" or "memory").
|
|
#[wasm_bindgen]
|
|
pub fn worker_main(store: String) {
|
|
eframe::WebLogger::init(log::LevelFilter::Info).ok();
|
|
let scope: web_sys::DedicatedWorkerGlobalScope = js_sys::global().unchecked_into();
|
|
// The main thread sends jobs only after "ready", i.e. once storage is open, and one at a time.
|
|
wasm_bindgen_futures::spawn_local(async move {
|
|
let backend = match store.as_str() {
|
|
"memory" => None,
|
|
_ => Backend::open(store == "idb").await.map_err(|e| log::warn!("render worker: no storage ({e})")).ok(),
|
|
};
|
|
let s = scope.clone();
|
|
let onmessage = Closure::<dyn FnMut(web_sys::MessageEvent)>::new(move |e: web_sys::MessageEvent| {
|
|
let (s, b) = (s.clone(), backend.clone());
|
|
wasm_bindgen_futures::spawn_local(async move { handle(&s, b, e.data()).await });
|
|
});
|
|
scope.set_onmessage(Some(onmessage.as_ref().unchecked_ref()));
|
|
onmessage.forget();
|
|
let ready = Object::new();
|
|
set(&ready, "ready", &true.into());
|
|
let _ = scope.post_message(&ready);
|
|
});
|
|
}
|
|
|
|
/// Run one job message and post the result.
|
|
async fn handle(scope: &web_sys::DedicatedWorkerGlobalScope, backend: Option<Backend>, msg: JsValue) {
|
|
let id = get(&msg, "id");
|
|
let t0 = now();
|
|
let out = Object::new();
|
|
set(&out, "id", &id);
|
|
let job: Result<WireJob, String> =
|
|
get(&msg, "job").as_string().ok_or("no job".to_string()).and_then(|j| serde_json::from_str(&j).map_err(|e| e.to_string()));
|
|
let mut transfer = Array::new();
|
|
let result: Result<(Rendered, u32), String> = async {
|
|
let job = job?;
|
|
let mut cache_miss = false;
|
|
// cached thumbnail?
|
|
if job.thumb_cached
|
|
&& let (Some(b), Some(key)) = (&backend, job.thumb_key())
|
|
{
|
|
match b.read(&key).await.ok().flatten().and_then(|bytes| lightcraft_preview::decode_jpeg(&bytes)) {
|
|
Some(image) => {
|
|
let histogram = Histogram::of_srgb8(&image);
|
|
return Ok((Rendered { image, histogram, deep: None }, 0));
|
|
}
|
|
None => cache_miss = true,
|
|
}
|
|
}
|
|
set(&out, "cacheMiss", &cache_miss.into());
|
|
let original = match CORE.with(|c| c.borrow_mut().needs_original(&job)) {
|
|
Some(hash) => match &backend {
|
|
Some(b) => b.read(&crate::store::storage_key(&hash)).await?,
|
|
None => None,
|
|
},
|
|
None => None,
|
|
};
|
|
let rendered = CORE.with(|c| c.borrow_mut().render(&job, original.as_deref()))?;
|
|
let mut stored = 0;
|
|
if let (Some(b), Some(key)) = (&backend, job.thumb_key())
|
|
&& let Some(jpeg) = lightcraft_preview::encode_jpeg(&rendered.image)
|
|
{
|
|
match b.write(&key, &jpeg).await {
|
|
Ok(()) => stored = jpeg.len() as u32,
|
|
Err(e) => log::warn!("thumbnail cache write: {e}"),
|
|
}
|
|
}
|
|
Ok((rendered, stored))
|
|
}
|
|
.await;
|
|
match result {
|
|
Ok((r, stored)) => {
|
|
let rgba = Uint8Array::from(r.image.as_bytes().as_ref());
|
|
let h = &r.histogram;
|
|
let hist = Uint32Array::from(h.to_words().as_slice());
|
|
set(&out, "ok", &true.into());
|
|
set(&out, "w", &(r.image.width as u32).into());
|
|
set(&out, "h", &(r.image.height as u32).into());
|
|
set(&out, "rgba", &rgba);
|
|
set(&out, "hist", &hist);
|
|
set(&out, "stored", &stored.into());
|
|
transfer = Array::of2(&rgba.buffer(), &hist.buffer());
|
|
}
|
|
Err(e) => {
|
|
set(&out, "ok", &false.into());
|
|
set(&out, "err", &e.into());
|
|
}
|
|
}
|
|
set(&out, "ms", &(now() - t0).into());
|
|
if let Err(e) = scope.post_message_with_transfer(&out, &transfer) {
|
|
log::error!("render worker: post: {e:?}");
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// main side
|
|
|
|
struct Busy {
|
|
disk_key: Option<String>,
|
|
namespace: u64,
|
|
request_id: u64,
|
|
cache_generation: u64,
|
|
id: u32,
|
|
slot: Slot,
|
|
photo: PhotoId,
|
|
level: SourceLevel,
|
|
key: u64,
|
|
cache: Option<(Arc<PreviewCache>, Hash128)>,
|
|
/// A slideshow slide: the worker renders the photo, it is composed here when it comes back.
|
|
slide: Option<Arc<lightcraft_engine::slide::SlideCompose>>,
|
|
}
|
|
|
|
struct W {
|
|
worker: web_sys::Worker,
|
|
ready: bool,
|
|
dead: bool,
|
|
busy: Option<Busy>,
|
|
}
|
|
|
|
struct Inner {
|
|
/// The rendered-thumbnail cache the stored thumbnails belong to.
|
|
preview_generation: CacheWatch,
|
|
workers: Vec<W>,
|
|
done: Vec<(Slot, RenderResult, f64)>,
|
|
next_id: u32,
|
|
/// (photo, level) → worker that last decoded it.
|
|
affinity: HashMap<(PhotoId, SourceLevel), usize>,
|
|
index: ThumbIndex,
|
|
/// The session's thumbnail cache budget in bytes. Kept here because the prune happens on a
|
|
/// worker message, where the session is out of reach; the app refreshes it every frame.
|
|
budget: u64,
|
|
backend: Option<Backend>,
|
|
ctx: egui::Context,
|
|
/// Jobs finished by workers / inline (for diagnostics).
|
|
remote_done: u64,
|
|
inline_done: u64,
|
|
inline_this_frame: bool,
|
|
}
|
|
|
|
impl Inner {
|
|
fn observe_cache(&mut self, cache: &Arc<PreviewCache>, generation: u64) {
|
|
let gone = self.preview_generation.observe(cache, generation, &mut self.index);
|
|
if !gone.is_empty()
|
|
&& let Some(backend) = self.backend.clone()
|
|
{
|
|
wasm_bindgen_futures::spawn_local(async move {
|
|
for key in gone {
|
|
let _ = backend.remove(&thumb_storage_key(&key)).await;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The most workers started, whatever `?workers=` asks: more than this brings nothing to a
|
|
/// render queue and a link can't make the page spawn workers (and their copies of the module)
|
|
/// without end.
|
|
pub const MAX_WORKERS: usize = 16;
|
|
|
|
/// Worker constructions failing in a row after which the rest are not tried: a browser that
|
|
/// refused this many refuses the next ones too.
|
|
const MAX_FAILED_STARTS: usize = 3;
|
|
|
|
/// The main thread's handle on the render workers.
|
|
#[derive(Clone)]
|
|
pub struct Workers(Rc<RefCell<Inner>>);
|
|
|
|
impl Workers {
|
|
/// Start `n` workers (none: every job runs inline). `store` is the backend kind for workers.
|
|
/// `index` lists the stored thumbnails of `cache`, the session's rendered-thumbnail cache
|
|
/// now: watching it from the start, a clear before the first render request still counts.
|
|
pub fn start(
|
|
n: usize,
|
|
store: &str,
|
|
backend: Option<Backend>,
|
|
index: ThumbIndex,
|
|
cache: &Arc<PreviewCache>,
|
|
budget: u64,
|
|
ctx: egui::Context,
|
|
) -> Workers {
|
|
let w = Workers(Rc::new(RefCell::new(Inner {
|
|
preview_generation: CacheWatch::new(cache),
|
|
workers: Vec::new(),
|
|
done: Vec::new(),
|
|
next_id: 0,
|
|
affinity: HashMap::new(),
|
|
index,
|
|
budget,
|
|
backend,
|
|
ctx,
|
|
remote_done: 0,
|
|
inline_done: 0,
|
|
inline_this_frame: false,
|
|
})));
|
|
let module = get(&js_sys::global(), "lightcraftModule");
|
|
if module.is_undefined() {
|
|
log::warn!("render workers: no compiled module on the page (window.lightcraftModule); rendering on the main thread");
|
|
return w;
|
|
}
|
|
let mut slots = WorkerSlots::default();
|
|
let mut failed = 0;
|
|
for attempt in 0..n.min(MAX_WORKERS) {
|
|
if failed >= MAX_FAILED_STARTS {
|
|
log::warn!("render workers: {failed} failed to start in a row; not trying the rest");
|
|
break;
|
|
}
|
|
// The callbacks name their worker by its place in `workers`; a failed attempt takes none.
|
|
let i = slots.peek();
|
|
let opts = web_sys::WorkerOptions::new();
|
|
opts.set_type(web_sys::WorkerType::Module);
|
|
opts.set_name(&format!("lightcraft-render-{attempt}"));
|
|
let worker = match web_sys::Worker::new_with_options("./worker.js", &opts) {
|
|
Ok(x) => x,
|
|
Err(e) => {
|
|
log::warn!("render worker {attempt}: {e:?}");
|
|
failed += 1;
|
|
continue;
|
|
}
|
|
};
|
|
let me = w.clone();
|
|
let onmessage = Closure::<dyn FnMut(web_sys::MessageEvent)>::new(move |e: web_sys::MessageEvent| me.on_message(i, e.data()));
|
|
worker.set_onmessage(Some(onmessage.as_ref().unchecked_ref()));
|
|
onmessage.forget();
|
|
let me = w.clone();
|
|
let onerror = Closure::<dyn FnMut(JsValue)>::new(move |e: JsValue| me.on_error(i, e));
|
|
worker.set_onerror(Some(onerror.as_ref().unchecked_ref()));
|
|
onerror.forget();
|
|
let init = Object::new();
|
|
set(&init, "module", &module);
|
|
set(&init, "store", &store.into());
|
|
if let Err(e) = worker.post_message(&init) {
|
|
log::warn!("render worker {attempt}: {e:?}");
|
|
// It never joins the list, so it must not answer to the index of the next worker.
|
|
worker.set_onmessage(None);
|
|
worker.set_onerror(None);
|
|
worker.terminate();
|
|
failed += 1;
|
|
continue;
|
|
}
|
|
w.0.borrow_mut().workers.push(W { worker, ready: false, dead: false, busy: None });
|
|
slots.commit();
|
|
failed = 0;
|
|
}
|
|
w
|
|
}
|
|
|
|
fn on_error(&self, i: usize, e: JsValue) {
|
|
let msg = get(&e, "message").as_string().unwrap_or_else(|| format!("{e:?}"));
|
|
log::error!("render worker {i} failed: {msg}");
|
|
let mut g = self.0.borrow_mut();
|
|
if let Some(w) = g.workers.get_mut(i) {
|
|
w.dead = true;
|
|
w.worker.terminate();
|
|
if let Some(b) = w.busy.take() {
|
|
let r = RenderResult {
|
|
request_id: b.request_id,
|
|
source_key: None,
|
|
photo: b.photo,
|
|
level: b.level,
|
|
key: b.key,
|
|
rendered: Err(msg),
|
|
gpu_fallback: None,
|
|
loaded: None,
|
|
quick: None,
|
|
display: None,
|
|
frame: None,
|
|
};
|
|
g.done.push((b.slot, r, 0.0));
|
|
}
|
|
}
|
|
g.ctx.request_repaint();
|
|
}
|
|
|
|
fn on_message(&self, i: usize, data: JsValue) {
|
|
let mut g = self.0.borrow_mut();
|
|
let g = &mut *g;
|
|
g.ctx.request_repaint();
|
|
let Some(w) = g.workers.get_mut(i) else { return };
|
|
if get(&data, "ready").is_truthy() {
|
|
w.ready = true;
|
|
return;
|
|
}
|
|
let id = get(&data, "id").as_f64().unwrap_or(-1.0) as u32;
|
|
let Some(b) = w.busy.take_if(|b| b.id == id) else { return };
|
|
let ms = get(&data, "ms").as_f64().unwrap_or(0.0);
|
|
let rendered = if get(&data, "ok").is_truthy() {
|
|
let (wd, ht) = (get(&data, "w").as_f64().unwrap_or(0.0) as usize, get(&data, "h").as_f64().unwrap_or(0.0) as usize);
|
|
let rgba = Uint8Array::new(&get(&data, "rgba")).to_vec();
|
|
let hist = Uint32Array::new(&get(&data, "hist")).to_vec();
|
|
match (Rgba8::from_bytes(wd, ht, &rgba), Histogram::from_words(&hist)) {
|
|
(Some(image), Some(histogram)) => Ok(Rendered { image, histogram, deep: None }),
|
|
_ => Err("render worker: bad image".to_string()),
|
|
}
|
|
} else {
|
|
Err(get(&data, "err").as_string().unwrap_or_else(|| "render worker failed".into()))
|
|
};
|
|
if let Ok(r) = &rendered
|
|
&& let Some((cache, key)) = &b.cache
|
|
&& cache.generation() == b.cache_generation
|
|
&& b.namespace == g.index.namespace()
|
|
{
|
|
cache.put_at(b.cache_generation, *key, Arc::new(r.image.clone()));
|
|
let hex = b.disk_key.clone().unwrap_or_else(|| key.to_string());
|
|
let stored = get(&data, "stored").as_f64().unwrap_or(0.0) as u64;
|
|
if get(&data, "cacheMiss").is_truthy() {
|
|
g.index.remove(&hex);
|
|
}
|
|
if stored > 0 {
|
|
g.index.insert(&hex, stored);
|
|
// The session's own budget (`library.preferences ▸ cacheMb`), not the native
|
|
// default: the browser accepted the setting, so it has to be the one enforced
|
|
// (#245).
|
|
let gone = g.index.prune(g.budget);
|
|
if !gone.is_empty()
|
|
&& let Some(be) = g.backend.clone()
|
|
{
|
|
log::info!("thumbnail cache: pruning {} files", gone.len());
|
|
wasm_bindgen_futures::spawn_local(async move {
|
|
for k in gone {
|
|
let _ = be.remove(&thumb_storage_key(&k)).await;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
g.affinity.insert((b.photo, b.level), i);
|
|
// A cleared worker may finish writing after the initial deletion. Its old namespace
|
|
// is never reused, so removing this file cannot delete a replacement's cache entry.
|
|
if let Some((cache, _)) = &b.cache
|
|
&& (cache.generation() != b.cache_generation || b.namespace != g.index.namespace())
|
|
&& let (Some(backend), Some(key)) = (g.backend.clone(), b.disk_key.clone())
|
|
{
|
|
wasm_bindgen_futures::spawn_local(async move {
|
|
let _ = backend.remove(&thumb_storage_key(&key)).await;
|
|
});
|
|
}
|
|
g.remote_done += 1;
|
|
let rendered = match (rendered, &b.slide) {
|
|
(Ok(mut r), Some(slide)) => {
|
|
r.image = slide.compose(&r.image);
|
|
Ok(r)
|
|
}
|
|
(r, _) => r,
|
|
};
|
|
g.done.push((
|
|
b.slot,
|
|
RenderResult {
|
|
request_id: b.request_id,
|
|
source_key: None,
|
|
photo: b.photo,
|
|
level: b.level,
|
|
key: b.key,
|
|
rendered,
|
|
gpu_fallback: None,
|
|
loaded: None,
|
|
quick: None,
|
|
display: None,
|
|
frame: None,
|
|
},
|
|
ms,
|
|
));
|
|
}
|
|
|
|
/// The thumbnail index as JSON if it changed since the last call (the host saves it).
|
|
pub fn take_index_if_dirty(&self) -> Option<Vec<u8>> {
|
|
let mut g = self.0.borrow_mut();
|
|
if !g.index.dirty {
|
|
return None;
|
|
}
|
|
g.index.dirty = false;
|
|
Some(g.index.to_json())
|
|
}
|
|
|
|
/// (workers alive, workers ready, jobs done remotely, jobs done inline).
|
|
pub fn stats(&self) -> (usize, usize, u64, u64) {
|
|
let g = self.0.borrow();
|
|
let alive = g.workers.iter().filter(|w| !w.dead).count();
|
|
let ready = g.workers.iter().filter(|w| w.ready && !w.dead).count();
|
|
(alive, ready, g.remote_done, g.inline_done)
|
|
}
|
|
|
|
/// The budget the thumbnail index is pruned to. The app refreshes it from the session every
|
|
/// frame, so `library.preferences ▸ cacheMb` applies to the next stored thumbnail.
|
|
pub fn set_budget(&self, bytes: u64) {
|
|
self.0.borrow_mut().budget = bytes;
|
|
}
|
|
}
|
|
|
|
impl RenderOffload for Workers {
|
|
fn try_start(&mut self, slot: Slot, job: RenderJob) -> Option<RenderJob> {
|
|
let mut g = self.0.borrow_mut();
|
|
let g = &mut *g;
|
|
if let Some((cache, _)) = job.cache.as_ref().or(job.view_cache.as_ref()) {
|
|
g.observe_cache(cache, job.cache_generation);
|
|
}
|
|
// rendered thumbnail in memory: no worker needed
|
|
if let Some((cache, key)) = &job.cache
|
|
&& let Some(img) = cache.get_at(job.cache_generation, *key)
|
|
{
|
|
let image = Arc::unwrap_or_clone(img);
|
|
let histogram = Histogram::of_srgb8(&image);
|
|
let r = RenderResult {
|
|
request_id: job.request_id,
|
|
source_key: job.source_key,
|
|
photo: job.photo,
|
|
level: job.level,
|
|
key: job.key,
|
|
rendered: Ok(Rendered { image, histogram, deep: None }),
|
|
gpu_fallback: None,
|
|
loaded: None,
|
|
quick: None,
|
|
display: None,
|
|
frame: None,
|
|
};
|
|
g.done.push((slot, r, 0.0));
|
|
return None;
|
|
}
|
|
if g.workers.iter().all(|w| w.dead) {
|
|
// no workers: render here, one job per frame (as without workers)
|
|
if g.inline_this_frame {
|
|
return Some(job);
|
|
}
|
|
g.inline_this_frame = true;
|
|
let t0 = now();
|
|
let r = job.run();
|
|
g.inline_done += 1;
|
|
g.done.push((slot, r, now() - t0));
|
|
return None;
|
|
}
|
|
let free = |w: &W| w.ready && !w.dead && w.busy.is_none();
|
|
let pick = match g.affinity.get(&(job.photo, job.level)) {
|
|
// a decoded preview-level source, or the loupe's stage cache, is worth waiting for; a
|
|
// thumbnail isn't
|
|
Some(&i) if g.workers.get(i).is_some_and(free) => Some(i),
|
|
Some(&i) if (job.level == SourceLevel::Preview || job.stages.is_some()) && g.workers.get(i).is_some_and(|w| !w.dead) => None,
|
|
_ => g.workers.iter().position(free),
|
|
};
|
|
let Some(i) = pick else { return Some(job) };
|
|
// decided here, so the next job for this photo follows even before this one finishes
|
|
g.affinity.insert((job.photo, job.level), i);
|
|
let disk_key = job.cache.as_ref().map(|(_, key)| g.index.cache_key(*key));
|
|
let thumb_cached = disk_key.as_ref().is_some_and(|key| g.index.touch(key));
|
|
let mut wire = WireJob::from_job(&job, thumb_cached, &format!("{slot:?}"));
|
|
// The namespace also changes when a new library's cache starts again at generation zero.
|
|
wire.cache_generation = g.index.namespace();
|
|
wire.thumb = disk_key.clone();
|
|
let Ok(text) = serde_json::to_string(&wire) else { return Some(job) };
|
|
g.next_id = g.next_id.wrapping_add(1);
|
|
let id = g.next_id;
|
|
let msg = Object::new();
|
|
set(&msg, "id", &id.into());
|
|
set(&msg, "job", &text.into());
|
|
let namespace = g.index.namespace();
|
|
let w = &mut g.workers[i];
|
|
if let Err(e) = w.worker.post_message(&msg) {
|
|
log::warn!("render worker {i}: {e:?}");
|
|
return Some(job);
|
|
}
|
|
w.busy = Some(Busy {
|
|
disk_key,
|
|
namespace,
|
|
request_id: job.request_id,
|
|
cache_generation: job.cache_generation,
|
|
id,
|
|
slot,
|
|
photo: job.photo,
|
|
level: job.level,
|
|
key: job.key,
|
|
cache: job.cache,
|
|
slide: job.slide,
|
|
});
|
|
None
|
|
}
|
|
|
|
fn finished(&mut self) -> Vec<(Slot, RenderResult, f64)> {
|
|
let mut g = self.0.borrow_mut();
|
|
// Persist invalidation even when the cleared library has no visible render requests.
|
|
if let Some(cache) = g.preview_generation.cache() {
|
|
g.observe_cache(&cache, cache.generation());
|
|
}
|
|
g.inline_this_frame = false;
|
|
std::mem::take(&mut g.done)
|
|
}
|
|
}
|