From a720ca879c9c20a04aa6c534a08c8714efccc52a Mon Sep 17 00:00:00 2001 From: chrg6 Date: Mon, 9 Nov 2026 12:42:39 -0500 Subject: [PATCH] Let NX2NAVI_GPA_TWIN_MAX_KB keep the device address table's second writes to small pools, to measure what the twins cost. A shader's store or atomic through the table goes to a pool's VRAM mirror and again to its guest copy, which on a discrete GPU crosses PCIe. On PCIe 3.0 (RTX 4090, Kokiri Forest, DLSS Quality) turning the twins off took the GPU from 16 to 13.1 ms a frame; the writes that went wrong without them were foliage counts and records in pools of 20 and 44 KiB. gpa_twin_max_kb = n gives a twin only to mirrors of pools up to n KiB. A larger mirror's record names the mirror twice, so its second write goes to the table's scratch page, its buffer fills stay in VRAM (vk_mem_guest_twin), and it is never evicted, since guest memory lacks what the shaders wrote into it. The mirror report counts the mirrors made without a twin. Default 0: every mirror has one, as before. Co-Authored-By: Claude Opus 5.5 --- nx2navi.ini.example | 5 +++++ runtime/cfg.c | 6 +++--- runtime/vk_memory.c | 50 ++++++++++++++++++++++++++++++++++++++------- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 52364db2..63d62522 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -565,3 +565,8 @@ # page the game wrote; 0 (default) copies only the written pages there, which saved 47-57 MiB of copies a frame in # Kokiri Forest. # flush_upload = 0 +# For measuring: only mirrors of the game's memory pools up to this many KiB send what shaders write through the +# device address table to game memory as well; larger mirrors keep it in video memory and are not evicted. On PCIe 3.0 +# those second writes cost about 3 ms of GPU time a frame. 0 (default): every mirror sends them. A pool that needs them +# and is left without shows as foliage stretched into columns or flickering. +# gpa_twin_max_kb = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index 0087deea..9e10c45e 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -43,7 +43,7 @@ static const char *const known[] = { "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", - "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", + "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", @@ -116,7 +116,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, }; @@ -228,7 +228,7 @@ static const CfgMeta metas[] = { {"VK_NOOPT", "0", "0|1", RESTART}, // vk_pipeline.c noopt_flag // diagnostics {"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART}, - {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, + {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, {"MEM_CENSUS", "0", "0|1", RESTART}, {"WB_NO_REUP", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", RESTART}, {"WB_MERGE_MAX", "32", "1..256", RESTART}, {"TEXUP_LOG_MS", "0", "0..60000", RESTART}, // vk_texture.c vk_texstat_frame {"PASS_CRUMBS", "", "0|1", RESTART}, // vk_crumbs.c pass marks; unset: VK_AMD_buffer_marker only {"INPUT_RECORD", "", NULL, RESTART}, diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 756c7b68..5fa1c921 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -65,6 +65,7 @@ typedef struct PoolRec { uint32_t wb_sub; // replay: the last submission that wrote mirror bytes back to guest memory uint32_t retry_sub; // replay: a mirror refused for the VRAM budget is asked again from here, 0 never uint8_t asked_room; // replay: its refusal already asked the textures for room (once until it gets one) + uint8_t twin; // MS_ON: the table's writes reach guest memory too (twin_wanted); 0: the mirror only uint32_t vd_wrote_sub, vd_use_sub; // verify mode: the last submission that bound it written / used its mirror uint64_t vd_wrote_n; // verify mode: written bindings of it so far uint32_t mgen; // gen_lock: every page write stamped up to this generation is in the mirror @@ -1472,9 +1473,10 @@ static void refuse(PoolRec *r, int why, const char *what) { // write-back or snapshot lists), and every GPU write to it went back to guest memory at the end of the submission that // made it (vk_mem_mirror_end), so the imported buffer holds everything. The device address table moves its pages back // to the imported buffer (vk_gpa_remap_plain); a shader of a submission in flight that reads the old record still finds -// the mirror, destroyed only once the submission being recorded is done (vk_defer_buffer). The pool is mirrored again, -// from a whole copy, on its next buffer use. Its freed bytes count as room at once (evict_room), until the GPU is past -// the submission and the destroy shows in VMA's figures. +// the mirror, destroyed only once the submission being recorded is done (vk_defer_buffer). A mirror without a twin +// (twin_wanted) is never a victim: what shaders wrote into it through the table is in the mirror alone. The pool is +// mirrored again, from a whole copy, on its next buffer use. Its freed bytes count as room at once (evict_room), until +// the GPU is past the submission and the destroy shows in VMA's figures. #define MIRROR_IDLE 120 #define RETRY_SOON 16 static int mirror_evict_on = -1; @@ -1506,6 +1508,7 @@ static uint64_t mirror_evict(uint64_t need, uint32_t min_idle, const char *why) PoolRec *r = mlist[i]; uint32_t age = msub - r->acc_sub; if (age < min_idle || r->wb_sub == msub || msub - r->wb_sub < 2) continue; + if (!r->twin && r->gpa_rec) continue; // the table's writes are in the mirror alone int at = nv; while (at > 0 && msub - v[at - 1]->acc_sub < age) at--; if (at == MAXV) continue; @@ -1550,6 +1553,31 @@ static uint64_t mirror_evict(uint64_t need, uint32_t min_idle, const char *why) return freed; } +// ---- twins (NX2NAVI_GPA_TWIN_MAX_KB, default 0: every mirror has one) +// Shaders write through the device address table into both a mirror and its pool's imported buffer, guest memory +// (vk_gpa.c, "The twin"): such writes are not recorded as GPU writes, so with the mirror alone the CPU never saw +// them and a sync of a page the CPU wrote copied the stale guest page over them. On a discrete GPU the second write +// crosses PCIe: on PCIe 3.0 the twins cost about 3 ms of GPU time a frame in Kokiri Forest (NX2NAVI_GPA_TWIN=0 against +// on). The writes that went wrong without them were the foliage counts and records in pools of 20 and 44 KiB. +// GPA_TWIN_MAX_KB=n gives a twin only to mirrors of pools up to n KiB; a larger mirror keeps the table's writes to +// itself (its record names the mirror twice, which sends the second write to the table's scratch page) and its +// buffer fills stay in the mirror (vk_mem_guest_twin). Such a mirror is never evicted (mirror_evict), since guest +// memory lacks what the shaders wrote into it. For measuring which pools need a twin: a pool that does and is left +// without draws stretched or flickering foliage. +static int64_t twin_max = -1; // bytes; 0 no limit +static int64_t m_untwinned_n, m_untwinned_bytes; + +static int twin_wanted(const PoolRec *r) { + if (twin_max < 0) { + int kb = cfg_int("NX2NAVI_GPA_TWIN_MAX_KB"); + twin_max = kb > 0 ? (int64_t)kb << 10 : 0; + if (twin_max) + rt_log("mem: only mirrors of pools up to %d KiB send the device address table's writes to guest memory " + "too (NX2NAVI_GPA_TWIN_MAX_KB); larger ones keep them and are not evicted", kb); + } + return !twin_max || (int64_t)(r->import_hi - r->import_lo) <= twin_max; +} + // Replay thread: the pool's mirror on its first buffer use. static void mirror_create(PoolRec *r) { VkCtx *c = vk_ctx(); @@ -1624,7 +1652,15 @@ static void mirror_create(PoolRec *r) { r->mstate = MS_ON; r->asked_room = 0; // The device address table names the mirror from now on: the GPU's writes go there, and the sync copies the CPU's. - if (r->gpa_rec) r->gpa_rec = vk_gpa_remap(r->import_lo, r->import_hi, r->gpa_rec, r->mbuf, r->import_lo); + // With a twin they also go to the imported buffer; without one (twin_wanted) the record names the mirror twice. + r->twin = (uint8_t)twin_wanted(r); + if (!r->twin) { + plat_inc64(&m_untwinned_n); + plat_add64(&m_untwinned_bytes, (int64_t)n); + } + if (r->gpa_rec) + r->gpa_rec = r->twin ? vk_gpa_remap(r->import_lo, r->import_hi, r->gpa_rec, r->mbuf, r->import_lo) + : vk_gpa_remap_plain(r->import_lo, r->import_hi, r->gpa_rec, r->mbuf, r->import_lo); range_add(&pend, &npend, &cpend, r, 0, n); mrep.first_n++, mrep.first_bytes += n; plat_inc64(&m_live_n); @@ -1749,7 +1785,7 @@ void vk_mem_pick(VkMemSpan *sp) { VkBuffer vk_mem_guest_twin(const VkMemSpan *sp) { const PoolRec *r = sp->pool; - return r && r->mstate == MS_ON && sp->buffer == r->mbuf ? r->buf : VK_NULL_HANDLE; + return r && r->mstate == MS_ON && r->twin && sp->buffer == r->mbuf ? r->buf : VK_NULL_HANDLE; } int vk_mem_mirror_probe(uint64_t addr) { @@ -2410,7 +2446,7 @@ static void mirror_log(uint64_t frame) { "first use %.2f pools %.3f MiB, write-back %.3f MiB in %.1f ranges, merge %.3f MiB in %.1f dispatches " "(snapshots %.3f MiB, new buffers %.2f), flush calls %.1f (%.3f MiB), queries " "%.1f pre-snap (%.1f MiB) + %.1f sync, prologues %.2f, tracking cpu %.3f ms (pre-snap %.3f, syncs %.3f); so far " - "evicted %lld (%.1f MiB), budget refusals asked again %lld%s", + "evicted %lld (%.1f MiB), budget refusals asked again %lld, made without a twin %lld (%.1f MiB)%s", (unsigned long long)frame, (long long)m_live_n, m_live_bytes / M, (long long)m_refused_n[MR_BUDGET], m_refused_bytes[MR_BUDGET] / M, (long long)m_refused_n[MR_UNTRACKED], (long long)m_refused_n[MR_OVERLAP], (long long)m_refused_n[MR_ALLOC], a.subs / f, a.syncs / f, a.upload_bytes / f / M, a.upload_copies / f, @@ -2418,7 +2454,7 @@ static void mirror_log(uint64_t frame) { a.merge_dispatches / f, a.snap_bytes / f / M, a.snap_allocs / f, fc / f, fb / f / M, a.presnap_queries / f, a.presnap_bytes / f / M, a.sync_queries / f, a.prologues / f, qpc_ms(a.presnap_t + a.sync_t) / f, qpc_ms(a.presnap_t) / f, qpc_ms(a.sync_t) / f, (long long)m_evicted_n, m_evicted_bytes / M, (long long)m_retries, - ver); + (long long)m_untwinned_n, m_untwinned_bytes / M, ver); rt_log("mem: mirror frame %llu: upload sources per frame, overlaps counted twice: written pages %.3f MiB; flush ranges " "%.2f, %.3f MiB, from %.1f flushes of %.3f MiB; flushed and not copied (write-watched) %.3f MiB", (unsigned long long)frame, a.page_bytes / f / M, a.flush_n / f,