Skip to content

Shaders: lower 64-bit image atomics - #305

Closed
tarcisioads wants to merge 1 commit into
Marvisak:mainfrom
tarcisioads:shader/image-atomics-64
Closed

tarcisioads wants to merge 1 commit into
Marvisak:mainfrom
tarcisioads:shader/image-atomics-64

Conversation

@tarcisioads

Copy link
Copy Markdown

Implement lowering for 64-bit image_atomic_* instructions in victory-shader:

  • Add flags::ATOMIC_INT64_ON_TYPED_RESOURCE (1 << 27 in DxilShaderFlags) and map it to 0x40_0000 (ShaderFeatureInfo_AtomicInt64OnTypedResource) in SFI0 (crates/victory-shader/src/emit/shader.rs).
  • Support 64-bit image atomics (dmask == 3 for binary ops, dmask == 0xF for cmpswap) in crates/victory-shader/src/emit/lower.rs, declaring typed U64 resource properties (0x107), packing 2/4 VGPRs via pack64, emitting atomicBinOp.i64 / atomicCompareExchange.i64, and splitting glc results back into v[reg..reg+2].
  • Add unit test image_atomics_64_bit_lower_with_typed_int64_flag covering 64-bit image_atomic_add and image_atomic_cmpswap and verifying SFI0 feature bits.

Verification

  • cargo test -p victory-shader (57/57 passed on macOS; Windows dxil.dll validation and host D3D12 R32G32_UINT view mapping in victory-gpu not run)
  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower.rs crates/victory-shader/src/emit/shader.rs (ok)
  • cargo xtask policy (ok)

@jeremygold02

Copy link
Copy Markdown
Contributor

Thanks @tarcisioads and @flapwarp78. I compared the current diffs: both implement 64-bit image-atomic lowering. #48 also declares both typed-resource and descriptor-heap 64-bit atomic capabilities and adds an end-to-end synthetic emit() test for 32- and 64-bit add/cmp-swap (capability and emitter test); #305 adds focused lowerer coverage for the typed-resource flag (lowering and test). If maintainers consolidate these, #48 looks broader after rebasing and resolving integration issues. Please carry over #305's focused test and credit, and preserve #48's independent branch-threading/handle-cache work. This is for maintainers to decide.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Review note: running the DXIL validator (dxil.dll from the Windows SDK) on this PR's wide-add fixture fails with "Flags must match usage": the module declares the 64-bit-atomics-on-typed-resource feature but not the one for atomics on resources reached through the descriptor heap, which is how these handles are created here. DXC declares both for the same heap-based operation, and #48 does too. The validator's difference is 1 << 32 (the heap-resource atomics flag).

