GPU: clamp viewport and scissor counts and normalize inverted scissor rects #3176

Open
github-bot wants to merge 1 commit from github-bot/gh-3176 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:11 UTC · original
fuzeofficlal:fix/gpu-viewport-scissor-bounds → main · +120 −4 · 1 files · 1 commits · open

Problem

State::viewports and State::scissors in crates/victory-gpu/src/draw.rs forwarded arbitrary slices directly to ID3D12GraphicsCommandList::RSSetViewports / RSSetScissorRects and persisted them verbatim in self.fixed for segment-cut replay (State::cut):

  1. Passing more than D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE (16) viewports or scissor rects violates D3D12's pipeline limit and aborts the process under D3D12 validation (observed fatal exit code 2173 on both hardware D3D12 and WARP with VICTORY_D3D12_DEBUG=1).
  2. Passing an inside-out scissor rectangle (right < left or bottom < top) triggers a D3D12 Debug Layer error:
    ID3D12CommandList::RSSetScissorRects: In the scissor rect, bottom (...) is less than top (...) or right (...) is less than left (...). An inside-out rect can cause undefined behavior.

Fix

  • Clamp the viewport and scissor slices in State::viewports and State::scissors to D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE (16).
  • Normalize inside-out scissor rectangles by setting right = r.right.max(r.left) and bottom = r.bottom.max(r.top), which preserves empty-clipping semantics across the full i32 range (including negative coordinates) without overflow while satisfying D3D12's left <= right && top <= bottom invariant.
  • Store the clamped and normalized slices in self.fixed so State::cut replays valid bounds onto subsequent host command list segments as well.

Verification

Added draw::tests::viewports_and_scissors_clamp_count_and_inverted_rects covering empty slices, > 16 viewports and scissor rects, inside-out scissor rects (right < left, bottom < top, and negative coordinates), and State::cut segment replay:

  • Before fix (origin/main): aborts with exit code 2173 on both RTX 5090 + VICTORY_D3D12_DEBUG=1 and WARP + VICTORY_D3D12_DEBUG=1; inside-out scissor rects emit the D3D12 Debug Layer error above.
  • After fix:
    • Targeted test passes on hardware default, hardware --features diagnostics + VICTORY_D3D12_DEBUG=1, and WARP --features diagnostics + VICTORY_D3D12_DEBUG=1 with host.log_messages() == None.
    • Full cargo test -p victory-gpu --lib --locked passes on hardware default (230 passed), hardware diagnostics + DEBUG=1 (244 passed), and WARP diagnostics + DEBUG=1 (246 passed).
    • cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features) and xtask policy checks pass cleanly.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/3176) `fuzeofficlal:fix/gpu-viewport-scissor-bounds` → `main` · +120 −4 · 1 files · 1 commits · open ### Problem `State::viewports` and `State::scissors` in `crates/victory-gpu/src/draw.rs` forwarded arbitrary slices directly to `ID3D12GraphicsCommandList::RSSetViewports` / `RSSetScissorRects` and persisted them verbatim in `self.fixed` for segment-cut replay (`State::cut`): 1. Passing more than `D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE` (`16`) viewports or scissor rects violates D3D12's pipeline limit and aborts the process under D3D12 validation (observed fatal exit code `2173` on both hardware D3D12 and WARP with `VICTORY_D3D12_DEBUG=1`). 2. Passing an inside-out scissor rectangle (`right < left` or `bottom < top`) triggers a D3D12 Debug Layer error: `ID3D12CommandList::RSSetScissorRects: In the scissor rect, bottom (...) is less than top (...) or right (...) is less than left (...). An inside-out rect can cause undefined behavior.` ### Fix - Clamp the viewport and scissor slices in `State::viewports` and `State::scissors` to `D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE` (`16`). - Normalize inside-out scissor rectangles by setting `right = r.right.max(r.left)` and `bottom = r.bottom.max(r.top)`, which preserves empty-clipping semantics across the full `i32` range (including negative coordinates) without overflow while satisfying D3D12's `left <= right && top <= bottom` invariant. - Store the clamped and normalized slices in `self.fixed` so `State::cut` replays valid bounds onto subsequent host command list segments as well. ### Verification Added `draw::tests::viewports_and_scissors_clamp_count_and_inverted_rects` covering empty slices, `> 16` viewports and scissor rects, inside-out scissor rects (`right < left`, `bottom < top`, and negative coordinates), and `State::cut` segment replay: - **Before fix (`origin/main`)**: aborts with exit code `2173` on both RTX 5090 + `VICTORY_D3D12_DEBUG=1` and WARP + `VICTORY_D3D12_DEBUG=1`; inside-out scissor rects emit the D3D12 Debug Layer error above. - **After fix**: - Targeted test passes on hardware default, hardware `--features diagnostics` + `VICTORY_D3D12_DEBUG=1`, and WARP `--features diagnostics` + `VICTORY_D3D12_DEBUG=1` with `host.log_messages() == None`. - Full `cargo test -p victory-gpu --lib --locked` passes on hardware default (`230 passed`), hardware `diagnostics + DEBUG=1` (`244 passed`), and WARP `diagnostics + DEBUG=1` (`246 passed`). - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`) and `xtask` policy checks pass cleanly.
