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 cpu;
38pub mod l0;
39pub mod model;
40
41#[cfg(feature = "gpu")]
42pub mod gpu;
43
44pub use backend::{Backend, GpuProbe, decide, probe_from_env};
45pub use cpu::{Rendered, render as render_cpu};
46// CONS-CORE Phase C: the opt-in L0 kernel adoption (routes nodes/edges through the
47// shared `facett_core::render` Canvas). The default render path
48// ([`render_to_rgba`]) is UNCHANGED — this is the alternative L0 lane.
49pub use l0::{render_to_rgba_l0, lower as lower_to_l0};
50pub use model::{
51 BOX_H, BOX_W, Camera, Color, Decorations, GraphEdge, GraphModel, GraphNode, NodeDecoration, Pos,
52};
53
54/// Render `model` (decorated, under `camera`) to a `w × h` straight-RGBA8 frame,
55/// dispatching on the runtime-chosen `backend`. The one call a host makes after
56/// [`decide`].
57///
58/// Both [`Backend`] arms currently rasterize through [`cpu::render`]: the
59/// `GpuVello` arm builds its vello GPU scene (when the `gpu` feature is on) for
60/// the seam, then falls through to the CPU rasterizer for the readback — the GPU
61/// texture readback is the #17 follow-up. The point of this spike is the
62/// architecture + the proven CPU path + the version-aligned GPU seam, not a live
63/// GPU framebuffer on a headless box.
64pub fn render_to_rgba(
65 backend: Backend,
66 model: &GraphModel,
67 decorations: &Decorations,
68 camera: &Camera,
69 width: u32,
70 height: u32,
71 background: Color,
72) -> Rendered {
73 match backend {
74 Backend::GpuVello => {
75 #[cfg(feature = "gpu")]
76 {
77 // Build the GPU scene (proves the vello-0.9 seam compiles); the
78 // texture render+readback is the #17 follow-up, so we hand back
79 // the CPU raster for now to keep one pixel contract.
80 let _scene = gpu::build_scene(model, decorations, camera);
81 }
82 cpu::render(model, decorations, camera, width, height, background)
83 }
84 Backend::CpuVello => cpu::render(model, decorations, camera, width, height, background),
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 fn sample_graph() -> GraphModel {
93 let nodes = vec![
94 GraphNode {
95 id: "a".into(),
96 label: "alpha".into(),
97 fill: Color::rgb(60, 90, 160),
98 stroke: Color::WHITE,
99 pos: Pos::new(0.0, 0.0),
100 },
101 GraphNode {
102 id: "b".into(),
103 label: "beta".into(),
104 fill: Color::rgb(60, 140, 90),
105 stroke: Color::WHITE,
106 pos: Pos::new(300.0, -80.0),
107 },
108 GraphNode {
109 id: "c".into(),
110 label: "gamma".into(),
111 fill: Color::rgb(160, 90, 60),
112 stroke: Color::WHITE,
113 pos: Pos::new(300.0, 80.0),
114 },
115 ];
116 let edges = vec![
117 GraphEdge { from: "a".into(), to: "b".into(), color: Color::rgb(200, 200, 220), dashed: false, label: None },
118 GraphEdge { from: "a".into(), to: "c".into(), color: Color::rgb(200, 200, 220), dashed: true, label: None },
119 ];
120 GraphModel { nodes, edges }
121 }
122
123 #[test]
124 fn cpu_render_produces_nonblank_frame() {
125 let model = sample_graph();
126 let cam = Camera::fit(&model, 640.0, 480.0, 40.0);
127 let backend = decide(GpuProbe::cpu_only());
128 assert_eq!(backend, Backend::CpuVello);
129 let frame = render_to_rgba(
130 backend,
131 &model,
132 &Decorations::default(),
133 &cam,
134 640,
135 480,
136 Color::rgb(18, 20, 28),
137 );
138 assert_eq!(frame.rgba.len(), 640 * 480 * 4);
139 // Non-blank: more than one distinct colour drew (chips + edges over bg).
140 let mut seen = std::collections::HashSet::new();
141 for px in frame.rgba.chunks_exact(4) {
142 seen.insert([px[0], px[1], px[2]]);
143 if seen.len() > 3 {
144 break;
145 }
146 }
147 assert!(seen.len() > 3, "vello_cpu drew a real graph, not a flat pane");
148 }
149
150 #[test]
151 fn camera_fit_centers_world() {
152 let model = sample_graph();
153 let cam = Camera::fit(&model, 640.0, 480.0, 40.0);
154 // The world-bounds centre should land near the viewport centre.
155 let (min_x, min_y, max_x, max_y) = model.world_bounds().unwrap();
156 let (wcx, wcy) = ((min_x + max_x) * 0.5, (min_y + max_y) * 0.5);
157 let (sx, sy) = cam.project(Pos::new(wcx, wcy));
158 assert!((sx - 320.0).abs() < 1.0, "x centered, got {sx}");
159 assert!((sy - 240.0).abs() < 1.0, "y centered, got {sy}");
160 }
161}