GPU: marked pages go back to title memory on several threads (#2826) #2912
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2914"
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?
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T23:51:56Z.
Summary
Ports Machad3x/VIctory-nvidia-3080ti@
7229f41to main, per the #2826 triage invite ("PRs are open to everyone, so please open one with the commit and its test"). Authorship retained; that fork's doc page is left out.copy_back_marks' final loop runs throughfor_each_by_page: pages sorted and grouped by page index (copies covering one title page keep their order on one thread — thecopy_back_markedwrite/protection sequence stays single-writer per page), groups spread over up to six scoped threads including the submitting one.Tracked's changed/saved sets and arm bits (Mutex/atomics),watch::writing's guardsMutex,copystatcounters (AtomicU64);read_into/write_mappedtouch disjoint title ranges per group.The reporter's measurement (Ryzen 7 5700X, RTX 3080 Ti, medians, on a PR stack — #2120/#2130/#2280 — not main alone): marked pages 166→111 ms, copy-back 184→124 ms, frame 777→690 ms.
Test plan
cargo clippy -p victory-gpu --all-targets --locked -- -D warningscleanshadow::tests::many_marked_pages_all_go_back(96 marked pages over two overlapping copies) passes on GTX 1050mainalone (vs the PR stack) is still open — flagged in the issueGenerated with Devin
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T23:59:19Z.
Review: COMMENTED
Verified head
1c96dc2dcleanly onorigin/main(63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP +VICTORY_D3D12_DEBUG=1).Concurrency & Lock-Order Audit
for_each_by_page(crates/victory-gpu/src/shadow.rs:901-925) sorts by virtual page and groups viachunk_by(|a, b| a.1 == b.1), ensuring multipleShadowbuffers overlapping the same 64 KiB title page run sequentially on one worker without data races.GUARDS->Changed.0->Changed.1), host-copy slices[from - start .. to - start]are disjoint per page, andstd::thread::scopepropagates worker panics cleanly without deadlock.watch::writing,Writing::drop, andt.takenstill serializeVirtualProtectunderGUARDS, so the parallel gain comes fromhost != title,read_into,merge_writeback, and memory copy work.alias_of) and are both marked dirty via separate bufferShadows simultaneously, grouping by virtual page could process them on different threads (thoughalias_ofis primarily used for tiled texture heaps).Local Verification (
--features diagnostics,VICTORY_D3D12_DEBUG=1)shadow::+draw::tests31/31 (includingmany_marked_pages_all_go_back),watch::tests15/15.shadow::+draw::tests31/31.cargo clippy --locked -p victory-gpu --all-targets -- -D warnings(default and--features diagnostics) &cargo xtask policy(191 files ok): clean.Looks clean and ready to merge.
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T02:26:51Z.
Review: APPROVED
Tested merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090):cargo test -p victory-gpu --lib shadow::tests: 10 passed, 0 failed (including newmany_marked_pages_all_go_back).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Parallelizing marked-page copy-back by page via
for_each_by_pageis clean and verified on hardware.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T02:22:21Z.
Automated triage for the maintainers.
Merge candidate (a recommendation to look, not an approval). In
shadow.rscopy_back_marks, marked pages now go back on up to 6 scoped threads (for_each_by_page) once there are 16 or more. Pages are sorted and grouped so that every copy of one page stays on one thread, in order, and thewithheldcheck is kept. With fewer pages it stays serial. The scope joins before the function returns, so copy-back still finishes before the submission completes. The new test covers two overlapping copies of one memory. CIcheckpasses. Note that #2918 also rewrites this copy-back path (it writes only the bytes that changed), so whichever lands second will need a rebase.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by MeMako on 2026-11-26T05:56:54Z.
Independent AMD RDNA 4 / Ryzen 9 7950X verification of
#2914(1c96dc2):Setup: Windows 11 (
10.0.26200), Ryzen 9 7950X (16C/32T), AMD Radeon RX 9070 XT (driver32.0.32029.2010), local integration branch onorigin/main63bcca4with#2051+#2472+#2918+#1010+#2130(flags off) +#3055(copy_back_marksruns#2914'sfor_each_by_pageover#2918's exact-bytecopy_back_marked, whereSCRATCHisthread_local!).Checks:
cargo clippy -p victory-gpu -p victory --all-targets --locked -- -D warnings: clean.cargo test -p victory-gpu --locked: 266 passed, 0 failed (including newshadow::tests::many_marked_pages_all_go_back).Live title run (prison cell steady state, release build):
marksheavy med (avg)µs/page(heavy / all world)avg)#2914(1 thread)79.3)558.6)#2914(up to 6 threads)71.1)487.8)Notes:
-12.1 msheavy median despite +11% more pages and +32% more written bytes) with 0 crashes across the run.#1010(user-moderead_into) and#2918(SSE2 diff + plain-storestore_runs), scaling past ~15–20% is now mostly bounded by per-pagewatchguard-lock +VirtualProtectkernel serialization; onmainwithout#1010(where per-pageReadProcessMemory/WriteProcessMemorycost is higher), the relative speedup should be even larger.Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.
Imported from Marvisak/VIctory. Originally posted by moe-miau on 2026-11-26T06:17:46Z.
A measurement for this one on an NVIDIA stack (RTX 4070 SUPER, Windows 11; main + #2464 + #2098 + #2626 + #2628 + #2711 + #2851 + #2705 + #2925 + #2958, prologue cell). Medians of the 5 s reports over the last 6 minutes of a 15-minute run:
On this machine the threads did not shorten the loop. Each page takes the guard lock three times (
writing, its drop,Tracked::taken) and makes protection calls under it, so that part stays serial. #3069 removes the protection calls and the kernel write for physical-page memory (31 ms in the same scene), and the two should combine. Pictures were as before. AI-assisted (Claude Code). Runs done on my machine.Imported from Marvisak/VIctory. Originally posted by TheGrayOrange on 2026-11-26T07:22:45Z.
Tested
1c96dc2on an i7-14700KF using the actualShadow::copy_back_markedCPU path. For 2,048 marked 64 KiB pages, median execution time decreased from 46.013 ms serial to 36.619 ms with the PR's worker cap. This excludes GPU submission and bitmap collection.However, I reproduced an ordering issue with two virtual mappings of the same physical page. With the original mark order high-address then low-address:
0x11.0x22.This occurs with only two jobs, before parallel execution. Grouping by virtual address also appears insufficient to establish independence between workers when physical backing is shared.
Suggest preserving original mark order for serial batches and conservatively falling back to that path when physical backing independence cannot be established.
The temporary CPU-only reproduction is available if useful. Testing and analysis were performed with Codex assistance.
Imported from Marvisak/VIctory. Originally posted by imjustprism on 2026-11-26T15:48:25Z.
Review: CHANGES_REQUESTED
tested
1c96dc2in the title and read it againstwatch.rs. the gain is real on main, and there is one case where it loses shader writes, so i am asking for a change before this goes in.the measurement on main alone that the description lists as open, rx 9070 xt, world, two runs each way:
63bcca4plus #2122 (this card needs it) with a timer aroundcopy_back_marks, with and without this pr merged in. diag build, series-x, the same scripted key presses, warm shader and pipeline caches, quiet machine, 12-minute runs, medians over 47 to 74 world frame lines. no errors, no device removed in any run. about 20% less per page, the same in both runs. the frame as a whole did not move outside run-to-run spread. the code comment's 55 to 80 microseconds per page and 2,000 pages a frame are 18.5 and 1,500 to 1,800 here.what needs changing: two mappings of one physical page.
for_each_by_pagegroups by page address, so the two addresses of one physical page (watch::title_memory_aliased, linked fromXMemMapPhysicalPages) are two groups and go back on two threads at once. in the serial loop the first copy'swatch::writingsaves the alias page for the second copy before its own write lands (changed->mark_changed(.., true)->Changed::save), and the second copy merges onto it, so both copies' bytes reach title memory. on threads the second copy can already have rununarm,saved(none) andchanged(false) atshadow.rs:501-507; then the first copy'swritingsaves nothing for it, and the second writes its whole host page over the first one's bytes atshadow.rs:518-525, or drops its own. so the description's "read_into/write_mapped touch disjoint title ranges per group" does not hold for aliased pages.a test that shows it (below): one section mapped at two addresses 32 times over, a copy of each mapping, both armed and marked, a byte written through each, 200 rounds.
63bcca4plus #2122)this is the threaded side of what @TheGrayOrange reported for the sorted order. i have not seen the title mark both mappings of a page in one submission, and it does map physical pages itself, so i cannot rule it out. keeping every mapping of a physical page in one group (the links are in
Guards::alias_of), or sending pages that have an alias back on the submitting thread, would settle it; the test then passes.smaller things in the same code:
shadow.rs:135-136andshadow.rs:509: both// SAFETY:lines say the copy's data is written only on the submitting thread. the workers write it now. it is still sound (one thread per page of a copy, and the scope joins before anything reads), but the comments should say that.shadow.rs:921:scope.spawnpanics when the thread cannot start, and this runs underExecuteCommandLists, so that would abort the process.Builder::spawn_scopedwith the work left to the caller on error avoids it.texture::in_parallelhas the same pattern.victory-loader/src/threads.rs:240), which runs the title's own thread-attach and thread-exit code. workers that stay alive, as #2051'scopy_poolhas, would avoid that; it may also be why the per-page gain stops at 20%.shadow.rs:903: the thread count comes from the (copy, page) pairs, the work is handed out by page group. 48 pairs on two pages start five threads and four find nothing.many_marked_pages_all_go_backmarks every page through one copy only, so the per-page order the grouping exists for is not exercised: the test passes on main too, and would pass without the grouping.the test (goes into the tests module of shadow.rs)
Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T07:18:54Z.
The main-alone measurement the #2826 triage asked for — synthetic, so it isolates
copy_back_markswithout a title run. It confirms the change and, on this machine, it also shows the absolute size of the win is much smaller than the 184 → 124 ms reported from a PR stack.Method. Windows 11 x64, Intel Arc B580, release build (
cargo test --release -p victory-gpu --lib). A pool of N 64 KiB pages is registered as a shadow, every page is marked through the real mark bitmap (bitmap(false), the bitscopy_back_markedreads), 64 bytes per page differ in the host copy, thencopy_back_marks(&[], false)is timed, best of 3, bits re-marked between runs. Two builds:63bcca4, and63bcca4with this PR'sshadow.rsdiff (1c96dc2) applied to that file alone.So at 2,048 pages the change saves about 12 ms per submission here, and the scaling is what the design predicts: under 512 pages there is less to spread, at 2,048 the six threads hold up.
The caveat worth having on the record. A page costs 9.5 µs on main here, against the 55–80 µs #2826 quotes. Two things differ: this box's memory and the merge, which upstream already vectorizes (
merge_writebackruns at ~7.7 GB/s), so a large part of what #2826's stack paid per page is already gone inmain. That is why the same 2,000-page frame gives ~12 ms here and 60 ms there — both numbers are right, for their machines. It also means the parallel win will shrink further if #2280's cheaper per-page copy-back lands.Nothing about correctness changed in the measurement: marks cleared, bytes merged, no leftovers. Synthetic pages only, no game data, no title run.
If a number from a real frame on this card is wanted too, I can run
VICTORY_*A/B on the world scene — but that needs the title, which I cannot run here.Imported from Marvisak/VIctory. Originally posted by ZeOs360 on 2026-11-27T14:10:11Z.
A gameplay measurement of
1c96dc2, and a fix for the two-mappings case imjustprism and TheGrayOrange raised, since it does come up in this title.Setup. RX 7800 XT 16 GB, Ryzen 5 5600, 16 GB RAM, Windows 11, diagnostics build. Base:
main63bcca4+ #2875 (VICTORY_LOOSE_IN_GPU_UPLOAD=1) + #2821 + local changes that keep GPU-written loose windows in system memory (without them the frame is dominated by CPU reads of write-combined memory, and the marked step is hard to see). Prison cell after the prologue, 15-minute runs, medians of the 5 s reports from minute 4 on (111 to 116 reports each).So here the threads cut the marked step by about 2.5x, more than the ~20% per page in imjustprism's
mainruns (~18.5 µs a page) or the 15-19% on MeMako's stack: this base's per-page cost is higher (~85 µs a page).The two-mappings case happens in this title, a little. With a counter in the grouping, in a 15-minute run one physical page had both of its mappings marked in the same copy-back in nearly every frame (every report from minute 4 on, 117 of 117, and 138 of 151 over the whole run; about one such pair per frame, out of ~2,800 marked pages).
for_each_by_pagegroups by virtual page, so the two addresses of one physical page are separate groups, can run on two threads at once, and the serial path's address sort also changes their order (TheGrayOrange's0x11/0x22case). I did not see corrupted output in a 15-minute run, but the window is there every frame.A fix, on top of
1c96dc2:github.com/ZeOs360/VIctory@be1014d(+79/−5,shadow.rsandwatch.rs):watch::alias_roots(pages): for each page, the lowest page among it and itsalias_oflinks, all under oneGUARDSlock.for_each_by_page: the serial path keeps mark order (no sort). The threaded path sorts by alias root with a stable sort, so every mapping of a physical page is in one group, in mark order, on one thread.aliased_pages_go_back_on_one_thread_in_mark_order: 16 pages of a section mapped twice (twin_views), the higher mapping first in the list; it checks each page's two copies run on one thread in mark order. Withalias_rootsreturning each page itself (the current grouping), it fails on page 0, 3 of 3 runs.marked_pages_of_aliased_mappings_both_go_backfrom the review above (not in the commit): on1c96dc2it fails, 200 of 200 rounds losing a byte (2 of 2 runs); with the commit it passes (3 of 3 runs).1c96dc2with the commit:cargo test -p victory-gpu233 passed (3 runs),cargo clippy --workspace --all-targetsclean with and without--all-features. In the title it adds about 4 ms to the marked step (about 1.4 ms of it inalias_rootsunderGUARDS), within the run-to-run spread of the frame (third column).@kotekompotek feel free to take the commit into this PR, or tell me if you would rather I open it separately.
AI-assisted: written with Claude Code (Claude Opus 5.5) and reviewed by Gemini 4 Neon. I read the code and the reviews, and ran the tests and the game runs on my machine.
Imported from Marvisak/VIctory. Originally posted by pes7 on 2026-11-27T16:37:44Z.
A heads-up from the tested merge set in #3409. This PR still merges cleanly onto
main(63bcca4), so there is nothing to rebase now. It does conflict with #2437 incrates/victory-gpu/src/shadow.rs.Whichever of the two lands second will need a rebase onto the other. #2437 is in #3409's set, so this PR is left out of it for now. If you push a version on top of #2437 (or say which order you'd prefer), I'll run it through the same build, test and title checks there.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T20:28:58Z.
I tested the parallel write-back in this PR with @ZeOs360's alias-order commit
be1014dand the scratch-buffer reuse from #3604. Tested pair keeps both original commits and resolves the conflict by giving each worker its own reusable buffer.On Windows WARP, all writes in the 96-page fixture reached the expected locations. All six workers participated and each allocated its full-page buffer once. In the 16 dual-mapping cases, both visits stayed on one worker in mark order. The clipped cross-page shader fixture passed too.
The diagnostics GPU suite passed 248 project tests plus the SDK identity check, with one ignored. Strict GPU Clippy passed in both feature configurations, and the asset policy check passed.
The results are from the synthetic fixtures. @kotekompotek and @ZeOs360 can use the integration when reviewing the alias fix. Please retain the original Git authors if you cherry-pick or rebase. If you fold my conflict resolution into another commit, please include
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.