State::viewports and State::scissors previously forwarded arbitrary title slices directly to ID3D12GraphicsCommandList::RSSetViewports and RSSetScissorRects and saved them verbatim for segment-cut replay. Passing more than D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE (16) entries aborts the host process under D3D12 validation (exit code 2173 on both hardware and WARP), and passing an inside-out scissor rectangle (right < left or bottom < top) triggers a D3D12 Debug Layer error.

Clamp both slices to D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE (16) and normalize any inverted scissor rectangle by clamping right to max(right, left) and bottom to max(bottom, top) (preserving empty clipping semantics without overflow), storing the sanitized state so State::cut replays valid bounds as well.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:32:50 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 11:27 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 21/21 passed (including viewports_and_scissors_clamp_count_and_inverted_rects).
  • cargo test -p victory-gpu --lib list:: -- --test-threads=1: 4/4 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly clamps viewport/scissor counts to the D3D12 limit and normalizes inverted scissor rects.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 11:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/3176#pullrequestreview-6073350340) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 21/21 passed (including `viewports_and_scissors_clamp_count_and_inverted_rects`). - `cargo test -p victory-gpu --lib list:: -- --test-threads=1`: 4/4 passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly clamps viewport/scissor counts to the D3D12 limit and normalizes inverted scissor rects. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 11:28 UTC · original

WARP verification of #3176 at 308c228d (software adapter only; no physical GPU used). AI-assisted: Claude Code (Claude Sonnet 5.5).

Setup. The head is one commit on top of main 63bcca4 (merge-base = 63bcca4); head SHA confirmed before and after testing. Windows 11 (26100), rustc/cargo 1.98.1, VICTORY_D3D12_WARP=1 (WARP, d3d10warp.dll 10.0.26100.9818), VICTORY_DXIL = Windows SDK 10.0.26100.0 dxil.dll 1.8.2502.11. Every cargo command used -j 4, --locked, a separate target dir.

Results

  • cargo test -p victory-gpu --lib --locked viewports_and_scissors -- --test-threads=1 -> draw::tests::viewports_and_scissors_clamp_count_and_inverted_rects 1 passed. It runs for real on WARP (it returns early only when no host device exists), including the D3D12 Close()/execute and a check that the debug layer logged nothing.
  • Whole crate: cargo test -p victory-gpu --lib --locked -- --test-threads=1 -> 232 passed, 0 failed, 1 ignored. Baseline main 63bcca4, same command and environment: 231 passed, 0 failed, 1 ignored. The difference is the one new test; no failure to attribute.
  • Fix reverted (State::viewports / State::scissors restored to the 63bcca4 bodies, test kept): the test process crashes with STATUS_ACCESS_VIOLATION (0xc0000005) on WARP when handed 20 viewports/rects. So the test does detect the missing clamp (on this adapter the failure mode is a crash, not an assertion).

