Shaders: a rectangle's fourth corner is across the diagonal from its right angle, wherever that comes #2366
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2366
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2366/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@r4tedx on GitHub · 2026-11-25 11:57 UTC · original
r4tedx:fix/edge-triangles→main· +154 −42 · 1 files · 3 commits · closed without mergingThe problem
A rectangle list gives three corners of each rectangle. The hardware adds the fourth across the diagonal from the corner between the other two.
rectangle_gs(emit/rect.rs) assumes that corner is always the second vertex and adds the fourth atv0 + v2 - v1.The title's tiled-lighting draws give their tiles as top left, bottom left, top right, so the right-angle corner comes first. For them
v0 + v2 - v1is a point above the tile's top-right corner. The second triangle then covers the lower-right half of the tile above, with this tile's attributes, and each tile's own lower-right half is never drawn. On screen this shows as saw-tooth triangles on a diagonal, tile-sized grid along geometry edges.The fix
The geometry shader picks the corner between the other two as the one facing the longest side, in screen space (
x/w,y/wofSV_Position), and emits the completion(a, b, a + b - c). It turns the same way as the first triangle in all three orders. With no position output, or with a tie (a degenerate rectangle), it keeps the old rule (the second corner), so full-screen rectangles given as top left, bottom left, bottom right come out as before.Tests
rectangles_get_their_fourth_corner(existing, corner second) now shares its drawing code with two new tests:rectangles_with_the_corner_first_complete_across_their_diagonalrectangles_with_the_corner_third_complete_across_their_diagonalEach draws one square into 32x32 and checks every pixel: inside, the interpolated coordinates; outside, nothing.
Run on WARP (the software adapter) only:
main940d32femit::rect::tests(VICTORY_WARP=1)main'srect.rs)victory-shader --lib(VICTORY_WARP=1, every-adapter tests skipped)gathers_at_a_level_match_the_hardware,permutes_read_the_lane_named_and_zero_from_lanes_left_out)cargo clippy -p victory-shader --all-targets -- -D warningsHow it was found
Local title data was used for the diagnosis only: a target dump of one frame (RTX 5070, a build of
mainwith open PRs) and offline disassembly of the vertex shaders involved. The pattern first appears in the tiled-lighting output target, and in 128x128 tiles exactly thex + y >= 128half is lit. The tests above use no title data.Not covered
primitive.rs's CPU fallback (pipelines without a geometry shader) makes the same second-corner assumption. It doesn't know which input is the position, so it is unchanged here.AI-assisted
🤖 Generated with Claude Code
@Machad3x on GitHub · 2026-11-25 12:26 UTC · original
In-game check of this branch (the PR's "Not yet checked in game" item), RTX 3080 Ti, driver 621.24, Windows 11. Both commits (
a54e0c84,1853bce5) cherry-picked onto a local stack ofmain940d32fwith other open PRs, including #439.emit::recttests on the hardware adapter run: 3 passed.Only the screenshots were inspected, not a frame dump.
AI-assisted (Claude Code).
@r4tedx on GitHub · 2026-11-25 12:28 UTC · original
@Machad3x thank you for the in-game check: it is the first one on hardware, and it matches what the tiled-lighting draws led us to expect. Your note that the frame rate stayed the same is useful too, since the fix adds a few instructions to every rectangle.
@Melow-code thanks for the review. Taking the three points in order, from the code at
1853bce5:w = 0.
Bin::SDivis LLVM's binary opcode 4, and on float operands it isfdiv(the same opcodev_rcp_f32uses inlower.rs). So a zero w gives ±inf or NaN, not an undefined value. If any corner's x/w or y/w is inf or NaN, the two sides that touch it come out inf or NaN.OGTis ordered, so it is false for NaN and for inf > inf, and the only finite side is never the longest. Bothfirstandthirdare then false, andpicktakes the second corner, which is main's rule (v0 + v2 - v1). A degenerate w falls back to the old behaviour without needing a guard. I have not checked what happens when the three corners' w have different signs (some corners behind the eye). The pick then works on the mirrored projected points.Tests. The three input orders have a test each in
emit/rect.rs:rectangles_get_their_fourth_corner(the right angle second),rectangles_with_the_corner_first_complete_across_their_diagonalandrectangles_with_the_corner_third_complete_across_their_diagonal. Each one draws the square into 32x32 and checks every pixel on WARP and on the hardware adapter. You are right that the fallbacks have no test: no position output, a position with fewer than 4 columns, and a zero w. I'll add tests for them and report back here.Ties. A strict tie can only happen when two sides are equal and longer than the third. A real rectangle's three corners form a right triangle, and its hypotenuse is strictly the longest side, so a rectangle never ties. A tie means the input is not a rectangle (it is degenerate, or equilateral, or similar). Then both comparisons are false and the second corner is used, whatever the order, so the tie case is order-independent by construction: it is main's old rule. That is intended.
No title data was used for this reply (code reading only).
AI-assisted (Claude Code).
@hah346 on GitHub · 2026-11-25 12:57 UTC · original
Turing hardware result for #2366 at
1853bce5016d2be0ab6c397da7fab6ea4c9dc1d2, as requested in #439. Windows 11 build 26300, RTX 2060 (6 GiB), NVIDIA 616.64, Rust 1.98.1 MSVC.Clean checkout,
VICTORY_WARPunset:cargo test -p victory-shader --lib --locked emit::rect -- --test-threads=1gave 3 passed, 0 failed, 0 ignored (1.12 s).I also reran with temporary test-only diagnostics to rule out an adapter skip. All three cases explicitly selected the RTX 2060 for
warp falseand Microsoft Basic Render Driver forwarp true. Each case returned 1,024 texels per adapter, and all existing pixel checks passed (3,072 pixels per adapter, 6,144 total). The checkout is clean again. Synthetic fixtures only; AI-assisted with Codex.@r4tedx on GitHub · 2026-11-25 12:59 UTC · original
@Melow-code following up on the tests I said I'd add: head is now
d29e14f7(one test-only commit on1853bce5).rectangles_with_zero_w_complete_from_the_second_cornerdraws a rectangle whose second corner has w = 0 and checks a pixel the fourth corner weighs into. The comparisons come out unordered, so the completion falls back to main's rule (v0 + v2 - v1), as I described above. It passes on WARP.firstandthirdareNone, andpickreturns the second corner, which is main's rule unchanged. So there's no new test for it.SV_Positionmust have four components, so the validator refuses anything shorter. I tried testing it through the emitted DXIL text, but that means startingdxcfrom a test and matching exact value numbers, so I dropped it.VICTORY_WARP=1is set.Rect tests on WARP: 4 passed.
cargo clippy -p victory-shader --tests -- -D warnings: clean. Synthetic tests only; no title data used.AI-assisted
@r4tedx on GitHub · 2026-11-25 12:59 UTC · original
@hah346 thank you. That's the first hardware run of the rect tests on Turing, and checking which adapter was picked rules out a silent skip.
The head has since moved to
d29e14f7: one test-only commit on1853bce5that addsrectangles_with_zero_w_complete_from_the_second_corner. The fix itself is unchanged, so your result holds for it. A rerun at the new head would give 4 tests per adapter, if you want to cover the new one as well.AI-assisted
@desplmfao on GitHub · 2026-11-25 13:02 UTC · original
In-title result for this PR on hardware, since it has only run on WARP and macOS so far.
Setup: Grand Theft Auto VI Series X, the first mission's prison yard (player character at the window). RTX 5080 on Linux, Proton with vkd3d-proton 3.2.0 and the NVIDIA 580 driver. I built our working branch (
mainplus our open GPU fixes) with this PR merged at1853bce, then compared it with the same branch without this PR in the same scene.Without this PR: rows of light right-angled triangles in every frame. They run along the top edge above the window, along the bottom edge of the screen, across the window frame, and cut diagonally into the player character's neck and shoulders. Each one looks like half of a quad.
With this PR: all of those rows are gone. The rest of the picture is unchanged, at 4,861 draws per frame.
The tests also pass here:
cargo test -p victory-shader --lib --locked emit::rect -- --test-threads=1gives 3 passed. That's weak evidence, though. Under Wine, DXVK's DXGI has no WARP adapter (EnumWarpAdapter: WARP not supported, returning first hardware adapter), so both sides of the comparison ran on the RTX 5080. The in-title result is the part that matters.How it was found: the triangle rows showed up in every screenshot of this scene, and their regular half-quad shape pointed at rectangle expansion. I merged this PR into the same build and the user compared the two runs by eye.
Without this PR (same scene):

