Skip to content

d3d12: opt-in RDNA target-barrier, list raw-twin, and wave-32 switches for #2771 / #2770 - #2834

Open
zmasi wants to merge 13 commits into
ProjectLeonida:mainfrom
zmasi:fix/d3d12-rdna-gbuffer-terrain-tiles
Open

zmasi wants to merge 13 commits into
ProjectLeonida:mainfrom
zmasi:fix/d3d12-rdna-gbuffer-terrain-tiles

Conversation

@zmasi

@zmasi zmasi commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #2771, #2770 (D3D12-RDNA-GBUFFER-TERRAIN-TILES).

Because #2771 (RX 7800 XT, RDNA 3) and #2770 (RX 9070 XT, RDNA 4) reproduce only on RDNA 3 / RDNA 4 hardware in outdoor scenes and our local test machine has an RTX 3090 + WARP with no retail game data, the root cause is not claimed as proven here. Instead of changing default runtime behaviour unmeasured, this PR lands the three concrete AMD-relevant gaps between Mandy and monte-alt/vesper (7b7e556, GPL-3.0) as opt-in switches — all off by default — with per-frame diagnostic counters and hardware/WARP unit tests so RDNA 3 / RDNA 4 testers can isolate the exact mechanism in a single A/B pass.

With all three switches unset (the default), command-list recording, thin-list streams, descriptor slots, and shader translation are unchanged from main.

Switches Added (All Off by Default)

  1. LEONIDA_TARGET_BARRIERS=<bits> (leonida-gpu/src/list/groups.rs, list.rs, list/thin.rs, draw.rs, host/place.rs; adapted after monte-alt/vesper 7b7e556 VESPER_SMART_BARRIERS, GPL-3.0):

    • Bit 1 (named texture moves): parses the console's ID3D12GraphicsCommandList7::Barrier texture barrier groups (list.rs previously ignored _groups and emitted one null-resource UAV barrier) and transitions named single-subresource host textures left for reading (RENDER_TARGET / DEPTH_WRITE / COPY_DEST -> shader-read / copy-source state) through HostList::ensure_state, caching the single-subresource check (GetDesc + CheckFeatureSupport) per HostTexture::serial so partial barriers do not query the driver repeatedly. Capped at 31 textures per call with a once-logged warning; when bit 2 is also enabled (=3), calls exceeding 31 textures fall back to a full target wait for the remainder.
    • Bit 2 (target waits): upgrades resource-less texture barriers, global barriers, and pipeline syncs to HostList::wait_for_everything (wait_for_all) when the host list has recorded a draw, clear, or resolve into a target since its last target wait.
    • Supports LEONIDA_THIN=1 record/replay round-trips while keeping the unset thin-list encoding identical to main (single word) and supports live toggling via LEONIDA_AB=4,LEONIDA_TARGET_BARRIERS.
  2. LEONIDA_LIST_TWIN_MOVE=1 (leonida-gpu/src/descriptor.rs, descriptor/host.rs, descriptor/host/listed_raw_tests.rs; after monte-alt/vesper 7b7e556, GPL-3.0):

    • Applies move_raw in place to non-16-byte-aligned raw buffer twins inside per-thread LIST_VIEWS slots (where main only moved the single staging slot and left per-list raw twins biased or null), and adds the moved-in-list count to the raw-twins frame diagnostic line. Supports live toggling via LEONIDA_AB=4,LEONIDA_LIST_TWIN_MOVE.
  3. LEONIDA_HOST_WAVE32=1 (leonida-shader/src/emit/mod.rs, emit/lower.rs; after monte-alt/vesper 7b7e556 VESPER_RDNA_WAVE32, GPL-3.0):

  4. d3d12/docs/switches.md: Regenerated via python -I tools/d3d12_switches.py --out d3d12/docs/switches.md.

