From 02c831576a1d795d68c4e509547642ab2a166956 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 03:15:54 +0000 Subject: [PATCH] Smoother on Xorg and Live Starfield: draw one frame ahead, cache a backdrop's unchanging start Tested on a Vast GTX 1650 rig (8-core Xeon E5-2620 v4, NVIDIA Xorg, --x11 --fullscreen): GPU detection says hardware (nvidia), CHANDRA_RENDER=software switches. - Frame pacing: at most one frame is drawn ahead of the output instead of waiting until the output is ready. Waiting serialised drawing with the X server's copy of the last frame; overview on Xorg went from 24-27 fps to 41-48 fps (before the first round: 16-17 fps). - MoonBit host: a backdrop whose ops change every frame but begin the same (a desktop module: Live Starfield's sky and nebulae under moving stars) keeps that common start as cached pixels and draws only the rest over a copy of it. Starfield raster 13 -> 1.7 ms a frame and CPU 46% -> 11% in the cloud; it no longer clears, redraws and re-saves the whole backdrop every frame. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hkTQp5uN5FtwpzrXUA --- compositor/app/app.mbt | 12 ++++---- docs/rendering.md | 13 ++++++--- wasmhost/bin/raster.mbt | 65 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 74 insertions(+), 16 deletions(-) diff --git a/compositor/app/app.mbt b/compositor/app/app.mbt index 64a51cc..471260c 100644 --- a/compositor/app/app.mbt +++ b/compositor/app/app.mbt @@ -317,9 +317,9 @@ pub fn run(args : Array[String]) -> Int { k.set_cursor_shape(comp.desired_cursor()) // The host draws; only a changed desktop is sent, and the frame is shown by the host // straight from its framebuffer. - // Draw only when the display can take the frame (no page flip in flight): a frame - // drawn meanwhile would never be seen, and drawing it costs a whole frame's CPU. - let changed = if k.frame_ready() || comp.screen_off || pacer.overdue() { + // Draw at most one frame ahead of the display: while a drawn frame waits for the + // page flip in flight, another one would never be seen and costs a whole frame's CPU. + let changed = if !need_present || k.frame_ready() || comp.screen_off || pacer.overdue() { draw_frame(comp, trace, pacer) } else { false @@ -404,8 +404,8 @@ pub fn run(args : Array[String]) -> Int { comp.handle_input(ev, now_ms) } n.set_cursor_shape(comp.desired_cursor()) - // As on a display: draw when the host compositor can take the frame. - if (n.frame_ready() || pacer.overdue()) && draw_frame(comp, trace, pacer) { + // As on a display: at most one drawn frame waits for the host compositor. + if (!need_present || n.frame_ready() || pacer.overdue()) && draw_frame(comp, trace, pacer) { need_present = true busy = true } @@ -445,7 +445,7 @@ pub fn run(args : Array[String]) -> Int { comp.handle_input(ev, now_ms) } x.set_cursor_shape(comp.desired_cursor()) - if (x.frame_ready() || pacer.overdue()) && draw_frame(comp, trace, pacer) { + if (!need_present || x.frame_ready() || pacer.overdue()) && draw_frame(comp, trace, pacer) { need_present = true busy = true } diff --git a/docs/rendering.md b/docs/rendering.md index 912a47c..3b0098c 100644 --- a/docs/rendering.md +++ b/docs/rendering.md @@ -32,10 +32,15 @@ most ~30 frames a second however often it asks. ## What makes animations smooth everywhere -- Frames are drawn only when the output can take them: on KMS when no page flip is in flight, - nested and on X11 when the previous frame was shown. A frame drawn while one is still on its way - would never be seen (the KMS loop runs at 125 Hz), and it costs a whole frame of CPU. An output - that never says it is ready (a hidden nested window) still gets a frame every 250 ms. +- At most one frame is drawn ahead of the output: while a drawn frame waits for the page flip in + flight (KMS) or for the previous frame to be shown (nested, X11), no other one is drawn. Before, + frames drawn meanwhile were thrown away (the KMS loop runs at 125 Hz), each costing a whole + frame of CPU. Drawing the next frame while the X server copies the last one keeps both busy: + waiting for the copy first halved the frame rate on Xorg. An output that never says it is ready + (a hidden nested window) still gets a frame every 250 ms. +- A backdrop (wallpaper or desktop module) whose ops change every frame but begin the same, like + Live Starfield's sky and nebulae under moving stars, keeps that common start cached as pixels and + draws only the rest over a copy of it (MoonBit host). - Animations are timed by the clock, so a slow frame makes the next one jump ahead instead of slowing the animation down. - The rasteriser blends two colour channels at a time (16-bit lanes, exact division by 255), diff --git a/wasmhost/bin/raster.mbt b/wasmhost/bin/raster.mbt index 2b89f57..153f085 100644 --- a/wasmhost/bin/raster.mbt +++ b/wasmhost/bin/raster.mbt @@ -528,8 +528,33 @@ struct Frame { ///| The last frame drawn, and the wallpaper ops the cached backdrop was drawn from. let prev_frame : Ref[Frame?] = Ref(None) +///| The backdrop ops last sent (`wallpaper`, None before any), and how many of them, from the +/// start, the cached backdrop pixels hold. Usually all of them; when the ops change every frame +/// but begin the same (a desktop module such as Live Starfield: the sky and nebulae stay, the stars +/// move), only that common start is kept, and the rest is drawn over a copy of it each frame. +let backdrop_ops : Ref[Array[@wt.Val]?] = Ref(None) + ///| -let backdrop_ops : Ref[@wt.Val?] = Ref(None) +let backdrop_kept : Ref[Int] = Ref(0) + +///| How many ops `a` and `b` share from the start. +fn common_prefix(a : Array[@wt.Val], b : Array[@wt.Val]) -> Int { + let n = @cmp.minimum(a.length(), b.length()) + let mut i = 0 + while i < n && a[i] == b[i] { + i = i + 1 + } + i +} + +///| The backdrop ops past what the cached pixels hold, drawn over them (within `clip`). +fn draw_backdrop_rest(clip : R?) -> Unit { + match backdrop_ops.val { + Some(ops) if backdrop_kept.val < ops.length() => + draw_ops(ops[backdrop_kept.val:].to_array(), 0, 0, clip) + _ => () + } +} ///| Room around a window for its drop shadow (4 px right, 6 px down) and a little slack. const SHADOW_PAD : Int = 8 @@ -668,7 +693,7 @@ fn submit(v : @wt.Val) -> Unit { let backdrop_ready = rs_has_backdrop() != 0 && (match (f.wallpaper, backdrop_ops.val) { (None, Some(_)) => true - (Some(ops), Some(o)) => o == ops + (Some(ops), Some(o)) => o == ops.to_list() _ => false }) let clip : R? = match prev_frame.val { @@ -688,7 +713,8 @@ fn submit(v : @wt.Val) -> Unit { } _ => None } - if !resized && !backdrop_ready { + // (A new backdrop clears the buffer itself, below, unless its kept start can be copied in.) + if !resized && !backdrop_ready && f.wallpaper is None { rs_clear() } last_drawn.val = match clip { @@ -718,15 +744,42 @@ fn submit(v : @wt.Val) -> Unit { if backdrop_ready { set_clip(clip) rs_copy_backdrop() + draw_backdrop_rest(clip) } else { - draw_ops(ops.to_list(), 0, 0, None) - rs_save_backdrop() - backdrop_ops.val = Some(ops) + let ops = ops.to_list() + let kept = backdrop_kept.val + match backdrop_ops.val { + Some(old) if rs_has_backdrop() != 0 && kept > 0 && common_prefix(old, ops) >= kept => { + // The cached start still holds: copy it, draw the rest. + set_clip(None) + rs_copy_backdrop() + backdrop_ops.val = Some(ops) + draw_backdrop_rest(None) + } + old => { + // Keep only the start these ops share with the last ones when they share one (they + // will likely change again next frame), else all of them. + let shared = match old { + Some(o) => common_prefix(o, ops) + None => 0 + } + let keep = if shared > 0 && shared < ops.length() { shared } else { ops.length() } + if !resized { + rs_clear() + } + draw_ops(ops[:keep].to_array(), 0, 0, None) + rs_save_backdrop() + backdrop_ops.val = Some(ops) + backdrop_kept.val = keep + draw_backdrop_rest(None) + } + } } None => if rs_has_backdrop() != 0 && !occluded { set_clip(clip) rs_copy_backdrop() + draw_backdrop_rest(clip) } } if !occluded {