From 27009e85f34aa10646ec24503a5899e48a259c4f Mon Sep 17 00:00:00 2001 From: Tom Pipe Date: Tue, 10 Nov 2026 12:45:08 -0500 Subject: [PATCH 1/8] Link dav1d in tools/build_game.py, so the developer build links again. The developer build stopped at undefined dav1d symbols since the AV1 movie decoder (runtime/ov_movie2.c) came in: tools/make_game.py linked dav1d, tools/build_game.py did not. Windows links libdav1d.a statically, as libvpx and zstd are, so the exe imports no libdav1d DLL; Linux links -ldav1d. The llvm-mingw link list and the Linux release's static list get dav1d too, and docs/building.md lists the package for MSYS2 and Arch. From pull request #439, with the two other link lists added. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 4 ++-- tools/build_game.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/building.md b/docs/building.md index 8237793c..3da1c6ca 100644 --- a/docs/building.md +++ b/docs/building.md @@ -24,7 +24,7 @@ Versions are the ones the build was verified with; newer patch releases are fine MSYS2 packages, after `pacman -Syuu` (run it twice; the first pass updates the core and exits): ```sh -pacman -S --needed --noconfirm mingw-w64-ucrt-x86_64-{clang,lld,ninja,gcc,make,pkgconf,sdl3,opus,libvpx,zstd,vulkan-devel,\ +pacman -S --needed --noconfirm mingw-w64-ucrt-x86_64-{clang,lld,ninja,gcc,make,pkgconf,sdl3,opus,libvpx,dav1d,zstd,vulkan-devel,\ vulkan-memory-allocator,vulkan-validation-layers,shaderc,spirv-tools} base-devel git unzip ``` @@ -213,7 +213,7 @@ Requirements: - Packages on Arch: ```sh - pacman -S --needed clang lld ninja make sdl3 opus libvpx zstd vulkan-headers vulkan-icd-loader \ + pacman -S --needed clang lld ninja make sdl3 opus libvpx dav1d zstd vulkan-headers vulkan-icd-loader \ vulkan-validation-layers shaderc spirv-tools dotnet-sdk uv git ``` diff --git a/tools/build_game.py b/tools/build_game.py index 33a0236f..e4eaad0b 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -5,7 +5,7 @@ recompiled only when it, a header it includes or its flags change. On Linux the same steps make build//nx2navi with the clang, lld and ninja on PATH (or the clang and lld in --toolchain DIR/bin): the runtime's *_sdl.* and platform/linux.c take the place of its *_win.*, platform/win32.c and nx2navi.rc, the libraries are -the system's (Vulkan, SDL3, Opus, libvpx, zstd; libstdc++ static), and the executable finds a libSDL3.so.0 beside it +the system's (Vulkan, SDL3, Opus, dav1d, libvpx, zstd; libstdc++ static), and the executable finds a libSDL3.so.0 beside it first (rpath $ORIGIN). docs/building.md lists the packages. .venv\\Scripts\\python tools\\build_game.py # generated/v121 -> build/v121/nx2navi.exe @@ -45,8 +45,8 @@ of its processes (Windows job object accounting: committed private bytes). --ninja PATH names the Ninja to run; without MSYS2 the bin/ninja.exe of the release folder (tools/make_release.py) is used. --toolchain DIR builds with an llvm-mingw tree (DIR/bin/clang.exe) instead of MSYS2 UCRT64 clang. Ninja still comes -from MSYS2. The libraries and headers MSYS2 supplies (Vulkan, SDL3, Opus, libvpx, VMA) then come from --deps DIR -(default toolchain/deps-mingw: include/, lib/ with the import libraries and libvpx.a, bin/ with the DLLs), and the C++ +from MSYS2. The libraries and headers MSYS2 supplies (Vulkan, SDL3, Opus, dav1d, libvpx, VMA) then come from --deps DIR +(default toolchain/deps-mingw: include/, lib/ with the import libraries, libdav1d.a and libvpx.a, bin/ with the DLLs), and the C++ runtime is libc++ from llvm-mingw. Such a build never reuses another build's generated objects. Split build. --make-runtime-lib compiles only runtime/ into /libnx2navirt.a (what CI prebuilds). --runtime-lib PATH @@ -145,14 +145,14 @@ if WIN: RT_FLAGS.append('-D_WIN32_WINNT=0x0A00') # -Xlinker passes the map path as one argument; -Wl, would split it at any comma in the build dir path LINK_FLAGS = ['-fuse-ld=lld', '-Wl,--stack,16777216', '-Xlinker', '"--Map=$builddir/nx2navi.map"'] # the map feeds tools/prof_report.py - LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libzstd.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end; libvpx static (movies); libzstd static (resolution.c) + LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lSDL3', '-lopus', '-l:libdav1d.a', '-l:libvpx.a', '-l:libzstd.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end; libvpx and libdav1d static (movies); libzstd static (resolution.c) VMA_INCLUDE = UCRT.parent / 'include' / 'vma' # vk_mem_alloc.h; MSYS2 package vulkan-memory-allocator else: # -no-pie: the lifted modules' function tables (hundreds of thousands of pointers) need no load-time relocation. # $ORIGIN: a libSDL3.so.0 next to nx2navi comes before the system's (the release's portable folder). LINK_FLAGS = ['-fuse-ld=lld', '-no-pie', '-Wl,-z,noexecstack', "'-Wl,-rpath,$$ORIGIN'", '"-Wl,-Map=$builddir/nx2navi.map"'] - LINK_LIBS = ['-lvulkan', '-lSDL3', '-lopus', '-lvpx', '-lzstd', '-l:libstdc++.a', '-lm', '-lpthread', '-ldl'] + LINK_LIBS = ['-lvulkan', '-lSDL3', '-lopus', '-ldav1d', '-lvpx', '-lzstd', '-l:libstdc++.a', '-lm', '-lpthread', '-ldl'] VMA_INCLUDE = Path('/usr/include') # vk_mem_alloc.h; Arch (AUR) vulkan-memory-allocator, Debian libvulkan-memory-allocator-dev # Without a system vk_mem_alloc.h, the header the Windows release pins (tools/make_release.py, VMA 3.4.0) is fetched once # into toolchain/vma: one MIT header (THIRD_PARTY_NOTICES.md), checked against its hash. @@ -182,7 +182,7 @@ def vma_include(): # --toolchain: an llvm-mingw tree. The libraries, headers and DLLs MSYS2 supplies come from the deps directory instead. LLVM_LINK_LIBS = ['-lpsapi', '-lbcrypt', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lvulkan-1', '-lSDL3', '-lopus', - '-l:libvpx.a', '-l:libzstd.a', '-l:libc++.a', '-l:libc++abi.a', '-l:libunwind.a', '-l:libwinpthread.a'] + '-l:libdav1d.a', '-l:libvpx.a', '-l:libzstd.a', '-l:libc++.a', '-l:libc++abi.a', '-l:libunwind.a', '-l:libwinpthread.a'] DEFAULT_DEPS = ROOT / 'toolchain' / 'deps-mingw' # Runtime sources compiled per install, not in the library: romfs.c includes romfs_config.h, patches.c and ov_zstd.c # the patches.h of the player's game version (its patch page is the page after that version's main; ov_zstd.c's host @@ -856,9 +856,9 @@ def main(): DEPS = (args.deps or DEFAULT_DEPS).resolve() dlls = sorted((DEPS / 'bin').glob('*.dll')) if WIN else [] if DEPS and not WIN: - # A release (--deps): Opus, libvpx and zstd go into the executable, so it needs no more than libc, Vulkan and - # the libSDL3.so.0 beside it - for lib in ('opus', 'vpx', 'zstd'): + # A release (--deps): Opus, dav1d, libvpx and zstd go into the executable, so it needs no more than libc, + # Vulkan and the libSDL3.so.0 beside it + for lib in ('opus', 'dav1d', 'vpx', 'zstd'): LINK_LIBS[LINK_LIBS.index(f'-l{lib}')] = f'-l:lib{lib}.a' if args.sysroot and not WIN: # Every compile and the link: the game then needs no newer glibc than the sysroot's (the Steam Runtime's) From 0d9417a8c7ed4e9134187236ea22a90d4b579bc5 Mon Sep 17 00:00:00 2001 From: Shikakiben <108907095@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:45:33 -0500 Subject: [PATCH 2/8] Check zbic in the Linux AppImage the way make_game.py imports it. The AppImage tested for zbic with PYTHONPATH and the user's site-packages in view, then ran the build with Python's isolated mode, which sees neither. A player with zbic installed for their own Python of the same minor version passed the test, never got it installed where the build looks, and was told to run the pip command that had put it in the place the build ignores. The test now uses the same isolation and the same search path as the build, so zbic is installed into the AppImage's own folder whenever the build could not import it. From pull request #436. Co-Authored-By: Claude Fable 5.2 --- tools/make_appimage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/make_appimage.py b/tools/make_appimage.py index 99696708..79991371 100644 --- a/tools/make_appimage.py +++ b/tools/make_appimage.py @@ -91,7 +91,7 @@ EOF exit 1 fi -if ! PYTHONPATH="$PYDEPS" "$PY" -c 'import zbic' 2>/dev/null; then +if ! "$PY" -I -c 'import sys; sys.path[:0] = [sys.argv[1]]; import zbic' "$PYDEPS" 2>/dev/null; then echo "Installing zbic (GPL-2.0, unpacks the game's program files) into $PYDEPS ..." "$PY" -m pip install --quiet --disable-pip-version-check --target "$PYDEPS" zbic || { echo "error: pip could not install zbic (it needs the internet once)" >&2 From 86c0fe4199a8676d7eae467b23286dd44aacbaf1 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:47:22 -0500 Subject: [PATCH 3/8] Let a game build on a network share get past a short dropout and a share that refuses private folders. A player who keeps the game on a mapped network drive could not finish a build. Three things change in tools/make_game.py: - The output folder keeps its drive letter on Windows. It was resolved to the share's UNC name, which is a second connection to the same server, and some servers drop that one under the compilers' load while the mapped drive stays up. - When a source's size cannot be read because its folder stopped answering, the build says so and asks again for up to a minute before it stops with a message that names the drive. It used to end in a traceback. A file that is gone from a folder that answers still ends the build at once. - A finished file is put in place through a temporary file beside it instead of a file in a temporary folder. Such a folder is made for its owner alone, and some shares refuse that while they take ordinary files; the build then reported a permissions fault in the output folder after everything had compiled. From pull request #419: the first and third change are the contributor's, the wait is written again so that a file that is really gone does not wait a minute, and the launcher tests cover all three. Co-Authored-By: Matthias Co-Authored-By: Claude Fable 5.2 --- tests/launcher_test.py | 76 ++++++++++++++++++++++++++++++++++++++++++ tools/make_game.py | 59 ++++++++++++++++++++++++++++---- 2 files changed, 128 insertions(+), 7 deletions(-) diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 92837ed8..915ee895 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1685,6 +1685,82 @@ class MakeGameTest(Tmp): self.assertEqual(dst.stat().st_mode & 0o777, src.stat().st_mode & 0o777) self.assertFalse(self.mg.copy_if_changed(src, dst)) + def test_copy_if_changed_makes_no_folder(self): + """A network share can refuse the owner-only folder TemporaryDirectory makes: the copy needs a file only, and + leaves nothing behind, also when the copy fails.""" + out = self.tmp / 'share' + out.mkdir() + src, dst = self.tmp / 'new.bin', out / 'installed.bin' + src.write_bytes(b'new') + dst.write_bytes(b'old') + refused = mock.patch.object(self.mg.tempfile, 'TemporaryDirectory', side_effect=PermissionError('refused')) + with refused, mock.patch.object(self.mg.os, 'mkdir', side_effect=PermissionError('refused')): + self.assertTrue(self.mg.copy_if_changed(src, dst)) + self.assertEqual(dst.read_bytes(), b'new') + self.assertEqual([p.name for p in out.iterdir()], ['installed.bin']) + src.write_bytes(b'newer') + with mock.patch.object(self.mg.shutil, 'copy2', side_effect=OSError('the share went away')): + with self.assertRaises(OSError): + self.mg.copy_if_changed(src, dst) + self.assertEqual(dst.read_bytes(), b'new') # the installed file is as it was + self.assertEqual([p.name for p in out.iterdir()], ['installed.bin']) + + def test_source_size_waits_for_a_share(self): + """A share that stops answering for a moment: the source's folder is not there either, and the size is asked + again. A folder that answers without the file ends the build at once.""" + src = self.tmp / 'lift' / 'a.c' + src.parent.mkdir() + src.write_bytes(b'int a;\n') + real_stat, real_is_dir = Path.stat, Path.is_dir + down = {'left': 3} + + def stat(p, *a, **k): + if p == src and down['left'] > 0: + raise FileNotFoundError(2, 'The network path was not found', str(p)) + return real_stat(p, *a, **k) + + def is_dir(p): # each look at the folder is one try nearer to the share's return + if p == src.parent and down['left'] > 0: + down['left'] -= 1 + return False + return real_is_dir(p) + + lines = [] + with mock.patch.object(Path, 'stat', stat), mock.patch.object(Path, 'is_dir', is_dir), \ + mock.patch.object(self.mg.time, 'sleep') as sleep, mock.patch.object(self.mg, 'say', lines.append): + self.assertEqual(self.mg.source_size(src), 7) + self.assertEqual(sleep.call_count, 3) # three tries found the share down + self.assertEqual(len(lines), 1) # said once, not at every try + # the share never comes back: the build stops after SOURCE_WAIT and names the drive + down['left'] = 10 ** 6 + clock = iter(range(0, 10 ** 6, 5)) + with mock.patch.object(Path, 'stat', stat), mock.patch.object(Path, 'is_dir', is_dir), \ + mock.patch.object(self.mg.time, 'sleep'), mock.patch.object(self.mg, 'say'), \ + mock.patch.object(self.mg.time, 'monotonic', lambda: next(clock)), \ + mock.patch.object(self.mg, 'fail', side_effect=SystemExit) as fail: + with self.assertRaises(SystemExit): + self.mg.source_size(src) + self.assertIn('stopped answering', fail.call_args[0][0]) + # the file is gone and its folder answers: no wait + down['left'] = 0 + src.unlink() + with mock.patch.object(self.mg.time, 'sleep') as sleep, \ + mock.patch.object(self.mg, 'fail', side_effect=SystemExit) as fail: + with self.assertRaises(SystemExit): + self.mg.source_size(src) + sleep.assert_not_called() + self.assertIn('lift folder', fail.call_args[0][0]) + + def test_out_path_keeps_a_drive_letter(self): + """Windows: --out is made absolute without resolve(), which would turn a mapped drive into its UNC name.""" + with mock.patch.object(self.mg, 'WIN', True), \ + mock.patch.object(Path, 'resolve', side_effect=AssertionError('resolve() was called')): + got = self.mg.out_path('game') + self.assertEqual(got, Path(os.path.abspath('game'))) + self.assertTrue(got.is_absolute()) + with mock.patch.object(self.mg, 'WIN', False): + self.assertEqual(self.mg.out_path(self.tmp / 'x' / '..' / 'game'), (self.tmp / 'game').resolve()) + @unittest.skipUnless(shutil.which('cc'), 'needs a C compiler') def test_object_cache(self): src = self.tmp / 'src' diff --git a/tools/make_game.py b/tools/make_game.py index 12d2476c..49cecb37 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -534,7 +534,9 @@ def pe_imports(path): def copy_if_changed(src, dst): """Atomically replace dst unless its contents match, even when source modification times are normalized. - A same-directory temporary copy preserves modes, replaces read-only files on POSIX and leaves dst intact on copy failure.""" + A same-directory temporary copy preserves modes, replaces read-only files on POSIX and leaves dst intact on copy + failure. The copy is a temporary file, not a file in a temporary folder: such a folder is made for its owner alone, + and some network shares refuse that while they take ordinary files.""" dst = Path(dst) s = os.stat(src) try: @@ -543,13 +545,21 @@ def copy_if_changed(src, dst): return False except FileNotFoundError: pass + replacement = None try: - with tempfile.TemporaryDirectory(dir=dst.parent, prefix='.nx2navi-copy-') as tmp: - replacement = Path(tmp) / dst.name - shutil.copy2(src, replacement) - os.replace(replacement, dst) + fd, name = tempfile.mkstemp(dir=dst.parent, prefix='.nx2navi-copy-') + os.close(fd) + replacement = Path(name) + shutil.copy2(src, replacement) + os.replace(replacement, dst) except PermissionError: fail(f'cannot replace {dst}: close the game and check output-directory permissions') + finally: + if replacement is not None: # still there only when the copy or the replace failed + try: + replacement.unlink(missing_ok=True) + except OSError: + pass return True @@ -832,6 +842,34 @@ def earlier_kits(): return sorted(kits) +SOURCE_WAIT, SOURCE_PAUSE = 60, 2 # seconds source_size waits for a drive in all, and between two tries + + +def source_size(src): + """The size of a source file about to be compiled. The sources sit in the build cache beside the game, which a + player may keep on a network share, and a share that loses its connection under the build's load answers 'not there' + until it is back: while the source's folder does not answer either, the question is asked again for up to + SOURCE_WAIT seconds. A folder that answers without the file means the file is gone, which no wait brings back.""" + src = Path(src) + end = time.monotonic() + SOURCE_WAIT + told = False + while True: + try: + return src.stat().st_size + except OSError as e: + away = not src.parent.is_dir() + if not away or time.monotonic() >= end: + fail(f'cannot read {src} ({e.strerror or e}): ' + + ('the drive or network share with the game folder stopped answering; check it and run the ' + 'build again' if away else + 'the file is gone from the build cache; delete the lift folder in the build cache and run ' + 'the build again, which makes it anew')) + if not told: + say(f' {src.parent} does not answer; waiting up to {SOURCE_WAIT} s for the drive to come back') + told = True + time.sleep(SOURCE_PAUSE) + + def compile_all(srcs, cmd_for, cache, jobs, env, progress=None, salt='', stop=None, alts=None): """Compile each (source, object) whose object is not current in cache. cmd_for(src, dst) -> argv without the dependency-file options; salt identifies the compiler. Returns (compiled, reused, failures) with failures as @@ -853,7 +891,7 @@ def compile_all(srcs, cmd_for, cache, jobs, env, progress=None, salt='', stop=No reused = len(srcs) - len(todo) if adopted: say(f' {adopted} objects an earlier version compiled are kept: the same compile of the same sources') - todo.sort(key=lambda job: job[0].stat().st_size, reverse=True) + todo.sort(key=lambda job: source_size(job[0]), reverse=True) def cc(job): src, dst = job @@ -1774,6 +1812,13 @@ def step_key(step, identity, out=None): f'|{sha256_file(xlate_source() / "nx2navi_xlate.id")}') +def out_path(arg): + """--out as an absolute path. On Windows a mapped drive letter stays as given: resolve() would name the folder by + its server and share instead (a UNC path), which is a second connection to the same share, and some servers drop + that one under the compilers' load while the mapped drive stays up.""" + return Path(os.path.abspath(arg)) if WIN else Path(arg).resolve() + + def main(): global EVENTS ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) @@ -1804,7 +1849,7 @@ def main(): ap.error('give --nup, both --exefs and --romfs, or --rebuild') if a.rebuild and not a.out: ap.error('--rebuild needs --out') - out = Path(a.out).resolve() if a.out else None + out = out_path(a.out) if a.out else None cache = out / 'build-cache' if out else None state_path = cache / 'state.json' if cache else None state = load_state(state_path) if state_path else {} From 919aa7b2a880579d30eaf53d2bb3d35bf5056765 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:48:50 -0500 Subject: [PATCH 4/8] Let the mouse camera in stick mode reach the game's top turn speed at a camera speed above 1. With the mouse camera in stick mode and a camera speed above 1, the mouse could never turn faster than a full stick does at camera speed 1, however high the mouse sensitivity was set. The mouse's counts were limited to full tilt first and divided by the camera speed afterwards, so at camera speed 1.5 the most the mouse could send was two thirds of full tilt, which the game's own factor brought back to the old top speed. The division now comes before the limit (runtime/input_map.c input_mouse_stick): a slow movement turns as far as before, and a fast flick reaches full tilt, which is the game's top speed with the camera speed in it. Direct mouse mode, the default, does not use this code. A camera speed of 1 or less behaves as before. From pull request #424. tests/mouse_stick_check.py is new here: it builds input_map.c alone and checks the deflection at each camera speed without the game. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 5 ++++ docs/controls.md | 2 +- nx2navi.ini.example | 3 +- runtime/camera_speed.c | 3 +- runtime/cfg_probe.c | 16 ++++++---- runtime/input.c | 8 ++--- runtime/input_map.c | 6 ++-- runtime/input_map.h | 5 +++- tests/mouse_stick_check.c | 61 ++++++++++++++++++++++++++++++++++++++ tests/mouse_stick_check.py | 35 ++++++++++++++++++++++ 10 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 tests/mouse_stick_check.c create mode 100644 tests/mouse_stick_check.py diff --git a/docs/building.md b/docs/building.md index 3da1c6ca..da13975a 100644 --- a/docs/building.md +++ b/docs/building.md @@ -322,6 +322,11 @@ A change to what the library warm logs when the driver's cache of stage librarie `runtime/vk_pipeline.c` `lw_main`) passes `python tests\warm_hint_check.py`, which builds the header alone and checks the added text by GPU vendor and by why the cache was cold. No game files. +A change to the mouse camera's stick deflection (`runtime/input_map.c` `input_mouse_stick`) passes +`python tests\mouse_stick_check.py`, which builds that file with the rest of the runtime stubbed and checks the +deflection at each camera speed: a slow movement is divided by the factor, a fast flick reaches full tilt. No game +files. + The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter change passes these, all run with `.venv`: diff --git a/docs/controls.md b/docs/controls.md index a5f00305..7e467309 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -108,7 +108,7 @@ bind_Y = pad:north, key:mouse_l ## Mouse camera -Mouse motion (raw input) becomes right-stick deflection. Each presented frame takes the counts moved since the last frame, divides by the frame time and sets the deflection to `counts per second * NX2NAVI_MOUSE_SENS / 3000` of full, held for that frame, so the turn rate does not depend on the frame rate. This is not mouselook: the game's stick curve and dead zone still apply, so very slow movements do nothing and a fast flick saturates. The mouse deflection competes with the gamepad and arrow keys by the larger-deflection rule. +Mouse motion (raw input) becomes right-stick deflection. Each presented frame takes the counts moved since the last frame, divides by the frame time and sets the deflection to `counts per second * NX2NAVI_MOUSE_SENS / 3000` of full, held for that frame, so the turn rate does not depend on the frame rate. This is not mouselook: the game's stick curve and dead zone still apply, so very slow movements do nothing and a fast flick saturates. With a camera speed other than 1 (`NX2NAVI_STICK_SENS`) the deflection is divided by that factor before it is limited to full tilt: the turn for a given movement stays the same, and the fastest turn a flick reaches is the game's top speed with the factor in it. The mouse deflection competes with the gamepad and arrow keys by the larger-deflection rule. `NX2NAVI_MOUSE_SENS` (0 to 10, default 1; higher turns faster, 0 turns the mouse camera and the cursor capture off). While the window is the foreground window the cursor is hidden and confined to the client area. It is released on focus loss, minimise, while F11 changes the window, during a size or move loop and while a text entry is open. Esc releases it by hand: the cursor shows, the mouse camera and mouse buttons stop, and the window can be moved, minimised or closed. A mouse click inside the client area captures it again; that click does not reach the game. Activating the window by a click on its title bar or border does not capture, so it can be dragged or closed right away; Alt+Tab or a click in the client area captures as before. `NX2NAVI_INPUT=none` disables the capture too. diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 2ab095cc..eb75b735 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -533,7 +533,8 @@ # the stick reads centred, and the rest of its travel is scaled to start there. # stick_deadzone = 8 # Right-stick camera speed (0.1 to 3): multiplies the game's own camera turn rates, top speed included, at each of the -# five camera sensitivities in the game's Options. Keys bound to the right stick follow it; the mouse keeps mouse_sens. +# five camera sensitivities in the game's Options. Keys bound to the right stick follow it; the mouse keeps mouse_sens +# for its turn per movement, and in stick mode the fastest turn it can reach rises and falls with this. # A change while the game runs takes effect at once (the first one of a run within a few seconds). # stick_sens = 1 # The dead zone of one stick, in place of stick_deadzone for that stick (0 to 50; unset = stick_deadzone). The Controls diff --git a/runtime/camera_speed.c b/runtime/camera_speed.c index db2b2fe7..6a2c11af 100644 --- a/runtime/camera_speed.c +++ b/runtime/camera_speed.c @@ -4,7 +4,8 @@ // PlayerCameraParam.bgyml inside Pack/Actor/PlayerCamera.pack.zs. romfs.c serves a copy of that pack with every entry // multiplied (cam_pack_build), so the setting raises the camera's top speed as well as its speed at a partial tilt, at // any of the five sensitivities. The mouse camera reaches the game as a right-stick deflection (input.c, mouse_camera), -// which divides it by the same factor (cam_speed_applied), so NX2NAVI_MOUSE_SENS alone sets the mouse's speed. +// divided by the same factor (cam_speed_applied) before it is limited to full tilt (input_mouse_stick), so +// NX2NAVI_MOUSE_SENS alone sets the mouse's turn per movement, and its top speed is the game's with the factor in it. // // The pack is a SARC archive compressed with the dictionary pack.zsdic of Pack/ZsDic.pack.zs. A float in a BYML array // is stored in the array itself, so the multiplied values take the bytes of the old ones and no offset moves. diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c index 44c40088..a10cd1d5 100644 --- a/runtime/cfg_probe.c +++ b/runtime/cfg_probe.c @@ -330,18 +330,22 @@ int probe_cfg(void) { CHECK(st[0] == -32767 && st[1] == 0, "keys: left arrow is right stick left"); input_map_stick(dn, 0, st); CHECK(st[0] == 0 && st[1] == 0, "keys: arrows leave the left stick alone"); - input_mouse_stick(30, 0, 1.0 / 60, 1, st); + input_mouse_stick(30, 0, 1.0 / 60, 1, 1, st); CHECK(st[0] == 32767 * 1800 / 3000 && st[1] == 0, "mouse: 30 counts in 1/60 s is 60% right"); - input_mouse_stick(0, -10, 1.0 / 60, 1, st); + input_mouse_stick(0, -10, 1.0 / 60, 1, 1, st); CHECK(st[0] == 0 && st[1] > 0, "mouse: moving up is stick up"); - input_mouse_stick(30, 0, 1.0 / 30, 1, st); + input_mouse_stick(30, 0, 1.0 / 30, 1, 1, st); CHECK(st[0] == 32767 * 900 / 3000, "mouse: the same counts over twice the time are half the deflection"); - input_mouse_stick(15, 0, 1.0 / 60, 2, st); + input_mouse_stick(15, 0, 1.0 / 60, 2, 1, st); CHECK(st[0] == 32767 * 1800 / 3000, "mouse: doubling sensitivity halves the counts needed (15 counts at 2 = 30 at 1)"); - input_mouse_stick(1000, 1000, 1.0 / 60, 1, st); + input_mouse_stick(1000, 1000, 1.0 / 60, 1, 1, st); CHECK((int64_t)st[0] * st[0] + (int64_t)st[1] * st[1] <= 32768ll * 32768, "mouse: clamped to the unit circle"); - input_mouse_stick(30, 0, 1.0 / 60, 0, st); + input_mouse_stick(30, 0, 1.0 / 60, 0, 1, st); CHECK(st[0] == 0 && st[1] == 0, "mouse: sensitivity 0 is off"); + input_mouse_stick(30, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == 13107 && st[1] == 0, "mouse: a camera speed of 1.5 divides the deflection, so the turn per count stays"); + input_mouse_stick(1000, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == 32767 && st[1] == 0, "mouse: a fast flick reaches full tilt at a camera speed of 1.5 too"); input_pad_stick(3000, -2000, 0.15f, st); CHECK(st[0] == 0 && st[1] == 0, "pad stick: an 11% drift inside a 15% dead zone reads centred"); input_pad_stick(4000, -3000, 0, st); diff --git a/runtime/input.c b/runtime/input.c index 1b429cda..24936611 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -368,17 +368,15 @@ static void mouse_camera(int32_t out[2], int selector) { if (fr != frame) { double dt = age < 0.004 ? 0.004 : age > 0.1 ? 0.1 : age; ikb_mouse_take(&dx, &dy); - input_mouse_stick(dx, dy, dt, mouse_sens_get(), held); + input_mouse_stick(dx, dy, dt, mouse_sens_get(), cam_speed_applied(), held); frame = fr; t0 = now; } else if (age > 0.1) { held[0] = held[1] = 0; // frames stopped: do not hold a deflection } } - double k = cam_speed_applied(), x = held[0] / k, y = held[1] / k, len = sqrt(x * x + y * y); - if (len > STICK_MAX) x *= STICK_MAX / len, y *= STICK_MAX / len; - out[0] = (int32_t)lround(x); - out[1] = (int32_t)lround(y); + out[0] = held[0]; + out[1] = held[1]; } static float stick_dead[2] = {-1, -1}; // input_pad_deadzone of the left and right stick; live (cfg_version) diff --git a/runtime/input_map.c b/runtime/input_map.c index 2b55ca87..29875062 100644 --- a/runtime/input_map.c +++ b/runtime/input_map.c @@ -349,10 +349,10 @@ void input_map_stick(const uint8_t down[KEY_COUNT], int stick, int32_t out[2]) { out[1] = dy * m; } -void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, int32_t out[2]) { +void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, float cam, int32_t out[2]) { out[0] = out[1] = 0; - if (!(dt_s > 0) || !(sens > 0)) return; - double k = STICK_MAX * (double)sens / (MOUSE_FULL_CPS * dt_s); + if (!(dt_s > 0) || !(sens > 0) || !(cam > 0)) return; + double k = STICK_MAX * (double)sens / (MOUSE_FULL_CPS * dt_s * cam); double x = dx * k, y = -dy * k, len = sqrt(x * x + y * y); if (len > STICK_MAX) { x *= STICK_MAX / len; y *= STICK_MAX / len; } out[0] = (int32_t)lrint(x); diff --git a/runtime/input_map.h b/runtime/input_map.h index 024bfd32..299f4823 100644 --- a/runtime/input_map.h +++ b/runtime/input_map.h @@ -117,8 +117,11 @@ int input_map_held(const uint8_t down[KEY_COUNT], int action); // unit circle. void input_map_stick(const uint8_t down[KEY_COUNT], int stick, int32_t out[2]); // Mouse motion in raw counts over dt_s seconds to right-stick deflection: full deflection at 3000 / sens counts/s. +// `cam` is the factor the game multiplies the right stick's turn rates by (cam_speed_applied). The deflection is +// divided by it, so the turn per count stays what sens says, and only then clamped: a clamp taken first would hold the +// mouse's fastest turn at the game's unmultiplied top speed, however high the factor. // Clamped to the unit circle, up (negative dy) is +y. -void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, int32_t out[2]); +void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, float cam, int32_t out[2]); // A gamepad stick (x, y in -32768..32767, up is +y) through a round dead zone of `dead` (fraction of full deflection, // 0..0.5): inside the circle the stick reads 0, and the rest of the travel is scaled to start at 0 at the edge, so a // drifting stick rests at 0 and full deflection still reaches full. The result stays inside the unit square. diff --git a/tests/mouse_stick_check.c b/tests/mouse_stick_check.c new file mode 100644 index 00000000..50028ba2 --- /dev/null +++ b/tests/mouse_stick_check.c @@ -0,0 +1,61 @@ +// The mouse camera in stick mode (runtime/input_map.c input_mouse_stick): raw counts to a right-stick deflection with +// the camera speed taken out of it. The game multiplies the right stick's turn rates by the camera speed, so the +// deflection is divided by it, and the division comes before the limit to full tilt: a slow movement turns as far as +// at camera speed 1, and a fast flick reaches full tilt, which the game turns into its top speed with the factor in +// it. No game files. tests/mouse_stick_check.py builds and runs this with runtime/input_map.c. +#include +#include +#include +#include +#include "input_map.h" + +// what input_map.c needs from the rest of the runtime; none of it is reached from input_mouse_stick +const char *cfg_get(const char *key) { (void)key; return NULL; } +double cfg_double(const char *key) { (void)key; return 0; } +void rt_log(const char *fmt, ...) { (void)fmt; } + +#define FULL 32767 + +static int fails; +#define CHECK(cond, ...) do { if (!(cond)) { printf("FAIL: " __VA_ARGS__); putchar('\n'); fails++; } } while (0) + +int main(void) { + static const float cams[] = {0.1f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f}; + int32_t st[2], one[2]; + for (unsigned i = 0; i < sizeof cams / sizeof *cams; i++) { + float cam = cams[i]; + // a slow movement: the deflection is the one at camera speed 1 divided by the factor (while that is below full + // tilt), so the game's multiplication gives the same turn + for (int counts = 1; counts <= 40; counts += 3) { + input_mouse_stick(counts, 0, 1.0 / 60, 1, 1, one); + input_mouse_stick(counts, 0, 1.0 / 60, 1, cam, st); + double at_one = FULL * (counts * 60.0) / 3000.0; // full tilt at 3000 counts a second + double want = at_one / cam > FULL ? FULL : at_one / cam; + CHECK(fabs(one[0] - at_one) <= 0.5, "camera speed 1, %d counts: %d, expected %.1f", counts, one[0], at_one); + CHECK(fabs(st[0] - want) <= 0.5 && st[1] == 0, "camera speed %.1f, %d counts: %d, expected %.1f", cam, counts, + st[0], want); + } + // a fast flick reaches full tilt at every camera speed, in each direction + input_mouse_stick(5000, 0, 1.0 / 60, 1, cam, st); + CHECK(st[0] == FULL && st[1] == 0, "camera speed %.1f: a flick right gives %d %d", cam, st[0], st[1]); + input_mouse_stick(0, 5000, 1.0 / 60, 1, cam, st); + CHECK(st[0] == 0 && st[1] == -FULL, "camera speed %.1f: a flick down gives %d %d", cam, st[0], st[1]); + input_mouse_stick(-5000, -5000, 1.0 / 60, 1, cam, st); + double len = sqrt((double)st[0] * st[0] + (double)st[1] * st[1]); + CHECK(st[0] < 0 && st[1] > 0 && st[0] == -st[1] && len <= FULL + 1.0 && len >= FULL - 2.0, + "camera speed %.1f: a diagonal flick gives %d %d", cam, st[0], st[1]); + } + // the case that was wrong: at camera speed 1.5 a flick stopped at two thirds of full tilt, the top speed of camera + // speed 1 + input_mouse_stick(80, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == FULL, "camera speed 1.5, 80 counts in 1/60 s: %d, expected full tilt", st[0]); + input_mouse_stick(30, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == 13107, "camera speed 1.5, 30 counts in 1/60 s: %d, expected 13107", st[0]); + // nothing to divide by: no deflection + input_mouse_stick(30, 0, 1.0 / 60, 1, 0, st); + CHECK(st[0] == 0 && st[1] == 0, "camera speed 0 gives %d %d", st[0], st[1]); + input_mouse_stick(30, 0, 1.0 / 60, 1, -1, st); + CHECK(st[0] == 0 && st[1] == 0, "a negative camera speed gives %d %d", st[0], st[1]); + if (!fails) puts("mouse stick: the camera speed divides the deflection before the limit to full tilt"); + return fails != 0; +} diff --git a/tests/mouse_stick_check.py b/tests/mouse_stick_check.py new file mode 100644 index 00000000..c5e3455d --- /dev/null +++ b/tests/mouse_stick_check.py @@ -0,0 +1,35 @@ +"""Build and run tests/mouse_stick_check.c with runtime/input_map.c: the mouse camera's stick deflection at each camera +speed. No game files. + +Usage: python tests/mouse_stick_check.py [--cc ] +""" +import argparse +import os +import subprocess +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def main(): + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument('--cc', help='the C compiler (default: MSYS2 UCRT64 clang on Windows, clang elsewhere)') + args = ap.parse_args() + win = os.name == 'nt' + cc = args.cc or (str(MSYS_BIN / 'clang.exe') if win and MSYS_BIN.exists() else 'clang') + env = dict(os.environ) + if win and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + with tempfile.TemporaryDirectory(prefix='nx2navi-mouse-stick-') as tmp: + exe = Path(tmp) / ('check.exe' if win else 'check') + subprocess.check_call([cc, '-O2', '-Wall', '-fno-strict-aliasing', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'mouse_stick_check.c'), str(ROOT / 'runtime' / 'input_map.c'), + *([] if win else ['-lm']), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main()) From 541c4ff84c3732ddc11d77424b4b7bdad4004832 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:49:10 -0500 Subject: [PATCH 5/8] Start the game again after Restart on every processor it had (Linux). On Linux each Restart from the menu cost the next game one more processor core on machines with 12 or more logical processors. A process started with posix_spawn begins with the mask of the thread that spawned it, and since the recording thread has a core of its own no thread has every processor: the restarted game began without that core and took the next fast one for its own recording thread. plat_restart (runtime/platform/linux.c) now widens the calling thread's mask to all of the process's processors for the time of the spawn and puts it back. A mask set from outside is kept, helper programs started with plat_spawn are unchanged, and Windows is not touched. From pull request #435, with its test tests/platform_restart_mask_test.c, which needs Linux. Not compiled or run here: this machine has no Linux compiler. Co-Authored-By: Claude Fable 5.2 --- runtime/platform/linux.c | 19 +++++++ tests/platform_restart_mask_test.c | 82 ++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tests/platform_restart_mask_test.c diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 4851de73..66b7cb2d 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -2373,6 +2373,22 @@ static void spawn_close_from(posix_spawn_file_actions_t *fa, int from) { if (posix_spawn_file_actions_addclosefrom_np) posix_spawn_file_actions_addclosefrom_np(fa, from); } +// The calling thread's mask with every processor this process uses, for the time of a posix_spawn: a child starts +// with the mask of the thread that spawned it, and after plat_cpu_reserve no thread has them all (the recording thread +// has its core alone, every other thread lacks that core). The game plat_restart starts would begin without the core, +// take the next fast one for its own recording thread, and so have one core fewer after every Restart. +// 1 when the mask was widened: *saved is then the mask to put back. +static int spawn_mask_whole(cpu_set_t *saved) { + cpu_set_t whole; + plat_mutex_lock(&cpu_lock); + int taken = cpu_taker != 0; + CPU_OR(&whole, &cpu_taken, &cpu_others); + plat_mutex_unlock(&cpu_lock); + if (!taken || sched_getaffinity(0, sizeof *saved, saved)) return 0; + CPU_OR(&whole, &whole, saved); + return !CPU_EQUAL(&whole, saved) && !sched_setaffinity(0, sizeof whole, &whole); +} + int plat_restart(void) { // the arguments, NUL-separated, from /proc/self/cmdline static char args[16384]; @@ -2399,8 +2415,11 @@ int plat_restart(void) { posix_spawnattr_setsigdefault(&at, &all); posix_spawnattr_setflags(&at, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF); setenv(RESTART_WAIT, wait, 1); + cpu_set_t mask; + int whole = spawn_mask_whole(&mask); pid_t pid; int e = posix_spawn(&pid, exe, &fa, &at, argv, environ); + if (whole) sched_setaffinity(0, sizeof mask, &mask); unsetenv(RESTART_WAIT); posix_spawnattr_destroy(&at); posix_spawn_file_actions_destroy(&fa); diff --git a/tests/platform_restart_mask_test.c b/tests/platform_restart_mask_test.c new file mode 100644 index 00000000..d6678689 --- /dev/null +++ b/tests/platform_restart_mask_test.c @@ -0,0 +1,82 @@ +// plat_restart on Linux (runtime/platform/linux.c): the game it starts may run on every processor this one could +// before plat_cpu_reserve took a core for the recording thread, whichever thread asks for the restart. No game files. +// Build and run from the repository root: +// clang -O2 -ffunction-sections -fdata-sections -Iruntime -c runtime/platform/linux.c -o /tmp/plat.o +// clang -O2 -Wall -Iruntime tests/platform_restart_mask_test.c /tmp/plat.o -Wl,--gc-sections -lpthread -ldl -o /tmp/restart_mask +// /tmp/restart_mask (exit 77: this machine has no core plat_cpu_reserve would take) +// The test starts itself again through plat_restart; that copy only writes how many processors it may run on. +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "platform/platform.h" + +#define OUT "RESTART_MASK_TEST_OUT" + +static int allowed(void) { + cpu_set_t set; + return sched_getaffinity(0, sizeof set, &set) ? -1 : CPU_COUNT(&set); +} + +// What the copy plat_restart started wrote, waiting up to 5 s for it; -1 when it never came. +static int restarted_count(const char *path) { + for (int i = 0; i < 500; i++) { + FILE *f = fopen(path, "r"); + int n = -1; + if (f) { + int got = fscanf(f, "%d", &n); + fclose(f); + if (got == 1) { + remove(path); + return n; + } + } + usleep(10000); + } + return -1; +} + +static atomic_int go, done; + +// A thread that existed before the core was taken, as the game's main thread does: it asks for the restart. +static void *other_thread(void *arg) { + (void)arg; + while (!atomic_load(&go)) usleep(1000); + plat_restart(); + atomic_store(&done, 1); + return NULL; +} + +int main(void) { + const char *out = getenv(OUT); + if (getenv("NX2NAVI_RESTART_WAIT") && out) { // the copy plat_restart started + FILE *f = fopen(out, "w"); + if (f) fprintf(f, "%d\n", allowed()), fclose(f); + return 0; + } + char path[64]; + snprintf(path, sizeof path, "/tmp/restart-mask-%d.txt", (int)getpid()); + setenv(OUT, path, 1); + remove(path); + int before = allowed(), fails = 0; + pthread_t th; + pthread_create(&th, NULL, other_thread, NULL); + char which[128]; + int core = plat_cpu_reserve(which, sizeof which); + if (!core) { puts("SKIP: no core to reserve on this machine"); return 77; } + atomic_store(&go, 1); + while (!atomic_load(&done)) usleep(1000); + pthread_join(th, NULL); + int from_other = restarted_count(path); + plat_restart(); // and from the thread that holds the core + int from_holder = restarted_count(path); + printf("%d processors before; the core taken: %s; a restart from another thread starts with %d, from the holder with %d\n", + before, which, from_other, from_holder); + if (from_other != before) puts("FAIL: the restarted process lacks processors (asked by another thread)"), fails++; + if (from_holder != before) puts("FAIL: the restarted process lacks processors (asked by the holder)"), fails++; + if (!fails) puts("restart mask: the restarted process has every processor"); + return fails != 0; +} From 5374d04d0f1913ee3355a3239285260b096101be Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Tue, 10 Nov 2026 12:50:56 -0500 Subject: [PATCH 6/8] Keep an optional history of GPU address ranges, so the report of a lost graphics device can name what held the faulting address. After a lost device the driver's fault report gives an address and nothing says what was there. With va_report = 1 (NX2NAVI_VA_REPORT, off by default) the game keeps every address range the driver binds to a buffer or image and every range it unbinds (VK_EXT_device_address_binding_report, runtime/vk_vaddr.c). The report of a lost device then names the ranges that covered each faulting page, or the nearest ones when none did, and vaddr-history.txt beside the log holds the whole history, also when the driver gives no fault report. With 2 the call stack behind each range of 1 MiB or more is kept too. A player sees nothing of this unless the setting is on: without it no extension is enabled and no callback runs. From pull request #349. Its second part, which stops a frame dump from turning on mesh query statistics, is left out: it changes what a dump writes for everyone who uses one. Built for Windows; no device was lost to try it. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 7 +++++++ nx2navi.ini.example | 6 ++++++ runtime/cfg.c | 5 +++-- runtime/platform/linux.c | 16 ++++++++++++++++ runtime/platform/platform.h | 2 ++ runtime/platform/win32.c | 7 +++++++ runtime/vk.h | 9 +++++++++ runtime/vk_device.c | 21 ++++++++++++++++++--- runtime/vk_headroom.c | 1 + 9 files changed, 69 insertions(+), 5 deletions(-) diff --git a/docs/building.md b/docs/building.md index da13975a..0d7f0c89 100644 --- a/docs/building.md +++ b/docs/building.md @@ -278,6 +278,13 @@ Before a commit: `git config core.hooksPath .githooks` runs it before each local commit. - `build\\nx2navi.exe --selftest`: layout, dispatch table and host-function registry, without game files. +GPU page faults: `NX2NAVI_VA_REPORT=1` keeps the driver's GPU address bind/unbind reports +(VK_EXT_device_address_binding_report, `runtime/vk_vaddr.c`); `=2` also keeps the host call stack of ranges of 1 MiB +or more. After a lost device, the log names the reports whose range covered each faulting page, or the nearest ranges +when none did, and `vaddr-history.txt` beside the log holds the whole history. Stack frames are module offsets: add +the executable's load address (the link map's base) before looking them up in `ocarina.map`. Each bind and unbind +costs a callback, and `=2`'s stack walks enough to hide a timing-dependent fault. + A change to the in-game menu's look (`runtime/ui/menu_skin.cpp`, `option_rows.cpp`, `perf_panel.cpp`) can be looked at without game files: `python tools/menu_preview.py` builds the menu's own drawing code with the host's clang++ and writes the Screen tab, the full performance overlay and the boot screen to `build/menu-preview/` (`menu.png`, diff --git a/nx2navi.ini.example b/nx2navi.ini.example index eb75b735..87c50f13 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -621,6 +621,12 @@ # 1: samples each new ASTC texture's blocks on the CPU and logs how many are opaque ("ASTC alpha probe" lines), which # could be stored at half the video memory. Slows area loads. Default: 0. # astc_bc_probe = 0 +# GPU address history for the report of a lost graphics device, on drivers with +# VK_EXT_device_address_binding_report. 1: the game keeps every address range the driver binds to a buffer or image and +# every range it unbinds; after a lost device the log names what held each faulting address, and vaddr-history.txt +# beside the log holds the whole history. 2: also the call stack that made each range of 1 MiB or more, which slows +# area loads enough to hide a fault that depends on timing. Default: 0. +# va_report = 0 # Flight recorder: keeps the last frames and saves them to a flight folder next to the folder of nx2navi.exe when a frame # takes longer than flight_spike_ms (0 to 60000). # flight = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index 2605166f..e4fcaf27 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", "TEXTURE_DETAIL", - "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", + "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", "VA_REPORT", "VMA_BLOCK_MB", "ASTC_BC_PROBE", "SYNC_WAKE_ALL", "LOG_ASYNC", }; @@ -125,7 +125,7 @@ static const struct { const char *key, *section; } sections[] = { {"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"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, - {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, + {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VA_REPORT", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; @@ -268,6 +268,7 @@ static const CfgMeta metas[] = { {"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 {"PASS_CRUMBS", "", "0|1", RESTART}, // vk_crumbs.c pass marks; unset: VK_AMD_buffer_marker only + {"VA_REPORT", "0", "0|1|2", RESTART}, // vk_vaddr.c GPU address history for a lost device's report; 2: call stacks {"INPUT_RECORD", "", NULL, RESTART}, }; diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 66b7cb2d..ccb6eac0 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1567,6 +1567,22 @@ int plat_module_of(uintptr_t addr, char *name, size_t cap, uintptr_t *base) { return snprintf(name, cap, "%s", n ? n + 1 : d.dli_fname) < (int)cap; } +typedef struct { uintptr_t *pc; int n, max; } StackWalk; + +static _Unwind_Reason_Code stack_step(struct _Unwind_Context *ctx, void *p) { + StackWalk *w = p; + uintptr_t ip = _Unwind_GetIP(ctx); + if (!ip || w->n == w->max) return _URC_END_OF_STACK; + w->pc[w->n++] = ip; + return _URC_NO_REASON; +} + +int plat_stack(uintptr_t *pc, int max) { + StackWalk w = {pc, 0, max}; + _Unwind_Backtrace(stack_step, &w); + return w.n; +} + int plat_exe_path(char *out, size_t cap) { ssize_t n = readlink("/proc/self/exe", out, cap - 1); if (n <= 0 || (size_t)n >= cap - 1) return 0; diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index df8bad8b..81c7f741 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -416,6 +416,8 @@ uintptr_t plat_exe_base(void); // where the executabl // The module (executable or library) holding code address addr: its file name without directories into name, its load // address into *base. 0 when none. int plat_module_of(uintptr_t addr, char *name, size_t cap, uintptr_t *base); +// Return addresses of the calling thread's stack into pc, innermost first, at most max: their count. +int plat_stack(uintptr_t *pc, int max); #define PLAT_PATH_MAX 1024 // path buffers of the runtime #ifdef _WIN32 #define PLAT_EXE_SUFFIX ".exe" // of the programs the runtime starts (nx2navi_xlate) diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 7cbfa47c..9f8138d1 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1562,6 +1562,13 @@ int plat_module_of(uintptr_t addr, char *name, size_t cap, uintptr_t *base) { return WideCharToMultiByte(CP_UTF8, 0, n, -1, name, (int)cap, NULL, NULL) != 0; } +int plat_stack(uintptr_t *pc, int max) { + void *f[62]; + USHORT n = RtlCaptureStackBackTrace(1, max < 62 ? (DWORD)max : 62, f, NULL); + for (USHORT i = 0; i < n; i++) pc[i] = (uintptr_t)f[i]; + return n; +} + int plat_exe_path(char *out, size_t cap) { wchar_t w[WPATH]; DWORD n = GetModuleFileNameW(NULL, w, WPATH); diff --git a/runtime/vk.h b/runtime/vk.h index 8b3ca0db..55147b61 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -82,6 +82,15 @@ int vk_selftest(void); // 0 on success // process, after the GPU breadcrumbs (vk_crumbs_report). Any other result returns at once. void vk_device_fault_report(VkResult r); +// ---- GPU address history (vk_vaddr.c, NX2NAVI_VA_REPORT=1, 2 with call stacks): VK_EXT_device_address_binding_report's +// reports, kept so vk_device_fault_report can name the objects that held a faulting address. +int vk_vaddr_wanted(void); +void vk_vaddr_start(VkInstance instance); // create_instance, after vkCreateInstance +void vk_vaddr_device(int supported); // create_device: the driver reports address bindings +void vk_vaddr_fault(uint64_t addr, uint64_t precision); // logs the reports whose range covered addr's page +void vk_vaddr_dump(void); // the whole history to vaddr-history.txt beside the log +void vk_vaddr_stop(VkInstance instance); // before vkDestroyInstance + // ---- GPU breadcrumbs (vk_crumbs.c): with VK_AMD_buffer_marker, each crumb has the GPU write its code into host memory // twice, when the command processor reaches it and when all work before it has finished. After a lost device the two // codes bracket the work that hung. Without the extension every call returns at once. diff --git a/runtime/vk_device.c b/runtime/vk_device.c index c72af9a8..485fcaef 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -141,8 +141,9 @@ static int create_instance(void) { rt_log("vk: NX2NAVI_VK_VALIDATION set but %s is not installed; continuing without", layers[0]); validate = 0; } - // DLSS Frame Generation: Streamline's plugins call its naming functions even when they say it is off - if (validate || (vk_dlssg_wanted() && instance_ext_present(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))) + // DLSS Frame Generation: Streamline's plugins call its naming functions even when they say it is off; the GPU + // address history (vk_vaddr.c) gets the driver's binding reports through a messenger + if (validate || ((vk_dlssg_wanted() || vk_vaddr_wanted()) && instance_ext_present(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))) ext[n_ext++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; // armed, not wanted: the menu switches HDR on during the run (vk_hdr.c), and the extensions are fixed here if (vk_hdr_armed() && instance_ext_present(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) // HDR10 surface formats @@ -168,6 +169,7 @@ static int create_instance(void) { .pfnUserCallback = debug_cb}; if (f) f(g_vk.instance, &mi, NULL, &g_vk.messenger); } + vk_vaddr_start(g_vk.instance); return 1; } @@ -299,6 +301,7 @@ typedef struct { VkPhysicalDeviceShaderMaximalReconvergenceFeaturesKHR mrc; VkPhysicalDeviceMaintenance5FeaturesKHR m5; VkPhysicalDevicePipelineBinaryFeaturesKHR pbin; + VkPhysicalDeviceAddressBindingReportFeaturesEXT abr; } FeatureChain; static int create_device(void) { @@ -362,6 +365,12 @@ static int create_device(void) { CHAIN(fbl, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_FEATURES_EXT); CHAIN(lr, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_LOCAL_READ_FEATURES_KHR); } + // The GPU address history for a lost device's report (vk_vaddr.c, NX2NAVI_VA_REPORT=1) + int want_va = vk_vaddr_wanted() && has_ext(ep, ne, VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME); + if (want_va) { + ext[n_ext++] = VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME; + CHAIN(abr, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ADDRESS_BINDING_REPORT_FEATURES_EXT); + } vk_dlaa_device_exts(ep, ne, ext, &n_ext, 40); // NGX's; the eight slots after it stay for the eight below // FSR 3.1 (vk_ffx.cpp): its Vulkan back end looks vkGetBufferMemoryRequirements2KHR up by that name, which resolves // only with the extension enabled (the core function has no suffix); without it the back end called address 0. @@ -431,6 +440,7 @@ static int create_device(void) { else if (!h->textureCompressionBC) miss = "textureCompressionBC"; if (miss) { rt_log("vk: required feature missing on %s: %s", g_vk.props.deviceName, miss); free(c); return 0; } if (!c->fault.deviceFault) g_vk.has_device_fault = 0; + if (vk_vaddr_wanted()) vk_vaddr_device(want_va && c->abr.reportAddressBinding); if (g_vk.has_vrs) { // the rate is set per draw; no rate attachment and no per-primitive rates c->vrs.attachmentFragmentShadingRate = c->vrs.primitiveFragmentShadingRate = VK_FALSE; if (!c->vrs.pipelineFragmentShadingRate) g_vk.has_vrs = 0; @@ -537,6 +547,7 @@ static void destroy_all(void) { (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(g_vk.instance, "vkDestroyDebugUtilsMessengerEXT"); if (f) f(g_vk.instance, g_vk.messenger, NULL); } + vk_vaddr_stop(g_vk.instance); if (g_vk.instance) vkDestroyInstance(g_vk.instance, NULL); g_errors_total += g_vk.validation_errors; memset(&g_vk, 0, sizeof g_vk); @@ -596,6 +607,7 @@ void vk_device_fault_report(VkResult r) { if (!get) { rt_log("vk: device lost; no fault report (VK_EXT_device_fault %s)", c && c->has_device_fault ? "has no entry point" : "not enabled"); + vk_vaddr_dump(); // the history still names what held a page another report (the kernel's log) gives plat_mutex_unlock(&lock); return; } @@ -613,13 +625,16 @@ void vk_device_fault_report(VkResult r) { rt_log("vk: device fault: \"%s\" (%u address infos, %u vendor infos, vendor binary %llu bytes)", info.description, n.addressInfoCount, n.vendorInfoCount, (unsigned long long)binary); // The faulting access lies in the precision-aligned range around the reported address. - for (uint32_t i = 0; i < n.addressInfoCount; i++) + for (uint32_t i = 0; i < n.addressInfoCount; i++) { rt_log("vk: device fault address %u: %s at %#llx, precision %#llx", i, fault_address_type(ai[i].addressType), (unsigned long long)ai[i].reportedAddress, (unsigned long long)ai[i].addressPrecision); + vk_vaddr_fault(ai[i].reportedAddress, ai[i].addressPrecision); + } for (uint32_t i = 0; i < n.vendorInfoCount; i++) rt_log("vk: device fault vendor info %u: \"%s\" code %#llx data %#llx", i, vi[i].description, (unsigned long long)vi[i].vendorFaultCode, (unsigned long long)vi[i].vendorFaultData); } + vk_vaddr_dump(); free(ai); free(vi); plat_mutex_unlock(&lock); diff --git a/runtime/vk_headroom.c b/runtime/vk_headroom.c index 481e8eed..811d7cfd 100644 --- a/runtime/vk_headroom.c +++ b/runtime/vk_headroom.c @@ -157,6 +157,7 @@ static void top_up(const VkCtx *c, uint32_t type) { static void headroom_main(void *arg) { (void)arg; __atomic_store_n(&H.tid, plat_thread_id(), __ATOMIC_RELEASE); + if (vk_vaddr_wanted()) rt_log("vk: headroom: thread %u", H.tid); // names this thread in the address history (vk_vaddr.c) plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); uint64_t watch = 0; // memory types seen so far for (;;) { From ce206a9239d700ec01bbe625a6df7102d34a1e2b Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Tue, 10 Nov 2026 14:08:48 -0500 Subject: [PATCH 7/8] Order passes and dispatches by the game's own barriers on NVIDIA graphics cards, which takes about a tenth off the GPU's time for a frame. Every pass begin and every compute dispatch recorded a full barrier, which makes the GPU finish everything before each of them. With barrier_game = 1 (NX2NAVI_BARRIER_GAME) a pass after an earlier pass gets a barrier for its attachments only and a dispatch after other dispatches gets none; a change between drawing and compute keeps the full barrier, and so do the game's own barriers and the replay's copies, clears, fills and uniform updates. On an RTX 5090 at 2560x1440 the GPU's time for a frame fell from 5.4 to 4.9 ms standing in Kokiri Forest (two in-run A/B runs, the control's blocks differ by 0.7 %) and from 5.1 to 4.4 ms on a walk through it. The frame rate did not change there, because the replay thread is the limit on that machine; it should rise where the GPU is. The default is 1 on NVIDIA graphics cards only, and 0 on every other vendor's: the game placed its barriers for an NVIDIA GPU, Vulkan asks for more of them than the game recorded, and no AMD or Intel driver has run this. 34 screenshots across the A and B blocks of one run show the same picture on NVIDIA. NX2NAVI_TRACE=perf logs the barriers a frame by kind. From pull request #389, which had it on for every vendor; the default by vendor and the settings text are added here. The validation layer's synchronization check could not be run on this machine. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 5 ++ runtime/cfg.c | 3 +- runtime/vk_backend.c | 1 + runtime/vk_exec.c | 102 ++++++++++++++++++++++++++++++++++++++++- runtime/vk_exec.h | 9 ++++ runtime/vk_exec_draw.c | 4 +- 6 files changed, 119 insertions(+), 5 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 87c50f13..10748499 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -705,6 +705,11 @@ # about a third more frames a second in open areas. 0: copied back at the end of every GPU submission, as in 0.7.0. # With shadow storage (Linux, Wine) it stays off. # wb_lazy = 1 +# Barriers between the game's draw passes and compute dispatches. 1: only where the game itself asks for one, or where +# the work changes between drawing and compute; this takes about a tenth off the graphics card's time for a frame. +# 0: a full barrier before every pass and every dispatch. Default: 1 on NVIDIA graphics cards, 0 on all others, where it +# has not been tried: set 1 there to try it, and back to 0 if the picture shows faults. +# barrier_game = 1 # Linux, shadow storage: 1 clears the write watch's protection of a range in one call before the GPU's bytes are copied # back into the game's memory, instead of taking a fault at the first store into each page. # shadow_bulk_write = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index e4fcaf27..115377e2 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -33,6 +33,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o static const char *const known[] = { "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_STATS", + "BARRIER_GAME", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", @@ -124,7 +125,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"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VA_REPORT", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index cbd08768..e3859a65 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -219,6 +219,7 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin if (!vram_probe) vram_probe = 1, perfmon_set_vram_game(vk_mem_device_used_mb); // the overlay's "game" beside VRAM vk_exec_frame_log(t, frame); dynres_frame(); // before vk_dlaa_frame reads the DLSS mode it may override + vk_exec_barrier_log(frame); vk_dlaa_frame(); vk_sampler_aniso_live(); // NX2NAVI_ANISO changed in the menu: the surface textures' samplers take the new level vk_mvsurvey_frame(); diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 6ac73408..441f046b 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -585,8 +585,71 @@ static VkImageMemoryBarrier2 layout_barrier(VkImage img, VkImageAspectFlags aspe // Full barrier. The images in the feedback layout become want[] (pass begin) or none (every other barrier): the others // go back to GENERAL and the new ones leave it, in the same command. +// NX2NAVI_BARRIER_GAME (1; 0: every pass begin and dispatch a full barrier; ab: in the B blocks): the game's own +// barriers order its work, as on the Switch. A pass that +// samples what an earlier one drew, a dispatch that reads what another dispatch wrote, an indirect draw whose arguments +// a dispatch wrote: on the Switch each needs nvnCommandBufferBarrier (ORDER_* and INVALIDATE_* bits) in the game's +// command stream, which the replay keeps as a full barrier (OP_BARRIER). What the GPU orders by itself there is kept: a +// switch between graphics and compute work drains the one before (the 3D and compute classes share the engine). With +// every pass begin and every dispatch a full barrier, 2.3 ms of a 9.7 ms GPU frame in Kokiri Forest was barriers. +// With the switch: +// - a pass begin after an earlier pass and no dispatch since the last full barrier gets an attachment barrier +// (colour and depth/stencil output before the next pass's loads and writes: Vulkan orders attachment access only +// inside one pass instance), after neither nothing; +// - a dispatch after other dispatches only gets none; +// - a pass after a dispatch, and a dispatch after a pass, get a full barrier, the drain of the switch; +// - either gets a full barrier as before after one the replay itself asked for (vk_exec_barrier: its copies, +// clears, fills, uniform updates and stand-in copies call it before their commands, which sets bar_dirty), and +// when feedback-loop targets change layout. +// Copies, clears and the game's barriers keep their full barriers. +// +// The default is 1 on NVIDIA GPUs and 0 on every other. The game placed its barriers for an NVIDIA GPU, and what it +// left out is what such a GPU orders by itself; Vulkan asks for a barrier wherever a pass samples what an earlier pass +// drew or a dispatch reads what an earlier one wrote, whether the game recorded one or not. On an RTX 5090 the picture +// was the same with the switch on and off (34 screenshots across A/B blocks in Kokiri Forest) and the GPU time fell by +// 0.5 to 0.6 ms of 5.4 a frame; no other vendor's driver has run it, so there the setting has to be turned on by hand. +static int barrier_game(void) { + static int mode = -1; + if (mode < 0) { + const VkCtx *c = vk_ctx(); + int nvidia = c && c->props.vendorID == 0x10de; + mode = rt_knob("BARRIER_GAME", nvidia ? "1" : "0"); + if (mode) + rt_log("vk_exec: NX2NAVI_BARRIER_GAME=%s: passes and dispatches are ordered by the game's barriers", + mode == 2 ? "ab" : "1"); + } + return RT_KNOB_ON(mode); +} + +// NX2NAVI_TRACE=perf: barriers per frame by kind (vk_exec_barrier_log) +enum { BS_FULL, BS_GAME, BS_PASS_FULL, BS_PASS_ATT, BS_PASS_NONE, BS_DISP_FULL, BS_DISP_NONE, BS_N }; +static uint64_t bstat[BS_N]; +static const char *const bs_name[BS_N] = {"replay full", "game", "pass full", "pass attachment", "pass none", + "dispatch full", "dispatch none"}; +enum { BR_OTHER, BR_GAME, BR_PASS, BR_DISPATCH }; + +static void barrier_to_why(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant, int why); static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant) { + barrier_to_why(ex, want, wasp, nwant, BR_OTHER); +} + +static void attachment_barrier(VkExec *ex) { + const VkPipelineStageFlags2 st = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT | + VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT; + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = st, + .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, + .dstStageMask = st, + .dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | + VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT | + VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(ex->cb, &di); + ex->recorded++; +} + +static void barrier_to_why(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant, int why) { vk_exec_pass_end(ex); + if (why == BR_OTHER) ex->bar_dirty = 1; // also when it falls together with the last one: its commands follow VkImageMemoryBarrier2 ib[18]; uint32_t n = 0; for (int i = 0; i < ex->nfbl; i++) @@ -599,6 +662,21 @@ static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags for (int i = 0; i < nwant; i++) ex->fbl_img[i] = want[i], ex->fbl_aspect[i] = wasp[i]; ex->nfbl = nwant; if (ex->barrier_at == ex->recorded && !n) return; + if (!n && !ex->bar_dirty && ((why == BR_PASS && !ex->bar_dispatches) || (why == BR_DISPATCH && !ex->bar_passes)) && + barrier_game()) { + if (why == BR_DISPATCH) { + bstat[BS_DISP_NONE]++; + } else if (ex->bar_passes) { + attachment_barrier(ex); + bstat[BS_PASS_ATT]++; + } else { + bstat[BS_PASS_NONE]++; + } + return; + } + bstat[why == BR_GAME ? BS_GAME : why == BR_PASS ? BS_PASS_FULL : why == BR_DISPATCH ? BS_DISP_FULL : BS_FULL]++; + if (why != BR_OTHER) ex->bar_dirty = 0; // after BR_OTHER the replay's own commands follow: the next pass or + ex->bar_passes = ex->bar_dispatches = 0; // dispatch orders after them vk_exec_frame.fb_layouts += n; VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, @@ -613,6 +691,25 @@ static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags void vk_exec_barrier(VkExec *ex) { barrier_to(ex, NULL, NULL, 0); } +void vk_exec_barrier_dispatch(VkExec *ex) { + barrier_to_why(ex, NULL, NULL, 0, BR_DISPATCH); + ex->bar_dispatches++; +} + +void vk_exec_barrier_log(uint64_t frame) { + static uint64_t last; + if (!(rt_trace & RT_TRACE_PERF) || frame % 600) return; + double f = frame > last ? (double)(frame - last) : 1.0; + last = frame; + char s[320]; + int o = 0; + for (int k = 0; k < BS_N; k++) { + o += snprintf(s + o, sizeof s - o, "%s%s %.1f", k ? ", " : "", bs_name[k], bstat[k] / f); + bstat[k] = 0; + } + rt_log("vk_exec: frame %llu: barriers per frame: %s", (unsigned long long)frame, s); +} + void vk_exec_pass_begin(VkExec *ex) { if (ex->in_pass) return; resolve_rt(ex); @@ -630,7 +727,8 @@ void vk_exec_pass_begin(VkExec *ex) { if (has_image(want, nwant, t->image)) continue; want[nwant] = t->image, wasp[nwant++] = i < 8 ? VK_IMAGE_ASPECT_COLOR_BIT : t->img->aspect; } - barrier_to(ex, want, wasp, nwant); // passes, clears, copies and dispatches see each other's results + barrier_to_why(ex, want, wasp, nwant, BR_PASS); // passes, clears, copies and dispatches see each other's results + ex->bar_passes++; ex->fbl_mask = fbl; ex->fb_dirty = 0; ex->fb_read = 0; @@ -1390,7 +1488,7 @@ static const op_hdr *walk(VkExec *ex, const op_hdr *h, int depth) { case OP_COPY_STRIDE: ex->copy_stride = ((const p_copy_stride *)a)->stride; break; case OP_SET_TEX_POOL: ex->tex_pool = ((const p_set_pool *)a)->pool, ex->tex_serial++; break; case OP_SET_SAMP_POOL: ex->samp_pool = ((const p_set_pool *)a)->pool; break; - case OP_BARRIER: vk_exec_barrier(ex); break; + case OP_BARRIER: barrier_to_why(ex, NULL, NULL, 0, BR_GAME); break; case OP_DRAW: case OP_DRAW_IDX: case OP_DISPATCH: case OP_UPDATE_UB: case OP_DRAW_IDX_IND: case OP_DISPATCH_IND: if (h->op == OP_DRAW || h->op == OP_DRAW_IDX || h->op == OP_DRAW_IDX_IND) vk_exec_frame.draws_seen++, split_draws++; if (h->op == OP_DISPATCH || h->op == OP_DISPATCH_IND) vk_exec_frame.dispatches++, split_draws++; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 6ce192c6..baf35d82 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -103,6 +103,10 @@ typedef struct VkExec { void *views; // image views created for this command buffer (freed at its fence) int nviews, cap_views; int barrier_at; // recorded right after the last full barrier (-1: none yet) + // NX2NAVI_BARRIER_GAME (vk_exec.c barrier_to_why): the last full barrier was one the replay itself needed (a copy, + // clear or fill of its own follows it), so the next pass or dispatch gets a full one too; passes and dispatches + // begun since the last full barrier + int bar_dirty, bar_passes, bar_dispatches; // Feedback loops (vk.h VkCtx.feedback, vk_exec_draw.c feedback_prepare). Learned targets are attached in // ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT; they stay in it across consecutive passes and go back to GENERAL at the next @@ -128,6 +132,11 @@ void vk_exec_pass_end(VkExec *ex); // Full memory barrier (all commands, all memory); ends the open pass first. Copies and dispatches order themselves with // it. Skipped when nothing was recorded since the last one. void vk_exec_barrier(VkExec *ex); +// The barrier before a game dispatch: a full one, or with NX2NAVI_BARRIER_GAME none unless the replay's own commands +// came since the last full barrier (vk_exec.c barrier_to_why). Ends the open pass. +void vk_exec_barrier_dispatch(VkExec *ex); +// NX2NAVI_TRACE=perf, every 600 frames: the barriers per frame by kind. +void vk_exec_barrier_log(uint64_t frame); // vk_texture.c (NX2NAVI_TEX_ALIAS): commands that must run at the replay's current position. vk_exec_inline_begin returns // the command buffer being recorded after a full barrier (an open pass is ended; *reopen says so), or VK_NULL_HANDLE // when this thread is not recording a replay. vk_exec_inline_end begins the pass again when it was open. diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 4f375ebe..0c611fd9 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -5182,7 +5182,7 @@ static void op_dispatch(VkExec *ex, const p_dispatch *d) { VkPipeline p = vk_pipeline_compute(cs, &info); if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped"); if ((info->pool && !pool_on()) || shader_skipped(cs)) return; // NX2NAVI_POOL=0, NX2NAVI_SKIP_SHADERS - vk_exec_barrier(ex); // ends the pass; earlier writes are visible to the shader + vk_exec_barrier_dispatch(ex); // ends the pass; earlier writes are visible to the shader bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p); c0_dim[0] = (uint32_t)d->x, c0_dim[1] = (uint32_t)d->y, c0_dim[2] = (uint32_t)d->z; twin_dispatch = 1; @@ -5217,7 +5217,7 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) { VkPipeline p = vk_pipeline_compute(cs, &info); if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped"); if ((info->pool && !pool_on()) || shader_skipped(cs)) return; // NX2NAVI_POOL=0, NX2NAVI_SKIP_SHADERS - vk_exec_barrier(ex); + vk_exec_barrier_dispatch(ex); VK_MEM_USE(ip); bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p); c0_dim[0] = c0_dim[1] = c0_dim[2] = 0; From 4a954a1c6d401e2ed5c1e8218fdf17d7f89e3e5f Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Tue, 10 Nov 2026 14:09:21 -0500 Subject: [PATCH 8/8] Refresh the texture pool from a list of its entries instead of walking every slot, for about 7 % more frames a second where the replay thread is the limit. Each submission's first sync of the bindless texture pool walked every registered slot, 33,000 in Kokiri Forest, to find the few textures the CPU wrote and the few slots that changed. Three parts of that walk change, each with its own switch (pool_fast, pool_presnap, pool_group; on by default, 0 and ab accepted): - The freshness check goes through a list of the entries the slots resolved to, with a check by 2 MiB region first; slots that resolved to nothing are looked up again when the game's memory map changes and every eighth submission. The use stamps the idle eviction reads are left from a count of the slots that hold each entry. - The check takes the write-watch snapshots of its regions first, so unchanged entries pass the region test. - The GPU-written slots of one address bucket with one descriptor form a group, which the dirty walk leaves alone while its first member is unchanged. Measured on an RTX 5090 at 2560x1440, standing in Kokiri Forest, in-run A/B over five runs with all three switched (three of them with the barrier change switched too): 93.6 to 100.7, 96.4 to 103.1, 95.8 to 102.6, 99.3 to 107.1 and 91.9 to 99.9 frames a second, the replay thread 0.7 to 0.8 ms a frame shorter; a control's blocks differ by 0.4 %. On a walk with NX2NAVI_POOL_GROUP_CHECK=1 no group that was left alone had missed a write, and screenshots across the A and B blocks show the same picture. From pull request #390. Not tried: the texture eviction under memory pressure (a 9 GiB budget was never reached on the walk), AMD and Intel drivers, Linux. One of the nine runs with it on ended by itself with exit code 0 and nothing in its log two seconds after the window lost the focus; three repeats of that run did not. Co-Authored-By: Claude Fable 5.2 --- runtime/cfg.c | 1 + runtime/host.h | 1 + runtime/memory.c | 9 ++ runtime/vk.h | 36 ++++- runtime/vk_exec_draw.c | 261 ++++++++++++++++++++++++++++-- runtime/vk_memory.c | 37 +++++ runtime/vk_texture.c | 352 ++++++++++++++++++++++++++++++++++++++--- 7 files changed, 663 insertions(+), 34 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 115377e2..31cf1cca 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,6 +54,7 @@ static const char *const known[] = { "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC", "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW", + "POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "REFLEX", "RENDER_SCALE", "REPLAY_ASYNC", "RTSS", "RTSS_ENUM", "SCENE_SHARE", "PRESNAP_PLACE", diff --git a/runtime/host.h b/runtime/host.h index e77c8a6f..e8af6dde 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -161,6 +161,7 @@ uint32_t mem_map(uint64_t dst, uint64_t src, uint64_t n); uint32_t mem_unmap(uint64_t dst, uint64_t src, uint64_t n); // 0x05 uint32_t mem_query(uint64_t a, RtMemoryInfo *out); // 0x06 int mem_readable(uint64_t a, uint64_t n); // every page readable +uint32_t mem_map_gen(void); // changes with everything mem_readable answers from (mapping, heap, permissions) int mem_read_hold(uint64_t a, uint64_t n); // mem_readable that keeps the range mapped until mem_read_release void mem_read_release(void); uint32_t mem_map_physical(uint64_t a, uint64_t n); // 0x2C diff --git a/runtime/memory.c b/runtime/memory.c index 21983d6b..c8bff935 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -683,6 +683,11 @@ static uint64_t image_bytes(void) { static int overlaps(uint64_t a, uint64_t n, uint64_t b, uint64_t m) { return a < b + m && b < a + n; } static int inside(uint64_t a, uint64_t n, RtRange r) { return a >= r.base && n <= r.size && a - r.base <= r.size - n; } +// Bumped (lock held exclusively) by every change of what is mapped or readable: vk_exec_draw.c's texture pool looks the +// slots over unreadable storage up again only then (and now and then). +static uint32_t map_gen; +uint32_t mem_map_gen(void) { return __atomic_load_n(&map_gen, __ATOMIC_ACQUIRE); } + // ---- 0x01 SetHeapSize uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) { plat_mutex_lock(&lock); @@ -693,6 +698,7 @@ uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) { if (size > heap_size) commit(L.heap.base + heap_size, size - heap_size, "heap"); if (size < heap_size) decommit(L.heap.base + size, heap_size - size, "heap"); heap_size = size; + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); if (addr) *addr = L.heap.base; return R_OK; @@ -749,6 +755,7 @@ uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm) { } memset(t, (int)perm, n / PAGE); protect(a, n, perm == PERM_RW ? PLAT_PROT_RW : perm == PERM_R ? PLAT_PROT_READ : PLAT_PROT_NONE, "module data"); + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); rt_log("%s data permission +%#llx size %#llx perm %u", m->name, (unsigned long long)(a - m->base), (unsigned long long)n, perm); @@ -820,6 +827,7 @@ static uint32_t stack_move(int unmap, uint64_t dst, uint64_t src, uint64_t n) { stack_maps[nstack_maps++] = (StackMap){dst, src, n}; } out: + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); return r; } @@ -872,6 +880,7 @@ static uint32_t alias_map(int unmap, uint64_t a, uint64_t n) { alias_bytes += fresh * PAGE; } out: + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); if (r) rt_log("%sMapPhysicalMemory %#llx+%#llx failed %#x (alias %#llx used, heap %#llx)", unmap ? "Un" : "", (unsigned long long)a, (unsigned long long)n, r, (unsigned long long)alias_bytes, diff --git a/runtime/vk.h b/runtime/vk.h index 55147b61..a70d5d21 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -565,8 +565,42 @@ uint32_t vk_texture_entry_gen(void); // vk_texture.c: bumped by // Slots whose token may have changed since *pos (0 at first; then the value these calls leave): fn is called for each // (some more than once) and *pos advanced. 0 when too many changes came since: the caller looks at every slot. *pos // from vk_texture_dirty_pos_locked after such a walk. Texture lock held. -int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void *arg); +// gfn, when given, gets each slot group of a bucket in place of fn for its members. +int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void (*gfn)(int g, void *arg), void *arg); +// Slot groups (vk_texture.c): the GPU-written slots of one address bucket with one descriptor hash kh, which resolve +// alike. head the first member, vk_texture_group_next_locked the next (-1: the end), n how many; stamp and fmask the +// texture pool's (vk_exec_draw.c pool_dirty_group: 1 when every member holds the same views, of the kinds fmask; a slot +// that joins goes to the tail, refilled on its own), set 0 by vk_texture_groups_unstamp_locked. Texture lock held. +typedef struct { uint64_t kh; int32_t head, n; uint32_t stamp, fmask; } VkSlotGroup; +VkSlotGroup *vk_texture_group_locked(int g); +int vk_texture_group_next_locked(int id); +void vk_texture_groups_unstamp_locked(void); uint64_t vk_texture_dirty_pos_locked(void); +// The bindless pool's entries (vk_texture.c): after a pool fill of slot id, notes the entry it resolved to (1; 0: none) +// and gives its number for vk_texture_pool_hold_locked in *ent (0: none); a submission's freshness check of every +// noted entry (returns how many). Texture lock held. +int vk_texture_pool_note_locked(int id, uint32_t *ent); +int vk_texture_pool_fresh_locked(void); +// A pool slot that held the entry old (0: none) now holds ent: the entries slots hold get the use stamps a lookup +// leaves (vk_texture.c touch) from the check by list. Texture lock held. +void vk_texture_pool_hold_locked(uint32_t old, uint32_t ent); +// A slot whose last lookup found no entry, looked up again: 1 it resolves now; 0 still none, its storage may become +// readable; -1 none until its token changes (a CPU slot whose texture was released after its registration, no storage). +int vk_texture_slot_retry_locked(int id); +// vk_memory.c, for the pool's freshness check: the generation table's lock, shared; with it held, 1 when every 2 MiB +// region of [addr, addr+size) has this epoch's write-watch snapshot and no page stamped after since (nothing in it can +// have changed), 0 when a closer look is due (vk_mem_changed_snap). +void vk_mem_gen_lock_shared(void); +void vk_mem_gen_unlock_shared(void); +int vk_mem_unchanged_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch); +// The same check's reason when it fails: 1 a region without this epoch's snapshot yet, 2 one untracked, 3 one written +// since, 4 nothing to check (no size, beyond the regions); 0 unchanged. +int vk_mem_unchanged_why_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch); +// The lock exclusively, for the snapshot of the regions of [addr, addr+size) that have none in epoch yet (the number of +// write-watch queries it took). +void vk_mem_gen_lock(void); +void vk_mem_gen_unlock(void); +uint64_t vk_mem_snap_range_locked(uint64_t addr, uint64_t size, uint32_t epoch); void vk_sampler_dlss_bias(float bias); // texture LOD bias added for DLSS upscaling (material samplers) void vk_sampler_aniso_live(void); // each present: NX2NAVI_ANISO after a settings change (tiled samplers) uint32_t vk_sampler_gen(void); // changes whenever a sampler slot is registered diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 0c611fd9..36fd1c4b 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -1743,6 +1743,9 @@ static int tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int stor // absent from that submit's page tables, a GPU page fault on the 128x128 probe pass of Kokiri Forest's first frame // (RADV's radv_wait_for_vm_map_updates hid it, as did NX2NAVI_SPLIT_DRAWS=0). volatile uint32_t vk_pool_kinds_used; +// Per texture slot, what pool_fill keeps between refills: the descriptor hash of the registration it was read under, +// and the lookup memo stamp its entries were last written from (0: none) with the kinds that write covered. +typedef struct { uint64_t kh; uint32_t kreg, stamp, fmask; } PoolSlot; #define POOL_SETS VK_POOL_SETS #define POOL_LOG (1u << 20) // entries of the change log: binding * VK_POOL_TEX + id, one per write static struct { @@ -1758,7 +1761,24 @@ static struct { uint64_t logn; // writes so far uint64_t *last; // per entry: the log position of its newest write (catch-up writes it once, there) uint64_t switches, waits, catchup, written, rewrites, catch_ns, sync_ns, n_slots, n_refill, n_fresh; + uint64_t n_walk[2], walk_visit[2], walk_refill[2], walk_ns[2]; // dirty walks: at a submission's start, within one + uint64_t n_visit; // slots the dirty walks looked at + uint64_t n_gwalk, n_gskip, n_gskip_slots, n_grep; // POOL_GROUP: groups walked, left alone and their slots, first + // members looked up for the memo + uint64_t n_gchk_writes, n_gchk_groups; // POOL_GROUP_CHECK: writes the groups left alone got all the same, and in how many + uint64_t n_gchk_pending, n_gchk_bad; // of members registered since their last fill (not counted above); this group's + uint64_t n_gnouni, n_gchanged; // groups walked member by member: not known to hold one resolution, first member changed + int gmode; // pool_group() as of the last sync uint64_t n_by_reg, n_by_addr; // refills whose token changed by a registration, by an address generation only + uint64_t full_ns, n_full; // the walks over every slot (a submission's first sync) and their time + uint64_t ep_ns, n_ep, n_ep_ent; // POOL_FAST: a submission's freshness check of the noted entries instead + uint64_t unres_ns, n_unres_walks; // of it: the walks over the slots without an entry + uint32_t unres_mg; // mem_map_gen of the last such walk + int unres_more; // it left a lookup for the creation budget: the next submission walks again + PoolSlot *ps; // [id] + uint8_t *unres_on; // [id]: its last fill resolved to no entry (POOL_FAST: retried each submission) + int *unres, nunres, cunres; // those slots (some may have left: unres_on clear) + uint32_t *ent; // [id]: the entry its last fill resolved to, held there (pool_hold); 0: none uint64_t dirty_pos; // vk_texture_dirty_slots_locked position (0: the next sync walks every slot) int ready, uab, sealed; // sealed: a part of the current submission already reads sets[cur] uint32_t kinds, gen, epoch, sgen; @@ -1836,7 +1856,10 @@ static VkDescriptorSetLayout pool_layout_make(void) { PL.tok = calloc(VK_POOL_TEX, sizeof *PL.tok); PL.view = calloc((size_t)VK_POOL_KINDS * VK_POOL_TEX, sizeof *PL.view); PL.samp = calloc(VK_POOL_SAMP, sizeof *PL.samp); - if (!PL.tok || !PL.view || !PL.samp) rt_fatal("vk: out of memory"); + PL.ps = calloc(VK_POOL_TEX, sizeof *PL.ps); + PL.unres_on = calloc(VK_POOL_TEX, 1); + PL.ent = calloc(VK_POOL_TEX, sizeof *PL.ent); + if (!PL.tok || !PL.view || !PL.samp || !PL.ps || !PL.unres_on || !PL.ent) rt_fatal("vk: out of memory"); rt_log("vk: bindless texture pools: %d kinds x %d textures, %d samplers, %s, %d sets (one per submission in turn)%s", VK_POOL_KINDS, VK_POOL_TEX, VK_POOL_SAMP, PL.uab ? "update after bind" : "no update-after-bind on this GPU (entries written while bound are undefined)", PL.nsets, @@ -2071,6 +2094,7 @@ uint64_t vk_pool_takes(void) { return __atomic_load_n(&pool_takes, __ATOMIC_ACQU // The current set gets the entries written into other sets in [from, to) of the log, as PL.view / PL.samp hold them now. static uint64_t pool_memo_hits; // pool_fill: slots that shared another slot's lookup (defined with pool_memo below) +static uint64_t pool_memo_same; // of those, slots whose entries held the shared views already static void pool_catch_up(uint64_t from, uint64_t to) { VkDescriptorSet set = PL.set; @@ -2089,23 +2113,47 @@ static void pool_catch_up(uint64_t from, uint64_t to) { pool_flush(); PL.switches++; if (PL.switches % 3600 == 0) { // diagnosis: what invalidates the slots (vk_texture.c vk_texture_bucket_stats) - char bs[320]; + char bs[768]; vk_texture_bucket_stats(bs, sizeof bs); rt_log("vk: texture pool buckets over 3600 switches: %s", bs); } if (PL.switches % 3600 == 0) rt_log("vk: texture pool sets: %llu switches, %llu waited for the GPU, %.1f log entries and %.1f writes per " - "catch-up, %llu whole rewrites; %.3f ms catch-up and %.3f ms entry refresh per switch (%.0f slots, %.0f " - "refilled: %.0f registered, %.0f address changes, %.0f shared a lookup; %.0f checked); %llu identical registrations kept; " + "catch-up, %llu whole rewrites; %.3f ms catch-up and %.3f ms entry refresh per switch (%.3f ms of it in " + "%.2f walks over every slot, %.3f ms in %.2f checks of %.0f listed entries and %d slots without one " + "(%.3f ms in %.2f walks over those); " + "%.0f slots, %.0f refilled: %.0f registered, %.0f address changes, %.0f " + "shared a lookup, %.0f of them holding its views already; %.0f checked); %llu identical registrations kept; " "%llu refreshes created nothing for their frame's time (pool_create_ms), %llu lookups of slots left in the frame skipped", (unsigned long long)PL.switches, (unsigned long long)PL.waits, (double)PL.catchup / (double)PL.switches, (double)PL.written / (double)PL.switches, (unsigned long long)PL.rewrites, PL.catch_ns / 1e6 / (double)PL.switches, PL.sync_ns / 1e6 / (double)PL.switches, + PL.full_ns / 1e6 / (double)PL.switches, (double)PL.n_full / (double)PL.switches, + PL.ep_ns / 1e6 / (double)PL.switches, (double)PL.n_ep / (double)PL.switches, + PL.n_ep ? (double)PL.n_ep_ent / (double)PL.n_ep : 0.0, PL.nunres, + PL.unres_ns / 1e6 / (double)PL.switches, (double)PL.n_unres_walks / (double)PL.switches, (double)PL.n_slots / (double)PL.switches, (double)PL.n_refill / (double)PL.switches, (double)PL.n_by_reg / (double)PL.switches, (double)PL.n_by_addr / (double)PL.switches, - (double)pool_memo_hits / (double)PL.switches, + (double)pool_memo_hits / (double)PL.switches, (double)pool_memo_same / (double)PL.switches, (double)PL.n_fresh / (double)PL.switches, (unsigned long long)vk_texture_same_regs(), (unsigned long long)PL.n_frame_stops, (unsigned long long)PL.n_defer_skips); + if (PL.switches % 3600 == 0) + rt_log("vk: texture pool dirty walks per switch: %.2f at a submission's start (%.0f slots looked at, %.0f " + "refilled, %.3f ms), %.2f within one (%.0f looked at, %.0f refilled, %.3f ms)", + (double)PL.n_walk[0] / (double)PL.switches, (double)PL.walk_visit[0] / (double)PL.switches, + (double)PL.walk_refill[0] / (double)PL.switches, PL.walk_ns[0] / 1e6 / (double)PL.switches, + (double)PL.n_walk[1] / (double)PL.switches, (double)PL.walk_visit[1] / (double)PL.switches, + (double)PL.walk_refill[1] / (double)PL.switches, PL.walk_ns[1] / 1e6 / (double)PL.switches); + if (PL.switches % 3600 == 0) + rt_log("vk: texture pool slot groups per switch (NX2NAVI_POOL_GROUP): %.0f walked, %.0f left alone with %.0f " + "slots, %.0f first members refilled (%.0f changed), %.0f not known to hold one resolution; check " + "(NX2NAVI_POOL_GROUP_CHECK): %llu writes in %llu groups left alone so far, %llu more to members registered " + "since their last fill", + (double)PL.n_gwalk / (double)PL.switches, (double)PL.n_gskip / (double)PL.switches, + (double)PL.n_gskip_slots / (double)PL.switches, (double)PL.n_grep / (double)PL.switches, + (double)PL.n_gchanged / (double)PL.switches, (double)PL.n_gnouni / (double)PL.switches, + (unsigned long long)PL.n_gchk_writes, (unsigned long long)PL.n_gchk_groups, + (unsigned long long)PL.n_gchk_pending); } // Entry id of every kind in mask: the slot's view of that kind or the kind's stand-in. Texture lock held. 0: the @@ -2151,22 +2199,75 @@ static int pool_on(void) { // transient render target registered over its storage, and every finalize and initialize of that target changes their // tokens, so a refill looked up the same texture ~400 times, ~15,000 lookups a frame in Kokiri Forest. An entry holds the // views one slot got, for the slots with its hash, address generation and entry generation. +// Its stamp changes only when one of its views does (NX2NAVI_POOL_FAST, default on): a slot whose entries were written +// from the entry at the stamp it has now (PoolSlot) holds those views already, so a refill that finds the entry so +// compares and writes nothing. That is most refills: a transient render target released and taken back moves the +// address generation of its storage, ~1,300 slots in Kokiri Forest, and every slot resolves as before (18,500 of 18,800 +// refills a submission shared a lookup, each comparing up to 16 views a quarter megabyte apart: 1.9 ms). #define POOL_MEMO 4096 -static struct { uint64_t kh; uint32_t agen, egen, mask; VkImageView view[VK_POOL_KINDS]; } pool_memo[POOL_MEMO]; -static uint64_t pool_memo_hits; +static struct { uint64_t kh; uint32_t agen, egen, mask, stamp, got, ent; VkImageView view[VK_POOL_KINDS]; } pool_memo[POOL_MEMO]; +static uint32_t pool_stamp; // the last stamp given out +static uint64_t pool_memo_hits, pool_memo_same; + +static int pool_fast(void) { // ab: in the B blocks + static int on = -1; + if (on < 0) on = rt_knob("POOL_FAST", "1"); + return RT_KNOB_ON(on); +} + +// Slot id's last fill resolved to the entry ent (vk_texture_pool_note_locked; 0: none). vk_texture.c counts the slots +// that hold each entry: the entries held are the ones the walk over every slot reached with a lookup in each +// submission, and the check by list leaves their use stamps from that count (vk_texture_pool_hold_locked). +static void pool_hold(uint32_t id, uint32_t ent) { + if (PL.ent[id] == ent) return; + vk_texture_pool_hold_locked(PL.ent[id], ent); + PL.ent[id] = ent; +} + +// Slot id's last fill resolved to no entry (got 0) or to one: the slots without one are looked up again each +// submission (pool_sync_entries), as the walk over every slot did: an entry their storage could not have then may be +// made now, and its address generation then refills them. +static void pool_unres(uint32_t id, int got) { + if (got || PL.unres_on[id]) { + if (got) PL.unres_on[id] = 0; + return; + } + PL.unres_on[id] = 1; + if (PL.nunres == PL.cunres) { + int n = PL.cunres ? PL.cunres * 2 : 1024; + int *p = realloc(PL.unres, sizeof *p * (size_t)n); + if (!p) rt_fatal("vk: out of memory"); + PL.unres = p, PL.cunres = n; + } + PL.unres[PL.nunres++] = (int)id; +} static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { vk_texture_create_deferred_locked(); - uint64_t kh = tok ? vk_texture_slot_khash_locked((int)id) : 0; + PoolSlot *ps = &PL.ps[id]; + uint64_t kh = 0; + if (tok) { // the descriptor hash changes only with a registration (the token's upper half) + uint32_t reg = (uint32_t)(tok >> 32); + if (ps->kreg != reg || !ps->kh) ps->kh = vk_texture_slot_khash_locked((int)id), ps->kreg = reg; + kh = ps->kh; + } uint32_t ag = (uint32_t)tok; typeof(pool_memo[0]) *m = kh ? &pool_memo[(kh >> 20) % POOL_MEMO] : NULL; if (m && m->kh == kh && m->agen == ag && m->egen == vk_texture_entry_gen() && (m->mask & mask) == mask) { + pool_memo_hits++; + pool_unres(id, (int)m->got); + pool_hold(id, m->ent); + if (ps->stamp && ps->stamp == m->stamp && (ps->fmask & mask) == mask && pool_fast()) { + pool_memo_same++; + return 1; + } for (int k = 0; k < VK_POOL_KINDS; k++) { if (!(mask >> k & 1)) continue; VkImageView *e = &PL.view[(size_t)k * VK_POOL_TEX + id]; if (*e != m->view[k]) *e = m->view[k], pool_write((uint32_t)k, id, m->view[k], VK_NULL_HANDLE); } - pool_memo_hits++; + ps->fmask = ps->stamp == m->stamp ? ps->fmask | mask : mask; + ps->stamp = m->stamp; return 1; } for (int k = 0; k < VK_POOL_KINDS; k++) { @@ -2180,12 +2281,29 @@ static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { if (*e != ii.imageView) *e = ii.imageView, pool_write((uint32_t)k, id, ii.imageView, VK_NULL_HANDLE); } int ok = !vk_texture_create_deferred_locked(); + uint32_t ent = 0; + int got = vk_texture_pool_note_locked((int)id, &ent); + if (tok) pool_unres(id, got); + pool_hold(id, tok ? ent : 0); + ps->stamp = 0; // its entries match no memo entry, unless the one below if (m && ok) { // the generation after the lookups: an entry they created is part of what they resolved uint32_t eg = vk_texture_entry_gen(); - if (m->kh != kh || m->agen != ag || m->egen != eg) m->kh = kh, m->agen = ag, m->egen = eg, m->mask = 0; + // a view that changes gives the entry a new stamp; one kept outside the mask keeps its value, so a kind that + // comes back with another view is a change too + int changed = !m->stamp; for (int k = 0; k < VK_POOL_KINDS; k++) - if (mask >> k & 1) m->view[k] = PL.view[(size_t)k * VK_POOL_TEX + id]; + if (mask >> k & 1) { + VkImageView v = PL.view[(size_t)k * VK_POOL_TEX + id]; + if (m->view[k] != v) m->view[k] = v, changed = 1; + } + if (m->kh != kh || m->agen != ag || m->egen != eg) m->kh = kh, m->agen = ag, m->egen = eg, m->mask = 0; m->mask |= mask; + m->got = (uint32_t)got, m->ent = ent; + if (changed) { + if (!++pool_stamp) pool_stamp = 1; + m->stamp = pool_stamp; + } + ps->stamp = m->stamp, ps->fmask = mask; } return ok; } @@ -2237,6 +2355,7 @@ static int pool_refill(VkExec *ex, uint32_t id, uint32_t used, uint64_t t, int s static void pool_dirty_slot(int id, void *arg) { // vk_texture_dirty_slots_locked: a slot whose token may have changed PoolDirty *d = arg; + PL.n_visit++; if (id >= d->n) return; uint64_t t = vk_texture_slot_token(id); if (t == PL.tok[id]) return; @@ -2253,6 +2372,76 @@ static int pool_current(void) { vk_texture_slots_gen() == PL.gen && vk_texture_epoch() == PL.epoch && vk_sampler_gen() == PL.sgen; } +// NX2NAVI_POOL_GROUP=1 (default), 0, ab: a dirty bucket's slot groups (vk_texture.c) are walked whole. The members of a +// group resolve alike, so once a walk of the group brought every member to the current resolution (every fill done, an +// entry for each) they all hold the same views, and later the group is left alone when its first member still holds the +// current resolution: its token did not change (its views are what that token resolves to), or its refill wrote nothing. +// A slot that joined since is at the tail and refilled on its own (its registration's entry on the dirty ring). +// The members keep their older tokens: their views are what those tokens resolved to, and what the current ones do. +// (Comparing the memo's stamps instead left half the groups to their members: the memo's entries are shared between +// hashes, so the same views came back under a new stamp.) +static int pool_group(void) { + static int mode = -1; + if (mode < 0) mode = rt_knob("POOL_GROUP", "1"); + return RT_KNOB_ON(mode); +} + +// NX2NAVI_POOL_GROUP_CHECK=1: a group left alone is walked member by member all the same, and the writes that walk +// makes are counted (none when the rule holds; the pool log's slot groups line), but for the members registered since +// their last fill: their registration's entry on the dirty ring refills them in the same walk (counted apart). +static int pool_group_check(void) { + static int on = -1; + if (on < 0) on = rt_knob("POOL_GROUP_CHECK", "0") == 1; // a diagnosis: no settings row + return on; +} + +static void pool_dirty_group(int g, void *arg) { + PoolDirty *d = arg; + uint64_t chk = 0; + VkSlotGroup *G = vk_texture_group_locked(g); + int rep = G->head; + PL.n_gwalk++; + if (!G->stamp) PL.n_gnouni++; + if (G->stamp && (G->fmask & d->used) == d->used && rep >= 0 && rep < d->n && !PL.unres_on[rep]) { + uint64_t t = vk_texture_slot_token(rep); + int same = t == PL.tok[rep]; + if (!same) { // the first member's refill: nothing written, an entry found, its token taken + uint64_t l0 = PL.logn; + PL.n_grep++; + pool_dirty_slot(rep, arg); + same = PL.tok[rep] == t && PL.logn == l0 && !PL.unres_on[rep]; + if (!same) PL.n_gchanged++; + } + if (same) { + PL.n_gskip++, PL.n_gskip_slots += (uint64_t)G->n; + if (!pool_group_check()) return; + chk = PL.logn + 1; + } + } + int r0 = d->retry, uni = 1; // member by member: then all hold the current resolution, unless a fill was left + d->retry = 0; + for (int id = G->head; id >= 0; id = vk_texture_group_next_locked(id)) { + if (id >= d->n) continue; // outside the pool + if (chk) { // the check: a member registered since its last fill is refilled by its own entry anyway + uint64_t l0 = PL.logn; + int pending = vk_texture_slot_token(id) >> 32 != PL.tok[id] >> 32; + pool_dirty_slot(id, arg); + if (PL.logn != l0) { + if (pending) PL.n_gchk_pending += PL.logn - l0; + else PL.n_gchk_writes += PL.logn - l0, PL.n_gchk_bad++; + } + } else { + pool_dirty_slot(id, arg); + } + if (PL.unres_on[id]) uni = 0; + } + if (d->retry) uni = 0; + d->retry |= r0; + G = vk_texture_group_locked(g); + G->stamp = (uint32_t)uni, G->fmask = uni ? d->used : 0; + if (chk && PL.n_gchk_bad) PL.n_gchk_groups++, PL.n_gchk_bad = 0; +} + static void pool_sync_entries(VkExec *ex) { uint32_t used = __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED); uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen(); @@ -2277,12 +2466,51 @@ static void pool_sync_entries(VkExec *ex) { vk_texture_create_budget_locked(spent ? 0 : pool_create_budget()); PL.n_frame_stops += (uint64_t)spent; // Only a slot change since the last sync (no new kinds, no new submission, nothing left over): the slots that - // changed (vk_texture_dirty_slots_locked), not the whole table + // changed (vk_texture_dirty_slots_locked), not the whole table. With NX2NAVI_POOL_FAST (default on) a new + // submission too: its freshness check goes through the entries the slots resolved to and the slots that + // resolved to none (vk_texture_pool_fresh_locked, pool_unres), not every slot. PoolDirty pd = {ex, used, n, 0, spent}; - int walked = !fresh && ep == PL.epoch && !PL.retry && PL.dirty_pos && - vk_texture_dirty_slots_locked(&PL.dirty_pos, pool_dirty_slot, &pd); + int newep = ep != PL.epoch; + uint64_t v0 = PL.n_visit, f0 = PL.n_refill, w0 = kern_now_ns(); + int grp = pool_group(); + if (grp && !PL.gmode) vk_texture_groups_unstamp_locked(); // members were refilled one by one meanwhile + PL.gmode = grp; + int walked = !fresh && (!newep || pool_fast()) && !PL.retry && PL.dirty_pos && + vk_texture_dirty_slots_locked(&PL.dirty_pos, pool_dirty_slot, grp ? pool_dirty_group : NULL, &pd); + if (walked) { + int at = newep ? 0 : 1; + PL.n_walk[at]++, PL.walk_visit[at] += PL.n_visit - v0, PL.walk_refill[at] += PL.n_refill - f0; + PL.walk_ns[at] += kern_now_ns() - w0; + } retry = pd.retry; + if (walked && newep) { + uint64_t te = kern_now_ns(); + PL.n_ep_ent += (uint64_t)vk_texture_pool_fresh_locked(); + // The slots without an entry are over storage the guest has not mapped (stale descriptors, 1,160 in + // Kokiri Forest): looked up again when the mapping changed, while a lookup is left for the creation budget, + // and every 8th submission for the rest (an entry whose creation failed, an image at their address that + // took GPU content since: slot_resolve's alias rule) + uint32_t mg = mem_map_gen(); + uint64_t tu = kern_now_ns(); + int walk_unres = mg != PL.unres_mg || PL.unres_more || !(PL.n_ep & 7); + if (walk_unres) PL.unres_mg = mg, PL.n_unres_walks++, vk_texture_create_deferred_locked(); + for (int i = 0; walk_unres && i < PL.nunres;) { + int id = PL.unres[i]; + uint64_t t = PL.unres_on[id] ? vk_texture_slot_token(id) : 0; + int r = t && t == PL.tok[id] ? vk_texture_slot_retry_locked(id) : -1; + if (r) { // resolves now (an entry it made changes its token: refilled), was refilled since, or + PL.unres_on[id] = 0; // resolves to none until its token changes (the refill lists it again) + PL.unres[i] = PL.unres[--PL.nunres]; + continue; + } + i++; + } + if (walk_unres) PL.unres_more = vk_texture_create_deferred_locked(); + PL.unres_ns += kern_now_ns() - tu; + PL.n_ep++, PL.ep_ns += kern_now_ns() - te; + } if (!walked) PL.dirty_pos = vk_texture_dirty_pos_locked(); + uint64_t tw = walked ? 0 : kern_now_ns(); for (int id = 0; id < n && !walked; id++) { // slots past n were never registered: stand-ins since the first sync uint64_t t = vk_texture_slot_token(id); if (t != PL.tok[id]) { @@ -2298,7 +2526,10 @@ static void pool_sync_entries(VkExec *ex) { vk_texture_by_id_locked(id, NULL, NULL); // the submit's freshness check } } - if (!walked) PL.n_slots += (uint64_t)n; + if (!walked) { + PL.n_slots += (uint64_t)n, PL.n_full++, PL.full_ns += kern_now_ns() - tw; + vk_texture_groups_unstamp_locked(); // the walk over every slot refilled members one by one + } vk_texture_create_budget_locked(-1); } PL.retry = retry != 0; diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index f2ee73a1..e730abea 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -914,6 +914,43 @@ int vk_mem_changed_snap(uint64_t addr, uint64_t size, uint32_t since, uint32_t e return untracked ? -1 : changed; } +void vk_mem_gen_lock_shared(void) { plat_mutex_lock_shared(&gen_lock); } +void vk_mem_gen_unlock_shared(void) { plat_mutex_unlock_shared(&gen_lock); } + +void vk_mem_gen_lock(void) { plat_mutex_lock(&gen_lock); } +void vk_mem_gen_unlock(void) { plat_mutex_unlock(&gen_lock); } + +int vk_mem_unchanged_why_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch) { + if (!size || !epoch) return 4; + uint64_t lo = addr & ~0xfffull, hi = (addr + size + 0xfff) & ~0xfffull; + uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; + if (r1 >= SNAP_REGIONS) return 4; + for (uint64_t r = r0; r <= r1; r++) { // REG_SHIFT is SNAP_SHIFT: reg_max is the newest stamp in the region + if (snap_at[r] != epoch) return 1; + if (snap_untracked[r]) return 2; + if (reg_max[r] > since) return 3; + } + return 0; +} + +int vk_mem_unchanged_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch) { + return !vk_mem_unchanged_why_locked(addr, size, since, epoch); +} + +uint64_t vk_mem_snap_range_locked(uint64_t addr, uint64_t size, uint32_t epoch) { + if (!size || !epoch) return 0; + uint64_t lo = addr & ~0xfffull, hi = (addr + size + 0xfff) & ~0xfffull; + uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; + if (r1 >= SNAP_REGIONS) return 0; + for (uint64_t r = r0; r <= r1; r++) + if (snap_at[r] != epoch) { + uint64_t q = snap_regions(r0, r1, epoch); + n_snap_calls += q; + return q; + } + return 0; +} + void vk_mem_snap_stats(uint64_t *checks, uint64_t *calls, uint64_t *untracked) { *checks = __atomic_exchange_n(&n_snap_checks, 0, __ATOMIC_RELAXED); *calls = __atomic_exchange_n(&n_snap_calls, 0, __ATOMIC_RELAXED); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index e9fc6390..87a9cc4b 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -89,6 +89,8 @@ typedef struct Tex { uint64_t ow_frame; // gfx_counters.frames when overwritten was set uint8_t overwritten; // released, and a texture at another address was drawn over its storage (overwrite_others) uint8_t in_gwin; // in gwin[] + int pidx; // 1 + its index in pent[] (vk_texture_pool_note_locked); 0: not listed + int ph; // 1 + its index in pref[] (pool_ref); 0: none } Tex; // A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the @@ -296,32 +298,135 @@ static void tex_gen_flip(Tex *e, int plain) { dirty_push(DIRTY_BUCKET | b); } -// Moves slot id into bucket b's list (the lists exist once a slot was registered: vk_texture_register). -static void slot_rebucket(int id, uint32_t b) { +// Slot groups: the GPU-written slots of one address bucket that share a descriptor hash (TexSlot.khash) resolve alike +// under the same address and entry generations, as the texture pool's memo has it (vk_exec_draw.c pool_fill). Ocarina +// keeps up to ~400 descriptors of each transient render target registered over its storage, and the fullest bucket, +// 1,281 slots, moves 8 times a submission in Kokiri Forest: 12,800 of the 17,600 slots a submission's dirty walks +// refilled held their views already. The dirty walk can hand each group to the pool whole (vk_texture_dirty_slots_locked's +// gfn), which leaves its members alone when their resolution did not change (NX2NAVI_POOL_GROUP). A slot is in its +// bucket's own list (khash 0: a CPU-written texture) or in its group's member list, through the same links. A slot that +// joins a group goes to its tail and leaves the group's stamp as it was: its registration put it on the dirty ring, so +// the walk that follows refills it on its own, and the head, whose state the pool tests, is a member from before. (The +// game's 118 registrations a submission cleared the stamps of the groups they joined, and the walk at a submission's +// start went member by member as before: 11,300 of 11,550 slots.) +typedef struct { VkSlotGroup pub; int32_t gnext, gprev, tail; uint32_t bucket; } SlotGroup; +static SlotGroup *grp; +static int ngrp, cgrp, grp_free = -1, ngrp_live; +static uint64_t n_grouped; // slots in groups +static int32_t bucket_ghead[AGEN_N]; // per bucket: its first group (-1: none; set with the lists) +static int32_t *slot_grp; // per slot id: its group, -1 none +typedef struct { uint64_t kh, b; } GrpKey; +static nvn_hmap grp_by = {.klen = sizeof(GrpKey)}; // -> group + 1 + +static int grp_get(uint64_t kh, uint32_t b) { + GrpKey k = {kh, b}; + void *v = nvn_hm_get(&grp_by, &k); + if (v) return (int)((intptr_t)v - 1); + int g; + if (grp_free >= 0) { + g = grp_free, grp_free = grp[g].gnext; + } else { + if (ngrp == cgrp) { + int n = cgrp ? cgrp * 2 : 1024; + SlotGroup *p = realloc(grp, sizeof *p * (size_t)n); + if (!p) rt_fatal("vk: out of memory for texture slot groups"); + grp = p, cgrp = n; + } + g = ngrp++; + } + SlotGroup *G = &grp[g]; + memset(G, 0, sizeof *G); + G->pub.kh = kh, G->pub.head = G->tail = -1, G->bucket = b; + G->gprev = -1, G->gnext = bucket_ghead[b]; + if (G->gnext >= 0) grp[G->gnext].gprev = g; + bucket_ghead[b] = g; + nvn_hm_put(&grp_by, &k, (void *)(intptr_t)(g + 1)); + ngrp_live++; + return g; +} + +static void grp_drop(int g) { + SlotGroup *G = &grp[g]; + GrpKey k = {G->pub.kh, G->bucket}; + nvn_hm_del(&grp_by, &k); + if (G->gprev >= 0) grp[G->gprev].gnext = G->gnext; + else bucket_ghead[G->bucket] = G->gnext; + if (G->gnext >= 0) grp[G->gnext].gprev = G->gprev; + G->pub.kh = 0, G->pub.head = G->tail = -1, G->pub.n = 0, G->pub.stamp = 0; + G->gnext = grp_free, grp_free = g; + ngrp_live--; +} + +// Places slot id in bucket b: at the tail of its group when it has a descriptor hash, else at the head of the bucket's +// own list (the lists exist once a slot was registered: vk_texture_register). +static void slot_place(int id, uint32_t b, uint64_t kh) { if (slot_prev[id] != -2) { // out of its old list + int32_t g = slot_grp[id]; + int32_t *head = g >= 0 ? &grp[g].pub.head : &bucket_head[slot_bucket[id]]; if (slot_prev[id] >= 0) slot_next[slot_prev[id]] = slot_next[id]; - else bucket_head[slot_bucket[id]] = slot_next[id]; + else *head = slot_next[id]; if (slot_next[id] >= 0) slot_prev[slot_next[id]] = slot_prev[id]; + else if (g >= 0) grp[g].tail = slot_prev[id]; + slot_grp[id] = -1; + if (g >= 0) { + n_grouped--; + if (!--grp[g].pub.n) grp_drop(g); + } } slot_bucket[id] = (uint16_t)b; + if (kh) { + int g = grp_get(kh, b); // may move grp + SlotGroup *G = &grp[g]; + slot_grp[id] = g; + G->pub.n++, n_grouped++; + slot_next[id] = -1; + slot_prev[id] = G->tail; + if (G->tail >= 0) slot_next[G->tail] = id; + else G->pub.head = id; + G->tail = id; + return; + } slot_prev[id] = -1; slot_next[id] = bucket_head[b]; if (bucket_head[b] >= 0) slot_prev[bucket_head[b]] = id; bucket_head[b] = id; } +VkSlotGroup *vk_texture_group_locked(int g) { return &grp[g].pub; } +int vk_texture_group_next_locked(int id) { return slot_next[id]; } +void vk_texture_groups_unstamp_locked(void) { + for (int g = 0; g < ngrp; g++) grp[g].pub.stamp = 0; +} + uint64_t vk_texture_dirty_pos_locked(void) { return ndirty; } -int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void *arg) { +// A bucket listed several times since *pos is walked once a round: the walk reads its slots' tokens after every change +// listed before the round began (the lock is held). Changes the walk itself makes (an entry a lookup created) are the +// next round's. A bucket's slot groups go to gfn when given, else their members to fn. +int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void (*gfn)(int g, void *arg), void *arg) { if (!slot_next || ndirty - *pos > DIRTY_N) { *pos = ndirty; return 0; } - for (uint64_t q = *pos; q < ndirty; q++) { - uint32_t x = dirty[q % DIRTY_N]; - if (x & DIRTY_BUCKET) - for (int32_t id = bucket_head[x & ~DIRTY_BUCKET]; id >= 0; id = slot_next[id]) fn(id, arg); - else fn((int)x, arg); + static uint32_t seen[AGEN_N], round; + for (uint64_t q = *pos; q < ndirty;) { + uint64_t end = ndirty; + if (!++round) memset(seen, 0, sizeof seen), round = 1; + for (; q < end; q++) { + uint32_t x = dirty[q % DIRTY_N]; + if (x & DIRTY_BUCKET) { + uint32_t b = x & ~DIRTY_BUCKET; + if (seen[b] == round) continue; + seen[b] = round; + for (int32_t id = bucket_head[b]; id >= 0; id = slot_next[id]) fn(id, arg); + for (int32_t g = bucket_ghead[b]; g >= 0; g = grp[g].gnext) { // the walk registers nothing + if (gfn) gfn(g, arg); + else for (int32_t id = grp[g].pub.head; id >= 0; id = slot_next[id]) fn(id, arg); + } + } else { + fn((int)x, arg); + } + } } *pos = ndirty; return 1; @@ -1121,10 +1226,189 @@ static void rel_unlink(Tex *e) { e->rel_prev = e->rel_next = NULL; } +// The entries the bindless texture pool's slots resolved to, for a submission's freshness check (make_fresh_snap once +// per entry), which looked at every slot instead: 33,000 slots, 1.5 ms of the replay thread a submission in Kokiri +// Forest. vk_exec_draw.c pool_fill notes a slot's entry after it resolved the slot; only entries whose content the CPU +// provides are listed (the check does nothing for the others; one the GPU took over, gpu_owned, never uploads again +// and leaves), and an entry stays listed until it is destroyed: the list holds every entry a slot resolves to, and +// some that no slot does any more. Each keeps its storage range and the page generation of its last check here, so +// that the check of an entry whose 2 MiB regions took no write since (vk_mem_unchanged_locked, one shared lock for the +// whole list) touches neither the entry nor the lock: 5,600 entries took 0.77 ms a submission one by one. Texture lock +// held. +typedef struct { Tex *e; uint64_t addr, size; uint32_t since; int ok; } PEnt; // ok: uploaded as of since +static PEnt *pent; +static int npent, cpent, *pq, cpq; +static uint64_t n_pool_checks, n_pool_skips; +// The region check's misses (vk_mem_unchanged_why_locked): not uploaded, a region without this epoch's snapshot yet, one +// untracked, one written since, nothing to check; and the snapshot queries its own pass made (NX2NAVI_POOL_PRESNAP). +static uint64_t n_pool_miss[5], n_presnap_q; +static void make_fresh_snap(Tex *e); + +static void pool_unlist(Tex *e) { + if (!e->pidx) return; + int i = e->pidx - 1; + pent[i] = pent[--npent]; + pent[i].e->pidx = i + 1; + e->pidx = 0; +} + +// The entries the pool's slots hold: for each, how many slots' last fill resolved to it (vk_exec_draw.c pool_hold; a fill +// that shared another slot's lookup counts for the entry that lookup found). The walk over every slot looked each slot +// up in each submission, which left its entry's use stamp (touch): the idle clock reads it, and start_at's alias rule +// reads the frame of an entry with GPU content. The check by list is no lookup, so it leaves the stamps from these +// counts (pool_touch): in each submission for the GPU-owned entries, in turn for the others, each once in +// POOL_TOUCH_DIV submissions (IDLE_ROUNDS is 240 of them). An entry keeps its place until it is destroyed and no slot +// holds it any more: a destroyed one is refilled out of its slots by the bump of its bucket. +#define POOL_TOUCH_DIV 64 +typedef struct { Tex *e; uint32_t refs; int next; uint8_t gown; } PRef; // e NULL: destroyed, or free (refs 0, next) +static PRef *pref; +static int npref, cpref, pref_free; // pref_free: 1 + the first free place +static void touch(Tex *e); + +static uint32_t pool_ref(Tex *e) { + if (e->ph) return (uint32_t)e->ph; + int i; + if (pref_free) { + i = pref_free - 1, pref_free = pref[i].next; + } else { + if (npref == cpref) { + int n = cpref ? cpref * 2 : 1024; + PRef *q = realloc(pref, sizeof *q * (size_t)n); + if (!q) rt_fatal("vk: out of memory for the texture pool's entry counts"); + pref = q, cpref = n; + } + i = npref++; + } + pref[i] = (PRef){e, 0, 0, e->pub.gpu_owned}; + return (uint32_t)(e->ph = i + 1); +} + +void vk_texture_pool_hold_locked(uint32_t old, uint32_t ent) { + if (ent) pref[ent - 1].refs++; + if (old && !--pref[old - 1].refs && !pref[old - 1].e) pref[old - 1].next = pref_free, pref_free = (int)old; +} + +static void pool_forget(Tex *e) { // destroy_tex + if (!e->ph) return; + PRef *q = &pref[e->ph - 1]; + q->e = NULL; + if (!q->refs) q->next = pref_free, pref_free = e->ph; + e->ph = 0; +} + +static void pool_touch(void) { + static int at; + for (int i = 0; i < npref; i++) + if (pref[i].gown && pref[i].refs && pref[i].e) touch(pref[i].e); + for (int k = (npref + POOL_TOUCH_DIV - 1) / POOL_TOUCH_DIV; k-- > 0;) { + if (at >= npref) at = 0; + const PRef *q = &pref[at++]; + if (!q->gown && q->refs && q->e) touch(q->e); + } +} + +// e becomes GPU-owned: the list's check does nothing for it any more, and its use stamp is left in each submission. +static void pool_gpu_owned(Tex *e) { + pool_unlist(e); + if (e->ph) pref[e->ph - 1].gown = 1; +} + +int vk_texture_pool_note_locked(int id, uint32_t *ent) { + *ent = 0; + if (id < 0 || id >= ntslots || !tslots[id].used) return 0; + // The caller's lookups left the slot resolved under its bucket's generation, unless it made none (a fill without a + // token or a kind): an entry cached under another generation may be destroyed, so the slot is looked up first. + if (tslots[id].ce && tslots[id].cgen != agen[sinfo[id] & 0xffff]) vk_texture_by_id_locked(id, NULL, NULL); + Tex *e = tslots[id].ce; + if (!e) return 0; + if (!e->pidx && e->pub.mirrored && !e->pub.gpu_owned) { + if (npent == cpent) { + int n = cpent ? cpent * 2 : 1024; + PEnt *p = realloc(pent, sizeof *p * (size_t)n); + int *q = realloc(pq, sizeof *q * (size_t)n); + if (!p || !q) rt_fatal("vk: out of memory for the texture pool's entry list"); + pent = p, pq = q, cpent = cpq = n; + } + pent[npent++] = (PEnt){e, e->pub.addr, e->t.L.size, 0, 0}; + e->pidx = npent; + } + *ent = pool_ref(e); + return 1; +} + +// Released entries are left out: a slot resolves to none of those whose content the CPU provides (slot_resolve's stale +// rule), and the game may be unmapping their storage. +// NX2NAVI_POOL_PRESNAP=1 (default), 0, ab: the check takes the write-watch snapshot of an entry's regions that have +// none in this submission yet before it looks at them (one exclusive hold of the generation table's lock for the list), +// so an unchanged entry in such a region passes there. Without it 97 % of the 5,600 entries missed the region check for +// want of a snapshot and went the long way, one entry and one lock at a time. With it 97 % pass, 3.5 snapshot queries +// a submission: the check 0.50 to 0.19 ms a submission, the replay thread -0.37 ms against the control. +static int pool_presnap(void) { + static int mode = -1; + if (mode < 0) mode = rt_knob("POOL_PRESNAP", "1"); + return RT_KNOB_ON(mode); +} + +// The submission being recorded checked the pool's entries by this list: for the idle clock it walked the pool +// (vk_texture_new_submit). +static int pool_by_list; + +int vk_texture_pool_fresh_locked(void) { + int nq = 0, ps = pool_presnap(); + pool_by_list = 1; + if (ps) vk_mem_gen_lock(); + else vk_mem_gen_lock_shared(); + for (int i = 0; i < npent; i++) { + const PEnt *p = &pent[i]; + if (p->ok) { + if (ps) n_presnap_q += vk_mem_snap_range_locked(p->addr, p->size, submit_epoch); + int why = vk_mem_unchanged_why_locked(p->addr, p->size, p->since, submit_epoch); + if (!why) continue; + n_pool_miss[why]++; + } else { + n_pool_miss[0]++; + } + pq[nq++] = i; + } + if (ps) vk_mem_gen_unlock(); + else vk_mem_gen_unlock_shared(); + n_pool_skips += (uint64_t)(npent - nq); + for (int k = nq; k-- > 0;) { // from the end: an entry leaving swaps the last one into its place + int i = pq[k]; + PEnt *p = &pent[i]; + Tex *e = p->e; + if (e->pub.gpu_owned) { + pool_unlist(e); + continue; + } + if (e->released) continue; + if (!e->uploaded || e->checked != submit_epoch) { + make_fresh_snap(e); + e->checked = submit_epoch; + n_pool_checks++; + } + p->since = e->wgen, p->ok = e->uploaded; + } + pool_touch(); + return npent; +} + +// slot_resolve's last answer was none because of its stale rule (vk_texture_slot_retry_locked) +static int resolve_stale; + +int vk_texture_slot_retry_locked(int id) { + if (id < 0 || id >= ntslots || !tslots[id].used || !tslots[id].t.mem) return -1; + resolve_stale = 0; + if (vk_texture_by_id_locked(id, NULL, NULL)) return 1; + return resolve_stale ? -1 : 0; +} + static void retire_remove(Tex *e); static void destroy_tex(Tex *e) { if (e->retiring) retire_remove(e); + pool_unlist(e); + pool_forget(e); own_remove(e); gwin_remove(e); n_site[0]++; @@ -1186,6 +1470,10 @@ static int comes_back_intact(const Tex *e) { return e->pub.mirrored && !e->pub.g // texture object, nothing samples it. The clock is look_round, which only a submission whose slot checks reached half // of the registered pool slots moves on (vk_texture_new_submit): counted in submissions, a pause screen or a fade that // draws nothing through the pool made the whole world's textures idle within seconds. +// With NX2NAVI_POOL_FAST the refresh checks the entries by list instead (vk_texture_pool_fresh_locked), which is no +// lookup. Such a submission counts for the clock all the same, and the entries the pool's slots hold get their stamp +// from the count of those slots (pool_touch), each at least once in POOL_TOUCH_DIV submissions: an entry a slot names +// never comes near IDLE_ROUNDS. #define IDLE_ROUNDS 240 static uint32_t look_round = 1; // submissions whose lookups walked the texture pool static uint32_t round_checks; // slot checks of the submission being recorded (vk_texture_by_id_locked) @@ -1198,6 +1486,7 @@ static void touch(Tex *e) { } void vk_texture_pool_ctx(int on) { pool_ctx = on; } + static int idle(const Tex *e, uint64_t now); // NX2NAVI_TEX_RELEASED_MB (default a thirty-second of the installed memory, 512 MiB to 2 GiB; 0 = no limit) bounds the @@ -1552,6 +1841,7 @@ static uint64_t evict_disused(uint64_t want) { } retiring[nretiring++] = e; e->retiring = 1, e->retire_takes = takes, e->retire_due = 0; + pool_unlist(e); // no slot resolves to it in the pool refresh; taken back, the refill of its slots lists it again retiring_bytes += e->bytes; freed += e->bytes; tex_gen_bump(e->key.addr); @@ -2336,6 +2626,7 @@ static void start_at(Tex *e, const TexKey *k, int taken_back) { if (e->pub.mirrored) { // the content is src's GPU render, newer than the storage bytes vk_mem_changed(e->pub.addr, e->t.L.size, 0, &e->wgen); e->uploaded = 1; + pool_gpu_owned(e); e->pub.gpu_owned = 1; } e->chash_ok = 0; @@ -2491,6 +2782,7 @@ void vk_texture_gpu_written(const tex_t *t) { e->scan_frame = now + 1; overwrite_others(e, now); } + pool_gpu_owned(e); e->pub.gpu_owned = 1; e->chash_ok = 0; // the image now holds the GPU's content: matching storage bytes no longer mean "unchanged" if (e->pub.skip) { // texture detail leaves out what the GPU can write (detail_skip); said if it ever does @@ -2578,10 +2870,11 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { if (!sinfo) { uint64_t *p = calloc(1 << 20, sizeof *p); slot_next = malloc(sizeof *slot_next << 20), slot_prev = malloc(sizeof *slot_prev << 20); + slot_grp = malloc(sizeof *slot_grp << 20); slot_bucket = calloc(1 << 20, sizeof *slot_bucket); - if (!p || !slot_next || !slot_prev || !slot_bucket) rt_fatal("vk: out of memory for texture slot tokens"); - for (int i = 0; i < (1 << 20); i++) slot_prev[i] = -2, slot_next[i] = -1; - for (int i = 0; i < AGEN_N; i++) bucket_head[i] = -1; + if (!p || !slot_next || !slot_prev || !slot_grp || !slot_bucket) rt_fatal("vk: out of memory for texture slot tokens"); + for (int i = 0; i < (1 << 20); i++) slot_prev[i] = -2, slot_next[i] = -1, slot_grp[i] = -1; + for (int i = 0; i < AGEN_N; i++) bucket_head[i] = -1, bucket_ghead[i] = -1; __atomic_store_n(&sinfo, p, __ATOMIC_RELEASE); } TexSlot *s = &tslots[id]; @@ -2607,7 +2900,6 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { slots_gen++; if (!++reg_serial) reg_serial = 1; __atomic_store_n(&sinfo[id], (uint64_t)reg_serial << 16 | agen_idx((uintptr_t)t->mem), __ATOMIC_RELEASE); - slot_rebucket(id, agen_idx((uintptr_t)t->mem)); dirty_push((uint32_t)id); s->has_view = v != NULL; if (v) s->v = *v; @@ -2623,6 +2915,7 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { for (size_t i = 0; i < sizeof s->v; i++) h = (h ^ b[i]) * 0x100000001b3ull; s->khash = (h ^ s->has_view) | 1; } + slot_place(id, agen_idx((uintptr_t)t->mem), s->khash); unlock(); } @@ -2689,6 +2982,7 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) { else if (!e && !cpu_slot) e = own && own->superseded ? NULL : own; // a superseded one: entry_of takes it back else if (!e && cpu_slot && s->finalized) stale = 1; } + resolve_stale = stale; if (!e && s->t.mem && !stale) { // evicted by disuse: the pool refresh keeps the stand-in, without spending the creation budget on it if (pool_ctx && holds.n && nvn_hm_get(&holds, &k)) resolve_held = 1; @@ -2757,7 +3051,27 @@ void vk_texture_bucket_stats(char *out, size_t n) { (unsigned long long)n_site[2], (unsigned long long)n_site[3], (unsigned long long)n_undone, (unsigned long long)hot, occ[top[0]], occ[top[1]], occ[top[2]], occ[top[3]], bump_count[top[0]], bump_count[top[1]], bump_count[top[2]], bump_count[top[3]]); - n_bumps = n_undone = 0; + size_t used = strlen(out); + if (used < n) + snprintf(out + used, n - used, "; pool freshness check: %llu entries checked, %llu passed by their regions (missed: " + "%llu not uploaded, %llu a region without a snapshot yet, %llu untracked, %llu written since, %llu " + "nothing to check; %llu snapshot queries of NX2NAVI_POOL_PRESNAP)", + (unsigned long long)n_pool_checks, (unsigned long long)n_pool_skips, + (unsigned long long)n_pool_miss[0], (unsigned long long)n_pool_miss[1], + (unsigned long long)n_pool_miss[2], (unsigned long long)n_pool_miss[3], + (unsigned long long)n_pool_miss[4], (unsigned long long)n_presnap_q); + used = strlen(out); + if (used < n) + snprintf(out + used, n - used, "; %d slot groups holding %llu slots", ngrp_live, (unsigned long long)n_grouped); + int held = 0, gown = 0; + for (int i = 0; i < npref; i++) + if (pref[i].refs && pref[i].e) held++, gown += pref[i].gown; + used = strlen(out); + if (used < n) + snprintf(out + used, n - used, "; pool slots hold %d entries, %d of them GPU-owned (stamped in each submission " + "checked by list)", held, gown); + n_bumps = n_undone = n_pool_checks = n_pool_skips = n_presnap_q = 0; + memset(n_pool_miss, 0, sizeof n_pool_miss); memset(n_site, 0, sizeof n_site); memset(bump_count, 0, sizeof bump_count); unlock(); @@ -2800,8 +3114,9 @@ const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view) { s->cheld = (uint8_t)(!s->ce && resolve_held); } Tex *e = s->ce; - // a draw binding: the texture is in use, whether or not the pool walk checked it first in this submission - if (e && !pool_ctx) e->samp_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + // a draw binding: the texture is in use, whether or not the pool's refresh checked it first in this submission + // (by list that check is no lookup, vk_texture_pool_fresh_locked, so the stamp of the use is left here too) + if (e && !pool_ctx) touch(e); // Bindings of one submit share one check, answered from the submit's write-watch snapshot (guest writes during a // replay race the GPU read on hardware too). After one check an entry is either uploaded or never uploads (not // mirrored, GPU-owned or no CPU layout), so a slot that checked its entry in this submit has nothing left to do. A @@ -2858,8 +3173,9 @@ uint32_t vk_texture_epoch(void) { return __atomic_load_n(&submit_epoch, __ATOMIC void vk_texture_new_submit(void) { lock(); - if (round_checks && (uint64_t)round_checks * 2 >= pool_slots) look_round++; // the last one walked the pool - round_checks = 0; + // the last one walked the pool, or checked its entries by list + if (pool_by_list || (round_checks && (uint64_t)round_checks * 2 >= pool_slots)) look_round++; + round_checks = 0, pool_by_list = 0; if (!++submit_epoch) submit_epoch = 1; rate_tick(); sweep_pressure();