From 9a274f91dfc2ac619390fb8015df04609fd11e5d Mon Sep 17 00:00:00 2001 From: SiroArt <152216922+SiroArt@users.noreply.github.com> Date: Wed, 4 Nov 2026 22:13:38 +0000 Subject: [PATCH] Planned bodies: smaller GPU chunks, and a chunk keeps its colours while they are unchanged (#5490) Co-authored-by: Claude Sonnet 5.5 --- crates/gpu/src/view.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/gpu/src/view.rs b/crates/gpu/src/view.rs index be747134c..07ff5a798 100644 --- a/crates/gpu/src/view.rs +++ b/crates/gpu/src/view.rs @@ -1677,9 +1677,11 @@ impl PaletteGpu { } /// Planned bodies end a GPU chunk one time in this many (by their plan's address), and a chunk -/// holds at most [`MAX_CHUNK_BODIES`]: a scene of a thousand bodies takes a few dozen dispatches -/// once, and a changed body re-evaluates a few dozen bodies, not all of them. -const CHUNK_SPLIT: u64 = 32; +/// holds at most [`MAX_CHUNK_BODIES`]: a scene of a thousand bodies takes a few hundred +/// dispatches once, and a changed body re-evaluates a few bodies, not all of them. (At 32, a +/// refinement that finished while a 194-body assembly was orbited re-evaluated about 10 bodies +/// for each one that changed, 12 ms a chunk.) +const CHUNK_SPLIT: u64 = 4; const MAX_CHUNK_BODIES: usize = 128; /// A block's see-through triangles as the global glass stream took them ([`Batches::glass_from`]). @@ -1744,6 +1746,10 @@ struct Batches { /// Evaluated chunks by their plans' addresses, holding the plans so the addresses stay /// theirs: a chunk whose plans are unchanged is not evaluated again when the scene is rebuilt. resident: std::collections::HashMap, (Vec>, Arc)>, + /// Each resident chunk's colour buffer by the same key, with what it was built from (the + /// chunk's mesh and its bodies' face colours): a scene rebuilt for a change that leaves the + /// colours alone keeps it, instead of filling a colour for every vertex of every chunk again. + chunk_colors: std::collections::HashMap, (u64, wgpu::Buffer)>, } impl Batches { @@ -2008,6 +2014,7 @@ impl Batches { } let key = |c: &[&PlannedBody]| c.iter().map(|b| Arc::as_ptr(&b.plan) as usize).collect::>(); self.resident.retain(|k, _| chunks.iter().any(|c| key(c) == *k)); + self.chunk_colors.retain(|k, _| chunks.iter().any(|c| key(c) == *k)); let mut out = Vec::with_capacity(chunks.len()); for chunk in &chunks { let mesh = match self.resident.get(&key(chunk)) { @@ -2029,6 +2036,19 @@ impl Batches { } } }; + let looks = { + let mut h = std::collections::hash_map::DefaultHasher::new(); + std::hash::Hash::hash(&(Arc::as_ptr(&mesh) as usize), &mut h); + for b in chunk { + std::hash::Hash::hash(&b.face_colors, &mut h); + } + std::hash::Hasher::finish(&h) + }; + if let Some((_, colors)) = self.chunk_colors.get(&key(chunk)).filter(|(l, _)| *l == looks) { + let (class, closed) = chunk.first().map_or((TriClass::Opaque, true), |b| (b.class, b.closed)); + out.push(PlannedDraw { mesh, colors: colors.clone(), class, closed, plans: key(chunk) }); + continue; + } let mut bytes = Vec::with_capacity(mesh.vertex_face.len() * 4); for (part, b) in mesh.parts.iter().zip(chunk) { let last = b.face_colors.last().copied().unwrap_or(PaletteIndex(0)); @@ -2040,6 +2060,7 @@ impl Batches { self.uploads.planned += bytes.len() as u64; if let Some((colors, _)) = upload(device, "sc_planned_colors", &bytes, 4).and_then(|c| c.parts.into_iter().next()) { let (class, closed) = chunk.first().map_or((TriClass::Opaque, true), |b| (b.class, b.closed)); + self.chunk_colors.insert(key(chunk), (looks, colors.clone())); out.push(PlannedDraw { mesh, colors, class, closed, plans: key(chunk) }); } }