A/B Isolation Plan for RDNA 3 / RDNA 4 (#2771, #2770)

From the same outdoor save spot through the launcher (diag profile):

# Fastest single-run live toggle check (switches bits 1+2 on/off every 4 seconds):
.\scripts\play-d3d12.ps1 -More @{ LEONIDA_AB = '4,LEONIDA_TARGET_BARRIERS' }

# Individual one-variable runs:
.\scripts\play-d3d12.ps1 -More @{ LEONIDA_TARGET_BARRIERS = '3' }   # then '1' and '2' if '3' fixes it
.\scripts\play-d3d12.ps1 -More @{ LEONIDA_LIST_TWIN_MOVE = '1' }    # or LEONIDA_AB = '4,LEONIDA_LIST_TWIN_MOVE'
.\scripts\play-d3d12.ps1 -More @{ LEONIDA_HOST_WAVE32 = '1' }       # first run retranslates; expect "no wave32 lowering for this wave64 program" refusals / missing pipelines (check ground tiles only)

Useful existing zero-code controls on the same spot if needed: LEONIDA_INDIRECT_BATCH='0'; LEONIDA_INDIRECT_WINDOW='0', LEONIDA_RAW_VIEWS='0', and LEONIDA_TWIN_BIAS='0'.

Key log lines to include from each run:

  • Startup settings line + host wave lanes
  • the title's waits for what was drawn into targets per frame ... (reports full target waits, skipped waits, named read textures, and in-list transitions)
  • raw twins of buffer views made per frame ...
  • LEONIDA_HOST_WAVE32=1: ... plus "no wave32 lowering for this wave64 program" / DROPPED / no host pipeline lines (on LEONIDA_HOST_WAVE32=1, wave64-only shader refusals and missing pipelines are expected)

Once a reporter confirms which switch resolves the outdoor ground tiles (and its frame-time delta), flipping that default is a one-line follow-up.

Verification & AI Disclosure

  • AI-assisted: Authored with Claude Fable 5.2 and Gemini 4 Carbon.
  • Separate reviewer agents: Independently reviewed prior to opening/updating the PR by GPT-6.2 Astra (5 structured review passes; final verdict go, risk 0/4), Claude Opus 5.5 (4 adversarial review passes), and Gemini 4 Neon (opus-systems-critic review on the follow-up cache/counter/test commit).
  • Hardware & test verification (no retail game data): Tested on Windows 11 (10.0.26200), NVIDIA GeForce RTX 3090 (24 GB, driver 610.88) and D3D12 WARP (LEONIDA_D3D12_WARP=1):
    • cargo test -p leonida-gpu --lib (default, --features diagnostics, and LEONIDA_D3D12_WARP=1): all passing, including 16 new hardware/WARP tests across list::groups (including cached one_subresource query count verification), list::thin, and descriptor::host::listed_raw_tests (including fresh-thread make_listed -> LIST_SLOTS -> own_slot verification on hardware and WARP).
    • cargo test -p leonida-shader --lib (default and --features diagnostics): 404 passed, 0 failed (including emit::tests::a_card_with_waves_of_32_to_64_is_taken_for_one_of_32_only_when_asked).
    • Red -> Green mutation verification across 5 fault-injection passes confirming the new tests fail when each mechanism is reverted.
    • cargo clippy --all-targets (0 warnings on added lines), python -I tools/d3d12_switches.py --check d3d12/docs/switches.md (rc=0), tests/test_docs_*.py (rc=0), python -I scripts/check_no_game_data.py (rc=0).
    • Not verified locally: RDNA 3 / RDNA 4 hardware reproduction (listed_twins_moved_readback remains #[ignore]d locally because heap.raw == 0 on RTX 3090 / WARP), and ID3D12Debug layer barrier validation (d3d12SDKLayers.dll / Windows Graphics Tools is not installed on this machine).

…heir views (D3D12-RDNA-GBUFFER-TERRAIN-TILES)

The raw twin of a buffer view that does not begin on 16 bytes is moved to
where its view begins only in the one slot views are made in beside the
heaps (move_raw, where the card's CPU descriptors hold the hardware
descriptor). A twin made in a slot a list keeps its views in is left at
the multiple of 16 before its view, with a bias, or null. So such a card
has moved twins in the title's slots and biased or null ones in the
tables its lists make. monte-alt/vesper 7b7e556 (GPL-3.0, as this) moves
both kinds (its TwinSlot::private) and has no bias; it draws on an
RX 9070 XT the ground this tree leaves black in patches (ProjectLeonida#2770).

With LEONIDA_LIST_TWIN_MOVE=1 a list's own twins are moved too where
move_raw applies; elsewhere nothing changes. Off by default: that the
patches come from here is a hypothesis, and which of the two kinds of
card an RX 7800 XT or an RX 9070 XT is was not measured (no such card
here). By turns with LEONIDA_AB in a diagnostics build; the frame line of
the raw twins counts those moved.

Tests: a_twin_in_a_lists_own_slot_is_moved_to_its_view (descriptor memory
of the test's own: moved with the switch, biased without; it fails with
the list branch of own_slot off). listed_twins_moved_readback reads
through moved twins on a card that has raw twins: ignored and not run, it
needs an AMD card.

AI-assisted (Claude Fable 5.2; reviewed by GPT-6.2 Astra and Claude
Opus 5.5). Tested on RTX 3090, driver 610.88, and on WARP: leonida-gpu
lib tests 597 passed (677 with --features diagnostics), no clippy warning
on the changed lines. Not run on an AMD card.

Refs ProjectLeonida#2771
Refs ProjectLeonida#2770
AI-assisted: yes
…DNA card (D3D12-RDNA-GBUFFER-TERRAIN-TILES)

On a card whose widest wave has 64 lanes the translator emits the forms
for 64 lanes (a console wave64 compute program runs with WaveSize(64));
a card of 32 lanes, every NVIDIA card, gets other forms of the same
programs. The forms for 64 lanes run on AMD cards only, and none was at
hand here. monte-alt/vesper 7b7e556 (GPL-3.0, as this) takes the forms
for 32 lanes by default on such a card for the programs they fit (its
VESPER_RDNA_WAVE32, default 2), and draws on an RX 9070 XT the ground
this tree leaves black in patches (ProjectLeonida#2770; ProjectLeonida#2771 on an RX 7800 XT).

With LEONIDA_HOST_WAVE32=1 the translator takes an AMD card with waves of
32 to 64 lanes (set_host_rdna) for one of 32: it gets what an NVIDIA card
gets, WaveSize(32) declared where a form depends on it. A program the
forms for 32 lanes cannot take is refused as there ("no wave32 lowering
for this wave64 program"); vesper leaves those at 64, which this does
not. Off by default: a switch to tell the two sets of forms apart on
such a card. Reading found no defect in the forms for 64 lanes. The
shader cache keys on the lanes taken (host_settings) and on the switch's
value, so a run with it translates anew. host_wave32() says what the
switch came to on the card, for the log.

emit/lower.rs is over the repository's file size limit as it is on main
(753,642 bytes of 655,360): the decision and its test are in
emit/mod.rs, and lower.rs grows by the lines that ask it.

AI-assisted (Claude Fable 5.2; reviewed by GPT-6.2 Astra and Claude
Opus 5.5). Tested on RTX 3090, driver 610.88: leonida-shader lib tests
404 passed, also with --features diagnostics; no clippy warning on the
changed lines. With LEONIDA_WARP=1 396 pass and 8 fail, the same 8 that
fail on main (d4fabd3) so. Not run on an AMD card.

Refs ProjectLeonida#2771
Refs ProjectLeonida#2770
AI-assisted: yes
…d syncs to the host's textures (D3D12-RDNA-GBUFFER-TERRAIN-TILES)

The title's enhanced barriers (Barrier(group count, groups)) and its
pipeline syncs each record one wait for shader writes, an unordered
access barrier with no resource, and move no texture: the groups are not
read. A host texture changes state where the host sees it used, and a
view a shader picks from the heap by a number it computes is not seen
(host.rs: "directly indexed textures are not transitioned"). So what a
draw wrote into a target is read by such a shader with the texture still
in its target state, and with no wait before it that names targets;
D3D12 asks for the texture to leave the target state first.
monte-alt/vesper 7b7e556 (GPL-3.0, as this) moves the textures such
barriers name and waits at the others (its summarize_barrier_groups and
classify_texture_direct_transition), and draws on an RX 9070 XT the
ground this tree leaves black in patches (ProjectLeonida#2770; ProjectLeonida#2771 on an RX 7800 XT).

LEONIDA_TARGET_BARRIERS is a set of two bits, off by default:

- 1: a texture barrier that names a texture and leaves it to be read (by
  shaders, or as a copy's source after draws) moves the host's texture
  to that state through the list's own tracking, as a legacy transition
  of the title's does. Behind draws into it, a copy into it, or no
  access among the lists run together; never behind unordered access or
  any access, and not back towards a target (the next draw does that).
  By the accesses, or by the layout where the barrier names no access
  after it. A barrier on a part of a texture moves it only if the host's
  texture is one subresource.
- 2: a barrier on all memory (a texture barrier that names no texture, a
  global barrier) and a pipeline sync record a wait for everything
  (HostList::wait_for_everything: the wait of the copies done at their
  place in a list) in place of the one for shader writes, if the list
  recorded a draw, a clear or a resolve into a target since its last.

A list whose calls are only written down (LEONIDA_THIN) writes down what
the groups ask when the title calls and does it when it is replayed;
with the switch off a call is written down as before. The frame lines
say how many waits and moves a frame; one line says what the switch was
taken as, and one what LEONIDA_HOST_WAVE32 came to on the card.

That this is what the issues show is a hypothesis: no AMD card was here,
there is no report of the patches from an RX 6800, and what the waits
cost a frame is not measured. The debug layer has not said that the
recorded barriers are legal (it is not on the machine, and installing it
needs elevation): the lists ran on an RTX 3090 and on WARP.

Tests (list::groups, list::thin): what a barrier asks, by its words;
through the list's own methods on the card, a texture drawn into is
moved by a barrier that names it (each bit alone, and both), also one
another list drew into, submitted with it and before it; a part of a
texture; the waits at barriers that name no texture and at syncs, after
a clear and a resolve the card does; a call written down and replayed.
Each fails with its part of the change taken out. No test records a draw
through a title list (it needs a console pipeline): the mark a draw sets
(State::bind) is covered by reading only.

AI-assisted (Claude Fable 5.2; reviewed by GPT-6.2 Astra and Claude
Opus 5.5). Tested on RTX 3090, driver 610.88, and on WARP
(LEONIDA_D3D12_WARP=1): leonida-gpu lib tests 597 passed (677 with
--features diagnostics), no clippy warning on the changed lines. Not run
on an AMD card.

Refs ProjectLeonida#2771
Refs ProjectLeonida#2770
AI-assisted: yes
…OVE and LEONIDA_HOST_WAVE32 (D3D12-RDNA-GBUFFER-TERRAIN-TILES)

Regenerated with tools/d3d12_switches.py for the three names of this
branch: LEONIDA_TARGET_BARRIERS, LEONIDA_LIST_TWIN_MOVE and
LEONIDA_HOST_WAVE32. The comments of the first and the last now begin
with the sentence that says what the switch does, which is the one the
page takes.

The page was behind the sources before this branch: regenerated on
d4fabd3 it changes 13 rows that are not this branch's (the rows whose
first file is now apps/leonida/src/leonida.rs, some counts of files, the
names test code makes up), and those come with this. This branch's own
are the three new rows, the count of names, and one file more for
LEONIDA_AB, LEONIDA_D3D12_DEBUG and LEONIDA_THIN.

AI-assisted (Claude Fable 5.2). python -I tools/d3d12_switches.py
--check d3d12/docs/switches.md: no difference. The two comments are the
only change to the code: leonida-gpu lib tests 597 passed (677 with
--features diagnostics) on RTX 3090, driver 610.88, and on WARP, no
clippy warning on the changed lines.

Refs ProjectLeonida#2771
Refs ProjectLeonida#2770
AI-assisted: yes
…r-terrain-tiles

# Conflicts:
#	d3d12/docs/switches.md
@fiestorra

Copy link
Copy Markdown
Contributor

Verified on Windows with the three code commits and without the fourth, which is documentation and conflicts on its own. The workspace is green apart from the asset policy that main already fails.

main 10bed9ff2 (this branch's merge base), cargo 1.98.1:

one commit conflicts: a3df308fb  CONFLICT (content): d3d12/docs/switches.md
  (its subject: "switches.md lists LEONIDA_TARGET_BARRIERS, LEONIDA_LIST_TWIN_MOVE and ...")
so three of four applied: c2c6bc5ef, 4aac11147, 7fc86a592   -> tree 81e03b910

cargo test --workspace
  exit 101, 6.9 min
  one failure: policy::tests::repository_passes_policy      5 passed; 1 failed

That remaining failure is main failing its own asset policy on two source files over 640 KiB - not this diff; #2815 fixes it.

So the 1,567 lines of code across list/groups.rs (1,096 new), list.rs, list/thin.rs, host/place.rs, emit/mod.rs and emit/lower.rs compile and pass every test in the workspace. The 12 lines this does not cover are the docs/switches.md row for three new switches, and the conflict there is the switches table's usual one.

What I could not test: what the switches do. LEONIDA_TARGET_BARRIERS, LEONIDA_LIST_TWIN_MOVE and LEONIDA_HOST_WAVE32 need a title driving an RDNA card - this machine has a 4090, and the wave32 switch is for a card this is not. The suite passes; the behaviour those switches ask for is what its author measured and I cannot.

One thing worth saying about the shape: three switches and 1,096 new lines in one file is a lot to review, and the split this branch already has - one commit per switch plus the doc row - is the right one. If it is rebased, keep that split: the doc commit is what needs the rerere, and the code is what a reviewer should read.

AI-assisted: yes - DeepSeek v4.1 (flash), a 6.9-minute workspace run on the three-commit tree.

Written with an AI assistant.

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

The code is careful and the default path is genuinely the old one: words() returns the single count word when the bits are 0, own_slot keeps the staging-slot behaviour and leaves a listed twin biased, and host_max_wave() returns the real lanes when asked is false. The bit combinations, the thin-list round trip and the WARP paths are covered (13 list::groups, the enhanced-barrier and twin tests, all passing on this machine's GPU and on WARP), and the barrier moves/waits are per call — so an A/B phase boundary cannot leave a texture permanently in a wrong state; the worst case is a few calls from the previous phase at the boundary on thin lists, which are valid barriers. switches.md is current (--check rc 0), the shader and gpu crates are 405/0 and 607/0 here.

Three things before merge:

  1. The LEONIDA_HOST_WAVE32 experiment guidance is incomplete, and this is the one that will waste someone's run: the switch refuses wave64-only programs (emit/mod.rs:1104, "no wave32 lowering for this wave64 program"), but the A/B plan only says "any DROPPED / no host pipeline lines". Name that message and state that missing pipelines are expected on the =1 run — otherwise a tester reads a worse picture as #2771 getting worse and abandons the experiment.
  2. one_subresource queries the device per named partial barrier (list.rs:2828: GetDesc + CheckFeatureSupport) inside a switch whose frame-time delta is a reported result. Cache the answer per host texture, or state the overhead, so the A/B is not partly measuring a driver query.
  3. The real LIST_SLOTS path has no running test: LIST_SLOTS.set in make_listed is exercised only by the #[ignore]d listed_twins_moved_readback, while the unignored twin test fakes the slot range. A WARP test that makes a real list view would close it, or say in the body that the ignored test was run and passed.

Nits, both about the log rather than behaviour: the 31-texture fallback wait requires bit 2 (the groups.rs warning says so, the body implies more), and TARGET_WAITS[2] counts named textures rather than actual moves, so the "each moved … unless it was in it" line reports a count that is not the number of moves.

@zmasi

zmasi commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @KelboLive (and @fiestorra for the full-workspace run)! Pushed 6f7d5910c and updated the PR description to address all three items and both nits:

  1. LEONIDA_HOST_WAVE32=1 refusal guidance: Updated both the switch summary and A/B run notes in the PR description to name "no wave32 lowering for this wave64 program" (leonida-shader/src/emit/mod.rs) explicitly and state that refused wave64-only shaders / missing pipelines are expected on =1, so testers evaluate only whether the ground tiles themselves change rather than treating unrelated missing draws as a regression.
  2. one_subresource cached per host texture (list.rs): Added a lock-free 4,096-slot [AtomicU64; 4096] cache keyed by HostTexture::serial ((serial << 2) | (1 + one)) so GetDesc + CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO) runs at most once per HostTexture (absent rare hash eviction) instead of on every partial barrier, covered by a query-count assertion in list::groups::tests::a_barrier_on_a_part_of_a_texture_moves_it_only_if_that_is_all_of_it.
  3. Running make_listed -> LIST_SLOTS -> own_slot test (descriptor/host/listed_raw_tests.rs): Added un-ignored make_listed_populates_thread_list_slots_for_own_slot, which spawns a fresh worker thread (LIST_SLOTS == (0, 0)), calls heap.make_listed(...) on a real registered placed buffer view, verifies LIST_SLOTS matches LIST_VIEWS's [start, end) range, and checks own_slot across interior, boundary, and staging handles on both hardware (RTX 3090) and LEONIDA_D3D12_WARP=1.
  4. Log & counter nits (draw.rs + PR body): Clarified in the PR body that the >31-texture fallback wait applies when bit 2 is enabled (=3), and added TARGET_WAITS[3] (ensure_state_moved) so the frame log reports both {named} host textures named for reading and ({inline} transitioned in the list, the rest at submission or already in state).

AI-assisted: authored with Gemini 4 Carbon; independently reviewed with Gemini 4 Neon (opus-systems-critic). Tested on Windows 11, RTX 3090 (driver 610.88) + LEONIDA_D3D12_WARP=1.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
…r-terrain-tiles

# Conflicts:
#	d3d12/docs/switches.md
@github-actions github-actions Bot added needs rebase Conflicts with the base branch: rebase it on main and removed needs rebase Conflicts with the base branch: rebase it on main labels Dec 1, 2026
…r-terrain-tiles

# Conflicts:
#	d3d12/docs/switches.md
…r-terrain-tiles

# Conflicts:
#	d3d12/crates/leonida-shader/src/emit/mod.rs
#	d3d12/docs/switches.md
@github-actions github-actions Bot added needs rebase Conflicts with the base branch: rebase it on main and removed needs rebase Conflicts with the base branch: rebase it on main labels Dec 1, 2026
…r-terrain-tiles

# Conflicts:
#	d3d12/docs/switches.md
@merzleckeier

Copy link
Copy Markdown
Contributor

I ran two-vendor barrier and descriptor checks at 6e1b7eb against 88a9f13. The Rust tree auto-merges. The only unresolved path is d3d12/docs/switches.md, so this is coverage of the merged Rust code. Win64 MSVC target, Wine 11.22 and VKD3D-Proton 3.1.0, Radeon Raphael RADV 26.3.1 and RTX 4070 SUPER 620.73.04.

Each GPU passed 18 tests across list::groups, the enhanced-barrier round trip and the listed descriptor tests. Each also passed the 26 paired-wave and twin-bias shader tests. The off-by-default wave-limit selector test passed separately. The two ignored raw-twin readbacks, when explicitly run, both stopped at the prerequisite that a raw-twin heap be available.

To exercise the readbacks, I made a local build with only the test heap sizes reduced (raw twin base 2^18 and dynamic minimum 2^15). All six listed-descriptor tests then passed on each GPU. The two readbacks returned the expected values with the switch both off and on. Both drivers retained a bias in the on arm because their descriptor layouts do not satisfy move_raw, so that part verifies fallback readback only. The test with synthetic descriptors separately covers an actual move. No RDNA3/RDNA4 terrain-repro claim. MEASURED with synthetic tests. AI-assisted (Codex).

zmasi added 2 commits December 1, 2026 07:48
…r-terrain-tiles

# Conflicts:
#	d3d12/docs/switches.md
…r-terrain-tiles

# Conflicts:
#	d3d12/docs/switches.md
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
…r-terrain-tiles

# Conflicts:
#	d3d12/docs/switches.md
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