Skip to content

vulkan: keep CLEAR barriers on COPY queues without enabling image clears - #8746

Closed
Kalmarv wants to merge 1 commit into
ProjectLeonida:mainfrom
Kalmarv:fix/copy-queue-clear-stage
Closed

Kalmarv wants to merge 1 commit into
ProjectLeonida:mainfrom
Kalmarv:fix/copy-queue-clear-stage

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue as: #8504 (comment)

Depends on: #8187

What and why

Replay records WriteBufferImmediate as vkCmdUpdateBuffer, and the zero fill in ResolveQueryData as vkCmdFillBuffer. Synchronization2 runs both in the CLEAR stage. #8187 maps D3D12_BARRIER_SYNC_COPY to COPY | CLEAR. That orders them on DIRECT and COMPUTE queues, but not on COPY queues.

On a COPY queue, restrict_to_queue limits a barrier's stages to queue_stages_copy (runtime/src/vulkan_replay.h). That mask has no CLEAR_BIT, so CLEAR is dropped. A DEFAULT-mode write followed by a SYNC_COPY barrier and a copy is then still unordered there.

The Vulkan registry allows the CLEAR stage on transfer-only queues (syncsupport for VK_PIPELINE_STAGE_2_CLEAR_BIT: GRAPHICS, COMPUTE and TRANSFER). vkCmdUpdateBuffer and vkCmdFillBuffer run there too.

The same bit also decided in initialize() whether the queue may call vkCmdClearColorImage. That command needs a GRAPHICS or COMPUTE queue (vkCmdClearDepthStencilImage needs GRAPHICS). So adding CLEAR to the mask alone would put a colour-image clear on a transfer-only queue.

The fix is two lines:

  • queue_stages_copy gains CLEAR_BIT. queue_stages_compute keeps the same value, now through queue_stages_copy.
  • In initialize(), the clear test reads COMPUTE_SHADER_BIT instead of CLEAR_BIT. It gives the same answer as before on DIRECT (no mask), COMPUTE and COPY queues.

New test runtime/test/replay_copy_queue_clear_stage_test.cpp (CPU only). It mocks the Vulkan functions and captures every barrier, with its stage and access masks, and every command in order. It checks four parts:

  • P1: the COMPUTE mask is unchanged, and the COPY mask has CLEAR and no shader stage.
  • P2: restrict_to_queue keeps COPY | CLEAR and TRANSFER_WRITE on DIRECT, COMPUTE and COPY queues.
  • P3: a DEFAULT WriteBufferImmediate, then a SYNC_COPY buffer barrier (COPY_DEST -> COPY_SOURCE), then CopyBufferRegion. Replay must emit exactly update, barrier, copy, and the barrier's source stages must cover CLEAR with the transfer write. This is checked on each queue kind and needs d3d12: SYNC_COPY waits for the CLEAR stage too, so WriteBufferImmediate is ordered (D3D-838) #8187.
  • P4: the first transition of a never-used colour or depth image:
    • DIRECT clears both;
    • COMPUTE clears colour and only moves depth;
    • COPY clears neither, zeroes colour with a buffer copy and only moves depth.

