Skip to content

Runtime: preserve and replay sixteen D3D12 viewport and scissor slots - #4550

Open
pmilewsskiedu-gif wants to merge 2 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-multi-viewport-20261122
Open

pmilewsskiedu-gif wants to merge 2 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/d3d12-multi-viewport-20261122

Conversation

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

Claimed before production implementation on current coordinator #4421: #4421 (comment)

Production defect and fix

Unmodified origin/main 03b09a086592a197db64d91655c6e65c30764d96 records only viewport/scissor slot 0 in RSSetViewports/RSSetScissorRects and in Draw IR, and creates Vulkan graphics pipelines with viewportCount=scissorCount=1. A shader writing D3D12 SV_ViewportArrayIndex=1 (translated GLSL gl_ViewportIndex=1) cannot rasterize through a separately bound viewport/scissor even though its shader translator already supports that output.

Fixes the entire graphics path: COM copies every bound slot (up to D3D12's 16), resets viewport arrays on zero-viewports, snapshots the state in recorded Draw commands and inherited parent-to-bundle execution, and dynamically emits the selected Vulkan viewport/scissor slots. The graphics device enables multiViewport only where physically supported; GraphicsPipeline builds its normal 1-slot pipeline by default, lazily compiling a 16-slot variant only for multi-slot draws, avoiding a 16× dynamic state overhead on ordinary scenes. Additional slot snapshots are allocated only when needed. Vulkan-invalid missing/zero-size slots receive a legal placeholder viewport and empty scissor. The prepare path rejects a multi-viewport draw when the logical device lacks multiViewport rather than issuing invalid Vulkan commands; attachment-free rendering area spans the bound viewports. Unrelated zero-count scissor semantics are intentionally reserved to already published PR #4411 (no duplicate implementation here).

Same-source original-main RED / patched GREEN, real GPU and WARP pixel oracle

Original synthetic VS compiled in HLSL to WARP (SV_ViewportArrayIndex=1) and in GLSL to SPIR-V for the translator-style Leonida layer (gl_ViewportIndex=1); no game shaders or other game assets. Five scenarios: (1) two viewports, shader selects slot1; (2) independent scissor1 clips half; (3) full 16 viewport/scissor slot boundary; (4) parent viewports inherited by an ExecuteBundle draw; (5) host input arrays deliberately modified after binding to verify snapshot. Native WARP calls Close()==S_OK and draws expected red pixels through viewport1 in all cases.

  • Unmodified production main, detached test-only negative control LeonidaMultiViewportBaseline20261122: all five WARP-vs-Leonida pixel scenarios fail (WARP slot1 red but Leonida all-blue), Close S_OK and zero Vulkan validation errors, executable exit 1. CMake and new test-only files are copied identically; runtime/src and runtime/shims are unchanged.
  • Patched commit e88f66a19f50800311647d95dd3e9d46ac5250bb on RTX 4060 Ti with LEONIDA_VK_VALIDATION=1: all five scenarios match WARP pixel-for-pixel, Close S_OK, 0 Vulkan validation errors, exit 0. Identical test source SHA256 2698C9EDADC14CCBD24BF9DBE41D2DC04D0D34CB9E88A0C2890FB60174B7344A on baseline and patched worktrees.
  • MSVC 19.44/Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0: focused CTests vk_command_replay, d3d12_com, d3d12_draw, d3d12_bundle, d3d12_multi_viewport 5/5 PASS (6.00 seconds). d3d12_bundle and d3d12_multi_viewport each 10/10 consecutive repeats PASS (12.65 seconds total). replay_cpu_bench --self-test passes 14 kinds and 296-command frame. New GLSL's SPIR-V passes Vulkan 1.3 spirv-val; git diff --check clean.

Scope, limitations, disclosure

  • Original production and synthetic test code only, no game data, keys, decryption or protection bypass.
  • AI-assisted implementation, tests and review explicitly disclosed.

No authentic GTA VI execution is claimed (game_scarlett_master.exe unavailable). Cross-GPU Vulkan and full-suite coverage not yet run; tested RTX 4060 Ti Vulkan and native Windows WARP. This PR does not duplicate #4411's zero-count scissor correction; future integration may require a small rebase of adjacent COM lines. Base 03b09a08, head e88f66a19f50800311647d95dd3e9d46ac5250bb. Maintainer review required; I will not self-merge.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran d3d12_multi_viewport_test at e88f66a1 on Windows 11, RTX 5070 Ti: 5 scenarios, 0 failures, Vulkan validation errors=0.

The layer matches WARP byte-for-byte for two viewports, a scissor clipping half of viewport 1, all sixteen viewport/scissor slots, parent viewports inherited into a bundle, and arrays snapshotted before draw. No blocking issue.

@whattheflop whattheflop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested exact 548f810e on Windows 11, Ryzen 7 9800X3D Radeon iGPU, AMD 25.10.30.02. Stock multi-viewport test passed 5/5 with 320/320 pixels matching WARP, all ten command lists closed successfully, and zero Vulkan validation errors. The 16-slot case rendered through slot 1. Details in #166. AI-assisted.

@theswiss24

Copy link
Copy Markdown

Tested 548f810e merged onto main d0835dd9 (clean, d8c5daf1) on NVIDIA GeForce RTX 4070 (Ada), driver 621.24, Windows 11 (26300), MSVC from Visual Studio 2026 (Ninja, RelWithDebInfo), Vulkan SDK 1.4.363.1, with LEONIDA_VK_VALIDATION=1: pass.

ctest -R "vk_command_replay|d3d12_com$|d3d12_draw$|d3d12_bundle|d3d12_multi_v": 6/6 passed, 13.5 s: vk_command_replay, d3d12_com, d3d12_draw, d3d12_bundle, d3d12_multi_viewport (new), d3d12_bundle_forbidden. No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two state/draw semantics differ from D3D12 in the current viewport/scissor path.

// No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing,
// and Vulkan forbids a 0x0 viewport, so the draw is not recorded.
if (!(viewport_.width > 0) || !(viewport_.height > 0)) return;
if (!viewport_count_ ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep the draw when no viewport can rasterize

A zero viewport count disables viewport state; it does not cancel the draw command. Returning here drops input assembly and vertex-shader invocations along with rasterization, so pipeline-statistics queries (including VSInvocations) no longer match D3D12. Microsoft documents that undefined viewports are disabled and that VSInvocations count vertex-shader invocations. Please preserve the draw with a no-rasterization state (for example, a placeholder viewport plus an empty scissor), as the older #873 path did. RSSetViewports, pipeline statistics.


void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override {
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
if (!count || !rects) return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Clear stored scissor state for a zero-count call

RSSetScissorRects(0, nullptr) is a valid way to bind no scissor rectangles. Returning without clearing scissors_ and scissor_count_ makes later draws reuse the previous rectangles, although D3D12 says rectangles not defined by the call are disabled. Reset the stored array/count when count == 0; keep the null-pointer rejection for a positive count. Microsoft RSSetScissorRects contract.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 548f810 merged onto main d0835dd on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 156/156.
  • d3d12_multi_viewport: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

FAIL to merge, verified: git merge-tree reports content conflicts in runtime/shims/d3d12_com.cpp and runtime/src/command_list.h — main's f506039 (D3D-391) moved render targets and vertex buffers into the shared DrawViews block, the same Draw aggregate this PR rewrites. Not superseded: main still documents only-the-first-viewport at command_list.h:428. At the PR head the design reviewed well — snapshot-consistency reject in prepare_draw, clean refusal where multiViewport is absent, the legacy count-0 fallback preserved, and zero-sized slots get legal placeholders without any new unconditional read. Please rebase onto the DrawViews shape; the GPU/WARP parity claims were not verifiable on this host. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants