Skip to content

Runtime: a full barrier between two ExecuteCommandLists of one queue - #5997

Open
marquezinii wants to merge 1 commit into
ItsNotRexed:mainfrom
marquezinii:fix/ecl-boundary-barrier
Open

marquezinii wants to merge 1 commit into
ItsNotRexed:mainfrom
marquezinii:fix/ecl-boundary-barrier

Conversation

@marquezinii

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #5934 (comment) (new D3D item: no barrier between two ExecuteCommandLists of one queue)

Depends on: none. Related to #4997, which adds a transfer-stage barrier before buffer copies inside the replay; it touches vulkan_replay.h and replay_query_edges_test.cpp (a different function of that file), so either can land first.

What and why

vk_queue fails on main 561b65a8 under LEONIDA_VK_VALIDATION=1 with a recent Khronos validation layer (1.4.369): 5 of 5 runs end with line 329: d.validation_errors() == 0, after 10 WRITE_AFTER_WRITE reports from synchronization validation. The backlog step (section 5) submits the same copy into one buffer 256 times on the direct queue, and the layer reports each copy against the copy of the submission before it.

The layer is right, and so is the test. D3D12 finishes and flushes all of a queue's work at the end of every ExecuteCommandLists (the Enhanced Barriers spec, "Excessive flush operations" and "Barrier-Free Access"), so a later ExecuteCommandLists may read or write a buffer, or a simultaneous-access texture, that an earlier one wrote, with no barrier from the application. VulkanQueue::execute records nothing at the start of its command buffer, and Vulkan does not order two submissions of one queue by itself: without a barrier their commands may overlap. This applies to every access, not only copies: a dispatch that writes a UAV buffer and a draw in the next ExecuteCommandLists that reads it as a vertex buffer are unordered too.

The change:

  • runtime/src/vulkan_backend.h: every ExecuteCommandLists command buffer starts with one full memory barrier, ALL_COMMANDS to ALL_COMMANDS, memory writes to memory reads and writes. Its first scope covers everything submitted earlier on that queue, which is the D3D12 boundary. Work on other queues is already ordered by the timeline semaphores of Wait/Signal.
  • runtime/test/vk_queue_test.cpp, new step 6: two ExecuteCommandLists, each a copy of 64 KiB into the same buffer, 8 rounds on each of the direct, compute and copy queues. It checks that each of the 48 command buffers records the full barrier (by wrapping vkBeginCommandBuffer, vkEndCommandBuffer and vkCmdPipelineBarrier in context.vk), and that the buffer holds the second copy's bytes. It uses its own fence: the test's shared fence is already past 11 at that point.
  • runtime/test/replay_query_edges_test.cpp: its mock device supplies only the entry points a query-only queue uses, so vkCmdPipelineBarrier was null and the test crashed with the change; it gets a no-op.
  • docs/d3d12_vulkan.md: the rule, after the line on how a queue submits.

The cost is one vkCmdPipelineBarrier per ExecuteCommandLists, the flush a D3D12 driver does there anyway. I did not measure it.

Tested

RTX 5070, NVIDIA 621.24, Windows 11 (26300), MSVC 14.52 (VS Build Tools 18), Ninja, RelWithDebInfo, on main 561b65a8. Khronos validation layer 1.4.369 built from source, with LEONIDA_VK_VALIDATION=1 (synchronization validation on); the test prints device: NVIDIA GeForce RTX 5070 (validation on). No game files.

main this PR
vk_queue_test, validation on, unchanged test FAIL 5 of 5: 10 WRITE_AFTER_WRITE (vkCmdCopyBuffer against the previous submission's vkCmdCopyBuffer, same VkQueue), line 329 —
vk_queue_test with step 6, validation off FAIL 3 of 3: 0 of 48 submissions record a full barrier PASS 3 of 3: 48 of 48, 0 stale bytes
vk_queue_test with step 6, validation on FAIL (0 of 48) PASS 4 of 4: 48 of 48, validation errors: 0
full ctest -j4, validation on 212/213, vk_queue fails 213/213 (7 package-gated tests skip, gameinput disabled)

The byte check in step 6 found no stale byte on this GPU before the change either: NVIDIA happens to run these copies in order. The hazard shows through the validation layer and the spec; I have no GPU here where it corrupts data. Earlier testers of #4191 saw no validation error in vk_queue with layer 1.4.357, so the report seems to need a newer layer.

Also run: tests/test_docs_links.py, tests/test_docs_list_items.py, tests/test_docs_repeated_lines.py, tests/test_docs_table_ids.py, tests/test_ci_registration.py and scripts/check_no_game_data.py pass.

Not tested: AMD, Intel, Linux and macOS (the change is in the portable backend; replay_query_edges is the CPU test that Linux CI runs over it).

AI-assisted.

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 a translator change.)

D3D12 finishes and flushes a queue's work at the end of every ExecuteCommandLists, so a later one may use a buffer
an earlier one wrote with no barrier. Vulkan orders nothing between two submissions of one queue, and synchronization
validation (1.4.369) reports WRITE_AFTER_WRITE in vk_queue's backlog step. Every ExecuteCommandLists command buffer
now starts with an ALL_COMMANDS memory barrier; vk_queue_test checks it on the three queue types, and the
query-only mock in replay_query_edges_test gets a no-op vkCmdPipelineBarrier.
@thehollowedone

Copy link
Copy Markdown

Independent Windows check of f29030d8 on 561b65a8. Windows 11 x64, RTX 3080 Ti / 621.35, MSVC 19.44.35230, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, RelWithDebInfo.

The new vk_queue case observed all 48 expected barriers across direct, compute and copy queues. All 24 readbacks of 64 KiB matched the second write byte for byte. Earlier copy and prepared-storage checks also passed. Stock replay_query_edges passed. Both CTests ran without skips, and Vulkan core and synchronization validation reported zero messages.

The runtime code and the new barrier case were exact. The scratch GPU fixture omitted the earlier refusal, backlog and timeout-injection cases. Synthetic inputs only. AI-assisted.

@FIocker

FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Independent Windows RTX 5080 check of f29030d8, AI-assisted. The new queue test built against 561b65a8 recorded 0 of 48 boundary barriers and failed its assertion. On this head it recorded 48 of 48 across the three queue types with zero stale bytes. With Khronos 1.4.357 synchronization validation active, all four focused CTests passed (queue, presenter sync, handover, and query edges). The synchronization canary caught its intentional hazard, though the older validation layer did not flag the original stock vk_queue test on this GPU.

I also timed the same instrumented test source built against each runtime with validation off. Each trial submitted the same copy 256 times and waited for the final fence. After one warm-up pair, ten alternating pairs gave medians of 4.762 ms on base and 4.727 ms here for 64-byte copies, and 121.911 ms on base and 122.374 ms here for 16 MiB copies. The run-to-run ranges overlapped for both sizes. Windows 11 26200, MSVC 19.44, NVIDIA 621.01.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Data point for this fix (not claiming): on main 561b65a8, RTX 5080, NVIDIA 615.78.08, Linux, native build with LEONIDA_VK_VALIDATION=1 and Khronos Vulkan-ValidationLayers nightly b321c028 (API 1.4.369), sync validation on: vk_queue is the one ctest failure, with 10 WRITE_AFTER_WRITE sync-validation messages between back-to-back vkCmdCopyBuffer submits on one queue. Seen while running TEST-934 for #4290 (#4290 (comment)); the same hazard also shows under Wine with the Linux layer injected into Wine's host instance. Happy to re-run vk_queue with validation on this head if useful. AI-assisted (Claude Code).

@Codejustcode12

Copy link
Copy Markdown

Tested f29030d (base 561b65a) on NVIDIA GeForce GTX 1050 Ti (Pascal), driver 560.94, Windows 10 Enterprise LTSC 21H2 (19044), MSVC 14.44, Vulkan SDK 1.4.363, RelWithDebInfo: pass. A Pascal / Windows 10 data point next to the 3080 Ti and 5080.

vk_queue_test: exit 0 with LEONIDA_VK_VALIDATION=1 (×3) and without, "48 of 48 submissions record a full barrier, 0 stale bytes". replay_query_edges_test, d3d12_com, d3d12_compute and d3d12_sparse_queue_family: exit 0. 0 VUIDs.

Same as @FIocker saw with 1.4.357: with the SDK 1.4.363 layer, stock main does not flag the original hazard here (3/3 runs, no WRITE_AFTER_WRITE), although vk_sync_validation_selftest confirms sync validation is active. Their negative control reproduces here too: this PR's test against 561b65a's vulkan_backend.h fails at line 410 with 0 of 48 barriers, and without the barrier the copies on this GPU still read back correctly (0 stale bytes), so the barrier count is the only check that catches it on Pascal.

How: test executables run directly (not ctest). No game files. AI-assisted (Claude Code).

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested f29030d on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass

  • vk_queue_test with LEONIDA_VK_VALIDATION=0 and =1: "ExecuteCommandLists boundary: 48 of 48 submissions record a full barrier, 0 stale bytes", 0 validation errors, exit 0.
  • replay_query_edges_test with validation 0 and 1: exit 0 (the no-op vkCmdPipelineBarrier stub is in place).
  • Negative control: PR head with runtime/src/vulkan_backend.h reverted to main: "0 of 48 submissions record a full barrier", exit 1. So the new step 6 does fail without the barrier.
  • Unchanged main with validation on (layer 1.4.363): 0 validation errors, 0 stale bytes. The WRITE_AFTER_WRITE report the PR cites needs layer 1.4.369, so it cannot be reproduced here, and the byte check shows no stale data on RDNA4 with or without the barrier. The test proves the barrier is recorded, not that it fixes anything on this GPU.
  • Not done: the layer 1.4.369 validation run; no timing of the added barrier.

AI-assisted (Claude Code).

@sennecools

Copy link
Copy Markdown

Adding to @Jadiac5's RX 9070 XT run at f29030d8: a second AMD point and a longer negative control.

  • 9800X3D iGPU (RDNA2, AMD 32.0.21045): vk_queue_test "48 of 48 submissions record a full barrier, 0 stale bytes", 2 of 2.
  • Negative control (main's vulkan_backend.h, this test): "0 of 48" at line 410 in 10 of 10 runs (5 on the 9070 XT, 2 of them with validation, 3 on the iGPU), still 0 stale bytes every time. So neither AMD GPU shows stale data without the barrier either; only the barrier count catches it.

AI-assisted.

@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.

Reviewed head f29030d88c548fefdb2419fbf27f9dbd2edae404. Windows 11 26200, MSVC 19.44.35207, Ninja, RelWithDebInfo, Vulkan 1.4.363, RTX 5070 Ti.

vk_queue 1/1 and replay_query_edges 1/1; 48/48 submissions across DIRECT/COMPUTE/COPY record the full barrier (8 rounds x 3 types). Red: reverting runtime/src/vulkan_backend.h only exits 1 with 0/48.

Caveat: without validation the stale-byte half did not reproduce here (0 stale bytes; this NVIDIA driver is coherent for the test), and the cited WRITE_AFTER_WRITE did not print because synchronization validation is not on by default. The teeth are the barrier assertion, not a local data-visibility failure.

Verdict: ready to merge. The barrier sits at the start of the next command buffer, so it orders the previous submission's writes before everything recorded after it; the harmless first-submission barrier is acceptable.

Cost: one ALL_COMMANDS memory barrier per ExecuteCommandLists. Correctness first; revisit if submission-rate profiling shows it. Related to #5384 (prepared lists across submits) and #3270/#5075 (init timelines) in the same submit path; no direct duplicate.

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.

8 participants