From 21f13f52992e4da0c02f98a269bc2bbfa67fe930 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Tue, 17 Nov 2026 00:37:39 +0100 Subject: [PATCH] Build node graph click targets from the layout pass and time them against the editor --- .../storage_tests/layout_pass_tests.rs | 61 ++++++-- libraries/graph-layout/benches/layout.rs | 30 +++- libraries/graph-layout/src/click_targets.rs | 134 ++++++++++++++++++ libraries/graph-layout/src/lib.rs | 23 +++ 4 files changed, 238 insertions(+), 10 deletions(-) create mode 100644 libraries/graph-layout/src/click_targets.rs diff --git a/editor/src/messages/portfolio/document/storage_tests/layout_pass_tests.rs b/editor/src/messages/portfolio/document/storage_tests/layout_pass_tests.rs index 5c77558e40..bd7cb07b8c 100644 --- a/editor/src/messages/portfolio/document/storage_tests/layout_pass_tests.rs +++ b/editor/src/messages/portfolio/document/storage_tests/layout_pass_tests.rs @@ -2,6 +2,7 @@ use super::test_support::open_demo; use crate::messages::portfolio::document::utility_types::network_interface::{LayerPosition, NodeNetworkInterface, NodePosition, NodeTypePersistentMetadata}; +use crate::messages::portfolio::document::utility_types::text_metrics::text_width; use glam::IVec2; use graph_craft::document::NodeInput; use graph_layout::{Graph, Node, Placement, Wire, layout}; @@ -20,9 +21,25 @@ const DEMOS: [&str; 8] = [ "valley-of-spires.graphite", ]; const REPETITIONS: u32 = 200; +const FONT_SIZE: f64 = 14.; /// The root network of `interface` as the layout pass reads it, with the node each index stands for. -fn graph_of(interface: &NodeNetworkInterface) -> (Vec, Graph) { +/// The width of each layer name at the node graph's font size, measured once per distinct name. +#[derive(Default)] +struct NameWidths(HashMap); + +impl NameWidths { + fn of(&mut self, name: &str) -> f64 { + if let Some(width) = self.0.get(name) { + return *width; + } + let width = text_width(name, FONT_SIZE); + self.0.insert(name.to_string(), width); + width + } +} + +fn graph_of(interface: &NodeNetworkInterface, name_widths: &mut NameWidths) -> (Vec, Graph) { let network = interface.document_network(); let node_ids: Vec = network.nodes.keys().copied().collect(); let indices: HashMap = node_ids.iter().enumerate().map(|(index, id)| (*id, index as u32)).collect(); @@ -42,14 +59,17 @@ fn graph_of(interface: &NodeNetworkInterface) -> (Vec, Graph) { }, }; let height = interface.node_height(id, &[]); + let name_width = if is_layer { name_widths.of(&interface.display_name(id, &[])) } else { 0. }; Node { placement, is_layer, rows: height.saturating_sub(1), - width: 5, - inputs: network.nodes[id].inputs.len() as u32, - outputs: 1, + width: if is_layer { interface.layer_width(id, &[]).unwrap_or(8) } else { 5 }, + inputs: network.nodes[id].inputs.iter().filter(|input| input.is_exposed()).count() as u32, + outputs: interface.number_of_outputs(id, &[]) as u32, chainable: !interface.is_redirect_dot(id, &[]) && !interface.hidden_primary_output(id, &[]), + name_width, + locked: interface.is_locked(id, &[]), } }) .collect(); @@ -80,7 +100,9 @@ fn layout_pass_matches_and_times_the_editor_resolver() { for demo in DEMOS { let mut document = open_demo(demo); let interface = &mut document.network_interface; - let (node_ids, graph) = graph_of(interface); + let mut name_widths = NameWidths::default(); + let (node_ids, graph) = graph_of(interface, &mut name_widths); + let layer_names: Vec = node_ids.iter().filter(|id| interface.is_layer(id, &[])).map(|id| interface.display_name(id, &[])).collect(); let pass = layout(&graph); let mismatches: Vec<(NodeId, Option, IVec2)> = node_ids @@ -122,7 +144,7 @@ fn layout_pass_matches_and_times_the_editor_resolver() { let start = Instant::now(); for _ in 0..REPETITIONS { - std::hint::black_box(graph_of(interface)); + std::hint::black_box(graph_of(interface, &mut name_widths)); } let conversion = start.elapsed() / REPETITIONS; @@ -132,10 +154,33 @@ fn layout_pass_matches_and_times_the_editor_resolver() { } let one_pass = start.elapsed() / REPETITIONS; + let start = Instant::now(); + for _ in 0..REPETITIONS { + for name in &layer_names { + std::hint::black_box(text_width(std::hint::black_box(name), FONT_SIZE)); + } + } + let measuring_names = start.elapsed() / REPETITIONS; + + let start = Instant::now(); + for _ in 0..REPETITIONS { + for name in &layer_names { + std::hint::black_box(name_widths.of(std::hint::black_box(name))); + } + } + let looking_up_names = start.elapsed() / REPETITIONS; + + let start = Instant::now(); + for _ in 0..REPETITIONS { + std::hint::black_box(graph_layout::click_targets(&graph, std::hint::black_box(&pass))); + } + let new_click_targets = start.elapsed() / REPETITIONS; + let target_count = graph_layout::click_targets(&graph, &pass).len(); + println!( - "{demo:34} {:5} nodes {:5} wires | resolver {resolver:>10.1?} | click targets {click_targets:>10.1?} | pass {one_pass:>9.1?} (+ conversion {conversion:>9.1?}) | {} position mismatches | {chained} chained, {} chain mismatches", + "{demo:34} {:4} nodes {:3} layers | resolver {resolver:>9.1?} | editor click targets {click_targets:>9.1?} | measuring names {measuring_names:>9.1?} | memo {looking_up_names:>8.1?} | pass {one_pass:>8.1?} | new click targets {new_click_targets:>8.1?} ({target_count} targets) | conversion {conversion:>8.1?} | {} position, {} chain mismatches ({chained} chained)", graph.nodes.len(), - graph.wires.len(), + layer_names.len(), mismatches.len(), chain_mismatches.len(), ); diff --git a/libraries/graph-layout/benches/layout.rs b/libraries/graph-layout/benches/layout.rs index d586e58ee0..3954bfbf18 100644 --- a/libraries/graph-layout/benches/layout.rs +++ b/libraries/graph-layout/benches/layout.rs @@ -1,6 +1,7 @@ use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; +use glam::DVec2; use glam::IVec2; -use graph_layout::{Graph, Node, Placement, Wire, layout}; +use graph_layout::{Graph, Node, Placement, Wire, click_targets, layout, target_at}; use std::hint::black_box; const SIZES: [usize; 3] = [1_000, 10_000, 100_000]; @@ -29,6 +30,8 @@ fn node(placement: Placement, is_layer: bool) -> Node { inputs: 3, outputs: 1, chainable: true, + name_width: 60., + locked: false, } } @@ -138,5 +141,28 @@ fn bench_hit_test(criterion: &mut Criterion) { group.finish(); } -criterion_group!(benches, bench_layout, bench_hit_test); +fn bench_click_targets(criterion: &mut Criterion) { + let mut building = criterion.benchmark_group("click_targets"); + for size in SIZES { + let graph = stacked_layers(size); + let graph_layout = layout(&graph); + building.bench_with_input(BenchmarkId::from_parameter(size), &(graph, graph_layout), |bencher, (graph, graph_layout)| { + bencher.iter(|| click_targets(black_box(graph), black_box(graph_layout))) + }); + } + building.finish(); + + let mut hitting = criterion.benchmark_group("target_at"); + for size in SIZES { + let graph = stacked_layers(size); + let targets = click_targets(&graph, &layout(&graph)); + let point = DVec2::new(-100., 1_000.); + hitting.bench_with_input(BenchmarkId::from_parameter(size), &targets, |bencher, targets| { + bencher.iter(|| target_at(black_box(targets), black_box(point))) + }); + } + hitting.finish(); +} + +criterion_group!(benches, bench_layout, bench_hit_test, bench_click_targets); criterion_main!(benches); diff --git a/libraries/graph-layout/src/click_targets.rs b/libraries/graph-layout/src/click_targets.rs new file mode 100644 index 0000000000..84262d1207 --- /dev/null +++ b/libraries/graph-layout/src/click_targets.rs @@ -0,0 +1,134 @@ +//! The areas of a laid out graph that react to the pointer, as plain boxes in node graph pixels. + +use crate::{CHAIN_WIDTH, Graph, LAYER_HEIGHT, Layout}; +use glam::DVec2; + +pub const GRID_SIZE: f64 = 24.; +const HALF_GRID_SIZE: f64 = GRID_SIZE / 2.; +const NODE_WIDTH: f64 = 5. * GRID_SIZE; +const SIDE_PORT_HALF_SIZE: DVec2 = DVec2::new(12., 12.); +const STACK_PORT_HALF_SIZE: DVec2 = DVec2::new(12., 8.); +const GRIP_WIDTH: f64 = 8.; +const NAME_LEFT_OFFSET: f64 = HALF_GRID_SIZE + 3. * GRID_SIZE + 8.; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Part { + Body, + Input(u32), + Output(u32), + Visibility, + Lock, + Grip, + Name, +} + +#[derive(Clone, Copy, Debug)] +pub struct ClickTarget { + pub min: DVec2, + pub max: DVec2, + pub node: u32, + pub part: Part, +} + +impl ClickTarget { + fn around(center: DVec2, half_size: DVec2, node: u32, part: Part) -> Self { + Self { + min: center - half_size, + max: center + half_size, + node, + part, + } + } + + pub fn contains(&self, point: DVec2) -> bool { + point.cmpge(self.min).all() && point.cmplt(self.max).all() + } +} + +/// Every click target of the graph, with the parts of a node after its body so they take precedence over it. +pub fn click_targets(graph: &Graph, layout: &Layout) -> Vec { + let mut chain_lengths = vec![0_u32; graph.nodes.len()]; + for layer in layout.chain_layers.iter().flatten() { + chain_lengths[*layer as usize] += 1; + } + + let mut targets = Vec::with_capacity(graph.nodes.len() * 4); + for (index, node) in graph.nodes.iter().enumerate() { + let top_left = layout.positions[index].as_dvec2() * GRID_SIZE; + let index = index as u32; + + if !node.is_layer { + let body_top_left = top_left + DVec2::new(0., HALF_GRID_SIZE); + let height = node.rows as f64 * GRID_SIZE; + targets.push(ClickTarget { + min: body_top_left, + max: body_top_left + DVec2::new(NODE_WIDTH, height), + node: index, + part: Part::Body, + }); + + for input in 0..node.inputs { + let center = top_left + DVec2::new(0., GRID_SIZE * (input as f64 + 1.)); + targets.push(ClickTarget::around(center, SIDE_PORT_HALF_SIZE, index, Part::Input(input))); + } + for output in 0..node.outputs { + let center = top_left + DVec2::new(NODE_WIDTH, GRID_SIZE * (output as f64 + 1.)); + targets.push(ClickTarget::around(center, SIDE_PORT_HALF_SIZE, index, Part::Output(output))); + } + continue; + } + + let width = node.width as f64 * GRID_SIZE; + let height = LAYER_HEIGHT as f64 * GRID_SIZE; + let chain_width = if node.inputs > 1 { + (chain_lengths[index as usize] as i32 * CHAIN_WIDTH + 1) as f64 * GRID_SIZE + } else { + 0. + }; + targets.push(ClickTarget { + min: top_left - DVec2::new(chain_width, 0.), + max: top_left + DVec2::new(width, height), + node: index, + part: Part::Body, + }); + + targets.push(ClickTarget::around(top_left + DVec2::new(2. * GRID_SIZE, height + 8.), STACK_PORT_HALF_SIZE, index, Part::Input(0))); + if node.inputs > 1 { + targets.push(ClickTarget::around(top_left + DVec2::new(0., GRID_SIZE), SIDE_PORT_HALF_SIZE, index, Part::Input(1))); + } + targets.push(ClickTarget::around(top_left + DVec2::new(2. * GRID_SIZE, -8.), STACK_PORT_HALF_SIZE, index, Part::Output(0))); + + let icon_half_size = DVec2::splat(HALF_GRID_SIZE); + let icons_width = if node.locked { GRID_SIZE } else { 0. }; + targets.push(ClickTarget::around(top_left + DVec2::new(width, GRID_SIZE), icon_half_size, index, Part::Visibility)); + if node.locked { + targets.push(ClickTarget::around(top_left + DVec2::new(width - GRID_SIZE, GRID_SIZE), icon_half_size, index, Part::Lock)); + } + + let grip_right = top_left.x + width - HALF_GRID_SIZE - icons_width; + targets.push(ClickTarget { + min: DVec2::new(grip_right - GRIP_WIDTH, top_left.y + HALF_GRID_SIZE), + max: DVec2::new(grip_right, top_left.y + GRID_SIZE + HALF_GRID_SIZE), + node: index, + part: Part::Grip, + }); + + let name_left = top_left.x + NAME_LEFT_OFFSET; + let name_right = (name_left + node.name_width).min(top_left.x + width - HALF_GRID_SIZE - icons_width - GRIP_WIDTH); + if name_right > name_left { + targets.push(ClickTarget { + min: DVec2::new(name_left, top_left.y + HALF_GRID_SIZE), + max: DVec2::new(name_right, top_left.y + GRID_SIZE + HALF_GRID_SIZE), + node: index, + part: Part::Name, + }); + } + } + + targets +} + +/// The last added target containing `point`, which is a node's part before its body and a later node before an earlier one. +pub fn target_at(targets: &[ClickTarget], point: DVec2) -> Option<&ClickTarget> { + targets.iter().rev().find(|target| target.contains(point)) +} diff --git a/libraries/graph-layout/src/lib.rs b/libraries/graph-layout/src/lib.rs index 2fe0455201..8cabb856a7 100644 --- a/libraries/graph-layout/src/lib.rs +++ b/libraries/graph-layout/src/lib.rs @@ -1,5 +1,8 @@ //! Lays out a node graph in one pass: every node is placed after all the nodes reading it, then its bounds, ports, and wires follow from its position. +mod click_targets; + +pub use click_targets::{ClickTarget, GRID_SIZE, Part, click_targets, target_at}; use glam::IVec2; /// How many grid cells a chain steps left for each node. @@ -31,6 +34,9 @@ pub struct Node { pub outputs: u32, /// Whether the node can be drawn in a layer's chain, which redirect dots and nodes with a hidden primary output can't. pub chainable: bool, + /// The width of a layer's name in pixels, measured by the caller. + pub name_width: f64, + pub locked: bool, } impl Node { @@ -257,6 +263,8 @@ mod tests { inputs: 2, outputs: 1, chainable: true, + name_width: 60., + locked: false, } } @@ -332,4 +340,19 @@ mod tests { assert_eq!(layout.node_at(IVec2::new(1, 1)), Some(0)); assert_eq!(layout.node_at(IVec2::new(20, 20)), None); } + + #[test] + fn a_port_takes_precedence_over_its_node_body() { + let graph = Graph { + nodes: vec![node(Placement::Absolute(IVec2::ZERO), false)], + wires: Vec::new(), + }; + + let targets = click_targets(&graph, &layout(&graph)); + let at = |x: f64, y: f64| target_at(&targets, glam::DVec2::new(x, y)).map(|target| target.part); + + assert_eq!(at(2., GRID_SIZE), Some(Part::Input(0))); + assert_eq!(at(60., GRID_SIZE), Some(Part::Body)); + assert_eq!(at(60., 200.), None); + } }