d3d12-gpu: prime a draw's tested depth before its picked textures are read #2866
Closed
leonida/mandy
aznohonza wants to merge 0 commits from
gpu/draw-depth-read into main
pull from: gpu/draw-depth-read
merge into: leonida:main
leonida:main
leonida:github-pr/3295
leonida:beta
leonida:docs/changelog-3483-3500
leonida:github-pr/3294
leonida:github-pr/2975
leonida:github-pr/2815
leonida:feat/gate-driver-start
leonida:feat/skip-pipelines-release
leonida:perf/lazy-compare
leonida:perf/mwaitx-wait
leonida:test/prio-ceiling-main
leonida:fix/ab-cpu-stdout
leonida:fix/gate-old-crashes
leonida:diag/ab-cpu
leonida:test/main-0312
leonida:github-pr/3291
leonida:diag/indirect-clamp-census
leonida:feat/barrier-enhanced
leonida:perf/texture-frame-ceiling
leonida:perf/dstorage-threads
leonida:fix/console-queue-dma-tables
leonida:fix/port-descriptors-record-parts
leonida:fix/frame-waits-fence
leonida:fix/console-sampler-words
leonida:fix/graphics-userdata-spill
leonida:fix/xg-reserved-layout-rotation
leonida:fix/barrier-console-flags
leonida:fix/switches-wait-test-child
leonida:test/list-value-waits
leonida:test/lot-gxdk-0412
leonida:fix/bvh-compacted-size
leonida:docs/i18n-quickstarts
leonida:feat/native-pipeline-slots
leonida:fix/clippy-leonida-gpu
leonida:fix/list-value-producers
leonida:fix/descriptor-page-straddle
leonida:fix/xmem-low-window
leonida:fix/ffx-jitter-never-zero
leonida:fix/run-stdout-closed
leonida:github-pr/2892
leonida:github-pr/2896
leonida:feat/warm-save-every
leonida:github-pr/2980
leonida:github-pr/2994
leonida:github-pr/3005
leonida:github-pr/3010
leonida:github-pr/3136
leonida:github-pr/3303
leonida:github-pr/3220
leonida:github-pr/3143
leonida:github-pr/3330
leonida:github-pr/3226
leonida:github-pr/3187
leonida:carry/3395
leonida:github-pr/2946
leonida:github-pr/2947
leonida:github-pr/2945
leonida:github-pr/2983
leonida:github-pr/3001
leonida:github-pr/3372
leonida:github-pr/3128
leonida:github-pr/3296
leonida:github-pr/2588
leonida:github-pr/3119
leonida:github-pr/3378
leonida:fix/crash-diagnostics
leonida:feat/dlss-mip-bias
leonida:fix/retired-out-of-order
leonida:fix/dxr-postbuild-barrier
leonida:fix/amd-relay-default
leonida:fix/removed-report-wait
leonida:feat/heap-twin
leonida:feat/vram-margin
leonida:github-pr/2472
leonida:github-pr/3123
leonida:github-pr/3088
leonida:assets/annonce-v1
leonida:github-pr/2794
leonida:github-pr/3300
leonida:github-pr/3304
leonida:github-pr/3308
leonida:github-pr/3367
leonida:github-pr/3369
leonida:fix/launcher-dlss-diagnostics
leonida:github-pr/3269
leonida:github-pr/2957
leonida:github-pr/3205
leonida:fix/crash-zero-guard
leonida:github-pr/3185
leonida:github-pr/3099
leonida:github-pr/3104
leonida:github-pr/3348
leonida:github-pr/3379
leonida:github-pr/3377
leonida:perf/darner-title-priority-ceiling
leonida:github-pr/3376
leonida:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
leonida:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
leonida:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
leonida:github-pr/3373
leonida:github-pr/3368
leonida:github-pr/3198
leonida:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
leonida:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
leonida:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
leonida:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
leonida:github-pr/3346
leonida:github-pr/3375
leonida:github-pr/2958
leonida:github-pr/3151
leonida:github-pr/3307
leonida:github-pr/3278
leonida:github-pr/3374
leonida:fix/runtime-test-conservative-fixtures
leonida:github-pr/3366
leonida:ci/shard-semantic-linux
leonida:github-pr/3354
leonida:docs/fg-limit-fps-switch
leonida:github-pr/3365
leonida:fix/read-file-hard-link
leonida:github-pr/3364
leonida:fix/book-slot-heap-never-read
leonida:github-pr/3361
leonida:github-pr/3263
leonida:github-pr/3360
leonida:github-pr/3353
leonida:github-pr/3331
leonida:fix/alias-later-queued-submissions
leonida:github-pr/3352
leonida:github-pr/3228
leonida:github-pr/3349
leonida:claude/gallant-sagan-aamatr
leonida:github-pr/3200
leonida:github-pr/3132
leonida:github-pr/3323
leonida:github-pr/3332
leonida:github-pr/3340
leonida:github-pr/3311
leonida:github-pr/3334
leonida:github-pr/3297
leonida:github-pr/3256
leonida:github-pr/2567
leonida:github-pr/2851
leonida:d3d12/agility-sdk-621
leonida:github-pr/3164
leonida:github-pr/3114
leonida:github-pr/3050
leonida:github-pr/3113
leonida:github-pr/3326
leonida:github-pr/3144
leonida:github-pr/2642
leonida:github-pr/3072
leonida:github-pr/2789
leonida:github-pr/2643
leonida:github-pr/3009
leonida:github-pr/3035
leonida:github-pr/3031
leonida:github-pr/2686
leonida:github-pr/2834
leonida:github-pr/3321
leonida:github-pr/3325
leonida:github-pr/3290
leonida:github-pr/3324
leonida:github-pr/3130
leonida:claude/fervent-curie-m19lnr
leonida:github-pr/3309
leonida:github-pr/3305
leonida:github-pr/3306
leonida:github-pr/3302
leonida:github-pr/3301
leonida:github-pr/3345
leonida:github-pr/3211
leonida:github-pr/3027
leonida:github-pr/3286
leonida:github-pr/3235
leonida:github-pr/3265
leonida:github-pr/3275
leonida:codex/skip-no-hit-ray-passes
leonida:github-pr/2823
leonida:github-pr/2911
leonida:github-pr/3262
leonida:github-pr/3125
leonida:github-pr/3206
leonida:github-pr/3250
leonida:github-pr/3238
leonida:github-pr/3188
leonida:github-pr/2952
leonida:github-pr/3217
leonida:github-pr/3168
leonida:t3/preserve-fsr-sdk-restart-path
leonida:t3/propagate-host-failure-hresult
leonida:github-pr/3209
leonida:t3/launcher-custom-parameters
leonida:github-pr/3207
leonida:github-pr/3183
leonida:github-pr/3160
leonida:github-pr/3201
leonida:ci/less-to-run
leonida:github-pr/2930
leonida:github-pr/2797
leonida:github-pr/2999
leonida:perf/native-compute-shader-forms
leonida:github-pr/3191
leonida:github-pr/3011
leonida:github-pr/3019
leonida:github-pr/2996
leonida:gh-readonly-queue/main/pr-2789-45dc41f95c1cd0dcc29c2c50582e0e9af8a77259
leonida:claude/blissful-franklin-ukp7ag
leonida:github-pr/3141
leonida:github-pr/3156
leonida:github-pr/3150
leonida:github-pr/3153
leonida:github-pr/3163
leonida:github-pr/3142
leonida:github-pr/3139
leonida:github-pr/2841
leonida:github-pr/3069
leonida:github-pr/3098
leonida:github-pr/3107
leonida:github-pr/3071
leonida:github-pr/2774
leonida:github-pr/2889
leonida:github-pr/3075
leonida:github-pr/2661
leonida:github-pr/3064
leonida:github-pr/3054
leonida:github-pr/3049
leonida:github-pr/2855
leonida:github-pr/2943
leonida:github-pr/3016
leonida:github-pr/2887
leonida:github-pr/3006
leonida:github-pr/3003
leonida:github-pr/2993
leonida:notes/card-time-and-plant-flicker
leonida:github-pr/2989
leonida:feat/vulkan-rs
leonida:github-pr/2985
leonida:github-pr/2951
leonida:github-pr/2982
leonida:github-pr/752
leonida:github-pr/792
leonida:github-pr/2877
leonida:github-pr/2654
leonida:github-pr/2893
leonida:github-pr/2948
leonida:github-pr/2944
leonida:github-pr/2919
leonida:github-pr/2915
leonida:archive/ci/less-to-run/20261202T1121Z
leonida:github-pr/2800
leonida:github-pr/2822
leonida:github-pr/2833
leonida:github-pr/2840
leonida:github-pr/2831
leonida:github-pr/2832
leonida:github-pr/2801
leonida:github-pr/2890
leonida:github-pr/2891
leonida:github-pr/2664
leonida:github-pr/2807
leonida:rt-call-trace
leonida:github-pr/2755
leonida:perf/ahead-split
leonida:github-pr/2587
leonida:github-pr/2698
leonida:github-pr/2609
leonida:github-pr/3219
leonida:docs/readme-runtime-proposal
leonida:github-pr/2017
leonida:github-pr/1741
leonida:github-pr/1667
leonida:codex/coord-claim-check-20261124-7e48
No reviewers
Assignees
Clear assignees
No assignees
Labels
Clear labels
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
enhancement
New feature or request
missing claim
Needs a claim in #14
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
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
Projects
Clear projects
No items
No project
Milestone
Clear milestone
No items
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
leonida/mandy!2866
Loading…
Reference in a new issue
No description provided.
Delete branch "gpu/draw-depth-read"
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?
Pull request closed
This pull request cannot be reopened because the branch was deleted.
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.
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.
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.
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.
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.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.