Diff review. No concrete defect found. Both setters now truncate to D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE (16) before recording and issuing, and the scissor path normalizes right = max(right, left), bottom = max(bottom, top) and stores the normalized rects in fixed.scissors, so the replay in State::cut is also valid. Observations, not defects: an inverted rect becomes an empty rect at its left/top edge, which is a choice about guest semantics that I could not check against console behaviour; a count above 16 is truncated silently (no log line); the test only exercises the host-list path, not an end-to-end draw.

Not tested

  • Any physical GPU or driver (only WARP; a hardware driver may react differently to the invalid inputs than WARP did).
  • Real draws using these states, or game content; no game files used.
  • Other crates, cargo clippy, cargo fmt --check, release profile.
  • The console's behaviour for inverted scissor rects.
**@⁠oni-swr** on GitHub · 2026-11-26 11:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3176#issuecomment-7190779596) **WARP verification of #3176 at `308c228d`** (software adapter only; no physical GPU used). AI-assisted: Claude Code (Claude Sonnet 5.5). **Setup.** The head is one commit on top of `main` `63bcca4` (merge-base = `63bcca4`); head SHA confirmed before and after testing. Windows 11 (26100), rustc/cargo 1.98.1, `VICTORY_D3D12_WARP=1` (WARP, `d3d10warp.dll` 10.0.26100.9818), `VICTORY_DXIL` = Windows SDK 10.0.26100.0 `dxil.dll` 1.8.2502.11. Every cargo command used `-j 4`, `--locked`, a separate target dir. **Results** - `cargo test -p victory-gpu --lib --locked viewports_and_scissors -- --test-threads=1` -> `draw::tests::viewports_and_scissors_clamp_count_and_inverted_rects` **1 passed**. It runs for real on WARP (it returns early only when no host device exists), including the D3D12 `Close()`/`execute` and a check that the debug layer logged nothing. - Whole crate: `cargo test -p victory-gpu --lib --locked -- --test-threads=1` -> **232 passed, 0 failed, 1 ignored**. Baseline `main` `63bcca4`, same command and environment: **231 passed, 0 failed, 1 ignored**. The difference is the one new test; no failure to attribute. - Fix reverted (`State::viewports` / `State::scissors` restored to the `63bcca4` bodies, test kept): the test process **crashes with `STATUS_ACCESS_VIOLATION` (0xc0000005)** on WARP when handed 20 viewports/rects. So the test does detect the missing clamp (on this adapter the failure mode is a crash, not an assertion). **Diff review.** No concrete defect found. Both setters now truncate to `D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE` (16) before recording and issuing, and the scissor path normalizes `right = max(right, left)`, `bottom = max(bottom, top)` and stores the normalized rects in `fixed.scissors`, so the replay in `State::cut` is also valid. Observations, not defects: an inverted rect becomes an empty rect at its left/top edge, which is a choice about guest semantics that I could not check against console behaviour; a count above 16 is truncated silently (no log line); the test only exercises the host-list path, not an end-to-end draw. **Not tested** - Any physical GPU or driver (only WARP; a hardware driver may react differently to the invalid inputs than WARP did). - Real draws using these states, or game content; no game files used. - Other crates, `cargo clippy`, `cargo fmt --check`, release profile. - The console's behaviour for inverted scissor rects.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3176/head:github-bot/gh-3176'
git switch 'github-bot/gh-3176'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3176'
git switch 'github-bot/gh-3176'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3176'
git switch 'github-bot/gh-3176'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3176'
git switch 'main'
git merge --squash 'github-bot/gh-3176'
git switch 'main'
git merge --ff-only 'github-bot/gh-3176'
git switch 'main'
git merge 'github-bot/gh-3176'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3176
No description provided.