d3d12-gpu: prime a draw's tested depth before its picked textures are read #2866
Closed
aznohonza wants to merge 2 commits from
upstream-pr/2866 into main
pull from: upstream-pr/2866
merge into: ProjectLeonida:main
ProjectLeonida:main
ProjectLeonida:claude/loop-total
ProjectLeonida:claude/pops-collision-seed
ProjectLeonida:claude/dstorage-hold-page-reads
ProjectLeonida:claude/host-image-copy-switch
ProjectLeonida:feat/gdk-lifecycle-window-hook
ProjectLeonida:claude/d16s8-uav-nvidia-fallback
ProjectLeonida:feat/mfplat-offline-shim
ProjectLeonida:claude/gameinput-slow-reader
ProjectLeonida:fix/rt-null-barrier-clear
ProjectLeonida:claude/depth-resolve-sample-positions
ProjectLeonida:integration/palmetto-parity
ProjectLeonida:claude/valu-literal-sext
ProjectLeonida:claude/slot74-pool-handle
ProjectLeonida:claude/rgb9e5-uint-alias-view
ProjectLeonida:claude/fssm-root-layout-tables
ProjectLeonida:feat/gdk-speech-display-lifecycle
ProjectLeonida:asset-audio-skip-current
ProjectLeonida:qemmhd/old-9940-rectangle-quad-lists
ProjectLeonida:feat/ws2-offline-shim
ProjectLeonida:qemmhd/rt-d16-unorm-uav
ProjectLeonida:feat/xs-device-stops-7-9
ProjectLeonida:qemmhd/rt-depth-same-base-shapes
ProjectLeonida:qemmhd/rt-lazy-attachment-variant
ProjectLeonida:feat/acphal-xma2-decoder
ProjectLeonida:asset-audio-skip
ProjectLeonida:codex/coord-claim-check-20261124-7e48
ProjectLeonida:feat/xs-frame-pacing-presentx
ProjectLeonida:feat/xs-device-vm-options
ProjectLeonida:old-pr/9844
ProjectLeonida:old-pr/9978
ProjectLeonida:old-pr/9815
ProjectLeonida:old-pr/10057
ProjectLeonida:old-pr/9874
ProjectLeonida:old-pr/9965
ProjectLeonida:old-pr/9863
ProjectLeonida:old-pr/10068
ProjectLeonida:old-pr/10066
ProjectLeonida:old-pr/10067
ProjectLeonida:old-pr/10007
ProjectLeonida:old-pr/10061
ProjectLeonida:old-pr/10065
ProjectLeonida:old-pr/10059
ProjectLeonida:old-pr/10063
ProjectLeonida:old-pr/10055
ProjectLeonida:old-pr/10062
ProjectLeonida:old-pr/10042
ProjectLeonida:old-pr/10041
ProjectLeonida:old-pr/10049
ProjectLeonida:old-pr/10047
ProjectLeonida:old-pr/10046
ProjectLeonida:old-pr/10045
ProjectLeonida:old-pr/10056
ProjectLeonida:old-pr/10043
ProjectLeonida:old-pr/10036
ProjectLeonida:old-pr/10034
ProjectLeonida:old-pr/9984
ProjectLeonida:old-pr/10052
ProjectLeonida:old-pr/10038
ProjectLeonida:old-pr/10033
ProjectLeonida:old-pr/10028
ProjectLeonida:old-pr/10050
ProjectLeonida:old-pr/9891
ProjectLeonida:old-pr/9889
ProjectLeonida:old-pr/10051
ProjectLeonida:old-pr/10019
ProjectLeonida:old-pr/10027
ProjectLeonida:old-pr/10029
ProjectLeonida:old-pr/10031
ProjectLeonida:old-pr/10026
ProjectLeonida:old-pr/9941
ProjectLeonida:old-pr/10030
ProjectLeonida:old-pr/10040
ProjectLeonida:old-pr/9990
ProjectLeonida:old-pr/10021
ProjectLeonida:old-pr/9962
ProjectLeonida:old-pr/10022
ProjectLeonida:old-pr/10020
ProjectLeonida:old-pr/10015
ProjectLeonida:old-pr/10025
ProjectLeonida:old-pr/9886
ProjectLeonida:old-pr/10048
ProjectLeonida:old-pr/10014
ProjectLeonida:old-pr/10011
ProjectLeonida:old-pr/9861
ProjectLeonida:old-pr/10010
ProjectLeonida:old-pr/10008
ProjectLeonida:old-pr/9860
ProjectLeonida:old-pr/10006
ProjectLeonida:old-pr/9920
ProjectLeonida:old-pr/9995
ProjectLeonida:old-pr/10000
ProjectLeonida:old-pr/10016
ProjectLeonida:old-pr/10004
ProjectLeonida:old-pr/10023
ProjectLeonida:old-pr/9982
ProjectLeonida:old-pr/9981
ProjectLeonida:old-pr/9983
ProjectLeonida:old-pr/9970
ProjectLeonida:old-pr/9969
ProjectLeonida:old-pr/9968
ProjectLeonida:old-pr/9971
ProjectLeonida:old-pr/9972
ProjectLeonida:old-pr/9942
ProjectLeonida:old-pr/9963
ProjectLeonida:old-pr/9960
ProjectLeonida:old-pr/9964
ProjectLeonida:old-pr/9985
ProjectLeonida:old-pr/9973
ProjectLeonida:old-pr/9862
ProjectLeonida:old-pr/9953
ProjectLeonida:old-pr/10017
ProjectLeonida:old-pr/9952
ProjectLeonida:old-pr/9955
ProjectLeonida:old-pr/9946
ProjectLeonida:old-pr/9954
ProjectLeonida:old-pr/9938
ProjectLeonida:old-pr/9956
ProjectLeonida:old-pr/9856
ProjectLeonida:old-pr/9947
ProjectLeonida:old-pr/9924
ProjectLeonida:old-pr/9961
ProjectLeonida:old-pr/9927
ProjectLeonida:old-pr/9979
ProjectLeonida:old-pr/10032
ProjectLeonida:old-pr/9926
ProjectLeonida:old-pr/5237
ProjectLeonida:old-pr/9951
ProjectLeonida:old-pr/9931
ProjectLeonida:old-pr/9902
ProjectLeonida:old-pr/9921
ProjectLeonida:old-pr/9994
ProjectLeonida:old-pr/9775
ProjectLeonida:old-pr/9768
ProjectLeonida:old-pr/9905
ProjectLeonida:old-pr/9936
ProjectLeonida:old-pr/9901
ProjectLeonida:old-pr/9904
ProjectLeonida:old-pr/9906
ProjectLeonida:old-pr/9988
ProjectLeonida:old-pr/9908
ProjectLeonida:old-pr/9909
ProjectLeonida:old-pr/9911
ProjectLeonida:old-pr/9912
ProjectLeonida:old-pr/9913
ProjectLeonida:old-pr/9887
ProjectLeonida:old-pr/9916
ProjectLeonida:old-pr/9919
ProjectLeonida:old-pr/9925
ProjectLeonida:old-pr/9933
ProjectLeonida:old-pr/9935
ProjectLeonida:old-pr/9918
ProjectLeonida:old-pr/9879
ProjectLeonida:old-pr/9880
ProjectLeonida:old-pr/9881
ProjectLeonida:old-pr/9996
ProjectLeonida:old-pr/9876
ProjectLeonida:old-pr/9892
ProjectLeonida:old-pr/9899
ProjectLeonida:old-pr/9915
ProjectLeonida:old-pr/9914
ProjectLeonida:old-pr/9872
ProjectLeonida:old-pr/9869
ProjectLeonida:old-pr/9866
ProjectLeonida:old-pr/9819
ProjectLeonida:old-pr/10012
ProjectLeonida:old-pr/9857
ProjectLeonida:old-pr/9893
ProjectLeonida:old-pr/9897
ProjectLeonida:old-pr/9928
ProjectLeonida:old-pr/9868
ProjectLeonida:old-pr/9917
ProjectLeonida:old-pr/9940
ProjectLeonida:old-pr/9873
ProjectLeonida:old-pr/9816
ProjectLeonida:old-pr/10009
ProjectLeonida:old-pr/9900
ProjectLeonida:old-pr/9966
ProjectLeonida:old-pr/9859
ProjectLeonida:old-pr/9843
ProjectLeonida:old-pr/9991
ProjectLeonida:old-pr/10058
ProjectLeonida:old-pr/9883
ProjectLeonida:old-pr/9838
ProjectLeonida:old-pr/9835
ProjectLeonida:old-pr/9959
ProjectLeonida:old-pr/9895
ProjectLeonida:old-pr/9837
ProjectLeonida:old-pr/9858
ProjectLeonida:old-pr/9865
ProjectLeonida:old-pr/9864
ProjectLeonida:old-pr/9840
ProjectLeonida:old-pr/9820
ProjectLeonida:old-pr/4669
ProjectLeonida:old-pr/9848
ProjectLeonida:old-pr/9850
ProjectLeonida:old-pr/9810
ProjectLeonida:old-pr/9799
ProjectLeonida:old-pr/9853
ProjectLeonida:old-pr/9875
ProjectLeonida:old-pr/9831
ProjectLeonida:old-pr/9796
ProjectLeonida:old-pr/9801
ProjectLeonida:old-pr/9813
ProjectLeonida:old-pr/9794
ProjectLeonida:old-pr/7417
ProjectLeonida:old-pr/9781
ProjectLeonida:old-pr/9806
ProjectLeonida:old-pr/9958
ProjectLeonida:old-pr/9809
ProjectLeonida:old-pr/9791
ProjectLeonida:old-pr/9792
ProjectLeonida:old-pr/10053
ProjectLeonida:old-pr/9784
ProjectLeonida:old-pr/9789
ProjectLeonida:old-pr/9849
ProjectLeonida:old-pr/9846
ProjectLeonida:old-pr/9773
ProjectLeonida:old-pr/9785
ProjectLeonida:old-pr/9803
ProjectLeonida:old-pr/9826
ProjectLeonida:old-pr/9839
ProjectLeonida:old-pr/9922
ProjectLeonida:old-pr/9948
ProjectLeonida:old-pr/7189
ProjectLeonida:old-pr/9957
ProjectLeonida:old-pr/9823
ProjectLeonida:old-pr/9807
ProjectLeonida:old-pr/9851
ProjectLeonida:old-pr/9774
ProjectLeonida:old-pr/9830
ProjectLeonida:old-pr/9836
ProjectLeonida:old-pr/9822
ProjectLeonida:old-pr/9783
ProjectLeonida:old-pr/9833
ProjectLeonida:old-pr/9890
ProjectLeonida:old-pr/9770
ProjectLeonida:old-pr/9769
ProjectLeonida:old-pr/9772
ProjectLeonida:old-pr/9787
ProjectLeonida:old-pr/9852
ProjectLeonida:old-pr/9834
ProjectLeonida:old-pr/9845
ProjectLeonida:old-pr/9871
ProjectLeonida:old-pr/9771
ProjectLeonida:old-pr/9776
ProjectLeonida:old-pr/10005
ProjectLeonida:old-pr/9949
ProjectLeonida:old-pr/9923
ProjectLeonida:old-pr/9805
ProjectLeonida:old-pr/9829
ProjectLeonida:old-pr/9790
ProjectLeonida:old-pr/9827
ProjectLeonida:old-pr/9929
ProjectLeonida:old-pr/9855
ProjectLeonida:old-pr/9934
ProjectLeonida:old-pr/9907
ProjectLeonida:old-pr/9910
ProjectLeonida:old-pr/9779
ProjectLeonida:old-pr/9780
ProjectLeonida:old-pr/9885
ProjectLeonida:old-pr/9882
ProjectLeonida:old-pr/9877
ProjectLeonida:old-pr/9998
ProjectLeonida:old-pr/4251
ProjectLeonida:old-pr/9987
ProjectLeonida:old-pr/9989
ProjectLeonida:old-pr/9950
ProjectLeonida:old-pr/9777
ProjectLeonida:old-pr/10044
ProjectLeonida:old-pr/9884
ProjectLeonida:old-pr/2906
ProjectLeonida:old-pr/5076
ProjectLeonida:old-pr/9937
ProjectLeonida:old-pr/9986
ProjectLeonida:old-pr/3014
ProjectLeonida:old-pr/9943
ProjectLeonida:old-pr/9939
ProjectLeonida:old-pr/4682
ProjectLeonida:old-pr/4765
No reviewers
Labels
Clear labels
No items
No labels
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.
Reference
ProjectLeonida/mandy!2866
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2866"
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 aznohonza on 2026-12-01T11:19:26Z.
State::bindbrings the textures picked by index up to date at its start, and those views go throughreadable(heap_textures, andflush_for_bakebefore it).readableis what notes that a draw reads a view of the depth target it only tests: it setsTESTED_DEPTH's read bit, and a read-only depth target then keepsSHADER_RESOURCEin the stateState::prepare_forgives it (DEPTH_READ | SHADER_RESOURCE).TESTED_DEPTHwas primed for the draw after that pass, so during it the cell still held the previous draw's target. A heap-indexed read of this draw's own depth target was therefore not marked, the pass left the target inSHADER_RESOURCEalone, andprepare_forthen moved it toDEPTH_READalone while the shader samples it.This moves the priming before
early_textures; the state decision inprepare_forand the statesreadablepicks are unchanged. Reads found by the table walk later inbindwere already covered, since that walk is after the priming.Reported in #2570 by @zmasi (
finding: read-only depth plus a heap-indexed read gets DEPTH_READ without SHADER_RESOURCE, pointing at this call).Verification:
cargo xwin check --target x86_64-pc-windows-msvc --workspace --all-targets --lockedpasses. There is no D3D12 device on this Linux host, so the pass itself could only be read-verified here; thed3d12CI job builds and runs the workspace's tests. No game data, no new switches.AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T11:59:48Z.
Independent check of
b0f0c0b45onbf30e2585. An isolated state test exercising the production decisions forState::bind,readable, andprepare_fordropped the shader-resource read state for a draw’s own early-bound depth SRV on the base. With the PR, the matching depth resource retains both read states. A different target and compute retained their respective states in both versions.The Windows GPU crate built with this PR, #2834 and #2831. Three existing draw tests passed on AMD Raphael (RADV 26.3.1) and RTX 4070 SUPER (NVIDIA 620.73.4) under Wine and vkd3d-proton 3.1.0. The isolated test checks state transitions. A complete offscreen draw and pixel comparison remain open.
AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by aznohonza on 2026-12-01T12:00:53Z.
Thanks — that matches the intent exactly: the early pass now reads the picked views with the draw they belong to, so
readablemarks its own depth target andprepare_forkeepsDEPTH_READ | SHADER_RESOURCEinstead of moving the target back toDEPTH_READalone. The base's behaviour you saw is the bug; a different target and compute draws being unchanged is the scope.The full offscreen draw and pixel comparison needs the title's own draw; no D3D12 device here, so the state transition is as far as this side goes. Nothing further from us on this one. AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T12:45:47Z.
Review: COMMENTED
The diagnosis and the move are right.
readable()decidesSHADER_RESOURCEat upload time whileprepare_fordecides the DSV state later, and they only agree ifTESTED_DEPTHalready names this draw — so priming it at the top ofState::bind, beforeearly_textures, is the minimal fix. The guard is the right condition too: read-only DSV (D3D12_DSV_FLAG_READ_ONLY_DEPTH/..._STENCIL,draw.rs:7376-7397), not "DepthEnable", because a draw that writes depth cannot share the target with an SRV anyway and goes throughtwin_use. No ordering hole: the value is reset perbind(compute → 0), the table caches hash the tested address, and nothing reads it outside that bind.Two things before merge:
AGENTS.mdasks that such a change come with a test — at minimum say in the body that the mechanism is GPU-only and that the verification was a run, and ideally add a seam that records whatearly_texturessees inTESTED_DEPTHwhen a picked view is a read of the draw's tested depth.early_texturesrunsheap_textureson a separatebake_list(draw.rs:3882-3907), so with the prime in place that upload list can now also record the combined-state barrier for the depth target. Harmless — same state, per-list tracking, and the draw's own list gets its own transition viaprepare_for— but one sentence in the comment would save the next reader the question.Also worth noting:
texture::alias_cast_tests::committed_and_placed_r32_uint_textures_cast_to_r9g9b9e5_under_legacy_barriersfailed once in a mutated run attexture.rs:5664(pool::fresh) and passes alone and in both unmutated runs — a pre-existing parallel-execution flake, unrelated to this change, but you may want it on the silent/flaky list.Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-01T15:21:44Z.
VERIFY-2866 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files
Verdict: PASS. The ordering change is necessary, and the pull request's explanation of why is accurate: the value it moves is a "which depth target does this draw test" marker that a reader compares against the texture being read, and in
mainthat marker was stale during the very pass that reads the depth texture.Head
b0f0c0b4, base the currentmain88a9f133, merge basebf30e258, 1 commit, 1 file, +11/−8:d3d12/crates/leonida-gpu/src/draw.rs.1. What the marker is
draw.rs:190-196(a thread-local, with its own documentation):The first component is the depth texture the draw only tests; the second says whether a view the draw's shaders read turned out to be of that same texture.
The reader is
readable()atdraw.rs:7740:So the comparison
tested == source as usizeis what decides whether the texture being read is the draw's depth target, and whether the texture keepsDEPTH_READas well asSHADER_RESOURCE. If the marker does not match, the texture is left withSHADER_RESOURCEonly, and the "a view of it was read" flag is not raised.The other end of the flag is
State::prepare_foratdraw.rs:2990-3087, which is where the depth target's state is decided:prepare_foris beforebindin the file (prepare_for2990-3087,bind3096-3380), so whatbindleaves behind is what the next draw'sprepare_forreads — which is what the thread-local's own comment says: "State::bind, then its targets' states".2. Where the stale value came from
readable()is reached from four places, and the one the pull request is about is the earliest-textures pass:readabledraw.rs:3789flush_for_bakedraw.rs:4643heap_texturesdraw.rs:5470,:5483table_entrydraw.rs:5788,:5799,:5889,:5961tablesand
heap_texturesis called fromearly_textures(draw.rs:3934,:4008,:4026), whichbind()calls atdraw.rs:3096— "Textures picked by index, before anything asks for them."So the chain the pull request names is real:
3. The order in
mainand the order in the pull requestbind()inmain:bind()in the pull request:In
main,readable()during the early-textures pass compares the texture it is bringing up to date against the previous draw's depth target, because the priming happens later in the same function. The value that is left over is(depth of the previous draw, false).4. What that costs
The comparison fails whenever the draw's depth target is not the previous draw's — the first draw, or any draw that switches depth target. In that case:
readable()takes theelsebranch: the depth texture is put inSHADER_RESOURCEonly, withoutDEPTH_READ, and the "a view of it was read" flag is never raised;(depth of this draw, false);prepare_forreadsread == falseand leaves the depth texture atDEPTH_READalone — the shader-readable state is not kept.With the pull request's order the marker is this draw's depth target for the whole of
bind(), soreadable()matches, sets the flag, and puts the texture inDEPTH_READ | SHADER_RESOURCE; the nextprepare_forthen seesread == trueand keeps it.This also matches the thread-local's documented meaning — "the depth target the draw only tests" — which in
mainwas not true during the early-textures pass.Two details make the change safe rather than merely different:
0unless!compute && self.depth_state == DEPTH_READ, so a compute dispatch or a draw that writes depth still matches nothing (readable()requirestested != 0). The condition is the same onemainused, so nothing that used to match stops matching.TESTED_DEPTH. The only reads indraw.rsare at:3075(prepare_for),:5444,:5462-:5472(table_entry),:5748-:5803(tables) and:5875-:5890(tables) — none of them inside the moved block.heap_buffers→heap_textures→readable()atdraw.rs:3298sits insidebind()after the new priming position, so it now sees this draw's marker as well; inmainit saw this draw's marker there, because the old priming sat just before it.5. What I did not check
leonida-gpudoes not build on Linux — upstream says so ("The crates are Windows-only today (leonida-gpu does not build on macOS); Linux follows #2577 if it lands"), andcargo test -p leonida-gpu --offline --no-runonmainfails with 12 errors, all inside#[cfg(test)]modules that call Windows-gated items (crate::host,crate::texture,crate::toggled!,crate::behind,line_on,host_resource). So the crate's own tests, the pull request's included, did not run here.DEPTH_READ/SHADER_RESOURCEon the depth texture was visible on screen (a shader reading the depth as a texture) needs a GPU and the title, and I have neither.cast_alias_ofpath.readable()only takes the branch above for a cast view of the depth texture (crate::resource::cast_alias_of), so the effect is limited to draws that read the depth through such a view. I did not enumerate which titles take that path.readable()callers.flush_for_bake,table_entryandtablesrun at other points in the frame; I checked that none of them readsTESTED_DEPTHin the moved block, and that the priming value is the same onemainused, but I did not trace every one of them against the new order.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 aznohonza on 2026-12-01T15:59:11Z.
Thanks for the PASS and for walking the chain. One nuance on the ordering: the targets' states are not the next draw's —
State::prepare_forcallsbindat its start and sets the depth target's state a few lines after it returns, all within the same draw. So the sequence is within oneprepare_for:bind→early_textures→readable→ (back inprepare_for) the depth state. That is exactly why the marker has to be primed before the pass: the same draw'sprepare_forreads it immediately afterwards, not on the next draw, and inmainit still held the previous draw's value.The rest matches: the early pass is the only
readablecaller before that state decision;table_entryandtablesrun insidebindafter the priming and were already covered. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by aznohonza on 2026-12-01T16:57:31Z.
Both points, on
31e64fcf0:The bake path — you are right:
early_texturesrunsheap_textureson the separatebake_list(draw.rs:3890-3911), so with the prime in place that upload list can record the combined-state barrier for the depth target as well. Same state, tracked per list, and the draw's own list still gets its transition inprepare_for; the comment at the prime now says exactly that.The test — I took your minimum, and the body now says it plainly rather than implying a run: the mechanism is GPU-only (the wrong state shows only in the barriers a list records), reverting the reorder leaves the suites green (627 passed, 0 failed here), and no device has drawn this with the change — no D3D12 device on this host, so it is read-verified only. I also said why a seam is not cheap on this code:
readable's arm needsresource::cast_alias_ofto name the tested depth and a list that recordsensure_state, anddraw.rs's tests are pure helpers, so a test would have to build an alias-cast texture plus a recording list, not just publish whatearly_texturessaw.If you want the seam anyway, the shape I would give it is: have
readable'sView::Shader/Srvarm withtested == sourcenote the pair it chose (the resource andDEPTH_READ | SHADER_RESOURCE) in aCelllikeTESTED_DEPTH, then atexture-side test that aliases a cast of a read-only depth target, callsreadablefor a picked view before anyprepare_for, and checks that published state — say the word and I will send it as its own PR rather than grow this one.Re-ran
cargo xwin check --target x86_64-pc-windows-msvc --workspace --all-targets --locked: clean. AI-assisted.Pull request closed