diff options
| author | cixotdev <339371196+cixotdev@users.noreply.github.com> | 2026-11-22 21:17:10 -0300 |
|---|---|---|
| committer | cixotdev <339371196+cixotdev@users.noreply.github.com> | 2026-11-22 21:17:10 -0300 |
| commit | 8dfe9bce1a3cf078685db7a8da88b3b2b47e07a8 (patch) | |
| tree | f2763ee918e8b01b7457b17d8ff59525606cc9dc /runtime | |
| parent | aa2988cc555bad7ecd508b48a702adbb32fe7c95 (diff) | |
runtime: notify QueueHolds when a held CPU Fence::Signal fires
When ID3D12Fence::Signal is deferred behind a pending GPU queue signal,
Fence::Signal enqueues a FenceWaitDispatcher callback that calls
vkSignalSemaphore directly instead of going through VulkanFence::signal.
That callback previously never notified QueueHolds via
holds().signaled(semaphore, value) and holds().pump_ready(), leaving any
cross-queue Wait-before-Signal batch stalled on that fence value
forever even after GetCompletedValue() reached it.
Record whether vkSignalSemaphore succeeded, decrement held_ first, and
then call holds().signaled(semaphore, value) and holds().pump_ready()
with no dispatcher or queue lock held. Add cross-queue regression cases
to check_held_cpu_signal in d3d12_fence_timing_test.cpp covering both a
Wait queued before the held CPU Signal and a Wait queued while the CPU
Signal is already held.
Claim: https://github.com/ItsNotRexed/Leonida/issues/4084#issuecomment-7088187469
AI-assisted: yes (Antigravity)
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 11 | ||||
| -rw-r--r-- | runtime/test/d3d12_fence_timing_test.cpp | 46 |
2 files changed, 54 insertions, 3 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index d9b1a009c..fc664a52b 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -1134,14 +1134,19 @@ public: | |||
| 1134 | const VkSemaphore semaphore = fence_.semaphore(); | 1134 | const VkSemaphore semaphore = fence_.semaphore(); |
| 1135 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { | 1135 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { |
| 1136 | std::uint64_t now = 0; | 1136 | std::uint64_t now = 0; |
| 1137 | context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now); | 1137 | bool signaled = false; |
| 1138 | if (now >= queued && now < value) { | 1138 | if (context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now) == VK_SUCCESS && |
| 1139 | now >= queued && now < value) { | ||
| 1139 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; | 1140 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; |
| 1140 | info.semaphore = semaphore; | 1141 | info.semaphore = semaphore; |
| 1141 | info.value = value; | 1142 | info.value = value; |
| 1142 | context->vk.vkSignalSemaphore(context->device(), &info); | 1143 | signaled = context->vk.vkSignalSemaphore(context->device(), &info) == VK_SUCCESS; |
| 1143 | } | 1144 | } |
| 1144 | held->fetch_sub(1); | 1145 | held->fetch_sub(1); |
| 1146 | if (signaled) { | ||
| 1147 | context->holds().signaled(semaphore, value); | ||
| 1148 | context->holds().pump_ready(); | ||
| 1149 | } | ||
| 1145 | }); | 1150 | }); |
| 1146 | return S_OK; | 1151 | return S_OK; |
| 1147 | } | 1152 | } |
diff --git a/runtime/test/d3d12_fence_timing_test.cpp b/runtime/test/d3d12_fence_timing_test.cpp index 89ad8df49..8e543a7cb 100644 --- a/runtime/test/d3d12_fence_timing_test.cpp +++ b/runtime/test/d3d12_fence_timing_test.cpp | |||
| @@ -193,6 +193,52 @@ int check_held_cpu_signal(ID3D12Device* device, ID3D12CommandQueue* queue) { | |||
| 193 | EXPECT(SUCCEEDED(gate->Signal(2))); | 193 | EXPECT(SUCCEEDED(gate->Signal(2))); |
| 194 | EXPECT(SUCCEEDED(f->SetEventOnCompletion(50, nullptr))); | 194 | EXPECT(SUCCEEDED(f->SetEventOnCompletion(50, nullptr))); |
| 195 | EXPECT(f->GetCompletedValue() == 50); | 195 | EXPECT(f->GetCompletedValue() == 50); |
| 196 | |||
| 197 | // A cross-queue Wait held in QueueHolds for a deferred CPU Signal must wake when the dispatcher applies it. | ||
| 198 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 199 | Ref<ID3D12CommandQueue> queue_b; | ||
| 200 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue_b.p)))); | ||
| 201 | Ref<ID3D12Fence> done; | ||
| 202 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&done.p)))); | ||
| 203 | |||
| 204 | EXPECT(SUCCEEDED(queue->Wait(gate.p, 3))); | ||
| 205 | EXPECT(SUCCEEDED(queue->Signal(f.p, 60))); | ||
| 206 | EXPECT(SUCCEEDED(queue_b->Wait(f.p, 70))); | ||
| 207 | EXPECT(SUCCEEDED(queue_b->Signal(done.p, 1))); | ||
| 208 | EXPECT(SUCCEEDED(f->Signal(70))); | ||
| 209 | Event done_ev; | ||
| 210 | EXPECT(SUCCEEDED(done->SetEventOnCompletion(1, done_ev.h))); | ||
| 211 | EXPECT(!done_ev.set_within(50)); | ||
| 212 | EXPECT(done->GetCompletedValue() == 0); | ||
| 213 | EXPECT(SUCCEEDED(gate->Signal(3))); | ||
| 214 | if (!done_ev.set_within(5000)) { | ||
| 215 | std::fprintf(stderr, "held CPU signal did not wake QueueHolds: f=%llu done=%llu\n", | ||
| 216 | static_cast<unsigned long long>(f->GetCompletedValue()), | ||
| 217 | static_cast<unsigned long long>(done->GetCompletedValue())); | ||
| 218 | return 1; | ||
| 219 | } | ||
| 220 | EXPECT(done->GetCompletedValue() == 1); | ||
| 221 | EXPECT(f->GetCompletedValue() == 70); | ||
| 222 | |||
| 223 | // Same release when the Wait is queued while the CPU signal is already held behind the GPU signal. | ||
| 224 | EXPECT(SUCCEEDED(queue->Wait(gate.p, 4))); | ||
| 225 | EXPECT(SUCCEEDED(queue->Signal(f.p, 80))); | ||
| 226 | EXPECT(SUCCEEDED(f->Signal(90))); | ||
| 227 | EXPECT(SUCCEEDED(queue_b->Wait(f.p, 90))); | ||
| 228 | EXPECT(SUCCEEDED(queue_b->Signal(done.p, 2))); | ||
| 229 | Event done_ev2; | ||
| 230 | EXPECT(SUCCEEDED(done->SetEventOnCompletion(2, done_ev2.h))); | ||
| 231 | EXPECT(!done_ev2.set_within(50)); | ||
| 232 | EXPECT(done->GetCompletedValue() == 1); | ||
| 233 | EXPECT(SUCCEEDED(gate->Signal(4))); | ||
| 234 | if (!done_ev2.set_within(5000)) { | ||
| 235 | std::fprintf(stderr, "already-held CPU signal did not wake QueueHolds: f=%llu done=%llu\n", | ||
| 236 | static_cast<unsigned long long>(f->GetCompletedValue()), | ||
| 237 | static_cast<unsigned long long>(done->GetCompletedValue())); | ||
| 238 | return 1; | ||
| 239 | } | ||
| 240 | EXPECT(done->GetCompletedValue() == 2); | ||
| 241 | EXPECT(f->GetCompletedValue() == 90); | ||
| 196 | return 0; | 242 | return 0; |
| 197 | } | 243 | } |
| 198 | 244 | ||