With this PR:

@r4tedx on GitHub · 2026-11-25 13:03 UTC · original
@desplmfao thank you. It's the first in-title check on vkd3d-proton under Linux, and the before/after in one scene shows exactly the half-quad rows this was meant to remove.
Your note on the tests is a good one to have on record. Under Wine, DXVK's DXGI hands back the first hardware adapter when asked for WARP, so on that setup both halves of each rect test run on the RTX 5080, as you say. The head is now
d29e14f7(one test-only commit adding a zero-w case). The fix is the same, so your result applies to it.This reply is based only on reading your report and the PR's code; no title data was used.
AI-assisted
@hah346 on GitHub · 2026-11-25 13:03 UTC · original
Reran at
d29e14f7802647acfe0b0a9e5e167c4e86af6687on the same Windows 11 / RTX 2060 (616.64) setup, withVICTORY_WARPunset. The clean checkout gave 4 passed, 0 failed, 0 ignored (1.40 s).A separate diagnostic rerun confirmed that all four cases, including
rectangles_with_zero_w_complete_from_the_second_corner, drew and passed their existing checks on both the RTX 2060 and Microsoft WARP. The adapter checks were local to the test and the checkout is clean again. Synthetic fixtures only; AI-assisted with Codex.@Marvisak on GitHub · 2026-11-25 14:50 UTC · original
Automated triage for the maintainers.
Merge candidate.
rectangle_gsincrates/victory-shader/src/emit/rect.rsno longer assumes the right-angle corner of a rectangle-list primitive is the second vertex: it picks the corner opposite the longest screen-space side and completes(a, b, a + b - c), falling back tomain'sv0 + v2 - v1with no position, a tie, or a non-finitex/w. I checked thatBin::SDiv/Bin::Addon f32 operands encode asfdiv/fadd(as elsewhere in the lowering), thatOGTis ordered so NaN/inf falls back to the old rule, and that the winding of the second triangle matches the first in all three orders. The evidence is the title's own tiled-lighting vertex shaders (corners given top left, bottom left, top right), plus independent in-title before/after reports in the thread; the tests cover all three orders and zero w on hardware and WARP.Caveats for the maintainer:
primitive.rs's CPU fallback keeps the second-corner rule, and #1907 touches the same function.CI:
checkpassing.Generated by Claude Code
@r4tedx on GitHub · 2026-11-25 15:49 UTC · original
Closing this as superseded by
8ff79135on main ("rectangles complete opposite their right angle, wherever it is"). It finds the corner opposite the longest side the same way, keepsv1on ties and NaN, and covers the CPU expansion too. Thanks to everyone who tested this, especially Machad3x for the RTX 3080 Ti confirmation and Melow-code for the review.Two small differences, for the record only:
v1, where main returnsMalformed.If a title ever turns up a case where either matters, I'll send a small follow-up against main.
AI-assisted
@Slay326 on GitHub · 2026-11-25 16:25 UTC · original · edited 2026-11-25 16:27 UTC
Note: I posted this after the PR was closed as superseded by
8ff79135on main; the run below used this PR's head, not8ff79135. Leaving it for the record, since it is the same fix on the same scene. I have not yet run8ff79135itself on this card.Hardware test: this fixes the orange triangles on an RTX 3070. RTX 3070 (8 GB, 32-lane waves), Windows 11, Series X mode, 480 s runs into the world of Grand Theft Auto VI 1.0.2.1. Local test branch: main
940d32fplus #1906, #1890, #1883, #1962, #1779, #1763, #1762, a page-mark guard like #2167 and the memory PRs #2129, #2180, #1873, #1969, #2161.Before: orange right triangles along tile edges over the whole screen in the lit world (the tiled-lighting rectangle draws
VS_TiledLighting,VS_TiledWeathering_*,VS_Blit; the lighting target showed the second triangle of each tile one tile up, as the description says).With this PR (merged without conflicts, together with #158, #2342, #2324, #2363, #2348, #1014): the triangles are gone in the same cell scene. 480 s run, no device removal, world median 1.9 fps.
victory-shader270 passed, 0 failed on this card.AI-assisted.
Pull request closed