diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-24 21:31:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-24 21:31:21 +0100 |
| commit | ccd9c5f369bee45a810bcb0a4405f8c8a670c896 (patch) | |
| tree | 28a66dd04d5e69e5f95cec273dbf6a11e93c3abf /runtime/shims/d3d12_com.cpp | |
| parent | e0f171958f5a2e57741142545a98c3c0c645e530 (diff) | |
| parent | 7d50f3b749ab8c2902bef94ef8cc76885c22712a (diff) | |
Merge pull request #510 from jujumoneylol/pr/d3d12-multi-viewport
d3d12/vulkan: implement multi-viewport and multi-scissor rendering (D3D-MULTI-VIEWPORT)
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 bb9331b34..42c8e5cea 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 | ||
| @@ -1273,6 +1291,12 @@ private: | |||
| 1273 | leonida::IndexBufferView index_{}; | 1291 | leonida::IndexBufferView index_{}; |
| 1274 | leonida::Viewport viewport_{}; | 1292 | leonida::Viewport viewport_{}; |
| 1275 | leonida::Rect scissor_{}; | 1293 | leonida::Rect scissor_{}; |
| 1294 | std::array<leonida::Viewport, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> viewports_{}; | ||
| 1295 | UINT viewport_count_ = 0; | ||
| 1296 | std::array<leonida::Rect, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> scissors_{}; | ||
| 1297 | UINT scissor_count_ = 0; | ||
| 1298 | std::shared_ptr<const leonida::DrawViewports> draw_viewports_; | ||
| 1299 | bool draw_viewports_stale_ = true; | ||
| 1276 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; | 1300 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; |
| 1277 | UINT render_target_count_ = 0; | 1301 | UINT render_target_count_ = 0; |
| 1278 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a | 1302 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a |
| @@ -1375,6 +1399,12 @@ private: | |||
| 1375 | index_ = {}; | 1399 | index_ = {}; |
| 1376 | viewport_ = {}; | 1400 | viewport_ = {}; |
| 1377 | scissor_ = {}; | 1401 | scissor_ = {}; |
| 1402 | viewports_.fill({}); | ||
| 1403 | viewport_count_ = 0; | ||
| 1404 | scissors_.fill({}); | ||
| 1405 | scissor_count_ = 0; | ||
| 1406 | draw_viewports_.reset(); | ||
| 1407 | draw_viewports_stale_ = true; | ||
| 1378 | render_targets_.fill({}); | 1408 | render_targets_.fill({}); |
| 1379 | render_target_count_ = 0; | 1409 | render_target_count_ = 0; |
| 1380 | draw_views_stale_ = true; | 1410 | draw_views_stale_ = true; |
| @@ -1415,16 +1445,37 @@ private: | |||
| 1415 | return draw_views_; | 1445 | return draw_views_; |
| 1416 | } | 1446 | } |
| 1417 | 1447 | ||
| 1448 | bool any_nonempty_viewport() const { | ||
| 1449 | for (UINT i = 0; i < viewport_count_; ++i) | ||
| 1450 | if (viewports_[i].width > 0.0f && viewports_[i].height > 0.0f) return true; | ||
| 1451 | return false; | ||
| 1452 | } | ||
| 1453 | |||
| 1454 | std::shared_ptr<const leonida::DrawViewports> draw_viewports() { | ||
| 1455 | if (viewport_count_ <= 1 && scissor_count_ <= 1) return nullptr; | ||
| 1456 | if (draw_viewports_stale_) { | ||
| 1457 | auto block = std::make_shared<leonida::DrawViewports>(); | ||
| 1458 | block->viewports = viewports_; | ||
| 1459 | block->viewport_count = viewport_count_; | ||
| 1460 | block->scissors = scissors_; | ||
| 1461 | block->scissor_count = scissor_count_; | ||
| 1462 | draw_viewports_ = std::move(block); | ||
| 1463 | draw_viewports_stale_ = false; | ||
| 1464 | } | ||
| 1465 | return draw_viewports_; | ||
| 1466 | } | ||
| 1467 | |||
| 1418 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1468 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1419 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1469 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1420 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1470 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1421 | note_direct_heap_indexing(graphics_.layout); | 1471 | note_direct_heap_indexing(graphics_.layout); |
| 1422 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1472 | // No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes |
| 1423 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1473 | // nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1424 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1474 | if (!any_nonempty_viewport()) return; |
| 1425 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, | 1475 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, |
| 1426 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, | 1476 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, |
| 1427 | first, base_vertex, first_instance}; | 1477 | first, base_vertex, first_instance}; |
| 1478 | draw.viewports = draw_viewports(); | ||
| 1428 | check(list_.draw(std::move(draw))); | 1479 | check(list_.draw(std::move(draw))); |
| 1429 | } | 1480 | } |
| 1430 | }; | 1481 | }; |