perf(vk): a texture the game's heap compaction moved keeps its image (RX_VK_TEX_MOVE, off by default) #1810
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
6 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1810
Loading…
Reference in a new issue
No description provided.
Delete branch "GummiNaomi/Longshot:perf/tex-move-keeps-image"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The game tidies its texture memory by moving textures (a heap compaction, at most one round a second since #1478). Every move reached the renderer as a brand-new texture, made, hashed and uploaded again, twice per move. With
RX_VK_TEX_MOVE=1the renderer recognises a move and keeps the texture's image under its new address. It does that only once the texture's first draw has compared every byte of the new storage with the old one; then nothing is made or uploaded. A move it cannot prove is made as before.The switch stays off by default. In Castle Town's market, turning it on measured slower in one session (+0.05 ms a frame on 16 cores, +0.14 ms on 4) and faster in the next (-0.06 ms and -0.14 ms). Either way the effect was smaller than this PC's drift from run to run, so I found no gain to ship it on for. The first-draw compare is what the review asked for, and it catches real cases: in a six-area tour it refused 15 moves whose new storage no longer matched, and 2 or 3 in each of two market stands. The first version of this PR would have kept the old picture for those.
Why a move looked like a new texture: the game keeps every texture in one 2.4 GB heap. A compaction round copies a texture's storage to a work heap, then to its new place. After each copy it makes the same NVN texture again over the copy at the same slot (Finalize, Initialize of the same object, RegisterTexture at the same id; the game's f_807981f0). The renderer keys images by storage address.
Read first:
gpu/vk/tex_move.h(new): what makes a pair a move. It must be the same object finalized and made again back to back on one thread, with the same description, layout, pool and size, both storages in the game's own memory and apart. The bytes must match at 34 sampled places; that is only a filter that turns pairs away early. The header also holds the queue of noted moves,storages_equal(the whole compare, in 1 MB pieces) andjudge(the first draw's verdict).gpu/vk/vk_texture.cpp:move_take, the first draw. The image must still be there, not uploaded since, not drawn into, not sparse, and its old storage not written since its upload. Then every byte of the new storage, as an upload would read it at that draw (guest_bytes: guest memory, or the pages the capture kept as submitted), must equal the old storage's. The image's upload generation is the one read before that compare, so a write the compare did not see is uploaded at a later draw.texture_move_*note the move on the game's thread and confirm it at the registration;move_logwrites the counts every 600 presents.gpu/vk/vk_replay.cpp: the hooks (texture_released_of/texture_created_ofon the game's thread; the registration job, in order with the submits).host/nvn_native.c: with the switch on, a texture's packaged data that is its own storage is not copied onto itself. The switch is read through atomics, because the first textures are made on several threads at once.gpu/vk/switches.def: theRX_VK_TEX_MOVErow (attempt, off;=2also compares whole storages on the game's thread, as a check of the samples).tests/test_tex_move.py/tests/test_tex_move.cpp(new). They cover the rules and the compare in pieces, and a move's whole life in simulated guest memory: a write into the new storage between Initialize and the first draw, and a difference between the sampled places, both refused. Source checks cover the hooks, the compare, the generation read before it, the host's atomic read and both defaults.See #1478, #1608
Testing
Tested: Windows 11 Pro 26200, RTX 4070 SUPER (NVIDIA 617.42), Ryzen 9 9950X3D (SMT off), headless.
=1on 4 cores, two runs each; main and=1on 16 cores, one run each. 0 error lines in every log.HOS_TEX_COMPACT_MS=0),=2against=0. The pictures look the same; the differences (0.5-2.3 % of pixels) are Navi, Link's pose and the insects. In the=2tour the game's thread compared every byte of all 78,413 noted moves and the samples missed nothing; at the first draw 15 of them no longer matched and were made as before.Checks:
tests/test_tex_move.pyandtests/test_switch_table.pypass on Windows and in WSL (Ubuntu 24.04, g++).run_all.py --changedsince65488813), 111 pass on Windows. 18 fail, and all 18 fail on65488813too: test_crash_dialog, test_dev_ab, test_dev_packs, test_early_split_before_draw, test_frame_overlap, test_hle_guest_ranges, test_hold_gpu, test_host_guest_writes, test_linux_pressure, test_profiling_mode, test_readback_early, test_readback_late, test_readback_timeout_source, test_slow_frames, test_split_before_draw, test_upscaler_rows, test_walk_key, test_wrote_regions.65488813too.judge, four cases fail (the destination writes, the unsampled byte, the verdict). The new source checks stop at e841323c'smove_takeandnvnTextureInitialize.Still to test:
RX_VK_TEX_MOVE=1and unset, expecting no wrong textures, and whether=1has fewer long frames there (@Beanstalk steamdeck, Castle Town's market 2 minutes with RX_VK_TEX_MOVE=1 against unset)RX_VK_TEX_MOVE=2, expecting the same pictures and the log'sother bytes nowcount shown (@Beanstalk amd 8gb, the market with RX_VK_TEX_MOVE=1 against unset, and a six-area tour with RX_VK_TEX_MOVE=2)RX_VK_TEX_MOVE=1(@Beanstalk linux, Castle Town's market 2 minutes with RX_VK_TEX_MOVE=1 against unset)RX_VK_TEX_MOVE=1, several runs each, all cores and with the game held to 4 cores, expecting this PR unset to match main and showing whether=1gains anything (@Beanstalk windows 12gb, Castle Town's market 2 minutes, main against this PR unset and with RX_VK_TEX_MOVE=1, all cores and 4 cores)HOS_TEX_COMPACT_MS=0), main against this PR withRX_VK_TEX_MOVE=1, expecting fewer long frames with it and the log'sother bytes nowcount shown (@Beanstalk windows, Castle Town's market 2 minutes with HOS_TEX_COMPACT_MS=0, main against this PR with RX_VK_TEX_MOVE=1)Measurements
Rebased: this head,
4b7f1fed, iscd5f4bf4rebased onto main89c3d21ewith no conflicts. The code is the same: in these files main changed onlygpu/vk/switches.def(other rows) and added #1158's three-lineframegen_idlecall indo_present, away from this PR's hooks. The numbers below are fromcd5f4bf4on65488813. At4b7f1fed,tests/test_tex_move.pyandtests/test_switch_table.pypass on Windows (clang).Verdict: no gain I can show, so
RX_VK_TEX_MOVEstays off. Against main on 4 cores,=1measured -0.32 ms a frame and frames over 16.7 ms went 2.44 → 1.15 %. But this PR with the switch unset, where none of the new code runs past one check per texture, also measured -0.18 ms against main, which is drift (main ran first and last). The earlier session on the same code measured=1+0.14 ms slower than off on 4 cores and +0.05 ms on 16. Pooled over both sessions' four 4-core off/on pairs, the mean frame is 13.16 ms on both sides.Setup: Windows 11 Pro 26200, RTX 4070 SUPER 12 GB (NVIDIA 617.42), Ryzen 9 9950X3D (SMT off), 32 GB. Castle Town's market (child), standing 130 s after a debug teleport from the
kokiri-startsave. Output 2560x1440, uncapped (HOS_FPS_MAX=1000), no window, otherwise the game's defaults. "4 cores": the game held to cores 8-11 (affinity 0xF00); "16 cores": no affinity.RX_VK_FRAME_TRACE=1, the trace's last 100 s: fps, frames of 16.7 ms or more and of 33 ms or more, p99, the median. Δ is the difference of the mean frame time, with a 95 % bootstrap interval over 3 s blocks. On 4 cores two runs a side, interleaved (main,=1, unset, unset,=1, main), the builds swapped between runs; on 16 cores one run each of main and=1(the round was stopped there to cap the runs).65488813cd5f4bf4cd5f4bf4=1against main (95 %)The earlier session, on the same code with its default on (85919ba7, build tm20;
RX_VK_TEX_MOVE=0against unset, ABBA, two runs a side):The PR's first version measured a gain, but it compared samples only and had no first-draw compare. That was on main @
f505340b, withRX_VK_ASTC_BC_ALL=1 RX_VK_IMAGE_BLOCK_MB=32 RX_VK_LOAD=create RX_VK_BC_LAYERS=8 RX_VK_LATE_FIX=0, one run a side on 4 cores. Frames over 16.7 ms went 2.6 → 1.4 % (73.4 → 75.3 fps), and 20.0 → 7.5 % with compaction every frame. That gain does not reproduce at this head with the game's defaults. Whether the cost is the whole compare or main's changes since is not isolated.RX_VK_TEX_MOVEships off (attempt). Turned on, it was slower in one session and faster in the other, by less than this PC's drift between runs, so neither shows a gain.Not measured: this PR unset on 16 cores, a second 16-core pair, compaction every frame on 4 cores at this head, which part of the cost is the whole compare, and any other GPU, CPU or OS.
Risks
RX_VK_TEX_MOVEunset, or revert.longshot_options.inior the caches.Checklist
Ready for review
main; the title is a Conventional Commit. (Rebased onto main89c3d21eas4b7f1fed; it still merges cleanly with #1813.)mise run testpasses (any failure that also fails onmainis named). (The checks that read the changed files, on Windows and Linux; every failure is named in Testing and also fails on65488813.)main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here. (Fails on this PR's first version,e841323c, where the review found the bugs.)gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Possibly related, for context:
Recommendation: Request changes. Reusing a moved texture can hide a later destination write. The opt-in fast path also treats sampled bytes as proof of equality.
Does it help: Unproven for the submitted revision. GummiNaomi's earlier Windows RTX 4070 SUPER A/B reports 73.4 to 75.3 FPS and 2.6% to 1.4% of frames over 16.7 ms with four cores, but no clear benefit with sixteen. The earlier full-compare tour reported 143,391 moves with zero mismatches. This is encouraging coverage for that route and build. The exact head has focused checks but no gameplay run, and the comparison does not establish safety after a destination write.
Other systems: The path changes common host/replay texture handling. Recheck Linux, Deck, AMD and lower-VRAM hardware after correctness is fixed.
Switches: RX_VK_TEX_MOVE defaults off. Mode 1 uses sampling; mode 2 compares the whole storage at initialization.
Reviewed:
e9799a31against0de25c3e, with exact Graphify maps and source. Read the host initialize hook, renderer registration and cache paths, and the source-backed fixture. Nothing run.Fixed in this review: Nothing.
Checked and fine: The new hint table is bounded. Registration checks the old slot serial; first use checks image ID and upload generation.
Blocking
gpu/vk/vk_texture.cpp:2855,2884: after Initialize compares the storages, a CPU write to the destination before first use can leave the old image stale. The reuse check reads CPU dirtiness only on the source, then stamps the moved image with the current generation at its new address. A destination write already included in that generation can go unseen by the normal upload check. Record the destination's validated generation and reject intervening writes, or compare against the actual replay-visible bytes at consumption. Test a destination-only update between Initialize and first draw.gpu/vk/vk_texture.cpp:2922: for a large texture, a changed block between samples can still pass mode 1 if the object, layout and slot are reused. The sample match should only be a rejection filter before an exact comparison or a proven copy-identity check. Add a fixture with a difference outside the sampled regions.host/nvn_native.c:782–783: the new writable static can be initialized concurrently on the first texture creations, even when the switch is unset. Use thread-safe initialization or precompute it before guest workers start.Testing: Rebase, bring the description/checklist up to date, and validate exact-head gameplay and the current default. CI's description and Guide screenshots checks are red; the source jobs listed as skipped provide no coverage.
Overlaps and merge order: Texture-ahead at #1813 shares the queue and cache path. The changes should be tested together after their individual correctness gates. #1811 and #1812 touch the same host/replay files and need rebase reconciliation. The banking and virtual-core changes are shared-route performance variables, not prerequisites for this texture fix.
@ -2819,0 +2881,4 @@src->gpu = t->gpu, src->guest = host->gpu_guest(t->gpu);// (its contents are its new storage's as of now: the game's copy into it was written before this draw's submit, and is in the// page generations; what the CPU writes there later is uploaded as for any texture)src->upload_gen = memory_generation();This stamps later CPU writes to the destination as already uploaded. Keep a destination validation generation and reject newer writes before reusing the image.
@ -2819,0 +2919,4 @@const uint8_t* p = host->gpu_ptr(mv_fin.gpu, &a);const uint8_t* q = host->gpu_ptr(t->gpu, &b);if (!p || !q || a < n || b < n) return void(mv.unreadable++);bool same = tex_move::samples_match(p, q, n);An unsampled changed block can pass mode 1. Use an exact equality or copy identity check before accepting reuse.
@ -780,0 +780,4 @@over its copy so (f_807981f0: SetPackagedTextureData(builder, the new storage)). The bytes are there already: copying themonto themselves cost a memcpy of the whole texture on the game's thread and marked every page written for the memory sync */static int same_skip = -1;if (same_skip < 0) same_skip = getenv("RX_VK_TEX_MOVE") && atoi(getenv("RX_VK_TEX_MOVE")) > 0;Concurrent first texture initialization can race on this writable static even with the option unset. Initialize it before workers or use a thread-safe once operation.
Let's beanstalk and try enable for all? Let's test on/off and see if we should just enable by default
@Beanstalk
Beanstalk test farm · an earlier run: commit
cd5f4bf(the newer results are in a comment below)Commit
cd5f4bf· tier reviewed · approved by GummiNaomi · dashboardWhat will be tested · AI plan (
gpt-6.1-terra)On Steam Deck, Linux, and Windows PCs with 8 GB AMD and 12 GB NVIDIA GPUs, the plan teleports from kokiri-start to the child Market and stands for two minutes. It compares main with the PR’s default-equivalent off mode and opt-in mode, recording frame times, hitches, screenshots, and the first-draw rejection count; crashes, substantial performance regressions, or large image differences fail. The requested GPU-size combinations may wait for matching online testers. Each tester needs about 43 minutes; per-frame compaction is omitted to stay within the run-time limit, and frame tracing is not permitted by the farm.
0or1(PR only).kokiri-start, then teleport to Market, measure market for 2 min (after 0 s warm-up), take the picture market-after-compaction to compare with main; 1 run on each side for each of 2 cells (2 game starts a run).Started at 13:15 UTC.
Estimate: done in about 15 h 30 min, from the queue and the testers online now; some jobs can't start yet.
RX_VK_TEX_MOVE=0 (PR only)RX_VK_TEX_MOVE=0 (PR only)RX_VK_TEX_MOVE=0 (PR only)RX_VK_TEX_MOVE=0 (PR only)RX_VK_TEX_MOVE=1 (PR only)RX_VK_TEX_MOVE=1 (PR only)RX_VK_TEX_MOVE=1 (PR only)RX_VK_TEX_MOVE=1 (PR only)e9799a315a7b30e1762c7b30e1762ce841323c89Tested #1810 at
e9799a31plus two fixes, on main068fb8ebwith a local stand-in for #1815 in both arms. Steam Deck OLED, 1280×800, fullscreen Game Mode, HDR on, GPU held at peak clocks as main does on Deck.Threaded rendering defaults off on Deck. The first fix confirms moves on the synchronous path. The second keeps an image only if the new storage still holds its original bytes, including after chained moves. Links on
deck-1810-fixes, rebased ontoe841323c:Standing in the Market, I alternated
RX_VK_TEX_MOVE=0and1; #1814 and #1780 stayed off. Two pairs, 60 s per run. Medians, off → on: 21.6 → 21.95 fps (+1.6%), 1% low 15.35 → 17.25 fps, p99 65 → 58 ms. Frames over 50 ms went from 12.3% to 10.0%, but only one pair improved on that measure.The two on runs kept 476 images (131 MB) and 266 (85 MB). With all three on, Market standing kept 176–556 images (44–161 MB). The newer round-2 combo kept 420 and 488.
Moving, the Field walk was main → all three on, not #1810 alone: 21.85 → 22.7 fps (+3.9%), 1% low 12.45 → 14.7 fps, p99 80 → 68 ms, frames over 50 ms 16.3% → 8.55%. The combo kept 556 images (257 MB) and 466 (383 MB). Two pairs are a first check.
Thanks, all three were real; fixed in
cd5f4bf4(one commit on top ofe841323c, a fast-forward).1. A destination write between Initialize and the first draw (
gpu/vk/vk_texture.cpp,move_take;gpu/vk/tex_move.h,storages_equalandjudge). The first draw no longer trusts what the game's thread saw. When every other check holds, it compares every byte of the new storage, as an upload there would read it then (guest_bytes: guest memory, or the pages the capture kept as submitted), with the old storage's, which nothing has written since the image's upload. That compare runs in 1 MB pieces. The image's upload generation is now the one read before that compare (const uint64_t gen = memory_generation()ahead ofjudge, thensrc->upload_gen = gen), so a write the compare did not see is stamped later and uploaded at a later draw. A difference is counted (other bytes nowin the log line) and the texture is made as before. This happens in real play. In a six-area tour on this code (85919ba7, which differs fromcd5f4bf4only in the unset default) withRX_VK_TEX_MOVE=2andHOS_TEX_COMPACT_MS=0, the game's thread compared every byte of 78,413 moves at Initialize and found none different. At the first draw, 15 of them differed. In plain 16-core market stands, 2 of 1,490 differed (85919ba7) and 3 of 988 (cd5f4bf4).e841323cwould have kept the old image for those.Test:
tests/test_tex_move.cppsimulates a move's whole life in guest memory (the game's copy, Initialize, the registration, the first draw). One case is a one-byte write into the new storage after Initialize, another 64 KB cleared there; the old storage's page generations say nothing in both. Both must be refused (Verdict::differs).tests/test_tex_move.pychecks thatmove_takecompares throughstorages_equal/guest_bytesand reads the generation before the compare and not again after it.2. Mode 1's samples treated as proof (
tex_move.h). The samples are now only a filter on the game's thread, so a pair whose samples differ is turned away early. Every move that would be taken, in mode 1 or 2, goes through the whole compare above at its first draw.=2still also compares whole storages on the game's thread, as a check of the filter.Test: a fixture whose one differing byte lies half way between two sampled pieces. The test asserts that
samples_matchpasses it (the filter's known gap) and that the first draw's verdict isdiffers. There are also cases for the compare in pieces: the last short piece, the first byte of the second piece, unreadable, and two read buffers.3. The host's writable static (
host/nvn_native.c,nvnTextureInitialize).same_skipis now read and written only through__atomic_load_n/__atomic_store_n, so the first texture creations racing on several threads are fine, the switch set or not.tests/test_tex_move.pyfails if any line there touchessame_skipwithout an atomic.Each new case fails on e841323c's code and passes now. I checked that by compiling the new test with the compare line taken out of
judge(4 FAILs: the differing verdict, the two destination writes, the unsampled byte) and by running the new source checks against e841323c'svk_texture.cppandnvn_native.c(they stop at the missing compare and at the missing atomic).Default: kept off. HumanBean asked whether to enable it for everyone, so I tested that. The measurements show no gain to ship it on for. Castle Town's market, standing 130 s (the last 100 s of the frame trace), the game's defaults, 2560x1440, uncapped, RTX 4070 SUPER and 9950X3D:
65488813, 4 cores (affinity 0xF00), two runs each, interleaved:=1measured -0.315 ms a frame (95 %: -0.404 to -0.227), with frames ≥16.7 ms going 2.44 → 1.15 % and none ≥33 ms. But this PR unset also measured -0.178 ms against main, and with the switch off none of the new code runs past one check per texture. That is drift (main ran first and last), and=1against unset is -0.137 ms.=1: -0.055 ms; frames ≥16.7 ms 0.03 % on both, none ≥33 ms.=1against=0was +0.136 ms on 4 cores (frames ≥16.7 ms 1.09 → 1.53 %) and +0.049 ms on 16.So the sign flips between sessions, and the size of the effect is below this PC's drift between runs. Pooled over all four 4-core off/on pairs, the mean frame is 13.16 ms on both sides. The 4-core gain I reported on
f505340b(2.6 → 1.4 % of frames ≥16.7 ms) does not reproduce at this head with the game's defaults. I have not isolated whether the whole compare costs what the moves save. It stays an opt-inattempt.Not measured, and in Still to test for the Test Farm: this PR unset on 16 cores, more 16-core pairs, compaction every frame at this head, the Deck, AMD, 8 GB cards and Linux. I stopped the local round there to keep testing to what's needed.
Tests: on Windows, 111 of the 130 checks that read the changed files pass and 18 fail; on Linux (WSL, g++), 168 pass and 31 fail. Every one of those failures also fails on the base
65488813. The branch merges cleanly with main89c3d21eand with #1813 (415777aa); #1813's pending-move skip intexture_ahead_runis untouched.@Beanstalk steamdeck, windows (an 8 GB AMD card if there is one, and a 12 GB NVIDIA one) and linux: Castle Town's market (child), standing 2 minutes after a teleport from kokiri-start,
RX_VK_FRAME_TRACE=1: main against this PR unset and this PR withRX_VK_TEX_MOVE=1, and on one Windows PC also with the game compacting every frame (HOS_TEX_COMPACT_MS=0). Expecting this PR unset to match main, no wrong textures with=1, and the log'sat the first draw ... other bytes nowline; the question is whether=1has fewer long frames anywhere, since this PC showed no gain and it ships off.(The farm's runs on #1846 and #1847 today ended in
errorbefore building: its checkout of main failedgit rev-parse --verify HEAD^{commit}in packs.py's cmd_base.)cd5f4bf4e54b7f1fed61Beanstalk test farm
Commit
4b7f1fe· approved by nobody yet · dashboardA member of
BigBeanscan review this commit and comment@Beanstalkto test it.@GummiNaomi before the Steam Deck farm run:
4b7f1fedstill hasregister_texture_queuedreturning straight toregister_texture_lockedwhen!async_on(). On the Deck threaded rendering is Auto-off (no mirrors), so registrations never go through the render thread's job and no move is ever confirmed. In our Deck runs ate9799a31without a fix,RX_VK_TEX_MOVE=1kept 0 images. So the farm's Deck cell would compare main with main.The fix is the first commit linked in my results comment above, aee1e361: with no render thread it reads the slot, registers and confirms under
replay_mu, as the job does. Your new whole-storage compare at the first draw covers what our second commit did, so only that first one is needed.Closing: no gain I can show. Pooled over both sessions' 4-core off/on pairs the mean frame is 13.16 ms on both sides, and the one session where
RX_VK_TEX_MOVE=1looked faster had the same drift with the switch unset. If a later profile shows moved textures costing arrivals, this can come back.Pull request closed