diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-23 02:15:15 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-23 02:15:15 +0100 |
| commit | 003baa94c1fcba61e324dbeb947e8bd401f4f837 (patch) | |
| tree | 10c60314513d90af84a03ad7e814b0772ba12db8 /runtime/shims/d3d12_com.cpp | |
| parent | 48cee7c2c69e502cc864b0789a9cf5bb88e98658 (diff) | |
| parent | 03b09a086592a197db64d91655c6e65c30764d96 (diff) | |
Merge origin/main into queue-wait-before-signal
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 91535a389..5190b094c 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -249,6 +249,7 @@ public: | |||
| 249 | // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords | 249 | // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords |
| 250 | // the translated shaders read (root_signature.h; docs section 3.5); a draw or dispatch records a copy. | 250 | // the translated shaders read (root_signature.h; docs section 3.5); a draw or dispatch records a copy. |
| 251 | void STDMETHODCALLTYPE SetComputeRootSignature(ID3D12RootSignature* signature) override { | 251 | void STDMETHODCALLTYPE SetComputeRootSignature(ID3D12RootSignature* signature) override { |
| 252 | d3d12_trace::record("ID3D12GraphicsCommandList::SetComputeRootSignature"); | ||
| 252 | bind_root(compute_, signature); | 253 | bind_root(compute_, signature); |
| 253 | } | 254 | } |
| 254 | void STDMETHODCALLTYPE SetGraphicsRootSignature(ID3D12RootSignature* signature) override { | 255 | void STDMETHODCALLTYPE SetGraphicsRootSignature(ID3D12RootSignature* signature) override { |
| @@ -306,12 +307,16 @@ public: | |||
| 306 | } | 307 | } |
| 307 | 308 | ||
| 308 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { | 309 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { |
| 310 | d3d12_trace::record("ID3D12GraphicsCommandList::Dispatch"); | ||
| 309 | if (defer([=](GraphicsCommandList& l) { l.record_dispatch(x, y, z); }, true)) return; | 311 | if (defer([=](GraphicsCommandList& l) { l.record_dispatch(x, y, z); }, true)) return; |
| 310 | record_dispatch(x, y, z); | 312 | record_dispatch(x, y, z); |
| 311 | } | 313 | } |
| 312 | 314 | ||
| 313 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). | 315 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). |
| 314 | void STDMETHODCALLTYPE ClearState(ID3D12PipelineState* state) override { reset_state(state); } | 316 | void STDMETHODCALLTYPE ClearState(ID3D12PipelineState* state) override { |
| 317 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | ||
| 318 | reset_state(state); | ||
| 319 | } | ||
| 315 | 320 | ||
| 316 | // Graphics state. Draws record a copy of it (command_list.h, Draw). | 321 | // Graphics state. Draws record a copy of it (command_list.h, Draw). |
| 317 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { | 322 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { |
| @@ -340,6 +345,7 @@ public: | |||
| 340 | 345 | ||
| 341 | // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). | 346 | // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). |
| 342 | void STDMETHODCALLTYPE RSSetViewports(UINT count, const D3D12_VIEWPORT* viewports) override { | 347 | void STDMETHODCALLTYPE RSSetViewports(UINT count, const D3D12_VIEWPORT* viewports) override { |
| 348 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | ||
| 343 | if (!count) { | 349 | if (!count) { |
| 344 | viewport_ = {}; | 350 | viewport_ = {}; |
| 345 | return; | 351 | return; |
| @@ -351,6 +357,7 @@ public: | |||
| 351 | } | 357 | } |
| 352 | 358 | ||
| 353 | void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override { | 359 | void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override { |
| 360 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | ||
| 354 | if (!count || !rects) return; | 361 | if (!count || !rects) return; |
| 355 | if (count > 1) stub("ID3D12GraphicsCommandList::RSSetScissorRects (more than one rectangle; the first is used)"); | 362 | if (count > 1) stub("ID3D12GraphicsCommandList::RSSetScissorRects (more than one rectangle; the first is used)"); |
| 356 | scissor_ = {rects[0].left, rects[0].top, rects[0].right, rects[0].bottom}; | 363 | scissor_ = {rects[0].left, rects[0].top, rects[0].right, rects[0].bottom}; |
| @@ -359,6 +366,7 @@ public: | |||
| 359 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. | 366 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. |
| 360 | void STDMETHODCALLTYPE OMSetRenderTargets(UINT count, const D3D12_CPU_DESCRIPTOR_HANDLE* targets, BOOL single_range, | 367 | void STDMETHODCALLTYPE OMSetRenderTargets(UINT count, const D3D12_CPU_DESCRIPTOR_HANDLE* targets, BOOL single_range, |
| 361 | const D3D12_CPU_DESCRIPTOR_HANDLE* depth) override { | 368 | const D3D12_CPU_DESCRIPTOR_HANDLE* depth) override { |
| 369 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | ||
| 362 | if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); | 370 | if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); |
| 363 | render_targets_.fill({}); | 371 | render_targets_.fill({}); |
| 364 | render_target_count_ = count; | 372 | render_target_count_ = count; |
| @@ -1100,13 +1108,10 @@ public: | |||
| 1100 | VkSemaphore semaphore() const { return fence_.semaphore(); } | 1108 | VkSemaphore semaphore() const { return fence_.semaphore(); } |
| 1101 | 1109 | ||
| 1102 | // A Vulkan timeline only increases; D3D12 allows setting a fence lower. Such a signal is reported and dropped. | 1110 | // A Vulkan timeline only increases; D3D12 allows setting a fence lower. Such a signal is reported and dropped. |
| 1103 | bool claim(UINT64 value) { | 1111 | // A queue's claim also records the value as one the GPU will signal, which a later CPU signal must not overtake. |
| 1104 | UINT64 current = highest_.load(); | 1112 | bool claim(UINT64 value, bool queue = false) { |
| 1105 | while (value > current) | 1113 | std::lock_guard lock(claim_mutex_); |
| 1106 | if (highest_.compare_exchange_weak(current, value)) return true; | 1114 | return claim_locked(value, queue); |
| 1107 | std::fprintf(stderr, "[rt] d3d12: fence signal %llu is not above %llu; Vulkan timelines cannot go back\n", | ||
| 1108 | static_cast<unsigned long long>(value), static_cast<unsigned long long>(current)); | ||
| 1109 | return false; | ||
| 1110 | } | 1115 | } |
| 1111 | 1116 | ||
| 1112 | UINT64 STDMETHODCALLTYPE GetCompletedValue() override { | 1117 | UINT64 STDMETHODCALLTYPE GetCompletedValue() override { |
| @@ -1114,8 +1119,32 @@ public: | |||
| 1114 | } | 1119 | } |
| 1115 | D3D12_FENCE_FLAGS STDMETHODCALLTYPE GetCreationFlags() override { return flags_; } | 1120 | D3D12_FENCE_FLAGS STDMETHODCALLTYPE GetCreationFlags() override { return flags_; } |
| 1116 | 1121 | ||
| 1122 | // vkSignalSemaphore must stay below every signal still pending on the GPU | ||
| 1123 | // (VUID-VkSemaphoreSignalInfo-value-03259). A CPU signal above a queue signal the GPU has not reached yet, or | ||
| 1124 | // made while an earlier one is held, is held: the fence dispatcher applies it once the GPU passes that queue | ||
| 1125 | // value, in the order the signals were made. Until then GetCompletedValue reads the GPU's progress, where D3D12 | ||
| 1126 | // would already read `value`. | ||
| 1117 | HRESULT STDMETHODCALLTYPE Signal(UINT64 value) override { | 1127 | HRESULT STDMETHODCALLTYPE Signal(UINT64 value) override { |
| 1118 | if (claim(value) && !fence_.signal(value)) return E_FAIL; | 1128 | std::lock_guard lock(claim_mutex_); |
| 1129 | if (!claim_locked(value, false)) return S_OK; | ||
| 1130 | const UINT64 queued = queued_; | ||
| 1131 | if (held_->load() == 0 && fence_.completed() >= queued) return fence_.signal(value) ? S_OK : E_FAIL; | ||
| 1132 | held_->fetch_add(1); | ||
| 1133 | // The callback must not touch the fence, which may be released first. Released (cancel) or device lost, | ||
| 1134 | // the queue value may not be reached, and signalling then would break the same rule: the signal is dropped. | ||
| 1135 | leonida::VulkanContext* context = &context_; | ||
| 1136 | const VkSemaphore semaphore = fence_.semaphore(); | ||
| 1137 | context_.enqueue_fence_callback(semaphore, queued, [context, semaphore, queued, value, held = held_] { | ||
| 1138 | std::uint64_t now = 0; | ||
| 1139 | context->vk.vkGetSemaphoreCounterValue(context->device(), semaphore, &now); | ||
| 1140 | if (now >= queued && now < value) { | ||
| 1141 | VkSemaphoreSignalInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; | ||
| 1142 | info.semaphore = semaphore; | ||
| 1143 | info.value = value; | ||
| 1144 | context->vk.vkSignalSemaphore(context->device(), &info); | ||
| 1145 | } | ||
| 1146 | held->fetch_sub(1); | ||
| 1147 | }); | ||
| 1119 | return S_OK; | 1148 | return S_OK; |
| 1120 | } | 1149 | } |
| 1121 | 1150 | ||
| @@ -1142,10 +1171,24 @@ public: | |||
| 1142 | static Fence* from(IUnknown* object) { return Child::from<Fence>(object); } | 1171 | static Fence* from(IUnknown* object) { return Child::from<Fence>(object); } |
| 1143 | 1172 | ||
| 1144 | private: | 1173 | private: |
| 1174 | bool claim_locked(UINT64 value, bool queue) { | ||
| 1175 | if (value <= highest_) { | ||
| 1176 | std::fprintf(stderr, "[rt] d3d12: fence signal %llu is not above %llu; Vulkan timelines cannot go back\n", | ||
| 1177 | static_cast<unsigned long long>(value), static_cast<unsigned long long>(highest_)); | ||
| 1178 | return false; | ||
| 1179 | } | ||
| 1180 | highest_ = value; | ||
| 1181 | if (queue) queued_ = value; | ||
| 1182 | return true; | ||
| 1183 | } | ||
| 1184 | |||
| 1145 | leonida::VulkanContext& context_; | 1185 | leonida::VulkanContext& context_; |
| 1146 | leonida::VulkanFence fence_; | 1186 | leonida::VulkanFence fence_; |
| 1147 | D3D12_FENCE_FLAGS flags_; | 1187 | D3D12_FENCE_FLAGS flags_; |
| 1148 | std::atomic<UINT64> highest_; | 1188 | std::mutex claim_mutex_; |
| 1189 | UINT64 highest_; // the highest value claimed, from either side | ||
| 1190 | UINT64 queued_ = 0; // the highest value a queue will signal | ||
| 1191 | std::shared_ptr<std::atomic<int>> held_ = std::make_shared<std::atomic<int>>(0); // CPU signals not applied yet | ||
| 1149 | }; | 1192 | }; |
| 1150 | 1193 | ||
| 1151 | class CommandQueue final | 1194 | class CommandQueue final |
| @@ -1290,7 +1333,7 @@ public: | |||
| 1290 | Fence* f = Fence::from(fence); | 1333 | Fence* f = Fence::from(fence); |
| 1291 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); | 1334 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); |
| 1292 | std::lock_guard lock(mutex_); | 1335 | std::lock_guard lock(mutex_); |
| 1293 | if (!f->claim(value)) return d3d12_trace::result(method, S_OK); | 1336 | if (!f->claim(value, true)) return d3d12_trace::result(method, S_OK); |
| 1294 | return d3d12_trace::result(method, queue_.signal(f->semaphore(), value, fence_pin(fence)) ? S_OK : E_FAIL); | 1337 | return d3d12_trace::result(method, queue_.signal(f->semaphore(), value, fence_pin(fence)) ? S_OK : E_FAIL); |
| 1295 | } | 1338 | } |
| 1296 | 1339 | ||