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
111 changes: 95 additions & 16 deletions crates/render/src/tile_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub struct TileMesh {
/// Made while the sprite was on its way, without the pattern fills: made again once it has
/// arrived.
pub waiting: bool,
/// The ids of the style's sprites its pattern fills were made from, if it has any.
pub sheets: Option<Vec<String>>,
}

/// The indices of one or more style layers showing at the same zooms.
Expand Down Expand Up @@ -253,7 +255,7 @@ pub fn tessellate(tile: &Tile, runs: &[Run], z: u8) -> TileMesh {
_ => mesh_runs.push(MeshRun { first, count, min_zoom: run.min_zoom, max_zoom: run.max_zoom }),
}
}
TileMesh { vertices: out.vertices, indices: out.indices, runs: mesh_runs, waiting: false }
TileMesh { vertices: out.vertices, indices: out.indices, runs: mesh_runs, waiting: false, sheets: None }
}

/// The most points of a ring fanned rather than given to lyon: the walk round it stays cheap,
Expand Down Expand Up @@ -350,6 +352,16 @@ struct Entry {
/// Made while the sprite was on its way: made again once it has arrived (or failed), not
/// before.
waiting: bool,
/// The ids of the style's sprites its pattern fills were made from, if it has any.
sheets: Option<Vec<String>>,
}

/// Whether a mesh made from the sprites `sheets`, `waiting` for them or not, still serves while
/// `pending` of them are on their way and `read` have been read. Other sprites read (one that
/// failed read after all, or another failing instead) make those with pattern fills again, once;
/// until then they stand, however long one keeps failing.
fn still_serves(waiting: bool, sheets: Option<&[String]>, pending: bool, read: &[String]) -> bool {
pending || (!waiting && sheets.is_none_or(|ids| ids == read))
}

