Skip to main content

merge_spans

Function merge_spans 

Source
pub fn merge_spans(rects: &[PixelRect]) -> Vec<PixelRect>
Expand description

Joins rectangles that sit side by side on the same rows (particles in one cell row), so a frame needs fewer copies.

Examples found in repository?
examples/frame_cost.rs (line 52)
13fn main() {
14    let mut args = std::env::args().skip(1);
15    let (w, h) = args
16        .next()
17        .and_then(|s| {
18            let (a, b) = s.split_once('x')?;
19            Some((a.parse().ok()?, b.parse().ok()?))
20        })
21        .unwrap_or((3024u32, 1964u32));
22    let fps: u64 = args.next().and_then(|a| a.parse().ok()).unwrap_or(12);
23    for activity in [
24        "warm_hands",
25        "stroll",
26        "stargaze",
27        "hike_ridge",
28        "broom_journey",
29    ] {
30        let ctx = ContentContext {
31            enabled_activities: vec![activity.into()],
32            ..ContentContext::default()
33        };
34        let r = compose(40, Aspect::from_size(w, h), &ctx);
35        let made = Instant::now();
36        let pkg = Arc::new(ScenePackage::capture(&r, w, h).expect("scene"));
37        let capture = made.elapsed();
38        let theme = pkg.themes()[0];
39        let mut player = Player::new(pkg.clone(), theme, true).expect("theme");
40        player.set_smooth(true);
41        let t0 = pkg.hero_ms(theme).unwrap_or(0);
42        player.advance(t0);
43        let frames = 10 * fps;
44        let (mut advance, mut pack, mut pixels) = (Duration::ZERO, Duration::ZERO, 0u64);
45        let mut packed: Vec<u8> = Vec::new();
46        for k in 1..=frames {
47            let t = t0 + k * 1000 / fps;
48            let a = Instant::now();
49            let rects = player.advance(t);
50            let b = Instant::now();
51            packed.clear();
52            for r in clairlune_live::gpu::merge_spans(&rects) {
53                let pitch = (r.w as usize).div_ceil(256) * 256;
54                for y in r.y..r.y + r.h {
55                    packed.extend_from_slice(player.index_row(r.x, y, r.w));
56                    let len = packed.len();
57                    packed.resize(len + pitch - r.w as usize, 0);
58                }
59                pixels += u64::from(r.w * r.h);
60            }
61            pack += b.elapsed();
62            advance += b - a;
63        }
64        let per = |d: Duration| d.as_secs_f64() * 1e3 / frames as f64;
65        let cpu = (advance + pack).as_secs_f64() / 10.0 * 100.0;
66        println!(
67            "{activity:<14} {w}x{h} at {fps} fps: advance {:.2} ms, pack {:.2} ms, {:.0} kpx/frame, ~{cpu:.1} % of a core (capture {:.0} ms)",
68            per(advance),
69            per(pack),
70            pixels as f64 / frames as f64 / 1e3,
71            capture.as_secs_f64() * 1e3
72        );
73    }
74}