Skip to content

Runtime: typed buffer UAVs and UAV counters (typed V#, counter V#; checked against the same adapter's D3D12) - #1858

Merged
ItsNotRexed merged 6 commits into
ItsNotRexed:mainfrom
AlxcBTW:buffer-uav-typed-counter
Nov 22, 2026
Merged

ItsNotRexed merged 6 commits into
ItsNotRexed:mainfrom
AlxcBTW:buffer-uav-typed-counter

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

What and why

Depends on: #1791. This is stacked on M4; until #1791 lands, the diff also shows M4.

Claimed in #1548. M4 left two kinds of buffer UAV stubbed: views with a typed format, and the counter of a structured view.

Typed buffer UAVs: CreateUnorderedAccessView on a buffer with a typed Format now writes a typed V#:

  • the GFX10 format code;
  • the element size as stride and NUM_RECORDS in elements;
  • OOB_SELECT 1;
  • the format's own channel order.

buffer_load_format, buffer_store_format and buffer_atomic in a translated RWBuffer<T> reach it. Formats the layer can't express get a null slot and a log line: typeless, 96-bit, compressed, codes ≥ 128, and formats without a Vulkan mapping. Typed clears (ClearUnorderedAccessViewUint/Float) already worked from the V# and work on the new views too.

UAV counters: a structured view with pCounterResource + CounterOffsetInBytes writes a second, raw 4-byte V# into bytes 16..31 of its slot. The combinations D3D12 documents as invalid leave a null slot. D3D12 has no CopyStructureCount (that's D3D11); the app copies the counter with CopyBufferRegion, which already works.

Honest limit, the counter contract is a hypothesis: the corpus has no GDS instruction and the translator no ds_append/ds_consume, so append/consume must be atomics on a counter in memory. Where a console shader finds that counter's V# isn't measured yet. It could be a second slot, a user SGPR pair or a spill. That's written down in §3.4 "Buffer UAVs" and in open question 16. If the console uses a different place, only write_buffer_uav changes.

New test d3d12_buffer_uav_test. Two hand-written RDNA2 compute shaders go through the translator:

  • cs_buf_typed: typed stores, loads and an atomic on R32_UINT, R16G16B16A16_FLOAT, R8G8B8A8_UNORM (from FirstElement 16) and R32G32B32A32_UINT, plus an R32_FLOAT → R32_SINT copy. Two views are shorter than the dispatch, so out-of-range stores leave the sentinel and out-of-range loads read 0.
  • cs_buf_counter: 256 lanes append and 128 consume on a counter at offset 4096. The counter is read back with CopyBufferRegion (256, then 128), and the records are compared as sets.

Both are compared with a CPU reference. In addition, typed-buffer and counter clears on 13 formats are compared with the host D3D12 runtime on the same adapter (LUID match).

Known differences, not covered: the clear values avoid exact .5 ties and integers wider than their channel. There the RTX 5070's own D3D12 rounds down and clamps, while the layer rounds up and keeps the low bits; same class as #1616. The translator's FORMATS still lacks the 8/16-bit SINT/UINT formats (other than RGBA8/RGBA16 UINT) and 10_11_11, so shaders reading those views get zeros. That's a follow-up.

Tested

Head 4d9372d4, Windows 11, MSVC 17.14:

  • AMD Radeon 880M (26.11.2): d3d12_buffer_uav passes, 21,394 values, error 0, with the host being the same 880M. LEONIDA_VK_VALIDATION=1: 0 VUIDs. d3d12_m4_compute passes.
  • RTX 5070 Laptop (617.42): d3d12_buffer_uav passes, 21,394 values, error 0, with the host being the same 5070. No validation errors.
  • Full ctest: on both GPUs, only two tests fail, and both fail on main without this change:
  • tests/test_ctest_registration.py and tests/test_shader_checks.py: 62 passed.

Checklist

  • No game data, keys or decryption
  • Registered with ctest (SKIP_RETURN_CODE 77); fixtures in run_gpu_tests.py's D3D12-harness list
  • docs/d3d12_vulkan.md: §3.4, known gaps, M4 row, question 16
  • Translator FORMATS gap (follow-up)

AI-assisted (Claude Code).

🤖 Generated with Claude Code

AlxcBTW and others added 6 commits November 22, 2026 15:09
…copy queues; buffer UAV views and indirect dispatch state

d3d12_m4_compute_test, through the proxy with D3D12 API only and two
translator-output compute shaders (cs_m4_fill.asm, cs_m4_indirect.asm):
a COPY queue streams an input buffer in, a COMPUTE queue waits on it and runs a
compute PSO over a UAV table (structured buffer, raw buffers, an R32_UINT
RWTexture2D), then ExecuteIndirect of a second PSO from arguments the first
wrote (with a transition to INDIRECT_ARGUMENT and UAV barriers), and a COPY
queue behind a cross-queue fence reads the results back. 1,924 words match the
CPU reference with error 0.

Layer gaps the test found, fixed:
- CreateUnorderedAccessView of a buffer was a stub: structured and raw views are
  now a V# in the slot.
- ExecuteIndirect of a dispatch ran whatever pipeline and root arguments the
  command buffer had bound; the command now carries the list's, as Dispatch does.
- An image created in UNORDERED_ACCESS and first used by a shader was
  zero-filled by the first barrier or copy that named it, losing the shader's
  writes; images with a UAV view are now moved to GENERAL (zeroed) at the next
  submission.
- COMPUTE and COPY command lists no longer log "unimplemented".

Test infrastructure: vk_test_device.h can create more than one VkQueue;
build.py gives a compute shader's FSSM program extra words (fssm-at/fssm-data
from 0x148) so two compute shaders with the same group size get distinct hashes,
and honours LEONIDA_SPIRV_TOOLS (as ItsNotRexed#1679) so the target builds under the Visual
Studio generator. docs/d3d12_vulkan.md section 6: M4 synthetic part met; M3 row
credits @whattheflop's ItsNotRexed#1584 measurement of fences in flight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ions, ordered for other queues

- The zero-fill/GENERAL init of images with a UAV view no longer drains on a copy
  queue or in a submission that runs no dispatch or draw; every other queue's next
  submission waits on the initializing one's progress semaphore.
- storage_pending_ is a set (UAV descriptors are rewritten every frame).
- write_buffer_uav: FirstElement and element count are checked against
  Width / unit, so the byte range cannot overflow.
- docs: M4 row names the remaining limitation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…# in the slot), d3d12_buffer_uav_test

CreateUnorderedAccessView on a buffer with a typed Format writes a typed V# (GFX10_FORMAT code, element size as
stride, element count, OOB_SELECT structured, the format's own DST_SEL), and a structured view with pCounterResource +
CounterOffsetInBytes writes a second raw 4-byte V# in the upper 16 bytes of the slot (a hypothesis for the console
contract, documented in section 3.4). The combinations D3D12 documents as invalid leave a null slot.

