Skip to content

d3d12-gpu: record enhanced barriers as the legacy ones they stand for - #2962

Open
aznohonza wants to merge 6 commits into
ProjectLeonida:mainfrom
aznohonza:gpu/record-enhanced-barriers
Open

aznohonza wants to merge 6 commits into
ProjectLeonida:mainfrom
aznohonza:gpu/record-enhanced-barriers

Conversation

@aznohonza

Copy link
Copy Markdown
Contributor

Re-created from the original #2906. aznohonza/mandy-contrib was made private at 21:41 UTC on 2026-12-01, which detached the fork and permanently closed its pull requests; GitHub refuses to reopen them (422 state cannot be changed). Same branch, same head ba1828afe3600716e20c81cc9ce2e4c77523b5a3. The original review thread is at #2906.


replay/record.rs listed ID3D12GraphicsCommandList7::Barrier as not recorded, so a stream that used enhanced barriers lost those ordering points when it was replayed. This hooks the method and records each group as the legacy barriers the runtime itself uses for the same wait:

  • Global → the alias + UAV pair play::wait_for_all falls back to on a device without enhanced barriers.
  • Texture → a transition between the D3D12_RESOURCE_STATES its layouts stand for (layout_state), with the subresource the range names when it names one. A same-state barrier becomes the UAV barrier it stands for, or nothing at all: a real device rejects a transition to the state it is already in (E_INVALIDARG).
  • Buffer → a UAV barrier when the access after is UAV, else the alias + UAV pair (a legacy barrier cannot name another buffer access).

The wire already carries Cmd::Barriers, and playback already issues those legacy barriers, so nothing changes in play.rs or wire.rs. The enhanced sync/access masks order the same accesses with less; the recorded legacy barriers are the same waits, never fewer.

One stated shortcut: a texture range naming more than one subresource is recorded as the whole resource, since a legacy transition carries one subresource. That is conservative; a title that keeps two subresources of one texture in different layouts at once would need the wire extended. The code says so where it happens.

Verification: layout_state covers every layout and both queue-specific forms; enhanced_records_its_texture_and_buffer_barriers drives enhanced() itself with texture and buffer groups, same_state_texture_barriers_are_a_wait_or_nothing covers the same-state forms, and one_subresource_ranges_flatten_to_that_subresource the range forms.

cargo xwin build --target x86_64-pc-windows-msvc -p leonida-gpu --lib --tests --features diagnostics
wine .../leonida_gpu-<hash>.exe enhanced_layouts_record
# test replay::record::tests::enhanced_layouts_record_as_the_states_they_stand_for ... ok

The workspace also checks with the default features (cargo xwin check --target x86_64-pc-windows-msvc --workspace --all-targets --locked). Replay is behind the diagnostics feature, so that unit test runs with cargo test -p leonida-gpu --features diagnostics --lib; there is no D3D12 device on this Linux host, so the recorded-and-replayed path itself is compile/read-verified here and exercised by the crate's device tests on Windows. No game data, no new switch.

Claimed in #2570 (RS-RECORD-ENHANCED-BARRIERS). AI-assisted.

`ID3D12GraphicsCommandList7::Barrier` was in the recorder's not-recorded
list, so a recorded stream lost those ordering points on replay. Hook it
and record each group as the legacy barriers the runtime already uses for
the same wait: a global one as the alias + UAV pair `play::wait_for_all`
falls back to, a texture one as a transition between the states its
layouts stand for, a buffer one as a UAV barrier on the whole buffer
(D3D12 requires offset 0 and the whole size).

A texture subresource range that names more than one subresource is
recorded as the whole resource: a legacy transition carries one
subresource. That is conservative and stated in the code, with the
condition to revisit it.

The mapping's test covers every layout and both queue-specific forms.
A transition to the state a resource is already in is invalid
(E_INVALIDARG, even with the debug layer off), and enhanced barriers
often keep the layout: two UAV dispatches are the common case. Record
the UAV barrier that wait stands for instead, and nothing at all when
both layouts stand for the same other state. This matches what `join`
does when two transitions cancel out.

A range with a non-zero `NumMipLevels` can still name exactly one
subresource. When all three counts are 1, flatten it to that
subresource with the resource's own mip and array sizes instead of
recording the whole resource. The subresource-index form is unchanged.

Both rules are a pure helper, tested without a device.
KelboLive's re-review: a buffer barrier whose access after is not UAV
was recorded as a bare UAV barrier, which orders nothing for, say,
COPY_DEST -> COPY_SOURCE; it now records the same global alias + UAV
pair as a global barrier. A zero-barrier group no longer makes a slice
from its (possibly null) array either.

The new test drives `enhanced()` itself through a texture group (which
also catches a swapped LayoutBefore/LayoutAfter at the call site) and a
buffer group in both forms.

AI-assisted: yes
@andrew-ai

Copy link
Copy Markdown

