diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 21:02:33 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 21:02:33 +0100 |
| commit | f8007dda67f9c45757b63df5c5fd8377647da4ae (patch) | |
| tree | cdffc5cc7c81dcbbf683af9c0df6077c6587ac0d /runtime/shims/d3d12_com.cpp | |
| parent | 9bebfb107070d84609e303009b0f91b011b9aa2e (diff) | |
Queue holds: review fixes (Present ordering, pins, timeline monotonicity, fence lifetime, teardown, docs)
- Present is queued behind held work (QueueHolds::after_held) and submitted in order by the releasing thread.
- Held batches keep the pipeline states and root signatures of their lists; fences a queue waits on or signals are
kept until the submission has completed on the GPU (a D3D12 queue holds them).
- A held Signal released below an unheld one on the same fence leaves the Vulkan signal out (claimed under the submit lock).
- A queue destroyed with held Signals completes the CPU waits on their fences and reports it.
- A failed held batch is reported (take_held_failure, last_execute_result), its waits stay for the next submission,
and the Signal behind it still signals.
- d3d12_queue_handover_test: pairs, two-thread signalling stress, held Signal of a released fence, shared fence,
teardown with a CPU waiter; --two-queues and --shared-queue modes on a caller-owned device.
- Tests for each fix in vk_presenter_sync_test, vk_queue_query_sync_test and d3d12_m4_compute_test; docs section 4.4.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index e1c8deca1..6240a02a1 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -178,6 +178,8 @@ public: | |||
| 178 | } | 178 | } |
| 179 | 179 | ||
| 180 | const leonida::CommandList& commands() const { return list_; } | 180 | const leonida::CommandList& commands() const { return list_; } |
| 181 | // The pipeline states and root signatures the recording names (it holds a reference to each until Reset). | ||
| 182 | const std::vector<IUnknown*>& pinned() const { return held_; } | ||
| 181 | UINT front_stencil_ref() const { return front_stencil_ref_; } | 183 | UINT front_stencil_ref() const { return front_stencil_ref_; } |
| 182 | UINT back_stencil_ref() const { return back_stencil_ref_; } | 184 | UINT back_stencil_ref() const { return back_stencil_ref_; } |
| 183 | 185 | ||
| @@ -1150,6 +1152,30 @@ public: | |||
| 1150 | // {e1a85d76-0b3f-4c29-a7d4-58f2c6e0b913} | 1152 | // {e1a85d76-0b3f-4c29-a7d4-58f2c6e0b913} |
| 1151 | static constexpr GUID kTag{0xe1a85d76, 0x0b3f, 0x4c29, {0xa7, 0xd4, 0x58, 0xf2, 0xc6, 0xe0, 0xb9, 0x13}}; | 1153 | static constexpr GUID kTag{0xe1a85d76, 0x0b3f, 0x4c29, {0xa7, 0xd4, 0x58, 0xf2, 0xc6, 0xe0, 0xb9, 0x13}}; |
| 1152 | 1154 | ||
| 1155 | // What a held batch (queue_holds.h) keeps alive until it has run: the pipeline states and root signatures its | ||
| 1156 | // lists name, which the application may release and Reset away as soon as ExecuteCommandLists returns. Resources | ||
| 1157 | // are not held (a D3D12 application keeps them until the fence it signals; the docs, section 4.4, list this). | ||
| 1158 | static std::shared_ptr<void> pin_lists(const std::vector<const GraphicsCommandList*>& lists) { | ||
| 1159 | auto objects = std::shared_ptr<std::vector<IUnknown*>>(new std::vector<IUnknown*>, [](auto* held) { | ||
| 1160 | for (IUnknown* object : *held) object->Release(); | ||
| 1161 | delete held; | ||
| 1162 | }); | ||
| 1163 | for (const GraphicsCommandList* list : lists) | ||
| 1164 | for (IUnknown* object : list->pinned()) { | ||
| 1165 | object->AddRef(); | ||
| 1166 | objects->push_back(object); | ||
| 1167 | } | ||
| 1168 | return objects; | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | // A D3D12 queue holds the fences it waits on and signals until the work has been submitted. | ||
| 1172 | static leonida::VulkanQueue::PinFn fence_pin(ID3D12Fence* fence) { | ||
| 1173 | return [fence]() -> std::shared_ptr<void> { | ||
| 1174 | fence->AddRef(); | ||
| 1175 | return std::shared_ptr<void>(fence, [](void* held) { static_cast<ID3D12Fence*>(held)->Release(); }); | ||
| 1176 | }; | ||
| 1177 | } | ||
| 1178 | |||
| 1153 | static leonida::VulkanQueue::QueueKind queue_kind_for(D3D12_COMMAND_LIST_TYPE type) { | 1179 | static leonida::VulkanQueue::QueueKind queue_kind_for(D3D12_COMMAND_LIST_TYPE type) { |
| 1154 | if (type == D3D12_COMMAND_LIST_TYPE_COPY) return leonida::VulkanQueue::QueueKind::copy; | 1180 | if (type == D3D12_COMMAND_LIST_TYPE_COPY) return leonida::VulkanQueue::QueueKind::copy; |
| 1155 | if (type == D3D12_COMMAND_LIST_TYPE_COMPUTE) return leonida::VulkanQueue::QueueKind::compute; | 1181 | if (type == D3D12_COMMAND_LIST_TYPE_COMPUTE) return leonida::VulkanQueue::QueueKind::compute; |
| @@ -1170,7 +1196,11 @@ public: | |||
| 1170 | } | 1196 | } |
| 1171 | 1197 | ||
| 1172 | bool ok() const { return queue_.ok(); } | 1198 | bool ok() const { return queue_.ok(); } |
| 1173 | leonida::ReplayResult last_result() const { return last_result_; } | 1199 | leonida::ReplayResult last_result() { |
| 1200 | // A batch the queue held fails when it is released, after the call returned: the failure is the result. | ||
| 1201 | if (const auto held = queue_.take_held_failure(); held != leonida::ReplayResult::success) last_result_ = held; | ||
| 1202 | return last_result_; | ||
| 1203 | } | ||
| 1174 | 1204 | ||
| 1175 | D3D12_COMMAND_QUEUE_DESC STDMETHODCALLTYPE GetDesc() override { | 1205 | D3D12_COMMAND_QUEUE_DESC STDMETHODCALLTYPE GetDesc() override { |
| 1176 | d3d12_trace::record("ID3D12CommandQueue::GetDesc"); | 1206 | d3d12_trace::record("ID3D12CommandQueue::GetDesc"); |
| @@ -1183,6 +1213,7 @@ public: | |||
| 1183 | constexpr auto method = "ID3D12CommandQueue::ExecuteCommandLists"; | 1213 | constexpr auto method = "ID3D12CommandQueue::ExecuteCommandLists"; |
| 1184 | std::lock_guard lock(mutex_); | 1214 | std::lock_guard lock(mutex_); |
| 1185 | std::vector<const leonida::CommandList*> recorded; | 1215 | std::vector<const leonida::CommandList*> recorded; |
| 1216 | std::vector<const GraphicsCommandList*> graphics; | ||
| 1186 | for (UINT i = 0; i < count; ++i) { | 1217 | for (UINT i = 0; i < count; ++i) { |
| 1187 | const GraphicsCommandList* list = GraphicsCommandList::from(lists[i]); | 1218 | const GraphicsCommandList* list = GraphicsCommandList::from(lists[i]); |
| 1188 | if (!list || list->type() == D3D12_COMMAND_LIST_TYPE_BUNDLE) { | 1219 | if (!list || list->type() == D3D12_COMMAND_LIST_TYPE_BUNDLE) { |
| @@ -1197,8 +1228,9 @@ public: | |||
| 1197 | return; | 1228 | return; |
| 1198 | } | 1229 | } |
| 1199 | recorded.push_back(&list->commands()); | 1230 | recorded.push_back(&list->commands()); |
| 1231 | graphics.push_back(list); | ||
| 1200 | } | 1232 | } |
| 1201 | last_result_ = queue_.execute(recorded); | 1233 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); |
| 1202 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); | 1234 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); |
| 1203 | if (last_result_ != leonida::ReplayResult::success) | 1235 | if (last_result_ != leonida::ReplayResult::success) |
| 1204 | std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists failed: %s; nothing was submitted\n", | 1236 | std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists failed: %s; nothing was submitted\n", |
| @@ -1256,7 +1288,7 @@ public: | |||
| 1256 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); | 1288 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); |
| 1257 | std::lock_guard lock(mutex_); | 1289 | std::lock_guard lock(mutex_); |
| 1258 | if (!f->claim(value)) return d3d12_trace::result(method, S_OK); | 1290 | if (!f->claim(value)) return d3d12_trace::result(method, S_OK); |
| 1259 | return d3d12_trace::result(method, queue_.signal(f->semaphore(), value) ? S_OK : E_FAIL); | 1291 | return d3d12_trace::result(method, queue_.signal(f->semaphore(), value, fence_pin(fence)) ? S_OK : E_FAIL); |
| 1260 | } | 1292 | } |
| 1261 | 1293 | ||
| 1262 | HRESULT STDMETHODCALLTYPE Wait(ID3D12Fence* fence, UINT64 value) override { | 1294 | HRESULT STDMETHODCALLTYPE Wait(ID3D12Fence* fence, UINT64 value) override { |
| @@ -1264,7 +1296,7 @@ public: | |||
| 1264 | Fence* f = Fence::from(fence); | 1296 | Fence* f = Fence::from(fence); |
| 1265 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); | 1297 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); |
| 1266 | std::lock_guard lock(mutex_); | 1298 | std::lock_guard lock(mutex_); |
| 1267 | queue_.wait(f->semaphore(), value); | 1299 | queue_.wait(f->semaphore(), value, fence_pin(fence)); |
| 1268 | return d3d12_trace::result(method, S_OK); | 1300 | return d3d12_trace::result(method, S_OK); |
| 1269 | } | 1301 | } |
| 1270 | 1302 | ||
| @@ -1591,7 +1623,7 @@ bool command_signature_info(ID3D12CommandSignature* signature, leonida::Indirect | |||
| 1591 | 1623 | ||
| 1592 | leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue) { | 1624 | leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue) { |
| 1593 | if (ID3D12CommandQueue* real = d3d12_xs::queue_proxy_real_queue(queue)) queue = real; // the Xbox-ABI proxy | 1625 | if (ID3D12CommandQueue* real = d3d12_xs::queue_proxy_real_queue(queue)) queue = real; // the Xbox-ABI proxy |
| 1594 | const CommandQueue* q = CommandQueue::from(queue); | 1626 | CommandQueue* q = CommandQueue::from(queue); |
| 1595 | return q ? q->last_result() : leonida::ReplayResult::unsupported; | 1627 | return q ? q->last_result() : leonida::ReplayResult::unsupported; |
| 1596 | } | 1628 | } |
| 1597 | 1629 | ||