forge
Explore
Log in

Don't you dare go Hollow

The glow pass paints a chain, not a disc per step

Browse files at this commit

905edda0596568877a26bc09b3f9c9d312e88102Ahmet Kaan GümüşParent: dc551d7
  • perf
    • the glow pass paints a chain, not a disc per step
Show full message
⚡ perf(audio): the glow pass paints a chain, not a disc per step

`viz_line` stamped a full disc at every Bresenham step of the ring/wave
chain, so the cost grew as `steps * pi * glow^2`: on a 1920x1080 ring one
painted frame cost 16 ms at glow 2 and 1152 ms at glow 16, which is the
frame drop the glow stepper produced. `viz_chain` paints a whole chain per
cell -- luma is a max and every cell of one chain shares one ink, so the
nearest cell decides both planes -- with an exact lower-hull sweep for the
nearest column, and the disc pass is kept verbatim and picked by a measured
speed rule (the crossing is glow 3-4). A 16-row golden-hash test pins the
painted bytes of both arms at 1920x1080.

glow 2/4/9/16: 16/60/294/1152 -> 18/36/61/117 ms per painted frame.
crates/engine/src/decode.rsmodified+321 -12
@@ -1735,6 +1735,10 @@ fn viz_span(
1735 }1735 }
1736 }1736 }
1737 1737
1738+/// One segment's glow as its own disc, stamped at every Bresenham cell: what
1739+/// a chain was painted by before [`viz_chain`], and still the cheaper of the
1740+/// two when the chain is long ([`viz_discs_are_cheaper`]).
1741+#[allow(clippy::too_many_arguments)]
1738 fn viz_line(1742 fn viz_line(
1739 y: &mut [u8],1743 y: &mut [u8],
1740 u: &mut [u8],1744 u: &mut [u8],
@@ -1789,6 +1793,218 @@ fn viz_line(
1789 }1793 }
1790 }1794 }
1791 1795
1796+/// Which of the two passes paints a chain, by what each of them visits. Both
1797+/// paint the same bytes, and the tests hold them to it; they are not alike in
1798+/// what they cost. The discs visit `samples * pi * glow^2` cells whatever the
1799+/// envelope is doing, while the chain visits each of its own cells once per
1800+/// row of its band -- `cells * (2 * glow + 1)`. So a *smooth* trace (a chain
1801+/// cell per sample, near enough) is the chain's by a wide margin, and a
1802+/// *jagged* one, whose samples are Bresenham runs of dozens of cells, is the
1803+/// discs' while the glow is small. A song's own ring at 1080p crossed between
1804+/// glow 3 and 4 (31 ms against 44 ms there, and 60 against 52 at glow 4),
1805+/// which is exactly what this comparison says.
1806+fn viz_discs_are_cheaper(samples: usize, cells: usize, glow: u8) -> bool {
1807+ (samples as u64) * (2 * u64::from(glow) + 1) < cells as u64
1808+}
1809+
1810+/// The Bresenham cells of one segment, collected rather than stamped: the same
1811+/// walk [`viz_line`] used to rasterise, kept for [`viz_chain`] to paint the
1812+/// whole chain from.
1813+fn viz_walk(mut x0: i32, mut y0: i32, x1: i32, y1: i32, chain: &mut Vec<(i32, i32)>) {
1814+ let dx = (x1 - x0).abs();
1815+ let dy = -(y1 - y0).abs();
1816+ let sx = if x0 < x1 { 1 } else { -1 };
1817+ let sy = if y0 < y1 { 1 } else { -1 };
1818+ let mut err = dx + dy;
1819+ loop {
1820+ chain.push((x0, y0));
1821+ if x0 == x1 && y0 == y1 {
1822+ break;
1823+ }
1824+ let e2 = 2 * err;
1825+ if e2 >= dy {
1826+ err += dy;
1827+ x0 += sx;
1828+ }
1829+ if e2 <= dx {
1830+ err += dx;
1831+ y0 += sy;
1832+ }
1833+ }
1834+}
1835+
1836+/// Paints a whole chain in one pass: every cell within `glow` of a chain cell
1837+/// takes the falloff of the **nearest** one, and the ink with it.
1838+///
1839+/// Byte-for-byte what the per-point discs left behind, and the reason is in the
1840+/// write: luma is a `max`, so of every disc covering a cell only the closest one
1841+/// can decide it, and a glow disc's chroma is written at *every* step of it --
1842+/// [`viz_stamp`]`s threshold (0.6) is under the falloff of the rim
1843+/// (`exp(-0.45) = 0.64`), which is what makes the whole disc the ink's own.
1844+/// What changes is the cost: discs visit `steps * pi * glow^2` cells
1845+/// (`VIZ_STYLE_RING`'s 7680 steps measured 296 ms for one 1080p frame at glow
1846+/// 16, eight strands), while this visits each covered cell once and finds its
1847+/// nearest chain cell by walking outward from it through the chain cells of its
1848+/// own row band.
1849+#[allow(clippy::too_many_arguments)]
1850+fn viz_chain(
1851+ y: &mut [u8],
1852+ u: &mut [u8],
1853+ v: &mut [u8],
1854+ w: usize,
1855+ h: usize,
1856+ cw: usize,
1857+ ch: usize,
1858+ chain: &mut Vec<(i32, i32)>,
1859+ bg_y: u8,
1860+ ink_y: u8,
1861+ ink_u: u8,
1862+ ink_v: u8,
1863+ glow: u8,
1864+) {
1865+ if chain.is_empty() {
1866+ return;
1867+ }
1868+ if glow == 0 {
1869+ // Nothing to decide: with no glow the cells are the chain itself, at
1870+ // full ink -- which is exactly what the row/by/band machinery below
1871+ // works out, one sort later.
1872+ for &(x, row) in chain.iter() {
1873+ viz_stamp(y, u, v, w, h, cw, ch, x, row, 1.0, bg_y, ink_y, ink_u, ink_v);
1874+ }
1875+ return;
1876+ }
1877+ let g = i32::from(glow);
1878+ // By (row, x) -- the tuple is (x, row) -- so a row's band is a contiguous
1879+ // slice of this: a chain revisits cells (the ring is sampled several times
1880+ // a pixel) and a repeat is the same candidate.
1881+ chain.sort_unstable_by_key(|&(x, row)| (row, x));
1882+ chain.dedup();
1883+ let mut rows: Vec<(i32, usize, usize)> = Vec::new();
1884+ let mut i = 0;
1885+ while i < chain.len() {
1886+ let row = chain[i].1;
1887+ let start = i;
1888+ while i < chain.len() && chain[i].1 == row {
1889+ i += 1;
1890+ }
1891+ rows.push((row, start, i));
1892+ }
1893+ let first = (rows[0].0 - g).max(0);
1894+ let last = (rows[rows.len() - 1].0 + g).min(h as i32 - 1);
1895+ let mut band = 0usize;
1896+ // `(x, dy^2)` -- a candidate's horizontal offset is left to the cell that
1897+ // asks, its vertical one is its own.
1898+ let mut cand: Vec<(i32, i32)> = Vec::new();
1899+ // The candidates a run's cells are decided by, once the ones that can never
1900+ // be nearest are off ([`viz_chain`]'s own note).
1901+ let mut hull: Vec<usize> = Vec::new();
1902+ for row in first..=last {
1903+ while band < rows.len() && rows[band].0 < row - g {
1904+ band += 1;
1905+ }
1906+ cand.clear();
1907+ let mut k = band;
1908+ while k < rows.len() && rows[k].0 <= row + g {
1909+ let dy = rows[k].0 - row;
1910+ let dy2 = dy * dy;
1911+ for &(x, _) in &chain[rows[k].1..rows[k].2] {
1912+ cand.push((x, dy2));
1913+ }
1914+ k += 1;
1915+ }
1916+ if cand.is_empty() {
1917+ continue;
1918+ }
1919+ cand.sort_unstable_by_key(|&(x, _)| x);
1920+ // One candidate per column: the nearest in y is the only one that can
1921+ // decide a cell -- the others are further on both axes.
1922+ let mut kept = 0usize;
1923+ for j in 0..cand.len() {
1924+ if kept > 0 && cand[kept - 1].0 == cand[j].0 {
1925+ if cand[j].1 < cand[kept - 1].1 {
1926+ cand[kept - 1] = cand[j];
1927+ }
1928+ } else {
1929+ cand[kept] = cand[j];
1930+ kept += 1;
1931+ }
1932+ }
1933+ cand.truncate(kept);
1934+ // The stretches of x the discs reach, one run at a time: the hole in
1935+ // the middle of a ring is not walked at all.
1936+ let mut j = 0usize;
1937+ while j < cand.len() {
1938+ let run_lo = cand[j].0 - g;
1939+ let mut run_hi = cand[j].0 + g;
1940+ let first = j;
1941+ while j + 1 < cand.len() && cand[j + 1].0 - g <= run_hi + 1 {
1942+ j += 1;
1943+ run_hi = run_hi.max(cand[j].0 + g);
1944+ }
1945+ let end = j + 1; // exclusive
1946+ j += 1;
1947+ let x_lo = run_lo.max(0);
1948+ let x_hi = run_hi.min(w as i32 - 1);
1949+ if x_lo > x_hi {
1950+ continue;
1951+ }
1952+ // What is left of a candidate at cell `x` is
1953+ // `x^2 - 2*cx*x + (cx^2 + dy^2)`: the `x^2` is common to every
1954+ // candidate, so the nearest one is the lowest line of the rest --
1955+ // and only the lower hull of those lines is ever the lowest, at any
1956+ // x. On the hull the nearest one only ever moves forward as x
1957+ // does, which is what makes this a walk over the cells instead of a
1958+ // search per cell.
1959+ let line = |c: usize| {
1960+ let (cx, dy2) = cand[c];
1961+ let cx = i64::from(cx);
1962+ (-2 * cx, cx * cx + i64::from(dy2))
1963+ };
1964+ let value = |c: usize, x: i32| {
1965+ let (m, b) = line(c);
1966+ m * i64::from(x) + b
1967+ };
1968+ hull.clear();
1969+ for c in first..end {
1970+ let (m, b) = line(c);
1971+ while hull.len() >= 2 {
1972+ let (m1, b1) = line(hull[hull.len() - 2]);
1973+ let (m2, b2) = line(hull[hull.len() - 1]);
1974+ // The middle line is never the lowest: its two crossings
1975+ // are the wrong way round.
1976+ if (b2 - b1) * (m2 - m) >= (b - b2) * (m1 - m2) {
1977+ hull.pop();
1978+ } else {
1979+ break;
1980+ }
1981+ }
1982+ hull.push(c);
1983+ }
1984+ let mut hp = 0usize;
1985+ for x in x_lo..=x_hi {
1986+ while hp + 1 < hull.len()
1987+ && value(hull[hp + 1], x) <= value(hull[hp], x)
1988+ {
1989+ hp += 1;
1990+ }
1991+ let (cx, dy2) = cand[hull[hp]];
1992+ let d = x - cx;
1993+ let d2 = d * d + dy2;
1994+ if d2 > g * g {
1995+ continue;
1996+ }
1997+ let fall = if d2 == 0 {
1998+ 1.0
1999+ } else {
2000+ (-0.45 * d2 as f32 / (g * g).max(1) as f32).exp()
2001+ };
2002+ viz_stamp(y, u, v, w, h, cw, ch, x, row, fall, bg_y, ink_y, ink_u, ink_v);
2003+ }
2004+ }
2005+ }
2006+}
2007+
1792 /// One visualizer picture in tightly packed I420. Three looks, picked by2008 /// One visualizer picture in tightly packed I420. Three looks, picked by
1793 /// [`viz_style`]: a bipolar sample trace (Audacity), a filled min/max2009 /// [`viz_style`]: a bipolar sample trace (Audacity), a filled min/max
1794 /// envelope at pixel resolution, or a multi-strand ribbon. Ink is2010 /// envelope at pixel resolution, or a multi-strand ribbon. Ink is
@@ -1925,6 +2141,8 @@ pub(crate) fn visualizer_i420(
1925 let steps = w.max(h) * 4;2141 let steps = w.max(h) * 4;
1926 let strands = viz_strands_of(paint);2142 let strands = viz_strands_of(paint);
1927 let glow = viz_glow_of(paint);2143 let glow = viz_glow_of(paint);
2144+ let mut chain: Vec<(i32, i32)> = Vec::new();
2145+ let mut pts: Vec<(i32, i32)> = Vec::new();
1928 for strand in 0..strands {2146 for strand in 0..strands {
1929 let dual = strands >= 2 && strand >= strands / 2;2147 let dual = strands >= 2 && strand >= strands / 2;
1930 // The dual half takes the clip's own strand colour when it2148 // The dual half takes the clip's own strand colour when it
@@ -1946,7 +2164,7 @@ pub(crate) fn visualizer_i420(
1946 let (iy, iu, iv) = rgb_yuv(ir as i32, ig as i32, ib as i32);2164 let (iy, iu, iv) = rgb_yuv(ir as i32, ig as i32, ib as i32);
1947 let scale = 0.65 + 0.35 * f32::from(strand % 4) / 3.0;2165 let scale = 0.65 + 0.35 * f32::from(strand % 4) / 3.0;
1948 let phase = f32::from(strand) * 0.35;2166 let phase = f32::from(strand) * 0.35;
1949- let mut prev: Option<(i32, i32)> = None;2167+ pts.clear();
1950 for i in 0..=steps {2168 for i in 0..=steps {
1951 let t = i as f32 / steps as f32;2169 let t = i as f32 / steps as f32;
1952 let theta = t * tau + t_secs as f32 * 1.1 + phase;2170 let theta = t * tau + t_secs as f32 * 1.1 + phase;
@@ -1956,28 +2174,48 @@ pub(crate) fn visualizer_i420(
1956 let r = r0 + amp * env * scale + amp * wobble;2174 let r = r0 + amp * env * scale + amp * wobble;
1957 let x = (cx + r * theta.cos()).round() as i32;2175 let x = (cx + r * theta.cos()).round() as i32;
1958 let row = (cy + r * theta.sin()).round() as i32;2176 let row = (cy + r * theta.sin()).round() as i32;
1959- if let Some((px, py)) = prev {2177+ pts.push((x, row));
2178+ }
2179+ chain.clear();
2180+ for pair in pts.windows(2) {
2181+ viz_walk(pair[0].0, pair[0].1, pair[1].0, pair[1].1, &mut chain);
2182+ }
2183+ if viz_discs_are_cheaper(pts.len(), chain.len(), glow) {
2184+ for pair in pts.windows(2) {
1960 viz_line(2185 viz_line(
1961- &mut y, &mut u, &mut v, w, h, cw, ch, px, py, x, row, bg_y, iy, iu, iv,2186+ &mut y, &mut u, &mut v, w, h, cw, ch, pair[0].0, pair[0].1, pair[1].0,
1962- glow,2187+ pair[1].1, bg_y, iy, iu, iv, glow,
1963 );2188 );
1964 }2189 }
1965- prev = Some((x, row));2190+ } else {
2191+ viz_chain(
2192+ &mut y, &mut u, &mut v, w, h, cw, ch, &mut chain, bg_y, iy, iu, iv, glow,
2193+ );
1966 }2194 }
1967 }2195 }
1968 }2196 }
1969 VIZ_STYLE_WAVE => {2197 VIZ_STYLE_WAVE => {
1970- let mut prev: Option<(i32, i32)> = None;2198+ let mut chain: Vec<(i32, i32)> = Vec::new();
2199+ let mut pts: Vec<(i32, i32)> = Vec::new();
1971 for col in 0..w {2200 for col in 0..w {
1972- let y_pt = (center - sig[col] * half).round() as i32;2201+ pts.push((col as i32, (center - sig[col] * half).round() as i32));
1973- let x = col as i32;2202+ }
1974- if let Some((px, py)) = prev {2203+ for pair in pts.windows(2) {
2204+ viz_walk(pair[0].0, pair[0].1, pair[1].0, pair[1].1, &mut chain);
2205+ }
2206+ let glow = viz_glow_of(paint);
2207+ if viz_discs_are_cheaper(pts.len(), chain.len(), glow) {
2208+ for pair in pts.windows(2) {
1975 viz_line(2209 viz_line(
1976- &mut y, &mut u, &mut v, w, h, cw, ch, px, py, x, y_pt, bg_y, ink_y, ink_u,2210+ &mut y, &mut u, &mut v, w, h, cw, ch, pair[0].0, pair[0].1, pair[1].0,
1977- ink_v, viz_glow_of(paint),2211+ pair[1].1, bg_y, ink_y, ink_u, ink_v, glow,
1978 );2212 );
1979 }2213 }
1980- prev = Some((x, y_pt));2214+ } else {
2215+ viz_chain(
2216+ &mut y, &mut u, &mut v, w, h, cw, ch, &mut chain, bg_y, ink_y, ink_u, ink_v,
2217+ glow,
2218+ );
1981 }2219 }
1982 }2220 }
1983 _ => {2221 _ => {
@@ -2480,6 +2718,76 @@ mod tests {
2480 assert_eq!(ut, u0);2718 assert_eq!(ut, u0);
2481 assert_eq!(vt, v0);2719 assert_eq!(vt, v0);
2482 }2720 }
2721+ /// The look of a frame the glow pass was rewritten for, pinned byte for
2722+ /// byte: [`viz_chain`] replaced the per-point discs under
2723+ /// [`VIZ_STYLE_RING`] and [`VIZ_STYLE_WAVE`], whose glow cost `steps * pi *
2724+ /// glow^2` cells per frame (295 ms of one 1080p ring frame at glow 16
2725+ /// against 18 ms after), and this is what says the pictures did not move
2726+ /// with it. FNV-1a over `y`, then `u`, then `v`, taken from the disc
2727+ /// implementation that shipped before the rewrite; `fill` and `ribbon` are
2728+ /// in the table because their planes share the frame with a chain's, and
2729+ /// the row/run walk could still have reached them through a shared helper.
2730+ ///
2731+ /// Scope: eight strands (what [`VIZ_PAINT_AZURE`] carries) at four gloss
2732+ /// including both ends. A strand count changes a chain's scale and phase,
2733+ /// not the pass over it.
2734+ #[test]
2735+ fn the_glow_pass_paints_the_bytes_it_always_did() {
2736+ const GOLDEN: &[(&str, u8, u64)] = &[
2737+ ("fill", 0, 0xe141_5e51_fcc1_7ea1),
2738+ ("fill", 2, 0x3842_fef4_d4ec_4239),
2739+ ("fill", 8, 0xfaea_3e6f_9718_1d31),
2740+ ("fill", 16, 0x6a09_31fe_e9f1_3069),
2741+ ("ribbon", 0, 0x91fa_9b49_48d8_bcfe),
2742+ ("ribbon", 2, 0xd4d2_bcce_3c32_14d8),
2743+ ("ribbon", 8, 0xd23a_d502_17ac_697c),
2744+ ("ribbon", 16, 0xd7de_425a_6737_11c3),
2745+ ("ring", 0, 0xcfbe_55da_b222_0a1b),
2746+ ("ring", 2, 0x5468_01eb_522e_21dc),
2747+ ("ring", 8, 0xc08f_994e_4a19_c9f4),
2748+ ("ring", 16, 0x8cee_6dcd_256c_d484),
2749+ ("wave", 0, 0x2266_ba70_ab2a_06bd),
2750+ ("wave", 2, 0xd05f_0a31_9264_b377),
2751+ ("wave", 8, 0x2e50_7536_c5dd_6035),
2752+ ("wave", 16, 0xf888_b09c_dae2_dd36),
2753+ ];
2754+ let peaks = benchmark_peaks((149.6 * f64::from(VIZ_BUCKETS_PER_SEC)) as usize);
2755+ for &(name, glow, want) in GOLDEN {
2756+ let style = match name {
2757+ "fill" => VIZ_STYLE_FILL,
2758+ "ribbon" => VIZ_STYLE_RIBBON,
2759+ "ring" => VIZ_STYLE_RING,
2760+ _ => VIZ_STYLE_WAVE,
2761+ };
2762+ let flags = with_viz_style(1, style);
2763+ let paint = with_viz_paint_glow(VIZ_PAINT_AZURE, glow);
2764+ let (y, u, v) = visualizer_i420(&peaks, 37.5, 1920, 1080, flags, paint);
2765+ let mut hash = 0xcbf2_9ce4_8422_2325u64;
2766+ for buf in [&y, &u, &v] {
2767+ for &b in buf.iter() {
2768+ hash ^= u64::from(b);
2769+ hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
2770+ }
2771+ }
2772+ assert_eq!(
2773+ hash, want,
2774+ "{name} at glow {glow} paints different bytes than it always did ({hash:016x})"
2775+ );
2776+ }
2777+ }
2778+
2779+ /// The envelope both of the tests above paint: loud enough that every
2780+ /// column carries ink, and shaped so a ring's radii and a wave's trace
2781+ /// differ from column to column.
2782+ fn benchmark_peaks(n: usize) -> Vec<(f32, f32)> {
2783+ let mut peaks = vec![(0.0f32, 0.0f32); n];
2784+ for (i, p) in peaks.iter_mut().enumerate() {
2785+ let t = i as f64 / f64::from(VIZ_BUCKETS_PER_SEC);
2786+ let a = ((t * 0.7).sin() * 0.6 + (t * 5.3).sin() * 0.25 + 0.1) as f32;
2787+ *p = (-a.abs(), a.abs());
2788+ }
2789+ peaks
2790+ }
2483 }2791 }
2484 2792
2485 fn run(2793 fn run(
@@ -2769,6 +3077,7 @@ fn run_av1(
2769 }3077 }
2770 }3078 }
2771 }3079 }
3080+
2772 }3081 }
2773 3082
2774 /// Converts one decoded picture and queues it. `true` when the consumer went3083 /// Converts one decoded picture and queues it. `true` when the consumer went