The partial-range shortcut changes the recorded state of untouched mips even when all subresources start in the same layout. I reproduced it at ba1828a using the existing two-mip, two-array-slice fixture in enhanced_records_its_texture_and_buffer_barriers.

  1. Start all four subresources in UAV. Transition both mips of array slice 1 to shader resource.
  2. Transition mip 0 of array slice 0 from UAV to copy destination.

I passed the range through enhanced() and round-tripped its Cmd::Barriers through the current wire encoder and decoder. Applying the decoded first command to a four-entry state table yields [SRV, SRV, SRV, SRV]. The second recorded transition expects UAV at index 0. Encoding the first command as two existing legacy transitions (indices 2 and 3) round-trips intact and yields [UAV, UAV, SRV, SRV], matching the second transition.

For a known texture descriptor, expanding a partial range into per-subresource transitions fits the existing wire format. ALL can still represent a range that actually covers the whole resource. This was a CPU-side recorder/codec test with synthetic inputs; it did not submit commands to a GPU. Codex-assisted for #2570.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same head (ba1828afe) as #2906, which I reviewed through three rounds — the blocker there (same-state texture transitions) was fixed and verified on a real device, and merge-tree against current main is clean. No re-review needed. The only remaining item is non-blocking: a buffer barrier with AccessBefore = COPY_DEST, AccessAfter = UNORDERED_ACCESS still records a bare Uav, which does not order the copy. Approved in substance.

…s pair

A legacy UAV barrier orders UAV accesses only, so a bare Uav is enough only
when the access before is UAV (or NONE). A copy before it records the global
alias + UAV pair, as the non-UAV case already did. The test covers
COPY_DEST -> UAV, NONE -> UAV, and COPY_DEST -> COPY_SOURCE; the six
replay::record::tests pass under Wine.

AI-assisted.
@aznohonza

Copy link
Copy Markdown
Contributor Author

Fixed in a4a26ec63. The bare Uav is now recorded only when AccessAfter is UAV and AccessBefore is UAV or NONE; a copy before it records the global alias + UAV pair, the same as the non-UAV case. The test covers COPY_DEST -> UAV (alias pair), NONE -> UAV (bare), and COPY_DEST -> COPY_SOURCE (alias pair); the six replay::record::tests pass under Wine. Thanks for catching it.

AI-assisted.

@merzleckeier

Copy link
Copy Markdown
Contributor

The current recorder still widens a partial 3D mip range to ALL.

I reran a4a26ec6320e52f2a3536b3b7be2d6a47c8773d4 on main 1abef08d with #2975, whose files are separate from the recorder. All six stock recorder tests passed under Wine, including the updated buffer COPY_DEST to UAV alias-plus-UAV case.

The earlier array-slice finding also applies to a known 3D texture. My synthetic descriptor used depth 16 and four mips. Asking the production converter to transition only mips 1 and 2 from UAV to shader resource produced Transition { sub: ALL, before: 8, after: 192 }. The next request for untouched mip 0 still produced Transition { sub: 0, before: 8, after: 1024 } (UAV to copy destination). Applying the first recording to the four mips puts mip 0 in shader resource, which disagrees with that second before-state.

This fixture checks the CPU recorder with synthetic input. AI-assisted, independently verified against the raw results.

@aznohonza

Copy link
Copy Markdown
Contributor Author

Thanks for the 3D case — you are right, and it is a real limitation, not a fixture artifact: a partial mip range of a 3D texture is several subresources (one per mip), and the legacy encoding needs one transition per subresource, not ALL. Collapsing to ALL writes the after-state into mips the range never named, which is exactly why the next barrier's before-state disagrees.

That needs texture_barrier to return a list rather than one barrier and a per-mip split for 3D ranges, so I would rather not squeeze it into this reviewed head. I will open a separate item for it (partial 3D mip ranges: one legacy transition per mip, with your synthetic depth-16/four-mip fixture as the test) and link this PR. The array-slice half you found earlier is the same flattening decision.

AI-assisted.

@aznohonza

Copy link
Copy Markdown
Contributor Author

The partial-range flattening is fixed in replay/partial-mip-split (b1f4a8f6d, stacked on this branch until it lands; I will retarget it to main and open the PR then, as promised above).

What changed in texture_barrier: it returns the list of legacy barriers the enhanced texture barrier stands for. A single-subresource range keeps the index subresource gives; a partial range enumerates its mips × array slices × planes into one transition each; a range that covers the whole resource stays one ALL transition.

The test is the synthetic depth-16, four-mip 3D fixture from @merzleckeier: mips 1–2 → transitions for subresources 1 and 2, untouched mip 0 → subresource 0, whole-resource range → ALL, and the array-slice case → subresources 2 and 4. All seven replay::record::tests pass under Wine; with the enumeration replaced by a single ALL transition the new test fails.

AI-assisted.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants