d3d12/vulkan: implement multi-viewport and multi-scissor rendering (D3D-MULTI-VIEWPORT) #510

Manually merged
github merged 1 commit from gh-pr/510/head into gh-pr/510/base 2026-11-26 09:04:57 +00:00
Owner

@⁠jujumoneylol on GitHub · 2026-11-24 06:38 UTC · original
jujumoneylol:pr/d3d12-multi-viewport → main · +652 −40 · 9 files · 1 commits · merged 2026-11-24 20:31 UTC

Claimed in the coordination issue as: D3D-MULTI-VIEWPORT (#14)

Depends on: #469, #499 (adjacent lines in vulkan_replay.h draw replay, d3d12_vulkan_device.cpp, d3d12_com.cpp, and vk_test_device.h if multiple merge; this branch is based cleanly on current main 8f481cf16 with no code dependency on either)

What and why

Previously, RSSetViewports and RSSetScissorRects silently dropped every viewport and scissor past index 0 (stub("more than one viewport; the first is used")), and graphics pipelines fixed viewportCount = 1 / scissorCount = 1 even though main already enables Vulkan 1.2 shaderOutputViewportIndex and advertises VPAndRTArrayIndexFromAnyShaderFeedingRasterizerSupportedWithoutGSEmulation = TRUE.

  • Command list (runtime/src/command_list.h, runtime/shims/d3d12_com.cpp):
    • Stores all 1..16 D3D12 viewports and 1..16 scissor rects in GraphicsCommandList (validating count <= 16 on both) and caches a shared DrawViewports block (zero allocation per draw, and nullptr / zero overhead for the common 1-viewport case).
    • Updates record_draw and DispatchMesh to skip rasterization only when all bound viewports are empty, so draws with an empty viewport 0 and active viewports 1..15 still run.
  • Vulkan 1.3 dynamic state (runtime/src/vulkan_backend.h, runtime/shims/d3d12_vulkan_device.cpp, runtime/src/vulkan_replay.h):
    • Enables VkPhysicalDeviceFeatures::multiViewport in create_vulkan_device_standalone and vktest::Device (plus shaderOutputViewportIndex in vktest::Device).
    • Loads vkCmdSetViewportWithCount and vkCmdSetScissorWithCount (Vulkan 1.3 core) and switches graphics pipelines to VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT + VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT (with viewportCount = 0, scissorCount = 0) whenever features.multiViewport and the entry points are available (falling back cleanly to single-viewport vkCmdSetViewport / vkCmdSetScissor otherwise).
    • Handles mismatched viewport/scissor counts and empty viewport slots per both D3D12 and Vulkan specs: passes matching count = max(vp_count, sc_count) to both calls, pairs empty/unbound viewport slots with a 1×1 dummy viewport and an empty {0, 0, 0, 0} scissor so nothing rasterizes and Vulkan's width > 0 VUID is satisfied, and clips unbound scissor slots with {0, 0, 0, 0}.
  • Live GPU host-parity test (runtime/test/d3d12_multi_viewport_test.cpp):
    • Self-contained (in-memory SPIR-V 1.5 VS with OpCapability MultiViewport + OpCapability ShaderViewportIndex writing BuiltIn ViewportIndex, HLSL via d3dcompiler_47 for host reference).
    • Verifies 4 simultaneous quadrant viewports in a single draw call, 4 independent half-clipping scissors, mismatched counts (4 viewports / 2 scissors), and empty viewport 0 + active viewports 1..3 — comparing all 64/64 pixels byte-for-byte against host Windows D3D12.

Tested

  • Built d3d12_multi_viewport_test, d3d12_state_map_test, and replay_cpu_bench with MSVC 19.44 (vcvars64) + Ninja on top of main 8f481cf16.
  • d3d12_multi_viewport_test: ALL 4 CASES PASSED (EXACT MATCH vs host D3D12) — 64/64 pixels exact on every case on NVIDIA GeForce GTX 1650 with 0 validation errors.
  • d3d12_state_map_test: PASSED.
  • py -3.13 -I scripts/check_no_game_data.py on all 9 touched files: clean.

GPUs tested: NVIDIA GeForce GTX 1650 Mobile 4 GB (driver 592.00, Vulkan 1.4.325, Windows 11 26200)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — D3D12 → Vulkan runtime only)
**@⁠jujumoneylol** on GitHub · 2026-11-24 06:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/510) `jujumoneylol:pr/d3d12-multi-viewport` → `main` · +652 −40 · 9 files · 1 commits · merged 2026-11-24 20:31 UTC Claimed in the coordination issue as: D3D-MULTI-VIEWPORT (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) Depends on: #469, #499 (adjacent lines in `vulkan_replay.h` draw replay, `d3d12_vulkan_device.cpp`, `d3d12_com.cpp`, and `vk_test_device.h` if multiple merge; this branch is based cleanly on current `main` `8f481cf16` with no code dependency on either) ### What and why Previously, `RSSetViewports` and `RSSetScissorRects` silently dropped every viewport and scissor past index 0 (`stub("more than one viewport; the first is used")`), and graphics pipelines fixed `viewportCount = 1` / `scissorCount = 1` even though `main` already enables Vulkan 1.2 `shaderOutputViewportIndex` and advertises `VPAndRTArrayIndexFromAnyShaderFeedingRasterizerSupportedWithoutGSEmulation = TRUE`. - **Command list (`runtime/src/command_list.h`, `runtime/shims/d3d12_com.cpp`)**: - Stores all 1..16 D3D12 viewports and 1..16 scissor rects in `GraphicsCommandList` (validating `count <= 16` on both) and caches a shared `DrawViewports` block (zero allocation per draw, and `nullptr` / zero overhead for the common 1-viewport case). - Updates `record_draw` and `DispatchMesh` to skip rasterization only when *all* bound viewports are empty, so draws with an empty viewport 0 and active viewports 1..15 still run. - **Vulkan 1.3 dynamic state (`runtime/src/vulkan_backend.h`, `runtime/shims/d3d12_vulkan_device.cpp`, `runtime/src/vulkan_replay.h`)**: - Enables `VkPhysicalDeviceFeatures::multiViewport` in `create_vulkan_device_standalone` and `vktest::Device` (plus `shaderOutputViewportIndex` in `vktest::Device`). - Loads `vkCmdSetViewportWithCount` and `vkCmdSetScissorWithCount` (Vulkan 1.3 core) and switches graphics pipelines to `VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT` + `VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT` (with `viewportCount = 0`, `scissorCount = 0`) whenever `features.multiViewport` and the entry points are available (falling back cleanly to single-viewport `vkCmdSetViewport` / `vkCmdSetScissor` otherwise). - Handles mismatched viewport/scissor counts and empty viewport slots per both D3D12 and Vulkan specs: passes matching `count = max(vp_count, sc_count)` to both calls, pairs empty/unbound viewport slots with a 1×1 dummy viewport and an empty `{0, 0, 0, 0}` scissor so nothing rasterizes and Vulkan's `width > 0` VUID is satisfied, and clips unbound scissor slots with `{0, 0, 0, 0}`. - **Live GPU host-parity test (`runtime/test/d3d12_multi_viewport_test.cpp`)**: - Self-contained (in-memory SPIR-V 1.5 VS with `OpCapability MultiViewport` + `OpCapability ShaderViewportIndex` writing `BuiltIn ViewportIndex`, HLSL via `d3dcompiler_47` for host reference). - Verifies 4 simultaneous quadrant viewports in a single draw call, 4 independent half-clipping scissors, mismatched counts (4 viewports / 2 scissors), and empty viewport 0 + active viewports 1..3 — comparing all 64/64 pixels byte-for-byte against host Windows D3D12. ### Tested - Built `d3d12_multi_viewport_test`, `d3d12_state_map_test`, and `replay_cpu_bench` with MSVC 19.44 (`vcvars64`) + Ninja on top of `main` `8f481cf16`. - `d3d12_multi_viewport_test`: **ALL 4 CASES PASSED (EXACT MATCH vs host D3D12)** — 64/64 pixels exact on every case on NVIDIA GeForce GTX 1650 with 0 validation errors. - `d3d12_state_map_test`: PASSED. - `py -3.13 -I scripts/check_no_game_data.py` on all 9 touched files: clean. GPUs tested: NVIDIA GeForce GTX 1650 Mobile 4 GB (driver 592.00, Vulkan 1.4.325, Windows 11 26200) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — D3D12 → Vulkan runtime only)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:04:56 +00:00
github manually merged commit 7d50f3b749 into gh-pr/510/base 2026-11-26 09:04:57 +00:00
Author
Owner

