fix(d3d12_x): one waiter thread per fence, ordered copies and zero-count rasterizer calls #67

Manually merged
github merged 1 commit from gh-pr/67/head into gh-pr/67/base 2026-11-26 19:17:13 +00:00
Owner

@⁠Dark3581 on GitHub · 2026-11-23 02:06 UTC · original
Dark3581:fix/fence-waiter → main · +165 −11 · 7 files · 1 commits · merged 2026-11-23 02:10 UTC

What

Three robustness fixes in the d3d12_x objects, for problems titles are known to trigger:

  • Fence event waits (objects/Fence.cpp). Each SetEventOnCompletion with an event used to start a detached thread that held a reference to the fence until the value arrived. That caused three problems:

    • a value that never arrived leaked a blocked thread and kept the fence alive;
    • those threads could still be inside vkWaitSemaphores when the device went away at shutdown;
    • every wait cost a thread.

    Now one waiter thread per fence serves all its events:

    • it starts on the first event wait and keeps pending events ordered by value;
    • it waits for whichever comes first, the fence semaphore reaching the lowest pending value or a private wake semaphore (VK_SEMAPHORE_WAIT_ANY_BIT), which new waits and destruction signal under the lock so wake values stay increasing;
    • it holds no reference, and the fence's destructor stops and joins it, dropping events still pending;
    • if the wait fails (device lost), every pending event is signalled, as a removed D3D12 device does.
  • Copy ordering (objects/CommandList.cpp). Back-to-back CopyBufferRegion, CopyTextureRegion, CopyResource and ResolveSubresource calls with no barrier between them were recorded as unordered Vulkan transfers, so a second copy over the same range could land before the first. A transfer-to-transfer barrier now goes before each copy that follows another; a resource barrier clears that state. Titles rely on D3D12 copies running in order. This also serializes independent copies, a cost noted in the debt entry.

  • Zero-count RSSetViewports and RSSetScissorRects recorded an error and stopped the title; they now record nothing.

Tested

Windows 10, RTX 3070 Ti (driver 621.14), Khronos validation layer.

  • pso_probe fence-gpu --validate, now 96 checks:

    • 50 event waits on one fence (values 10 to 59): a CPU signal to 30 releases exactly the first 21, the other 29 stay pending, and a GPU signal to 59 releases the rest;
    • releasing a fence with a wait that is never reached returns at once and signals nothing.

    It passed 15 repeated runs.

  • pso_probe commandlist-gpu --validate, now 28 checks: a 4 KiB copy followed by a 2 KiB copy over its first half reads back the second copy's bytes; the pending-copy state is tracked; zero-count viewport and scissor calls don't stop the title.

  • descriptors-gpu still passes. 0 validation errors in all three.

  • MinGW ctest: 73 of 73. With MSVC 2022, fence-gpu and commandlist-gpu pass, and every target builds except ab_gameinput, which needs gameinput.lib, which this machine doesn't have. python tools/check_conventions.py and debt_fold.py --check: ok.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • python tools/check_conventions.py passes (no comments in code, one declaration per export, Conventional Commit subjects)
  • Unimplemented paths stop (AbxNotImplemented / AbxUnsupported); silent stubs are listed under Silent stubs through a debt fragment
  • Known gaps added, changed or removed through docs/dev/debt/changes/<branch>.md
  • Generic behaviour, not specific to one title
  • No game files, keys, notes or logs added to the repository
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • Source of behaviour: Vulkan specification (timeline semaphores, VK_SEMAPHORE_WAIT_ANY_BIT, transfer synchronization); D3D12 documentation (fence events, device removal)
  • AI-assisted: yes
**@⁠Dark3581** on GitHub · 2026-11-23 02:06 UTC · [original](https://github.com/Dark3581/AnyBox/pull/67) `Dark3581:fix/fence-waiter` → `main` · +165 −11 · 7 files · 1 commits · merged 2026-11-23 02:10 UTC ### What Three robustness fixes in the d3d12_x objects, for problems titles are known to trigger: - **Fence event waits** (`objects/Fence.cpp`). Each `SetEventOnCompletion` with an event used to start a detached thread that held a reference to the fence until the value arrived. That caused three problems: - a value that never arrived leaked a blocked thread and kept the fence alive; - those threads could still be inside `vkWaitSemaphores` when the device went away at shutdown; - every wait cost a thread. Now one waiter thread per fence serves all its events: - it starts on the first event wait and keeps pending events ordered by value; - it waits for whichever comes first, the fence semaphore reaching the lowest pending value or a private wake semaphore (`VK_SEMAPHORE_WAIT_ANY_BIT`), which new waits and destruction signal under the lock so wake values stay increasing; - it holds no reference, and the fence's destructor stops and joins it, dropping events still pending; - if the wait fails (device lost), every pending event is signalled, as a removed D3D12 device does. - **Copy ordering** (`objects/CommandList.cpp`). Back-to-back `CopyBufferRegion`, `CopyTextureRegion`, `CopyResource` and `ResolveSubresource` calls with no barrier between them were recorded as unordered Vulkan transfers, so a second copy over the same range could land before the first. A transfer-to-transfer barrier now goes before each copy that follows another; a resource barrier clears that state. Titles rely on D3D12 copies running in order. This also serializes independent copies, a cost noted in the debt entry. - **Zero-count `RSSetViewports` and `RSSetScissorRects`** recorded an error and stopped the title; they now record nothing. ### Tested Windows 10, RTX 3070 Ti (driver 621.14), Khronos validation layer. - `pso_probe fence-gpu --validate`, now 96 checks: - 50 event waits on one fence (values 10 to 59): a CPU signal to 30 releases exactly the first 21, the other 29 stay pending, and a GPU signal to 59 releases the rest; - releasing a fence with a wait that is never reached returns at once and signals nothing. It passed 15 repeated runs. - `pso_probe commandlist-gpu --validate`, now 28 checks: a 4 KiB copy followed by a 2 KiB copy over its first half reads back the second copy's bytes; the pending-copy state is tracked; zero-count viewport and scissor calls don't stop the title. - `descriptors-gpu` still passes. 0 validation errors in all three. - MinGW `ctest`: 73 of 73. With MSVC 2022, `fence-gpu` and `commandlist-gpu` pass, and every target builds except `ab_gameinput`, which needs `gameinput.lib`, which this machine doesn't have. `python tools/check_conventions.py` and `debt_fold.py --check`: ok. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] `python tools/check_conventions.py` passes (no comments in code, one declaration per export, Conventional Commit subjects) - [x] Unimplemented paths stop (`AbxNotImplemented` / `AbxUnsupported`); silent stubs are listed under Silent stubs through a debt fragment - [x] Known gaps added, changed or removed through `docs/dev/debt/changes/<branch>.md` - [x] Generic behaviour, not specific to one title - [x] No game files, keys, notes or logs added to the repository - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] Source of behaviour: Vulkan specification (timeline semaphores, `VK_SEMAPHORE_WAIT_ANY_BIT`, transfer synchronization); D3D12 documentation (fence events, device removal) - [x] AI-assisted: yes
Event waits on a fence share one waiter thread that is joined when the fence is released, instead of a detached thread per wait that kept the fence alive and could outlive the device. Copies and resolves that follow each other without a barrier are ordered, and zero-count viewport and scissor calls no longer stop the title.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:17:12 +00:00
github manually merged commit 8829486664 into gh-pr/67/base 2026-11-26 19:17:13 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.