diff options
| author | thibaut <thibaut-leaux-angeli@live.fr> | 2026-11-23 18:50:06 +0100 |
|---|---|---|
| committer | thibaut <thibaut-leaux-angeli@live.fr> | 2026-11-23 18:50:06 +0100 |
| commit | 4336b7a2a0bed13d702247f29f8506efb0e7568b (patch) | |
| tree | a8ac0306ca9c21fe5aeb17deaa20d69227058103 /runtime/shims/d3d12_com.cpp | |
| parent | 4fb24d2faaa86e7287fa7da2670f54ea89e80fce (diff) | |
| parent | dde82f27e994337094afc476ded0b1426f6e87e2 (diff) | |
Merge remote-tracking branch 'origin/main' into slot133-host-va
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 111 |
1 files changed, 108 insertions, 3 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4dd766b31..e33769c58 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -202,12 +202,15 @@ public: | |||
| 202 | const std::vector<IUnknown*>& pinned() const { return held_; } | 202 | const std::vector<IUnknown*>& pinned() const { return held_; } |
| 203 | UINT front_stencil_ref() const { return front_stencil_ref_; } | 203 | UINT front_stencil_ref() const { return front_stencil_ref_; } |
| 204 | UINT back_stencil_ref() const { return back_stencil_ref_; } | 204 | UINT back_stencil_ref() const { return back_stencil_ref_; } |
| 205 | D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base() const { return cbv_srv_uav_base_; } | ||
| 206 | D3D12_GPU_VIRTUAL_ADDRESS sampler_base() const { return sampler_base_; } | ||
| 205 | 207 | ||
| 206 | D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override { | 208 | D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override { |
| 207 | d3d12_trace::record("ID3D12CommandList::GetType"); | 209 | d3d12_trace::record("ID3D12CommandList::GetType"); |
| 208 | return type_; | 210 | return type_; |
| 209 | } | 211 | } |
| 210 | D3D12_COMMAND_LIST_TYPE type() const { return type_; } | 212 | D3D12_COMMAND_LIST_TYPE type() const { return type_; } |
| 213 | bool invalid() const { return invalid_; } | ||
| 211 | 214 | ||
| 212 | // E_INVALIDARG when a call during recording was invalid, or a query was begun and not ended, as D3D12's Close | 215 | // E_INVALIDARG when a call during recording was invalid, or a query was begun and not ended, as D3D12's Close |
| 213 | // reports it. | 216 | // reports it. |
| @@ -248,6 +251,7 @@ public: | |||
| 248 | 251 | ||
| 249 | void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { | 252 | void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { |
| 250 | d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); | 253 | d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); |
| 254 | if (list_.closed()) return; | ||
| 251 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); | 255 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); |
| 252 | GraphicsCommandList* bundle = from(command_list); | 256 | GraphicsCommandList* bundle = from(command_list); |
| 253 | if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || | 257 | if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || |
| @@ -279,13 +283,47 @@ public: | |||
| 279 | 283 | ||
| 280 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { | 284 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { |
| 281 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); | 285 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); |
| 286 | if (list_.closed()) return; | ||
| 282 | if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state); | 287 | if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state); |
| 283 | set_pipeline(state); | 288 | set_pipeline(state); |
| 284 | } | 289 | } |
| 285 | 290 | ||
| 286 | // The bound heaps matter to direct heap indexing, which is still open (section 3.4); tables carry their VAs. | 291 | // Validates shader-visible descriptor heaps and tracks bound heap identities/bases (docs/d3d12_vulkan.md §3.2/§3.4). |
| 287 | void STDMETHODCALLTYPE SetDescriptorHeaps(UINT, ID3D12DescriptorHeap* const*) override { | 292 | void STDMETHODCALLTYPE SetDescriptorHeaps(UINT num, ID3D12DescriptorHeap* const* heaps) override { |
| 288 | d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps", "ignored"); | 293 | d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps"); |
| 294 | if (type_ == D3D12_COMMAND_LIST_TYPE_COPY) return check(false); | ||
| 295 | if (num > 2 || (num && !heaps)) return check(false); | ||
| 296 | |||
| 297 | ID3D12DescriptorHeap* cbv = nullptr; | ||
| 298 | ID3D12DescriptorHeap* smp = nullptr; | ||
| 299 | D3D12_GPU_VIRTUAL_ADDRESS cbv_base = 0; | ||
| 300 | D3D12_GPU_VIRTUAL_ADDRESS smp_base = 0; | ||
| 301 | IUnknown* const owner = this->attached_device(); | ||
| 302 | for (UINT i = 0; i < num; ++i) { | ||
| 303 | ID3D12DescriptorHeap* raw = heaps[i]; | ||
| 304 | if (!raw) return check(false); | ||
| 305 | if (ID3D12DescriptorHeap* real = d3d12_xs::heap_proxy_real_heap(raw)) raw = real; | ||
| 306 | D3D12_DESCRIPTOR_HEAP_DESC desc{}; | ||
| 307 | D3D12_GPU_VIRTUAL_ADDRESS base = 0; | ||
| 308 | IUnknown* const heap_owner = descriptor_heap_info(raw, desc, base); | ||
| 309 | if (!heap_owner || (owner && heap_owner != owner)) return check(false); | ||
| 310 | if (!(desc.Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)) return check(false); | ||
| 311 | if (desc.Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) { | ||
| 312 | if (cbv) return check(false); | ||
| 313 | cbv = raw; | ||
| 314 | cbv_base = base; | ||
| 315 | } else if (desc.Type == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER) { | ||
| 316 | if (smp) return check(false); | ||
| 317 | smp = raw; | ||
| 318 | smp_base = base; | ||
| 319 | } else { | ||
| 320 | return check(false); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | hold(cbv); | ||
| 324 | hold(smp); | ||
| 325 | if (defer([=](GraphicsCommandList& l) { l.verify_bundle_descriptor_heaps(cbv, smp); })) return; | ||
| 326 | set_descriptor_heaps(cbv, cbv_base, smp, smp_base); | ||
| 289 | } | 327 | } |
| 290 | 328 | ||
| 291 | void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { | 329 | void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { |
| @@ -334,12 +372,14 @@ public: | |||
| 334 | 372 | ||
| 335 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). | 373 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). |
| 336 | void STDMETHODCALLTYPE ClearState(ID3D12PipelineState* state) override { | 374 | void STDMETHODCALLTYPE ClearState(ID3D12PipelineState* state) override { |
| 375 | if (list_.closed()) return; | ||
| 337 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | 376 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); |
| 338 | reset_state(state); | 377 | reset_state(state); |
| 339 | } | 378 | } |
| 340 | 379 | ||
| 341 | // Graphics state. Draws record a copy of it (command_list.h, Draw). | 380 | // Graphics state. Draws record a copy of it (command_list.h, Draw). |
| 342 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { | 381 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { |
| 382 | if (list_.closed()) return; | ||
| 343 | if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return; | 383 | if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return; |
| 344 | topology_ = u(topology); | 384 | topology_ = u(topology); |
| 345 | } | 385 | } |
| @@ -358,6 +398,7 @@ public: | |||
| 358 | } | 398 | } |
| 359 | 399 | ||
| 360 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { | 400 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { |
| 401 | if (list_.closed()) return; | ||
| 361 | const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt; | 402 | const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt; |
| 362 | if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return; | 403 | if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return; |
| 363 | set_index_buffer(view); | 404 | set_index_buffer(view); |
| @@ -405,6 +446,7 @@ public: | |||
| 405 | } | 446 | } |
| 406 | 447 | ||
| 407 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { | 448 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { |
| 449 | if (list_.closed()) return; | ||
| 408 | const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} | 450 | const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} |
| 409 | : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } | 451 | : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } |
| 410 | if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return; | 452 | if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return; |
| @@ -412,6 +454,7 @@ public: | |||
| 412 | } | 454 | } |
| 413 | 455 | ||
| 414 | void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { | 456 | void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { |
| 457 | if (list_.closed()) return; | ||
| 415 | if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return; | 458 | if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return; |
| 416 | front_stencil_ref_ = back_stencil_ref_ = reference; | 459 | front_stencil_ref_ = back_stencil_ref_ = reference; |
| 417 | } | 460 | } |
| @@ -1141,6 +1184,10 @@ private: | |||
| 1141 | RootState graphics_; | 1184 | RootState graphics_; |
| 1142 | std::uintptr_t compute_pipeline_ = 0; | 1185 | std::uintptr_t compute_pipeline_ = 0; |
| 1143 | std::uintptr_t graphics_pipeline_ = 0; | 1186 | std::uintptr_t graphics_pipeline_ = 0; |
| 1187 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; | ||
| 1188 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; | ||
| 1189 | D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base_ = 0; | ||
| 1190 | D3D12_GPU_VIRTUAL_ADDRESS sampler_base_ = 0; | ||
| 1144 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED | 1191 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED |
| 1145 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; | 1192 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; |
| 1146 | leonida::IndexBufferView index_{}; | 1193 | leonida::IndexBufferView index_{}; |
| @@ -1205,10 +1252,44 @@ private: | |||
| 1205 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); | 1252 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); |
| 1206 | } | 1253 | } |
| 1207 | 1254 | ||
| 1255 | void set_descriptor_heaps(ID3D12DescriptorHeap* cbv, D3D12_GPU_VIRTUAL_ADDRESS cbv_base, | ||
| 1256 | ID3D12DescriptorHeap* smp, D3D12_GPU_VIRTUAL_ADDRESS smp_base) { | ||
| 1257 | cbv_srv_uav_heap_ = cbv; | ||
| 1258 | cbv_srv_uav_base_ = cbv_base; | ||
| 1259 | sampler_heap_ = smp; | ||
| 1260 | sampler_base_ = smp_base; | ||
| 1261 | } | ||
| 1262 | |||
| 1263 | // D3D12 bundle rule: every non-null heap bound by a bundle's SetDescriptorHeaps must match the parent list's | ||
| 1264 | // currently bound heap of that type, without clearing any omitted heap slot on the parent. | ||
| 1265 | void verify_bundle_descriptor_heaps(ID3D12DescriptorHeap* cbv, ID3D12DescriptorHeap* smp) { | ||
| 1266 | if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false); | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | void note_direct_heap_indexing(const RootArgumentLayout* layout) const { | ||
| 1270 | if (!layout) return; | ||
| 1271 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { | ||
| 1272 | if (!cbv_srv_uav_heap_) | ||
| 1273 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); | ||
| 1274 | else | ||
| 1275 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | ||
| 1276 | } | ||
| 1277 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | ||
| 1278 | if (!sampler_heap_) | ||
| 1279 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); | ||
| 1280 | else | ||
| 1281 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | ||
| 1282 | } | ||
| 1283 | } | ||
| 1284 | |||
| 1208 | void reset_state(ID3D12PipelineState* state) { | 1285 | void reset_state(ID3D12PipelineState* state) { |
| 1209 | front_stencil_ref_ = back_stencil_ref_ = 0; | 1286 | front_stencil_ref_ = back_stencil_ref_ = 0; |
| 1210 | compute_ = {}; | 1287 | compute_ = {}; |
| 1211 | graphics_ = {}; | 1288 | graphics_ = {}; |
| 1289 | cbv_srv_uav_heap_ = nullptr; | ||
| 1290 | sampler_heap_ = nullptr; | ||
| 1291 | cbv_srv_uav_base_ = 0; | ||
| 1292 | sampler_base_ = 0; | ||
| 1212 | topology_ = 0; | 1293 | topology_ = 0; |
| 1213 | vertex_buffers_ = {}; | 1294 | vertex_buffers_ = {}; |
| 1214 | index_ = {}; | 1295 | index_ = {}; |
| @@ -1237,6 +1318,7 @@ private: | |||
| 1237 | 1318 | ||
| 1238 | void record_dispatch(UINT x, UINT y, UINT z) { | 1319 | void record_dispatch(UINT x, UINT y, UINT z) { |
| 1239 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 1320 | if (!compute_pipeline_ || !compute_.layout) return check(false); |
| 1321 | note_direct_heap_indexing(compute_.layout); | ||
| 1240 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 1322 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); |
| 1241 | } | 1323 | } |
| 1242 | 1324 | ||
| @@ -1256,6 +1338,7 @@ private: | |||
| 1256 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1338 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1257 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1339 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1258 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1340 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1341 | note_direct_heap_indexing(graphics_.layout); | ||
| 1259 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1342 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, |
| 1260 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1343 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1261 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1344 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; |
| @@ -1438,6 +1521,12 @@ public: | |||
| 1438 | void STDMETHODCALLTYPE ExecuteCommandLists(UINT count, ID3D12CommandList* const* lists) override { | 1521 | void STDMETHODCALLTYPE ExecuteCommandLists(UINT count, ID3D12CommandList* const* lists) override { |
| 1439 | constexpr auto method = "ID3D12CommandQueue::ExecuteCommandLists"; | 1522 | constexpr auto method = "ID3D12CommandQueue::ExecuteCommandLists"; |
| 1440 | std::lock_guard lock(mutex_); | 1523 | std::lock_guard lock(mutex_); |
| 1524 | if (count > 0 && !lists) { | ||
| 1525 | note_once("ExecuteCommandLists: null command list array with non-zero count"); | ||
| 1526 | last_result_ = leonida::ReplayResult::unsupported; | ||
| 1527 | d3d12_trace::record(method, "failed"); | ||
| 1528 | return; | ||
| 1529 | } | ||
| 1441 | std::vector<const leonida::CommandList*> recorded; | 1530 | std::vector<const leonida::CommandList*> recorded; |
| 1442 | std::vector<const GraphicsCommandList*> graphics; | 1531 | std::vector<const GraphicsCommandList*> graphics; |
| 1443 | for (UINT i = 0; i < count; ++i) { | 1532 | for (UINT i = 0; i < count; ++i) { |
| @@ -1458,6 +1547,13 @@ public: | |||
| 1458 | d3d12_trace::record(method, "failed"); | 1547 | d3d12_trace::record(method, "failed"); |
| 1459 | return; | 1548 | return; |
| 1460 | } | 1549 | } |
| 1550 | // A list whose recording failed (Close returned E_INVALIDARG) must not replay its earlier commands. | ||
| 1551 | if (list->invalid()) { | ||
| 1552 | std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists: list %u is invalid; refusing batch\n", i); | ||
| 1553 | last_result_ = leonida::ReplayResult::unsupported; | ||
| 1554 | d3d12_trace::record(method, "failed"); | ||
| 1555 | return; | ||
| 1556 | } | ||
| 1461 | if (!timestamps_fit(*list)) { | 1557 | if (!timestamps_fit(*list)) { |
| 1462 | last_result_ = leonida::ReplayResult::invalid_query; | 1558 | last_result_ = leonida::ReplayResult::invalid_query; |
| 1463 | d3d12_trace::record(method, "failed"); | 1559 | d3d12_trace::record(method, "failed"); |
| @@ -1892,4 +1988,13 @@ bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Com | |||
| 1892 | return true; | 1988 | return true; |
| 1893 | } | 1989 | } |
| 1894 | 1990 | ||
| 1991 | bool bound_descriptor_heap_bases(ID3D12GraphicsCommandList* list, D3D12_GPU_VIRTUAL_ADDRESS& cbv_srv_uav, | ||
| 1992 | D3D12_GPU_VIRTUAL_ADDRESS& sampler) { | ||
| 1993 | const GraphicsCommandList* l = GraphicsCommandList::from(list); | ||
| 1994 | if (!l) return false; | ||
| 1995 | cbv_srv_uav = l->cbv_srv_uav_base(); | ||
| 1996 | sampler = l->sampler_base(); | ||
| 1997 | return true; | ||
| 1998 | } | ||
| 1999 | |||
| 1895 | } // namespace rt::d3d12 | 2000 | } // namespace rt::d3d12 |