upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

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