Aim dynamic resolution at the frame limit's frame time by default and hold each step for a time, not a frame count.

Dynamic resolution's target was 16.7 ms unless set, whatever the frame limit, so under a 120 or 144 fps limit the GPU
could take up to 18 ms a frame at full size before a step down, and the game drew at about 55 fps instead of the
limit. Each step was held for 45 presented frames, 0.75 s at 60 fps but 0.19 s at 240, so the size changed four times
as often there. The target now defaults to 1000 / the rate the game draws at under the limit (frame_rate.c
frame_step_game_hz: the monitor's rate for display, the drawn frames with frame generation), read every frame so a
limit changed in the menu moves it, and 16.7 ms while that rate is unknown or unlimited; dyn_res_target set to 1 or
more still wins (its row's default is now 0). dyn_res_step keeps its key, range and default, now counted in 60ths of
a second of the clock, so 45 is 0.75 s at any frame rate and the same as before at 60 fps. The switch stays off by
default.

Not reproduced before the change (read from the code). The new tests/dynres_check.py builds dynres.c with stand-ins
and passes (24 failures on the old code); cfg_rows_check reports 0 problems; the runtime compile check and the -Wall
comparison of dynres.c show no new warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 19:04:35 -05:00
parent 5c9e480f90
commit 1c26907abe
5 changed files with 224 additions and 22 deletions

View File

@@ -331,8 +331,9 @@ static const CfgMeta metas[] = {
{"TAIL_EARLY", "0", "0|1", RESTART},
{"REPLAY_SMALL", "0", "0..63", RESTART}, // vk_exec.c group_start
{"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART},
// dynamic resolution (dynres.c): the scene follows the GPU time toward DYN_RES_TARGET milliseconds
{"DYN_RES", "0", "0|1", LIVE}, {"DYN_RES_TARGET", "16.7", "1..100", LIVE}, {"DYN_RES_STEP", "45", "8..600", LIVE},
// dynamic resolution (dynres.c): the scene follows the GPU time toward DYN_RES_TARGET milliseconds (under 1: the
// frame limit's frame time), holding each step DYN_RES_STEP 60 Hz frames of the clock
{"DYN_RES", "0", "0|1", LIVE}, {"DYN_RES_TARGET", "0", "0..100", LIVE}, {"DYN_RES_STEP", "45", "8..600", LIVE},
// the recording thread: a core of its own (auto: with 12 or more logical processors) and the highest priority
// (vk_exec.c rec_claim)
{"RECORDER_CORE", "0", "auto|0|1", RESTART}, {"RECORDER_PRIO", "1", "0|1", RESTART}, {"REPLAY_TIME", "0", "0|1", RESTART}, {"LOCK_TIME", "0", "0|1", RESTART}, {"POOL_CREATE_BUDGET", "16", "1..4096", RESTART}, {"POOL_CREATE_MS", "6", "0..100", RESTART}, {"POOL_FINI_IDLE", "0", "0|1", RESTART}, {"FREEZE_BUDGET_US", "1000", "0..100000", RESTART}, {"POOL_LOAD_BACKLOG", "512", "0..65536", RESTART},

View File

@@ -7,11 +7,16 @@
// NX2NAVI_DLSS is one of those upscaling modes, which is then the sharpest step it uses; the lowest is performance,
// or ultra performance when that is the player's own mode. While it runs it asks for GPU timestamps
// (perf_gpu_timing_want; vk_sync.c times every submission part) and reads their running total (vk_gpu_busy_all) once
// per presented frame. It averages the busy time and moves one step at a time: down when the average is over
// NX2NAVI_DYN_RES_TARGET milliseconds (default 16.7, a 60 Hz frame) by 8 %, up when it is under 75 % of it, and holds
// each step for NX2NAVI_DYN_RES_STEP frames (default 45), because a size that changes every few frames is worse to look
// at than a steady one. The step in use goes to vk_dlaa.c dlss_mode as an override (dynres_override), which applies it
// as it applies the menu's choice; the player's setting itself never changes.
// per presented frame. It averages the busy time and moves one step at a time: down when the average is over the
// target by 8 %, up when it is under 75 % of it, and holds each step for NX2NAVI_DYN_RES_STEP 60 Hz frames of the clock
// (default 45, 750 ms), because a size that changes every few frames is worse to look at than a steady one. The target
// is NX2NAVI_DYN_RES_TARGET milliseconds when that is set (1 to 100), else the frame time of the limit the game draws
// at (frame_rate.c frame_step_game_hz: 1000 / the limit, the monitor's rate for display, the drawn frames with frame
// generation), and 16.7 ms, a 60 Hz frame, while that rate is unknown or unlimited. A fixed 16.7 ms target let the GPU
// take up to 18 ms a frame at full size under a 120 or 144 fps limit, and a hold of 45 presented frames lasted 0.19 s
// at 240 fps. The step in use goes to vk_dlaa.c dlss_mode as an override (dynres_override), which applies it as it
// applies the menu's choice; the player's setting itself never changes.
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -20,18 +25,21 @@
#include "cfg.h"
#include "host.h"
#include "vk.h"
#include "platform/platform.h"
#define DYN_LEVELS 4
#define DYN_TARGET_MS 16.7 // the target while the game's rate is unknown or unlimited: a 60 Hz frame
static const char *const DYN_MODE[DYN_LEVELS] = {"quality", "balanced", "performance", "ultra_performance"};
static struct {
int on; // dynamic resolution is acting now
int base, low; // the player's step (the sharpest used) and the lowest one used
int level; // the step in use
int settle; // frames the step has been held
int step; // frames to hold a step
uint64_t held_from; // plat_now_ns when the step in use was taken
uint64_t hold_ns; // how long to hold a step
double avg_ns; // moving average of the GPU's busy time per frame
double target_ns;
double target_ms; // NX2NAVI_DYN_RES_TARGET; under 1: the frame time of the game's rate
double target_ns; // the target in use
uint64_t last_busy; // vk_gpu_busy_all at the previous presented frame
unsigned cfg_seen; // cfg_version the settings were read at
char base_mode[32];
@@ -43,22 +51,31 @@ static int mode_index(const char *m) {
return -1;
}
// The target in ns: the setting's, else the frame time of the rate the game draws at under the limit.
static double target_now_ns(void) {
if (D.target_ms >= 1) return D.target_ms * 1e6;
double hz = frame_step_game_hz();
return (hz > 0 ? 1000.0 / hz : DYN_TARGET_MS) * 1e6;
}
// The settings, again whenever one changed (the menu switches them live).
static void dynres_read(void) {
D.cfg_seen = cfg_version();
const char *m = cfg_value("NX2NAVI_DLSS");
int want = cfg_bool("NX2NAVI_DYN_RES"), base = mode_index(m);
double ms = cfg_double("NX2NAVI_DYN_RES_TARGET");
D.target_ns = (ms >= 1 ? ms : 16.7) * 1e6;
D.step = cfg_int("NX2NAVI_DYN_RES_STEP");
if (D.step < 8) D.step = 8;
D.target_ms = cfg_double("NX2NAVI_DYN_RES_TARGET");
D.target_ns = target_now_ns();
int step = cfg_int("NX2NAVI_DYN_RES_STEP");
if (step < 8) step = 8;
D.hold_ns = (uint64_t)step * 1000000000ull / 60;
int on = want && base >= 0;
if (on && (!D.on || strcmp(m, D.base_mode))) { // (re)start at the player's step
D.base = D.level = base;
D.low = base > 2 ? base : 2;
D.settle = 0, D.avg_ns = 0, D.last_busy = vk_gpu_busy_all();
rt_log("resolution: dynamic resolution on: target %.1f ms, DLSS %s down to %s", D.target_ns / 1e6, DYN_MODE[D.base],
DYN_MODE[D.low]);
D.held_from = plat_now_ns(), D.avg_ns = 0, D.last_busy = vk_gpu_busy_all();
rt_log("resolution: dynamic resolution on: target %.1f ms (%s), each step held %.2f s, DLSS %s down to %s",
D.target_ns / 1e6, D.target_ms >= 1 ? "as set" : "the frame limit's", D.hold_ns / 1e9,
DYN_MODE[D.base], DYN_MODE[D.low]);
} else if (!on && D.on) {
rt_log("resolution: dynamic resolution off: DLSS %s as set", m);
} else if (want && base < 0) {
@@ -77,6 +94,16 @@ const char *dynres_override(void) { return D.on && D.level != D.base ? DYN_MODE[
void dynres_frame(void) {
if (cfg_version() != D.cfg_seen) dynres_read();
if (!D.on) return;
double target = target_now_ns(); // the limit, the monitor's rate and frame generation change without a setting
if (target != D.target_ns) { // logged when it moves by more than 1 %: a measured refresh rate wanders a little
static double logged_ns;
static int said;
if (fabs(target - logged_ns) > logged_ns * 0.01 && said < 20) {
said++, logged_ns = target;
rt_log("resolution: dynamic resolution: target %.2f ms (the frame limit's)", target / 1e6);
}
D.target_ns = target;
}
uint64_t now = vk_gpu_busy_all(), d = now - D.last_busy;
D.last_busy = now;
if (!d || d > 1000000000ull) return; // no timing this frame yet, or a stall that says nothing about the scene
@@ -87,12 +114,12 @@ void dynres_frame(void) {
rt_log("resolution: dynamic resolution: GPU %.2f ms a frame, DLSS %s, target %.2f ms", D.avg_ns / 1e6,
DYN_MODE[D.level], D.target_ns / 1e6);
}
if (++D.settle < D.step) return;
if (plat_now_ns() - D.held_from < D.hold_ns) return;
int to = D.level;
if (D.avg_ns > D.target_ns * 1.08 && D.level < D.low) to = D.level + 1;
else if (D.avg_ns < D.target_ns * 0.75 && D.level > D.base) to = D.level - 1;
if (to == D.level) return;
rt_log("resolution: dynamic resolution: GPU %.2f ms a frame against %.2f ms: DLSS %s -> %s", D.avg_ns / 1e6,
D.target_ns / 1e6, DYN_MODE[D.level], DYN_MODE[to]);
D.level = to, D.settle = 0;
D.level = to, D.held_from = plat_now_ns();
}