diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 4bd4d65b..dabaa738 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -483,3 +483,7 @@ # the next refresh and show their stand-in until then. Lower spreads streaming over more frames (smaller hitches, a # frame or two of pop-in); higher loads sooner. # pool_create_budget = 16 +# Written storage-buffer ranges up to this size in MiB (1 to 16) go back to game memory as a merge: copied within video +# memory and only the words that changed written over PCIe. Larger ones are copied back whole at the end of every +# submission. Raising it from 4 to 16 took a 5.5 MiB range off the plain copies (about 46 MiB a frame in the field). +# wb_merge_max = 16 diff --git a/runtime/cfg.c b/runtime/cfg.c index 97e99b18..bdd3fdb7 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -62,7 +62,7 @@ static const char *const known[] = { "SWKBD_ANSWER", "SET_AT", "SWKBD_TEST", "TAA", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TEX_RELEASED_MB", "TEX_COMMIT_FREE_MB", "TEX_EVICT", "MIRROR_EVICT", "VRAM_BUDGET_MB", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NOOPT", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECONVERGE", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", - "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", + "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WB_MERGE_MAX", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "ASTC_GPU_DESWIZZLE", "ASTC_SCRATCH_KEEP", "ASTC_ARENA", "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", @@ -115,7 +115,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, - {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, + {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, }; // The settings the menu shows: the value in effect when the key is unset, and what the key accepts. The only place a @@ -220,7 +220,7 @@ static const CfgMeta metas[] = { // 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}, - {"TEXUP_LOG_MS", "0", "0..60000", RESTART}, // vk_texture.c vk_texstat_frame + {"WB_MERGE_MAX", "16", "1..16", 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 97cc385a..51d75aa7 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -1710,7 +1710,24 @@ int vk_mem_mirror_probe(uint64_t addr) { #define SNAP_CHUNK (1u << 20) #define MERGE_SPAN (64ull << 20) // the bytes of one pool a merge dispatch binds at most #define MERGE_WORDS (65535u * 64u) // the words one merge dispatch covers at most -#define MERGE_MAX (4ull << 20) // larger written ranges stay plain copies +// Larger written ranges stay plain copies (NX2NAVI_WB_MERGE_MAX, MiB, default 16, at most what one dispatch covers). +// A plain copy writes the whole range over PCIe at the end of each submission even when the shader changed a few words; +// a merge copies it within video memory and writes only the words that changed. With a 4 MiB limit the field ran about +// 46 MiB of plain write-back a frame in ranges of about 5.5 MiB, just above it; at 16 the same outdoor route went from a +// median 47.5 to 67.3 fps and from 11.3 to 7.2 ms of GPU time a frame (RTX 4080 SUPER, 120 fps limit, 2026-11-08). +#define MERGE_MAX_CAP ((uint64_t)MERGE_WORDS * 4u) +static uint64_t merge_max(void) { + static uint64_t m; + if (!m) { + const char *e = cfg_get("NX2NAVI_WB_MERGE_MAX"); + uint64_t mib = e && *e ? (uint64_t)strtoull(e, NULL, 10) : 16; + m = mib << 20; + if (!m) m = 1; + if (m > MERGE_MAX_CAP) m = MERGE_MAX_CAP; + rt_log("mem: write-back merge covers ranges up to %.1f MiB (NX2NAVI_WB_MERGE_MAX)", m / 1048576.0); + } + return m; +} typedef struct { PoolRec *r; uint64_t off, size, soff; int chunk; } SnapRange; typedef struct { VkBuffer buf; VmaAllocation alloc; uint64_t size, used; int host; } SnapChunk; typedef struct { int n; SnapChunk c[]; } SnapFree; // a submission's chunks and range lists, pooled at its fence @@ -1722,8 +1739,8 @@ static int nchunks, cchunks; // the pool holds the frames in flight: a VMA allocation that needs a new memory block costs 8-11 ms on the replay // thread. Chunk sizes are powers of two so that one fits the next request; what the pool cannot hold is destroyed. #define SNAP_LIST_MIN (64u << 10) -#define SNAP_POOL_N 32 -#define SNAP_POOL_BYTES (16ull << 20) +#define SNAP_POOL_N 64 +#define SNAP_POOL_BYTES (256ull << 20) // room for merges of multi-MiB ranges across the submissions in flight static PlatMutex snap_pool_lock = PLAT_MUTEX_INIT; static SnapChunk snap_pool[SNAP_POOL_N]; static int nsnap_pool; @@ -1929,7 +1946,7 @@ static void gpu_wrote(const VkMemSpan *sp, uint64_t size, int shader) { if (!r || r->mstate != MS_ON || sp->buffer != r->mbuf || !size) return; r->vd_wrote_sub = msub, r->vd_wrote_n++; // A binding to the end of its pool (no size) can span hundreds of MiB: those stay copies. - if (shader && !(sp->offset & 3) && !(size & 3) && size <= MERGE_MAX && merging() && snap_add(r, sp->offset, size)) + if (shader && !(sp->offset & 3) && !(size & 3) && size <= merge_max() && merging() && snap_add(r, sp->offset, size)) return; range_add(&wbl, &nwbl, &cwbl, r, sp->offset, size); }