summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 13:55:51 -0300
committerGabriel Schneider <[email protected]>2026-10-24 20:59:59 -0300
commit55dd0c16e0dcc6a7e1c9e7babb31371bfe31119d (patch)
tree504bde4992eb945274931f50b4b6537e392c5f81
parent93d901ec5b80c36cb6c9d9027a8b39de5adafaca (diff)
downloadpardes-55dd0c16e0dcc6a7e1c9e7babb31371bfe31119d.tar.gz
pardes-55dd0c16e0dcc6a7e1c9e7babb31371bfe31119d.zip
anim 2: frames paced as a game's while anything moves: one a display refresh on a grid of ticks, each sampled at its tick, input acted on as it comes and drawn at the next tick; the uncapped ~230 fps loop that showed motion at random phase is gone
-rw-r--r--src/gui/gui.zig66
1 files changed, 62 insertions, 4 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 0a3f3340..5921d5dc 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -1481,6 +1481,10 @@ const Gui = struct {
demo_name: []const u8 = "",
demo_next: ?usize = null,
demo_start_ns: u64 = 0,
+ /// The tick the last paced frame was sampled at (waitInput), and
+ /// whether this loop's wait read an input (drawn at once, not paced).
+ pace_ns: u64 = 0,
+ input_seen: bool = false,
/// G9 and G11: the selection's and the hovered word's glow blocks as
/// last drawn, for their fade-in.
sel_glow: SelGlow = .{},
@@ -3137,8 +3141,10 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void {
// An input's time, for its latency to the frame that answers it
// (FrameStats): SDL's stamp, moved onto the monotonic clock.
switch (sev.type) {
- c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_TEXT_INPUT, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_WHEEL => if (frame_probe) |probe|
- probe.input(monoNs() -| (c.SDL_GetTicksNS() -| sev.common.timestamp)),
+ c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_TEXT_INPUT, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_WHEEL => {
+ g.input_seen = true;
+ if (frame_probe) |probe| probe.input(monoNs() -| (c.SDL_GetTicksNS() -| sev.common.timestamp));
+ },
else => {},
}
// What the post chain sees of the pointer and the focus.
@@ -3819,6 +3825,9 @@ const Shell = struct {
test_clock: ?u64 = null,
/// The display rate a virtual clock stands in for (PARDES_TEST_HZ).
test_hz: u64 = 60,
+ /// The tick a paced frame is sampled at (waitInput): the clock the
+ /// core is advanced to while anything moves.
+ paced_ns: ?u64 = null,
fn reap(s: *Shell) void {
const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds();
@@ -4094,7 +4103,33 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
const blink_ms: ?c_int = if (blink) |b| (if (b.edge) 1 else if (b.next_ns) |next| @as(c_int, @intCast(@min(std.math.maxInt(c_int), next / std.time.ns_per_ms + 1))) else null) else null;
const blinked: c_int = if (blink_ms) |b| (if (waited < 0) b else @min(waited, b)) else waited;
// The timedemo's next step is a wake too.
- const ms: c_int = if (demoDue(g)) |due| (if (blinked < 0) due else @min(blinked, due)) else blinked;
+ var ms: c_int = if (demoDue(g)) |due| (if (blinked < 0) due else @min(blinked, due)) else blinked;
+ // While anything moves, frames are paced as a game's are: one a
+ // display refresh, on a grid of ticks a period apart, each sampled
+ // at its tick (guiNow), however early or late the loop got there.
+ // Input that comes between two ticks is acted on as it comes (the
+ // core's state is never held back) and drawn at the next tick. Not
+ // paced, the loop ran as fast as it could present (the soft present
+ // has no vsync), ~230 frames a second on a 60 Hz display, and the
+ // compositor showed whichever was newest at each refresh: motion
+ // sampled at random phase, uneven steps, a judder in every scroll.
+ // ponytail: the period is the display mode's refresh and the grid's
+ // phase is our own, not the display's (no vblank time to read on the
+ // soft present); a 59.94 Hz display drifts a tick every 17 s. The
+ // upgrade: the presentation feedback protocol's timestamps.
+ const paced = s.test_clock == null and latency_fd < 0 and !s.test_mode and !minimized and core.continuous();
+ var tick: u64 = 0;
+ if (paced) {
+ const period = refreshNs(g);
+ const now = c.SDL_GetTicksNS();
+ // A motion from rest draws at once; one under way, at its next
+ // tick, skipping (never bunching) the ticks a slow frame missed.
+ tick = if (g.pace_ns != 0 and now -| g.pace_ns < 2 * period) g.pace_ns + period else now;
+ if (tick < now) tick += (now - tick) / period * period;
+ const left: c_int = @intCast(@min(std.math.maxInt(c_int), (tick -| now) / std.time.ns_per_ms));
+ ms = if (ms < 0) left else @min(ms, left);
+ }
+ g.input_seen = false;
// The wait is the 9P connections' turn with the core.
pardes.turn.rest();
const got = c.SDL_WaitEventTimeout(&sev, ms);
@@ -4137,6 +4172,27 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
if (motion) |*m| dispatch(g, &in, m);
}
runDemo(g, &in, core);
+ // Paced: what came before the tick is in; wait out the rest of it,
+ // unless an input came: its frame is drawn at once (the paced loop
+ // never adds a tick to an input's latency), and the ticks after it
+ // run from it.
+ if (paced and g.input_seen) tick = c.SDL_GetTicksNS();
+ while (paced and !g.input_seen) {
+ const left = tick -| c.SDL_GetTicksNS();
+ if (left < std.time.ns_per_ms) break;
+ const wait_ms: c_int = @intCast(@min(left / std.time.ns_per_ms, @as(u64, @intCast(demoDue(g) orelse std.math.maxInt(c_int)))));
+ pardes.turn.rest();
+ const more = c.SDL_WaitEventTimeout(&sev, wait_ms);
+ pardes.turn.wake();
+ if (more) {
+ dispatch(g, &in, &sev);
+ while (c.SDL_PollEvent(&sev)) dispatch(g, &in, &sev);
+ }
+ runDemo(g, &in, core);
+ if (g.input_seen) tick = c.SDL_GetTicksNS();
+ }
+ s.paced_ns = if (paced) tick else null;
+ if (paced) g.pace_ns = tick;
}
if (s.test_mode) {
const r = s.feed.pump(s.gpa, &in, s.gui) catch {
@@ -4264,6 +4320,7 @@ fn runDemo(g: *Gui, in: *Input, core: *pardes.Pardes) void {
defer g.demo_next = next;
while (next < steps.len and g.demo_start_ns + @as(u64, steps[next].at_ms) * std.time.ns_per_ms <= now) : (next += 1) {
const r = core.rects[core.active];
+ if (steps[next].act != .phase) g.input_seen = true;
switch (steps[next].act) {
.wheel => |notches| {
core.frames.input(now);
@@ -4424,7 +4481,8 @@ fn postPresent(ctx: ?*anyopaque) void {
}
fn guiNow(ctx: ?*anyopaque) u64 {
- return shellOf(ctx).test_clock orelse c.SDL_GetTicksNS();
+ const s = shellOf(ctx);
+ return s.test_clock orelse s.paced_ns orelse c.SDL_GetTicksNS();
}
fn gridPollFrame(ctx: ?*anyopaque) void {