Shaders: avoid f32 precision loss in thin rectangle right-angle check #2601

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/2603 into main
fuzeofficlal commented 2026-11-25 16:38:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T16:38:17Z.


Summary

right_angle and rectangle_gs in crates/victory-shader/src/emit/rect.rs picked the corner opposite the longest side by comparing squared side lengths directly (d12 > d20 && d12 > d01, d01 > d12 && d01 > d20).

For thin rectangles whose aspect ratio is at least 2^12 (for example a 1-pixel-tall bar across a 4K target, [[0, 0], [0, 2^-12], [1, 0]]), the squared hypotenuse 1.0 + 2^-24 rounds to 1.0 in f32, tying d20 = 1.0. The strict > check fails and falls back to v1 instead of v0, placing the completed fourth corner on the wrong side ((1, -2^-12) instead of (1, +2^-12)).

Changes

  • crates/victory-shader/src/emit/rect.rs:
    • Rewrite the comparisons in both right_angle and rectangle_gs using the law of cosines:
      • d12 > d20 && d12 > d01 ⇔ d01 > 2 * dot(e01, e02) && d20 > 2 * dot(e01, e02)
      • d01 > d12 && d01 > d20 ⇔ d20 > 2 * dot(e02, e12) && d12 > 2 * dot(e02, e12)
    • This avoids absorbing the short edge's squared length into the long edge's squared length while preserving exact tie, degenerate-point, obtuse-triangle, and NaN fallback semantics (v1).
    • Add rule_tests::thin_rectangles_find_their_right_angle (axis-aligned, shifted, 2^-20, rotated, and thin isosceles tie cases) and tests::thin_rectangles_get_their_fourth_corner (validating DXIL and rasterizing a 2^-12-tall rectangle on an 8x8192 target on both hardware and WARP).

Verification

  • Negative control (before fix): thin_rectangles_find_their_right_angle fails (left: 1, right: 0), and thin_rectangles_get_their_fourth_corner renders empty pixels at row 4095 on hardware/WARP because the fourth corner flips onto -y.
  • After fix: cargo test -p victory-shader --lib emit::rect — 4/4 pass on hardware and WARP (dxil.dll validation included).
  • cargo clippy -p victory-shader --all-targets -- -D warnings and --features host — pass (0 warnings).
  • cargo xtask policy — pass (184 files ok).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6609107085 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2603). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T16:38:17Z. --- ## Summary `right_angle` and `rectangle_gs` in `crates/victory-shader/src/emit/rect.rs` picked the corner opposite the longest side by comparing squared side lengths directly (`d12 > d20 && d12 > d01`, `d01 > d12 && d01 > d20`). For thin rectangles whose aspect ratio is at least `2^12` (for example a 1-pixel-tall bar across a 4K target, `[[0, 0], [0, 2^-12], [1, 0]]`), the squared hypotenuse `1.0 + 2^-24` rounds to `1.0` in `f32`, tying `d20 = 1.0`. The strict `>` check fails and falls back to `v1` instead of `v0`, placing the completed fourth corner on the wrong side (`(1, -2^-12)` instead of `(1, +2^-12)`). ## Changes - `crates/victory-shader/src/emit/rect.rs`: - Rewrite the comparisons in both `right_angle` and `rectangle_gs` using the law of cosines: - `d12 > d20 && d12 > d01` ⇔ `d01 > 2 * dot(e01, e02) && d20 > 2 * dot(e01, e02)` - `d01 > d12 && d01 > d20` ⇔ `d20 > 2 * dot(e02, e12) && d12 > 2 * dot(e02, e12)` - This avoids absorbing the short edge's squared length into the long edge's squared length while preserving exact tie, degenerate-point, obtuse-triangle, and `NaN` fallback semantics (`v1`). - Add `rule_tests::thin_rectangles_find_their_right_angle` (axis-aligned, shifted, `2^-20`, rotated, and thin isosceles tie cases) and `tests::thin_rectangles_get_their_fourth_corner` (validating DXIL and rasterizing a `2^-12`-tall rectangle on an `8x8192` target on both hardware and WARP). ## Verification - Negative control (before fix): `thin_rectangles_find_their_right_angle` fails (`left: 1, right: 0`), and `thin_rectangles_get_their_fourth_corner` renders empty pixels at row `4095` on hardware/WARP because the fourth corner flips onto `-y`. - After fix: `cargo test -p victory-shader --lib emit::rect` — 4/4 pass on hardware and WARP (`dxil.dll` validation included). - `cargo clippy -p victory-shader --all-targets -- -D warnings` and `--features host` — pass (0 warnings). - `cargo xtask policy` — pass (`184 files ok`).
