Skip to content

Commit ade2cf9

Browse files
radialmonsterclaude
andcommitted
LR-UI-ZOOM: Fit and Fill keep smooth magnification; nearest only past 100% (storytold#2645)
Share the native-photo filter choice with each view's explicit zoom mode. Refresh retained samplers with one unchanged texel instead of uploading the whole image, and keep automatic Fit fallbacks from magnifying a nearest Main. Cover Fit/200% transitions, sampler deltas, CPU copies and fallback reuse in the native-small-photo regression. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014ti8KemZbjE2ZgHgV1q1BM
1 parent 8dc583c commit ade2cf9

10 files changed

Lines changed: 169 additions & 12 deletions

File tree

‎crates/ui-egui/examples/thumb_request_bench.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ fn micro(points: usize, pending: bool) {
123123
ms: 0.0,
124124
quick: None,
125125
full_res: false,
126+
first_pixel: Some(egui::Color32::GRAY),
126127
pixels: None,
127128
},
128129
);

‎crates/ui-egui/src/panels/compare.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ fn request_tile_renders(app: &mut LightcraftApp, id: PhotoId, slot: Slot, want:
208208
}
209209
}
210210

211+
/// Survey always fits; interactive panes use the mode after this frame's zoom gesture.
212+
fn tile_texture_zoom(app: &mut LightcraftApp, slot: Slot, zoom: Zoom) -> Zoom {
213+
let zoom = if matches!(app.ui.view, ViewMode::Compare | ViewMode::Reference) { app.ui.zoom } else { zoom };
214+
app.renderer.set_texture_zoom(slot, zoom);
215+
if let Slot::Compare(i) = slot {
216+
app.renderer.set_texture_zoom(Slot::CompareRegion(i), zoom);
217+
}
218+
zoom
219+
}
220+
211221
/// Draw one photo fitted into `area` (rendered at its display size into `slot`), with its caption
212222
/// strip below. Returns the image rect and the click/drag response.
213223
fn photo_tile(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, slot: Slot, area: Rect, label: &str, zoom: Zoom) -> (Rect, egui::Response) {
@@ -238,6 +248,7 @@ fn photo_tile(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, slot: Slo
238248
super::detail::pan_image(app, img_area, img, resp.drag_delta());
239249
}
240250
}
251+
let zoom = tile_texture_zoom(app, slot, zoom);
241252
let texture_side = super::detail::texture_side(ui.ctx());
242253
let want =
243254
(img.width().max(img.height()).min(img_area.width().max(img_area.height()) * 4.0) * ppp).min(2560.0_f32.min(texture_side as f32)) as usize;

‎crates/ui-egui/src/panels/detail.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
269269
Vec::new()
270270
};
271271
let windows = request_windows(app, &window_ctx, window_views);
272+
for slot in [Slot::Main, Slot::Before, Slot::Region, Slot::RegionBefore, Slot::Hover] {
273+
app.renderer.set_texture_zoom(slot, app.ui.zoom);
274+
}
272275
let p = ui.painter_at(canvas);
273276
let draw = |slot: Slot, r: Rect, clip: Rect| draw_slot(app, &p, id, ppp, slot, r, clip);
274277
// soft proofing: a paper-white surround and the proof's name, as Lightroom shows it

‎crates/ui-egui/src/panels/detail/filmstrip.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ pub(super) fn navigator(app: &mut LightcraftApp, ui: &mut egui::Ui, canvas: Rect
1010
if !app.ui.navigator || !zoomed || app.ui.fullscreen {
1111
return None;
1212
}
13-
let tex = app.renderer.thumb(id).or_else(|| app.renderer.textures.get(&Slot::Main).filter(|t| t.photo == id));
1413
let aspect = img.width() / img.height().max(1.0);
1514
let (w, h) = if aspect >= 1.0 { (NAV_W, NAV_W / aspect) } else { (NAV_W * aspect, NAV_W) };
15+
let ppp = ui.ctx().pixels_per_point();
16+
let tex = app.renderer.thumb(id).or_else(|| app.renderer.main_for_fit(id, [w * ppp, h * ppp]));
1617
let frame = Rect::from_min_size(pos2(canvas.right() - w - 16.0, canvas.bottom() - h - 16.0), vec2(w, h));
1718
let p = ui.painter_at(canvas);
1819
p.rect_filled(frame.expand(5.0), 4.0, Color32::from_black_alpha(190));

‎crates/ui-egui/src/panels/second.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ fn body(app: &mut LightcraftApp, ui: &mut egui::Ui) {
5353
app.renderer.request(Slot::Second, job, 95);
5454
}
5555
let mine = |s: Slot| app.renderer.textures.get(&s).filter(|t| t.photo == id);
56-
let tex = mine(Slot::Second).or_else(|| mine(Slot::Main)).or_else(|| app.renderer.thumb(id));
56+
// Main can be explicitly zoomed while this automatic Fit view is open. Do not borrow
57+
// its nearest sampler: use the smooth thumbnail until this window's render is ready.
58+
let tex =
59+
mine(Slot::Second).or_else(|| app.renderer.main_for_fit(id, [area.width() * ppp, area.height() * ppp])).or_else(|| app.renderer.thumb(id));
5760
if let Some(t) = tex {
5861
let [tw, th] = t.tex.size();
5962
let s = (area.width() / tw as f32).min(area.height() / th as f32);

‎crates/ui-egui/src/panels/slideshow.rs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ fn wanted(play: &Playback, now: f64) -> Vec<(Item, u32)> {
251251
/// The texture to draw for slide `item`: its own render, else (a photo) the loupe's or a
252252
/// thumbnail (a slide stepped to with ←/→ shows at once, sharp when its render lands). A title
253253
/// screen has nothing to stand in for it: the black canvas shows until it lands.
254-
fn texture(app: &LightcraftApp, item: Item) -> Option<&crate::render::Tex> {
254+
fn texture(app: &LightcraftApp, item: Item, physical_bounds: [f32; 2]) -> Option<&crate::render::Tex> {
255255
let Item::Photo(id) = item else { return app.renderer.textures.get(&Slot::Slide(item)) };
256256
app.renderer
257257
.textures
258258
.get(&Slot::Slide(item))
259259
.filter(|t| t.photo == id)
260-
.or_else(|| app.renderer.textures.get(&Slot::Main).filter(|t| t.photo == id))
260+
.or_else(|| app.renderer.main_for_fit(id, physical_bounds))
261261
.or_else(|| app.renderer.thumb(id))
262262
}
263263

@@ -324,9 +324,10 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
324324
app.renderer.release_slides(&keep);
325325
let p = ui.painter_at(canvas);
326326
p.rect_filled(canvas, 0.0, Color32::BLACK);
327+
let physical_bounds = [canvas.width() * ui.ctx().pixels_per_point(), canvas.height() * ui.ctx().pixels_per_point()];
327328
let rect_of = |t: &crate::render::Tex| super::detail::fit_texture_rect(canvas, t.size);
328329
let draw = |id: Item, alpha: f32| -> Option<Rect> {
329-
let t = texture(app, id)?;
330+
let t = texture(app, id, physical_bounds)?;
330331
let r = rect_of(t);
331332
p.image(t.tex.id(), r, full_uv(), Color32::WHITE.gamma_multiply(alpha.clamp(0.0, 1.0)));
332333
Some(r)
@@ -347,8 +348,8 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
347348
// crossfade: where the slides overlap the new one goes over the old at `k`; where only
348349
// the old one is, it fades to the backdrop
349350
(Some(from), None) => {
350-
let new_rect = texture(app, frame.current).map(rect_of);
351-
if let Some(t) = texture(app, from) {
351+
let new_rect = texture(app, frame.current, physical_bounds).map(rect_of);
352+
if let Some(t) = texture(app, from, physical_bounds) {
352353
let r = rect_of(t);
353354
match new_rect {
354355
Some(n) => {

‎crates/ui-egui/src/render.rs‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ pub struct Tex {
9393
pub quick: Option<QuickSource>,
9494
/// These pixels are a native-resolution photo render, not a stand-in or a reduced view.
9595
pub full_res: bool,
96+
/// One unchanged texel lets a sampler change avoid uploading the whole image.
97+
pub first_pixel: Option<egui::Color32>,
9698
/// CPU copy of the pixels (only with [`Renderer::keep_pixels`]; for headless screenshots).
9799
pub pixels: Option<std::sync::Arc<egui::ColorImage>>,
98100
}
@@ -290,6 +292,8 @@ pub struct Renderer {
290292
/// Slot → (key, priority) of the request in flight (queued or running).
291293
pending: HashMap<Slot, (u64, u32)>,
292294
pub textures: HashMap<Slot, Tex>,
295+
/// Each view owns its zoom; automatic Fit/Fill views default to linear.
296+
texture_zoom: HashMap<Slot, crate::state::Zoom>,
293297
/// The last [`Slot::HdrHistogram`] result: (photo, job key, histogram).
294298
pub live_hdr_histogram: Option<(PhotoId, u64, Histogram)>,
295299
pub last_main_ms: f64,
@@ -340,8 +344,10 @@ const IDLE_TRIM: std::time::Duration = std::time::Duration::from_secs(3);
340344
impl Slot {
341345
/// Only native photo-view renders expose original pixels. Thumbnails and stand-ins stay
342346
/// linear even when a small original fits their cache entry without losing any pixels.
343-
fn texture_options(self, full_res: bool) -> egui::TextureOptions {
347+
/// Fit and Fill also stay smooth: only explicit zoom past 100% exposes the pixel grid.
348+
fn texture_options(self, full_res: bool, zoom: crate::state::Zoom) -> egui::TextureOptions {
344349
if full_res
350+
&& matches!(zoom, crate::state::Zoom::Percent(p) if p > 100.0)
345351
&& matches!(
346352
self,
347353
Self::Main
@@ -397,6 +403,7 @@ impl Renderer {
397403
seq: 0,
398404
pending: HashMap::new(),
399405
textures: HashMap::new(),
406+
texture_zoom: HashMap::new(),
400407
live_hdr_histogram: None,
401408
last_main_ms: 0.0,
402409
completed: 0,
@@ -422,6 +429,37 @@ impl Renderer {
422429
}
423430

424431
impl Renderer {
432+
/// The same choice drives uploads, retained samplers and software copies.
433+
fn texture_options(&self, slot: Slot, full_res: bool) -> egui::TextureOptions {
434+
slot.texture_options(full_res, self.texture_zoom.get(&slot).copied().unwrap_or_default())
435+
}
436+
437+
/// Automatic Fit views can borrow Main while their own render is pending, but must not
438+
/// enlarge its explicit-zoom nearest sampler. Shrinking it is still linear and safe.
439+
pub fn main_for_fit(&self, photo: PhotoId, physical_bounds: [f32; 2]) -> Option<&Tex> {
440+
self.textures.get(&Slot::Main).filter(|t| {
441+
t.photo == photo
442+
&& (self.texture_options(Slot::Main, t.full_res) == egui::TextureOptions::LINEAR
443+
|| physical_bounds[0] <= t.size[0] as f32
444+
|| physical_bounds[1] <= t.size[1] as f32)
445+
})
446+
}
447+
448+
/// Refresh a view's sampler even while navigation keeps its existing render (SizeHold).
449+
/// egui has no sampler-only delta. Rewriting one unchanged texel updates its sampler
450+
/// without retaining or re-uploading a potentially large CPU image.
451+
pub fn set_texture_zoom(&mut self, slot: Slot, zoom: crate::state::Zoom) {
452+
let old = self.texture_zoom.insert(slot, zoom).unwrap_or_default();
453+
if let Some(t) = self.textures.get_mut(&slot) {
454+
let options = slot.texture_options(t.full_res, zoom);
455+
if options != slot.texture_options(t.full_res, old)
456+
&& let Some(pixel) = t.first_pixel
457+
{
458+
t.tex.set_partial([0, 0], egui::ColorImage::filled([1, 1], pixel), options);
459+
}
460+
}
461+
}
462+
425463
/// Would asking for this thumbnail again (at `priority`) change nothing?
426464
pub fn thumb_current(&self, photo: &Arc<lightcraft_catalog::Photo>, bucket: usize, priority: u32) -> bool {
427465
let Some(&(ref old, size, key, quick, _)) = self.thumb_inputs.get(&photo.id) else { return false };
@@ -1133,11 +1171,13 @@ impl Renderer {
11331171
let color = color.unwrap_or_else(|| Arc::new(color_image(img)));
11341172
let pixels = self.keep_pixels.then(|| color.clone());
11351173
let name = format!("{slot:?}");
1136-
let options = slot.texture_options(full_res);
1174+
let options = self.texture_options(slot, full_res);
1175+
let first_pixel = color.pixels.first().copied();
11371176
match self.textures.get_mut(&slot) {
11381177
Some(t) => {
11391178
t.tex.set(color, options);
11401179
t.full_res = full_res;
1180+
t.first_pixel = first_pixel;
11411181
t.pixels = pixels;
11421182
t.look = look;
11431183
t.key = r.key;
@@ -1161,6 +1201,7 @@ impl Renderer {
11611201
ms,
11621202
quick: r.quick,
11631203
full_res,
1204+
first_pixel,
11641205
pixels,
11651206
},
11661207
);
@@ -1236,7 +1277,7 @@ impl Renderer {
12361277
self.textures
12371278
.iter()
12381279
.filter_map(|(slot, t)| {
1239-
let options = slot.texture_options(t.full_res);
1280+
let options = self.texture_options(*slot, t.full_res);
12401281
Some((
12411282
t.tex.id(),
12421283
crate::softpaint::CpuTexture {
@@ -1320,7 +1361,19 @@ mod stage_budget_tests {
13201361
let tex = ctx.load_texture("window", egui::ColorImage::new([1, 1], vec![egui::Color32::BLACK]), Default::default());
13211362
r.textures.insert(
13221363
Slot::Region,
1323-
Tex { look: None, key: 1, photo: PhotoId(1), tex, size: [1, 1], histogram: None, ms: 0.0, quick: None, full_res: false, pixels: None },
1364+
Tex {
1365+
look: None,
1366+
key: 1,
1367+
photo: PhotoId(1),
1368+
tex,
1369+
size: [1, 1],
1370+
histogram: None,
1371+
ms: 0.0,
1372+
quick: None,
1373+
full_res: false,
1374+
first_pixel: None,
1375+
pixels: None,
1376+
},
13241377
);
13251378
let long_ago = std::time::Instant::now().checked_sub(IDLE_TRIM * 2).unwrap();
13261379
r.idle = Some((long_ago, false));

‎crates/ui-egui/src/render/thumbnail_tests.rs‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ fn larger_texture_for_another_key_does_not_count_as_current() {
206206
ms: 0.0,
207207
quick: None,
208208
full_res: false,
209+
first_pixel: None,
209210
pixels: None,
210211
};
211212
let mut r = Renderer::default();
@@ -1271,6 +1272,7 @@ fn only_native_photo_pixels_use_nearest_magnification() {
12711272
job.request.window = window;
12721273
job.display_fit = display_fit;
12731274
job.key = lightcraft_preview::next_tick();
1275+
app.renderer.set_texture_zoom(slot, crate::state::Zoom::Percent(200.0));
12741276
app.renderer.request(slot, job, 100);
12751277
let (_, job) = take_job(&work);
12761278
let mut result = job.run();
@@ -1326,3 +1328,72 @@ fn thumbnail_slots_stay_linear_for_native_small_originals() {
13261328
}
13271329
}
13281330
}
1331+
1332+
#[test]
1333+
fn native_small_main_stays_linear_in_fit() {
1334+
let (mut app, work, ctx) = fixture();
1335+
let mut job = app.session.render_job(PhotoId(1), 512, 512, false, true).unwrap();
1336+
job.source = lightcraft_engine::media::SourceRef::Loaded(Box::new(lightcraft_engine::media::DecodedSource::new(
1337+
Arc::new(lightcraft_raster::Rgb32f::new(512, 512)),
1338+
Some(Default::default()),
1339+
)));
1340+
job.level = lightcraft_engine::SourceLevel::Full;
1341+
job.info = lightcraft_pipeline::SourceInfo { native_long: Some(512), ..Default::default() };
1342+
app.renderer.request(Slot::Main, job, 100);
1343+
let (_, job) = take_job(&work);
1344+
finish(&mut app, &work, &ctx, Slot::Main, job.run());
1345+
let t = &app.renderer.textures[&Slot::Main];
1346+
assert!(t.full_res);
1347+
let mut delta = ctx.tex_manager().write().take_delta();
1348+
let options = delta.set[&t.tex.id()].last().unwrap().options;
1349+
delta.clear();
1350+
assert_eq!(options, egui::TextureOptions::LINEAR, "native small Main in Fit");
1351+
let id = t.tex.id();
1352+
let key = t.key;
1353+
let pixels = t.pixels.clone().unwrap();
1354+
assert_eq!(app.renderer.cpu_textures()[&id].magnification, egui::TextureFilter::Linear);
1355+
// No render finishes during these switches: exercise the retained SizeHold texture.
1356+
for (zoom, filter, changed) in [
1357+
(crate::state::Zoom::Percent(200.0), egui::TextureFilter::Nearest, true),
1358+
(crate::state::Zoom::Percent(400.0), egui::TextureFilter::Nearest, false),
1359+
(crate::state::Zoom::Fit, egui::TextureFilter::Linear, true),
1360+
(crate::state::Zoom::Fill, egui::TextureFilter::Linear, false),
1361+
(crate::state::Zoom::Percent(100.0), egui::TextureFilter::Linear, false),
1362+
(crate::state::Zoom::Percent(200.0), egui::TextureFilter::Nearest, true),
1363+
(crate::state::Zoom::Percent(50.0), egui::TextureFilter::Linear, true),
1364+
] {
1365+
app.renderer.set_texture_zoom(Slot::Main, zoom);
1366+
let t = &app.renderer.textures[&Slot::Main];
1367+
assert_eq!(t.tex.id(), id);
1368+
assert_eq!(t.tex.size(), [512, 512]);
1369+
assert_eq!(t.key, key);
1370+
assert!(Arc::ptr_eq(t.pixels.as_ref().unwrap(), &pixels));
1371+
let mut delta = ctx.tex_manager().write().take_delta();
1372+
if changed {
1373+
let updates = &delta.set[&id];
1374+
assert_eq!(updates.len(), 1);
1375+
let update = &updates[0];
1376+
assert_eq!(update.pos, Some([0, 0]));
1377+
assert_eq!(update.image.size(), [1, 1], "never re-upload the whole texture");
1378+
assert_eq!(update.options.magnification, filter);
1379+
assert_eq!(update.options.minification, egui::TextureFilter::Linear);
1380+
let egui::ImageData::Color(image) = &update.image;
1381+
assert_eq!(image.pixels, vec![pixels.pixels[0]], "rewrite the same texel");
1382+
} else {
1383+
assert!(delta.set.is_empty(), "unchanged filtering must not upload anything");
1384+
}
1385+
delta.clear();
1386+
let cpu = app.renderer.cpu_textures();
1387+
assert!(app.renderer.main_for_fit(PhotoId(1), [256.0, 256.0]).is_some(), "minifying Main is safe in either zoom mode");
1388+
assert_eq!(app.renderer.main_for_fit(PhotoId(1), [768.0, 768.0]).is_some(), filter == egui::TextureFilter::Linear);
1389+
assert!(app.renderer.main_for_fit(PhotoId(2), [256.0, 256.0]).is_none());
1390+
assert_eq!(cpu[&id].magnification, filter);
1391+
assert_eq!(cpu[&id].minification, egui::TextureFilter::Linear);
1392+
}
1393+
// The normal GPU path does not retain the full CPU image.
1394+
app.renderer.textures.get_mut(&Slot::Main).unwrap().pixels = None;
1395+
app.renderer.set_texture_zoom(Slot::Main, crate::state::Zoom::Percent(200.0));
1396+
let mut delta = ctx.tex_manager().write().take_delta();
1397+
assert_eq!(delta.set[&id].last().unwrap().options.magnification, egui::TextureFilter::Nearest);
1398+
delta.clear();
1399+
}

‎crates/ui-egui/src/tests/lifecycle_037.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,19 @@ fn hidden_slideshow_pauses_and_keeps_its_remaining_time() {
609609
let tex = ctx.load_texture(format!("slide-{}", id.0), egui::ColorImage::filled([2, 2], egui::Color32::WHITE), Default::default());
610610
app.renderer.textures.insert(
611611
Slot::Slide(Item::Photo(id)),
612-
Tex { look: None, key: 0, photo: id, tex, size: [2, 2], histogram: None, ms: 0.0, quick: None, full_res: false, pixels: None },
612+
Tex {
613+
look: None,
614+
key: 0,
615+
photo: id,
616+
tex,
617+
size: [2, 2],
618+
histogram: None,
619+
ms: 0.0,
620+
quick: None,
621+
full_res: false,
622+
first_pixel: None,
623+
pixels: None,
624+
},
613625
);
614626
}
615627
let first = app.ui.slideshow.as_ref().unwrap().play.current();

‎crates/ui-egui/src/tests/zoom.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ fn a_pending_crop_or_rotation_does_not_reuse_the_old_textures_aspect() {
8080
ms: 0.0,
8181
quick: None,
8282
full_res: false,
83+
first_pixel: None,
8384
pixels: None,
8485
},
8586
);

0 commit comments

Comments
 (0)