SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Viewport: write hover edges as one record per line, colours interned once (#7416)
Jackson Oursland76879f8b3968
diff --git a/crates/gpu/src/view.rs b/crates/gpu/src/view.rsindex 1c4a945478..f7a8249cf2 100644--- a/crates/gpu/src/view.rs+++ b/crates/gpu/src/view.rs@@ -745,21 +745,11 @@ impl BlockBuilder {/// A depth-tested line, or one only shown where the model hides it.pub fn line(&mut self, a: [f32; 3], b: [f32; 3], c: PaletteIndex, width: f32) {- let out = &mut self.lines;- for v in a.iter().chain(&b) {- out.extend_from_slice(&v.to_le_bytes());- }- out.extend_from_slice(&c.0.to_le_bytes());- out.extend_from_slice(&width.to_le_bytes());+ self.lines.extend_from_slice(&line_record(a, b, c, width.to_bits()));}pub fn hidden_line(&mut self, a: [f32; 3], b: [f32; 3], c: PaletteIndex, width: f32) {- let out = &mut self.hidden;- for v in a.iter().chain(&b) {- out.extend_from_slice(&v.to_le_bytes());- }- out.extend_from_slice(&c.0.to_le_bytes());- out.extend_from_slice(&width.to_le_bytes());+ self.hidden.extend_from_slice(&line_record(a, b, c, width.to_bits()));}/// How many vertices the block holds so far.@@ -810,11 +800,14 @@ impl GpuScene {}/// A line shown only where the model hides it.pub fn hidden_line(&mut self, a: [f32; 3], b: [f32; 3], c: PaletteIndex, width: f32) {- for v in a.iter().chain(&b) {- self.hidden.extend_from_slice(&v.to_le_bytes());- }- self.hidden.extend_from_slice(&c.0.to_le_bytes());- self.hidden.extend_from_slice(&width.to_le_bytes());+ self.hidden.extend_from_slice(&line_record(a, b, c, width.to_bits()));+ }++ /// Room for `lines` more lines and `hidden` more hidden lines, so a highlight of many edges+ /// is written without growing its buffers line by line.+ pub fn reserve_lines(&mut self, lines: usize, hidden: usize) {+ let _ = self.lines.try_reserve(lines.saturating_mul(LINE_SIZE));+ let _ = self.hidden.try_reserve(hidden.saturating_mul(LINE_SIZE));}pub fn glass_tri(&mut self, p: [f32; 3], n: [f32; 3], c: PaletteIndex) {for v in p.iter().chain(&n) {@@ -863,20 +856,28 @@ impl GpuScene {/// unknown), so a face seen at a grazing angle no longer buries part of the band (it showed as/// a thin wedge instead of a line). See [`lifted_width`] for how this travels.pub fn lifted_line(&mut self, a: [f32; 3], b: [f32; 3], c: PaletteIndex, width: f32, normals: [[f32; 3]; 2]) {- for v in a.iter().chain(&b) {- self.lines.extend_from_slice(&v.to_le_bytes());- }- self.lines.extend_from_slice(&c.0.to_le_bytes());- self.lines.extend_from_slice(&lifted_width(width, normals).to_le_bytes());+ self.lifted_line_word(a, b, c, lifted_width(width, normals));+ }+ /// [`GpuScene::lifted_line`] with its width word already worked out ([`lifted_width`]), for+ /// many segments that share it.+ pub fn lifted_line_word(&mut self, a: [f32; 3], b: [f32; 3], c: PaletteIndex, word: u32) {+ self.lines.extend_from_slice(&line_record(a, b, c, word));}pub fn line(&mut self, a: [f32; 3], b: [f32; 3], c: PaletteIndex, width: f32, on_top: bool) {let out = if on_top { &mut self.overlay } else { &mut self.lines };- for v in a.iter().chain(&b) {- out.extend_from_slice(&v.to_le_bytes());- }- out.extend_from_slice(&c.0.to_le_bytes());- out.extend_from_slice(&width.to_le_bytes());+ out.extend_from_slice(&line_record(a, b, c, width.to_bits()));+ }+}++/// One line's [`LINE_SIZE`] bytes: its ends, its palette index and its width word (an `f32`'s+/// bits, or [`lifted_width`]'s).+fn line_record(a: [f32; 3], b: [f32; 3], c: PaletteIndex, width: u32) -> [u8; LINE_SIZE] {+ let mut out = [0u8; LINE_SIZE];+ let words = a.into_iter().chain(b).map(f32::to_bits).chain([c.0, width]);+ for (chunk, word) in out.as_chunks_mut::<4>().0.iter_mut().zip(words) {+ *chunk = word.to_le_bytes();}+ out}pub type SceneSlot = Arc<Mutex<Option<GpuScene>>>;@@ -4023,6 +4024,34 @@ mod glass_tests {assert_eq!(cut(&[(0, 12), (15, 24)], &[]), r(&[(0, 12), (15, 24)]));}+ /// A line is written as one record: its ends, its palette index and its width word, each+ /// four little-endian bytes, as the shader reads them; a lifted line's word is+ /// [`lifted_width`]'s, whether worked out per segment or once for many.+ #[test]+ fn lines_are_written_as_one_record() {+ let (a, b, c): ([f32; 3], [f32; 3], _) = ([1.0, -2.0, 3.5], [4.0, 5.0, -6.25], PaletteIndex(7));+ let mut expected = Vec::new();+ for v in a.iter().chain(&b) {+ expected.extend_from_slice(&v.to_le_bytes());+ }+ expected.extend_from_slice(&7u32.to_le_bytes());+ expected.extend_from_slice(&1.5f32.to_le_bytes());+ let mut sc = GpuScene::default();+ sc.line(a, b, c, 1.5, false);+ sc.hidden_line(a, b, c, 1.5);+ assert_eq!(sc.lines, expected);+ assert_eq!(sc.hidden, expected);+ let normals = [[0.0, 0.0, 1.0], [1.0, 0.0, 0.0]];+ let (mut per_segment, mut shared) = (GpuScene::default(), GpuScene::default());+ per_segment.lifted_line(a, b, c, 2.0, normals);+ shared.lifted_line_word(a, b, c, lifted_width(2.0, normals));+ assert_eq!(per_segment.lines, shared.lines);+ assert_eq!(per_segment.lines.get(28..32), Some(&lifted_width(2.0, normals).to_le_bytes()[..]));+ let mut block = BlockBuilder::default();+ block.line(a, b, c, 1.5);+ assert_eq!(*block.freeze().lines, expected);+ }+/// Without order-independent transparency, the sorted glass stream is kept while the blocks'/// see-through triangles stay the same: a colour edit (a new palette) uploads palette rows/// and no geometry; a class change rebuilds the stream.diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rsindex 2535f3ddaa..f5aba312f3 100644--- a/crates/ui-egui/src/gpu.rs+++ b/crates/ui-egui/src/gpu.rs@@ -10,7 +10,7 @@ use egui_wgpu::wgpu;pub use solidcraft_gpu::view::{AccessMap, BlockBuilder, BlockBytes, BlockTris, DrawStats, DrawStatsHandle, Frame, GpuImage, GpuScene, GpuTarget, INSTANCE_SIZE, InstanceLook,InstancedMesh, LINE_SIZE, LOD_PX, MAX_PALETTE, PBR_SIZE, Palette, PaletteIndex, PlannedBody, Renderer, SHARED_SIZE, SceneBlock, SceneSlot,- SharedTris, TRI_SIZE, TriClass, extend_runs, level_for, placement_scale, runs_within, supersampled_size,+ SharedTris, TRI_SIZE, TriClass, extend_runs, level_for, lifted_width, placement_scale, runs_within, supersampled_size,};/// A frame of the viewport as an egui paint callback.diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rsindex 4a61155baf..3ce8c1fa95 100644--- a/crates/ui-egui/src/viewport.rs+++ b/crates/ui-egui/src/viewport.rs@@ -2817,17 +2817,34 @@ const SKETCH_HALO_WIDTH: f32 = 2.0;/// A highlighted edge: a halo and a core, and where the model hides it, the core dimmed (so a/// pick on the far side still shows).-fn edge_lines(sc: &mut GpuScene, e: &[Vec3], core: Color32, halo: Color32, w: f32, normals: &[[[f32; 3]; 2]]) {- for (col, width) in [(halo, w + 3.0), (core, w)] {- let col = sc.color(c4(col));+/// The palette entries of a highlighted edge: its halo, its core, and its dimmed part where the+/// model hides it. Interned once for every edge drawn in them.+#[derive(Clone, Copy)]+struct EdgeColours {+ halo: crate::gpu::PaletteIndex,+ core: crate::gpu::PaletteIndex,+ hidden: crate::gpu::PaletteIndex,+}++impl EdgeColours {+ fn new(sc: &mut GpuScene, core: Color32, halo: Color32) -> EdgeColours {+ EdgeColours { halo: sc.color(c4(halo)), core: sc.color(c4(core)), hidden: sc.color(c4(core.gamma_multiply(0.45))) }+ }+}++fn edge_lines(sc: &mut GpuScene, e: &[Vec3], cols: EdgeColours, w: f32, normals: &[[[f32; 3]; 2]]) {+ let segments = e.len().saturating_sub(1);+ sc.reserve_lines(2 * segments, segments);+ for (col, width) in [(cols.halo, w + 3.0), (cols.core, w)] {+ // Segments without their faces' normals share one width word.+ let flat = crate::gpu::lifted_width(width, [[0.0; 3]; 2]);for (i, p) in e.windows(2).enumerate() {- let n = normals.get(i).copied().unwrap_or_default();- sc.lifted_line(p[0].to_f32(), p[1].to_f32(), col, width, n);+ let word = normals.get(i).map_or(flat, |n| crate::gpu::lifted_width(width, *n));+ sc.lifted_line_word(p[0].to_f32(), p[1].to_f32(), col, word);}}- let hidden = sc.color(c4(core.gamma_multiply(0.45)));for p in e.windows(2) {- sc.hidden_line(p[0].to_f32(), p[1].to_f32(), hidden, (w * 0.7).max(1.0));+ sc.hidden_line(p[0].to_f32(), p[1].to_f32(), cols.hidden, (w * 0.7).max(1.0));}}@@ -3307,6 +3324,8 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {return sc;}let mut feedback = GpuScene { palette: std::sync::Arc::new((*sc.palette).clone()), ..Default::default() };+ let (hover_edge, selected_edge) =+ (EdgeColours::new(&mut feedback, t.hover_edge, t.hover_edge_halo), EdgeColours::new(&mut feedback, t.sel_edge, t.sel_edge_rim));let mut hover_instances = HighlightInstances::default();let mut selected_instances = HighlightInstances::default();// Selected things.@@ -3337,7 +3356,7 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {{let m = drawn_mesh(app, b);if let Some(e) = edge_of(&m, *index, *point) {- edge_lines(&mut feedback, e, t.sel_edge, t.sel_edge_rim, 3.0, &edge_normals(&m, e));+ edge_lines(&mut feedback, e, selected_edge, 3.0, &edge_normals(&m, e));}}}@@ -3440,7 +3459,7 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {}for (ei, e) in m.edges.iter().enumerate() {if m.edge_pickable(ei) && (whole || m.edge_faces.get(ei).is_some_and(|fs| fs.iter().any(|f| faces.contains(&(*f as usize))))) {- edge_lines(&mut feedback, e, t.hover_edge, t.hover_edge_halo, 1.5, &[]);+ edge_lines(&mut feedback, e, hover_edge, 1.5, &[]);}}}@@ -3477,7 +3496,7 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {if let Some(p) = m.edges.get(e)&& m.edge_pickable(e){- edge_lines(&mut feedback, p, t.hover_edge, t.hover_edge_halo, 1.5, &[]);+ edge_lines(&mut feedback, p, hover_edge, 1.5, &[]);}}}@@ -3489,7 +3508,7 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {for i in m.circular_rim(*index) {let selected = sel.iter().any(|x| matches!(x, Sel::Edge { body: b, index: j, .. } if b == body && *j == i));if !selected && let Some(e) = if i == *index { edge_of(&m, i, *mid) } else { m.edges.get(i) } {- edge_lines(&mut feedback, e, t.hover_edge, t.hover_edge_halo, 2.0, &edge_normals(&m, e));+ edge_lines(&mut feedback, e, hover_edge, 2.0, &edge_normals(&m, e));}}}