Skip to content
Merged
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
12 changes: 12 additions & 0 deletions games/totk/runtime/settings_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
one turns the other off (user 2026-10-25, not to mislead)
frame interpolation off / 30+30 / 40+40 / 50+50 / runtime flag mfx_interp (gfx/nvn/totk_mfx.m)
unlimited
GPU keep-alive switch, default on dm_gfx_keepalive_set (gfx/nvn/nvn_mtl.m), at once;
gpu-keepalive.txt above the game's folder (both games);
TOTK_GPU_KEEPALIVE first (greyed out)
resolution 720p / + MetalFX / 1080p / runtime_output_mode_set (game_patches*.c); the player picks,
+ MetalFX / 2K /
2K + MetalFX (a 4K screen)
Expand Down Expand Up @@ -129,6 +132,7 @@
void dm_gfx_set_flag(const char *name, int value);
void dm_gfx_ssao_start(int pct); int dm_gfx_ssao_locked(void); int dm_gfx_ssao_live(void); void dm_gfx_ssao_set(int pct); /* totk_ssao.h */
void dm_gfx_aniso_start(int n); int dm_gfx_aniso_locked(void); int dm_gfx_aniso(void); void dm_gfx_aniso_set(int n); /* gfx/nvn/nvn_mtl.m */
void dm_gfx_keepalive_start(int on); int dm_gfx_keepalive_locked(void); int dm_gfx_keepalive(void); void dm_gfx_keepalive_set(int on); /* gfx/nvn/nvn_mtl.m */
#ifdef GAME_BOTW190
int runtime_shadow_res_available(void); int runtime_shadow_res_locked(void); int runtime_shadow_res_saved(void);
void runtime_shadow_res_save(int size); /* game_patches_botw.c */
Expand Down Expand Up @@ -271,6 +275,7 @@ static void settings_restore_begin(void) {
if(!getenv("TOTK_TAA")&&settings_read_int("graphics-taa.txt",&v)&&(v==0||v==1))taa_set(v);
settings_ssao_restore();
if(!dm_gfx_aniso_locked()&&settings_read_int("graphics-aniso.txt",&v)&&v<=16)dm_gfx_aniso_start(v);
if(!dm_gfx_keepalive_locked()&&runtime_shared_setting_word("gpu-keepalive.txt",word,sizeof word))dm_gfx_keepalive_start(!strncmp(word,"on",2));
settings_effects_restore();
const char *output=getenv("TOTK_OUTPUT_RES");
if(
Expand Down Expand Up @@ -844,6 +849,13 @@ static NSArray<TOTKSettingRow *> *settings_rows(void) {
@[@(MFX_OFF),@(MFX_30),@(MFX_40),@(MFX_50),@(MFX_60),@(MFX_MAX)],
^int{return mfx_requested();},^(int v){dm_gfx_set_flag("mfx_interp",v);if(v!=MFX_OFF)nvn_fps_lock_set(0);settings_save_frame();
if(v!=MFX_OFF&&front_hdr_enabled())runtime_front_hdr_set(0);})]; /* interpolated frames are SDR: HDR goes off (PR #1129) */
/* GPU keep-alive (nvn_mtl.m keepalive_start): short spin kernels hold the GPU clock up between frames. A Mac whose GPU
keeps its clock with the game's own work gains no frame from it, only heat and power (an M4 Max: ~90 % GPU busy with it,
~63 % without, the same frames), so the player can turn it off. */
{NSString *t=settings_text("GPU 保活(防降频,更热更耗电)","GPU keep-alive (holds the clock up; hotter, more power)");
if(dm_gfx_keepalive_locked())[rows addObject:settings_fixed(t,settings_on_off(dm_gfx_keepalive()))];
else [rows addObject:settings_row(t,nil,nil,^int{return dm_gfx_keepalive();},
^(int v){dm_gfx_keepalive_set(v);runtime_shared_setting_save("gpu-keepalive.txt",v?"on":"off","GPU keep-alive");})];}
int modes=runtime_output_modes();
NSMutableArray<NSString *> *res=[NSMutableArray array];NSMutableArray<NSNumber *> *resModes=[NSMutableArray array];
static const int order[]={OUTPUT_MODE_720P,OUTPUT_MODE_720P_METALFX,OUTPUT_MODE_OFF,OUTPUT_MODE_METALFX,OUTPUT_MODE_NATIVE,OUTPUT_MODE_NATIVE_METALFX,OUTPUT_MODE_NATIVE_4K,OUTPUT_MODE_NATIVE_5K,
Expand Down
37 changes: 30 additions & 7 deletions games/totk/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ static void udc_selftest(void) {
MLOG("UDC自检 %s %s %lux%lu 首值 %08x 与首值不同 %lu / %lu", g_dev.name.UTF8String, depth ? "Depth32F_S8" : "RGBA8", (unsigned long)w, (unsigned long)h, px[0], (unsigned long)bad, (unsigned long)(w * h));
}
}
/* TOTK: GPU keep-alive (default on on the Mac, TOTK_GPU_KEEPALIVE=0 turns it off; off on iOS, TOTK_GPU_KEEPALIVE=1 turns it on). A thread keeps up to two short spin kernels (one
/* TOTK: GPU keep-alive (default on on the Mac, TOTK_GPU_KEEPALIVE=0 or the settings panel's switch turns it off; off on iOS, TOTK_GPU_KEEPALIVE=1 turns it on). A thread keeps up to two short spin kernels (one
threadgroup) in flight on its own command queue while the game is submitting frames. The M4 GPU lowers its clock when it
idles part of each frame: the same compute work took 10.9 ms back to back, 16.8 ms with 10 ms gaps and 10.9 ms again with a
spinner in the gaps (standalone test). TOTK idles the GPU between frames because it waits for each frame before it submits
Expand All @@ -576,26 +576,49 @@ spinner in the gaps (standalone test). TOTK idles the GPU between frames because
static _Atomic int g_ka_target_us = 1000;
static _Atomic uint64_t g_ka_n, g_ka_us, g_ka_max_us; // spin kernels completed, their summed / longest GPU time (us)
void mtl_keepalive_stats(uint64_t v[3]) { v[0] = atomic_load(&g_ka_n); v[1] = atomic_load(&g_ka_us); v[2] = atomic_exchange(&g_ka_max_us, 0); }
static void keepalive_start(void) {
static int started; const char *e = getenv("TOTK_GPU_KEEPALIVE");
/* On or off: TOTK_GPU_KEEPALIVE decides first; otherwise, on the Mac, the settings panel's switch (gpu-keepalive.txt, read
before the guest starts: dm_gfx_keepalive_start) or, in play, dm_gfx_keepalive_set. Switched off in play, the thread stays
and only sleeps; switched on, it starts if it has not yet. On a fast Mac the game's own work may keep the clock up anyway:
an M4 Max at 60+60 interpolation lost no frame without the spinner, which kept its GPU ~90 % busy instead of ~63 %. */
static _Atomic int g_ka_on = -1, g_ka_started;
static void keepalive_start(void);
int dm_gfx_keepalive_locked(void) { return getenv("TOTK_GPU_KEEPALIVE") != NULL; }
int dm_gfx_keepalive(void) {
int v = atomic_load(&g_ka_on);
if (v < 0) {
const char *e = getenv("TOTK_GPU_KEEPALIVE");
#if TARGET_OS_IPHONE
int on = e && atoi(e); // iPhone / iPad: off unless TOTK_GPU_KEEPALIVE=1 (the spinning heats the phone; the gain above was measured on the M4)
int on = e && atoi(e); // iPhone / iPad: off unless TOTK_GPU_KEEPALIVE=1 (the spinning heats the phone; the gain above was measured on the M4)
#else
int on = !e || atoi(e);
int on = !e || atoi(e);
#endif
if (started || !on || nogpu() || getenv("TOTK_HEADLESS")) return; started = 1; // not in standalone probes
int was = -1; atomic_compare_exchange_strong(&g_ka_on, &was, on); v = atomic_load(&g_ka_on);
}
return v;
}
void dm_gfx_keepalive_start(int on) { if (!dm_gfx_keepalive_locked()) atomic_store(&g_ka_on, on ? 1 : 0); }
void dm_gfx_keepalive_set(int on) {
if (dm_gfx_keepalive_locked() || dm_gfx_keepalive() == !!on) return;
atomic_store(&g_ka_on, on ? 1 : 0);
MLOG("GPU 保活:设置面板%s | GPU keep-alive: %s from the settings panel", on ? "打开" : "关闭", on ? "on" : "off");
if (on && atomic_load(&g_last_sub_ns)) keepalive_start(); // the game already renders; before that the first submission starts it
}
static void keepalive_start(void) {
if (!dm_gfx_keepalive() || nogpu() || getenv("TOTK_HEADLESS")) return; // not in standalone probes
if (atomic_exchange(&g_ka_started, 1)) return;
NSError *err = nil;
id<MTLLibrary> lib = [g_dev newLibraryWithSource:@"#include <metal_stdlib>\nusing namespace metal;\nkernel void keep(device float *a [[buffer(0)]], constant uint &n [[buffer(1)]], uint i [[thread_position_in_grid]]) { float x = a[i]; for (uint j = 0; j < n; j++) x = fma(x, 1.0000001f, 0.5f); a[i] = x; }\n" options:nil error:&err];
id<MTLComputePipelineState> ps = lib ? [g_dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@"keep"] error:&err] : nil;
id<MTLCommandQueue> q = [g_dev newCommandQueue]; id<MTLBuffer> buf = [g_dev newBufferWithLength:4096 options:MTLResourceStorageModePrivate];
if (!ps || !q || !buf) { MLOG("【红】TOTK_GPU_KEEPALIVE:建不出自旋核 %s", err.localizedDescription.UTF8String); return; }
{ const char *us = getenv("TOTK_GPU_KEEPALIVE_US"); int v = us ? atoi(us) : 0; if (v >= 100 && v <= 1000) atomic_store(&g_ka_target_us, v); } /* like flag keepalive_us, from the start */
MLOG("GPU 保活:独立队列上持续跑短自旋核(每个约 %d µs),避免 GPU 在帧间空闲时降频(TOTK_GPU_KEEPALIVE=0 关闭)", atomic_load(&g_ka_target_us));
MLOG("GPU 保活:独立队列上持续跑短自旋核(每个约 %d µs),避免 GPU 在帧间空闲时降频(设置面板或 TOTK_GPU_KEEPALIVE=0 关闭)", atomic_load(&g_ka_target_us));
g_ka_idle = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
__block uint32_t n = 40000; dispatch_semaphore_t sem = dispatch_semaphore_create(1); // ~1 ms on the M4 at full clock
for (;;) @autoreleasepool {
int target_us = atomic_load(&g_ka_target_us);
if (!atomic_load(&g_ka_on)) { usleep(20000); continue; } // switched off in the settings panel
if (!target_us) { usleep(2000); continue; }
uint64_t last = atomic_load(&g_last_sub_ns);
if (!last || clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - last > 100000000ull) { usleep(10000); continue; } // the game is not rendering
Expand Down
9 changes: 7 additions & 2 deletions tests/settings_panel_probe.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ void dm_gfx_set_flag(const char *name, int value) {
static int fakeAniso;
int dm_gfx_aniso(void) { return fakeAniso; }
void dm_gfx_aniso_set(int n) { fakeAniso = n; }
static int fakeKeepalive = 1;
void dm_gfx_keepalive_start(int on) { fakeKeepalive = on != 0; }
int dm_gfx_keepalive_locked(void) { return 0; }
int dm_gfx_keepalive(void) { return fakeKeepalive; }
void dm_gfx_keepalive_set(int on) { fakeKeepalive = on != 0; }
void dm_gfx_sharpen_set(int pct) { fakeSharpen = pct; }
int dm_gfx_sharpen(void) { return fakeSharpen; }
static int fakeFxaaReplaced; /* Breath of the Wild 1.9.0: MetalFX replaces the FXAA / upscale draw (gfx/nvn/botw_finish.h) */
Expand Down Expand Up @@ -623,7 +628,7 @@ int main(int argc, const char **argv) { @autoreleasepool {
runtime_input_key_synthetic(18, 1); runtime_input_key_synthetic(18, 1); tick(); runtime_input_key_synthetic(18, 0); tick();
assert(settingsOpen && fakeSettingsCapture && settingsPanel && !settingsPanel.hidden && settingsPanel.superview == window.contentView);
assert(NSContainsRect(window.contentView.bounds, settingsPanel.frame));
assert(settingsPanel.allRows.count == 28 + 3*TOTK_V143 + BOTW_V190 + (TOTK_V143 && totk_hdr_available() ? SETTINGS_HDR_ROWS : 0)); /* + the HDR rows (PR #1129) */ /* 17 (anisotropy last on the display page, controller
assert(settingsPanel.allRows.count == 29 + 3*TOTK_V143 + BOTW_V190 + (TOTK_V143 && totk_hdr_available() ? SETTINGS_HDR_ROWS : 0)); /* + the HDR rows (PR #1129) */ /* 17 (anisotropy last on the display page, controller
background input on the input page), the user page's
two, then the effects page: FOV, two distances, shadows,
depth of field, FXAA, sharpening and effect switches
Expand Down Expand Up @@ -1054,7 +1059,7 @@ the FXAA row says so (rows are made at each opening). */
/* Pages: Display, Input, User and Key bindings; the page shown last opens again. */
press(18); assert(settingsOpen);
[settingsPanel selectPage:SETTINGS_PAGE_DISPLAY];
assert(settingsPanel.rows.count == 6 && [settingsPanel.rows[0].title isEqualToString:@"Overlay"]); /* edge smoothing, AO and anisotropy are on the effects page */
assert(settingsPanel.rows.count == 7 && [settingsPanel.rows[0].title isEqualToString:@"Overlay"]); /* edge smoothing, AO and anisotropy are on the effects page; the GPU keep-alive switch is on this one */
/* The display page's bottom right: contribute shaders and star the project, after the rows for a controller; the
version stays clear of them; a result shows for a moment where the version is. */
NSButton *contribute = settingsPanel.displayButtons[1], *starButton = settingsPanel.displayButtons[0];
Expand Down