Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions runtime/nvn/pace.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@
// resolution stayed at 30 outdoors, backing off: up to 16x the wait). The hold ends and 60 gets a
// fresh try, with a hitch's grace; frames that don't fit hold again as on a first try. Dynamic's own steps are not
// a setting change: its scale follows the load, and a reset there would bring back the 60 / 30 flicker on the edge.
// A GPU time read at 30 can't keep a hold for good either (issue #811: a Radeon 760M at 2560x1440 held entering a
// dungeon, its frames still heavy past the load's second of grace; held it read 23 ms a frame - an integrated GPU
// clocks down at half load - and stayed at 30 until a restart, which ran the same dungeon at 60). Without Dynamic
// resolution nothing else looks for 60 from there. So once the CPU has had clear headroom for PACE_TRY_FRAMES (10 s at
// 30) times the backoff while only the GPU's time keeps the hold, 60 gets a try (with a hitch's grace), whose frames
// measure the GPU at 60: frames that don't fit hold again, backing off (the next try waits twice as long, up to 16x).
// A try that failed with the GPU over the limit at 60 and not slower at 30 (gpu60: a GPU that keeps its clock) isn't
// tried again in that scene - its time at 30 is its time at 60.
// LARECOMP_PACE=0: off (the game's own choice, flips and all).
#define PACE_SLOW_NS 20000000ull
#define PACE_GPU_MS 15.0 // (16.7 ms a frame at 60, less some for the window image and jitter)
Expand All @@ -112,6 +120,10 @@
// A hold that busy frames started, after a failed return (backing off): the clear way back wants the GPU this much
// under its time at that return (see above; the iPad's returns read 11.4-13.0 ms, half a ms apart).
#define PACE_GPU_BACKOFF_UNDER_MS 2.0
// A try at 60 while only the GPU's time at 30 holds (see above): after this many held gameplay frames with clear CPU
// headroom, times the backoff; its GPU time at 30 counts as clocked down when this much over a failed try's at 60
#define PACE_TRY_FRAMES 300
#define PACE_GPU_DOWNCLOCK_MS 2.0
#define PACE_GRACE_NS 1000000000ull
#define PACE_GRACE_MAX_NS 4000000000ull // a render scale change's grace at most (see above)
#define PACE_SETTLE_FRAMES 10 // ... ends once this many frames in a row are on time
Expand Down Expand Up @@ -313,6 +325,10 @@ void pace_frame(void) {
static uint64_t back_at; // when frame pacing last went back to 60 (0: not yet)
static bool try_back; // (that was Dynamic's look for 60: a hold right after it keeps the backoff)
static double gpu_back = -1; // the GPU time per frame (gpu_avg) at that return (-1: not timed)
static int try_n; // held gameplay frames with clear CPU headroom while only the GPU's time holds (see above)
static bool trying; // (the return to 60 was such a try: a hold right after it learns its GPU time at 60)
static double try_gpu60 = -1; // the GPU time per frame at 60 when a try failed (-1: none), and its scene
static uint64_t try_scene;
static double avg; // cost (ms at 60 fps), moving average (~20 frames)
static uint64_t last_tcpu, last_rcpu;
extern uint64_t rq_render_cpu_ns(void); // rqueue.c: the render thread's CPU time so far (0: no render thread)
Expand Down Expand Up @@ -344,16 +360,16 @@ void pace_frame(void) {
const char *reset = __atomic_exchange_n(&reset_what, (const char *)NULL, __ATOMIC_ACQ_REL);
if (reset && on) { // a user setting changed the GPU's load: the hold and the backoff start over (see above)
LOG("[pace] %s setting changed: back-off cleared, trying 60\n", reset);
pace_slow = late_hold = false, backoff = 1, back_at = 0;
clear = calm = cpu_clear = 0, scene = 0, gpu_said = 0;
pace_slow = late_hold = trying = false, backoff = 1, back_at = 0, try_gpu60 = -1;
clear = calm = cpu_clear = try_n = 0, scene = 0, gpu_said = 0;
nover = nbusy = ncpu = 0;
memset(over, 0, sizeof over);
memset(recent, 0, sizeof recent);
}
if (__atomic_exchange_n(&try_60, 0, __ATOMIC_ACQ_REL) && on && pace_slow) { // (see above)
LOG("[pace] dynamic resolution looks for 60: trying 60\n");
pace_slow = late_hold = false, back_at = now, try_back = true, gpu_back = gpu_avg;
clear = calm = cpu_clear = 0, gpu_said = 0;
pace_slow = late_hold = trying = false, back_at = now, try_back = true, gpu_back = gpu_avg;
clear = calm = cpu_clear = try_n = 0, gpu_said = 0;
nover = nbusy = ncpu = 0;
memset(over, 0, sizeof over);
memset(recent, 0, sizeof recent);
Expand Down Expand Up @@ -489,9 +505,17 @@ void pace_frame(void) {
pace_slow = true, calm = clear = cpu_clear = 0, gpu_said = 0;
late_hold = nbusy < 12;
if (back_at && now - back_at < 20000000000ull && backoff < 16 && !try_back) backoff *= 2; // (flickering: wait longer)
try_back = false;
try_back = false, try_n = 0;
char g[48] = "";
// (a try at 60 from a hold for the GPU's time, failed - within 20 s, as the backoff: what the GPU took at 60. A
// hold long after a try that worked can be a level load's tail, which says nothing about the GPU at 60)
if (trying && gpu_avg >= 0 && now - back_at < 20000000000ull) {
try_gpu60 = gpu_avg, try_scene = pace_scene();
snprintf(g, sizeof g, ", gpu %.1f ms a frame at 60", gpu_avg);
}
trying = false;
LOG("[pace] frames don't fit 60 fps (%d of the last 60 over 16.2 ms busy or shown late, %d by busy time, %d by CPU time, "
"median %.1f): holding the game's 30 fps mode%s%s\n", nover, nbusy, ncpu, med,
"median %.1f%s): holding the game's 30 fps mode%s%s\n", nover, nbusy, ncpu, med, g,
ncpu >= 12 ? " - the CPU's" : dyn == 1 ? " - dynamic resolution at its floor" : "", backoff > 1 ? " (backing off)" : "");
} else if (pace_slow) {
float peak = 0;
Expand All @@ -504,7 +528,8 @@ void pace_frame(void) {
// PACE_GPU_LATE_BACKOFF_MS, or - held for busy frames - PACE_GPU_BACKOFF_UNDER_MS under the GPU time of that
// return (its time at 30 said nothing about 60; untimed, nothing was learned); the enough way keeps the first
// try's limit, so the backoff only lengthens the wait.)
bool gpu_fits = gpu_avg < (late_hold ? PACE_GPU_LATE_MS : PACE_GPU_MS);
double gpu_lim = late_hold ? PACE_GPU_LATE_MS : PACE_GPU_MS;
bool gpu_fits = gpu_avg < gpu_lim;
bool gpu_clear = gpu_fits && (backoff == 1 || (late_hold ? gpu_avg < PACE_GPU_LATE_BACKOFF_MS
: gpu_back >= 0 && gpu_avg < gpu_back - PACE_GPU_BACKOFF_UNDER_MS));
int clear_n = 60 * (backoff < 4 ? backoff : 4);
Expand All @@ -518,16 +543,29 @@ void pace_frame(void) {
}
clear = gpu_clear && cpu_clear ? clear + 1 : 0;
calm = sc && gpu_fits && med < 13.5f && nover <= 4 ? calm + 1 : 0;
// A try at 60 when only the GPU's time at 30 holds (see "A GPU time read at 30"): not where a failed try showed the
// GPU over the limit at 60 and no slower at 30. (Not counted off gameplay: the pause menu neither counts nor resets.)
bool gpu_known = try_gpu60 >= gpu_lim && sc == try_scene && gpu_avg <= try_gpu60 + PACE_GPU_DOWNCLOCK_MS;
if (sc) try_n = avg < 11.0 && peak < 14.5f && !gpu_fits && !gpu_known && !pace_target_30() ? try_n + 1 : 0;
if (clear >= clear_n || calm >= 150 * backoff) {
char g[32];
if (gpu_avg >= 0) snprintf(g, sizeof g, ", gpu %.1f ms", gpu_avg);
else snprintf(g, sizeof g, ", gpu not timed");
LOG("[pace] %d frames with %s headroom on gameplay frames (%.1f ms a frame at 60 on average, median %.1f, at most "
"%.1f%s): back to the game's own choice\n", clear >= clear_n ? clear : calm, clear >= clear_n ? "clear" : "enough",
avg, med, peak, g);
pace_slow = false, nover = nbusy = ncpu = 0, back_at = now, try_back = false, gpu_back = gpu_avg;
pace_slow = trying = false, nover = nbusy = ncpu = 0, back_at = now, try_back = false, gpu_back = gpu_avg;
memset(over, 0, sizeof over);
memset(recent, 0, sizeof recent);
} else if (try_n >= PACE_TRY_FRAMES * backoff) {
LOG("[pace] %d gameplay frames with clear CPU headroom (%.1f ms a frame at 60 on average), the GPU %.1f ms a frame "
"at 30 (its clock may be down there): trying 60%s\n", try_n, avg, gpu_avg, backoff > 1 ? " (backing off)" : "");
pace_slow = late_hold = false, back_at = now, try_back = false, gpu_back = gpu_avg, trying = true;
clear = calm = cpu_clear = try_n = 0, gpu_said = 0;
nover = nbusy = ncpu = 0;
memset(over, 0, sizeof over);
memset(recent, 0, sizeof recent);
pace_hitch("a try at 60");
}
}
hold_cadence(hold_30(pace_slow));
Expand Down
38 changes: 35 additions & 3 deletions tools/test_pace.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ static void overload_steps(double t, int n, Frame *f) {
if (t < 6 && k != last_k) last_k = k, pace_hitch("a render scale change");
}
static int held_soon(int holds, int backs, double held_s, double lc, bool end, double secs) {
return holds == 1 && backs == 0 && end && lc < 12; // (the last step at 5 s: held by 12 s, for the rest)
// (the last step at 5 s: held by 12 s, for the rest - but for one try at 60 its CPU headroom gets, a few seconds)
return holds <= 2 && backs <= 1 && end && held_s > secs - 15;
}

// A render scale change every 4.5 s at Dynamic's floor: frames on time for 1.5 s after it (settled), then GPU 20 ms to
Expand All @@ -207,7 +208,7 @@ static void gpu20_dyn_floor(double t, int n, Frame *f) { gpu20(t, n, f), dyn_sta
// ... reaching its floor 5 s in (its steps took the factor down meanwhile): held within a second or two of that
static void gpu20_dyn_reaches_floor(double t, int n, Frame *f) { gpu20(t, n, f), dyn_state = t < 5 ? 2 : 1; }
static int held_at_floor(int holds, int backs, double held_s, double lc, bool end, double secs) {
return holds == 1 && backs == 0 && end && lc >= 5 && lc < 8;
return holds <= 2 && backs <= 1 && end && held_s <= secs - 5 && held_s > secs - 11; // (held at 5-8 s, one try)
}
// The CPU the limit (the game thread 20 ms a frame) with Dynamic above its floor: held at any factor - no resolution
// helps the CPU. By CPU time, not busy time:
Expand All @@ -225,12 +226,39 @@ static void render20_dyn(double t, int n, Frame *f) {
dyn_state = 2;
}

// #811: a Radeon 760M (integrated, 2560x1440) entering a dungeon: GPU ~14.5 ms a frame at 60, but its frames stay heavy
// (22 ms) for 2.5 s after the level load - past the second of grace - and the game is held. Held at 30 the GPU clocks
// down and reads 23 ms a frame, over the limit for good: it stayed at 30 until a restart (which ran the same dungeon at
// 60). It must try 60 and stay there.
static void igpu_downclock(double t, int n, Frame *f) {
static bool loaded;
if (t >= 3 && !loaded) loaded = true, pace_hitch("a level load"), f->stall = 120;
bool load_tail = t >= 3 && t < 5.5;
f->cpu = 2 + noise(0.5), f->wait = 6, f->stall = f->stall > 0 ? f->stall : 0;
f->gpu = pace_is_slow() ? 23 + noise(1) : load_tail ? 22 + noise(1) : 14.5 + noise(1);
zone = t < 3 ? 1 : 2;
}
static int igpu_back(int holds, int backs, double held_s, double lc, bool end, double secs) {
return holds == 1 && backs == 1 && !end && lc < 20; // (held after the load, back within ~10 s of clear CPU)
}
// ... and one that doesn't fit 60 at full clock either (GPU 19 ms a frame at 60, 26 at 30): the tries at 60 fail and
// back off - few of them, further apart each time - and it ends held.
static void igpu_downclock_heavy(double t, int n, Frame *f) {
f->cpu = 2 + noise(0.5), f->wait = 6, f->stall = 0;
f->gpu = pace_is_slow() ? 26 + noise(1) : 19 + noise(1);
}
static int igpu_tries_back_off(int holds, int backs, double held_s, double lc, bool end, double secs) {
return backs >= 1 && backs <= 3 && end && held_s > secs - 12;
}

// Fits 60 with no hitches: never held.
static void steady(double t, int n, Frame *f) { f->cpu = 5 + noise(1), f->gpu = 11 + noise(1.5), f->stall = 0, f->wait = 8; }

static int never_held(int holds, int backs, double held_s, double lc, bool end, double secs) { return holds == 0; }
// (held within a few seconds and for the rest. Held for the GPU's time with CPU headroom: one try at 60 after 10 s,
// whose frames show the GPU over at 60 as at 30 - no other try in that scene - pace.c "A GPU time read at 30")
static int held_after(int holds, int backs, double held_s, double lc, bool end, double secs) {
return holds == 1 && backs == 0 && end && held_s > secs - 5; // (held within a few seconds and for the rest)
return holds <= 2 && backs <= 1 && end && held_s > secs - 8;
}
static int released(int holds, int backs, double held_s, double lc, bool end, double secs) {
return holds == 1 && backs == 1 && !end && lc < 12;
Expand Down Expand Up @@ -276,6 +304,10 @@ static const Case cases[] = {
{"resolution first: the game thread busy waiting on the GPU (fence 13 ms, CPU 4.5), Dynamic above its floor: never held",
60, gpu_fence_dyn, never_held},
{"resolution first: the render thread 20 ms of CPU, Dynamic above its floor: held (the CPU's)", 60, render20_dyn, held_after},
{"#811: integrated GPU 14.5 ms at 60, held by a level load's tail, reads 23 ms at 30 (clocked down): tries 60, stays",
40, igpu_downclock, igpu_back},
{"#811: integrated GPU 19 ms at 60, 26 ms at 30: its tries at 60 fail and back off, ends held", 120,
igpu_downclock_heavy, igpu_tries_back_off},
};

static int run(const Case *c) {
Expand Down
Loading