Skip to content

fix(shader): serve ds_ordered_count in wave launch order - #204

Open
Thimo188 wants to merge 23 commits into
mainfrom
upstream-pr/12944
Open

Thimo188 wants to merge 23 commits into
mainfrom
upstream-pr/12944

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#12944, originally opened by ZSWolt.

Source commit: 294347f4dc5e12f6ce0be825679b32e5180a02aa. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

Background. On AMD GCN/RDNA GPUs, GDS (global data share) is a small on-chip memory shared by all waves of a dispatch. ds_ordered_count is a GDS instruction that adds to, or swaps, a counter in GDS and returns the old value. Unlike an ordinary atomic, the hardware serves the waves in wavefront launch order: the first-launched wave gets the first value, the next wave the next, and so on. Shaders use it to give each wave a contiguous, ordered slice of an output buffer (stream compaction, particle lists, culling output). Because the order is guaranteed, the result is deterministic and the slices never overlap.

The bug. ds_ordered_count (ordered append on GDS) ran as a plain device atomic, so the waves of a dispatch added in whatever order the host ran them. The hardware hands the counter out in wavefront launch order. radeonsi's si_compute_prim_discard.c (mesa-20.3.0) describes it as "an atomic counter that is incremented in the wavefront launch order", with ORDER_MODE 0 launching in order. A title that turns the returned values into output offsets therefore got overlapping or shifted ranges, intermittently.

