perf(slint): immersive Spectrum - collapse ~2240 cubelet nodes to ~28 gradient bars

The cubelet port instantiated ~80 Rectangles per column x2 mirror x14 bins
(~2240 retained scene-graph nodes) whose `visible` flipped every frame off an
animated `shown-cubes`. Audio updates at 30fps kept that animation from ever
settling, so ~2240 visible-bindings re-evaluated per frame and femtovg
re-rasterized hundreds of tiny rects - ~5fps and a large CPU spike. Tauri is
cheap because it is immediate-mode Canvas2D (fillRect), not a retained tree.

Fix: one solid gradient bar per column (2 nodes/pair, ~28 total) with an
animated height. Same geometry (28 mirrored columns, baseY 0.85H, maxBarHeight
0.7H, widths/offsets), same data binding (VisualizerState.bars, bounds-safe),
same cyan->purple default gradient (now a vertical fill instead of per-cube).
Dropped the discrete-cubelet texture for the perf win; it can return cheaply as
a static stripe overlay if wanted. Removed the unused cube-indices array.

cc
This commit is contained in:
vicrodh committed 2026-06-13 10:34:41 -06:00
1 parent fa7aa46ce1
commit 03dfd66101
1 file changed
+28 -56
@@ -55,10 +55,19 @@
import { QualityBadgeFull } from "../primitives/QualityBadgeFull.slint";
import { NowPlayingState, VisualizerState } from "../state.slint";
// One mirrored pair of cube stacks for a single active source bin. Draws the
// LEFT column at `x-left` and the RIGHT (mirror) column at `x-right`, both from
// the same amplitude. Hand-positioned cubelets; a fixed MAX_CUBES with
// `visible: idx < num-cubes` (no model churn, no geometry watchers).
// One mirrored pair of bars for a single active source bin. Draws the LEFT
// column at `x-left` and the RIGHT (mirror) column at `x-right`, both from the
// same amplitude.
//
// PERF: this used to be ~80 cubelet Rectangles PER column (x2 mirror x14 bins =
// ~2240 retained nodes) whose `visible` flipped every frame off an animated
// `shown-cubes` — a retained-scene-graph churn that femtovg cannot keep up with
// (~5fps, CPU spike). Tauri is cheap because it is immediate-mode Canvas2D
// (fillRect), not a retained tree. The fix: ONE solid gradient bar per column
// (2 nodes/pair, ~28 total) with an animated height. The discrete-cubelet
// texture is dropped for the perf win (can return cheaply via a static stripe
// overlay if wanted). Cyan->purple is the Svelte default gradient
// (primary rgb(0,220,200) at base -> secondary rgb(150,50,255) at the tip).
component BarPair inherits Rectangle {
// Geometry passed in from the panel (computed from viewport-width).
in property <length> x-left;
@@ -68,54 +77,33 @@ component BarPair inherits Rectangle {
in property <length> max-bar-height;
// 0..1 amplitude for this bin (already smoothed + clamped in Rust).
in property <float> amp;
// Fixed index array [0..MAX_CUBES-1] supplied once by the panel; iterated
// with `visible: c < shown-cubes` so the model never churns.
in property <[int]> cube-indices;
// Cube metrics (GPU/hwaccel path): 6px cube + 2px gap = 8px unit.
property <length> cube-h: 6px;
property <length> cube-gap: 2px;
property <length> cube-unit: 8px; // cube-h + cube-gap
// barHeight = amp * maxBarHeight; numCubes = floor(barHeight / cubeUnit).
// barHeight = amp * maxBarHeight. Animated for an inter-frame settle (NOT a
// re-smooth — the Rust EMA already smoothed; this only eases the step).
property <length> bar-height: root.amp * root.max-bar-height;
// Animated cube threshold for an inter-frame settle (NOT a re-smooth — the
// Rust EMA already smoothed the data; this only eases the discrete step).
property <float> shown-cubes: Math.floor(root.bar-height / root.cube-unit);
animate shown-cubes { duration: 90ms; easing: ease; }
// Default-color gradient (Svelte defaults): cyan primary -> purple secondary.
// gradientRatio(c) = min(c/20, 1). channel = primary + ratio*(secondary-primary).
// primary = rgb(0,220,200) secondary = rgb(150,50,255).
pure function cube-color(c: int) -> color {
return rgb(
0 + Math.min(c / 20.0, 1.0) * (150 - 0),
220 + Math.min(c / 20.0, 1.0) * (50 - 220),
200 + Math.min(c / 20.0, 1.0) * (255 - 200));
}
animate bar-height { duration: 90ms; easing: ease; }
width: 0px; // purely a positioning host; children are absolute.
height: 0px;
// LEFT column.
for c in root.cube-indices: Rectangle {
// LEFT bar. Grows UP from base-y (y derives from the animated height).
Rectangle {
x: root.x-left;
// cube top-left Y = baseY - (c+1)*(cubeH+cubeGap).
y: root.base-y - (c + 1) * root.cube-unit;
width: root.bar-width;
height: root.cube-h;
background: root.cube-color(c);
visible: c < root.shown-cubes;
height: root.bar-height;
y: root.base-y - self.height;
// cyan at base -> purple at the tip (Svelte default). If it looks
// inverted, swap the two stops.
background: @linear-gradient(0deg, #00dcc8 0%, #9632ff 100%);
}
// RIGHT (mirror) column — same source bin, same per-cube color.
for c in root.cube-indices: Rectangle {
// RIGHT (mirror) bar — same source bin.
Rectangle {
x: root.x-right;
y: root.base-y - (c + 1) * root.cube-unit;
width: root.bar-width;
height: root.cube-h;
background: root.cube-color(c);
visible: c < root.shown-cubes;
height: root.bar-height;
y: root.base-y - self.height;
background: @linear-gradient(0deg, #00dcc8 0%, #9632ff 100%);
}
}
@@ -142,21 +130,6 @@ export component SpectrumPanel inherits Rectangle {
// these; the LEFT column sits at visual index i, the RIGHT at (28-1-i).
property <[int]> active-bins: [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
// Fixed cube-index array (length MAX_CUBES). MAX_CUBES = 80 covers
// maxBarHeight up to ~640px (viewport-height ~915px) with the 8px unit;
// beyond that the tallest cubes simply don't render (visible:false), no
// panic, no churn. NEVER drive cube count from a geometry-changed handler.
property <[int]> cube-indices: [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79
];
// --- Bars (drawn FIRST, behind the song card) --------------------------
// One BarPair per active bin; each draws its own left + mirrored-right
// stack. The amplitude is read with a SAFE bounds check (bars may be the
@@ -170,7 +143,6 @@ export component SpectrumPanel inherits Rectangle {
bar-width: root.bar-width;
base-y: root.base-y;
max-bar-height: root.max-bar-height;
cube-indices: root.cube-indices;
amp: VisualizerState.bars.length > src
? Math.max(0.0, Math.min(1.0, VisualizerState.bars[src]))
: 0.0;