Skip to content

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

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

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

Conversation

@aznohonza

Copy link
Copy Markdown
Contributor

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.
  • Buffer → a UAV barrier on the whole buffer (D3D12 requires offset 0 and the whole size).

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: the layout_state test covers every layout and both queue-specific 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.
@Kal0s-design

Copy link
Copy Markdown
Contributor

Checked on native Windows 11 with a real D3D12 device (rustc 1.98.1, MSVC target), since the run in the description is under Wine.

Head e18eeaf6e merged with main d2a564b66 (clean merge):

  • cargo test -p leonida-gpu --features diagnostics --lib replay::: 25 passed, 0 failed. enhanced_layouts_record_as_the_states_they_stand_for passes.
  • The test catches a wrong mapping. With 21 => 6 changed to 21 => 1 (direct-queue shader resource mapped as generic read), it fails with left: 2755, right: 192.

One case that would break playback: a texture barrier whose two layouts map to the same state.

Enhanced barriers often keep the layout and change only sync/access. Two UAV dispatches on one texture are the usual case: LayoutBefore == LayoutAfter == UNORDERED_ACCESS. Other pairs map to the same state too: UNDEFINED -> COMMON (both 0) and DIRECT_QUEUE_SHADER_RESOURCE -> SHADER_RESOURCE (both 0xC0). enhanced() records each of these as Barrier::Transition with before == after, and playback issues it through gpu::transition.

I issued that legacy barrier on a real device: a 64x64 RGBA8 texture, then one ResourceBarrier transition and Close().

transition Close() debug layer
UAV -> SRV (control) Ok no message
UAV -> UAV E_INVALIDARG ResourceBarrier: Before and after states must be different.
RT -> RT E_INVALIDARG same message
COMMON -> COMMON E_INVALIDARG same message
UAV -> UAV, debug layer off E_INVALIDARG (none)

So the replayed list fails to close even without the debug layer. edit.rs join only folds a transition into an earlier one on the same resource; a lone same-state transition goes through unchanged.

A possible fix: when layout_state(before) == layout_state(after), record no transition, and record Barrier::Uav { res } when the layout is UNORDERED_ACCESS (the wait the barrier stands for). That matches what join already does when two transitions cancel out on UNORDERED_ACCESS. A test that runs enhanced() on a UAV -> UAV texture group would cover it.

Context: on main I found no host code that calls Barrier / sets LayoutBefore, so this only matters once a recorded stream contains enhanced barriers. That is the case this pull request is for, though.

Not checked: a full record/replay round trip, and the game.

GPUs tested: the default hardware adapter (device-level ResourceBarrier / Close check only).

AI-assisted: yes (Claude Code).

@merzleckeier

Copy link
Copy Markdown
Contributor

Recorder check for equivalent single-subresource barriers at e18eeaf6e + d2a564b66 (Win64, Wine 11.22 / VKD3D-Proton 3.1.0). The 25 existing replay:: tests passed in both adapter configurations.

A separate test calls ID3D12GraphicsCommandList7::Barrier, closes the list and decodes the captured commands. The synthetic RGBA8 texture has two mips and two array slices. It specifies mip 1, slice 1, plane 0 in two equivalent ways. With NumMipLevels = 0, the flattened index 3 records transition subresource 3. With all three counts set to 1, the explicit range records 0xffffffff (all subresources). Main emits no recorded barrier in either case. The results match on AMD Radeon (RADV 26.3.1) and GeForce RTX 4070 SUPER (620.73.4 as reported by Wine).

NumMipLevels > 0 selects the range form even when that range contains only one subresource. A local trial flattened the 1×1×1 range using the texture descriptor and recorded subresource 3 in both forms on both adapters. The test checks the captured commands before queue submission.

MEASURED with a synthetic texture. AI-assisted (Codex). D3D12 subresource-range documentation

@KelboLive KelboLive 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.

Blocking: the texture path records a transition even when both layouts stand for the same state, and D3D12 rejects that at Close().

layout_state collapses UAV→UAV to 0x8→0x8, UNDEFINED→COMMON to 0x0→0x0, and DIRECT/QUEUE SHADER_RESOURCE→SHADER_RESOURCE to 0xC0→0xC0 (record.rs:2359-2365), and enhanced() emits a Transition unconditionally (:2420-2431). On a 4070 Ti SUPER I get E_INVALIDARG (0x80070057) for 0x8→0x8, 0x4→0x4, 0x0→0x0 and 0xC0→0xC0, with and without the debug layer (debug says "Before and after states must be different"), while the enhanced COMMON→UAV / UAV→UAV pair closes Ok and a control 0x8→0xC0 is fine. play.rs's close propagates the error (:2402-2414, "a list of the frame could not be recorded again"), so one such barrier aborts the whole frame's replay. Before this PR the call was merely skipped (noted_entry, hook.rs:231) and the capture still replayed — so this regresses the UAV→UAV case the PR exists for.

edit::join only folds a pair of matching transitions (edit.rs:126-155), so a lone same-state barrier survives, and merge_barriers is off by default anyway.

