Skip to content

shader: lower typed 2D image atomics in direct Compute DXIL - #3162

Merged
pigalot merged 2 commits into
R0Y00:mainfrom
RonLogic:shader/native-image-atomics
Nov 27, 2026
Merged

pigalot merged 2 commits into
R0Y00:mainfrom
RonLogic:shader/native-image-atomics

Conversation

@RonLogic

Copy link
Copy Markdown
Contributor

Change

Fixes #3156. Refs #2935, #1545 and #3074. The direct Compute compiler now lowers 32-bit image_atomic_add, image_atomic_umax and image_atomic_cmpswap on a fixed 2D R32_UINT UAV. Synthetic containers previously stopped at the three image instructions at +0x28 or +0x30 on 88cddb09.

DXIL uses a typed 2D handle and the ordinary EXEC predicate. GLC returns the previous word to the data register, and the compare-and-swap operands retain AMD's replacement/compare ordering. The operations count as UAV writes for wait/barrier translation. Published manifests carry the unsigned one-channel format, and the stand-in refuses a mismatched UAV before recording a dispatch.

The patch composes cleanly with the current heads of #3049 and #3154. In-process binding of this format can follow their changes to the native slot layout. The typed path here is the published DXIL/manifest route.

Verification

Tested ed946ef86369bec2a066673228507a9265576b0b on 88cddb0919fd4044ff45211c0919c3aefda73c35. Windows 11 26300.10075, MSVC with Visual Studio 2026 18.12.1, SDK 10.0.26100 and DXC 1.9.2602.17. RTX 4070 32.0.16.2124, WARP 10.0.26100.10068.

$env:XBOX2WINDOWS_ADAPTER = 'RTX 4070'
$tests = '^(native_(image_atomic_.+|gather_lod_.+)|compute_store_aot_mimg_atomic_(integer|minmax|exchange)_(warp|hardware)|startup_compute_dispatch_(native|texture|two_mips)_(warp|hardware))$'
foreach ($preset in 'release', 'dev') {
    cmake --build --preset $preset --target compile_dxil offscreen_check startup_stream_consumer d3d12_xs_shim
    ctest --preset $preset --no-tests=error -j 1 -R $tests -V
}
  • Debug and Release each had 18 passes, 3 WARP wave32 capability skips, and 0 failures. AOT image atomics, published float textures, and two-mip texture dispatches passed on both backends.
  • RTX 4070 matched all 336 texture words in each of seven direct shader cases, including unchanged mips, sparse EXEC, contended ADD, unsigned MAX across the sign boundary, and compare success/failure. Clearing GLC alone was detected in all three returning cases.
  • The published slot-67 dispatch incremented one texel by 32 and matched all 256 words. The R16G16B16A16_FLOAT control was refused and all 1,024 output halfwords stayed at their sentinels. The D3D12 debug layer reported no errors in the positive runtime case.
  • Compile controls refused unsupported dimensions, operand tuples, masks and flags. Documentation link and bidi checks passed.

Evidence and follow-up

The experiment note records source-built fixture generation, public reference provenance, reproduction and limits. The shadPS4 report is a behavioral reference. The code and fixtures were developed independently.

I used Codex to prepare the change and ran verification locally.

@KelboLive

Copy link
Copy Markdown
Contributor

LGTM! The typed 2D image atomic lowering is well-implemented:

Key features:

  • Supports image_atomic_add, image_atomic_umax, and image_atomic_cmpswap on R32_UINT UAVs
  • Properly handles GLC (pre-operation value readback) for compare-and-swap operations
  • Correctly translates to DXIL's RWTexture2D<uint> with integer coordinates
  • EXEC mask is properly honored for lane activity

Test coverage:

  • Tests verify unsigned max across sign boundary, compare success/failure, and contended adds with sparse lanes
  • Runtime fixture validates the full pipeline: manifest publishing, dispatch translation, and readback verification
  • Float UAV refusal test ensures type safety

Implementation notes:

  • The startup_stream_consumer.cpp changes correctly handle the R32_UINT format for atomic operations
  • Pitch alignment is properly calculated to avoid memory access issues
  • The stand-in approach allows testing without full runtime integration

This enables games using 2D texture atomics for compute shaders, which is common in modern rendering pipelines.

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I tried to review ed946ef8 over current main 673a9f2c. The trial merge conflicts in src/gfx1013_compute_native.h and src/direct_compute_cache.cpp. Since this PR's base 88cddb09, #3140 (673a9f2c) has appended ComputeViewKind::heap_sampler to the same enum and raised the cached-binding bound in decode() to it. This PR appends uint_texture_uav at the same place and raises the bound to that. The other six files merge cleanly (git merge-tree --write-tree origin/main review/3162).

When you rebase, both enumerators have to stay. The in.count(...) bound in decode() must name whichever one ends up last, or a cached binding of the other kind is rejected as invalid cached binding. #3140 also added a sampler_heap root check in that loop, so a uint_texture_uav binding needs to take the ordinary root_parameter < 16 branch. A cache round trip of a uint_texture_uav shader on the rebased branch would show both points. I haven't run that.

I'll review it once it is updated with main.

This reply was AI-assisted.

