Skip to content

fix(d3d): add HOST_READ barrier after capture buffer copy - #8019

Open
haayden wants to merge 2 commits into
ItsNotRexed:mainfrom
haayden:fix/d3d-819-capture-host-barrier
Open

haayden wants to merge 2 commits into
ItsNotRexed:mainfrom
haayden:fix/d3d-819-capture-host-barrier

Conversation

@haayden

@haayden haayden commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

  • Add VkBufferMemoryBarrier (TRANSFER_WRITE → HOST_READ) in VulkanPresenter::finish_image() after copy_image_to_buffer completes
  • Without this barrier, the fence signal does not guarantee the transfer write is visible to the host — read_back() can return stale or undefined pixel data on some GPU/driver combinations
  • The barrier is placed between the copy and the layout transition back, using VK_PIPELINE_STAGE_TRANSFER_BIT → VK_PIPELINE_STAGE_HOST_BIT

Test plan

  • Verified barrier fields match Vulkan spec requirements (srcAccessMask, dstAccessMask, queue family ownership)

  • VK_WHOLE_SIZE covers the entire capture buffer

  • No change to the existing Frame::done fence / read_back() synchronization contract — the barrier only adds the missing visibility guarantee

  • Regression 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_test counts 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 on main's vulkan_present.h with capture_host_barriers == 4.

Fixes: D3D-819
AI-assisted (Claude Code)

🤖 Generated with Claude Code

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 cryptocxeq left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 through VK_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() holds present_mutex_ while waiting on the last submitted frame's done fence and copying the mapped bytes. Submission increments frame_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 uses Frame::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.

@h0useadv

Copy link
Copy Markdown

Tested 3919daa06 (based on main dde82f27e) on AMD Radeon RX 9070 XT (RDNA4), AMD Windows driver 32.0.32029.2010, Windows 11 Pro build 26300: pass, no new failures. Built with clang-cl 23.1.6 + lld-link against an xwin splat (MSVC 14.44, SDK 10.0.26100), RelWithDebInfo; synthetic, no game files.

  • Present/readback tests all pass: vk_presenter_capture_colour, vk_presenter_sync, d3d12_present, xbox_present, xbox_present_readback_size.
  • Full ctest -j8, failures rerun with -j1: 262/268. The 6 that fail (vk_queue_query_sync, d3d12_vulkan_device, d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_depth_resolve, d3d12_pipeline_library_host) fail the same way on main dde82f27e on this GPU.

Code read: the barrier is recorded in the same command buffer as the copy, before the frame's done fence is signaled, which read_back() waits on, so TRANSFER_WRITE → HOST_READ is the missing host-domain availability step the spec requires (a fence signal alone doesn't make device writes available to the host). The capture memory is HOST_VISIBLE | HOST_COHERENT, so no vkInvalidateMappedMemoryRanges is needed on top. Note: the readback tests passed on main too on this GPU, so this run shows no regression, not a before/after difference; I didn't run with the Khronos validation/sync layers.

AI-assisted (Claude Code).

@nagavigneshwarrajendran-source

Copy link
Copy Markdown

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.

@opit80

opit80 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

@haayden, a regression test for this, since nagavigneshwarrajendran-source's control build (barrier removed) still passed: test-only commit e939b667d on top of 3919daa06, ready to cherry-pick (branch VibeClauding:pr8019-test, vk_presenter_sync_test.cpp only).

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 main dde82f27e:

vulkan_present.h vk_presenter_sync
this PR pass
main (no barrier) fail, capture_host_barriers == 4
#8198 (global VkMemoryBarrier) pass
this PR with the barrier moved before the copy fail

With LEONIDA_VK_VALIDATION=1, vk_presenter_sync, d3d12_present and xbox_present all pass on this PR plus the test.

AI-assisted (Claude Code).

@username634

Copy link
Copy Markdown

Tested 3919daa06 (base main dde82f27e) on Windows 11 Pro 26300, MSVC 14.44 RelWithDebInfo, on Intel UHD Graphics 730 (Xe-LP iGPU, Intel driver 101.7092, the first Intel result here) and RTX 5060 Ti (Blackwell, 621.01), with each GPU picked by LEONIDA_VK_DEVICE: no regression.

ctest -R "^(xbox_present|xbox_present_readback_size|vk_presenter_capture_colour|vk_presenter_sync|d3d12_present)$":

main this PR
RTX 5060 Ti 5/5 passed 5/5 passed
UHD 730 5/5 passed 5/5 passed

As with the other runs above, main also passes on both GPUs, so these tests don't show the missing barrier on Intel or Blackwell either. VibeClauding's suggested regression test would be the way to show it. No Vulkan validation layer here.

Synthetic tests only, no game files. AI-assisted (Claude Code).

@DaniiL35

Copy link
Copy Markdown

Tested 3919daa on RTX 3070 Ti, NVIDIA 621.24, Windows 11 (MSVC 14.44, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.1): pass

  • Full runtime suite (ctest -C RelWithDebInfo), PR head against its base dde82f2: 258/262 pass on both. The same 4 tests fail on both, with identical output, so they're unrelated to this change: d3d12_vulkan_device, d3d12_indirect_dispatch_count, d3d12_depth_resolve, d3d12_indirect_rt. (gameinput is disabled because SDL3 isn't installed.)
  • Presenter, capture and swapchain tests under validation (LEONIDA_VK_VALIDATION=1, VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1, implicit layers off): vk_presenter_sync, vk_presenter_capture_colour, vk_predicate_capture, xbox_present_readback_size, xbox_present, d3d12_present, dxgi_swapchain, dxgi_swapchain_count. 8/8 pass, 0 validation errors and 0 sync hazards, on base and head. vk_presenter_sync reports capture-buffer WAW barriers 4/4 and full-frame read_back ok.

Code read:

  • The fix is correct per the spec. The fence makes the copy's writes available but not host-visible, and TRANSFER→HOST / TRANSFER_WRITE→HOST_READ is the standard readback barrier (the same pattern as vulkan_backend.h:2503).
  • It covers all three present paths, because present(), present_planes() and present_blank() all end in finish_image.
  • The capture buffer is HOST_COHERENT (make_capture_buffer), so no vkInvalidateMappedMemoryRanges is needed.
  • The next frame's capture_waw barrier still orders its copy after this one.

Suggestion: vk_presenter_sync_test's interceptor only counts TRANSFER→TRANSFER barriers, so nothing fails without this fix. Also counting a TRANSFER_WRITE→HOST_READ barrier on the capture buffer after each copy (expect 4) would give it a regression test.

Note: this device has coherent host memory, so the missing barrier wasn't visibly breaking read_back here. This run shows the change is safe on NVIDIA Ampere; it doesn't reproduce the original problem.

AI-assisted (Claude Code)

@haayden

haayden commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks, everyone. Two updates:

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.

8 participants