Fix: emit no transition when before == after, plus Barrier::Uav when the layout is UAV. And test enhanced() itself — replacing after: layout_state(b.LayoutAfter) with b.LayoutBefore still passes all 25 replay:: tests, which is exactly why CI cannot see this.

Second, smaller issue: a buffer barrier becomes a UAV barrier even when its access masks are copy/vertex/index/…, and a legacy UAV barrier only orders UAV accesses — so "never fewer" does not hold there. A global alias+UAV flush would be the conservative legacy stand-in.

Minor: from_raw_parts(ptr, 0) per group (:2403, :2418, :2436) is UB if NumBarriers == 0 with a null pointer; the new test only asserts layout_state values (no PRESENT/SHADING_RATE_SOURCE/video); and the "tested via cargo test --test replay" claim does not exercise enhanced() at all — tests/replay.rs builds frames directly.

The GLOBAL mapping is right: Alias(NONE,NONE)+Uav(NONE) matches the non-enhanced fallback in wait_for_all (play.rs:2342-2370, :3392, :3376). Merge is clean against current main, and no other open PR touches replay/record.rs.

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.
@aznohonza

Copy link
Copy Markdown
Contributor Author

Thanks for the device check — you were right, and that exact case is fixed in 33d9b4f61 (merged with main 7fbdf8511).

When layout_state(before) == layout_state(after), enhanced() now records Barrier::Uav { flags: 0, res } if the state is UNORDERED_ACCESS, and records nothing otherwise. A UAV -> UAV texture group is covered by same_state_texture_barriers_are_a_wait_or_nothing, which also checks UNDEFINED -> COMMON and DIRECT_QUEUE_SHADER_RESOURCE -> SHADER_RESOURCE record nothing. The existing test runs under Wine here; the new one passes too.

AI-assisted.

@aznohonza

Copy link
Copy Markdown
Contributor Author

Thanks for the captured-command check — you were right about the range form, and it is fixed in 33d9b4f61 (merged with main 7fbdf8511).

With NumMipLevels > 0 and NumMipLevels == NumArraySlices == NumPlanes == 1, the range now flattens to first_mip + first_layer * MipLevels + first_plane * MipLevels * ArraySize using the resource's own descriptor (array size is 1 for 3D textures). Any wider range is still recorded as the whole resource, and the index form (NumMipLevels == 0) is unchanged. one_subresource_ranges_flatten_to_that_subresource covers mip 1 of slice 1, plane 1 of slice 1, 2 mips, the index form including 0xffffffff, and a 3D texture; it passes here under Wine.

AI-assisted.

@merzleckeier

Copy link
Copy Markdown
Contributor

Follow-up device check for both texture-barrier fixes at 33d9b4f6 on main 7fbdf851 (Win64, Wine 11.22 / VKD3D-Proton 3.1.0). I reran the recorder comparison on AMD Radeon (RADV 26.3.1) and RTX 4070 SUPER (NVIDIA 620.73.4 as reported by Wine).

  • On a synthetic two-mip, two-slice texture, flat index 3 and the equivalent explicit 1 × 1 × 1 range both recorded subresource 3, with the same before and after states.
  • A texture UAV-to-UAV layout barrier recorded exactly one resource-specific Barrier::Uav with flags 0. A separate list containing the equivalent native legacy D3D12_RESOURCE_BARRIER_TYPE_UAV closed successfully on both adapters.
  • The main control recorded no barriers for the three inputs. The updated PR passed all 27 stock replay:: tests in each adapter configuration, including both new targeted cases.

MEASURED with a synthetic texture. The scope was captured commands and command-list closure before queue submission. AI-assisted (Codex).

@KelboLive KelboLive 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.

Re-checked at 33d9b4f61 (merged with main 7fbdf8511; merge-tree clean). The blocker is fixed: on a real 4070 Ti SUPER, enhanced() now records Uav for UAV→UAV and nothing for UNDEFINED→COMMON, DIRECT_QUEUE_SHADER_RESOURCE→SHADER_RESOURCE and RT→RT, and all four Close() calls return S_OK. A forced 0x8→0x8 control still returns E_INVALIDARG, so the probe can still see the original defect. replay:: is 27/0, and deleting the before == after branch now fails same_state_texture_barriers_are_a_wait_or_nothing — good red/green.

Two from the review remain, neither the original blocker:

  1. Buffer barriers with non-UAV masks are still a bare Uav (record.rs:2480-2489). I drove a COPY_DEST→COPY_SOURCE buffer barrier through the same path and it records [Uav { res }], which orders nothing for that transition. Either special-case only AccessAfter == UAV and use a global alias+UAV pair otherwise, or document that buffer barriers are assumed UAV-adjacent.
  2. enhanced() itself is still untested: swapping layout_state(b.LayoutAfter) for LayoutBefore at record.rs:2473 leaves all 27 replay:: tests green — the same mutation as before. The new tests cover texture_barrier(), not its call site. A test that builds a D3D12_BARRIER_GROUP and asserts the returned Vec<Barrier> would close both this and the buffer case.

Minor, unchanged: std::slice::from_raw_parts(ptr, 0) with a null pointer for a NumBarriers == 0 group (:2450, :2465, :2482).

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