DrDarkDK commented 2026-11-25 17:05:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T16:50:34Z.


I found an exact tie where the fallback changes:

[[0.0, 0.0], [8193.0 / 16384.0, 4100.0 / 16384.0], [4100.0 / 16384.0, 8193.0 / 16384.0]]

Here d01 == d20 > d12. The base rule returns 1. The new rule returns 2 because d12 rounds to 0x3dffa009 and 2 * dot(e02, e12) rounds one ULP lower. That changes the fourth vertex in the CPU expansion path.

Could we add this input to rule_tests to cover the documented tie fallback? I verified the arithmetic with explicit f32 rounding after each operation. I didn’t run the Rust tests or the geometry shader.

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168614280 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7168614280). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-25T16:50:34Z. --- I found an exact tie where the fallback changes: `[[0.0, 0.0], [8193.0 / 16384.0, 4100.0 / 16384.0], [4100.0 / 16384.0, 8193.0 / 16384.0]]` Here `d01 == d20 > d12`. The base rule returns `1`. The new rule returns `2` because `d12` rounds to `0x3dffa009` and `2 * dot(e02, e12)` rounds one ULP lower. That changes the fourth vertex in the CPU expansion path. Could we add this input to `rule_tests` to cover the documented tie fallback? I verified the arithmetic with explicit f32 rounding after each operation. I didn’t run the Rust tests or the geometry shader. *AI-assisted with Codex.*
fuzeofficlal commented 2026-11-25 18:02:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T17:22:09Z.


Great catch @DrDarkDK — replacing d01 > d20 outright with d12 > 2 * dot(e02, e12) introduced a 1-ULP sensitivity on exact acute isosceles ties like [[0, 0], [8193/16384, 4100/16384], [4100/16384, 8193/16384]].

Updated (2a9cca1) in both right_angle and rectangle_gs to keep the strict squared-length check (d12 > d20 && d12 > d01) and pair it with a dot-product rescue (d01 > 4 * dot(e01, e02) && d20 > 4 * dot(e01, e02)) (and symmetrically for v2):

  • Whenever 4 * dot < min(leg0^2, leg1^2), the law of cosines guarantees opp^2 > max + 0.5 * min, recovering thin rectangles (both axis-aligned and rotated, well past 2^-20).
  • On any exact isosceles tie (2 * dot == base^2), 4 * dot == 2 * base^2 fails the rescue with a 2x margin (never near 1 ULP), preserving the exact v1 tie fallback.

Added your input (and its symmetric rotation) to rule_tests::thin_rectangles_find_their_right_angle asserting == 1, and re-verified all 4 emit::rect tests on RTX 5090 + WARP + dxil.dll, clippy (default + --features host), and cargo xtask policy.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169402983 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7169402983). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T17:22:09Z. --- Great catch @DrDarkDK — replacing `d01 > d20` outright with `d12 > 2 * dot(e02, e12)` introduced a 1-ULP sensitivity on exact acute isosceles ties like `[[0, 0], [8193/16384, 4100/16384], [4100/16384, 8193/16384]]`. Updated (`2a9cca1`) in both `right_angle` and `rectangle_gs` to keep the strict squared-length check `(d12 > d20 && d12 > d01)` and pair it with a dot-product rescue `(d01 > 4 * dot(e01, e02) && d20 > 4 * dot(e01, e02))` (and symmetrically for `v2`): - Whenever `4 * dot < min(leg0^2, leg1^2)`, the law of cosines guarantees `opp^2 > max + 0.5 * min`, recovering thin rectangles (both axis-aligned and rotated, well past `2^-20`). - On any exact isosceles tie (`2 * dot == base^2`), `4 * dot == 2 * base^2` fails the rescue with a 2x margin (never near 1 ULP), preserving the exact `v1` tie fallback. Added your input (and its symmetric rotation) to `rule_tests::thin_rectangles_find_their_right_angle` asserting `== 1`, and re-verified all 4 `emit::rect` tests on RTX 5090 + WARP + `dxil.dll`, clippy (default + `--features host`), and `cargo xtask policy`.
Marvisak commented 2026-11-26 00:03:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:19:13Z.


