vkback: present touches its fence and swapchain only after the submission thread is done #39

Manually merged
github merged 1 commit from github-bot/gh-39 into gh-pr/39/base 2026-12-01 08:16:15 +00:00 AGit
Owner

@⁠tmalik1116 on GitHub · 2026-12-01 03:11 UTC · original
tmalik1116:vkback-thread-safety → main · +28 −6 · 3 files · 1 commits · merged 2026-12-01 08:12 UTC

Fixes the "Thread safety" line of the docs/TASKS.md item "Vulkan validation errors" (swapchain and fence).

What was wrong

show() (greenfield/vkback/src/present.rs) set in_flight as soon as it handed the blit to the submission thread (Queued::run), before that thread had called vkQueueSubmit(.., fence) and vkQueuePresentKHR(swapchain). The next show() then called vkGetFenceStatus/vkResetFences and vkAcquireNextImageKHR, which could run while the other thread was still inside those calls. Both parameters must be externally synchronized (Vulkan spec, "Threading Behavior"), and a signaled fence does not mean the present call has returned.

The change

show() returns without showing the frame while a Queued present is left (Out::queued > 0). That is the same not-waiting choice the fence check already makes, so the live thread never blocks on the submission thread. While a present is queued its fence cannot be signaled yet, so the only extra skip is the short time between its submit and its present's return. recreate() already waits for the queue (settle) and is unchanged. In all, 12 lines of code plus the Queued doc comment. A learning goes in docs/learnings/present.md (rule 28).

No D3D12-path counterpart: victory-gpu presents through vkd3d-proton's swapchain. The rule followed is the Vulkan spec's, not a reference behaviour.

Verification (rules 27 and 30)

RX 6800 XT (RDNA2, RADV NAVI21), Mesa 26.3.1, vulkan-validation-layers 1.4.363.1, core, thread-safety and shader checks, errors only.

