GPU: a texture read knows its memory has no writer without the texture map's lock #3272
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3274"
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 KainanP on 2026-11-26T15:26:28Z.
Part of #2586 (the cost of list methods on the title's threads). Local change, same behaviour.
The problem
Every texture a draw reads through a descriptor table goes through
draw::readable, which callsresource::alias_sourceto ask whether a title texture the GPU writes shares the texture's memory. Each call took the globalTEXTURES_ATmutex. Title games record lists on about five threads at once, and all of them queue on that one lock.I profiled the list bench's draw sequence (root CBV, four root constants, index buffer, an indexed draw whose table reads six textures) on five threads, with a temporary per-call-site lock counter. The tree was main + #1978 + #2821 + #3258, the combination recommended on #2586. Ranked by how much more each lock costs on five threads than on one:
TEXTURES_AT(alias_source)descriptor::VIEWS, shared (view_at)Resource::host(each texture's own mutex)shadow::BUFFERS, shared (shadow::find)deferred,states,state,hosts)(The counter adds its own cost, so use these numbers for ranking only.
HostHeap::madeis not taken in this sequence because its slots are already made.)The change
Most textures share their memory with no texture the GPU writes. Each title texture now keeps that answer (
no_writer_seen) together with the change count of its memory's bucket. There are 1,024 counters, hashed by page number and bumped under the map's lock whenever a title texture is made at that memory or released from it. While the count hasn't changed,alias_sourcereturnsNonewithout taking the lock. Readers only read the counters, and only make/release writes them.Only the "no writer" answer is kept. A memory that has a writer is looked up as before, because whether two host copies differ can change without the map changing. The fallback path is main's code, now reusing the map lookup.
Tests
New:
resource::tests::a_texture_read_finds_a_writer_made_at_its_memory_later. It reads a BC1 texture alone at its memory twice (so the answer is kept), then places an R32G32_UINT unordered-access texture at the same memory (same layout, its own host texture). The BC1 read must find the writer, and must find none again once the writer is released. I checked that the test catches the bug it's meant for: with the bump on creation removed, it fails at "the writer made since".That test passes on main too, as oni-swr found. It guards behaviour, not the change.
resource::tests::a_texture_read_with_no_writer_does_not_wait_for_the_texture_map(commit6964f115) shows the change itself. A texture found once to have no writer is read again on another thread while the test thread holdsTEXTURES_ATexclusively. With this PR the read returns within 500 ms. On main it blocks until the lock is dropped, so the test fails on main (RTX and WARP) and passes here.--features diagnostics,VICTORY_D3D12_WARP=1)63bcca4The RTX failures are main's (
dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views, the NVIDIA heap cap).memory_tests::heap_direct_texture_reads_mark_their_heap_indexis skipped on NVIDIA because it access-violates on main too. Intel also failsa_texture_written_in_one_format_reads_in_another_through_a_placed_viewon main. One main run on the RTX also failedviews_bound_while_another_slot_gets_its_texture_do_not_wait(timing); it passed on the other main run and on every branch run. Clippy passes with-D warningsfor the workspace, all targets, with and without--all-features.repository_passes_policypasses, and rustfmt is clean on the changed lines.Measured
Method:
pipeline::list_bench(#[ignore]d, fork branchperf/list-call-bench, not part of this PR). Its draw sequence is recorded through the list's method table on a host device, on 1 thread and on 5 threads at once. Each thread records its own list, and all of them read the same six textures. A run takes the best of 5 × 20,000 draws per thread.VICTORY_LIST_BENCH_SCALING=1runs only these rows. Each build's test binary was run 7 times, interleaved round-robin with the other builds, and the table gives the median (with the range). Hardware: RTX 4060 Laptop GPU, Core i7-14650HX, Windows 11, release profile. The machine was not idle: seven single-threaded CPU-bound processes I don't control ran throughout. Interleaving gives every build the same load, but absolute numbers sit a little above earlier runs (main's one-thread draw was 2.78 µs on #2586, 3.14 µs here).ns per draw, per thread:
A repeat on the last three trees (7 more interleaved runs) gave 4818 → 2803 → 2691 ns on five threads.
On main the one-thread draw also gets 0.8 µs faster. Main's
alias_sourceworks out the texture's layout (a mip chain) on every call before it checks for writers, and #1978 reorders that. Here the check never gets that far for a texture with no writer.Conflicts
The text conflicts with #1978, #3245, #3213 and #1969, all edits next to
alias_sourceor theResourcefields. With #1978: keep #1978'slet mut written = …chain and start it fromothers?instead ofmap.get(&(b.data() as u64))?. That's what the measured tree uses.What this does not show
AI use: Claude Code (Claude Opus 5.5) profiled the locks, wrote the change and the test, and ran the benchmarks and test suites listed above.
🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:50:44Z.
Review: APPROVED
Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):
no_writer_seenbucket generation inResourceagainst theAcquire/Releasebucketed counterTEXTURES_AT_CHANGES(bumped underTEXTURES_ATon insert/remove) cleanly avoidsTEXTURES_ATmutex contention on hot texture reads while staying exact when writers appear or are released.cargo test -p victory-gpu --lib resource:: -- --test-threads=1(RTX 5090): 19 passed, 0 failed (includinga_texture_read_finds_a_writer_made_at_its_memory_later).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T15:44:45Z.
WARP verification report (test report only, no code changes)
e718f18863bcca43(control); WARP software adapter forced withVICTORY_WARP=1 VICTORY_D3D12_WARP=1,--features diagnostics, d3d10warp.dll 10.0.26100.9818; dxil.dll 1.8.2502.11 (Windows Kit 10.0.26100.0, viaVICTORY_DXIL).--exact),-j 4, synthetic tests only, no game data.resource::tests::a_texture_read_finds_a_writer_made_at_its_memory_later: passes on the PR (1 passed).victory-gpulib suite: PR 247 passed, 0 failed, 1 ignored; main 246 passed, 0 failed, 1 ignored. No failures on either.AI use: Claude Code (Claude Sonnet 5.5)
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T13:08:53Z.
Thanks @oni-swr. You were right:
a_texture_read_finds_a_writer_made_at_its_memory_laterguards the cache's behaviour, but it passes on main too, so it doesn't show the change.New commit
6964f115addsresource::tests::a_texture_read_with_no_writer_does_not_wait_for_the_texture_map. A placed texture's firstalias_sourcefinds no writer. Then the test thread holdsTEXTURES_ATexclusively, the way a title thread making or releasing a texture does, and another thread reads the texture again. With this PR that read returns within 500 ms. On main it blocks until the lock is dropped. Another test bumping the same memory bucket in between could make the PR path take the lock, so the test tries 3 times.63bcca43+ only this test):the read waited for the texture map's lockon RTX and WARP.--features diagnostics,VICTORY_D3D12_WARP=1).victory-gpu --lib: WARP 248 passed / 0 failed / 1 ignored. RTX has the same 2 failures as main (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views), withheap_direct_texture_reads_mark_their_heap_indexskipped.-D warnings(default and--all-features),cargo xtask policyand rustfmt on the new lines are clean.The earlier behaviour test stays.
AI use: Claude Code (Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-27T23:32:28Z.
I checked the open question about five recording threads reading different textures. With six private SRVs per thread (30 textures in one heap), #3274 finished the same batch sooner in 12/12 paired runs.
63bcca436964f115These are per-build median wall times from the first thread starting to the last finishing. Calculated within each pair, the disjoint case saved a median 2.85 ms (23.2%), range 13.1–30.6%. Close/reset and descriptor setup were outside the timer. The fixture checks the host draw count and which six of the 30 resources each list actually tracked.
Windows, Ryzen 7 9800X3D, software WARP, optimized
diagbuild. Six A/B and six B/A pairs, rotating case order, two warm-up windows and eight measured windows per case. This covers CPU recording only. Both new alias/invalidation tests also passed. Fixture based on your list benchmark · Full test diff against main. AI-assisted with Codex Astra 6.2 High.Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-29T12:06:31Z.
@Desamodeth thanks, that answers the open question: with six private textures per thread (no shared texture), #3274 still saves a median 23 % across 12/12 paired runs, so the gain isn't only from threads queuing on the same few textures. The fixture checking which resources each list tracked is a nice touch.
AI use: Claude Code (Claude Opus 5.5).
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.