d3d12-gpu: prime a draw's tested depth before its picked textures are read #2866
Closed
github-bot
wants to merge 2 commits from
github-bot/gh-2866 into main
AGit
pull from: github-bot/gh-2866
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!2866
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@aznohonza on GitHub · 2026-12-01 11:19 UTC · original
aznohonza:gpu/draw-depth-read→main· +15 −8 · 1 files · 2 commits · closed without mergingState::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.
@merzleckeier on GitHub · 2026-12-01 11:59 UTC · original
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.
@aznohonza on GitHub · 2026-12-01 12:00 UTC · original
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.
@KelboLive on GitHub · 2026-12-01 12:45 UTC · original · reviewed
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.@imShangLi on GitHub · 2026-12-01 15:21 UTC · original
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.
@aznohonza on GitHub · 2026-12-01 15:59 UTC · original
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.@aznohonza on GitHub · 2026-12-01 16:57 UTC · original
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