diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 17:15:39 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 17:15:39 -0500 |
| commit | 438859b525e8a4e8c55669da0252f143a6d49751 (patch) | |
| tree | b2baee7c71ed4105cca6d4a841882f2a2786aa63 /runtime/shims/d3d12_com.cpp | |
| parent | 88b8dbad9d59aab22da34c49035bd7841c822633 (diff) | |
| parent | 9f25f1ef2c9000c3d463bc4f5358cfcfd41a0c76 (diff) | |
Merge main into RT-SLOT41 (#377)
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 | 93 |
1 files changed, 78 insertions, 15 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index cf02df519..7c5244141 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -411,12 +411,20 @@ public: | |||
| 411 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | 411 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); |
| 412 | if (!count) { | 412 | if (!count) { |
| 413 | viewport_ = {}; | 413 | viewport_ = {}; |
| 414 | viewports_.fill({}); | ||
| 415 | viewport_count_ = 0; | ||
| 416 | draw_viewports_stale_ = true; | ||
| 414 | return; | 417 | return; |
| 415 | } | 418 | } |
| 416 | if (!viewports || count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false); | 419 | if (!viewports || count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false); |
| 417 | if (count > 1) stub("ID3D12GraphicsCommandList::RSSetViewports (more than one viewport; the first is used)"); | 420 | viewports_.fill({}); |
| 418 | const D3D12_VIEWPORT& v = viewports[0]; | 421 | for (UINT i = 0; i < count; ++i) { |
| 419 | viewport_ = {v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth}; | 422 | const D3D12_VIEWPORT& v = viewports[i]; |
| 423 | viewports_[i] = {v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth}; | ||
| 424 | } | ||
| 425 | viewport_count_ = count; | ||
| 426 | viewport_ = viewports_[0]; | ||
| 427 | draw_viewports_stale_ = true; | ||
| 420 | } | 428 | } |
| 421 | 429 | ||
| 422 | void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override { | 430 | void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override { |
| @@ -425,11 +433,25 @@ public: | |||
| 425 | // With no scissor bound, the rasterizer clips every fragment, as on native D3D12. | 433 | // With no scissor bound, the rasterizer clips every fragment, as on native D3D12. |
| 426 | if (!count) { | 434 | if (!count) { |
| 427 | scissor_ = {}; | 435 | scissor_ = {}; |
| 436 | scissors_.fill({}); | ||
| 437 | scissor_count_ = 0; | ||
| 438 | draw_viewports_stale_ = true; | ||
| 428 | return; | 439 | return; |
| 429 | } | 440 | } |
| 430 | if (!rects) return; | 441 | if (!rects) return; |
| 431 | if (count > 1) stub("ID3D12GraphicsCommandList::RSSetScissorRects (more than one rectangle; the first is used)"); | 442 | // WARP still closes a list after RSSetScissorRects(17) (d3d12_close_parity), so the call is not an error: |
| 432 | scissor_ = {rects[0].left, rects[0].top, rects[0].right, rects[0].bottom}; | 443 | // the rectangles past the 16 a pipeline can have are dropped. |
| 444 | if (count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) { | ||
| 445 | stub("ID3D12GraphicsCommandList::RSSetScissorRects(more than 16 rectangles; the first 16 are used)"); | ||
| 446 | count = D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; | ||
| 447 | } | ||
| 448 | scissors_.fill({}); | ||
| 449 | for (UINT i = 0; i < count; ++i) { | ||
| 450 | scissors_[i] = {rects[i].left, rects[i].top, rects[i].right, rects[i].bottom}; | ||
| 451 | } | ||
| 452 | scissor_count_ = count; | ||
| 453 | scissor_ = scissors_[0]; | ||
| 454 | draw_viewports_stale_ = true; | ||
| 433 | } | 455 | } |
| 434 | 456 | ||
| 435 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. | 457 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. |
| @@ -486,10 +508,11 @@ public: | |||
| 486 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); | 508 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); |
| 487 | if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); | 509 | if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); |
| 488 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); | 510 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); |
| 489 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized | 511 | if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized |
| 490 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, | 512 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, |
| 491 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; | 513 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; |
| 492 | draw.mesh = true; | 514 | draw.mesh = true; |
| 515 | draw.viewports = draw_viewports(); | ||
| 493 | check(list_.draw(std::move(draw))); | 516 | check(list_.draw(std::move(draw))); |
| 494 | } | 517 | } |
| 495 | 518 | ||
| @@ -835,9 +858,7 @@ public: | |||
| 835 | return check(false); | 858 | return check(false); |
| 836 | } | 859 | } |
| 837 | // The record size is checked for every call: D3D12 rejects a signature whose stride cannot hold a record | 860 | // The record size is checked for every call: D3D12 rejects a signature whose stride cannot hold a record |
| 838 | // even when no command runs. The argument span is what the signature can walk, max_command_count * stride: | 861 | // even when no command runs. |
| 839 | // the host refuses a range that ends past the buffer (`240` with stride `20` in a `256`-byte buffer), and | ||
| 840 | // it checks the offset itself when max_command_count is 0, where the required bytes are none. | ||
| 841 | const UINT64 record_size = | 862 | const UINT64 record_size = |
| 842 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) | 863 | kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) |
| 843 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) | 864 | : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) |
| @@ -845,9 +866,18 @@ public: | |||
| 845 | : kind == leonida::IndirectKind::dispatch_mesh ? sizeof(D3D12_DISPATCH_MESH_ARGUMENTS) | 866 | : kind == leonida::IndirectKind::dispatch_mesh ? sizeof(D3D12_DISPATCH_MESH_ARGUMENTS) |
| 846 | : 0; | 867 | : 0; |
| 847 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); | 868 | if (!record_size || stride < record_size || stride % 4 != 0) return check(false); |
| 848 | if (argument_buffer_offset > arg_desc.Width || | 869 | // The argument range is the bytes the commands read: (max_command_count - 1) * stride + record_size, and |
| 849 | UINT64(max_command_count) > (arg_desc.Width - argument_buffer_offset) / stride) | 870 | // nothing when max_command_count is 0. PC D3D12 runtimes do not agree here. Some bound max_command_count * |
| 850 | return check(false); | 871 | // stride and check the offset at a zero count; others (D3D12Core 10.0.26100.9818, WARP and NVIDIA alike) |
| 872 | // accept a last record that fits while its stride runs past the buffer, and any aligned offset at a zero | ||
| 873 | // count. The layer takes the wider rule: it is the one Vulkan's own indirect draws use, no byte outside the | ||
| 874 | // buffer is read, and refusing here drops the whole command list for a call a real runtime runs. | ||
| 875 | // d3d12_execute_indirect_validation_test lists the five cases the runtimes split on. | ||
| 876 | if (max_command_count) { | ||
| 877 | const UINT64 required = UINT64(max_command_count - 1) * stride + record_size; | ||
| 878 | if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required) | ||
| 879 | return check(false); | ||
| 880 | } | ||
| 851 | if (kind == leonida::IndirectKind::dispatch_mesh) { | 881 | if (kind == leonida::IndirectKind::dispatch_mesh) { |
| 852 | // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the | 882 | // Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the |
| 853 | // first such call says so. The list stays valid. | 883 | // first such call says so. The list stays valid. |
| @@ -1276,6 +1306,12 @@ private: | |||
| 1276 | leonida::IndexBufferView index_{}; | 1306 | leonida::IndexBufferView index_{}; |
| 1277 | leonida::Viewport viewport_{}; | 1307 | leonida::Viewport viewport_{}; |
| 1278 | leonida::Rect scissor_{}; | 1308 | leonida::Rect scissor_{}; |
| 1309 | std::array<leonida::Viewport, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> viewports_{}; | ||
| 1310 | UINT viewport_count_ = 0; | ||
| 1311 | std::array<leonida::Rect, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> scissors_{}; | ||
| 1312 | UINT scissor_count_ = 0; | ||
| 1313 | std::shared_ptr<const leonida::DrawViewports> draw_viewports_; | ||
| 1314 | bool draw_viewports_stale_ = true; | ||
| 1279 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; | 1315 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; |
| 1280 | UINT render_target_count_ = 0; | 1316 | UINT render_target_count_ = 0; |
| 1281 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a | 1317 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a |
| @@ -1378,6 +1414,12 @@ private: | |||
| 1378 | index_ = {}; | 1414 | index_ = {}; |
| 1379 | viewport_ = {}; | 1415 | viewport_ = {}; |
| 1380 | scissor_ = {}; | 1416 | scissor_ = {}; |
| 1417 | viewports_.fill({}); | ||
| 1418 | viewport_count_ = 0; | ||
| 1419 | scissors_.fill({}); | ||
| 1420 | scissor_count_ = 0; | ||
| 1421 | draw_viewports_.reset(); | ||
| 1422 | draw_viewports_stale_ = true; | ||
| 1381 | render_targets_.fill({}); | 1423 | render_targets_.fill({}); |
| 1382 | render_target_count_ = 0; | 1424 | render_target_count_ = 0; |
| 1383 | draw_views_stale_ = true; | 1425 | draw_views_stale_ = true; |
| @@ -1418,16 +1460,37 @@ private: | |||
| 1418 | return draw_views_; | 1460 | return draw_views_; |
| 1419 | } | 1461 | } |
| 1420 | 1462 | ||
| 1463 | bool any_nonempty_viewport() const { | ||
| 1464 | for (UINT i = 0; i < viewport_count_; ++i) | ||
| 1465 | if (viewports_[i].width > 0.0f && viewports_[i].height > 0.0f) return true; | ||
| 1466 | return false; | ||
| 1467 | } | ||
| 1468 | |||
| 1469 | std::shared_ptr<const leonida::DrawViewports> draw_viewports() { | ||
| 1470 | if (viewport_count_ <= 1 && scissor_count_ <= 1) return nullptr; | ||
| 1471 | if (draw_viewports_stale_) { | ||
| 1472 | auto block = std::make_shared<leonida::DrawViewports>(); | ||
| 1473 | block->viewports = viewports_; | ||
| 1474 | block->viewport_count = viewport_count_; | ||
| 1475 | block->scissors = scissors_; | ||
| 1476 | block->scissor_count = scissor_count_; | ||
| 1477 | draw_viewports_ = std::move(block); | ||
| 1478 | draw_viewports_stale_ = false; | ||
| 1479 | } | ||
| 1480 | return draw_viewports_; | ||
| 1481 | } | ||
| 1482 | |||
| 1421 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1483 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1422 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1484 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1423 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1485 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1424 | note_direct_heap_indexing(graphics_.layout); | 1486 | note_direct_heap_indexing(graphics_.layout); |
| 1425 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1487 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes |
| 1426 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1488 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1427 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1489 | if (!any_nonempty_viewport()) return; |
| 1428 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, | 1490 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, |
| 1429 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, | 1491 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, |
| 1430 | first, base_vertex, first_instance}; | 1492 | first, base_vertex, first_instance}; |
| 1493 | draw.viewports = draw_viewports(); | ||
| 1431 | check(list_.draw(std::move(draw))); | 1494 | check(list_.draw(std::move(draw))); |
| 1432 | } | 1495 | } |
| 1433 | }; | 1496 | }; |