Run Commit Threading reports Other distinct errors
base ac9badb UNASSIGNED-Threading-MultipleThreads-Read x3: VkFence, vkback-live vkGetFenceStatus vs vkback-submit vkQueueSubmit 29
fix ac9badb + this none from present the same 29
fix 19862d4 + this none from present; one -Write on VkQueue, vkDeviceWaitIdle vs vkQueueBindSparse (#38, code this change does not touch) the same
base 73ee0ab -Read x3, VkFence, the same pair as above 12
fix 73ee0ab + this none the same 12

Distinct errors in the 73ee0ab fix run: RuntimeSpirv-meshAndTaskShaderDerivatives-10153, vkBindImageMemory-memory-02728, VkBufferCreateInfo-size-06409, vkCmdDraw-dynamicRenderingUnusedAttachments-08910, VkComputePipelineCreateInfo-layout-01687, VkExportMemoryAllocateInfo-handleTypes-09860, VkGraphicsPipelineCreateInfo-layout-01688, VkRayTracingPipelineCreateInfoKHR-layout-03428, VkRenderingInfo-pNext-06079, VkRenderingInfo-pNext-06080, VkShaderModuleCreateInfo-pCode-08740, -08742. All are on the list except 08910, 06079 and 06080. Those also show on the base commit, so they are reported in #37 (rule 30).

  • Scene: these are not house-scene runs. This machine's save does not start at the house, so every run is the start-up auto-press of tools/play.sh into the world, 900 s, base and fix in the same scene.
  • Timing: the swapchain half of the race never fired in the base runs here, because thread-safety reports depend on timing. The change rules it out by construction: no acquire while a present is queued.
  • Tests: cargo test -p vkback --lib: 150 passed, 3 ignored. cargo test -p vkback --test policy: 6 passed. cargo xtask policy: ok.
  • Loud errors (rule 9f), 5-minute runs on 73ee0ab and 73ee0ab + this: every kind is already owned in TASKS.md ("Loud-fail counters"). Nothing new.
  • Frame rate: not a performance change. Three normal 5-minute pairs gave a median host fps of 22.2/21.2/22.7 on base against 22.7/23.2/19.9 with the fix. The pairs do not agree, which is inside the 10–20 % run-to-run spread, so I am not claiming a difference either way.

Why this is enough: the change only decides whether a frame is shown, and the thread-safety checks of the validation layer are the tool that sees this race. The picture is not changed beyond an occasional skipped frame, which the fence check already does.

Reported outside this change (rule 29)

  • #37: three validation errors on RX 6800 XT that show on the base commit and are not on the list.
  • #38: vkDeviceWaitIdle on vkback-live vs vkQueueBindSparse on umem-binds.

🤖 Generated with Claude Code

https://claude.ai/code/session_013Ci8ajhytjZ3vRHasVhzrB

**@⁠tmalik1116** on GitHub · 2026-12-01 03:11 UTC · [original](https://github.com/vktory6/VKtory/pull/39) `tmalik1116:vkback-thread-safety` → `main` · +28 −6 · 3 files · 1 commits · merged 2026-12-01 08:12 UTC Fixes the "Thread safety" line of the docs/TASKS.md item "Vulkan validation errors" (swapchain and fence). ## What was wrong `show()` (greenfield/vkback/src/present.rs) set `in_flight` as soon as it handed the blit to the submission thread (`Queued::run`), before that thread had called `vkQueueSubmit(.., fence)` and `vkQueuePresentKHR(swapchain)`. The next `show()` then called `vkGetFenceStatus`/`vkResetFences` and `vkAcquireNextImageKHR`, which could run while the other thread was still inside those calls. Both parameters must be externally synchronized (Vulkan spec, "Threading Behavior"), and a signaled fence does not mean the present call has returned. ## The change `show()` returns without showing the frame while a `Queued` present is left (`Out::queued > 0`). That is the same not-waiting choice the fence check already makes, so the live thread never blocks on the submission thread. While a present is queued its fence cannot be signaled yet, so the only extra skip is the short time between its submit and its present's return. `recreate()` already waits for the queue (`settle`) and is unchanged. In all, 12 lines of code plus the `Queued` doc comment. A learning goes in `docs/learnings/present.md` (rule 28). No D3D12-path counterpart: victory-gpu presents through vkd3d-proton's swapchain. The rule followed is the Vulkan spec's, not a reference behaviour. ## Verification (rules 27 and 30) RX 6800 XT (RDNA2, RADV NAVI21), Mesa 26.3.1, `vulkan-validation-layers` 1.4.363.1, core, thread-safety and shader checks, errors only. | Run | Commit | Threading reports | Other distinct errors | |---|---|---|---| | base | ac9badb | `UNASSIGNED-Threading-MultipleThreads-Read` x3: VkFence, `vkback-live` `vkGetFenceStatus` vs `vkback-submit` `vkQueueSubmit` | 29 | | fix | ac9badb + this | none from present | the same 29 | | fix | 19862d4 + this | none from present; one `-Write` on VkQueue, `vkDeviceWaitIdle` vs `vkQueueBindSparse` (#38, code this change does not touch) | the same | | base | **73ee0ab** | `-Read` x3, VkFence, the same pair as above | 12 | | fix | **73ee0ab + this** | **none** | the same 12 | Distinct errors in the 73ee0ab fix run: `RuntimeSpirv-meshAndTaskShaderDerivatives-10153`, `vkBindImageMemory-memory-02728`, `VkBufferCreateInfo-size-06409`, `vkCmdDraw-dynamicRenderingUnusedAttachments-08910`, `VkComputePipelineCreateInfo-layout-01687`, `VkExportMemoryAllocateInfo-handleTypes-09860`, `VkGraphicsPipelineCreateInfo-layout-01688`, `VkRayTracingPipelineCreateInfoKHR-layout-03428`, `VkRenderingInfo-pNext-06079`, `VkRenderingInfo-pNext-06080`, `VkShaderModuleCreateInfo-pCode-08740`, `-08742`. All are on the list except 08910, 06079 and 06080. Those also show on the base commit, so they are reported in #37 (rule 30). - **Scene:** these are not house-scene runs. This machine's save does not start at the house, so every run is the start-up auto-press of `tools/play.sh` into the world, 900 s, base and fix in the same scene. - **Timing:** the swapchain half of the race never fired in the base runs here, because thread-safety reports depend on timing. The change rules it out by construction: no acquire while a present is queued. - **Tests:** `cargo test -p vkback --lib`: 150 passed, 3 ignored. `cargo test -p vkback --test policy`: 6 passed. `cargo xtask policy`: ok. - **Loud errors** (rule 9f), 5-minute runs on 73ee0ab and 73ee0ab + this: every kind is already owned in TASKS.md ("Loud-fail counters"). Nothing new. - **Frame rate:** not a performance change. Three normal 5-minute pairs gave a median host fps of 22.2/21.2/22.7 on base against 22.7/23.2/19.9 with the fix. The pairs do not agree, which is inside the 10–20 % run-to-run spread, so I am not claiming a difference either way. Why this is enough: the change only decides whether a frame is shown, and the thread-safety checks of the validation layer are the tool that sees this race. The picture is not changed beyond an occasional skipped frame, which the fence check already does. ## Reported outside this change (rule 29) - #37: three validation errors on RX 6800 XT that show on the base commit and are not on the list. - #38: `vkDeviceWaitIdle` on `vkback-live` vs `vkQueueBindSparse` on `umem-binds`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_013Ci8ajhytjZ3vRHasVhzrB
show() set in_flight when it handed the blit to the submission thread, before that thread
had called vkQueueSubmit(.., fence) and vkQueuePresentKHR(swapchain). The next show then
called vkGetFenceStatus/vkResetFences and vkAcquireNextImageKHR, which could run while the
other thread was still inside those calls. Both parameters must be externally synchronized
(Vulkan spec, "Threading Behavior"), and a signaled fence does not mean the present has
returned. show() now returns without showing the frame while a Queued present is left
(Out::queued > 0), the same not-waiting choice the fence check already makes. While a
present is queued its fence is not signaled yet, so only the time between its submit and
its present's return skips a frame the fence check would have shown. recreate() already
waits for the queue (settle), so it needs no change.

No D3D12-path counterpart: victory-gpu presents through vkd3d-proton's swapchain; the rule
followed here is the Vulkan spec's external synchronization, not a reference behaviour.
No file access is added on any thread.

Removes the TASKS.md thread-safety line (swapchain and fence) it fixes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013Ci8ajhytjZ3vRHasVhzrB
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 03:12:39 +00:00
github-bot changed target branch from main to gh-pr/39/base 2026-12-01 08:16:12 +00:00
github manually merged commit 74c3e17e80 into gh-pr/39/base 2026-12-01 08:16:15 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-01 08:11 UTC · original

Merged, thanks! The approach is the right one: skipping the frame while a present is still queued covers both the fence and the swapchain and never blocks the renderer. Out::queued drops only after the present call has returned, so the ordering holds. Check on the reference system (RX 9070 XT): tests pass, and the house-scene validation run shows no new error and no threading report. The thread-safety line for this pair is off the validation list. #38 (the device-idle pair) stays on it.

**@⁠vktory6** on GitHub · 2026-12-01 08:11 UTC · [original](https://github.com/vktory6/VKtory/pull/39#issuecomment-7305002730) Merged, thanks! The approach is the right one: skipping the frame while a present is still queued covers both the fence and the swapchain and never blocks the renderer. `Out::queued` drops only after the present call has returned, so the ordering holds. Check on the reference system (RX 9070 XT): tests pass, and the house-scene validation run shows no new error and no threading report. The thread-safety line for this pair is off the validation list. #38 (the device-idle pair) stays on it.
This discussion has been locked. Commenting is limited to contributors.
No description provided.