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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3176 into main
fuzeofficlal commented 2026-11-26 11:11:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:11:23Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6625384117 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3176). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:11:23Z. --- ### 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.
Dukestar69 commented 2026-11-26 12:05:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T11:27:27Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6073350340 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3176#pullrequestreview-6073350340). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T11:27:27Z. **Review:** APPROVED --- 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).
oni-swr commented 2026-11-26 12:05:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T11:28:44Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7190779596 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3176#issuecomment-7190779596). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T11:28:44Z. --- **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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3176:upstream-pr/3176'
git switch 'upstream-pr/3176'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3176'
git switch 'upstream-pr/3176'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3176'
git switch 'upstream-pr/3176'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3176'
git switch 'main'
git merge --squash 'upstream-pr/3176'
git switch 'main'
git merge --ff-only 'upstream-pr/3176'
git switch 'main'
git merge 'upstream-pr/3176'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3174
No description provided.