@⁠sodove on GitHub · 2026-11-24 07:46 UTC · original

Independent Windows verification of the exact #510 diff (7d50f3b) applied to main 1c93c86. NVIDIA RTX 5070 Ti, driver 610.74, Windows 11 26100.7623, MSVC 19.44.35220 Release. Native D3D12 used the same adapter. Khronos Vulkan validation 1.4.369 was enabled and reported zero errors.

  • Stock test: four cases, 256/256 pixels matched native D3D12.
  • Five draws in one direct list with reordered viewport arrays, multi-to-single transitions, zero viewports, zero scissors, and a scissor-only rebind: 64/64 pixels matched both native D3D12 and an independently computed expected image.
  • Draw through viewport/scissor slot 15 with all earlier slots empty, then shrink to one viewport and draw again in the same list: 64/64 pixels matched both references.

Synthetic fixtures derived from the PR test. AI-assisted.

**@⁠sodove** on GitHub · 2026-11-24 07:46 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/510#issuecomment-7125350366) Independent Windows verification of the exact #510 diff (7d50f3b) applied to main 1c93c86. NVIDIA RTX 5070 Ti, driver 610.74, Windows 11 26100.7623, MSVC 19.44.35220 Release. Native D3D12 used the same adapter. Khronos Vulkan validation 1.4.369 was enabled and reported zero errors. - Stock test: four cases, 256/256 pixels matched native D3D12. - Five draws in one direct list with reordered viewport arrays, multi-to-single transitions, zero viewports, zero scissors, and a scissor-only rebind: 64/64 pixels matched both native D3D12 and an independently computed expected image. - Draw through viewport/scissor slot 15 with all earlier slots empty, then shrink to one viewport and draw again in the same list: 64/64 pixels matched both references. Synthetic fixtures derived from the PR test. AI-assisted.
Author
Owner

