From 71eb5fbe26a8f30aee0b3e44e4e5227be2c33b70 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 06:55:13 -0500 Subject: [PATCH] Make an area's textures at once after a load instead of over the first seconds, which showed the ground and walls black. The texture pool creates 16 entries a refresh and stops after 6 ms a frame, so that textures streaming in do not make long frames. An area load leaves thousands of slots at once (4,936 loading the test save), and at that rate they came in over the frames after the load screen: the scene's first frames showed a quarter of the picture as black stand-ins, for 22 presented frames on a free machine and for about three seconds with the CPU busy. While a refresh leaves pool_load_backlog slots or more (512; 0 never), the pool is loading: the count limit is off, a frame's refreshes may take pool_load_ms (50), and frame_hold leaves out the presents of frames drawn meanwhile (up to 2 s), so the picture before the load stays until fewer than 64 slots are left. Measured with the flight recorder's thumbnails of every present: the 4,847 textures are made in one frame of 345 ms, 30 frames before the scene fades in, and the black share of the picture goes from the fade straight to its final value. The streaming limits are as before below the backlog. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 5 +++++ runtime/cfg.c | 6 +++--- runtime/vk_backend.c | 22 +++++++++++++++++++++- runtime/vk_exec.h | 3 +++ runtime/vk_exec_draw.c | 42 +++++++++++++++++++++++++++++++++++------- 5 files changed, 67 insertions(+), 11 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 66978fbe..e742928e 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -625,6 +625,11 @@ # keeps its budget): an area load then spreads over several frames with stand-ins instead of holding one frame for # 200-400 ms. 0: no limit. # pool_create_ms = 6 +# An area load is told from streaming by the number of textures still to make: at this many or more, the two limits +# above give way (a frame may take pool_load_ms for them) and the picture before the load stays on screen until the +# area's textures are there, instead of showing the ground and the walls black for the first seconds. 0: never. +# pool_load_backlog = 512 +# pool_load_ms = 50 # Written storage-buffer ranges up to this size in MiB (1 to 256) go back to game memory as a merge, in 4 MiB pieces: # 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 diff --git a/runtime/cfg.c b/runtime/cfg.c index 284ce66f..b2b0dd95 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -68,7 +68,7 @@ static const char *const known[] = { "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", - "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", + "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", "VMA_BLOCK_MB", "ASTC_BC_PROBE", }; @@ -111,7 +111,7 @@ static const struct { const char *key, *section; } sections[] = { {"RECORDER_CORE", "performance"}, {"RECORDER_PRIO", "performance"}, {"BIND_REUSE", "performance"}, {"DYN_DELTA", "performance"}, {"STG_ARENA", "performance"}, {"SUBMIT_BATCH", "performance"}, {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, - {"POOL_CREATE_MS", "performance"}, + {"POOL_CREATE_MS", "performance"}, {"POOL_LOAD_BACKLOG", "performance"}, {"POOL_LOAD_MS", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, @@ -208,7 +208,7 @@ static const CfgMeta metas[] = { {"DYN_RES", "0", "0|1", LIVE}, {"DYN_RES_TARGET", "16.7", "1..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 vk_exec_record) - {"RECORDER_CORE", "auto", "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}, + {"RECORDER_CORE", "auto", "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_LOAD_BACKLOG", "512", "0..65536", RESTART}, {"POOL_LOAD_MS", "50", "1..1000", RESTART}, // held submissions handed to the queue together (vk_sync.c, submit batching) {"SUBMIT_BATCH", "4", "1..16", RESTART}, {"CACHE_DIE", "1", "0|1|ab", RESTART}, {"ONE_PER_CORE", "0", "0|1|ab", RESTART}, // perf.c cpu_placement diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 27a0e29b..91c7627d 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -112,8 +112,16 @@ void vk_present_drain(void) { // setting and does not use the allowance. It holds with DLSS Frame Generation too: a present left out is a longer gap // between two rendered frames to it (its constants and tags are set at the host present, gfx_present.c), and a dark // frame it is given comes out as several generated ones. +// And it covers an area load (vk_exec_draw.c vk_exec_pool_loading): while the texture pool still makes the area's +// textures, the frames show the ground and the walls as black stand-ins, so the picture before the load stays for up +// to LOAD_HOLD_NS, also beside the allowance. #define RESIZE_HOLD 6 -static struct { int64_t limit_ns, left_ns; uint64_t last_ns, held, runs, shown; int holding, resize_left; } FH = {.limit_ns = -1}; +#define LOAD_HOLD_NS 2000000000ull +static struct { + int64_t limit_ns, left_ns; + uint64_t last_ns, held, runs, shown, load_t0, load_held; + int holding, resize_left; +} FH = {.limit_ns = -1}; static void frame_hold_refill(uint64_t now) { if (FH.limit_ns < 0) { @@ -148,6 +156,18 @@ static int frame_hold(uint32_t skipped, int resized, uint64_t frame) { FH.resize_left--; return 1; } + if (FH.limit_ns > 0 && vk_exec_pool_loading()) { + uint64_t now = kern_now_ns(); + if (!FH.load_t0) FH.load_t0 = now, FH.load_held = 0; + if (now - FH.load_t0 < LOAD_HOLD_NS) { + FH.load_held++; + return 1; + } + } else if (FH.load_t0) { + rt_log("vk: frame %llu: the previous picture stayed for %llu frames, %.0f ms, while an area's textures were made", + (unsigned long long)frame, (unsigned long long)FH.load_held, (double)(kern_now_ns() - FH.load_t0) / 1e6); + FH.load_t0 = 0; + } int hold = skipped && FH.left_ns > 0 && !vk_dlssg_active(); if (hold && !FH.holding) FH.runs++; if (skipped && !hold) FH.shown++; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 2392a342..b551705d 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -189,6 +189,9 @@ void vk_exec_frame_log(const tex_t *presented, uint64_t frame); // a frame lacking them would be held back at its present (vk_backend.c frame_hold). uint32_t vk_exec_pending_draws_take(void); int vk_frame_hold_left(void); +// 1 while the texture pool makes the textures of an area load (vk_exec_draw.c pool_load_backlog): the frames drawn +// meanwhile show black stand-ins, and frame_hold leaves their presents out. +int vk_exec_pool_loading(void); // NX2NAVI_DUMP= (vk_dump.c): between presents frame-1 and frame, every pass's targets and every dispatch's storage // images are copied out and written as PNGs to NX2NAVI_DUMP_DIR (default "dump"), with passes.txt (targets, pipelines, diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index a0342f1c..a7b32f80 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -1928,13 +1928,30 @@ static uint16_t pool_kind_type(int k) { // default 16). The sync holds the texture lock throughout, and with 64 streaming in new textures made frames of 20-40 ms // with the replay and other threads waiting on that lock (24 ms of holds per slow frame in a dense area); 16 brought // that to 5 ms with no pop-in a player could see. The slots it leaves keep their stand-in until the next sync. +// An area load is not streaming. It leaves thousands of slots at once (4,925 in the load measured below), and at the +// streaming budget they came in over a hundred frames and more: the first seconds out of a load screen showed the +// ground, the walls and the buildings as their black stand-ins. So while a sync leaves NX2NAVI_POOL_LOAD_BACKLOG slots +// or more (default 512; 0: never) the pool is loading: the count budget is off, a frame's refreshes may take +// NX2NAVI_POOL_LOAD_MS (default 50), and the presents of those frames are left out so the picture before the load +// stays up (vk_backend.c frame_hold, vk_exec_pool_loading) until fewer than POOL_LOAD_END slots are left. The load +// screen lasts those few frames longer and the area appears whole. +#define POOL_LOAD_END 64 +static volatile int32_t pool_loading; // replay: a sync left an area load's worth of slots, and they are not made yet +static uint64_t pool_load_made, pool_load_frame0; +int vk_exec_pool_loading(void) { return __atomic_load_n(&pool_loading, __ATOMIC_RELAXED); } +static int pool_load_backlog(void) { + static int n = -1; + if (n < 0) n = cfg_int("NX2NAVI_POOL_LOAD_BACKLOG"); + return n; +} + static int pool_create_budget(void) { static int n = -1; if (n < 0) { n = cfg_int("NX2NAVI_POOL_CREATE_BUDGET"); if (n < 1) n = 1; } - return n; + return pool_loading ? 1 << 20 : n; } // The refresh runs before every draw that binds the pools, not once per submission, so the count above held for each @@ -1944,12 +1961,13 @@ static int pool_create_budget(void) { // and skip the slots left earlier in the frame (pool_defer) instead of looking them up again; the first refresh of // each frame keeps its count budget. The slots left keep their stand-in, a few frames longer in a large load. static uint64_t pool_create_ns(void) { - static int64_t ns = -1; + static int64_t ns = -1, load_ns; if (ns < 0) { - int ms = cfg_int("NX2NAVI_POOL_CREATE_MS"); + int ms = cfg_int("NX2NAVI_POOL_CREATE_MS"), lms = cfg_int("NX2NAVI_POOL_LOAD_MS"); + load_ns = (int64_t)(lms < 1 ? 1 : lms) * 1000000; ns = (int64_t)(ms < 0 ? 0 : ms) * 1000000; } - return (uint64_t)ns; + return (uint64_t)(pool_loading && ns ? load_ns : ns); } static uint32_t pool_defer[VK_POOL_TEX]; // per slot: 1 + the low bits of the frame a refresh left it in static VkWriteDescriptorSet pool_w[POOL_WBATCH]; @@ -2200,7 +2218,7 @@ static void pool_dirty_slot(int id, void *arg) { // vk_texture_dirty_slots_loc if (t >> 32 != PL.tok[id] >> 32) PL.n_by_reg++; else PL.n_by_addr++; if (pool_refill(d->ex, (uint32_t)id, d->used, t, d->spent)) PL.tok[id] = t; - else d->retry = 1; + else d->retry++; } // Every entry as the slots, kinds and samplers are now (pool_sync_entries has nothing to write). @@ -2246,7 +2264,7 @@ static void pool_sync_entries(VkExec *ex) { if (t >> 32 != PL.tok[id] >> 32) PL.n_by_reg++; else PL.n_by_addr++; if (pool_refill(ex, (uint32_t)id, used, t, spent)) PL.tok[id] = t; - else retry = 1; + else retry++; } else if (t && fresh) { pool_fill(ex, (uint32_t)id, fresh, t); } else if (t && ep != PL.epoch) { @@ -2257,7 +2275,17 @@ static void pool_sync_entries(VkExec *ex) { if (!walked) PL.n_slots += (uint64_t)n; vk_texture_create_budget_locked(-1); } - PL.retry = retry; + PL.retry = retry != 0; + // retry counts the slots this sync left; a sync with slots left walks the whole table, so that is all of them + int backlog = pool_load_backlog(); + if (!pool_loading && backlog > 0 && retry >= backlog) { + pool_load_made = (uint64_t)retry, pool_load_frame0 = PL.frame; + __atomic_store_n(&pool_loading, 1, __ATOMIC_RELEASE); + } else if (pool_loading && retry < POOL_LOAD_END) { + __atomic_store_n(&pool_loading, 0, __ATOMIC_RELEASE); + rt_log("vk: texture pool: an area load of about %llu textures was made in %llu frames (NX2NAVI_POOL_LOAD_BACKLOG)", + (unsigned long long)pool_load_made, (unsigned long long)(PL.frame - pool_load_frame0 + 1)); + } if (sg != PL.sgen) { int ns = vk_sampler_slot_count_locked(); for (int id = 0; id < VK_POOL_SAMP; id++) {