Automated triage for the maintainers.

In crates/victory-shader/src/emit/rect.rs, right_angle and the matching GPU logic in rectangle_gs keep the existing strict longest-side test and add a rescue: d01 > 4*dot(e01,e02) && d20 > 4*dot(e01,e02) for v0, and the same for v2. This handles thin rectangles whose squared hypotenuse rounds to the long leg in f32. I checked the math: by the law of cosines, 4*dot < min(leg²) gives opp² > max + min/2, so in exact arithmetic the rescue fires only when the opposite side really is the longest. The v0 and v2 rescues cannot both hold, and the GPU pick order matches the CPU order. Exact isosceles ties fail the rescue by a factor of 2 and stay on v1, including the tie case raised in review, which is now in rule_tests. A rasterizing test on an 8x8192 target is also added. It merges cleanly with current main. CI: passing (check). This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177403601 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7177403601). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:19:13Z. --- <!-- victory-triage v1 head=2a9cca14cc9bd3209c8d7d179fe8ca0efecf8ea1 verdict=merge-candidate at=2026-11-25T23:19:05Z --> Automated triage for the maintainers. In `crates/victory-shader/src/emit/rect.rs`, `right_angle` and the matching GPU logic in `rectangle_gs` keep the existing strict longest-side test and add a rescue: `d01 > 4*dot(e01,e02) && d20 > 4*dot(e01,e02)` for v0, and the same for v2. This handles thin rectangles whose squared hypotenuse rounds to the long leg in f32. I checked the math: by the law of cosines, `4*dot < min(leg²)` gives `opp² > max + min/2`, so in exact arithmetic the rescue fires only when the opposite side really is the longest. The v0 and v2 rescues cannot both hold, and the GPU `pick` order matches the CPU order. Exact isosceles ties fail the rescue by a factor of 2 and stay on v1, including the tie case raised in review, which is now in `rule_tests`. A rasterizing test on an 8x8192 target is also added. It merges cleanly with current `main`. CI: passing (check). This is a recommendation for a maintainer to look, not an approval. --- _Generated by [Claude Code](https://claude.ai/code)_
futurepro62 commented 2026-11-26 00:03:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-25T23:52:06Z.


Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head 2a9cca1 (on its base 67e8ae1).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 775 passed, 0 failed, 9 ignored.
  • The new tests ran and passed: thin_rectangles_find_their_right_angle, thin_rectangles_get_their_fourth_corner.
  • --skip leaves out tests that fail on main 842b0cb on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7178149670 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7178149670). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-25T23:52:06Z. --- Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head `2a9cca1` (on its base `67e8ae1`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **775 passed, 0 failed, 9 ignored**. - The new tests ran and passed: `thin_rectangles_find_their_right_angle`, `thin_rectangles_get_their_fourth_corner`. - `--skip` leaves out tests that fail on main `842b0cb` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
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/2603:upstream-pr/2603'
git switch 'upstream-pr/2603'

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/2603'
git switch 'upstream-pr/2603'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2603'
git switch 'upstream-pr/2603'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2603'
git switch 'main'
git merge --squash 'upstream-pr/2603'
git switch 'main'
git merge --ff-only 'upstream-pr/2603'
git switch 'main'
git merge 'upstream-pr/2603'
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!2601
No description provided.