* Viewport: palette-indexed scene and incremental appearance updates - Move triangle, line and instance colours and opacity into a per-scene palette (an R32Uint texture, WebGL2-safe); appearance edits update palette entries instead of vertex bytes. - Build CPU body triangles as one canonical vertex stream per block with opaque/glass/faded index lists; an opacity class change rewrites only the index list. - Cache resolved body appearances by a per-body key, so an edit resolves only the bodies whose look changed. - Split the model scene key from the session revision: selection and view-only commands no longer rebuild or upload the model scene. - Replace the O(n^2) section-index lookup and per-placement instance scan with hash lookups. Co-Authored-By: Claude Code <noreply@anthropic.com> * Viewport: skip unchanged palette work when rebuilding - Reuse each body's appearance key while the document is unchanged. - Leave a palette slot alone when its value did not change, and allocate every body's slots once before the build loop. Co-Authored-By: Claude Code <noreply@anthropic.com> * Viewport: measure scene rebuild cost by edit kind An ignored scale test times the scene build after selection, appearance, theme and one-body geometry edits at 200, 1,000 and 5,000 bodies. Co-Authored-By: Claude Code <noreply@anthropic.com> * Viewport: read faded grid alphas through the scene palette The grid-fade tests now resolve line palette indices instead of reading the line record's old colour byte. Co-Authored-By: Claude Code <noreply@anthropic.com> * Viewport: measure rebuild cost on few large meshes Adds the 20/100-torus case where a block rebuild used to re-emit a large part of the model. Co-Authored-By: Claude Code <noreply@anthropic.com> * Viewport: measure rebuild cost on an assembly Unique detailed parts plus one component placed many times, timed for selection, part and component colour, opacity, theme, hide and move. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Viewport: draw block classes as vertex runs, not index lists A body's vertices are three per triangle in mesh order, so each draw class is a few runs of the block's vertex buffer: all-opaque blocks need no index buffer, and an opacity class change uploads nothing. A fragmented class (more than 64 runs) falls back to one index buffer. Each triangle is written as one record into a block reserved up front, which takes the cold scene build of a 7.1M-triangle assembly from 97 ms to 55 ms (main: 83 ms). Also fix reindexing a block whose bodies are drawn elsewhere: it walked the mesh's triangles from that body's start, past its own vertices. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Viewport: draw the highlight's own block, and light faded bodies without copies The highlight scene keeps its feedback (a hovered face, a body copied on the CPU) in a block of its own; since blocks became vertex runs, only the model's blocks were drawn, so that feedback was uploaded and never shown. Draw the highlight's blocks with its triangles. Whole faded and see-through bodies (another component active, or an appearance below full opacity) were lit by copying every triangle on the CPU at each hover change: 85 ms for a design row of 812 bodies. They are now drawn again from the model's own block vertices, in the highlight palette with their slots set to the highlight colour, or as placements of the model's shared triangles. The picture is the same as the copy's. Adds a GPU frame benchmark for document-tree hover (ignored test). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Viewport: count scene uploads, and keep the sorted glass stream across colour edits Renderer::uploads reports the bytes the model scene's last change uploaded (geometry, placements, palette rows, planned colours, re-evaluated vertices). Without order-independent transparency the global glass stream is kept while the blocks' see-through triangles are the same, so a colour edit on a glass body uploads palette rows only. Tests assert both on a GPU. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Viewport: keep palette slots and looks without rehashing every body A rebuild found every body's palette slots by name and wrote them back, allocating and hashing a new key per body, and copied every shown name into a set to check the arena still belongs to the design. Slots are written back only when a body's faces grew, and the looks cache is pruned only when it holds more bodies than are shown. Adds the STEP-file benchmark (SOLIDCRAFT_SCALE_STEP) and the colour command's own time to the assembly benchmark. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Viewport: round-pick construction lines through the scene palette The axis and centre feedback for round picks (#4610) drew with a raw colour; on the palette scene it is interned once like the other construction geometry. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Viewport: draw faded and see-through bodies from their plans with OIT A body faded outside the active component, or see-through, left the GPU tessellation path and was built triangle by triangle on the CPU, so working in a component of a large assembly rebuilt every other body. Where the renderer blends see-through surfaces order-independently (GpuTarget::oit, set by the renderer), a body whose faces share one class is drawn from its plan: chunks are split by class, opaque passes, AO and the section draw only opaque chunks, and the OIT pass draws the rest. The sorted fallback keeps them on the CPU. The model and its highlights decide through one rule (model_plan_class). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Code <noreply@anthropic.com>
189 lines
7.5 KiB
Rust
189 lines
7.5 KiB
Rust
//! Stable palette slots for the model scene.
|
|
//!
|
|
//! Cached block bytes index a scene palette (`solidcraft_gpu::view::Palette`). A body's faces,
|
|
//! edges and placement keep their slots while the scene rebuilds, so an appearance or theme edit
|
|
//! changes only palette entries — never a block's vertex bytes. Slots are append-only for the
|
|
//! session and are reset when a different design replaces the one they belonged to.
|
|
//!
|
|
//! The model scene needs at most one stable slot per visible body face (plus a few per body), so
|
|
//! the arena is capped by [`MAX_PALETTE`]. Past the cap a face falls back to its body's slot:
|
|
//! editing such a face once rebuilds that body's block (the build still succeeds).
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use crate::gpu::{MAX_PALETTE, PaletteIndex};
|
|
|
|
/// The stable slots of one body: its own colour, its face range, its edges and its instances.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(crate) struct BodySlots {
|
|
/// The body's own colour: the fallback for faces past the allocated range.
|
|
pub body: PaletteIndex,
|
|
/// The first face slot, or [`u32::MAX`] when the arena is full (every face uses `body`).
|
|
pub faces: u32,
|
|
pub face_capacity: u32,
|
|
pub edge: PaletteIndex,
|
|
pub hidden_edge: PaletteIndex,
|
|
pub instance: PaletteIndex,
|
|
}
|
|
|
|
impl BodySlots {
|
|
/// The slot of face `face`, or the body's slot when the face has none.
|
|
pub fn face(&self, face: usize) -> PaletteIndex {
|
|
if self.faces == u32::MAX || face >= self.face_capacity as usize {
|
|
return self.body;
|
|
}
|
|
PaletteIndex(self.faces.saturating_add(face as u32))
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub(crate) struct PaletteArena {
|
|
entries: Vec<[u8; 4]>,
|
|
bodies: HashMap<String, BodySlots>,
|
|
/// Bumped when the arena is reset (a different design); cached blocks must rebuild then.
|
|
generation: u64,
|
|
}
|
|
|
|
impl PaletteArena {
|
|
/// The stable entries, copied into the scene's palette.
|
|
pub fn entries(&self) -> Vec<[u8; 4]> {
|
|
self.entries.clone()
|
|
}
|
|
|
|
pub fn generation(&self) -> u64 {
|
|
self.generation
|
|
}
|
|
|
|
/// The slots of `name`, allocating them (and `face_count` face slots) the first time.
|
|
pub fn body(&mut self, name: &str, face_count: usize) -> BodySlots {
|
|
if let Some(slots) = self.bodies.get(name).copied() {
|
|
let grown = self.grow_faces(&slots, face_count);
|
|
// Written back only when it grew: most rebuilds find every body as it was.
|
|
if grown != slots
|
|
&& let Some(kept) = self.bodies.get_mut(name)
|
|
{
|
|
*kept = grown;
|
|
}
|
|
return grown;
|
|
}
|
|
let body = self.push();
|
|
let edge = self.push();
|
|
let hidden_edge = self.push();
|
|
let instance = self.push();
|
|
let slots = BodySlots { body, faces: u32::MAX, face_capacity: 0, edge, hidden_edge, instance };
|
|
let slots = self.grow_faces(&slots, face_count);
|
|
self.bodies.insert(name.to_string(), slots);
|
|
slots
|
|
}
|
|
|
|
/// Extend a body's face range when its face count grew. The range is reused when it is at the
|
|
/// end of the arena; otherwise a new range is appended (the old one is left unused rather
|
|
/// than moving an index cached blocks may refer to).
|
|
fn grow_faces(&mut self, slots: &BodySlots, face_count: usize) -> BodySlots {
|
|
let want = u32::try_from(face_count).unwrap_or(u32::MAX);
|
|
if slots.faces != u32::MAX && slots.face_capacity >= want {
|
|
return *slots;
|
|
}
|
|
let room = MAX_PALETTE.saturating_sub(self.entries.len());
|
|
let need = usize::try_from(want).unwrap_or(usize::MAX).saturating_sub(slots.face_capacity as usize);
|
|
let at_end = slots.faces != u32::MAX && slots.faces as usize + slots.face_capacity as usize == self.entries.len();
|
|
if at_end && need <= room {
|
|
self.entries.resize(self.entries.len() + need, [0; 4]);
|
|
return BodySlots { face_capacity: want, ..*slots };
|
|
}
|
|
if want as usize <= room {
|
|
let base = u32::try_from(self.entries.len()).unwrap_or(u32::MAX);
|
|
self.entries.resize(self.entries.len() + want as usize, [0; 4]);
|
|
return BodySlots { faces: base, face_capacity: want, ..*slots };
|
|
}
|
|
// Out of room: every face falls back to the body's own slot.
|
|
BodySlots { faces: u32::MAX, face_capacity: 0, ..*slots }
|
|
}
|
|
|
|
/// Append a zero entry; [`PaletteIndex(0)`] when the arena is full (never a panic).
|
|
fn push(&mut self) -> PaletteIndex {
|
|
if self.entries.len() >= MAX_PALETTE {
|
|
return PaletteIndex(0);
|
|
}
|
|
let i = u32::try_from(self.entries.len()).unwrap_or(0);
|
|
self.entries.push([0; 4]);
|
|
PaletteIndex(i)
|
|
}
|
|
|
|
/// The slots of `name`, if it has any (nothing is allocated).
|
|
pub fn slots(&self, name: &str) -> Option<BodySlots> {
|
|
self.bodies.get(name).copied()
|
|
}
|
|
|
|
/// The colour at `slot`, if it is allocated.
|
|
pub fn get(&self, slot: PaletteIndex) -> Option<[u8; 4]> {
|
|
self.entries.get(slot.0 as usize).copied()
|
|
}
|
|
|
|
/// Set a slot's colour; out-of-range slots are ignored.
|
|
pub fn set(&mut self, slot: PaletteIndex, rgba: [u8; 4]) {
|
|
if let Some(entry) = self.entries.get_mut(slot.0 as usize) {
|
|
*entry = rgba;
|
|
}
|
|
}
|
|
|
|
/// Drop every body when the scene belongs to a different design (no body name survives).
|
|
/// A design that removed every body and then adds one keeps its slots (the empty set does
|
|
/// not reset the arena).
|
|
pub fn sync<'a>(&mut self, names: impl IntoIterator<Item = &'a str>) {
|
|
if self.bodies.is_empty() {
|
|
return;
|
|
}
|
|
let mut names = names.into_iter().peekable();
|
|
if names.peek().is_none() {
|
|
return;
|
|
}
|
|
if !names.any(|n| self.bodies.contains_key(n)) {
|
|
self.entries.clear();
|
|
self.bodies.clear();
|
|
self.generation = self.generation.wrapping_add(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn slots_survive_rebuilds_and_faces_fall_back_at_the_cap() {
|
|
let mut arena = PaletteArena::default();
|
|
let first = arena.body("A", 2);
|
|
let again = arena.body("A", 2);
|
|
assert_eq!(first, again);
|
|
assert_ne!(first.face(0), first.face(1));
|
|
assert_eq!(first.face(2), first.body);
|
|
arena.set(first.face(0), [1, 2, 3, 4]);
|
|
assert_eq!(arena.entries()[first.face(0).0 as usize], [1, 2, 3, 4]);
|
|
// A grown face count reuses the range at the end of the arena.
|
|
let grown = arena.body("A", 4);
|
|
assert_eq!(grown.faces, first.faces);
|
|
assert_ne!(grown.face(3), grown.body);
|
|
// A new design with no shared names resets the slots.
|
|
let generation = arena.generation();
|
|
arena.sync(["B"]);
|
|
assert_eq!(arena.generation(), generation.wrapping_add(1));
|
|
assert!(arena.entries().is_empty());
|
|
// An empty scene does not reset.
|
|
let before = arena.generation();
|
|
arena.sync([]);
|
|
assert_eq!(arena.generation(), before);
|
|
}
|
|
|
|
#[test]
|
|
fn the_cap_falls_back_to_the_body_slot_without_panicking() {
|
|
// The arena cannot allocate past MAX_PALETTE; simulate a full one by filling entries.
|
|
let mut arena = PaletteArena { entries: vec![[0; 4]; MAX_PALETTE], ..Default::default() };
|
|
let slots = arena.body("Big", 10);
|
|
assert_eq!(slots.faces, u32::MAX);
|
|
assert_eq!(slots.face(3), slots.body);
|
|
// Every push after the cap is index 0, never an out-of-range write.
|
|
assert_eq!(arena.push(), PaletteIndex(0));
|
|
}
|
|
}
|