d3d12_buffer_uav_test runs two hand-written RDNA2 compute shaders through the translator (typed store/load/atomic on
five formats with NUM_RECORDS as the bound; append by 256 lanes and consume by 128 on a counter at offset 4096, read
back with CopyBufferRegion) against a CPU reference, then compares typed-buffer and counter clears with the host D3D12
runtime on the same adapter (LUID).

Depends on: ItsNotRexed#1791

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

# Conflicts:
#	docs/d3d12_vulkan.md
#	runtime/shims/d3d12_vulkan_device.cpp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlxcBTW

AlxcBTW commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Update on the counter hypothesis: @Vonsek's #1811 measured the whole corpus (SHADER-65). The game uses no append/consume counters (0 of 445,394 PSV0 records, no ds_append/ds_consume/GDS). So the counter V# here covers D3D12-API completeness and has no game shader to match; I'll say so in §3.4 and question 16 once #1811 lands.

#1811 also shows that the format of every formatted MUBUF access comes from the descriptor the executable writes (88,924 of 90,316 from a scalar load). That makes the translator's FORMATS gap noted above more important than the counter: the typed V#s this PR writes can carry formats the translator doesn't decode yet. I'm taking that as a follow-up (claim in #1548).

AI-assisted (Claude Code).

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.

2 participants