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 community;
39pub mod cpu;
40pub mod decorated_view;
41pub mod depgraph_layout;
42pub mod fdeb;
43pub mod gpu_layout;
44pub mod l0;
45pub mod metro;
46pub mod model;
47
48#[cfg(feature = "gpu")]
49pub mod gpu;
50
51pub use backend::{Backend, GpuProbe, decide, probe_from_env};
52pub use clip::{GraphClipReport, WidgetRect, pick, render_graph_clipped};
53pub use cpu::{Rendered, render as render_cpu};
54pub use decorated_view::{DecoratedGraphView, downstream_of};
55pub use depgraph_layout::{DepEdge, DepGraphLayout, EdgeClass, LaidEdge, LaidNode};
56// The elite GPU-compute layout engine (#1): CSR streamed from Arrow → force-directed
57// positions that iterate in VRAM (feature `gpu`), with the deterministic CPU fallback
58// always built. See [`gpu_layout`] + `.nornir/elite-graph-engine.md`.
59pub use gpu_layout::{relax_cpu, step_cpu, GraphCsr, LayoutParams, LayoutState};
60// Force-directed edge bundling (#2): the hairball cure — compatible edges attract
61// into shared "information highways". See [`fdeb`].
62pub use fdeb::{bundle, occupancy, BundleParams, BundledEdge};
63// Topological semantic zoom (#3): Louvain communities collapse into meta-nodes when
64// zoomed out and fracture open (injected-clock easing) when zoomed in. See [`community`].
65pub use community::{
66 fracture_t, fractured_positions, louvain, meta_graph, visible_count, Communities, MetaGraph, MetaNode,
67};
68// CONS-CORE Phase C: the opt-in L0 kernel adoption (routes nodes/edges through the
69// shared `facett_core::render` Canvas). The default render path
70// ([`render_to_rgba`]) is UNCHANGED — this is the alternative L0 lane.
71pub use l0::{render_to_rgba_l0, lower as lower_to_l0};
72pub use metro::{MetroBranch, MetroLine, MetroMap, MetroStation, MetroView, StationKind};
73pub use model::{
74 BOX_H, BOX_W, Camera, Color, Decorations, GraphEdge, GraphModel, GraphNode, NodeDecoration, Pos,
75};
76
77/// Render `model` (decorated, under `camera`) to a `w × h` straight-RGBA8 frame,
78/// dispatching on the runtime-chosen `backend`. The one call a host makes after
79/// [`decide`].
80///
81/// Both [`Backend`] arms currently rasterize through [`cpu::render`]: the
82/// `GpuVello` arm builds its vello GPU scene (when the `gpu` feature is on) for
83/// the seam, then falls through to the CPU rasterizer for the readback — the GPU
84/// texture readback is the #17 follow-up. The point of this spike is the
85/// architecture + the proven CPU path + the version-aligned GPU seam, not a live
86/// GPU framebuffer on a headless box.
87pub fn render_to_rgba(
88 backend: Backend,
89 model: &GraphModel,
90 decorations: &Decorations,
91 camera: &Camera,
92 width: u32,
93 height: u32,
94 background: Color,
95) -> Rendered {
96 // ── render-lane dispatch emit: record WHICH backend arm this call took ─────
97 // The recurring smear hid in an uninstrumented branch; emit the chosen arm so
98 // the matrix proves both the GpuVello and CpuVello dispatch arms get exercised.
99 #[cfg(feature = "testmatrix")]
100 facett_core::testmatrix::emit(
101 "facett-graphview::render_to_rgba",
102 "render_dispatch",
103 true,
104 &format!("backend={backend:?} nodes={} edges={}", model.nodes.len(), model.edges.len()),
105 );
106 match backend {
107 Backend::GpuVello => {
108 #[cfg(feature = "gpu")]
109 {
110 // Build the GPU scene (proves the vello-0.9 seam compiles); the
111 // texture render+readback is the #17 follow-up, so we hand back
112 // the CPU raster for now to keep one pixel contract.
113 let _scene = gpu::build_scene(model, decorations, camera);
114 }
115 cpu::render(model, decorations, camera, width, height, background)
116 }
117 Backend::CpuVello => cpu::render(model, decorations, camera, width, height, background),
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 fn sample_graph() -> GraphModel {
126 let nodes = vec![
127 GraphNode {
128 id: "a".into(),
129 label: "alpha".into(),
130 fill: Color::rgb(60, 90, 160),
131 stroke: Color::WHITE,
132 pos: Pos::new(0.0, 0.0),
133 },
134 GraphNode {
135 id: "b".into(),
136 label: "beta".into(),
137 fill: Color::rgb(60, 140, 90),
138 stroke: Color::WHITE,
139 pos: Pos::new(300.0, -80.0),
140 },
141 GraphNode {
142 id: "c".into(),
143 label: "gamma".into(),
144 fill: Color::rgb(160, 90, 60),
145 stroke: Color::WHITE,
146 pos: Pos::new(300.0, 80.0),
147 },
148 ];
149 let edges = vec![
150 GraphEdge { from: "a".into(), to: "b".into(), color: Color::rgb(200, 200, 220), dashed: false, label: None },
151 GraphEdge { from: "a".into(), to: "c".into(), color: Color::rgb(200, 200, 220), dashed: true, label: None },
152 ];
153 GraphModel { nodes, edges }
154 }
155
156 #[test]
157 fn cpu_render_produces_nonblank_frame() {
158 let model = sample_graph();
159 let cam = Camera::fit(&model, 640.0, 480.0, 40.0);
160 let backend = decide(GpuProbe::cpu_only());
161 assert_eq!(backend, Backend::CpuVello);
162 let frame = render_to_rgba(
163 backend,
164 &model,
165 &Decorations::default(),
166 &cam,
167 640,
168 480,
169 Color::rgb(18, 20, 28),
170 );
171 assert_eq!(frame.rgba.len(), 640 * 480 * 4);
172 // Non-blank: more than one distinct colour drew (chips + edges over bg).
173 let mut seen = std::collections::HashSet::new();
174 for px in frame.rgba.chunks_exact(4) {
175 seen.insert([px[0], px[1], px[2]]);
176 if seen.len() > 3 {
177 break;
178 }
179 }
180 assert!(seen.len() > 3, "vello_cpu drew a real graph, not a flat pane");
181 }
182
183 #[test]
184 fn camera_fit_centers_world() {
185 let model = sample_graph();
186 let cam = Camera::fit(&model, 640.0, 480.0, 40.0);
187 // The world-bounds centre should land near the viewport centre.
188 let (min_x, min_y, max_x, max_y) = model.world_bounds().unwrap();
189 let (wcx, wcy) = ((min_x + max_x) * 0.5, (min_y + max_y) * 0.5);
190 let (sx, sy) = cam.project(Pos::new(wcx, wcy));
191 assert!((sx - 320.0).abs() < 1.0, "x centered, got {sx}");
192 assert!((sy - 240.0).abs() < 1.0, "y centered, got {sy}");
193 }
194}