facett_graphview/lib.rs
1//! **facett-graphview** — a domain-agnostic, scalable 2D **graph render engine**,
2//! vello-backed, that **runtime-selects its backend by hardware**: `vello`
3//! (GPU/wgpu compute) when a usable GPU exists, `vello_cpu` (multithreaded SIMD)
4//! as the no-GPU fallback. The eventual single home for every graph surface —
5//! nornir's arch/dep/release dashboards, korp, graph-DB browsing — aiming to
6//! beat Graphviz on interactivity + scale.
7//!
8//! # The shared API (a future drop-in for nornir's `draw_graph`)
9//! The model — [`GraphModel`] (nodes id/label/pos/fill/stroke + edges
10//! from/to/color/dashed/label) and the caller-supplied [`Decorations`] overlay
11//! (per-node ring/badge + emphasis edges) — mirrors nornir's
12//! `src/viz/graph_render.rs` SHARED API exactly, so that egui routine can swap
13//! its painter for this engine without changing its callers. Decorations stay
14//! caller-supplied and domain-agnostic: facett never learns what a "release
15//! gate" is.
16//!
17//! # Backends (vello is NOT GPU-only)
18//! [`Backend`] {`GpuVello`, `CpuVello`}; [`decide`] turns a [`GpuProbe`] into a
19//! pick (probe → choose, mirroring nornir's embedder runtime-select). The CPU
20//! path ([`cpu::render`]) is the proven reference — `vello_cpu` already ships
21//! transitively inside epaint 0.34, so it always builds here. The GPU path is a
22//! real, version-aligned seam (`vello` 0.9 ↔ wgpu 29 ↔ egui-wgpu 0.34), wired
23//! behind the `gpu` cargo feature; see [`gpu`].
24//!
25//! # Render entry point
26//! [`render_to_rgba`] dispatches on the chosen [`Backend`] and returns straight
27//! RGBA8 pixels (PNG / egui-texture ready). Today both backends route through
28//! the CPU rasterizer (the GPU readback path is the #17 follow-up); the dispatch
29//! seam is in place.
30//!
31//! # What is STUBBED (documented, not built — see `.nornir/design.md`)
32//! LOD / viewport-culling, clustering / aggregation, edge-bundling, streaming
33//! layout, and the data-source **adapters** (nornir dep graph / FalkorDB /
34//! pipeline). Clear TODOs live at each seam.
35
36pub mod backend;
37pub mod clip;
38pub mod cpu;
39pub mod decorated_view;
40pub mod depgraph_layout;
41pub mod l0;
42pub mod metro;
43pub mod model;
44
45#[cfg(feature = "gpu")]
46pub mod gpu;
47
48pub use backend::{Backend, GpuProbe, decide, probe_from_env};
49pub use clip::{GraphClipReport, WidgetRect, pick, render_graph_clipped};
50pub use cpu::{Rendered, render as render_cpu};
51pub use decorated_view::{DecoratedGraphView, downstream_of};
52pub use depgraph_layout::{DepEdge, DepGraphLayout, EdgeClass, LaidEdge, LaidNode};
53// CONS-CORE Phase C: the opt-in L0 kernel adoption (routes nodes/edges through the
54// shared `facett_core::render` Canvas). The default render path
55// ([`render_to_rgba`]) is UNCHANGED — this is the alternative L0 lane.
56pub use l0::{render_to_rgba_l0, lower as lower_to_l0};
57pub use metro::{MetroBranch, MetroLine, MetroMap, MetroStation, MetroView, StationKind};
58pub use model::{
59 BOX_H, BOX_W, Camera, Color, Decorations, GraphEdge, GraphModel, GraphNode, NodeDecoration, Pos,
60};
61
62/// Render `model` (decorated, under `camera`) to a `w × h` straight-RGBA8 frame,
63/// dispatching on the runtime-chosen `backend`. The one call a host makes after
64/// [`decide`].
65///
66/// Both [`Backend`] arms currently rasterize through [`cpu::render`]: the
67/// `GpuVello` arm builds its vello GPU scene (when the `gpu` feature is on) for
68/// the seam, then falls through to the CPU rasterizer for the readback — the GPU
69/// texture readback is the #17 follow-up. The point of this spike is the
70/// architecture + the proven CPU path + the version-aligned GPU seam, not a live
71/// GPU framebuffer on a headless box.
72pub fn render_to_rgba(
73 backend: Backend,
74 model: &GraphModel,
75 decorations: &Decorations,
76 camera: &Camera,
77 width: u32,
78 height: u32,
79 background: Color,
80) -> Rendered {
81 // ── render-lane dispatch emit: record WHICH backend arm this call took ─────
82 // The recurring smear hid in an uninstrumented branch; emit the chosen arm so
83 // the matrix proves both the GpuVello and CpuVello dispatch arms get exercised.
84 #[cfg(feature = "testmatrix")]
85 facett_core::testmatrix::emit(
86 "facett-graphview::render_to_rgba",
87 "render_dispatch",
88 true,
89 &format!("backend={backend:?} nodes={} edges={}", model.nodes.len(), model.edges.len()),
90 );
91 match backend {
92 Backend::GpuVello => {
93 #[cfg(feature = "gpu")]
94 {
95 // Build the GPU scene (proves the vello-0.9 seam compiles); the
96 // texture render+readback is the #17 follow-up, so we hand back
97 // the CPU raster for now to keep one pixel contract.
98 let _scene = gpu::build_scene(model, decorations, camera);
99 }
100 cpu::render(model, decorations, camera, width, height, background)
101 }
102 Backend::CpuVello => cpu::render(model, decorations, camera, width, height, background),
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 fn sample_graph() -> GraphModel {
111 let nodes = vec![
112 GraphNode {
113 id: "a".into(),
114 label: "alpha".into(),
115 fill: Color::rgb(60, 90, 160),
116 stroke: Color::WHITE,
117 pos: Pos::new(0.0, 0.0),
118 },
119 GraphNode {
120 id: "b".into(),
121 label: "beta".into(),
122 fill: Color::rgb(60, 140, 90),
123 stroke: Color::WHITE,
124 pos: Pos::new(300.0, -80.0),
125 },
126 GraphNode {
127 id: "c".into(),
128 label: "gamma".into(),
129 fill: Color::rgb(160, 90, 60),
130 stroke: Color::WHITE,
131 pos: Pos::new(300.0, 80.0),
132 },
133 ];
134 let edges = vec![
135 GraphEdge { from: "a".into(), to: "b".into(), color: Color::rgb(200, 200, 220), dashed: false, label: None },
136 GraphEdge { from: "a".into(), to: "c".into(), color: Color::rgb(200, 200, 220), dashed: true, label: None },
137 ];
138 GraphModel { nodes, edges }
139 }
140
141 #[test]
142 fn cpu_render_produces_nonblank_frame() {
143 let model = sample_graph();
144 let cam = Camera::fit(&model, 640.0, 480.0, 40.0);
145 let backend = decide(GpuProbe::cpu_only());
146 assert_eq!(backend, Backend::CpuVello);
147 let frame = render_to_rgba(
148 backend,
149 &model,
150 &Decorations::default(),
151 &cam,
152 640,
153 480,
154 Color::rgb(18, 20, 28),
155 );
156 assert_eq!(frame.rgba.len(), 640 * 480 * 4);
157 // Non-blank: more than one distinct colour drew (chips + edges over bg).
158 let mut seen = std::collections::HashSet::new();
159 for px in frame.rgba.chunks_exact(4) {
160 seen.insert([px[0], px[1], px[2]]);
161 if seen.len() > 3 {
162 break;
163 }
164 }
165 assert!(seen.len() > 3, "vello_cpu drew a real graph, not a flat pane");
166 }
167
168 #[test]
169 fn camera_fit_centers_world() {
170 let model = sample_graph();
171 let cam = Camera::fit(&model, 640.0, 480.0, 40.0);
172 // The world-bounds centre should land near the viewport centre.
173 let (min_x, min_y, max_x, max_y) = model.world_bounds().unwrap();
174 let (wcx, wcy) = ((min_x + max_x) * 0.5, (min_y + max_y) * 0.5);
175 let (sx, sy) = cam.project(Pos::new(wcx, wcy));
176 assert!((sx - 320.0).abs() < 1.0, "x centered, got {sx}");
177 assert!((sy - 240.0).abs() < 1.0, "y centered, got {sy}");
178 }
179}