Skip to content

D3D12.X: barriers from the undefined layout can start from UNDEFINED natively - #3558

Draft
KDenKC wants to merge 1 commit into
xcl-dev:mainfrom
KDenKC:fix/barrier-undefined-native
Draft

KDenKC wants to merge 1 commit into
xcl-dev:mainfrom
KDenKC:fix/barrier-undefined-native

Conversation

@KDenKC

@KDenKC KDenKC commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

barrier_groups (list call #146) turns a title's enhanced barrier from the undefined layout into a legacy transition whose before-state is the last state it recorded for the texture (LAST). It records those states in the order the title records its lists, but the GPU runs them in the order they are submitted. A title that records on several threads therefore gets before-states the texture is not in when the list runs (the debug layer's errors 527/538), and what the GPU does with them is undefined. thehollowedone found this first, on #3094 (point 2 and the two-list regression); imnicho's answer there is that it needs a fix outside that PR.

A barrier with a layout before it names its own state, so the moves from the undefined layout are the only place barrier_groups depends on recording order. XCL_BARRIER_UNDEFINED_NATIVE=1 (default off) sends them as enhanced texture barriers with LayoutBefore = UNDEFINED, which need no before-state at all. With the switch on and enhanced barriers on the device, barrier_groups no longer depends on the order lists are recorded in. A state with no single layout, or a device without enhanced barriers, goes out as before.

Scope. This does not resolve states at submission. A complete fix keeps each recording's transitions private until submission and resolves them at the list's exact place in the queue's order, KickoffX and repeat submissions included, because the barriers of a closed list cannot be rewritten at ExecuteCommandLists (thehollowedone, #3094). This change only takes the moves from the undefined layout out of that dependency: they are the one place barrier_groups uses a before-state it recorded itself rather than one the title named. alias_copy's own record of states (#3094) keeps the same limit.

Related: #3489, #3094, #3527 (seeds texture states at creation; it should land first)

Based on main 9dfbe3e (it merges into current main without conflicts): see Regression run.

What is in it (one commit)

  • barrier_groups.rs: the FROM_UNDEFINED mark on moves from the undefined layout when the switch is on (native_undefined), native_layout (a state's one enhanced layout and access), and a marked move made even when the last state is unknown or already the target.
  • list_x.rs: resource_barrier_x's flag filter keeps the mark only for barrier_groups' own call (a thread-local scope, as_groups); send_barriers sends marked runs through ID3D12GraphicsCommandList7::Barrier and falls back to legacy transitions (as_legacy: the mark off, the moves with no before-state of their own left out); a_no_op keeps marked same-state words.
  • depth_uav.rs: plan_with keeps a marked move that parking turns into the same state before and after.
  • Tests: a_barrier_from_undefined_starts_from_the_state_in_submission_order (two lists recorded out of submission order: the legacy before-state is the recording-order one; after thehollowedone's two-list regression and their discriminator on D3D12.X: a sampled texture in the memory of a written one of another format is copied from it (XCL_ALIAS_COPY=1) #3094, one recording read under two submission orders), a_native_move_from_undefined_is_kept_without_a_before_state, a_state_from_the_undefined_layout_has_one_enhanced_layout, the_from_undefined_mark_passes_the_flag_filter_only_for_barrier_groups, a_marked_move_with_no_before_state_goes_natively_or_not_at_all, a_marked_move_into_unordered_access_of_a_parked_buffer_stays. All CPU-only.

Tested

  • NVIDIA GeForce RTX 3090 24 GB (VM), driver 621.24, Intel i7-10700T, Windows 11 build 26300.9974, Rust 1.99.1, debug:
    • The three tests of the kept moves, red before that part of the change (cac8b6ad): test result: FAILED. 140 passed; 3 failed; 0 ignored; 0 measured; 2632 filtered out; green with it (3731d0e7): test result: ok. 143 passed; 0 failed; 0 ignored; 0 measured; 2632 filtered out (the tests of barrier_groups, list_x, depth_uav).
    • cargo test --workspace --no-fail-fast -- --test-threads=4 (leaving out the 3 uma_driver tests that need a GPU upload segment this VM does not have): the same commit on main d0832e6 (42946bf8): 2888 passed, 5 failed, 51 ignored (a first run lost the lib test binary to an access violation in the parallel run; these counts are from its complete rerun); main d0832e61 on the same machine: 2882 passed, 5 failed, 51 ignored. This head bd888cf7 (the same commit on main 9dfbe3e): running, its counts will be added here. The same 5 fail on both (title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, the 3 title_textures::tests::stream_recheck_*, test_title_runs_and_game_folder_is_untouched). SKIPPED: 28 on both, the same tests (22 no wave64, 2 no heap windows, 3 no debug layer, 1 driver cache cannot be disabled).
  • The retail title, switch on: pending (twelve runs, six with the switch and six without, scored lit / dark: the run-to-run dark areas reported on Newer retail title (TitleID 7952987A): reaches world loading, then the title's GPU watchdog ("GPU Loc Sig") after ListX #101 stub #505 are the case this is meant for).

Regression run
Pending: one run of this head, paired with a main 9dfbe3e run of the same recipe (held Enter, 600 s, the retail title) that entered the world. main d0832e6 stays in the title's menus with held Enter on our rig (both runs of this change on it, and one of main d0832e6 alone; no device removal), so the pair uses 9dfbe3e.

Not tested

  • The retail title with the switch on (above): whether it changes what the title shows is not measured yet.
  • A device without enhanced barriers (the fallback is the code path of before; covered by the tests only).
  • AMD; Intel.

AI use

  • Claude Code (Claude Opus 5.5, Claude Sonnet 5.5), Codex (GPT-6.2-Sol), Antigravity (Gemini 4 Neon, Gemini 3.8 Flash): an agent swarm wrote, tested, reviewed and described this change; GPT-6.2-Sol's review found the parked-buffer case.

🤖 Generated with Claude Code
https://claude.ai/code/session_013oPUFCA9W9F8Qb5MPK33bR

…natively

barrier_groups turns a title's enhanced barrier from the undefined layout
into a legacy transition whose before-state is the last state it recorded
for the texture (LAST), in the order the title RECORDS its lists. The GPU
runs the lists in the order they are SUBMITTED, so a title that records on
several threads gets before-states the texture is not in (the debug
layer's errors 527/538); what the GPU then does is undefined. A barrier
with a layout before it names its own state, so these moves are the only
place barrier_groups depends on recording order.

XCL_BARRIER_UNDEFINED_NATIVE=1 (default off) sends them as enhanced
texture barriers with LayoutBefore UNDEFINED, which need no before-state.
They carry a FROM_UNDEFINED mark that only barrier_groups' own call passes
through resource_barrier_x's flag filter (a thread-local scope). A marked
move is made even when the last state is unknown or already the target,
and is kept where a legacy one would be a no-op (resource_barrier_x,
depth_uav's parking); without enhanced barriers, or for a state with no
single layout, the run goes out as before with the mark off and such moves
left out.

Tests: a_barrier_from_undefined_starts_from_the_state_in_submission_order
records two lists out of order and shows the legacy before-state is the
recording-order one (after thehollowedone's two-list regression on xcl-dev#3094);
the mark, the flag filter, the scope and the kept moves have their own.

Related: xcl-dev#3489, xcl-dev#3094

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 1, 2026
@thehollowedone

Copy link
Copy Markdown
Contributor

Source review on head bd888cf7: the opt-in native path sets SyncBefore = NONE for every marked texture barrier. The title's before-sync value is available in barrier_groups, but only its split marker is used. For example, a direct list can clear a render target, then request UNDEFINED → COPY_DEST with SyncBefore = RENDER_TARGET before overwriting that texture. The emitted barrier drops the clear's synchronization. Microsoft's enhanced-barrier specification permits SyncBefore = NONE only when there has been no earlier access or barrier to the subresource in that ExecuteCommandLists scope. The global UAV barrier emitted before this path does not change that requirement.

Please carry the title's before-sync scope into the native barrier, or conservatively use SYNC_ALL while keeping AccessBefore = NO_ACCESS for the undefined layout. Add a regression that inspects the final PC D3D12_TEXTURE_BARRIER for a clear followed by this transition; the current tests check the intermediate marked words and cannot catch this field.

This is a source and API-contract finding. I have not run the opt-in path on a GPU or title. AI use: Codex (GPT-6.2 Sol) and Codex (GPT-6.2 Astra) traced the path against Microsoft's specification.

@thehollowedone

Copy link
Copy Markdown
Contributor

Source review on head bd888cf7: the opt-in path also needs to account for legacy/enhanced barrier interop on the same texture. In the PR's two-transition example, list A moves one texture UNDEFINED → RENDER_TARGET → SHADER_RESOURCE. send_barriers sends the first move through enhanced Barrier and the second through legacy ResourceBarrier, with no RENDER_TARGET → COMMON enhanced handoff. This remains a problem even if the already reported SyncBefore = NONE is corrected.

Microsoft's enhanced-barrier interop contract requires a texture in an enhanced layout to be placed in D3D12_BARRIER_LAYOUT_COMMON, with preceding accesses finished and flushed, before a legacy ResourceBarrier references it. The reverse crossing also needs a legacy transition to D3D12_RESOURCE_STATE_COMMON before an enhanced barrier; the example starts with a prior legacy shader-resource state. Please keep a subresource in one barrier model across these moves, or perform the required COMMON handoffs in execution order. A recording-order state guess would reintroduce the problem this PR addresses.

A deterministic regression can use a fresh ordinary texture and capture the final PC calls, not only the intermediate marked words: on a direct list with enhanced barriers supported, issue UNDEFINED → RENDER_TARGET → SHADER_RESOURCE for the same subresource and assert that no enhanced-to-legacy crossing occurs without a synchronized COMMON handoff. Also cover a preceding explicit legacy non-COMMON state before an enhanced move. I traced the source and API contract; I have not run this path on a GPU or title.

AI use: Codex (GPT-6.2 Sol) checked the emitted sequence and Microsoft specification; Codex (GPT-6.2 Astra) independently checked the interop invariant.

@KDenKC

KDenKC commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@thehollowedone thank you for both reviews; both are right on the API contract.

  1. The native path set SyncBefore = NONE for every marked barrier, so a clear followed by a move from the undefined layout lost the clear's sync.
  2. A texture moved with an enhanced barrier and then with a legacy one in the same list (or the other way round) needs a hand-off through COMMON in the order the GPU runs them, and the first move of a texture whose legacy state was set by another list cannot be known when the list is recorded.

Rather than keep patching this opt-in path, the fixes go into the submit-time series announced on #3094 (resolving barrier states at submission): the title's before-sync carried into each barrier, one barrier model per subresource with the COMMON hand-offs placed in execution order, and XCL's own copies around title textures (depth twins, alias copies) following the same rule. Its part A (the harness for submission-order regressions, test-only) opens shortly, and your cases will be among its regressions.

This PR stays a draft with its switch off until then, and will be closed in favour of that series, with credit to you, once it covers this.

AI use: an agent swarm: Claude Opus 5.5, Claude Sonnet 5.5, GPT-6.2-Sol, Gemini 4 Neon and Gemini 3.8 Flash.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Review (head bd888cf, diff read, not a merge-check; draft with PR description red, so not built or run here):

  • Half of the change never reaches the runtime. barrier_groups::transitions still ends its retain_mut with w[5] != w[6] (barrier_groups.rs ~478), so the marked same-state barriers transitions_of now produces (last state unknown, or already the target) are dropped there and never reach resource_barrier_x. A never-seen texture, or one whose recording-order state already equals the target, still gets no barrier, as on main. The new tests call transitions_of, a_no_op and plan_with directly, never transitions(), so they don't catch it.
  • barrier_groups.rs ~50 still reads "LOCAL, not for a PR".
  • The thread_local! at list_x.rs ~6503 sits between stencil_copies_on's doc comment and the function, giving a new unused doc comment warning (~6498) in CI; at ~7506 the doc comment of the_from_undefined_mark_passes_the_flag_filter_only_for_barrier_groups is attached to the test before it.
  • Not yet measured: a run with XCL_BARRIER_UNDEFINED_NATIVE=1, a debug-layer run with enhanced and legacy barriers mixed on one resource, this head's suite counts, and the regression run.

Fine: with the switch off the barriers sent are unchanged (one unmarked run goes to the same legacy call, plus one small Vec per call). Native barriers are gated on EnhancedBarriersSupported with a legacy fallback, Barrier is slot 80 of ID3D12GraphicsCommandList7, the native_layout table is right, a title's own flags can't carry the marker, runs keep their order, and there's no overlap with #3238 / #839 (they change device_x::layout_state).

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Triage (#1607) – head bd888cf

No open PR makes the same change. #3527 also deals with barrier_groups' moves from the undefined layout (#3489), but it starts them from the texture's creation state, by default. This PR sends them as enhanced barriers from LAYOUT_UNDEFINED behind XCL_BARRIER_UNDEFINED_NATIVE=1, which also removes the dependency on recording order. These are different approaches. #3559 is the same author's test harness for that plan, not an overlap.
One problem in the diff: the doc comment of barrier_groups::FROM_UNDEFINED starts with "LOCAL, not for a PR", left over from a local branch. The slot used for ID3D12GraphicsCommandList7::Barrier (80) is right.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@gut0to gut0to left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted static review: I used an AI assistant to inspect this diff and checked the enum values against Microsoft documentation.

@KDenKC

KDenKC commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@gut0to thanks for checking the layouts so closely; that mapping is exactly where a mistake would void every run with the switch on. I compared it against the SDK, and the values match: in d3d12.h (and the windows crate 0.62.2 that XCL builds with) D3D12_BARRIER_LAYOUT_COMMON and _PRESENT are both 0, _GENERIC_READ 1, _RENDER_TARGET 2, _UNORDERED_ACCESS 3, _DEPTH_STENCIL_WRITE 4, _DEPTH_STENCIL_READ 5, _SHADER_RESOURCE 6, _COPY_SOURCE 7, _COPY_DEST 8, _RESOLVE_SOURCE 9, _RESOLVE_DEST 10 (and _UNDEFINED -1), and the access bits used next to them are _RENDER_TARGET 0x8, _UNORDERED_ACCESS 0x10, _DEPTH_STENCIL_WRITE 0x20, _DEPTH_STENCIL_READ 0x40, _SHADER_RESOURCE 0x80, _COPY_DEST 0x400, _COPY_SOURCE 0x800, _RESOLVE_DEST 0x1000, _RESOLVE_SOURCE 0x2000, which is what native_layout returns. Writing them as literals invites exactly this doubt, though: this PR stays frozen (the work moved into the submit-time series, #3559 and after), and where the native route enters that series it uses the SDK constants by name, with a test that pins each state's (layout, access) pair to them.

AI use: an agent swarm: Claude Opus 5.5, Claude Sonnet 5.5, GPT-6.2-Sol, Gemini 4 Neon and Gemini 4 Hydrogen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants