Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/map/src/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,14 @@ impl RasterLayer {
matches!(self.style.renderer, RasterRenderer::ShadedRelief { shadow, .. } if shadow > 0.0)
}

/// How many wait for its cast shadows now (exports), its copies' too: for tests of what an
/// export waits for.
#[doc(hidden)]
pub fn cast_shadow_waiters(&self) -> usize {
let Ok(jobs) = self.cast.0.lock() else { return 0 };
jobs.iter().map(|j| j.work.waiting.load(std::sync::atomic::Ordering::SeqCst)).sum()
}

/// The light its cast shadows leave on each cell (see [`crate::shadow::light`]), when it is
/// drawn as Shaded Relief with shadows. Worked out once per pixels, band, sun, Z factor and
/// mask and kept: each owner's latest, and a few of suns left, for going back.
Expand Down Expand Up @@ -964,8 +972,6 @@ const STOPPED: &str = "its cast shadows were stopped";
/// each owner's latest.
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(());

Expand All @@ -976,6 +982,8 @@ pub fn hold_cast_shadows() -> std::sync::RwLockWriteGuard<'static, ()> {
LIGHT_HOLD.write().unwrap_or_else(|e| e.into_inner())
}

/// Works out a light grid: natively on a thread of its own (here if the system refuses one),
/// in the browser here.
fn start_light(r: Arc<Raster>, key: LightKey, work: Arc<LightWork>) {
let run = move || {
drop(LIGHT_HOLD.read().unwrap_or_else(|e| e.into_inner()));
Expand Down
39 changes: 32 additions & 7 deletions crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6468,15 +6468,23 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option<Rendered> {
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));
// What it says of cast shadows (still to come, layers drawn without them) 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)), take_shadow_notices());
let r = complete_passes(map, view, record);
SHADOWS_TO_COME.with(|c| c.set(before || c.get()));
SHADOWS_TO_COME.with(|c| c.set(before.0 || c.get()));
let mut notices = before.1;
for e in take_shadow_notices() {
if !notices.contains(&e) {
notices.push(e);
}
}
SHADOW_NOTICES.with(|n| *n.borrow_mut() = notices);
r
}

/// [`complete`]'s passes, each saying afresh whether cast shadows were still to come.
/// [`complete`]'s passes, each saying afresh what it drew of cast shadows.
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();
Expand All @@ -6500,6 +6508,7 @@ fn complete_passes(map: &mut Map, view: &View, record: bool) -> Option<Rendered>
}
drawn_at = now;
SHADOWS_TO_COME.with(|c| c.set(false));
take_shadow_notices();
r = render_cpu(map, view, true, record)?;
}
Some(r)
Expand Down Expand Up @@ -8681,9 +8690,25 @@ mod tests {
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();
// Its work held before it traces: an export that waits for it can't end until it is let
// go, and says it waits (a copy of the layer shares its grids); one that doesn't ends.
let probe = m.layer(id).and_then(|l| l.raster.clone()).unwrap();
let hold = rustgis_map::raster::hold_cast_shadows();
let export = std::thread::spawn(move || {
render_complete(&mut m, &view).unwrap();
m
});
let start = std::time::Instant::now();
while probe.cast_shadow_waiters() == 0 && !export.is_finished() {
assert!(start.elapsed() < std::time::Duration::from_secs(30), "the export neither waited for the grid nor ended");
std::thread::sleep(std::time::Duration::from_millis(1));
}
Comment on lines +8702 to +8705

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The polling loop has no bound, so a regression can hang the test instead of failing it (confidence: medium). The loop exits only when the export has a waiter registered or has finished. If a future change makes the export block on some path that doesn't go through Waiting::on while the hold is held, neither condition ever becomes true. The test then spins forever and CI times out rather than reporting the failure.

A deadline doesn't weaken the check. waited is still read from the count after the loop, so a timeout with no waiter still fails the assertion.

Suggested change
while probe.cast_shadow_waiters() == 0 && !export.is_finished() {
std::thread::sleep(std::time::Duration::from_millis(1));
}
let started = std::time::Instant::now();
while probe.cast_shadow_waiters() == 0 && !export.is_finished() && started.elapsed() < std::time::Duration::from_secs(60) {
std::thread::sleep(std::time::Duration::from_millis(1));
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7c84165: the loop now fails after 30 s, saying the export neither waited for the grid nor ended.

let waited = probe.cast_shadow_waiters() > 0;
drop(hold);
let m = export.join().unwrap();
assert!(waited, "the export waited for the far band's cast shadows");
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");
assert!(matches!(rl.cast_shadows(false, (0, 0), || {}), rustgis_map::raster::CastShadows::Ready(_)), "and drew them");
}

/// An export doesn't wait for, nor start, the cast shadows of a layer it doesn't draw: one
Expand Down
Loading