Skip to content

d3d12/vulkan: predicate CopyResource, CopyTextureRegion, resolves, UAV clears and AtomicCopyBuffer - #3019

Open
LocranKing wants to merge 1 commit into
ItsNotRexed:mainfrom
LocranKing:feat/predicated-copies-resolves
Open

LocranKing wants to merge 1 commit into
ItsNotRexed:mainfrom
LocranKing:feat/predicated-copies-resolves

Conversation

@LocranKing

Copy link
Copy Markdown
Contributor

AI-assisted. No game files.

Follow-up to D3D-18 / #1277 (docs/d3d12_vulkan.md §4.7 Open), claimed in #2516 (comment).

Summary

Extends SetPredication support beyond CopyBufferRegion to cover the remaining D3D12 copy, resolve, UAV-clear, and atomic-copy commands that Vulkan's VK_EXT_conditional_rendering does not cover natively:

  1. CopyResource:
    • Buffer-to-buffer routed directly to PredicatedBufferCopier.
    • Single-sampled texture-to-texture (multi-mip, 2D arrays, BC, depth/stencil with RX 5700 XT aspect split preserved, BC ↔ uncompressed, and depth ↔ colour) staged via scratch buffers (src -> src_scratch, dst -> dst_scratch, conditional PredicatedBufferCopier src_scratch -> dst_scratch, dst_scratch -> dst).
    • Predicated MSAA-to-MSAA texture copies (samples > 1) return ReplayResult::unsupported, as Vulkan forbids vkCmdCopyImageToBuffer / vkCmdCopyBufferToImage on multisampled images.
  2. CopyTextureRegion:
    • Upload and readback (including padded RowPitch / slice_pitch, partial boxes, BC, depth/stencil, and 3D volumes) routed through a new byte-granular 3D strided compute helper (PredicatedOps / predicated_strided_copy.comp), leaving skipped destinations and inter-row readback padding untouched.
    • depth24_as_float and rgb96 uploads convert into depth24.scratch unconditionally and select via PredicatedBufferCopier; readbacks stage unconditionally into depth24.scratch and run depth24_run inside active conditional rendering.
    • Texture-to-texture uses the staged PredicatedBufferCopier path above.
  3. ResolveSubresource / ResolveSubresourceRegion:
    • Colour (4× MSAA → 1×, whole and partial rect) and depth/stencil (MIN / MAX / AVERAGE, whole and partial rect, preserving keep_around_resolve's surrounding bands) save the destination rect, run the resolve unconditionally, capture the resolved rect, select via conditional PredicatedBufferCopier, and write back.
  4. ClearUnorderedAccessViewUint / ClearUnorderedAccessViewFloat:
    • Buffers (raw, structured, typed 1–16 byte elements, whole view and multi-rect including <4-byte unaligned ranges) routed to a new byte-granular repeating-element compute fill helper (PredicatedOps / predicated_fill.comp).
    • 3D volume and non-color-attachment textures staged through scratch and conditionally filled via predicated_fill.comp (2D color-attachment views keep vkCmdClearAttachments).
  5. AtomicCopyBufferUINT / AtomicCopyBufferUINT64:
    • Empirically verified as predicated on native Windows D3D12 (RTX 3060 Ti 621.01) and WARP; routed to PredicatedBufferCopier.

Synchronization, Lifetime & Statistics Isolation

  • Scratch buffers (runtime/src/vulkan_scratch.h): added VK_BUFFER_USAGE_STORAGE_BUFFER_BIT so existing timeline-retired scratch chunks bind directly to predicated compute passes.
  • Conditional rendering discipline (D3D-148): all unconditional staging transfers (vkCmdCopyImageToBuffer, vkCmdCopyBufferToImage, vkCmdResolveImage, depth/stencil resolve rendering scope, and depth24/rgb96 upload conversion) suspend VK_EXT_conditional_rendering via an RAII SuspendPredicate guard and re-enter it around the predicated compute dispatch.
  • Sync validation: barriers between staging + conditional compute and copy-back cover VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT at TRANSFER_BIT | COMPUTE_SHADER_BIT → TRANSFER_READ_BIT at TRANSFER_BIT, keeping both predicate-true and predicate-false paths hazard-free. Also adds a TRANSFER_WRITE -> TRANSFER_WRITE barrier before unpredicated CopyTextureRegion readback into a COPY_DEST buffer.
  • PIPELINE_STATISTICS isolation (D3D-162): plan_statistics_segments splits once per command around predicated_strided, predicated_fill, predicated_copy, and has_depth24 dispatches.
  • Readback visibility: predicated buffer CopyResource, CopyTextureRegion readback, buffer ClearUnorderedAccessView*, and AtomicCopyBuffer* report VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT.
  • Shaders: synthetic predicated_strided_copy.comp and predicated_fill.comp compiled with Glslang 11:16.6.0 (-V --target-env vulkan1.2 -Os), validated with spirv-val --target-env vulkan1.2 (StorageBuffer8BitAccess only, no Int8).
  • Build: added /bigobj on gta6rt_d3d12_com (MSVC COFF COMDAT section count on d3d12_vulkan_device.cpp.obj crossed 32,767 sections).

Verification

  • Environment: Windows 11 Build 26200 x64, NVIDIA GeForce RTX 3060 Ti (driver 621.01 / 32.0.16.2101, Vulkan 1.4.357, Vulkan SDK 1.4.363.0, MSVC 14.44.35207). All Vulkan runs used VK_LOADER_LAYERS_DISABLE=~implicit~ and LEONIDA_VK_VALIDATION=1 (0 validation errors).
  • Base / Commit: rebased on origin/main 16282101, head 4b9b3674.
  • Negative control (before vulkan_replay.h changes): d3d12_predicated_ops_test --warp-only passed on WARP while unpatched Vulkan failed immediately on predicated CopyResource buffer (EQUAL_ZERO with 0 overwrote destination).
  • After implementation (4b9b3674):
    • d3d12_predicated_ops_test: 203/203 exact differential cases vs WARP (including PIPELINE_STATISTICS isolation across all operations), 0 validation errors (--warp-only: 199/199 PASS).
    • ctest --output-on-failure -j4: 115/115 run tests passed (100%, 7 skipped game-data/optional, 1 disabled gameinput).
    • Standalone GPU tests: vk_predicated_copy_test (with new forced-window PredicatedOps strided + 16-byte phase-fill stress), vk_command_replay_test, and vk_predicate_capture_test: all PASS, 0 validation errors.
    • Updated CPU/mock unit tests: predicated_copy_test and replay_query_predication_test PASS.

@hidrogenone

Copy link
Copy Markdown
Contributor

Tested 4b9b3674 on NVIDIA GeForce RTX 5080 (Blackwell), driver 32.0.16.2124, Windows 11, Vulkan SDK 1.4.357.0. Build: VS 2026 (MSVC 19.51), Windows SDK 10.0.26100, Ninja RelWithDebInfo.

ctest -R predicat → 5/5 pass:

d3d12_predicated_ops ................ Passed
predicated_copy ..................... Passed
replay_write_immediate_predication .. Passed
replay_query_predication ............ Passed

First NVIDIA coverage for the predicated CopyResource / CopyTextureRegion path. AI-assisted.

@Gabzinnn12

Copy link
Copy Markdown

Tested 4b9b367 on AMD Radeon RX 7600 (RDNA3), AMD 26.11.2 (LLPC), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8: pass.

  • d3d12_predicated_ops_test: 203 exact cases (Vulkan + WARP differential), exit 0 with and without LEONIDA_VK_VALIDATION=1 (+ VK_LAYER_VALIDATE_SYNC=1), no Vulkan validation errors reported.
  • predicated_copy_test and replay_query_predication_test: exit 0 in both modes.
  • d3d12_vulkan_device, d3d12_descriptor, d3d12_com, d3d12_draw, d3d12_compute, d3d12_m3_frame, d3d12_m4_compute, d3d12_buffer_uav: exit 0 in both modes, no Vulkan validation errors reported.
  • ctest: 0 failed out of 130 (123 passed, 7 skipped, gameinput disabled), including d3d12_predicated_copy and d3d12_predicated_ops.

AI-assisted (Claude Code, reviewed with Codex). No game files.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The embedded SPIR-V is byte-exact against glslang 16.6.0 (-V --target-env vulkan1.2 -Os; 591/591 and 632/632 words) and both modules validate. The contract mapping is correct: EQUAL_ZERO skips when the 64-bit value is zero, NOT_EQUAL_ZERO when it is not; the 8-byte read at an 8-aligned offset matches SO_STATISTICS.NumPrimitivesWritten and the binary-occlusion 0/1; snapshot-at-SetPredication matches the GPU snapping the value. The Vulkan conditional-rendering gating premise is sound.

But this is unproven at runtime here: I could not build or run d3d12_predicated_ops_test (the only configured build points at another checkout and has Vulkan_INCLUDE_DIR-NOTFOUND), so the 203/203 WARP claim was not reproduced. Also, AtomicCopyBufferUINT/AtomicCopyBufferUINT64 and ResolveSubresourceRegion are absent from D3D12's documented predicated set (Draw*, Dispatch, Copy{TextureRegion,BufferRegion,Resource,Tiles}, ResolveSubresource, Clear*View, ExecuteIndirect), so those two rest on one RTX 3060 Ti/WARP measurement and are untested on AMD/Intel.

Please either narrow predication to the documented set or document the measurement for those two and provide a configured build/run with validation enabled. Note also that predicated copy command lists still return unsupported (kind_ == QueueKind::copy, pre-existing), and predicated MSAA texture copies are documented unsupported.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 4b9b367 on Windows 11, RTX 5080 (driver 616.92) and AMD Radeon iGPU gfx1036 (driver 26.10.1): ctest -R "predicat|command_replay|replay_|d3d12_copy_xfmt|d3d12_stencil_copy" (12 tests) plus vk_predicated_copy_test, vk_command_replay_test and vk_predicate_capture_test run directly (MSVC, RelWithDebInfo, glslang available, LEONIDA_VK_DEVICE set per run, device line confirmed, validation off) — pass on both.

  • RTX 5080: ctest 12/12 passed; d3d12_predicated_ops: "203 exact cases (Vulkan + WARP differential)"; the three standalone tests exit 0.
  • AMD Radeon (gfx1036): ctest 12/12 passed; d3d12_predicated_ops: "193 exact cases (Vulkan + WARP differential)" (10 fewer than on the RTX 5080; the test prints no reason), no mismatches; the three standalone tests exit 0.
    Not run: the full ctest suite, and the PIPELINE_STATISTICS cases are only covered as far as d3d12_predicated_ops runs them.

AI-assisted.

@steamaze0

Copy link
Copy Markdown

Tested 4b9b367 on NVIDIA RTX 5060 Ti (Blackwell), subgroup 32, NVIDIA 621.01 (Vulkan 1.4.357), Windows 11, VS Build Tools 2022 (MSVC) + Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0: pass.

  • d3d12_predicated_ops: PASS — 203 exact cases (Vulkan + WARP differential), with LEONIDA_VK_VALIDATION=1.
  • d3d12_predicated_copy and predicated_copy: PASS.
  • ctest --output-on-failure -j4: 122 passed, 0 failed (game-data/optional skipped) — main plus the new predicated-ops test, no regression.

AI-assisted (Qoder): I built the full runtime with MSVC and ran ctest.

@zzEmmazz

Copy link
Copy Markdown
Contributor

Tested 4b9b3674, applied alone on main 16282101: pass.
Windows 11 (26200), MSVC 14.50 (VS 2026), Ninja RelWithDebInfo. Two GPUs: Ryzen 7 9700X iGPU (RDNA2, AMD 32.0.21042.62) and RTX 3070 (NVIDIA 610.74), picked with LEONIDA_VK_DEVICE.

  • Test changes alone on main (no runtime change): d3d12_predicated_ops_test fails on both GPUs with Vulkan failed: CopyResource buffer, and vk_predicated_copy_test fails on both at line 203 (the new fill check). predicated_copy_test and replay_query_predication_test do not compile there (PredicatedOps, prepare_predicated_strided are missing), as expected.
  • With the whole diff: full ctest -j 4, compared test by test with the same main: iGPU 123 passed, 7 skipped, 1 disabled of 131 (main: 122 / 7 / 1 of 130); RTX 3070 122 passed, 8 skipped, 1 disabled (main: 121 / 8 / 1). The only difference is the new d3d12_predicated_ops, which passes; no test changed status.
  • d3d12_predicated_ops: RTX 3070 "203 exact cases (Vulkan + WARP differential)"; iGPU "193 exact cases". The 10 the iGPU does not run are the depth AVERAGE ResolveSubresourceRegion cases, which the test leaves out when the mode is not in the device's supportedDepthResolveModes.
  • d3d12_predicated_copy (16 + 180 exact cases), predicated_copy and replay_query_predication pass on both.
  • With LEONIDA_VK_VALIDATION=1 on both GPUs: the four above pass, and vk_predicated_copy_test, vk_command_replay_test and vk_predicate_capture_test run on their own exit 0.
  • predicated_fill_spv.h and predicated_strided_copy_spv.h are word for word what glslangValidator -V --target-env vulkan1.2 -Os (11:16.6.0) gives for the two .comp files (591 and 632 words), and both pass spirv-val --target-env vulkan1.2.

Not checked: the diff was read only in part (about 3,000 lines). Two things noticed while reading, not measured: unpredicated_copies is no longer incremented anywhere in runtime/src, and cases that used to run unpredicated and be counted (MSAA to MSAA copies, a UAV clear or resolve whose format the staged path does not take) now make prepare return unsupported for the list.

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested 4b9b367 (merged onto main 1628210) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass

  • Merges cleanly; no new compiler warnings against main.
  • ctest: 121 of 121 pass. That's main's 120 plus d3d12_predicated_ops, with the same 8 skips as main. Loader test passes; validation run: 49 tests clean, including d3d12_predicated_ops and d3d12_predicated_copy under the layer.
  • ctest -R predicat -V on this card:
    • d3d12_predicated_ops: "203 exact cases (Vulkan + WARP differential)"
    • d3d12_predicated_copy: "16 exact cases, 4 recapture controls" for query-to-predicated-copy, and "180 exact cases (Vulkan + WARP + literal byte oracle)" for buffer copies
    • predicated_copy, replay_query_predication and replay_write_immediate_predication pass.

This adds an NVIDIA (Ada) result alongside the RTX 3060 Ti and WARP numbers in the PR body.

AI-assisted (Claude).

@Catskan

Catskan commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 4b9b367 on Sapphire RX 9070 XT Nitro+ (AMD 26.11.2 LLPC, driverVersion 2.0.416), Windows 11 26200, MSVC: pass. The runtime builds. Full ctest from a desktop session: 0 failed out of 130, with 7 skipped (game package or optional tools). AI-assisted run.

@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

A heads-up on merge order with #2928 (not a review of the predication logic itself).

#2928 moves a copy's prepared data out of detail::Prepared into detail::PreparedCopy (PreparedList::copies, indexed by Prepared::copy). Every command's entry is refilled on every ExecuteCommandLists, and on main it has grown to 1312 bytes. Splitting it is about 3–5x faster to prepare per command in replay_cpu_bench.

This PR adds copy-only fields to Prepared, about 200 bytes:

  • predicated_strided, predicated_fill;
  • copy_row_bytes/copy_row_pitch/copy_slice_pitch/copy_rows;
  • staged_dst, staged_resolve_saved/captured;
  • the two VkBufferImageCopys.

They belong in PreparedCopy next to predicated_copy, staged and staged_reads, and they need to be cleared in its reset(). A pool entry is reused across executions, so leftover predicated_strided would replay a stale dispatch (replay_cpu_bench --self-test checks this for the existing fields). The helpers then take (Prepared& p, PreparedCopy& c), as prepare_texture_copy does in #2928.

Whichever of the two lands second, I'm glad to do that move. If this one goes first, I'll fold these fields into PreparedCopy when I rebase #2928.

AI-assisted (Claude Code).

@Rossrocha

Copy link
Copy Markdown

Tested on AMD Radeon RX 6600 (RDNA2, gfx1032), Windows 11, Adrenalin 26.11.2 (Vulkan driver 32.0.21045.29003).

PR head 4b9b3674 (it conflicts with main aa2988cc, so I tested the head on its own), fresh MSVC build:

  • d3d12_predicated_ops_test, predicated_copy_test, replay_query_predication_test, vk_predicated_copy_test: all PASS, with and without LEONIDA_VK_VALIDATION=1
  • ctest: 130/130 passed, and 130/130 with LEONIDA_VK_VALIDATION=1
  • run_gpu_tests.py: 174/189 passed, 15 skipped (expected), native and with LEONIDA_W64_EMULATE=1

No failures on this hardware.

@mesterember104-cpu

Copy link
Copy Markdown

Tested 4b9b367 on Tiger Lake (Xe-LP) iGPU 0x9a78, driver 32.0.101.7088, Windows 11, MSVC 14.44: pass

Full ctest -C RelWithDebInfo: 106/113 counted pass — the 2 failures are the known Intel-main pair (d3d12_buffer_tiles, d3d12_descriptor), failing the same way on main, untouched by this PR (skips: shader_library_corpus, vk_sync_validation, d3d12_queue_wait, d3d12_rgb9e5_rtv, d3d12_first_light; gameinput Disabled).

The PR's own area, all green on Intel: d3d12_predicated_ops run directly with LEONIDA_VK_VALIDATION=1 — 203 exact cases (Vulkan + WARP differential), 0 validation errors; plus d3d12_predicated_copy, predicated_copy, replay_write_immediate_predication, replay_query_predication all Passed. The extended SetPredication surface (CopyResource, CopyTextureRegion, resolves, UAV clears, AtomicCopyBufferUINT/UINT64 via conditional rendering + compute helpers) behaves as the host on our Xe-LP iGPU (subgroup 8..32, no mesh).

AI-assisted (GLM, operated by the account owner).

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.