shader/emit: cache immediate sampler handles in entry block (D3D12-SHADER-SAMPLER-HANDLE-CACHE) #2862
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2862"
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 ubiquitarianism/leonida. Originally posted by djrolfes on 2026-12-01T10:31:50Z.
Claimed in the coordination issue as:
D3D12-SHADER-SAMPLER-HANDLE-CACHE(claim comment)What and why
Cache immediate sampler handles (
createHandleFromBinding+annotateHandle) once inself.entryinsideShader::sampler_handle(d3d12/crates/leonida-shader/src/emit/shader.rs), bringing it into parity withroot_cbv_handle,root_srv_handle, androot_uav_handle.main,root_cbv_handle,root_srv_handle, androot_uav_handlealready emit their binding handle once inself.entryand cache it, whereassampler_handleemitted a freshcreateHandleFromBinding+annotateHandlepair at every call site in every basic block.sampler_handles: Vec<(u32, bool, V)>keyed by(slot, is_cmp)(withn < u64::BITSguard before shiftingself.cmp_samplers), emits the handle pair inself.entry(which dominates all blocks), and returns the cached handle on subsequent calls. Completely orthogonal to open PR#2687(which hoistscreateHandleFromHeap, notcreateHandleFromBinding).d3d12/crates/leonida-shader/src/emit/shader.rs:526–625) + red→green unit test verifying handle reuse across sibling branches and valid DXIL serialization + MEASURED 300 s in-game gate (2 consecutive 310 s PASS runs) on exact PR headacae5b879.Tested
python3 -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/shader.rs→ exit 0.cargo test --locked -p leonida-shader:origin/main(only the new#[cfg(test)]module applied, no production change):acae5b879(rebased ontocd73ddab7):docs/game_gate.md) on exact PR headacae5b879(leonida.exe--profile play --features mimalloc,dlss,diagnostics, Series S + DLSS Quality 1280×720 → 1920×1080):1,155 → 422in the background during Pass 2, accounting for the lower transient median FPS compared to fully-cached non-DXIL-changing builds)310.14 s(world_at_s = 79.39 s,>= 300 srequired)world + 60 sto stop):719presented frames (759total world presents),7.22median FPS (9.00end-window median FPS as background PSO compilation drained),1,674.72 msp95 frame time (4,445.5median draws/frame)5.127 s(<= 10.0 srequired)6,299 MiB(<= 7,400 MiBguard)0device losses,0crashes/faults,0panics,0kernelNVRM: Xiderrors311.00 sin-world,386frames,5.21median FPS,8.672 slongest present gap,6,043 MiBpeak VRAM,0device losses/crashes/faults/panics/Xids)GPUs tested: NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Arch Linux (Wine 11.22 + VKD3D-Proton 3.2.0).
AI-assisted: yes (authored by Gemini 4 Neon; audited and approved in a separate independent review pass by Gemini 4 Neon).
Checklist
main(cd73ddab7) and merges cleanly.check_no_game_data.pyclean).docs/design.md(n/a — entry-block handle caching matching existing root handle helpers).acae5b879).Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T12:17:15Z.
Independent Rust check of
acae5b879on21e6bbf4b. The added fixture failed on upstream because the same immediate sampler slot used in two sibling blocks produced distinct handle instructions. With the PR, both the ordinary and comparison sampler handles are reused across the two blocks.I ran the full native
leonida-shaderunit suite with #2861 and #2864 also applied. It passed 250 tests, with 2 ignored. This checks IR construction and reuse. Driver performance was outside the run.AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T13:53:24Z.
Review: COMMENTED
The hoist is sound: only immediate (
Source::Constant) samplers reach this helper, bound as root-signature static samplers, so the binding is uniform and valid at entry, andentrydominates every block. Loop and branch-chosen cases behave (same slot dedups; distinct slots get their own handles and a uniform phi picks one). 406/0/5 here.Three things:
cmpin the key is redundant.register_sampleralready gives a distinct slot per(descriptor words, cmp)and sets the bit before returning it, so the slot alone identifies the sampler — droppingcmpchanges nothing and all sampler tests still pass. Either drop it or say in the comment that the cache relies on that slot↔descriptor invariant._blockis now dead (four call sites still passself.cur/arm). Remove the parameter so nobody reintroduces "create in the use block" by mistake.let block = self.entrywithlet block = _blockleaves the whole 406-test suite green, even though a handle created in one branch and reused in a sibling is a dominance violation — the test is named…emitted_once_in_entry…but never inspects the block and never uses the handle. Either assert the instruction lives inentry(a smallModuleaccessor) or drive a realsampleLevelfrom two sibling blocks and validate withdxil.dll, aswaterfall_tests::immediate_sampler_chosen_by_control_flowalready does.Also:
assert_ne!(h0_a, h1_a)passes even with the cache keyed bycmpalone (slot 0 non-cmp vs slot 1 cmp); two samplers of the same kind would pin the slot field.Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-01T14:01:53Z.
RUN (native Windows 10 + RTX 5070 verification; #2857, #2860, and #2862 merged cleanly together onto
mained867d7b2)cargo test --locked -p leonida-shader --lib sampler_handle_is_emitted_once_in_entry_and_cached:1 passed, 0 failed.Series S,-NoFrameGen,731.6 scold precompile,180 sin world (321.3 stotal,38world samples>= 1000draws, median22.2 fps/45.1 ms,gpu_wait 0.0 ms,4,424median /5,346max draws):0crashes,0panics,0device-lost events.GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver
621.40, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T)AI-assisted: Antigravity
Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-01T15:49:34Z.
VERIFY-2862 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files
Verdict: PASS. The change does what it says, and it also closes a debug-build panic that the pull request does not mention.
Head
acae5b87, base the currentmain88a9f133, merge basecd73ddab, 1 commit, 1 file, +49/−8:d3d12/crates/leonida-shader/src/emit/shader.rs.1. What it changes
sampler_handleused to emit a freshcreateHandleFromBindingevery time it was called, in whatever block the caller was in:The pull request emits one handle per
(slot, is_comparison)and always in the entry block:The callers pass the block they are in —
lower.rs:9642passesself.cur,lower.rs:13816passesarm(a switch arm's block) — so before this change the same sampler slot asked for from two different blocks produced two handles, each valid only in its own block. Putting the one handle in the entry block, which dominates everything, is the scope-correct place for it.2. Red and green, and the discrimination
cargo test -p leonida-shaderon the pull request's branch: 243 passed, 0 failed, 2 ignored.With only the cache lookup removed (the pull request's test kept):
3. The panic this also fixes
The new code guards the comparison-sampler bit with
n < u64::BITS;maindid not:self.cmp_samplersis au64, so>> nwithn >= 64is an overflow. In a debug build that is a panic. I called it directly:This is the same family as #2858 (an unchecked arithmetic operation on a value the title controls, reaching a panic), and it is not mentioned in the pull request's own text. Whether
ncan reach 64 in practice depends on how many samplers a shader may declare; I did not trace that bound, but the guard is correct either way.4. What the new test does not check
The test is called
sampler_handle_is_emitted_once_in_entry_and_cached, and it checks the caching (assert_eq!(h0_a, h0_b, …)) — but it does not check the "in entry" part. That half is not checked because it cannot easily be:Module::blocksis private to thebitcodemodule, so fromemit::shader::teststhere is no way to look at which block the instruction went into. I confirmed that by trying:The placement is unambiguous in the code (
let block = self.entry;immediately before theself.op(block, "createHandleFromBinding", …)call), so I am not asking for a change — but a reader should know that the name promises more than the test delivers.5. What I did not check
(slot, cmp)key. The test checks that two slots give two different handles and that one slot gives one handle; it does not check that a comparison sampler and a plain sampler for the same slot would be distinguished — the key does includecmp, but I did not build that case.leonida-gpu,leonida-hleandleonida-loaderdo not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows #2577"), so I ranleonida-shaderon its own.cargo clippy(already failing onmain, run as a report by the workflow) andcargo fmt(left out of the d3d12 workflow).AI-assisted: MiniMax M3.1 Flash.
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T20:53:48Z.
Merging as part of a group. Head
acae5b879; the group branch main1e3e316c5+ #2862 + #2824 + #2873 (7988c1c89, merged in that order) passed the full CI (https://github.com/OreoFR/leonida/actions/runs/46740411028, the same workflow file as here, run on that branch) and No Game Data (https://github.com/OreoFR/leonida/actions/runs/46740416586); main was1e3e316c5with its CI green when the group merged. Claim: D3D12-SHADER-SAMPLER-HANDLE-CACHE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7305584024). Red/green: merzleckeier (fixture fails on main, passes on this head), imShangLi (Linux, PASS), KelboLive review on this head. I ran: the five PRs #2860-#2862, #2824 and #2873 merged together onto mainab374c7d5(main since gained only #2935, a CI workflow change, and #2860 and #2861, both part of that test), with the shader (418 passed, 0 failed), gpu (644/0) and hle (247/0) cargo tests. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, a cold start to the world then a 330 s warm walking run, 0 device lost, no panic. CI: green on head, main and the group branch. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T23:07:19Z.
RUN, posted after the merge: the review points below are for a follow-up. (independent run on Windows, #2862 head
acae5b879, merged locally intomain0ff8a532fwithgit merge --no-ff, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)Commands, in
d3d12/, target x86_64-pc-windows-gnu (the host this machine's rustup selects; CI builds x86_64-pc-windows-msvc), toolchain 1.98.1, all with--locked, at low priority,RUST_TEST_THREADS=8:cargo test -p leonida-shader --no-fail-fast: main 414 passed / 0 failed / 5 ignored; merge 415 / 0 / 5 (the new test). Some of these tests run on the GPU, here an RTX 3090;host_forms_give_what_the_spelled_out_ones_givepasses on both sides.cargo test -p leonida-shader --no-fail-fast --features diagnostics: main 414 / 0 / 5; merge 415 / 0 / 5.shader.rswith this pull request's test module appended.cargo test -p leonida-shader --lib emit::shader::testsfails on "same sampler slot must reuse cached entry handle", leftInst(1), rightInst(3): the values in the body.let block = _block;in place oflet block = self.entry;), so a second use in a sibling block reads a value its block is not dominated by. The new test passes; the whole-crate run was not done here (the card was busy with game runs); its GPU tests build sampler-using programs for the card, so it is the run that could still catch this. The test checks reuse and thatfinish()returns bytes, butfinish()does not check that a definition dominates its uses, so the "in entry" half of the test's name is not pinned. Checking that thecreateHandleFromBindinglands ins.entry, or validating the container with the DXIL validator (which refuses such a use), would pin it.git statusclean),cargo test -p leonida-shader --lib emit::shader::tests: 1 passed.cargo clippy -p leonida-shader --all-targets, default and--features diagnostics, on main and on the merge: 6 warnings on each side, the same set (all invalu_tests.rs), none in the changed lines.python -I tools/d3d12_switches.py --check d3d12/docs/switches.mdis clean on the merge (no switch added).main), ands.entryonly moves before the program's blocks are lowered (the thread-count and packing preludes inrunpoint it at their body block, which then becomes the program's first block), so a handle made there dominates every block lowered after it. Then < u64::BITSguard also removes the shift overflow for a slot of 64 or more (a panic in a debug build before). Theblockparameter is now unused (_block) while the callers still pass one.Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.