d3d12/shadow: allocate cold buffer and window copies outside table write locks (D3D12-SHADOW-UNLOCK-ALLOC) #3139
Open
github-bot
wants to merge 2 commits from
github-bot/gh-3139 into main
AGit
pull from: github-bot/gh-3139
merge into: github:main
github:main
github:perf/darner-title-priority-ceiling
github:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
github:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
github:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
github:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
github:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
github:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
github:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
github:fix/runtime-test-conservative-fixtures
github:ci/shard-semantic-linux
github:docs/fg-limit-fps-switch
github:fix/runtime-test-fixture-paths
github:fix/runtime-test-fixture-readers
github:fix/read-file-hard-link
github:fix/book-slot-heap-never-read
github:fix/depth-resolve-spv-path
github:fix/indirect-unused-batched
github:gh-pr/3358/base
github:fix/xmem-refusal-diagnostics
github:gh-pr/3357/base
github:ci/shorten-ci-time
github:fix/alias-later-queued-submissions
github:claude/gallant-sagan-aamatr
github:gh-pr/3342/base
github:gh-pr/3341/base
github:d3d12/agility-sdk-621
github:gh-pr/3293/base
github:gh-pr/3319/base
github:gh-pr/3318/base
github:gh-pr/3315/base
github:gh-pr/3313/base
github:gh-pr/3314/base
github:claude/fervent-curie-m19lnr
github:gh-pr/3216/base
github:gh-pr/3272/base
github:gh-pr/3276/base
github:gh-pr/3279/base
github:gh-pr/3257/base
github:gh-pr/3258/base
github:gh-pr/3271/base
github:codex/skip-no-hit-ray-passes
github:gh-pr/3230/base
github:gh-pr/3266/base
github:gh-pr/3247/base
github:gh-pr/3246/base
github:gh-pr/3244/base
github:claude/sharp-sagan-1jqnp3
github:t3/preserve-fsr-sdk-restart-path
github:t3/propagate-host-failure-hresult
github:t3/launcher-custom-parameters
github:t3/series-x-2160-startup
github:codex/d3d12-uncapped-simulation
github:ci/less-to-run
github:perf/native-compute-shader-forms
github:claude/blissful-franklin-ukp7ag
github:codex/full-precompile-default
github:gh-pr/3166/base
github:gh-pr/3148/base
github:gh-pr/3138/base
github:gh-pr/3106/base
github:gh-pr/3057/base
github:gh-pr/2756/base
github:gh-pr/2812/base
github:gh-pr/3032/base
github:gh-pr/2992/base
github:notes/card-time-and-plant-flicker
github:gh-pr/2986/base
github:gh-pr/3000/base
github:gh-pr/2966/base
github:gh-pr/2924/base
github:gh-pr/726/base
github:gh-pr/2761/base
github:gh-pr/2663/base
github:gh-pr/2861/base
github:gh-pr/2862/base
github:gh-pr/2820/base
github:gh-pr/2811/base
github:gh-pr/2803/base
github:rt-call-trace
github:gh-pr/2802/base
github:gh-pr/2766/base
github:perf/ahead-split
github:gh-pr/2589/base
github:gh-pr/2699/base
github:gh-pr/2696/base
github:docs/readme-runtime-proposal
github:gh-pr/1346/base
github:codex/coord-claim-check-20261124-7e48
No reviewers
Labels
Clear labels
accessibility
Barrier affecting people with disabilities
area: ci
.github/ workflows and settings
area: d3d12
The D3D12 runtime in d3d12/ (Rust: leonida.exe and its crates)
area: docs
Documentation
area: launcher
The launcher (launcher/)
area: runtime (legacy)
The earlier Vulkan runtime in runtime/
area: shader translator
The shader translator (leonida-shader, tools/rdna2spirv) and its tests
area: tools
tools/ and scripts/
area: viewer
The map viewer (viewer/)
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
duplicate
This issue or pull request already exists
enhancement
New feature or request
good first issue
Good for newcomers
help wanted
Extra attention is needed
invalid
This doesn't seem right
missing claim
Needs a claim in #14
needs in-game run
Changes the code a frame runs through: needs the 300 s in-game gate (docs/game_gate.md)
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
wontfix
This will not be worked on
No labels
accessibility
area: ci
area: d3d12
area: docs
area: launcher
area: runtime (legacy)
area: shader translator
area: tools
area: viewer
bug
dependencies
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs in-game run
needs rebase
needs-triage
question
ready-for-agent
submodules
wontfix
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!3139
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3139"
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?
@zmasi on GitHub · 2026-12-02 13:36 UTC · original
zmasi:perf/d3d12-shadow-unlock-alloc→main· +1046 −178 · 1 files · 2 commits · openClaimed in the coordination issue as:
D3D12-SHADOW-UNLOCK-ALLOC(#2570)Addresses #3105 (does not close it: eliminates exclusive
BUFFERSandLOOSEtable write-lock contention across slow D3D12 host/card buffer creation onleonida-list-Nthreads; companion timeline attribution is in #3136).What and why
shadow::exact(d3d12/crates/leonida-gpu/src/shadow.rs:7700) already dropped its table lock before callingshared_buffer(CreateCommittedResource+MakeResident+Map) because holding the map lock across kernel/WDDM buffer creation serialized allleonida-list-Nthreads (WaitOnAddress). However, bothshadow::lookup(BUFFERS) andshadow::loose(LOOSE) still held exclusiveRwLockwrite guards across cold D3D12 allocations — matching #3105's stall trace pattern where oneleonida-list-Nthread hits an unhooked WDDM allocation/residency call insidelist::with_statewhile peerleonida-list-Mthreads block inWaitOnAddresslinked to it:shadow::lookup()(BUFFERS) cold allocation outsideBUFFERS.write():place_for,shared_buffer, andcard_forallocations outsideBUFFERS.write(), deduplicated per(start, resource, generation)viaBUFFERS_INFLIGHTwith an unwind-safe guard so panics orNonereturns always clean up the in-flight slot and wake waiters without poisoning.Entry.generation(NEXT_BUFFER_GEN) +entry.endto prevent ABA/resurrection ifunregisterorregisterraces mid-allocation, retries in-place opening across transientwatch::Remappingwindows (watch::change_over()) soLEONIDA_PORT_CONSTANTS=1/LEONIDA_INPLACEbuffers are not demoted to copies during streaming, and runsnote_carded+elide_note_boundsunderbuffers()strictly on winning publication (Shadow::newattachescard/mappedimmediately after allocation so unwinds never leak global byte counters).shadow::loose()(LOOSE) read-lock hits & unlocked cold window creation:tails()andany && fine()) underLOOSE.read()and coordinates cold(start, len)window creation (place_for,window_mapped_for,shared_buffer,window_card_for) outsideLOOSE.write()viaLOOSE_INFLIGHT, keepingArc::strong_count >= 2until followers clone and runnote_asksolet_go_idleremains race-free.Tested
python -I scripts/red_green.py HEAD --no-fetch --no-python:verdict: red then green(3/3new concurrency/race/unwind regression tests fail onmainand pass onHEAD;692tests pass on both).--release, 7 hot reader list threads + 1 thread in a simulated50 mscold allocation):BUFFERS(lookup/find): max reader stall48.241 ms->0.237 ms(203xlower stall latency;217,090->1,604,541reader ops).LOOSE(loose): max reader stall47.883 ms->0.026 ms(1,841xlower stall latency;194,799->2,368,813reader ops).x86_64-pc-windows-msvc):cargo test -p leonida-gpu [--features diagnostics] --lib --target x86_64-pc-windows-msvc shadow::—70 passed, 0 failedin both default anddiagnosticsbuilds (covering hot-read non-blocking during coldBUFFERS/LOOSEmisses, single-allocation deduplication on concurrent same-key misses,16 MiBcard-eligible unregister race + panic unwind with zerolive_span_bytes()leak, andLEONIDA_WINDOWS_BAR_MBindependence).cargo clippy -p leonida-gpu [--features diagnostics] --all-targets --no-deps --target x86_64-pc-windows-msvc—0warnings inshadow.rs.python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/shadow.rsandpython -I tests/test_text_write_line_endings.py— exit0.GPUs tested: NVIDIA GeForce RTX 3090 24 GB, driver 610.88, Windows 11 Pro
10.0.26200(D3D12 host/card buffer unit tests and multi-thread contention benchmarks; no retail title run).AI-assisted: yes — authored with Gemini 4 Carbon (High) and independently audited across two read-only review cycles by Gemini 4 Neon (XHigh) (which caught and verified pre-logging
Shadowattachment for unwind byte-counter safety andwatch::change_over()in-place retry during concurrent page remaps).Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.@Kal0s-design on GitHub · 2026-12-02 14:40 UTC · original
Ran natively on Windows 11, head
2bc1f6f43merged into currentmain(69cd4106c, merges cleanly), on WARP and on one NVIDIA desktop card. I also wrote some throwaway stress tests (not pushed) and ran them on the merge and onmainalone.Suites on the merge
cargo test -p leonida-gpu --lib --features diagnostics, WARPcargo test -p leonida-gpu --lib(default features), WARPunlock_alloc_tests, WARP and NVIDIAcargo clippy -p leonida-gpu --all-targets --features diagnosticsshadow.rsThe new tests return early when there is no host. A host was present in both runs (the panic-unwind case in
unregister_race_...only passes when the allocation path actually runs).Races, leaks and duplicates (stress, 20 rounds each, WARP and NVIDIA, merge vs
main)mainfinds over 48 cold buffers, no churn: allocations / copies per bufferunregister+registerwith a new size: a second copy for a buffer that was never re-registeredpeekshowing an old-size copy after the roundLIVEcount and bytes,live_span_bytes()after unregistering, both casesfinds over 24 cold 4 MiB windows: allocations / copies per window /LIVEafterLIVEafter, a normal buffer right afterIn the churn rounds the merge makes more copies than it hands out (for example 1,591 made, 1,352 seen). Those are the
Stalecopies dropped afterregisterraced the allocation. All of them were freed:LIVEwent back to zero every round.One behaviour
maindoes not have:findcan returnNonefor a live bufferlookupnow gives up after 8Staleoutcomes in a row. One thread re-registers a single buffer in a loop while 8 threadsfindit, 24,000 asks per run:Noneon the mergeNoneonmainmainthe loop waits behind each allocation)When I raised the bound from 8 to 1,000 locally, the merge went to 0
Noneat the 20 us pacing. So these are theStaleretries running out. The setup is artificial, and I don't know whether a title ever re-registers one buffer this fast. If it does, the draw gets no copy at all, wheremaingives one. Falling through to theloosepath or retrying without a bound would avoid that.Opt-in finer windows (
LEONIDA_WINDOW_KB=256,find_any), WARPThread A asks for a 1 MiB range and is held inside its window allocation. Thread B then asks for 64 bytes inside that range:
mainholder/longest_tailnow pick the key under the read lock, and the in-flight table only joins asks with the exact same key. So overlapping asks that race make more than one window, whichmainavoided by holding the write lock. Only withLEONIDA_WINDOW_KBbelow 4096 orLEONIDA_WINDOW_TAILS=1, and it costs memory and copies, not correctness. I did not tryLEONIDA_WINDOW_TAILS=1on its own.Lock hold time with real allocations (no simulated sleep)
The bench in the PR compares a sleep under the lock against a sleep outside it, so it shows what the design allows but not what an allocation costs. Here, one thread makes 64 real cold copies of 16 MiB buffers in a row while 6 threads look up a buffer that already has its copy. 3 runs each, ranges across runs:
main, WARPmain, NVIDIAOn
mainthe slowest lookup matches the slowest allocation, so readers wait out the whole allocation. On the merge they don't. The allocations themselves take about as long as before.Overlap with #2981
Both PRs edit
shadow.rs, and they conflict when merged together (5 hunks). Three hunks are only the two new test modules colliding at the end of the file. The other two are real: #2981 adds anaccountfield toCard, and the twoCard { .. }literals this PR rewrites inbuild_buffer_shadowandbuild_loose_shadowdon't have it. Whichever lands second has to addaccount: CardAccount::Bufferandaccount: CardAccount::Windowthere. Otherwise it won't compile, andDrop for Shadowwould release from the wrong counter. I did not resolve or build the combination.Not checked: in-game run; the combination with #2981;
LEONIDA_WINDOW_TAILS=1on its own;LEONIDA_PORT_CONSTANTS=1and in-place (LEONIDA_INPLACE) paths under concurrency; the debug layer.GPUs tested: one NVIDIA desktop card + WARP.
AI-assisted: yes (Claude Code).
@lixonik on GitHub · 2026-12-02 15:27 UTC · original
RUN (in game) -- unit tests + 300 s render-path verification for PR #3139:
2bc1f6f43mainSHA:acc350745(git merge --no-ff, clean merge)python -I scripts/check_no_game_data.pyPASScargo test -p leonida-gpu --features diagnostics --lib --locked shadow::-- 70 passed, 0 failed, 3 ignored (including all 4 newshadow::unlock_alloc_tests::*concurrency/coalescing/unwind tests)diagprofile (diagnostics,dlss,mimalloc), Series S profile, DLSS on, Frame Gen off, same restored save snapshot2bc1f6f43onacc350745)mainalone (acc350745)Loaded into the world cleanly, stayed up past 300 s in-world with 0 device lost / 0 crashes / 0
ERRORlines, and closed cleanly. Steady-state idle-camera FPS and preparing time matchmainwithin run-to-run variance (as expected, since lockless cold allocation targets cold-miss stalls on recording threads rather than hot-cache steady-state frames).Verdict: PASS.
AI-assisted: yes (Antigravity).
@fiestorra on GitHub · 2026-12-02 16:26 UTC · original
Verified here: it applies, and the whole
shadowgroup passes with it - 70 tests, 0 failed, including the four new ones that are the point of the change.Those four are the right four for "allocate outside the table write locks": a miss not blocking hot reads (the property the change exists for), two concurrent misses making one allocation (the failure mode of moving allocation out - a storm of duplicates), and an unregister racing an allocator panic cleaning up without a leak or a deadlock (the one that only shows up in a crash report months later). All pass, and nothing else in the module moved.
What that does not measure: the contention it is meant to reduce. A test can show that a miss no longer blocks a reader; it cannot show how much time that saves in the title, and I did not run one - the numbers for that would need a run, which this machine cannot do.
And one thing to keep an eye on:
unregister_race_and_allocator_panic_clean_up_without_leak_or_deadlockis a name that describes a real hazard - if it ever goes flaky, it is worth reading before skipping it.AI-assisted: yes - DeepSeek v4.1 (flash).
Written with an AI assistant.
@MoonEyes on GitHub · 2026-12-02 18:02 UTC · original
Light code review only, not built (Windows-only
leonida-gpu). Ready, based on the runs already posted on this head.2bc1f6f43, merge base044339e4c, 63 behind currentmain45dc41f95. A trial merge is clean. CI on the head: all 6 checks pass.main. This satisfies the render-path gate.shadowgroup passes.Review notes on
d3d12/crates/leonida-gpu/src/shadow.rs(1,046 added, 178 removed):place_for,shared_bufferandcard_forallocation now happens outsideBUFFERS.write()andLOOSE. Work is de-duplicated through an in-flight table with an unwind guard. EachEntry.generationplusendcheck on publication guards against ABA whenunregisterorregisterraces the allocation. This is the right shape for #3105: blocking WDDM calls no longer serialize theleonida-list-Nthreads.Shadow::newright after allocation, so a loser does not leak them. I did not trace every unwind path in a diff this size. The in-game and unit runs above are the evidence for those paths, not this review.Verdict: ready.
AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.
Generated by Claude Code
@fiestorra on GitHub · 2026-12-02 23:11 UTC · original
VERIFY-3139 — Windows nativo: 70 pruebas de
shadowpasan y el control muerde el coalescingad72eee3f—shadow.rs, +1046/−178 (the branch also mergesmain,2bc1f6f43)044339e4cRan at the head
Negative control (synthetic): I made the in-flight table never find its own entry (
map.get(&key).cloned().filter(|_| false)), so every concurrent miss allocates its own, rebuilt and ran:The test bites on the mechanism the commit adds — a per-key in-flight map with a
Condvar: one thread builds, the others wait and take the published value, and a hit never touches the table's write lock at all.Why the shape is right for what it is: this is a lock-hold-time change, so the tests are named after the property rather than the function — "a lookup miss does not block hot buffer reads", "…hot window reads", "concurrent misses coalesce a single allocation", "unregister race and allocator panic clean up without leak or deadlock". Those are the four ways this kind of change breaks, and each has one.
What I could not test
shadow), not the full suite on the merged tree.Written with an AI assistant.
@kaoru-maki on GitHub · 2026-12-03 05:26 UTC · original
For a follow-up to the cold allocation in
shadow::lookup, could we measure allocator time and the wait on unrelated hot lookups separately? A burst of misses for different keys would help show how far the stall spreads. Run the two versions in both orders from the same initial state.Synthetic test. Park the allocation for key A, queue a second cold miss for B, and look up the already-ready key C. With unlocked allocation, C should complete while A is still paused. Record the longest hot lookup wait in each run.
@kaoru-maki on GitHub · 2026-12-03 05:26 UTC · original
A follow-up to the unlocked allocation would be to install the ready buffer in a per-resource record. Validate the generation and range and install the handle while holding the same entry guard used to retire the resource. That would move final handle publication off the global index write lock.
Synthetic race test. Pause the builder for generation 1 of resource A after allocation. Unregister A, register it again as generation 2, and resume the old builder. Assert that the old candidate is discarded and generation 2 stays current. Publishing A should also allow lookups of an unrelated ready entry to finish.
@kaoru-maki on GitHub · 2026-12-03 08:07 UTC · original
Follow-up with four matched A/B pairs using this PR head (
2bc1f6f43399) merged locally into cleanmain(d9b618f10150). I used an RTX 5070 Ti Laptop GPU (12 GB) on Windows 11 with NVIDIA driver 621.47, native 2560×1440 DLAA, no frame generation, and Smooth Motion off. The order wasA B B A A B B Aafter two warm-ups. Each measured run started from the same prepared PSO cache snapshot.The paired change in median frame time was −0.43 ms for B versus A (95% CI −0.98 to +0.13 ms). The early window showed fewer long frames. The full-route result was inconclusive.
Run report in #2464.
@merzleckeier on GitHub · 2026-12-03 08:18 UTC · original
I ran the requested two-cold-key control against the actual lookup paths on
maind9b618f1and this head2bc1f6f4. The same test-only hook parked cold lookup A for 150 ms immediately before its host allocation. Fresh threads then calledfindfor cold key B and for an unrelated ready key C. C had an empty thread-local cache, and the test checked that it returned the existingArc. The run order was A/B, B/A, A/B.mainThe timings are from the synthetic 150 ms pause in a Windows MSVC build under Wine 11.22. The four existing
shadow::unlock_alloc_testspassed, with the separate ignored benchmark left ignored. This isolates the spread of the table-lock wait. AI-assisted with OpenAI Codex.@merzleckeier on GitHub · 2026-12-03 10:28 UTC · original
I checked the #2981 overlap with this exact head (
2bc1f6f43) onmaind9b618f10. The #2981 headb020637acstill conflicts here in five hunks ofshadow.rs. A local resolution kept this PR’s unlocked allocation and publication, setCardAccount::Bufferon the cold buffer constructor andCardAccount::Windowon the cold window constructor, and kept both test modules.The resolved diagnostics build passed the
shadow::suite on AMD Radeon (RADV 26.3.1, Wine 11.22, vkd3d-proton 3.1.0). 73 passed, 0 failed, 3 ignored. A separate synthetic test usedLEONIDA_CARD_ALL=1andLEONIDA_WINDOWS_OWN=1to exercise both actual constructors. Freeing the buffer returnedCARD_BYTES[0]to baseline while leaving the window’s reservation live. Freeing the window then returnedWINDOW_CARD_BYTESto baseline.BY_ADDRESS_USEDstayed at baseline throughout.git diff --checkpassed.AI-assisted verification with OpenAI Codex.
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.