In compute shaders the ordered add and swap now run in launch order:

  • Launch index: each wave computes its launch index: the flattened workgroup ID times the waves per workgroup, plus the wave index in the workgroup.
  • Waiting for its turn: one lane per guest wave waits until a turn dword kept one dword past the 64 KiB GDS equals that index. The GDS binding grows by two dwords (RuntimeAbi::GdsMetadataDwords), the turn and a timeout count (Pm4::GdsBindingBytes). Ordinary GDS accesses stay bounded at 64 KiB; only the ordering code reaches the two dwords. The wait, the add and the release run inside one loop, so guest waves that share a host subgroup (wave32 code in a 64-lane subgroup) do not wait on each other.
  • Passing the turn on: it performs the add or swap, then, when offset1 bit 1 (wave done) is set, compare-exchanges the turn from its own index to the next (0 after the dispatch's last wave). Only the wave that holds the turn moves it.
  • Reset per dispatch: the compiled program records whether it orders (orderedAppend, disk cache format 70). For those programs only, the driver zeroes the turn in the buffer the dispatch binds, before the dispatch and before each chunk of a split one. It uses a COMPUTE_SHADER → TRANSFER barrier, a 4-byte vkCmdFillBuffer, then TRANSFER → COMPUTE_SHADER. A GDS copy the CPU made is created fillable, so it is reset the same way. Each chunk counts its own waves.
  • Bounded wait: a wave goes ahead after 2^20 reads of the turn (about 130 ms on an RX 9060 XT). It adds unordered (as on main), marks the turn abandoned so the rest of the dispatch runs unordered at once, and adds 1 to the timeout count, which the test checks. With one wave that never takes its turn, a 65536-group dispatch takes about 350 ms; without the mark it lost the device after 2 s.
  • Graphics stages keep the unordered add.

The wait relies on waves being launched in workgroup order and on earlier waves running while later ones spin. AMD hardware does this, but Vulkan does not guarantee it. This is recorded in docs/dev/debt/shader.md, which now describes the new behaviour in place of "not modeled".

Tested

Ubuntu 26.04.1 (Linux 7.0.0-38), GCC 15.2.0, Release, RX 9060 XT (RADV GFX1200, Mesa 26.0.8-1ubuntu0.3). Based on main 6d56eb8 (merged into the branch on Oct 30).

  • New cases in agc_driver_gds_append_consume, each checking every lane's value, the counter and a timeout count of 0:
    • 64 single-wave workgroups each add 1, and group g must get start + g. Runs 8 dispatches back to back.
    • The same with group 0 delayed by a scalar loop, so later waves reach the instruction first.
    • 16 groups of 4 waves, group 0 delayed.
    • 4 groups where group 1 skips ds_ordered_count: group 0 gets the start value, groups 2–3 time out and get the remaining values, and exactly 2 timeouts are counted.
    • A normal dispatch right after that one, with the turn left at 1: it is ordered only because of the per-dispatch reset.
    • On main bb04a4f (with only this test file added) it fails: "64 single-wave groups, dispatch 1, ds_ordered_count: group 0 lane 0 returned 1025, expected 1000 (the counter in launch order)".
    • With this change it passes.
    • The existing single-workgroup ordered add and swap cases still pass.
  • Split-dispatch case (1 x 65537 single-wave groups, row 1 skips ds_ordered_count), in agc_driver_gds_append_consume and _copied. The RX 9060 XT on RADV reports maxComputeWorkGroupCount[1] = 65535, so the case takes the split path here: 2 chunks, row 0 gets the start value, 65533 waves of the first chunk run unordered, and the second chunk and the dispatch after it are ordered. It passes 3 of 3 in both registrations, with 0 VUID- messages.
    • On AMD's Windows driver (RX 9070 XT, reported by BunnyFl) row 0 got a value about 65,300 places late, which means it started after most of the dispatch. Vulkan does not guarantee that workgroups start in WorkgroupId order. For a row 0 served that late, the case skips only the row 0 value and the unordered count, and names the skip in its success line; the skipped row, the second chunk's order after its reset and the final counter are still checked. A row 0 served early but not first still fails. The debt entry names the assumption.
  • ctest -R '^agc_|^shader_|recompiler' -j4 on this branch: 1130 of 1132 pass. The 2 failures, agc_idle_publish_wait and agc_driver_native_multisample_depth, also fail on main on this machine (the second because a host import gets vkAllocateMemory -13 here).
  • Validation-layer compare: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest -V -R gds_append on the same machine and GPU, on main bb04a4f and on this branch.
    • Neither log has the "was not found but was requested" line, and both have 0 VUID- messages.
    • On main the test fails only on the new case, as intended.
  • God of War Ragnarök (PPSA08329), title-screen fireplace scene. Plain main stops at startup in this title (sceHttp2SetResolveRetry not implemented), so this ran on my local integration build (main plus local branches) with and without this PR.
    • Before the change, a particle compute shader that uses ordered append (0x1038fb8200) intermittently produced overlapping particle records, drawn as long streaks. That was 1–9 corrupted frames per 8-minute run; with the change, 0 in 3 runs (Oct 23, first version of this PR).
    • A/B on the current head, tools/trace_digest.py, APS5_TRACE_SECONDS=200:60, order without, with, with, without:
      • presents 203 / 217 / 214 / 198;
      • frame p50 287.0 / 275.9 / 277.9 / 292.2 ms;
      • gpu_busy_ms_per_present 148.5 / 128.9 / 128.6 / 151.9. This metric spans each batch from its first to its last command, idle gaps included, so it is not shader time. I have not attributed the change to a phase.
    • Earlier versions of this description gave presents "851–857 against about 790" on main. That was wrong: those were runs of the integration build against a baseline from another day.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed.
  • tools/pr_overlap.py --branch fix/recompiler-ordered-append: no open PR changes ds_ordered_count. The overlaps are on shared files only: Graphics/src/ShaderResources.cpp, SpirvMemoryInstructions.cpp, SpirvEmitterHelpers.cpp, Execution/src/Pm4.cpp and docs/dev/debt/shader.md (e.g. feat(agc): serve lane-keyed, pointer and runtime image-table columns boykopovar/AnyPS5#10193, feat(agc): test the depth bounds in the pixel program when the device lacks the depthBounds feature boykopovar/AnyPS5#11551, fix(agc): span table-reading wave64 programs by default on Intel drivers that share handles across fused threads boykopovar/AnyPS5#10846).

Progress

PPSA08329 on this machine (RX 9060 XT, RADV), on main with this change and other local, unrelated fixes; this PR depends on none of them. Bring-up thread: boykopovar#12128.

  • Streaks: the particle streaks from earlier runs are gone. Particle draws read records that compute shader 0x1038fb8200 writes through ds_ordered_count slots, and serving those slots in launch order removed every streak.
  • Gameplay reached: from a new game, the opening cutscene and the sled chase run through the first Freya fight, with the forest, rocks, snow ground and characters drawn. A 10-minute run from that save on Oct 29 showed the fight lit, with the forest and snow present.
  • Frame time, title-screen fireplace scene (Oct 29), A/B on the local build with and without this PR:
    • presents in 60 s: 203 / 198 without, 217 / 214 with;
    • gpu_busy_ms_per_present: 148.5 / 151.9 without, 128.9 / 128.6 with.
  • Still open, not addressed here: the CPU-bound packet thread (about 2 FPS in the chase).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA08329 God of War Ragnarök (PPSA08329) boykopovar/AnyPS5#12128 (God of War Ragnarök particle streaks)
  • GPUs tested: AMD Radeon RX 9060 XT, RADV GFX1200, Mesa 26.0.8, Ubuntu 26.04.1
  • AI-assisted: yes

🤖 Generated with Claude Code

ZSWolt and others added 23 commits October 29, 2026 17:27
The hardware hands out an ordered-append counter in wavefront launch
order. Compute waves now wait for a turn dword kept past the 64 KiB GDS
to reach their launch index (flattened workgroup ID times waves per
workgroup plus the wave index), perform the add or swap, and pass the
turn on when the wave-done bit is set; the last wave of a dispatch
resets it. The wait is bounded so a wave that never issues a done
operation cannot hang the device.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A wave now passes the turn on with a compare-exchange from its launch
index, so a wave that gave up waiting adds unordered and leaves the turn
alone. The driver zeroes the turn in the buffer each dispatch binds,
before the dispatch and each chunk of a split one, instead of relying on
the last wave. Waves that give up are counted in a dword after the turn.

The wait, the add and the release now run inside the loop, one lane per
guest wave, so guest waves sharing a host subgroup no longer wait on each
other until the timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The compiled program now records whether it orders GDS appends, and the
driver resets the turn only for those, behind a compute-to-transfer
barrier instead of draining all prior work. A GDS copy the CPU made is no
longer zeroed at record time. The first wave that gives up waiting marks
the turn abandoned, so the rest of the dispatch runs unordered at once
instead of each later wave waiting a full budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d-append

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d-append

The shared GDS mapping from boykopovar#13159 takes GdsBindingBytes, as its import check does, so the ordered-append turn and timeout count past the 64 KiB GDS stay mapped; disk cache format 52 follows main's 51.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ordinary GDS accesses are bounded by the guest GDS again: the binding
holds RuntimeAbi::GdsMetadataDwords more, which only the ordering helper
reaches. A CPU-made copy that holds the turn is created fillable, so the
turn is reset on the GPU before each dispatch and chunk whatever backs
the GDS; an image mirror holding it throws. The debt entry keeps its
encoding sources, with the ordering model as its own entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d-append

orderedAppend sits beside the artifact's other flags, so main's encoder size assertions stay as they are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d case

Vulkan does not guarantee that workgroups start in WorkgroupId order. On AMD's Windows driver row 0 of a 1 x 65535 dispatch started after most of the dispatch, so its wait could only end unordered. The split case now reports that as a skip with the values it saw; a row 0 that is served early but not first still fails. The ordering debt entry names the assumption and says that chunks are ordered by the barrier between them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…first row

When row 0 is served late, only the row 0 value and the unordered count depend on when waves start. The skipped row, the second chunk's launch order after its reset and the final counter are checked as on any driver, and the skip is named in the test's success line. The debt entry words the AMD Windows report as an observation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bumps ShaderDiskCache FormatVersion to 53. Main landed boykopovar#10000 with format 52, which this branch also used for its own cache format change; both sides set the same line, so the merge kept 52 silently, and caches written by main would load under this format.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main moved the shader disk cache to format 56; this branch's ordered-append
field moves it to 57.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main moved the shader disk cache to format 60; this branch's ordered-append
field moves it to 61.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main moved the shader disk cache to format 68; this branch's ordered-append
field moves it to 69.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… 64 KiB units

The ordered-append metadata made the GDS binding 0x10008 bytes, and the
shared memory file and its host import used that size. The import's last
page then ran past the end of the file, which loses the device on Mesa ANV
and fails the shared-memory alias check on Windows. The allocation and the
import now cover GdsBindingBytes rounded up to 64 KiB; shaders still bind
GdsBindingBytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main reached shader disk cache format 69, the number this branch had, so the
ordered-append field moves to 70.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve conflicts: keep both SpirvEmitter fields (orderedAppend,
anySubgroupSize), keep main's atomicLockTable setup with the PR's
GdsAllocationBytes host import, and move the shader cache
FormatVersion to 92 above main's 90.
@github-actions github-actions Bot added unblocks title Names a title ID on the Unblocks line needs rebase Conflicts with main, rebase needed labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants