Repository navigation
Conversation
The copy_image_to_buffer in finish_image() lacked a memory barrier before the fence signal. On GPUs where the transfer write isn't automatically visible to the host, read_back() could read stale or undefined data from capture_ptr_. Add VkBufferMemoryBarrier (TRANSFER_WRITE -> HOST_READ) between the copy and the transition back, so the fence guarantees the buffer contents are visible. Fixes: D3D-819 AI-assisted (Claude Code) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
cryptocxeq
left a comment
There was a problem hiding this comment.
Independent CPU code review of head 3919daa062394640cdd7dfe5df293c7619dd2728 against dde82f27e994337094afc476ded0b1426f6e87e2 (D3D-819). No blocking correctness findings in the 10-line change.
- The barrier follows
copy_image_to_buffer, covers its destination buffer from offset 0 throughVK_WHOLE_SIZE, and uses matching TRANSFER / TRANSFER_WRITE -> HOST / HOST_READ stage/access pairs. Both queue-family indices are ignored, as appropriate for a host visibility dependency rather than a queue-family ownership transfer. finish_image()is shared by the blit, composed-plane and blank-frame paths. The existing capture-buffer write-after-write dependency remains in place.read_back()holdspresent_mutex_while waiting on the last submitted frame'sdonefence and copying the mapped bytes. Submission incrementsframe_index_after success, so this wait selects the capture submission. The same lock excludes new presenter submissions while the CPU reads.make_capture_buffer()requires HOST_VISIBLE | HOST_COHERENT memory, so no explicit mapped-memory invalidation is needed with the barrier and completed fence wait.- The PR description mentions
blit_fence_, but this revision actually usesFrame::done; please update that name for clarity.
Non-blocking test suggestion: extend the existing cmd_pipeline_barrier / cmd_copy_image_to_buffer observation in runtime/test/vk_presenter_sync_test.cpp. It currently counts only TRANSFER_WRITE -> TRANSFER_WRITE barriers before copies, so it does not explicitly assert the new HOST_READ barrier. Check that each capture copy is followed by the host-read dependency for the same destination buffer/range before command-buffer completion; removing this PR's barrier should fail that check. A successful pixel readback on a coherent driver alone cannot prove the barrier was recorded.
Validation performed: git diff <base> HEAD --check passed; built and ran the existing vk_presenter_capture_colour_test.cpp with native Windows MSVC 19.50, C++20, /W4 (exit 0). That test checks the CPU pixel comparison helper only; it does not exercise Vulkan or prove the synchronization fix. No full runtime build or GPU/validation-layer run performed.
References: Vulkan fence host-visibility rules and Khronos CPU readback example.
AI-assisted (OpenAI Codex). No game files used.
|
Tested
Code read: the barrier is recorded in the same command buffer as the copy, before the frame's AI-assisted (Claude Code). |
|
Tested vk_presenter_sync_test on Windows with an RTX 3050 Laptop GPU, NVIDIA driver 616.92, and the Khronos validation layer loaded. Both PR commit 3919daa and a control build with the PR’s barrier removed passed. Both reported 4/4 capture-buffer barriers and successful full-frame readback. I couldn’t reproduce stale pixels on this setup. |
|
Tested 3919daa on Windows 11 / MSVC 19.44, NVIDIA GeForce RTX 5070 Ti (617.42), Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1 and VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1. Focused capture/present set: 7/7 pass (vk_presenter_sync, vk_presenter_capture_colour, vk_predicate_capture, vk_sync_validation, d3d12_present, xbox_present, xbox_present_readback_size), 0 VUIDs. Full ctest: 267/272 pass; the same 5 failures (vk_queue, d3d12_vulkan_device, d3d12_indirect_dispatch_count, d3d12_depth_resolve, d3d12_mesh) reproduce on merge-base dde82f2, and the only 3 VUID reports (d3d12_mesh pipelineStatistics-07076) are identical on both sides. No new failure or validation message from this change. Caveat: main also passed the focused set with 0 VUIDs, so this driver did not reproduce the original missing-barrier race; this is a no-regression check rather than an independent repro. AI-assisted. |
…> HOST_READ barrier Regression test for the capture buffer's host-read barrier (D3D-819). read_back() reads capture_buffer_ on the host after the frame fence, and a fence alone makes no device write visible to the host. The test counts one TRANSFER_WRITE -> HOST_READ barrier per capture copy, as a buffer barrier or a global memory barrier, and counts one only once a copy is recorded ahead of it. Ported from bitzin-dev's closed ItsNotRexed#8214, now also accepting the global VkMemoryBarrier form (ItsNotRexed#8198's) and checking the order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@haayden, a regression test for this, since nagavigneshwarrajendran-source's control build (barrier removed) still passed: test-only commit It ports bitzin-dev's test from the closed #8214 and changes two things:
Measured on an RTX 3070 (NVIDIA 610.88), MSVC 14.44 RelWithDebInfo, base
With AI-assisted (Claude Code). |
|
Tested
As with the other runs above, Synthetic tests only, no game files. AI-assisted (Claude Code). |
|
Tested
Code read:
Suggestion: Note: this device has coherent host memory, so the missing barrier wasn't visibly breaking AI-assisted (Claude Code) |
|
Thanks, everyone. Two updates:
AI-assisted (Claude Code). |
Summary
VkBufferMemoryBarrier(TRANSFER_WRITE → HOST_READ) inVulkanPresenter::finish_image()aftercopy_image_to_buffercompletesread_back()can return stale or undefined pixel data on some GPU/driver combinationsVK_PIPELINE_STAGE_TRANSFER_BIT → VK_PIPELINE_STAGE_HOST_BITTest plan
Verified barrier fields match Vulkan spec requirements (srcAccessMask, dstAccessMask, queue family ownership)
VK_WHOLE_SIZEcovers the entire capture bufferNo change to the existing
Frame::donefence /read_back()synchronization contract — the barrier only adds the missing visibility guaranteeRegression test from @VibeClauding (e939b66, ported from @bitzin-dev's vulkan_present: insert TRANSFER-to-HOST barrier after capture copy (D3D-819) #8214):
vk_presenter_sync_testcounts one TRANSFER_WRITE -> HOST_READ barrier per capture copy, recorded after the copy. RTX 5070 Ti, MSVC 14.44 RelWithDebInfo: passes on this branch, fails onmain'svulkan_present.hwithcapture_host_barriers == 4.Fixes: D3D-819
AI-assisted (Claude Code)
🤖 Generated with Claude Code