diff --git a/games/totk/runtime/settings_game.h b/games/totk/runtime/settings_game.h index 6b5297ffb..024d5d3ab 100644 --- a/games/totk/runtime/settings_game.h +++ b/games/totk/runtime/settings_game.h @@ -12,6 +12,10 @@ 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) + long-pass split switch, default on only in BotW dm_gfx_pass_split_set (gfx/nvn/nvn_mtl.m), at once; + 1.9.0 (PASS_SPLIT_DEFAULT) + pass-split.txt above the game's folder (both games); + TOTK_PASS_SPLIT first (greyed out) resolution 720p / + MetalFX / 1080p / runtime_output_mode_set (game_patches*.c); the player picks, + MetalFX / 2K / 2K + MetalFX (a 4K screen) @@ -120,6 +124,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 */ +void dm_gfx_pass_split_start(int on); int dm_gfx_pass_split_locked(void); int dm_gfx_pass_split(void); void dm_gfx_pass_split_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 */ @@ -249,6 +254,7 @@ static void settings_restore_begin(void) { 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)); + if(!dm_gfx_pass_split_locked()&&runtime_shared_setting_word("pass-split.txt",word,sizeof word))dm_gfx_pass_split_start(!strncmp(word,"on",2)); settings_effects_restore(); /* Edge smoothing's resolve, kept in graphics-taa-mode.txt: "fast" (one compute pass) or "metalfx" (the default); TOTK_TAA_FAST decides first. The display page's edge smoothing row (off / fast / MetalFX) writes it. */ @@ -697,6 +703,14 @@ static NSArray *settings_rows(void) { 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");})];} + /* Long-pass split (nvn_mtl.m pass_split_try): a long render pass is committed in parts while the GPU idles, so the GPU + starts on it sooner. Breath of the Wild 1.9.0 uncapped on an M4 Max kept its GPU clock (641 MHz against 344-380 without) + and its frames (10.9 against 14-19 ms); the GPU then works more, so more power at an uncapped frame rate. On by default + only there (nvn_mtl.m PASS_SPLIT_DEFAULT); Tears of the Kingdom and Breath of the Wild 1.6.0 show it off until switched. */ + {NSString *t=settings_text("长通道拆分(GPU 少降频,帧更多)","Long-pass split (steadier GPU clock, more fps)"); + if(dm_gfx_pass_split_locked())[rows addObject:settings_fixed(t,settings_on_off(dm_gfx_pass_split()))]; + else [rows addObject:settings_row(t,nil,nil,^int{return dm_gfx_pass_split();}, + ^(int v){dm_gfx_pass_split_set(v);runtime_shared_setting_save("pass-split.txt",v?"on":"off","long-pass split");})];} int modes=runtime_output_modes(); NSMutableArray *res=[NSMutableArray array];NSMutableArray *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_4K_TO_5K_METALFX,OUTPUT_MODE_NATIVE_5K, diff --git a/games/totk/tests/command_q/probe.m b/games/totk/tests/command_q/probe.m index 00e8e47e1..baea22380 100644 --- a/games/totk/tests/command_q/probe.m +++ b/games/totk/tests/command_q/probe.m @@ -165,6 +165,7 @@ void runtime_controller_map_reset(void){} void dm_gfx_set_flag(const char *name,int value){(void)name;(void)value;} void dm_gfx_aniso_start(int n){(void)n;} int dm_gfx_aniso_locked(void){return 0;} int dm_gfx_aniso(void){return 0;} void dm_gfx_aniso_set(int n){(void)n;} /* the display page's anisotropy row (nvn_mtl.m) */ void dm_gfx_keepalive_start(int on){(void)on;} int dm_gfx_keepalive_locked(void){return 0;} int dm_gfx_keepalive(void){return 1;} void dm_gfx_keepalive_set(int on){(void)on;} /* the display page's GPU keep-alive row (nvn_mtl.m, PR #2049) */ +void dm_gfx_pass_split_start(int on){(void)on;} int dm_gfx_pass_split_locked(void){return 0;} int dm_gfx_pass_split(void){return 1;} void dm_gfx_pass_split_set(int on){(void)on;} /* the display page's long-pass split row (nvn_mtl.m) */ /* the leaderboard (board_upload.h): the graphics layer's frame record and picture settings, none here */ void fps_board_start(void){} void fps_board_reset(void){} int fps_board_counting(void){return 0;} int fps_board_snapshot(fps_board_stats *s){(void)s;return 0;} void mtl_board_view(int *ow,int *oh,int *sw,int *sh,int *mfx){*ow=*oh=*sw=*sh=*mfx=0;} void nvn_board_pacing(int *lock,int *interp){*lock=*interp=0;} int mtl_display_hz(void){return 0;} diff --git a/games/totk/third_party/d2r_gfx/gfx/nvn/hle_nvn.c b/games/totk/third_party/d2r_gfx/gfx/nvn/hle_nvn.c index 7dfc3951a..24ae3113b 100644 --- a/games/totk/third_party/d2r_gfx/gfx/nvn/hle_nvn.c +++ b/games/totk/third_party/d2r_gfx/gfx/nvn/hle_nvn.c @@ -3352,7 +3352,14 @@ static void tsummary(uint64_t frame) { if (pf && frame > pf) LOG("[nvn] 上传槽等待 至帧 %llu:每帧 %.2f 次 / %.3f ms", frame, (double)(un - pun) / (double)(frame - pf), (double)(uns - puns) / 1e6 / (double)(frame - pf)); pun = un; puns = uns; } { static uint64_t pcc; uint64_t mtl_chunk_commits(void), cc = mtl_chunk_commits(); - if (pf && frame > pf) LOG("[nvn] 分段提交 至帧 %llu:每帧额外 %.2f 段", frame, (double)(cc - pcc) / (double)(frame - pf)); + static uint64_t pps; uint64_t mtl_pass_splits(void), ps = mtl_pass_splits(); + if (pf && frame > pf) LOG("[nvn] 分段提交 至帧 %llu:每帧额外 %.2f 段,其中长通道拆分 %.2f 次 | chunks to frame %llu: %.2f extra a frame, %.2f of them long-pass splits", frame, (double)(cc - pcc) / (double)(frame - pf), + (double)(ps - pps) / (double)(frame - pf), frame, (double)(cc - pcc) / (double)(frame - pf), (double)(ps - pps) / (double)(frame - pf)); + { static uint64_t pb[5]; uint64_t b[5]; void mtl_pass_split_busy(uint64_t[5]); mtl_pass_split_busy(b); double nf = (double)(frame - pf); + if (pf && frame > pf && (b[0] - pb[0] || b[1] - pb[1] || b[2] - pb[2] || b[3] - pb[3] || b[4] - pb[4])) LOG("[nvn] long-pass split candidates to frame %llu, draws per frame: GPU idle %.1f, 1 command buffer in flight %.1f, 2 %.1f, 3+ %.1f, pass not splittable %.1f", + frame, (b[0] - pb[0]) / nf, (b[1] - pb[1]) / nf, (b[2] - pb[2]) / nf, (b[3] - pb[3]) / nf, (b[4] - pb[4]) / nf); + for (int i = 0; i < 5; i++) pb[i] = b[i]; } + pps = ps; pcc = cc; } if (synclog_env() && pf && frame > pf) { static uint64_t pc[2], pb[2], pn[2], pw[2]; double nf = (double)(frame - pf); for (int k = 0; k < 2; k++) { uint64_t c = atomic_load(&g_sw_calls[k]), b = atomic_load(&g_sw_blocked[k]), n = atomic_load(&g_sw_ns[k]), w = atomic_load(&g_sw_newest[k]); diff --git a/games/totk/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m b/games/totk/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m index e7dc905a7..f8f3fc2fb 100644 --- a/games/totk/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m +++ b/games/totk/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m @@ -4007,6 +4007,7 @@ static int diag_shader_prefix_csv_matches(const char *key,const char *prefixes) } static void dead_stat_print(unsigned long long frame); static void function_cache_set(int on); // below, with function_cache_on +static void pass_split_set(int draws); // TOTK_PASS_SPLIT, with draw_inner /* Tears of the Kingdom 1.4.3's "4K, MetalFX to 5K" picture preset (game_patches_v143.c, PR #2240): set as a flag by that runtime, read by output_upscale; 0 in every other game, which never sets it. */ static _Atomic int g_mfx_5k_output; @@ -4022,6 +4023,7 @@ void dm_gfx_set_flag(const char *name, int v) { if (!strcmp(name, "render_stream")) { atomic_store(&g_rs_on, v != 0); MLOG("[flag] render_stream = %d (next render pass)", v != 0); return; } if (!strcmp(name, "function_cache")) { function_cache_set(v); MLOG("[flag] function_cache = %d (border and varying shader variants looked up in the per-shader function cache)", v != 0); return; } if (!strcmp(name, "gpu_trace")) { gpu_trace_set(v); MLOG("[flag] gpu_trace = %d (pipeline binds recorded for GPU failure reports)", v != 0); return; } + if (!strcmp(name, "pass_split")) { pass_split_set(v); MLOG("[flag] pass_split = %d (a long render pass is split while the GPU idles, after that many draws; 0 off)", v > 0 ? v : 0); return; } if (!strcmp(name, "cube_merge")) { atomic_store(&g_cg_on, v != 0); MLOG("[flag] cube_merge = %d", v != 0); return; } if (!strcmp(name, "skip_invisible")) { skv_set(v); return; } if (!strcmp(name, "skipvc")) { atomic_store(&g_skipvc_any, v); MLOG("[flag] skipvc = %d", v); return; } @@ -18823,6 +18825,70 @@ static void oot_bar_before_draw(NvProg *pr) { static void oot_bar_note_draw(NvProg *pr) { if (g_oot_bar_mode) g_oot_bar_writes |= oot_bar_shader_writes(pr); } +/* TOTK_PASS_SPLIT (after seedds/nxnative's pass_split_try, its PR #1468): mid-submission commits (chunk_maybe_split) happen + only between render passes, so a long pass reaches the GPU in one piece at its end. Breath of the Wild 1.9.0 draws about + 2,700 of its 4,100 draws a frame into one G-buffer pass, and the GPU, done with the shadow passes before it, waits for the + whole pass to be translated (nxnative measured about 2.5 ms of a 9 ms frame; M4 Max at 120 fps, frame p50 / p90 + 8.75 / 9.33 -> 8.34 / 8.68 ms). At a draw, with nothing committed still on the GPU (g_q4_inflight) and TOTK_PASS_SPLIT + draws (PASS_SPLIT_DEFAULT, 0 off) since the last commit, the pass is ended, the chunk committed as chunk_maybe_split does, and + the draw opens the pass again: its attachments were stored (always, here) and are loaded, so the pictures are the same. + A split costs a store and a load of the attachments, so it is taken only while the GPU would otherwise wait, and at most + TOTK_PASS_SPLIT_MAX (default 2) times per NVN pass (a SetRenderTargets, g_cg.rt_seq). Not split: passes opened without + all their attachments (g_enc_mode), ranged, layered or cube-merged passes, hoisted or pending clears, the TBDR rewrites + and geometry draws. "flag pass_split " sets it while the game runs (0 off; A/B). */ +#define PASS_SPLIT_DRAWS 256 // the threshold the settings switch turns on, and the default where the split is on +/* On by default only where it was measured: Breath of the Wild 1.9.0 on the Mac (review of PR #2191). Other games, other + versions and iPhone / iPad start with it off; the settings switch and TOTK_PASS_SPLIT=256 turn it on there. Widen this + as more games are measured. */ +#if defined(GAME_BOTW190) && !TARGET_OS_IPHONE +#define PASS_SPLIT_DEFAULT PASS_SPLIT_DRAWS +#else +#define PASS_SPLIT_DEFAULT 0 +#endif +static _Atomic int g_pass_split = -1; +static uint64_t g_pass_splits, g_split_rtseq; static int g_split_n; +static uint64_t g_split_busy[5]; // draws past the threshold in an open pass: [0..3] by command buffers still in flight (0, 1, 2, 3+), [4] the pass may not be split +static int pass_split_draws(void) { + int v = atomic_load_explicit(&g_pass_split, memory_order_relaxed); + if (v < 0) { const char *e = getenv("TOTK_PASS_SPLIT"); v = e ? atoi(e) : PASS_SPLIT_DEFAULT; if (v < 0) v = 0; + int unset = -1; atomic_compare_exchange_strong(&g_pass_split, &unset, v); v = atomic_load(&g_pass_split); } + return v; +} +static void pass_split_set(int draws) { atomic_store(&g_pass_split, draws > 0 ? draws : 0); } +static int pass_split_max(void) { static int v = -1; if (v < 0) { const char *e = getenv("TOTK_PASS_SPLIT_MAX"); v = e ? atoi(e) : 2; } return v; } +uint64_t mtl_pass_splits(void) { return g_pass_splits; } +/* The settings panel's display page switch (settings_game.h, pass-split.txt; like the GPU keep-alive): on = PASS_SPLIT_DRAWS + draws, off = 0; with nothing saved it shows PASS_SPLIT_DEFAULT. TOTK_PASS_SPLIT decides first and greys the switch out. */ +int dm_gfx_pass_split_locked(void) { return getenv("TOTK_PASS_SPLIT") != NULL; } +int dm_gfx_pass_split(void) { return pass_split_draws() > 0; } +void dm_gfx_pass_split_start(int on) { if (!dm_gfx_pass_split_locked()) pass_split_set(on ? PASS_SPLIT_DRAWS : 0); } +void dm_gfx_pass_split_set(int on) { + if (dm_gfx_pass_split_locked() || dm_gfx_pass_split() == !!on) return; + pass_split_set(on ? PASS_SPLIT_DRAWS : 0); + MLOG("长通道拆分:设置面板%s | long-pass split: %s from the settings panel", on ? "打开" : "关闭", on ? "on" : "off"); +} +void mtl_pass_split_busy(uint64_t v[5]) { for (int i = 0; i < 5; i++) v[i] = g_split_busy[i]; } +static void pass_split_try(void) { + int n = pass_split_draws(); + if (n <= 0 || !g_enc || !g_chunk_size || !g_cb || g_frz_on || tbdr_on() || pt_active()) return; // chunk_maybe_split's conditions, and a pass open + if (g_stat_draw - g_chunk_mark < (uint64_t)n) return; // too soon + /* A continued pass (g_rt_merged: same-target SetRenderTargets, TOTK_RT_CONTINUE) is split like any other: ending one is + what a landed clear and a sampled attachment already do (rp_end), and the halves store and load as the passes did + before continuation. A counting occlusion query is too: each Metal pass counts into its own slot (vis_count_begin, + g_pass.serial) and the report adds all the slots since the reset. A pending clear is not: the reopened pass would + clear instead of loading. */ + int clears = R.clear_d || R.clear_s; for (int i = 0; i < 8; i++) clears |= R.clear_c[i]; + if (clears || g_enc_mode || g_rt_range_active || g_lay.active || g_cg_force_layers || g_hoist_n || g_defer_replaying || g_fz_checking || g_geo_ras.pr) { g_split_busy[4]++; return; } + { int busy = atomic_load_explicit(&g_q4_inflight, memory_order_relaxed); g_split_busy[busy > 3 ? 3 : busy < 0 ? 0 : busy]++; + if (busy) return; } // the GPU has work: a commit would only queue + if (g_split_rtseq != g_cg.rt_seq) { g_split_rtseq = g_cg.rt_seq; g_split_n = 0; } + if (pass_split_max() > 0 && g_split_n >= pass_split_max()) return; + g_split_n++; + rp_end(); + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + mtl_sub_split(); + if (!g_pass_splits++) MLOG("TOTK: pass split: a long render pass was ended and its chunk committed while the GPU had nothing to do (TOTK_PASS_SPLIT=%d, frame %llu)", n, (unsigned long long)atomic_load(&g_cur_frame)); +} static void draw_inner(const p_draw *dw, const p_draw_idx *di) { NSUInteger native_output_width=0,native_output_height=0; uint64_t native_output_source=0; @@ -18841,6 +18907,7 @@ static void draw_inner(const p_draw *dw, const p_draw_idx *di) { pr->key[0] ? [pr->key[0] substringToIndex:16].UTF8String : "-", pr->key[1] ? [pr->key[1] substringToIndex:16].UTF8String : "-", pr->key[2] ? [pr->key[2] substringToIndex:16].UTF8String : "-", (const void *)S.gst[0], (const void *)S.gst[1], (const void *)S.gst[2]); } } draw_skipped(); return; } // TOTK (iOS): a stage is not translated yet; Mac (async shaders): still in the background + pass_split_try(); // before anything of this draw touches the encoder: the draw opens the pass again when it was split oot_bar_before_draw(pr); if(mesh_indirect_mode()==2)mesh_indirect_written(pr); mesh_batch_written(pr); diff --git a/tests/settings_panel_probe.m b/tests/settings_panel_probe.m index 1e77fbf93..302f227ba 100644 --- a/tests/settings_panel_probe.m +++ b/tests/settings_panel_probe.m @@ -75,6 +75,11 @@ void dm_gfx_set_flag(const char *name, int value) { 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; } +static int fakePassSplit = 1; +void dm_gfx_pass_split_start(int on) { fakePassSplit = on != 0; } +int dm_gfx_pass_split_locked(void) { return 0; } +int dm_gfx_pass_split(void) { return fakePassSplit; } +void dm_gfx_pass_split_set(int on) { fakePassSplit = 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) */ @@ -1143,7 +1148,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 == 32 + 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 == 33 + 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 @@ -1589,7 +1594,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 == 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 */ + assert(settingsPanel.rows.count == 8 && [settingsPanel.rows[0].title isEqualToString:@"Overlay"]); /* edge smoothing, AO and anisotropy are on the effects page; the GPU keep-alive and long-pass split switches are on this one */ /* Every page's bottom right: 访问主页 (user 2026-11-11, was the display page's star), after the rows for a controller; the version stays clear of it; a result shows for a moment where the version is. */ NSButton *starButton = settingsPanel.homeButton;