Repository navigation
vulkan_present: insert TRANSFER-to-HOST barrier after capture copy (D3D-819) - #8214
bitzin-dev wants to merge 1 commit into
Conversation
…3D-819) VulkanPresenter::finish_image copies the swapchain image to capture_buffer_ via fn.copy_image_to_buffer, but did not emit a pipeline barrier transitioning the buffer's access from VK_ACCESS_TRANSFER_WRITE_BIT in VK_PIPELINE_STAGE_TRANSFER_BIT to VK_ACCESS_HOST_READ_BIT in VK_PIPELINE_STAGE_HOST_BIT. On drivers where host read visibility requires an explicit transfer-to-host barrier before fences signal, read_back() could read stale or incomplete data from the mapped capture buffer. Insert a VkBufferMemoryBarrier for capture_buffer_ from TRANSFER to HOST after the copy, and update vk_presenter_sync_test to verify that HOST_READ barriers are observed on each captured present. Tested: runtime/test/vk_presenter_sync_test.cpp. AI-assisted: Yes.
ihymonte
left a comment
There was a problem hiding this comment.
Duplicate & code-read check on 624803e8f (D3D-819, check_no_game_data.py and git diff --check clean; Win32/Vulkan test not compiled on macOS):
- Duplicate of #8019 (opened earlier for
D3D-819with the same buffer-scopedVkBufferMemoryBarrier, already reviewed + tested on RX 9070 XT; also noted as taken in #8047) and overlaps #8198 / #8163. - Technical note: the buffer-scoped
VkBufferMemoryBarrier(TRANSFER_WRITE -> HOST_READ,TRANSFER_BIT -> HOST_BIT) inVulkanPresenter::finish_image()is cleaner than #8198's globalVkMemoryBarrierand adds avk_presenter_sync_test.cppassertion, though the test assertion checks barrier count without assertingbuffer == capture_buffer_or ordering aftercmd_copy_image_to_buffer.
AI-assisted (Antigravity).
tupacfan04
left a comment
There was a problem hiding this comment.
The barrier itself is correct: runtime/src/vulkan_present.h:945-954 inserts a VkBufferMemoryBarrier on the capture buffer with TRANSFER_WRITE → HOST_READ and TRANSFER → HOST, after copy_image_to_buffer; the fence/read_back and WAW serialization are sound. The new test does not verify the capture path, though.
runtime/test/vk_presenter_sync_test.cpp:143-148 increments capture_host_barriers for any buffer barrier with the right masks/stages while observe_capture is set. It never checks:
- that the barrier is on
capture_buffer_(the fake driver records the copy'sdstbut not the barrier'sbuffer); - that the barrier is after that copy;
- that each captured present has exactly one host barrier.
So a barrier inserted before the copy, a barrier on a different buffer with the same masks, or one missing plus one duplicate all still satisfy EXPECT(capture_host_barriers == 4) at :349. The WAW check is stronger: cmd_pipeline_barrier sets saw_capture_waw and cmd_copy_image_to_buffer consumes it (:131-141, :157-160), so the WAW is tied to each copy; the host check has no equivalent.
The real-device readback (:351-353) cannot close the gap on a host-coherent capture buffer either: the pixels may still be correct without the barrier, so the counter is the only check and it is buffer/order-agnostic.
Fix the test: record the copy destination and compare it with the host barrier's buffer; set a saw_host flag and consume it at the next copy/end so the barrier must follow the copy; assert one host barrier per capture. Optionally have the fake driver require the barrier for the visible data. Synthetic/code-read only; no game files. AI-assisted (OpenCode).
|
Tested
Note: main |
…> 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>
Problem
In
runtime/src/vulkan_present.h,VulkanPresenter::finish_image()copies the swapchain image tocapture_buffer_viacopy_image_to_buffer(). While a WAW barrier was present to serialize successive capture copies, there was no barrier transitioningcapture_buffer_fromVK_PIPELINE_STAGE_TRANSFER_BIT(VK_ACCESS_TRANSFER_WRITE_BIT) toVK_PIPELINE_STAGE_HOST_BIT(VK_ACCESS_HOST_READ_BIT) before the fence is signaled. On drivers where memory visibility to host reads requires an explicit transfer-to-host barrier before fences signal,read_back()could observe stale or incomplete frame data.Solution
VkBufferMemoryBarrierforcapture_buffer_transitioningVK_ACCESS_TRANSFER_WRITE_BIT->VK_ACCESS_HOST_READ_BITacrossVK_PIPELINE_STAGE_TRANSFER_BIT->VK_PIPELINE_STAGE_HOST_BITimmediately aftercopy_image_to_buffer().runtime/test/vk_presenter_sync_test.cppto track and verify that a TRANSFER-to-HOSTHOST_READbarrier is dispatched for each captured present.No game files used or modified.
AI-assisted: Yes.