diff --git a/docs/building.md b/docs/building.md index d41af214..6aae4898 100644 --- a/docs/building.md +++ b/docs/building.md @@ -272,7 +272,10 @@ writes come back at its fence. Pages both sides write keep a base copy, so each a VRAM mirror's write-back merge marks the pages it changed, and only those are compared at the fence; a lookup that reads guest memory itself (the uniform snapshot, `vk_mem_lookup`) does not queue its pool for the copy-in. In Kokiri Forest (1.1.1, RTX 4090) that costs about 1 ms a frame (copy-in 0.5 ms, write-back 0.6 ms), and the copies hold about -5 GB. `NX2NAVI_SHADOW=1` turns it on under Windows too. The Steam Deck's RADV has `VK_EXT_external_memory_host` as +5 GB. The GPU's bytes reach guest memory in the fence's completion callback, later than an import's, so the wait +after a frame's first submission covers the write-backs there (`runtime/vk_sync.c`, "the executed timeline"): the game +rewrites what those passes wrote without a fence, and a write-back that landed after the rewrite left the GPU adding +light onto its own last output, the whole scene brighter for a frame. `NX2NAVI_SHADOW=1` turns it on under Windows too. The Steam Deck's RADV has `VK_EXT_external_memory_host` as well: the shadow copies are chosen for the measured cost above, not for a missing extension. The device address table (`runtime/vk_gpa.c`) names a pool's shadow copy as it names an import, so shaders follow guest diff --git a/runtime/cfg.c b/runtime/cfg.c index 167c8a64..2e16200a 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -279,7 +279,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}, {"WB_PROBE", "0", "0|1", RESTART}, {"WB_SPLIT", "1", "0|1", RESTART}, {"WB_LAZY", WB_LAZY_DEFAULT, "0|1", RESTART}, {"WB_LAZY_TOUCH", "0", "0..16", RESTART}, {"WB_LAZY_CHUNK", "1", "0|1", RESTART}, {"FLUSH_UPLOAD", "0", "0|1", RESTART}, {"GPA_TWIN_MAX_KB", "0", "0..1048576", 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}, {"WB_PROBE", "0", "0|1", RESTART}, {"WB_SPLIT", "1", "0|1|2", RESTART}, {"WB_LAZY", WB_LAZY_DEFAULT, "0|1", RESTART}, {"WB_LAZY_TOUCH", "0", "0..16", RESTART}, {"WB_LAZY_CHUNK", "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 {"VMA_BLOCK_MB", "64", "16..256", RESTART}, // vk_headroom.c vk_vma_block_size {"ASTC_BC_PROBE", "0", "0|1", RESTART}, // vk_texture.c astc_probe_alpha diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index 9e7ee07a..d28dadf3 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -82,12 +82,35 @@ static int hold_max = -1; // write-backs follow in a part of their own, which signals the timeline as before. A submission without that split // signals both at its end. vk_sync_exec_wait waits on exec_tl; every other wait, the fences the game asks for among // them, still covers the write-backs. NX2NAVI_WB_SPLIT=0: no split, and the wait is the fence's. +// Not with shadow storage (vk_memory.c): there the game does need the write-backs before it goes on. With an import a +// write-back stores into guest memory on the GPU, right behind the passes. With a shadow copy it stores into the copy, +// and the bytes reach guest memory in the fence's completion callback (shadow_wb_cb), after the write-back part, the +// fence and the callbacks queued before it, while the game is already rewriting for its next frame what those passes +// wrote. A write-back that lands after such a rewrite puts the GPU's older bytes over it in guest memory and moves the +// page's base copy along, so the next copy-in finds nothing the CPU changed; and a rewrite with the value the base +// copy holds (a reset to zero) is no difference at any time. The GPU then goes on from its own last output, as the +// game's lighting accumulators do, which the shaders add to and the CPU resets. +// Measured: with the split the whole 3D scene came out brighter for single frames (3.8 of 255, the interface as it +// was), each of them a frame whose main work was held at its CPU event wait and replayed late, together with the next +// frame's: standing outdoors, 695, 624, 443, 323 and 2 of 1,800 frames in five runs, and none in eight runs without +// the split (RTX 5090 with NX2NAVI_SHADOW=1, 2026-11-10). A player on Linux (RX 7800 XT, RADV) saw the open field lit +// in one frame and flat in the next; that it is the same fault is not confirmed on their machine. Which words the +// write-backs overtook was not traced: NX2NAVI_SHADOW_CHECK counts no byte that both sides changed to different +// values, which fits a reset to the base copy's value. +// NX2NAVI_WB_SPLIT=2 keeps the split with shadow storage too, to measure that fault. static VkSemaphore exec_tl; static uint64_t exec_sig; // sub_lock: the newest value a submitted part signals on exec_tl static int wb_split = -1; int vk_sync_wb_split(void) { - if (wb_split < 0) wb_split = cfg_bool("NX2NAVI_WB_SPLIT"); + if (wb_split < 0) { + const char *e = cfg_get("NX2NAVI_WB_SPLIT"); + int forced = e && !strcmp(e, "2"); + wb_split = cfg_bool("NX2NAVI_WB_SPLIT") && (forced || !vk_mem_shadow_storage()); + if (vk_mem_shadow_storage()) + rt_log("vk: the wait after a frame's first submission %s (shadow storage; NX2NAVI_WB_SPLIT=2 leaves them out)", + wb_split ? "leaves out its write-backs" : "covers its write-backs and their copies into guest memory"); + } return wb_split && exec_tl != VK_NULL_HANDLE; }