@RonLogic

Copy link
Copy Markdown
Contributor Author

Fixed

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Retested e99e1dfd integrated over main 6d690968 (tree 33882c84). The merge is clean and git diff --cached --check is clean. The conflict from my comment on ed946ef8 is resolved as suggested. uint_texture_uav follows heap_sampler, the decode() bound names uint_texture_uav, and a uint_texture_uav binding takes the ordinary root_parameter branch of #3140's check.

What I traced.

  • Encoding. The opcodes match RDNA 2 MIMG: image_atomic_cmpswap 16, image_atomic_add 17, image_atomic_umax 23. GLC is word 0 bit 13. The refusal mask 0x3c0c6 covers NSA, DLC, R128, TFE and LWE. Word 1 0xffe00000 covers SSAMP, A16 and D16. dmask must be 1, or 3 for cmpswap.
  • Operand order. AMD's cmpswap data is vdata[0] = new value, vdata[1] = compare. The lowering passes read(257 + dst) as DXIL's compareValue and read(256 + dst) as newValue, which matches. AtomicBinOp codes 0 (IADD) and 7 (UMAX) are right. With GLC, the pre-operation value goes back to vdata[0].
  • Predication and barriers. The atomic sits behind br i1 active(). Image atomics now count in writes_uavs, so s_waitcnt_vscnt null, 0 and s_barrier emit the UAV barrier.
  • Runtime. The tex2d uint ... channels 1 manifest line parses into a 2D slot flagged atomic_uint_texture. That slot accepts only R32_UINT, and the float-format check now skips it.

What I ran. I built compile_dxil offscreen_check startup_stream_consumer d3d12_xs_shim native_store_replay native_compute_ir_dump in dev and release, then ran the PR's own selection:

ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(native_(image_atomic_.+|gather_lod_.+)|compute_store_aot_mimg_atomic_(integer|minmax|exchange)_(warp|hardware)|startup_compute_dispatch_(native|texture|two_mips)_(warp|hardware))$'

The _hardware tests ran at -j 1. In Debug and in Release /O2, each run had 18 passes, 0 failures and 3 skips: native_gather_lod_cs_warp, native_image_atomic_warp and startup_compute_dispatch_native_warp. All three are WARP wave32 capability skips (77), as in the description. native_image_atomic_hardware matched 336 texture words in each of the seven cases. native_image_atomic_runtime_hardware matched all 256 words and left the float UAV unchanged. The debug layer was enabled with no errors.

Negative control and mutants. Each mutant was rebuilt in dev and run against native_image_atomic_{controls,hardware,runtime_hardware}:

Mutant Result
the four source files back to main killed: all three refuse at "direct image opcode or descriptor modifiers"
UMAX 7 → signed IMAX 5 killed (umax {}: 16 of 336 words differ)
cmpswap compare and new value swapped killed (cmpswap {}: 16 words differ)
ignore EXEC (br i1 true) killed (sparse add, and the runtime case at word 197)
drop the runtime R32_UINT check killed (native_image_atomic_runtime_hardware)
image atomics not counted in writes_uavs survives

Restored and rebuilt afterwards; git diff --quiet is clean.

Non-blocking. The UAV barrier is untested. Every fixture program already ends in s_waitcnt_vscnt null, 0 (0xBBFD0000), so one more assert in controls() pins it. It passes on this head and kills the surviving mutant:

assert '@dx.op.barrier(i32 80, i32 2)' in dump, dump

Verdict: approve on e99e1dfd. This supersedes my conflict comment on ed946ef8.

Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088, Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; SDK 10.0.26100, DXC 1.9.2602.17; Python 3.12.10; wave32; D3D12 debug layer enabled in the hardware tests.
CI: Build and test on Windows passes on e99e1dfd (the pr preset doesn't include these tests).

This reply was AI-assisted.

@pigalot
pigalot merged commit 22f3852 into R0Y00:main Nov 27, 2026
5 checks passed
R0Y00 pushed a commit that referenced this pull request Nov 28, 2026
* compute: lower more typed 2D image atomics in direct compute

The direct compute typed 2D image atomic path of #3162 (cmpswap, add,
umax) now also lowers image_atomic_swap, _umin, _and, _or and _xor to
DXIL AtomicBinOp Exchange, UMin, And, Or and Xor on the same
RWTexture2D<uint> binding, with the same operand checks, EXEC guard and
GLC return of the old value. image_atomic_smin and _smax stay refused,
because signed min/max is not verified on this R32_UINT binding; sub,
inc, dec and the float atomics stay refused too.

New test native_image_atomic_binops (controls, WARP, hardware) reuses
the #3162 fixture: each new op compiles to atomicBinOp with its code,
the operand refusals still apply, and the refused ops keep "direct
image opcode or descriptor modifiers". The readback covers distinct and
contended texels, GLC on and off, and a sparse EXEC.

AI-assisted.

* docs: list the new typed 2D image atomics

native-image-atomics now lists swap, umin, and, or and xor with their
AtomicBinOp codes, keeps signed min/max outside the verified scope, and
links the binary-operation fixture.

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.

shader: lower typed R32_UINT 2D image atomics in direct Compute DXIL

3 participants