#[derive(Default)]
Expand All @@ -368,18 +380,28 @@ fn lock() -> MutexGuard<'static, Store> {
/// (while its tile and the style are, and within a memory budget; one made while the sprite was
/// on its way until it has arrived); `None` for one that is no vector tile, or not made
/// yet: with `until`, those missing are made nearest `centre` first until then (a round of them
/// at least). Natively, they are made on several threads.
/// at least). Natively, they are made on several threads. Also whether the sprites of its pattern
/// fills are on their way, as a mesh kept meanwhile (one made when a sprite failed, which is now
/// tried again) doesn't say so itself.
pub fn meshes(
style: &Arc<Resolved>,
source: &str,
tiles: &[DataTile],
level: u8,
until: Option<(web_time::Instant, [f64; 2])>,
) -> Vec<Option<Arc<TileMesh>>> {
) -> (Vec<Option<Arc<TileMesh>>>, bool) {
// Whether the style's sprite is still on its way, asked before the store is locked (a
// lookup in the sprites the tile cache keeps).
let sprite_pending =
style.sprite.as_deref().is_some_and(|b| matches!(rustgis_tiles::sprite::load(b, false), rustgis_tiles::sprite::Load::Pending));
let (sprite_pending, sprites_read) = if rustgis_tiles::shapes::draws_patterns(style, level) {
match rustgis_tiles::sprite::load(&style.sprites, false) {
rustgis_tiles::sprite::Load::Pending => (true, Vec::new()),
rustgis_tiles::sprite::Load::Ready(s) => (false, s.sheet_ids()),
rustgis_tiles::sprite::Load::Failed => (false, Vec::new()),
Comment thread
milovanderlinden marked this conversation as resolved.
Resolved
}
} else {
// No pattern fills here: nothing to load the sprites for.
(false, Vec::new())
};
let key = |t: &DataTile| Key { payload: Arc::as_ptr(&t.payload) as usize, style: Arc::as_ptr(style) as usize, level };
let mut out: Vec<Option<Arc<TileMesh>>> = Vec::with_capacity(tiles.len());
let mut missing = Vec::new();
Expand All @@ -394,7 +416,7 @@ pub fn meshes(
let found = s.entries.get_mut(&key(t)).filter(|e| {
e.payload.upgrade().is_some_and(|p| Arc::ptr_eq(&p, &t.payload))
&& e.style.upgrade().is_some_and(|x| Arc::ptr_eq(&x, style))
&& (!e.waiting || sprite_pending)
&& still_serves(e.waiting, e.sheets.as_deref(), sprite_pending, &sprites_read)
});
match found {
Some(e) => {
Expand All @@ -420,7 +442,7 @@ pub fn meshes(
let t = tiles.get(*i)?;
let Payload::Vector(tile) = &*t.payload else { return None };
let s = shapes(style, source, tile, level);
Some(Arc::new(TileMesh { waiting: s.waiting, ..tessellate(tile, &s.runs, t.z) }))
Some(Arc::new(TileMesh { waiting: s.waiting, sheets: s.sheets, ..tessellate(tile, &s.runs, t.z) }))
};
// A tile that panicked (hostile data) is done: an empty mesh, kept, so it isn't made again
// every frame, and the map settles.
Expand Down Expand Up @@ -461,6 +483,7 @@ pub fn meshes(
mesh: mesh.clone(),
used: tick,
waiting: mesh.waiting,
sheets: mesh.sheets.clone(),
};
// The bytes are counted again by `evict`, below.
s.entries.insert(key(t), entry);
Expand All @@ -476,7 +499,7 @@ pub fn meshes(
}
// Every call, so meshes of tiles the cache let go go as the map moves over others.
evict(&mut s, tick);
out
(out, sprite_pending)
}

/// `f` of every item (`None` for one that panicked), natively shared among threads. In the
Expand Down Expand Up @@ -628,6 +651,60 @@ mod tests {
assert_eq!(dash_pattern(Some(&[1.0, 2.0, 3.0, 4.0, 5.0])), None);
}

#[test]
fn a_mesh_is_made_again_when_other_sprites_are_read() {
let ids = |list: &[&str]| list.iter().map(|id| id.to_string()).collect::<Vec<_>>();
let (none, a, b, both) = (ids(&[]), ids(&["a"]), ids(&["b"]), ids(&["a", "b"]));
// Without pattern fills it stands, whatever the sprites do.
assert!(still_serves(false, None, false, &none) && still_serves(false, None, false, &both));
// Made from all that could be read: it stands while the other keeps failing...
assert!(still_serves(false, Some(&a), false, &a));
// ...and while that one is asked for again...
assert!(still_serves(false, Some(&a), true, &none));
// ...and is made again, once, when it is read.
assert!(!still_serves(false, Some(&a), false, &both));
assert!(still_serves(false, Some(&both), false, &both));
// As many sprites, but another one: made again.
assert!(!still_serves(false, Some(&a), false, &b));
// Made while they were on their way: until they've come.
assert!(still_serves(true, Some(&none), true, &none));
assert!(!still_serves(true, Some(&none), false, &both) && !still_serves(true, Some(&none), false, &none));
// None could be read: it stands, not made again on every frame.
assert!(still_serves(false, Some(&none), false, &none));
}

#[test]
fn meshes_say_a_sprite_is_on_its_way() {
use rustgis_tiles::style::{Source, Style, StyleLayer};
// Left unanswered, so the sprite stays on its way.
crate::tiles::tests::serve();
let address = "https://stuck.test/mesh-sprite".to_owned();
let resolved = |paint: serde_json::Value| {
let layer = serde_json::json!({"id": "p", "type": "fill", "source": "s", "source-layer": "l", "paint": paint});
let sprites = vec![("default".to_owned(), address.clone())];
Arc::new(Resolved {
style: Style {
name: String::new(),
sources: vec![("s".into(), Source::parse(&serde_json::json!({"type": "vector", "tiles": ["x/{z}/{x}/{y}"]})))],
layers: StyleLayer::parse(&layer).into_iter().collect(),
glyphs: None,
sprites: sprites.clone(),
sprites_left_out: Vec::new(),
sprites_listed: 1,
view: Default::default(),
},
sprites,
attribution: Vec::new(),
left_out: Vec::new(),
made: std::sync::Mutex::new(web_time::Instant::now()),
waiting: false,
})
};
assert!(meshes(&resolved(serde_json::json!({"fill-pattern": "dots"})), "s", &[], 3, None).1);
// Without pattern fills, a mesh doesn't wait for sprites.
assert!(!meshes(&resolved(serde_json::json!({"fill-color": "#f00"})), "s", &[], 3, None).1);
}

#[test]
fn meshes_are_made_once() {
let mut t = tile();
Expand All @@ -641,10 +718,12 @@ mod tests {
sources: vec![("s".into(), Source::parse(&serde_json::json!({"type": "vector", "tiles": ["x/{z}/{x}/{y}"]})))],
layers: StyleLayer::parse(&layer).into_iter().collect(),
glyphs: None,
sprite: None,
sprites: Vec::new(),
sprites_left_out: Vec::new(),
sprites_listed: 0,
view: Default::default(),
},
sprite: None,
sprites: Vec::new(),
attribution: Vec::new(),
left_out: Vec::new(),
made: std::sync::Mutex::new(web_time::Instant::now()),
Expand All @@ -653,16 +732,16 @@ mod tests {
let at = Extent::new(0.0, 0.0, 1.0, 1.0);
let tiles =
[DataTile { payload: payload.clone(), at, z: 1, clip: at }, DataTile { payload: Arc::new(Payload::Empty), at, z: 1, clip: at }];
let a = meshes(&style, "s", &tiles, 3, None);
let (a, _) = meshes(&style, "s", &tiles, 3, None);
assert!(a[0].as_ref().is_some_and(|m| !m.indices.is_empty()) && a[1].is_none());
let b = meshes(&style, "s", &tiles, 3, None);
let (b, _) = meshes(&style, "s", &tiles, 3, None);
// A payload standing in twice is made once, for both.
let twice = [tiles[0].clone(), tiles[0].clone()];
let t = meshes(&style, "s", &twice, 6, None);
let (t, _) = meshes(&style, "s", &twice, 6, None);
assert!(Arc::ptr_eq(t[0].as_ref().unwrap(), t[1].as_ref().unwrap()));
assert!(Arc::ptr_eq(a[0].as_ref().unwrap(), b[0].as_ref().unwrap()), "kept");
// Another level is another mesh.
let c = meshes(&style, "s", &tiles, 4, None);
let (c, _) = meshes(&style, "s", &tiles, 4, None);
assert!(!Arc::ptr_eq(a[0].as_ref().unwrap(), c[0].as_ref().unwrap()));
// Out of time before it starts: a round made, nearest the centre first; the rest later.
let many: Vec<DataTile> = (0..64)
Expand All @@ -671,10 +750,10 @@ mod tests {
DataTile { payload: Arc::new(Payload::Vector(tile_named("parks"))), at, z: 1, clip: at }
})
.collect();
let some = meshes(&style, "s", &many, 5, Some((web_time::Instant::now(), [63.5, 0.5])));
let (some, _) = meshes(&style, "s", &many, 5, Some((web_time::Instant::now(), [63.5, 0.5])));
let made = some.iter().filter(|m| m.is_some()).count();
assert!(made >= 1 && made < many.len() && some[63].is_some() && some[0].is_none(), "{made} made");
let all = meshes(&style, "s", &many, 5, Some((web_time::Instant::now() + std::time::Duration::from_secs(60), [0.0, 0.0])));
let (all, _) = meshes(&style, "s", &many, 5, Some((web_time::Instant::now() + std::time::Duration::from_secs(60), [0.0, 0.0])));
assert!(all.iter().all(Option::is_some));
}

Expand Down
9 changes: 5 additions & 4 deletions crates/render/src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ type LastTile = (std::sync::Weak<crate::tile_mesh::TileMesh>, [f64; 2], [f32; 4]
type Last = std::collections::HashMap<usize, (std::sync::Weak<rustgis_tiles::source::Resolved>, Vec<LastTile>)>;
static LAST: std::sync::OnceLock<std::sync::Mutex<Last>> = std::sync::OnceLock::new();

/// The meshes of `v`'s tiles, and whether some were made while the sprite was on its way. A map
/// The meshes of `v`'s tiles, and whether a sprite they use is on its way. A map
/// drawn for the screen at a pace (see `rustgis_tiles::Pace`) makes those missing a frame's worth
/// at a time, nearest `centre` first, the tiles drawn last standing in for the rest meanwhile,
/// clipped to their squares.
fn mesh_tiles(v: &rustgis_tiles::shapes::VectorTiles, centre: [f64; 2]) -> (Vec<MeshTile>, bool) {
let pace = rustgis_tiles::pace();
let meshes = crate::tile_mesh::meshes(&v.style, &v.source, &v.tiles, v.level, pace.map(|(_, until)| (until, centre)));
let (meshes, mut waiting) = crate::tile_mesh::meshes(&v.style, &v.source, &v.tiles, v.level, pace.map(|(_, until)| (until, centre)));
let lock = || LAST.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner());
let key = std::sync::Arc::as_ptr(&v.style) as usize;
let last: Vec<MeshTile> = lock()
Expand All @@ -183,7 +183,7 @@ fn mesh_tiles(v: &rustgis_tiles::shapes::VectorTiles, centre: [f64; 2]) -> (Vec<
.collect()
})
.unwrap_or_default();
let (mut stood_in, mut tiles, mut waiting) = (Vec::new(), Vec::new(), false);
let (mut stood_in, mut tiles) = (Vec::new(), Vec::new());
for (d, m) in v.tiles.iter().zip(meshes) {
let o = [d.at.min_x, d.at.max_y];
match m {
Expand Down Expand Up @@ -346,7 +346,8 @@ pub(crate) fn draw(
if !tiles.is_empty() {
g.cmds.push(DrawCmd::TileMeshes { tiles });
}
let d = rustgis_tiles::Drawn { labels: v.labels, attribution: v.attribution, pending: v.pending || waiting, error: None };
let d =
rustgis_tiles::Drawn { labels: v.labels, attribution: v.attribution, pending: v.pending || waiting, error: v.error };
collect(d, &t.name, &mut out);
continue;
}
Expand Down
Loading
Loading