@⁠Sangonapou on GitHub · 2026-11-24 14:54 UTC · original

Tested 7d50f3b7 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files. Tested as it stands (it lists #469 and #499 as adjacent, not merged in).

ctest -R ^d3d12_multi_viewport$ -V, host reference the same RTX 3080 Ti through the real D3D12 runtime:

4 quadrant viewports + 4 full scissors               [64/64 px EXACT vs host] OK
4 viewports + 4 independent half-clipping scissors   [64/64 px EXACT vs host] OK
4 viewports + 2 scissors (slots 2,3 clipped)         [64/64 px EXACT vs host] OK
empty vp0 + active vp1..3                            [64/64 px EXACT vs host] OK
d3d12_multi_viewport_test: ALL 4 CASES PASSED (EXACT MATCH vs host D3D12)
**@⁠Sangonapou** on GitHub · 2026-11-24 14:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/510#issuecomment-7135133298) Tested 7d50f3b7 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass Runtime built at this head with `scripts\build_runtime.ps1` (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files. Tested as it stands (it lists #469 and #499 as adjacent, not merged in). `ctest -R ^d3d12_multi_viewport$ -V`, host reference the same RTX 3080 Ti through the real D3D12 runtime: ``` 4 quadrant viewports + 4 full scissors [64/64 px EXACT vs host] OK 4 viewports + 4 independent half-clipping scissors [64/64 px EXACT vs host] OK 4 viewports + 2 scissors (slots 2,3 clipped) [64/64 px EXACT vs host] OK empty vp0 + active vp1..3 [64/64 px EXACT vs host] OK d3d12_multi_viewport_test: ALL 4 CASES PASSED (EXACT MATCH vs host D3D12) ```
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-24 20:31 UTC · original

Merged. This was compared with #827 for D3D-MULTI-VIEWPORT, and it is the better fix:

  • With an empty viewport 0 and other viewports in use, this sends a 1x1 dummy viewport with an empty scissor. A zero-width VkViewport there would break VUID-VkViewport-width-01770.
  • The pixel test against native D3D12 covers 4 quadrant viewports, separate scissors, mismatched counts and an empty viewport 0, and two other contributors confirmed it on Windows.
  • The claim is valid and first, and it merges cleanly onto current main.

Worth a follow-up, from #827: the docs/d3d12_vulkan.md line for this feature, and a notice when multiViewport is missing.

AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-24 20:31 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/510#issuecomment-7143675665) Merged. This was compared with #827 for D3D-MULTI-VIEWPORT, and it is the better fix: - With an empty viewport 0 and other viewports in use, this sends a 1x1 dummy viewport with an empty scissor. A zero-width `VkViewport` there would break `VUID-VkViewport-width-01770`. - The pixel test against native D3D12 covers 4 quadrant viewports, separate scissors, mismatched counts and an empty viewport 0, and two other contributors confirmed it on Windows. - The claim is valid and first, and it merges cleanly onto current `main`. Worth a follow-up, from #827: the `docs/d3d12_vulkan.md` line for this feature, and a notice when `multiViewport` is missing. AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 20:43 UTC · original

Post-merge finding: d3d12_close_parity has failed on main since this PR (113 cases, 1 failure):

FAIL RSSetScissorRects(17): layer Close 0x80070057, WARP succeeds

The cause is the new line in RSSetScissorRects (runtime/shims/d3d12_vulkan_device.cpp): if (count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false);. That puts the list into the error state, so Close returns E_INVALIDARG. The parity row at runtime/test/d3d12_close_parity_test.cpp:362 expects the layer to match WARP, and WARP's Close succeeds after RSSetScissorRects(17, …). Before this PR the call only stubbed and used the first rectangle, so the row passed.

Measured on Windows 11 / RTX 4070 SUPER (VS 2022, Ninja, RelWithDebInfo): it passes at 58be9cdf8, and fails at plain 32a5d9698 both plain and with LEONIDA_VK_VALIDATION=1. The only other runtime change in that range is #878 (stencil face masks), which doesn't touch scissors. The rest of the suite is unchanged.

There are two ways to fix it, depending on which behaviour the project wants:

  • Keep WARP parity: for count > 16, ignore the call or keep the first 16 instead of calling check(false). That matches the existing row and native D3D12's Close.
  • Make refusal intended: update that row of the parity test to expect a failure, with a note why.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-24 20:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/510#issuecomment-7143969965) Post-merge finding: `d3d12_close_parity` has failed on main since this PR (113 cases, 1 failure): ``` FAIL RSSetScissorRects(17): layer Close 0x80070057, WARP succeeds ``` The cause is the new line in `RSSetScissorRects` (`runtime/shims/d3d12_vulkan_device.cpp`): `if (count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false);`. That puts the list into the error state, so `Close` returns `E_INVALIDARG`. The parity row at `runtime/test/d3d12_close_parity_test.cpp:362` expects the layer to match WARP, and WARP's `Close` succeeds after `RSSetScissorRects(17, …)`. Before this PR the call only stubbed and used the first rectangle, so the row passed. Measured on Windows 11 / RTX 4070 SUPER (VS 2022, Ninja, RelWithDebInfo): it passes at 58be9cdf8, and fails at plain 32a5d9698 both plain and with `LEONIDA_VK_VALIDATION=1`. The only other runtime change in that range is #878 (stencil face masks), which doesn't touch scissors. The rest of the suite is unchanged. There are two ways to fix it, depending on which behaviour the project wants: - **Keep WARP parity:** for `count > 16`, ignore the call or keep the first 16 instead of calling `check(false)`. That matches the existing row and native D3D12's Close. - **Make refusal intended:** update that row of the parity test to expect a failure, with a note why. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-24 21:17 UTC · original

@⁠QemmHD thanks, confirmed, and the fix is up as #1286.

  • Reproduced with a clang-cl + xwin build under Wine, on llvmpipe and on an RDNA2 iGPU: d3d12_close_parity has 0 failures at e0f171958 (the commit before this merge) and fails at ccd9c5f36 with the same RSSetScissorRects(17) line.
  • Where: the check is in RSSetScissorRects in runtime/shims/d3d12_com.cpp, not in d3d12_vulkan_device.cpp.
  • The fix keeps WARP parity, your first option: for more than 16 rectangles it uses the first 16, with a stub notice, where the merged code called check(false). Marking the row stricter would make the layer refuse lists that D3D12 runs.
  • Tested: with #1286, d3d12_close_parity and d3d12_multi_viewport pass on both devices, and no other test in the suite changes state.

I merged #510 without running the whole Windows suite on it; that's on me, and I run it before every runtime merge now. I can't merge my own PR, so could you take a look at #1286? If it can't land soon, I'll revert ccd9c5f36 so main is green in the meantime.

AI-assisted (Claude Code, Claude Fable 5.2).

**@⁠aslan2142** on GitHub · 2026-11-24 21:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/510#issuecomment-7144726205) @⁠QemmHD thanks, confirmed, and the fix is up as #1286. - **Reproduced** with a clang-cl + xwin build under Wine, on llvmpipe and on an RDNA2 iGPU: `d3d12_close_parity` has 0 failures at `e0f171958` (the commit before this merge) and fails at `ccd9c5f36` with the same `RSSetScissorRects(17)` line. - **Where:** the check is in `RSSetScissorRects` in `runtime/shims/d3d12_com.cpp`, not in `d3d12_vulkan_device.cpp`. - **The fix keeps WARP parity,** your first option: for more than 16 rectangles it uses the first 16, with a `stub` notice, where the merged code called `check(false)`. Marking the row `stricter` would make the layer refuse lists that D3D12 runs. - **Tested:** with #1286, `d3d12_close_parity` and `d3d12_multi_viewport` pass on both devices, and no other test in the suite changes state. I merged #510 without running the whole Windows suite on it; that's on me, and I run it before every runtime merge now. I can't merge my own PR, so could you take a look at #1286? If it can't land soon, I'll revert `ccd9c5f36` so `main` is green in the meantime. AI-assisted (Claude Code, Claude Fable 5.2).
This discussion has been locked. Commenting is limited to contributors.
No description provided.