The three open PRs for this (#48, #305, and the closed #173) agree on the rest: the wide data masks (0x3, 0xF) and the replacement-before-comparand register order for cmpswap.

D3D12 only checks the container hash, so a driver may still take the module; hence the test passing without the validator.

From the documents and synthetic tests only; I have no copy of the title and have not run this on console hardware.

@SpaghettiMaster545

Copy link
Copy Markdown

Tested with the title 038a36f (this PR), against main and against #48 on the same machine.

  • NVIDIA GeForce RTX 5070 Ti, driver 621.24 (32.0.16.2124); Ryzen 7 9800X3D; Windows 11 10.0.26100; Rust 1.98.1. Diag build of the PR merged alone onto main d2a4bae, its own empty VICTORY_CACHE_DIR, scripted keys into story mode, until the host GPU failure (NVIDIA RTX 5070 Ti: world hang (DEVICE_HUNG) on 97c78d1 is not one program, not loops, not barriers; 77 compute pipelines refused #71, unrelated), compared program by program with a main run made the same way.
  • In the main run, 14 pixel programs stop with a 64-bit image atomic.
  • With this PR: 0 programs stop with that reason; 9 of the 14 translate and their pipelines are made; 3 stop at a later reason; 2 were not reached in this run. 0 programs that ran on main stop.
  • The driver accepted every pipeline it made with this PR (115 refused, all pre-existing compute ones that main also has), so the missing heap-resource atomics flag that the DXIL validator reports (review above) did not make the NVIDIA driver refuse anything here.
  • For comparison, Translate 64-bit texture atomics and follow proven shader branches #48 under the same conditions: 0 programs stop with that reason, 10 of the 14 run, and 10 more pixel programs run that stopped on main with a scalar memory read as data (7) and a descriptor read only as data (3).

Not tested

  • AMD GPUs, a release build, the image results themselves (only translation and pipeline creation are compared).

(Claude Code, Claude Opus 5.5: ran the title on each build and compared the logs program by program.)

@GamingRipperDoc

Copy link
Copy Markdown

Validation data for this PR, from victory translate <dump> --validate (offline, dxil.dll validator) on Windows 10 + RX 9070 XT, base d2a4bae + #175 + #336:

vertex/pixel emitted validated Flags must match usage
base 12,284 12,184 0
+ this PR alone +524 +0 520 (528 with #54 also applied)

So the ~524 programs this PR lets through (the 64-bit image atomic ones) are emitted, but every one fails validation with error: Flags must match usage., which reads like the module's shader flags not declaring what the code uses (64-bit atomics on typed resources / Int64 ops?). That's on this PR alone, with no other shader PR applied.

Why it matters: VIctory doesn't validate at run time, so in game these go to the driver. In a play run with this PR in a larger shader stack, AMD's shader compiler crashed (amdxc64.dll+0x85fd0f, access violation reading 0x8) after ~1 min in the world; I can't say which program it was compiling, but these were among ~1,200 non-validating programs in that stack.

No game data in this comment.

AI-assisted (Claude Code); I ran the measurements on my machine.

@claude-GTA6

Copy link
Copy Markdown

From a tester working with Claude (AI-assisted); measured on my own machine.

The lowering looks right to me, but on my machine dxil.dll rejects every shader it newly emits until one more flag is set.

What happens: every 64-bit atomic in the corpus is image_atomic_umax, 2D, dmask 3 (653 programs, the *_AlphaPrepass* / *VBuffer* pixel shaders). With this PR they all emit, but victory translate --validate fails 520 of them with:

validator: error: Flags must match usage.

Cause: the image's handle comes from createHandleFromHeap, and a 64-bit atomic on a descriptor-heap resource needs AtomicInt64OnDescriptorHeapResource as well as AtomicInt64OnTypedResource: ShaderFlags bit 32, SFI0 0x1000_0000. (#48 sets it too.)

Fix on top of this PR:

// shader.rs, mod flags
pub const ATOMIC_INT64_ON_HEAP_RESOURCE: u64 = 1 << 32;

// shader.rs, SFI0 features
if self.flags & flags::ATOMIC_INT64_ON_HEAP_RESOURCE != 0 {
    features |= 0x1000_0000;
}

// lower.rs, the wide branch
self.s.flags |= flags::INT64
    | flags::ATOMIC_INT64_ON_TYPED_RESOURCE
    | flags::ATOMIC_INT64_ON_HEAP_RESOURCE;

(plus features & 0x1000_0000 in the test's asserts).

Measured (translate --validate, dxil.dll from SDK 10.0.26100):

The 133 remaining 64-bit-atomic programs now stop on later causes (104 "a scalar memory read as data", 23 barycentrics). With --pipelines on an RTX 5090 (driver 621.24), all emitted VS/PS and mesh pipelines are created.

@slint6614-dev

Copy link
Copy Markdown

Measured on the full retail shader pack (26,503 containers) with victory translate --validate --pipelines (pipelines created on an AMD RX 9070, Adrenalin, Windows 11; dxil.dll from SDK 10.0.26100), against main 1f858b0. Counts are emitted / validated / pipelines.

VS/PS CS 64-bit image atomic refusals
main 12272 / 12172 / 12272 1693 / 1670 / 1693 653
#539 12790 / 12690 / 12790 1695 / 1672 / 1695 0
#305 12790 / 12172 / 12790 1695 / 1670 / 1695 0
#589 12272 / 12172 / 12272 1693 / 1670 / 1693 653

@Exorion1er

Copy link
Copy Markdown
Contributor

#705 matches this PR almost line for line: 157 of its 158 added lines are identical, including the pack64 helper, the ATOMIC_INT64_ON_TYPED_RESOURCE flag and the test image_atomics_64_bit_lower_with_typed_int64_flag. The difference that matters is merge state: #705 merges cleanly with main today, and this PR conflicts.

#539 is a different implementation of the same lowering. It also covers image atomics through the descriptor heap (ATOMIC_INT64_HEAP), which neither of the other two handles, but it has no test. All three edit the same lowering function, so only one can land.

Checked by comparing the diffs against main. AI-assisted (Claude Code).

@tarcisioads
tarcisioads force-pushed the shader/image-atomics-64 branch from 038a36f to 78dae83 Compare November 24, 2026 11:11
@tarcisioads

Copy link
Copy Markdown
Author

Rebased onto main (ffe310e) and addressed the DXIL validator feedback:

  • Added flags::ATOMIC_INT64_ON_HEAP_RESOURCE (1 << 32 in ShaderFlags, 0x1000_0000 in SFI0) and set it alongside ATOMIC_INT64_ON_TYPED_RESOURCE for 64-bit image atomics in 78dae83.
  • Updated image_atomics_64_bit_lower_with_typed_int64_flag to assert 0x1000_0000 in SFI0 and pass Some(&st) to resource::track.
  • cargo test -p victory-shader (97 passed, 0 failed on macOS; dxil.dll validation still needs Windows).

@h0useadv

Copy link
Copy Markdown

Offline coverage on main ffe310e (full retail shader pack, victory translate --validate, programs matched one by one): +520 programs validate, 0 newly fail; a 64-bit image atomic 653 → 0. #305, #539 and #705 give exactly the same 520 programs, so on coverage they're interchangeable and the choice can rest on review alone. Stacked with #180 and the lane-mask branch from #653: +1,193, 0 regressions. Full table: #293 (comment)

AI-assisted (Claude Code). Counts only, no game data.

@WALID1618Y

Copy link
Copy Markdown

Tested 78dae83 head-to-head against #539 (719079f) on Windows 11 / Rust 1.98.1 / RTX 3050 (32.0.16.2124) + WARP + dxil.dll (full table posted on #539).

  • Passes: both clippy checks clean, victory-shader 121 passed / 0 failed, dxil.dll valid, and real 64-bit image_atomic_add GPU dispatch on a 1x1 R32G32_UINT UAV produces the exact 32→64-bit carry sum and glc old-value readback on RTX 3050 and WARP.
  • Two small gaps vs Shaders: 64-bit image atomics #539:
    1. image_atomic_* still calls self.handle(...) (lower.rs:3311), which caches by props.0 without props.1, so an image_get_resinfo followed by a 64-bit atomic on the same 2D UAV reuses the cached float4 (0x409) handle.
    2. In image_atomics_64_bit_lower_with_typed_int64_flag, 0xF044_1388 and 0xF040_1F88 set bit 7 (dlc=1) rather than bit 13 (glc=1), so the if cache.glc { split64 } return path isn't exercised by the test (nor Validator::validate).

AI-assisted; executed locally on RTX 3050 + WARP.

@alexdeliu

Copy link
Copy Markdown
Contributor

Neither 64-bit image atomics PR had a GPU test, so I wrote one: alexdeliu/VIctory@5f5ce1f (branch atomic64-test, on main @ ffe310eb). It is test-only:

  • emit/image_atomic64_tests.rs.
  • A View::Uint64TextureUav (R32G32_UINT) in the test host.

It is a synthetic RDNA2 compute shader. It first runs image_get_resinfo on the same view, so a handle with another element type already exists in that block. Then it runs 64-bit atomics on a 4×1 texture:

  • add twice, for the carry into the high dword;
  • cmpswap;
  • umax;
  • add then sub, for the borrow.

The test checks the texels. It skips devices without AtomicInt64OnTypedResourceSupported / AtomicInt64OnDescriptorHeapResourceSupported.

On AMD RX 9070 (RDNA4) and WARP, merged onto ffe310eb:

64-bit atomic test cargo test --workspace clippy ×2
main fails in lowering: a 64-bit image atomic
#539 @ 719079f2 pass (hardware + WARP) 343 pass clean
#305 @ 78dae836 pass (hardware + WARP) 344 pass clean

So both produce correct results on real hardware. The handle-cache concern I raised about #305 (no element type in the key) does not show up with a size query before the atomic in one block. Feel free to cherry-pick the test into either PR.

dieharddillon1 pushed a commit to dieharddillon1/VIctory that referenced this pull request Nov 24, 2026
Adapt the validated lowering from flapwarp78 (VIctory Marvisak#48) and Tarcisio (VIctory Marvisak#305), and key typed handles by both resource properties from Ortomanic (VIctory Marvisak#177). Gate the runtime on the typed and descriptor-heap capabilities and test both dwords against hardware.

(cherry picked from commit 26cc0b15c12e6ac6339929754b01c4ce4660c26b)
@justlovett0

Copy link
Copy Markdown

Verified + integrated. Stack: main ffe310e + #175/#994/#747/#302/#193/#704/#309/#953/#1067/#986/#1260/#1267/#1254/#1276/#189 (wave64 emulation), RTX 3080 10 GB / Win10 21H2 (19044.7846) / driver 621.24 (32.0.16.2124). Re-census of captured runtime containers; world stable ~9 min, 0 device removals.

The "a 64-bit image atomic" class in our captured runtime containers is cleared (2 → 0); compute emission 493/560. Clean merge, no regressions (148 logic tests pass; the GPU-readback failures are the permanent environmental SM 6.6 baseline on this Win10 box).

AI use: Qwen Code; local measurements, user-confirmed visuals. No game data posted.

@alessiodeia

Copy link
Copy Markdown
Contributor

In-game data for 78dae83 on an RTX 2070 SUPER (Turing), driver 621.40, Windows 11: the 64-bit image atomic pipelines are created and run.

Tree: main ffe310e + #439 + #704 + #1130 + #1065 + #979's two commits + #493 #587 #282 #876 #67 #490 #464 #984 + fix/unaligned-buffer-srv. Diag build, Series X, DRED, 30 minutes with and without this PR.

This one merges cleanly onto that tree. #539 conflicts there in emit/mod.rs.

No game data attached. AI-assisted (Claude Code).

@Marvisak Marvisak closed this Nov 24, 2026
fornorharry pushed a commit to NitricAcidDev/VIctory-Omega-Fork that referenced this pull request Nov 25, 2026
…ored, views off their copy's elements get their own bytes, card room for the first world frame, levels on demand run on the card

- Descriptor records the title copies into its heap as bytes are mirrored
  before each submission (pull request Marvisak#305 by Flyander, after upstream
  VIctory 86a5ed6), with a per-slot fingerprint so only records that
  changed are mirrored, the page read through the kernel, a switch
  (VICTORY_HEAP_SYNC=0) and a counter: about 40,000 records a frame on the
  test machine.
- A buffer view off its copy's elements gets a copy of its own bytes
  (after upstream 86a5ed6; VICTORY_VIEW_OWN_COPY=0: refused as before).
- Room on the card for the world's first-frame bursts: buffer copies
  (VICTORY_CARD_ROOM_FOR_BUFFERS, now on) and render targets
  (VICTORY_CARD_ROOM_FOR_TARGETS_MB, 3,072); a copy turned down gives its
  room up and is logged; the texture pass asks again every 32 MiB. 98
  buffer copies made (945 MB) where earlier runs made 1; 25 MB compared
  on the processor a frame where it was 550 to 710.
- Levels on demand: the fixes of pull request Marvisak#747 by AscilCH. First run
  on a card (RTX 4070, VICTORY_LEVELS_ON_DEMAND=1): no device loss, 3.1 GB
  not allocated for levels the title does not hold. Still off by default.
- The guarded blocks under storage writes into physical pages are opened
  by default (VICTORY_PAGE_WRITES=0: counted only).
- Shaders: a program is translated with first-half interpolation pairs
  only if it was refused for one (with it on for every program 8,551
  working pixel programs came out changed); parked lane-mask pairs,
  f64 conversions and sums, lane permutations: 14 more programs translate.
- Counted, not fixed: an indirect compute pass asks for about 77,500
  groups in x and is cut to 65,535.

One five-minute game run of these changes on the test machine: no device
loss, no failed request. This merged tree was built; its test suites
were not run.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
alex3112 pushed a commit to alex3112/VIctory that referenced this pull request Nov 25, 2026
…integration

Already covered by the 64-bit texture atomics of Marvisak#48 (which also sets the heap-atomics feature and returns the old value under the guard), so the code is ours unchanged; only the PR's test is taken, and it passes against our lowering.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
PastiiJay pushed a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 25, 2026
…ored, views off their copy's elements get their own bytes, card room for the first world frame, levels on demand run on the card

- Descriptor records the title copies into its heap as bytes are mirrored
  before each submission (pull request Marvisak#305 by Flyander, after upstream
  VIctory 86a5ed6), with a per-slot fingerprint so only records that
  changed are mirrored, the page read through the kernel, a switch
  (VICTORY_HEAP_SYNC=0) and a counter: about 40,000 records a frame on the
  test machine.
- A buffer view off its copy's elements gets a copy of its own bytes
  (after upstream 86a5ed6; VICTORY_VIEW_OWN_COPY=0: refused as before).
- Room on the card for the world's first-frame bursts: buffer copies
  (VICTORY_CARD_ROOM_FOR_BUFFERS, now on) and render targets
  (VICTORY_CARD_ROOM_FOR_TARGETS_MB, 3,072); a copy turned down gives its
  room up and is logged; the texture pass asks again every 32 MiB. 98
  buffer copies made (945 MB) where earlier runs made 1; 25 MB compared
  on the processor a frame where it was 550 to 710.
- Levels on demand: the fixes of pull request Marvisak#747 by AscilCH. First run
  on a card (RTX 4070, VICTORY_LEVELS_ON_DEMAND=1): no device loss, 3.1 GB
  not allocated for levels the title does not hold. Still off by default.
- The guarded blocks under storage writes into physical pages are opened
  by default (VICTORY_PAGE_WRITES=0: counted only).
- Shaders: a program is translated with first-half interpolation pairs
  only if it was refused for one (with it on for every program 8,551
  working pixel programs came out changed); parked lane-mask pairs,
  f64 conversions and sums, lane permutations: 14 more programs translate.
- Counted, not fixed: an indirect compute pass asks for about 77,500
  groups in x and is cut to 65,535.

One five-minute game run of these changes on the test machine: no device
loss, no failed request. This merged tree was built; its test suites
were not run.
Biskwit added a commit to Biskwit/VIctory-reloaded that referenced this pull request Nov 26, 2026
Descriptor records the title copies into a placed shader-visible heap as
bytes, with no descriptor call, are mirrored before each submission: the
heap's range is tracked by the write journal or the guards
(watch::Tracked), its changed pages are read through the kernel, and the
records on them whose fingerprint is not the one last seen there are
made in the host heap (HostHeap::mirror_copied). Capped host heaps stop
before their dynamic slots. VICTORY_HEAP_SYNC=0: not done, as before;
a frame line counts pages, records and time.

A buffer view whose bytes do not start on an element of its buffer's
host copy (the title places views at ring positions in its big heap)
gets a copy of its own bytes (shadow::exact), brought up to date and
written back like any other, kept while a list or slot holds it.
Before, such a view was refused and read zeros. VICTORY_VIEW_OWN_COPY=0:
refused, as before.

The fork's author reports the first room rendering right with these.

Ported from fxceless/victory-fork a26859f (the mirroring is pull
request Marvisak#305 by Flyander, after upstream VIctory 86a5ed6), these two
parts only. Its card room for the first world frame, levels on demand,
the page-write default, the counts of cut indirect compute groups and
the shader changes are not taken.
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.