diff options
| author | Juju <blupixleyt@gmail.com> | 2026-11-24 01:38:53 -0500 |
|---|---|---|
| committer | Juju <blupixleyt@gmail.com> | 2026-11-24 01:38:53 -0500 |
| commit | 7d50f3b749ab8c2902bef94ef8cc76885c22712a (patch) | |
| tree | 4e9dad52e76373c76b9458f55b46aef20752f22d /runtime/shims/d3d12_com.cpp | |
| parent | 8f481cf16f261c7f37458aeb45e8f4e19994146d (diff) | |
d3d12/vulkan: implement multi-viewport and multi-scissor rendering (1..16 viewports/scissors)
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 69 |
1 files changed, 60 insertions, 9 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4f4904e96..9928b74bc 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,20 @@ 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 | if (count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false); |
| 432 | scissor_ = {rects[0].left, rects[0].top, rects[0].right, rects[0].bottom}; | 443 | scissors_.fill({}); |
| 444 | for (UINT i = 0; i < count; ++i) { | ||
| 445 | scissors_[i] = {rects[i].left, rects[i].top, rects[i].right, rects[i].bottom}; | ||
| 446 | } | ||
| 447 | scissor_count_ = count; | ||
| 448 | scissor_ = scissors_[0]; | ||
| 449 | draw_viewports_stale_ = true; | ||
| 433 | } | 450 | } |
| 434 | 451 | ||
| 435 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. | 452 | // D3D12 reads render-target and depth descriptors here, so their views are copied now. |
| @@ -486,10 +503,11 @@ public: | |||
| 486 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); | 503 | d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); |
| 487 | if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); | 504 | 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); | 505 | if (!graphics_pipeline_ || !graphics_.layout) return check(false); |
| 489 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized | 506 | if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized |
| 490 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, | 507 | 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}; | 508 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; |
| 492 | draw.mesh = true; | 509 | draw.mesh = true; |
| 510 | draw.viewports = draw_viewports(); | ||
| 493 | check(list_.draw(std::move(draw))); | 511 | check(list_.draw(std::move(draw))); |
| 494 | } | 512 | } |
| 495 | 513 | ||
| @@ -1266,6 +1284,12 @@ private: | |||
| 1266 | leonida::IndexBufferView index_{}; | 1284 | leonida::IndexBufferView index_{}; |
| 1267 | leonida::Viewport viewport_{}; | 1285 | leonida::Viewport viewport_{}; |
| 1268 | leonida::Rect scissor_{}; | 1286 | leonida::Rect scissor_{}; |
| 1287 | std::array<leonida::Viewport, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> viewports_{}; | ||
| 1288 | UINT viewport_count_ = 0; | ||
| 1289 | std::array<leonida::Rect, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> scissors_{}; | ||
| 1290 | UINT scissor_count_ = 0; | ||
| 1291 | std::shared_ptr<const leonida::DrawViewports> draw_viewports_; | ||
| 1292 | bool draw_viewports_stale_ = true; | ||
| 1269 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; | 1293 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; |
| 1270 | UINT render_target_count_ = 0; | 1294 | UINT render_target_count_ = 0; |
| 1271 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a | 1295 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a |
| @@ -1368,6 +1392,12 @@ private: | |||
| 1368 | index_ = {}; | 1392 | index_ = {}; |
| 1369 | viewport_ = {}; | 1393 | viewport_ = {}; |
| 1370 | scissor_ = {}; | 1394 | scissor_ = {}; |
| 1395 | viewports_.fill({}); | ||
| 1396 | viewport_count_ = 0; | ||
| 1397 | scissors_.fill({}); | ||
| 1398 | scissor_count_ = 0; | ||
| 1399 | draw_viewports_.reset(); | ||
| 1400 | draw_viewports_stale_ = true; | ||
| 1371 | render_targets_.fill({}); | 1401 | render_targets_.fill({}); |
| 1372 | render_target_count_ = 0; | 1402 | render_target_count_ = 0; |
| 1373 | draw_views_stale_ = true; | 1403 | draw_views_stale_ = true; |
| @@ -1408,16 +1438,37 @@ private: | |||
| 1408 | return draw_views_; | 1438 | return draw_views_; |
| 1409 | } | 1439 | } |
| 1410 | 1440 | ||
| 1441 | bool any_nonempty_viewport() const { | ||
| 1442 | for (UINT i = 0; i < viewport_count_; ++i) | ||
| 1443 | if (viewports_[i].width > 0.0f && viewports_[i].height > 0.0f) return true; | ||
| 1444 | return false; | ||
| 1445 | } | ||
| 1446 | |||
| 1447 | std::shared_ptr<const leonida::DrawViewports> draw_viewports() { | ||
| 1448 | if (viewport_count_ <= 1 && scissor_count_ <= 1) return nullptr; | ||
| 1449 | if (draw_viewports_stale_) { | ||
| 1450 | auto block = std::make_shared<leonida::DrawViewports>(); | ||
| 1451 | block->viewports = viewports_; | ||
| 1452 | block->viewport_count = viewport_count_; | ||
| 1453 | block->scissors = scissors_; | ||
| 1454 | block->scissor_count = scissor_count_; | ||
| 1455 | draw_viewports_ = std::move(block); | ||
| 1456 | draw_viewports_stale_ = false; | ||
| 1457 | } | ||
| 1458 | return draw_viewports_; | ||
| 1459 | } | ||
| 1460 | |||
| 1411 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1461 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1412 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1462 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1413 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1463 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1414 | note_direct_heap_indexing(graphics_.layout); | 1464 | note_direct_heap_indexing(graphics_.layout); |
| 1415 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1465 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes |
| 1416 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1466 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1417 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1467 | if (!any_nonempty_viewport()) return; |
| 1418 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, | 1468 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, |
| 1419 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, | 1469 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, |
| 1420 | first, base_vertex, first_instance}; | 1470 | first, base_vertex, first_instance}; |
| 1471 | draw.viewports = draw_viewports(); | ||
| 1421 | check(list_.draw(std::move(draw))); | 1472 | check(list_.draw(std::move(draw))); |
| 1422 | } | 1473 | } |
| 1423 | }; | 1474 | }; |