No capability flags change here. Advertising COPY in WriteBufferImmediateSupportFlags (#8457 answers DIRECT | BUNDLE | COMPUTE) is left to a follow-up.

Tested

This was tested on the CPU only, with no GPU. MSVC 2022, RelWithDebInfo, Windows 11. I ran the same test against variants of vulkan_replay.h:

Variant Failing checks
main 867d922 P1, P2 on COPY, P3 on all three queues (source stages 0x100000000, COPY only)
CLEAR added to the COPY mask without the initialize() change P3 on all three, P4 COPY colour (vkCmdClearColorImage runs on COPY)
#8187 alone (1cb2e43) P1, P2 on COPY, P3 on COPY
mask without the guard change, plus #8187 P4 COPY colour only
this branch alone P3 on all three, as expected until #8187 merges
this branch + #8187 none; the barrier is 0x900000000/0x1000 -> 0x900000000/0x800 on all three queues

This branch, then #8187 (1cb2e43), then #8457 (85931f8) merge without conflicts. In that tree, these pass:

Not run: GPU tests, the Vulkan validation layer (not installed here) and the full ctest. The local P3 checks fail without #8187 and pass with it; this PR is draft pending that dependency.

No game files used.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

🤖 Generated with Claude Code

… stay off COPY queues

restrict_to_queue masks a barrier's stages to the queue's (queue_stages_copy, queue_stages_compute).
The COPY mask had no VK_PIPELINE_STAGE_2_CLEAR_BIT, so on a COPY queue a barrier naming CLEAR lost
it. vkCmdUpdateBuffer (WriteBufferImmediate) and vkCmdFillBuffer run in the CLEAR stage under
synchronization2, and the Vulkan registry allows that stage on transfer-only queues. With ItsNotRexed#8187
mapping SYNC_COPY to COPY | CLEAR, the mask was the last thing leaving a DEFAULT WriteBufferImmediate
unordered before an enhanced SYNC_COPY barrier on a COPY queue.

The same bit doubled as initialize()'s test for whether the queue may call vkCmdClearColorImage,
which needs a compute or graphics queue. That test now reads COMPUTE_SHADER_BIT, so it gives the
same answer on DIRECT, COMPUTE and COPY queues as before, and adding CLEAR to the COPY mask does
not put a colour-image clear on a transfer-only queue.

replay_copy_queue_clear_stage_test (CPU only, mocked Vulkan functions, every emitted barrier and
command captured):
- the COMPUTE mask is unchanged and the COPY mask has CLEAR;
- restrict_to_queue keeps COPY | CLEAR on DIRECT, COMPUTE and COPY;
- DEFAULT WriteBufferImmediate, SYNC_COPY buffer barrier, CopyBufferRegion: exactly update,
  barrier, copy, and the barrier's source stages contain CLEAR on all three (needs ItsNotRexed#8187);
- a never-used colour or depth image on each queue kind: DIRECT clears both, COMPUTE clears
  colour only, COPY clears neither and zeroes colour with a buffer copy.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MartinIndra02

Copy link
Copy Markdown
Contributor

Independent build + test: PASS together with #8187; on its own the new ctest FAILS (merge order)

AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220, Ninja RelWithDebInfo. CPU-only replay tests; machine GPU NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124.

Head 6ffb81bee, based on current main 867d922f0.

Tree replay_copy_queue_clear_stage_test Other ctests
this PR alone exit 1: FAIL: DIRECT: the SYNC_COPY barrier's source stages contain the CLEAR stage, the same for COMPUTE: and COPY: (part P3) vk_command_replay, d3d12_queue_list_type, replay_readback_stages, replay_texture_barrier passed
this PR + #8187 (refs/pull/8187/head, auto-merged) exit 0 vk_command_replay passed
vulkan_replay.h reverted to main (PR's test kept) exit 1, plus P1/P2: FAIL: the COPY barrier mask has CLEAR and no shader stage, FAIL: COPY: restrict_to_queue keeps COPY | CLEAR and TRANSFER_WRITE

The description says P3 needs #8187 ("Depends on: #8187"), and #8187 is still open. If this PR merges first, the new replay_copy_queue_clear_stage ctest fails on main until #8187 lands. Either merge #8187 first, or have P3 skip (or leave it out of this PR) until SYNC_COPY maps to COPY | CLEAR.

The code change itself (CLEAR allowed on COPY queues, the image-clear test keyed on COMPUTE_SHADER_BIT) is consistent with the Vulkan stage support table cited in the description, and P1/P2/P4 pass on their own.

@nnonno91

Copy link
Copy Markdown

Independent Windows GPU test of #8746 (6ffb81b) with #8187 (1cb2e43) and #8457 (85931f8) applied on main 867d922. Setup: native x64 Release, MSVC 14.51, Windows 11 build 26200, Vulkan headers 1.4.363 and Khronos validation layer 1.4.357, and LEONIDA_VK_VALIDATION=1. Synthetic input only, AI-assisted.

I added a local --copy-queue mode to #8187's vk_command_replay part K. It selects a Vulkan transfer-only queue and sets ReplayFunctions::queue_stages to the production COPY mask. It records a DEFAULT WriteBufferImmediate (vkCmdUpdateBuffer), an enhanced SYNC_COPY buffer barrier (COPY_DEST to COPY_SOURCE), and CopyBufferRegion (vkCmdCopyBuffer). It checks the destination word at offset 16.

GPU Transfer family With #8746 Control with #8187 and without #8746
AMD Radeon(TM) Graphics, 32.0.21042.62 2 3/3 copied 0x5eed1234, zero validation errors READ_AFTER_WRITE at vkCmdCopyBuffer, following a vkCmdUpdateBuffer write in CLEAR
NVIDIA GeForce RTX 5090, 617.42 1 3/3 copied 0x5eed1234, zero validation errors Same READ_AFTER_WRITE

The control was built in a separate worktree with byte-identical probe sources. #8457's changes are outside the directly linked Vulkan replay path and were included for the additional D3D12 tests.

The combined build also passed all seven relevant CTests three times per GPU: vk_command_replay, vk_sync_validation, d3d12_write_immediate_queues, replay_write_immediate_barriers, replay_write_immediate_predication, replay_copy_queue_clear_stage, and d3d12_state_map. The image-initialization cases ran in the mocked replay test. The dedicated transfer queue probe exercised buffer ordering.

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested 6ffb81be on Linux, GCC, CPU only, merged onto main 867d922f (the merge base is that commit, so this is not a rebase artefact): the PR's own test fails with the change applied

  • CTest target replay_copy_queue_clear_stage: 0% tests passed, 1 tests failed out of 1.
  • Failing checks, with the change: DIRECT: the SYNC_COPY barrier's source stages contain the CLEAR stage (same for COMPUTE and COPY).
  • With the PR's runtime/src files reverted to main, the same target also fails, on different checks: the COPY barrier mask has CLEAR and no shader stage, COPY: restrict_to_queue keeps COPY | CLEAR and TRANSFER_WRITE.
  • The standalone ASan/UBSan build fails on the same assertion as the WITH run, so it is not a test-harness artifact.

Reading the two failure sets together: the diff adds CLEAR to queue_stages_copy / restrict_to_queue but not to the SYNC_COPY barrier's source mask that its new test checks, so neither state satisfies the test. Worth a look before review continues.

(The sanitizer run is the agent's; I ran the CTest phases.)

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting.

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Author

Closing this in favour of #8870, which makes the same production change to vulkan_replay.h: CLEAR goes into queue_stages_copy, and the image-clear gate moves to COMPUTE_SHADER. #8870 is not a draft and its test passes on main by itself, so one PR is less for reviewers to track. I've stopped working on Leonida, so I won't send the follow-up I offered. Anyone can take over the composition test with #8187 (replay_copy_queue_clear_stage_test, in this branch) once #8187 lands. Thanks to everyone who tested it.

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.

4 participants