Repository navigation
fix(render): #1782 follow-ups: exports wait only for the cast shadows they draw, previews and files drawn on the window's thread say what they lack #1823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quality / robustness (confidence ~60%): a test hook is now public production API, and it is process-wide.
A cfg gate is not available across crates here (the render and engine tests call it). Options: a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is hidden with |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn hold_cast_shadows() -> std::sync::RwLockWriteGuard<'static, ()> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LIGHT_HOLD.write().unwrap_or_else(|e| e.into_inner()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn start_light(r: Arc<Raster>, key: LightKey, work: Arc<LightWork>) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
967
to
979
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quality (confidence ~95%): the doc comment for The "Works out a light grid…" comment was above Suggested fix: move the
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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,16 +2507,16 @@ 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), 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"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| heard.sort_unstable(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(heard, ["joiner", "starter"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(matches!(rl.cast_shadows(true, (0, 1), || {}), CastShadows::Ready(_))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<f64> { | ||
| 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<RenderOutput> { | ||
| // 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<u32> = { | ||
| 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))) | ||
|
|
@@ -6420,10 +6427,16 @@ pub(crate) fn frame_with_shadows_to_come<T>(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 | ||
| /// 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) { | ||
| /// Waits for the cast shadows of the map's Shaded Relief layers drawn in `view` (visible, in | ||
| /// 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 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); | ||
| } | ||
|
|
@@ -6453,8 +6466,18 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option<Rendered> { | |
| // 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); | ||
| } | ||
| // 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<Rendered> { | ||
| // 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)?; | ||
|
|
@@ -6476,6 +6499,7 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option<Rendered> { | |
| idle += 1; | ||
| } | ||
| drawn_at = now; | ||
| SHADOWS_TO_COME.with(|c| c.set(false)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible bug (confidence ~35%): notices from an earlier pass survive a later pass that drew the layer.
I have not confirmed it can be reached: it needs a grid that stops or fails in pass 1 and succeeds on a retry within the same export. If that is impossible, a comment saying so would settle it. If it is possible, the notices from a pass that was drawn again should be dropped in the same way (keep only the last pass's, or keep them only when that pass still lacks the layer's shadows).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| r = render_cpu(map, view, true, record)?; | ||
| } | ||
| Some(r) | ||
|
|
@@ -6544,13 +6568,19 @@ pub fn render_placed(map: &mut Map, view: &View) -> Result<Placed, String> { | |
| 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))); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| 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( | ||
| "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(), | ||
| ); | ||
| } | ||
| 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 +8664,65 @@ 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"); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /// 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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.