Skip to content

vulkan_replay: no zero-size viewport on the one-viewport path, viewport depths in [0, 1] (RT-REPLAY-VIEWPORT-ZERO-AND-DEPTH-CLAMP) - #2485

Open
Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-replay-viewport-zero-and-depth-clamp
Open

Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-replay-viewport-zero-and-depth-clamp

Conversation

@Tyrannys

@Tyrannys Tyrannys commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-REPLAY-VIEWPORT-ZERO-AND-DEPTH-CLAMP (#1608 (comment)). This is item 2 of @kevincommits' finding in #1608 (#1608 (comment)). Thank you for the exact repro, which the new test follows.

Depends on: none. #499 (stale, needs rebase) moves this viewport code into a helper. Whichever lands second needs a small textual rebase. The two changes share no logic.

What and why

In emit() (runtime/src/vulkan_replay.h), the draw's viewport state:

  1. One-viewport path (!graphics.viewport_with_count, used when the device lacks multiViewport or vkCmdSetViewportWithCount). It passed draw->viewport straight to vkCmdSetViewport. The draw is kept when any viewport slot is non-empty (any_nonempty_viewport()), so viewport 0 can be empty. That gave a 0 x -0 viewport (VUID-VkViewport-width-01770 / -height-01773) with the full scissor still on. This path now does what the counted path already does for an empty D3D12 viewport: a 1x1 dummy viewport paired with an empty scissor, so nothing rasterizes, as D3D12 behaves.
  2. Both paths: depth range. minDepth/maxDepth were forwarded unclamped. Without VK_EXT_depth_range_unrestricted Vulkan requires them in [0, 1] (VUID-VkViewport-minDepth-01234, VUID-VkViewport-maxDepth-01235), which is also D3D12's range. They are now clamped, with NaN going to 0 and 1, the same way ClearDepthStencil and set_depth_bounds already handle depths. With the extension enabled, the values pass through unchanged.

The viewport build is now one lambda shared by both paths, plus one shared dummy viewport.

Tested

New CPU test replay_viewport (runtime/test/replay_viewport_test.cpp, no GPU). It records the vkCmdSetViewport* / vkCmdSetScissor* calls from emit() and checks every recorded viewport against the three VUIDs above. Cases:

  • One viewport, empty, depths [-0.5, 2] (kevincommits' repro). Expected: a 1x1 viewport and an empty scissor. A zero-height viewport alone behaves the same.
  • One viewport and counted, depths [-0.5, 2] and NaN. Expected: clamped to [0, 1]. Depths inside [0, 1] are kept.
  • Both paths with depth_range_unrestricted. Expected: depths kept as given.
  • Two viewports with slot 0 empty (the d3d12_multi_viewport_test case). Counted path: slot 0 is the dummy, and slot 1 draws with its depths held. One-viewport path: valid.

On main (c0201a806, header reverted) the test fails with 9 FAIL lines and 8 invalid viewports, the first being w 0 h -0 depth [-0.5, 2]. With the change it passes.

The one-viewport path cannot be reached on a real device here, because the RTX 4070 SUPER has multiViewport. The counted path was also run on the GPU under LEONIDA_VK_VALIDATION=1: d3d12_multi_viewport (4/4 exact match vs host D3D12), the depth tests (d3d12_depth*, d3d12_clear_restricted_depth, d3d12_typeless_depth, xbox_depth_storage) and the replay_* / d3d12_state_map tests. All pass with validation on.

Full suite (ctest -C Release -j 3): 503/505. The two failures, xbox_present and d3d12_set_descriptor_heaps, also fail on main unchanged.

No game files.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11

AI-assisted: yes (Claude Code)

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)

🤖 Generated with Claude Code

…rt depths in [0, 1]

The path for pipelines without VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT (no
multiViewport) passed an empty viewport 0 straight to vkCmdSetViewport
(VUID-VkViewport-width-01770) with its scissor still on, although the
draw is kept when a later viewport slot is non-empty. It now uses the
same 1x1 viewport and empty scissor as the counted path. Both paths
clamp minDepth/maxDepth to [0, 1] (NaN to the range's end) unless
VK_EXT_depth_range_unrestricted is on (VUID-VkViewport-minDepth-01234,
VUID-VkViewport-maxDepth-01235), as clears and depth bounds already do.

Reported by kevincommits in ubiquitarianism#1608 (RT-REPLAY-VIEWPORT-ZERO-AND-DEPTH-CLAMP).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@lixonik lixonik 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.

Reviewed 1377575be (runtime/src/vulkan_replay.h, runtime/test/replay_viewport_test.cpp) and tested:

  • One-viewport path (!graphics.viewport_with_count) now maps empty/NaN D3D12 viewports to the 1x1 dummy viewport paired with an empty scissor, matching the viewport_with_count path and avoiding VUID-VkViewport-width-01770 / -height-01773.
  • Both paths clamp minDepth/maxDepth to [0, 1] (with NaN -> 0.0f/1.0f, ±Inf clamped cleanly) unless vk.depth_range_unrestricted is set.
  • Tested on Windows 11 (MSVC 19.44): replay_viewport passed (0.03 s).

Looks good to merge.

AI-assisted (Antigravity, Gemini).

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.

2 participants