From 9890bcbf86fa37725fd467999c21d1b5d44506e5 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Thu, 12 Nov 2026 00:20:53 -0300 Subject: [PATCH 1/2] fix(render): #1782 follow-ups: exports wait only for the cast shadows they draw (#1782) - wait_for_cast_shadows uses render_frame's test (visible, in scale range, not held by a group): an export no longer starts and waits for a grid it never draws. - The presentation and report previews read the tile cache's generation before they draw, so a grid landing meanwhile draws them again. - A file exported on the window's thread (Export Map while a style's view loads, a script's or an agent's export) says it was made without the shadows still being worked out; a GeoTIFF lists it among its gaps. - who_joins_work_under_way_is_told_when_it_lands asserts its precondition. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/share.rs | 7 ++-- crates/engine/src/tests.rs | 13 +++++++ crates/map/src/raster.rs | 15 ++++----- crates/render/src/lib.rs | 58 ++++++++++++++++++++++++++++---- crates/ui-egui/src/docpanes.rs | 3 +- crates/ui-egui/src/layoutview.rs | 11 +++--- crates/ui-egui/src/reportpane.rs | 3 +- 7 files changed, 86 insertions(+), 24 deletions(-) diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index e658c7184f..10254a89fe 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -222,13 +222,14 @@ pub(crate) fn saying_shadows(s: &mut Session, what: &str, draw: impl FnOnce(& } /// What frames said of their cast shadows, as warnings about `what`: layers drawn without them, -/// and, drawn on the interface's thread (which doesn't wait for them), that some were still -/// being worked out. What was drawn is written all the same. +/// and, drawn on the interface's thread (which doesn't wait for them: an image, a print, or a +/// file exported there, by a script or while a style's view loads), that some were still being +/// worked out. What was drawn is written all the same. pub(crate) fn shadow_warnings(what: &str, said: &rustgis_render::ShadowsSaid) -> Vec { let mut out: Vec = said.notices.iter().map(|e| format!("{what}: {e}")).collect(); if said.to_come { out.push(format!( - "{what}: some layers are drawn without their cast shadows, still being worked out: draw it again once the map shows them" + "{what}: made without the cast shadows still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again" )); } out diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 9e8af949e2..4c0a9383ca 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3239,6 +3239,19 @@ fn every_drawing_of_the_map_says_a_layer_drawn_without_its_cast_shadows() { "{:?}", s.notices.iter().map(|n| &n.text).collect::>() ); + // A file exported there (a script's, or while a style's view loads) says it was written + // without them, a GeoTIFF too. + let dir = std::env::temp_dir().join(format!("rustgis-cast-here-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + for (azimuth, name) in [(124, "here.png"), (125, "here.tif")] { + s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": azimuth, "shadow": 1})).unwrap(); + s.notices.clear(); + let path = dir.join(name).display().to_string(); + s.run("share.exportMap", &json!({"path": path, "width": 200, "height": 150})).unwrap(); + let texts: Vec<&String> = s.notices.iter().map(|n| &n.text).collect(); + assert!(texts.iter().any(|t| t.starts_with(&path) && t.contains("still being worked out")), "{name}: {texts:?}"); + } + std::fs::remove_dir_all(&dir).ok(); } #[test] diff --git a/crates/map/src/raster.rs b/crates/map/src/raster.rs index eccaf4c545..27ee15fe03 100644 --- a/crates/map/src/raster.rs +++ b/crates/map/src/raster.rs @@ -2498,14 +2498,13 @@ mod tests { }; let first = rl.cast_shadows(false, (0, 1), told("starter")); let joined = rl.cast_shadows(false, (5, 1), told("joiner")); - // Joined while under way (it traces far under a 3° sun), both are told; done already, the - // joiner has the grid and needs no call. - if first == CastShadows::Pending && joined == CastShadows::Pending { - let mut heard = vec![landings.recv_timeout(std::time::Duration::from_secs(120)).unwrap()]; - heard.push(landings.recv_timeout(std::time::Duration::from_secs(120)).unwrap()); - heard.sort_unstable(); - assert_eq!(heard, ["joiner", "starter"]); - } + // Joined while under way (it traces far under a 3° sun, for longer than the next call + // takes), both are told. + assert!(first == CastShadows::Pending && joined == CastShadows::Pending, "joined while under way"); + let mut heard = vec![landings.recv_timeout(std::time::Duration::from_secs(120)).unwrap()]; + heard.push(landings.recv_timeout(std::time::Duration::from_secs(120)).unwrap()); + heard.sort_unstable(); + assert_eq!(heard, ["joiner", "starter"]); assert!(matches!(rl.cast_shadows(true, (0, 1), || {}), CastShadows::Ready(_))); } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 3935b3ca2c..7fc35fd6e7 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -6420,10 +6420,13 @@ pub(crate) fn frame_with_shadows_to_come(f: impl FnOnce() -> T) -> (T, bool) (out, to_come) } -/// Waits for the cast shadows of the map's visible Shaded Relief layers, as an export does (it +/// Waits for the cast shadows of the map's Shaded Relief layers drawn in `view` (visible, in +/// their scale range and not held by a group, as `render_frame` draws them), as an export does (it /// runs off the interface's thread); the wait gives up when the run is cancelled. -fn wait_for_cast_shadows(map: &Map) { - for layer in map.layers.iter().filter(|l| l.visible) { +fn wait_for_cast_shadows(map: &Map, view: &View) { + let scale = map.ground_res_at(&view.camera) / rustgis_geom::PIXEL_M; + let held = map.held_by_group(scale); + for layer in map.layers.iter().filter(|l| l.visible && l.in_scale_range(scale) && !held.contains(&l.id)) { if let Some(rl) = &layer.raster { rl.cast_shadows(true, (map.instance_id(), layer.instance_id()), shadows_landed); } @@ -6453,7 +6456,7 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option { // Cast shadows still being worked out are waited for first, unless this draws on the // interface's thread (see `on_the_interface_thread`). if !NO_SHADOW_WAIT.with(std::cell::Cell::get) { - wait_for_cast_shadows(map); + wait_for_cast_shadows(map, view); } // What had come when it drew: what lands meanwhile is news to the wait. let mut drawn_at = rustgis_tiles::cache::generation(); @@ -6544,13 +6547,19 @@ pub fn render_placed(map: &mut Map, view: &View) -> Result { let revisions = archive_revisions([&*map]); // Layers drawn without their cast shadows are said as gaps, not as tile layers that failed; // what frames before it said is kept for `shadows_said`. - let before = take_shadow_notices(); + let (before, before_come) = (take_shadow_notices(), SHADOWS_TO_COME.with(|c| c.replace(false))); let r = render_exported(map, view); - let shadows = take_shadow_notices(); + let (shadows, to_come) = (take_shadow_notices(), SHADOWS_TO_COME.with(|c| c.replace(before_come))); SHADOW_NOTICES.with(|n| *n.borrow_mut() = before); let r = r?; let tile_errors: Vec<&String> = r.tile_errors.iter().filter(|e| !shadows.contains(e)).collect(); let mut gaps = shadows; + if to_come { + gaps.push( + "some layers' cast shadows were still being worked out, as the window doesn't wait for them, so they are drawn without them: export again once the map shows them" + .to_string(), + ); + } if r.tiles_pending { gaps.push("some tiles were still on their way when the export stopped waiting, so parts of tile layers are empty: export again once the map has drawn them".to_string()); } @@ -8634,6 +8643,43 @@ mod tests { assert_ne!(plain.pixmap.data(), shaded.pixmap.data(), "drawn with its shadows"); } + /// An export doesn't wait for, nor start, the cast shadows of a layer it doesn't draw: one + /// outside its scale range, or held by a group. + #[test] + fn an_export_waits_only_for_the_cast_shadows_it_draws() { + let camera = Camera { center: lonlat_to_merc(Coord::new(0.0, 0.0)), res: 1200.0, bearing: 0.0, pitch: 0.0 }; + let view = View { width: 200.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; + for grouped in [false, true] { + let mut m = hill_map(); + let id = m.layers.iter().find(|l| l.raster.is_some()).map(|l| l.id).unwrap(); + let l = m.layer_mut(id).unwrap(); + l.visible = true; + l.raster.as_mut().unwrap().style.renderer = rustgis_map::RasterRenderer::ShadedRelief { + band: 0, + ramp: None, + azimuth: 200.0, + altitude: 5.0, + z_factor: 1.0, + shadow: 1.0, + }; + // Drawn only zoomed in past 1:1: or in a group drawn only there. + if grouped { + let mut g = rustgis_map::LayerGroup::new("Relief"); + g.children = vec![id]; + g.min_scale = Some(1.0); + m.add_group(g); + } else { + l.min_scale = Some(1.0); + } + render_complete(&mut m, &view).unwrap(); + let rl = m.layer(id).and_then(|l| l.raster.as_ref()).unwrap(); + assert!( + matches!(rl.cast_shadows(false, (0, 0), || {}), rustgis_map::raster::CastShadows::Pending), + "grouped {grouped}: nothing was worked out for it" + ); + } + } + #[test] fn a_raster_at_the_closest_zoom_is_drawn_where_it_is() { // At the deepest zoom (#902) the raster's far corners lie billions of pixels from the diff --git a/crates/ui-egui/src/docpanes.rs b/crates/ui-egui/src/docpanes.rs index d27d62eedd..9214b90dcb 100644 --- a/crates/ui-egui/src/docpanes.rs +++ b/crates/ui-egui/src/docpanes.rs @@ -277,6 +277,7 @@ fn preview(app: &mut RustGisApp, ui: &mut Ui, i: usize, t: &Tokens) { let key = format!("{i}#{}#{}", app.docs.page, app.session.revision); if app.docs.preview.as_ref().is_none_or(|p| !crate::layoutview::preview_current(&p.0, &key)) { let (s, page) = (&mut app.session, app.docs.page); + let drawn_at = rustgis_engine::render::rustgis_tiles::cache::generation(); let (drawn, said) = rustgis_engine::render::shadows_said(|| { std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| rustgis_engine::cmd::presentation::draw_page(s, i, page, 30.0))) .unwrap_or_else(|_| Err("the preview failed unexpectedly".into())) @@ -286,7 +287,7 @@ fn preview(app: &mut RustGisApp, ui: &mut Ui, i: usize, t: &Tokens) { let img = egui::ColorImage::from_rgba_premultiplied([pm.width() as usize, pm.height() as usize], pm.data()); ui.ctx().load_texture("presentation_preview", img, egui::TextureOptions::LINEAR) }); - app.docs.preview = Some((crate::layoutview::preview_key(&key, &said), tex)); + app.docs.preview = Some((crate::layoutview::preview_key(&key, &said, drawn_at), tex)); } if app.docs.preview.as_ref().is_some_and(|p| crate::layoutview::preview_waits(&p.0)) { ui.ctx().request_repaint_after(std::time::Duration::from_millis(250)); diff --git a/crates/ui-egui/src/layoutview.rs b/crates/ui-egui/src/layoutview.rs index c045eb7691..8c0b38b459 100644 --- a/crates/ui-egui/src/layoutview.rs +++ b/crates/ui-egui/src/layoutview.rs @@ -66,11 +66,12 @@ pub(crate) fn say_frame_shadows(app: &mut RustGisApp, said: &rustgis_engine::ren } } -/// The key a preview drawn for `base` is kept under: with the tile cache's generation when its -/// frames drew without cast shadows still being worked out (on the interface's thread, which -/// doesn't wait for them), so it is drawn again as they land (see [`preview_current`]). -pub(crate) fn preview_key(base: &str, said: &rustgis_engine::render::ShadowsSaid) -> String { - if said.to_come { format!("{base}#shadows@{}", rustgis_engine::render::rustgis_tiles::cache::generation()) } else { base.to_string() } +/// The key a preview drawn for `base` is kept under: with the tile cache's generation read before +/// it was drawn (`drawn_at`) when its frames drew without cast shadows still being worked out (on +/// the interface's thread, which doesn't wait for them), so it is drawn again as they land, even +/// while it was drawn (see [`preview_current`]). +pub(crate) fn preview_key(base: &str, said: &rustgis_engine::render::ShadowsSaid, drawn_at: u64) -> String { + if said.to_come { format!("{base}#shadows@{drawn_at}") } else { base.to_string() } } /// Whether a preview kept under `kept` (see [`preview_key`]) is still the one `base` asks for: diff --git a/crates/ui-egui/src/reportpane.rs b/crates/ui-egui/src/reportpane.rs index 140a1d7c29..e95c6ec7c3 100644 --- a/crates/ui-egui/src/reportpane.rs +++ b/crates/ui-egui/src/reportpane.rs @@ -328,6 +328,7 @@ fn preview(app: &mut RustGisApp, ui: &mut Ui, i: usize, t: &Tokens) { None => return, }; let (s, page) = (&mut app.session, app.reports.page); + let drawn_at = rustgis_engine::render::rustgis_tiles::cache::generation(); let (drawn, said) = rustgis_engine::render::shadows_said(|| { std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<_, String> { let page = page.min(pages.len().saturating_sub(1)); @@ -344,7 +345,7 @@ fn preview(app: &mut RustGisApp, ui: &mut Ui, i: usize, t: &Tokens) { let img = egui::ColorImage::from_rgba_premultiplied([pm.width() as usize, pm.height() as usize], pm.data()); let tex = ui.ctx().load_texture("report_preview", img, egui::TextureOptions::LINEAR); let key = format!("{i}#{page}#{}", app.session.revision); - app.reports.preview = Some((crate::layoutview::preview_key(&key, &said), tex, total)); + app.reports.preview = Some((crate::layoutview::preview_key(&key, &said, drawn_at), tex, total)); } Err(e) => { ui.label(egui::RichText::new(e).small().color(Color32::from_rgb(0xC0, 0x30, 0x30))); From 1e5873fbeeeb4b7f1f4fd3b63af34b3220ac7b67 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Thu, 12 Nov 2026 01:13:23 -0300 Subject: [PATCH 2/2] fix(render): a tilted export waits for the cast shadows its bands draw (#1782) - wait_for_cast_shadows tests the scales render_pass draws at (the centre's and, tilted, each band's) with Map::draws_at; both share drawn_scales. - complete keeps only its last pass's word on shadows still to come: a pass drawn again with them no longer leaves a false notice. - The GeoTIFF gap says the same words as the PNG warning. - rustgis_map::raster::hold_cast_shadows (hidden test hook) holds light work before it traces; the joiner test and the engine test use it, so their Pending is certain. The engine test's folder is removed by a guard. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 22 ++++++++----- crates/map/src/raster.rs | 16 ++++++++-- crates/render/src/lib.rs | 63 ++++++++++++++++++++++++++++++++------ 3 files changed, 81 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 4c0a9383ca..eb16e429e9 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3201,9 +3201,16 @@ fn every_drawing_of_the_map_says_a_layer_drawn_without_its_cast_shadows() { r.overview = Some((800, 600)); rl.data = Some(std::sync::Arc::new(r)); let said = |s: &Session| s.notices.iter().filter(|n| n.text.contains("scene: its cast shadows aren't drawn")).count(); - let dir = std::env::temp_dir().join(format!("rustgis-cast-every-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let path = |name: &str| dir.join(name).display().to_string(); + /// Removed however the test ends. + struct Dir(std::path::PathBuf); + impl Drop for Dir { + fn drop(&mut self) { + std::fs::remove_dir_all(&self.0).ok(); + } + } + let dir = Dir(std::env::temp_dir().join(format!("rustgis-cast-every-{}", std::process::id()))); + std::fs::create_dir_all(&dir.0).unwrap(); + let path = |name: &str| dir.0.join(name).display().to_string(); let draws: Vec<(&str, Box)> = vec![ ("map image", Box::new(|s| drop(crate::cmd::share::map_png(s, Some(200.0), Some(150.0), None).unwrap()))), ("map PDF", Box::new(|s| drop(crate::cmd::share::map_pdf(s, 72.0).unwrap()))), @@ -3227,12 +3234,13 @@ fn every_drawing_of_the_map_says_a_layer_drawn_without_its_cast_shadows() { draw(&mut s); assert!(said(&s) > 0, "{what}: {:?}", s.notices.iter().map(|n| &n.text).collect::>()); } - std::fs::remove_dir_all(&dir).ok(); // On the interface's thread a new sun's shadows aren't waited for: the image says so. let mut s = with_raster(); s.run("layer.zoomTo", &json!({"layer": "scene"})).unwrap(); s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 123, "shadow": 1})).unwrap(); crate::render::on_the_interface_thread(); + // Held before they trace, the shadows are still to come whatever the computer's speed. + let hold = crate::map::raster::hold_cast_shadows(); crate::cmd::share::map_png(&mut s, Some(200.0), Some(150.0), None).unwrap(); assert!( s.notices.iter().any(|n| n.text.contains("still being worked out")), @@ -3241,17 +3249,15 @@ fn every_drawing_of_the_map_says_a_layer_drawn_without_its_cast_shadows() { ); // A file exported there (a script's, or while a style's view loads) says it was written // without them, a GeoTIFF too. - let dir = std::env::temp_dir().join(format!("rustgis-cast-here-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); for (azimuth, name) in [(124, "here.png"), (125, "here.tif")] { s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": azimuth, "shadow": 1})).unwrap(); s.notices.clear(); - let path = dir.join(name).display().to_string(); + let path = path(name); s.run("share.exportMap", &json!({"path": path, "width": 200, "height": 150})).unwrap(); let texts: Vec<&String> = s.notices.iter().map(|n| &n.text).collect(); assert!(texts.iter().any(|t| t.starts_with(&path) && t.contains("still being worked out")), "{name}: {texts:?}"); } - std::fs::remove_dir_all(&dir).ok(); + drop(hold); } #[test] diff --git a/crates/map/src/raster.rs b/crates/map/src/raster.rs index 27ee15fe03..dce60010d6 100644 --- a/crates/map/src/raster.rs +++ b/crates/map/src/raster.rs @@ -966,8 +966,19 @@ const KEPT_LIGHTS: usize = 3; /// Works out a light grid: natively on a thread of its own (here if the system refuses one), /// in the browser here. +/// Held by [`hold_cast_shadows`]: light work waits for it before it traces. +static LIGHT_HOLD: std::sync::RwLock<()> = std::sync::RwLock::new(()); + +/// Holds every light grid's work before it traces, until the guard is dropped: for tests of what +/// is drawn, and said, while cast shadows are still to come, whatever the computer's speed. +#[doc(hidden)] +pub fn hold_cast_shadows() -> std::sync::RwLockWriteGuard<'static, ()> { + LIGHT_HOLD.write().unwrap_or_else(|e| e.into_inner()) +} + fn start_light(r: Arc, key: LightKey, work: Arc) { let run = move || { + drop(LIGHT_HOLD.read().unwrap_or_else(|e| e.into_inner())); let threads = rustgis_data::threads::max(); let traced = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { crate::shadow::light(&r, key.band, &key.mask, key.azimuth, key.altitude, key.z_factor, threads, &work.stop) @@ -2496,10 +2507,11 @@ mod tests { let _ = tx.send(who); } }; + // Joined while under way (held before it traces), both are told. + let hold = hold_cast_shadows(); let first = rl.cast_shadows(false, (0, 1), told("starter")); let joined = rl.cast_shadows(false, (5, 1), told("joiner")); - // Joined while under way (it traces far under a 3° sun, for longer than the next call - // takes), both are told. + drop(hold); assert!(first == CastShadows::Pending && joined == CastShadows::Pending, "joined while under way"); let mut heard = vec![landings.recv_timeout(std::time::Duration::from_secs(120)).unwrap()]; heard.push(landings.recv_timeout(std::time::Duration::from_secs(120)).unwrap()); diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 7fc35fd6e7..654d2716fb 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -2107,6 +2107,16 @@ fn render_any(map: &mut Map, view: &View, full_res_rasters: bool, backend: Backe revisions.current([&*map]).then_some(output).flatten() } +/// The scales a frame of `view` from `camera` (normalised) draws layers at: its centre's, and each +/// tilt band's when it is tilted (not on a globe). A layer drawn at any of them is drawn. +fn drawn_scales(map: &Map, camera: &Camera, view: &View) -> Vec { + let mut scales = vec![map.ground_res_at(camera) / rustgis_geom::PIXEL_M]; + if camera.pitch != 0.0 && !map.projection.is_globe() { + scales.extend(camera.tilt_bands(view.width, view.height).iter().map(|b| map.ground_res_at(&b.camera) / rustgis_geom::PIXEL_M)); + } + scales +} + /// One entire pass, including all bands and backend fallbacks, under `render_any`'s guard. fn render_pass(map: &mut Map, view: &View, full_res_rasters: bool, backend: Backend, record: bool) -> Option { // A bearing or pitch set directly on the camera may be unnormalised (or no number, which @@ -2117,10 +2127,7 @@ fn render_pass(map: &mut Map, view: &View, full_res_rasters: bool, backend: Back // A persisted Shapefile has no chunk handle until it opens. Preflight required local // sources in every view so an open failure cannot become a successful incomplete export. let complete_layers: std::collections::HashSet = { - let mut scales = vec![map.ground_res_at(&camera) / rustgis_geom::PIXEL_M]; - if camera.pitch != 0.0 && !map.projection.is_globe() { - scales.extend(camera.tilt_bands(view.width, view.height).iter().map(|b| map.ground_res_at(&b.camera) / rustgis_geom::PIXEL_M)); - } + let scales = drawn_scales(map, &camera, view); map.layers .iter() .filter(|l| scales.iter().any(|scale| map.draws_at(l, *scale))) @@ -6421,12 +6428,15 @@ pub(crate) fn frame_with_shadows_to_come(f: impl FnOnce() -> T) -> (T, bool) } /// Waits for the cast shadows of the map's Shaded Relief layers drawn in `view` (visible, in -/// their scale range and not held by a group, as `render_frame` draws them), as an export does (it -/// runs off the interface's thread); the wait gives up when the run is cancelled. +/// their and their groups' scale range at the view's centre or, tilted, at one of its bands, as +/// `render_pass` draws them), as an export does (it runs off the interface's thread); the wait +/// gives up when the run is cancelled. fn wait_for_cast_shadows(map: &Map, view: &View) { - let scale = map.ground_res_at(&view.camera) / rustgis_geom::PIXEL_M; - let held = map.held_by_group(scale); - for layer in map.layers.iter().filter(|l| l.visible && l.in_scale_range(scale) && !held.contains(&l.id)) { + let mut camera = view.camera; + camera.bearing = rustgis_map::camera::normalize_bearing(camera.bearing); + camera.pitch = rustgis_map::camera::normalize_pitch(camera.pitch); + let scales = drawn_scales(map, &camera, view); + for layer in map.layers.iter().filter(|l| scales.iter().any(|s| map.draws_at(l, *s))) { if let Some(rl) = &layer.raster { rl.cast_shadows(true, (map.instance_id(), layer.instance_id()), shadows_landed); } @@ -6458,6 +6468,16 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option { if !NO_SHADOW_WAIT.with(std::cell::Cell::get) { wait_for_cast_shadows(map, view); } + // Whether cast shadows were still to come is the last pass's to say (an earlier one's may + // have been drawn again with them); what frames before it said is kept (see `shadows_said`). + let before = SHADOWS_TO_COME.with(|c| c.replace(false)); + let r = complete_passes(map, view, record); + SHADOWS_TO_COME.with(|c| c.set(before || c.get())); + r +} + +/// [`complete`]'s passes, each saying afresh whether cast shadows were still to come. +fn complete_passes(map: &mut Map, view: &View, record: bool) -> Option { // What had come when it drew: what lands meanwhile is news to the wait. let mut drawn_at = rustgis_tiles::cache::generation(); let mut r = render_cpu(map, view, true, record)?; @@ -6479,6 +6499,7 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option { idle += 1; } drawn_at = now; + SHADOWS_TO_COME.with(|c| c.set(false)); r = render_cpu(map, view, true, record)?; } Some(r) @@ -6556,7 +6577,7 @@ pub fn render_placed(map: &mut Map, view: &View) -> Result { let mut gaps = shadows; if to_come { gaps.push( - "some layers' cast shadows were still being worked out, as the window doesn't wait for them, so they are drawn without them: export again once the map shows them" + "made without the cast shadows still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again" .to_string(), ); } @@ -8643,6 +8664,28 @@ mod tests { assert_ne!(plain.pixmap.data(), shaded.pixmap.data(), "drawn with its shadows"); } + /// A tilted export waits for the cast shadows of a layer drawn only in one of its far bands, + /// outside its scale range at the view's centre. + #[test] + fn a_tilted_export_waits_for_the_cast_shadows_its_bands_draw() { + let mut m = hill_map(); + let id = m.layers.iter().find(|l| l.raster.is_some()).map(|l| l.id).unwrap(); + let camera = Camera { center: lonlat_to_merc(Coord::new(0.0, 0.0)), res: 1200.0, bearing: 0.0, pitch: 60.0 }; + let view = View { width: 200.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; + let scales = drawn_scales(&m, &camera, &view); + let (centre, farthest) = (scales[0], scales.iter().copied().fold(0.0, f64::max)); + assert!(farthest > centre * 1.01, "a far band is drawn at a smaller scale: {scales:?}"); + let l = m.layer_mut(id).unwrap(); + l.visible = true; + l.raster.as_mut().unwrap().style.renderer = + rustgis_map::RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 210.0, altitude: 5.0, z_factor: 1.0, shadow: 1.0 }; + // Drawn only zoomed out past a scale between the centre's and the far band's. + l.max_scale = Some((centre + farthest) / 2.0); + render_complete(&mut m, &view).unwrap(); + let rl = m.layer(id).and_then(|l| l.raster.as_ref()).unwrap(); + assert!(matches!(rl.cast_shadows(false, (0, 0), || {}), rustgis_map::raster::CastShadows::Ready(_)), "waited for"); + } + /// An export doesn't wait for, nor start, the cast shadows of a layer it doesn't draw: one /// outside its scale range, or held by a group. #[test]