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

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

@⁠fuzeofficlal on GitHub · 2026-11-25 16:38 UTC · original
fuzeofficlal:fix/shader-rect-thin-right-angle → main · +155 −20 · 1 files · 1 commits · open

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).
**@⁠fuzeofficlal** on GitHub · 2026-11-25 16:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/2603) `fuzeofficlal:fix/shader-rect-thin-right-angle` → `main` · +155 −20 · 1 files · 1 commits · open ## 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`).
In right_angle and rectangle_gs, comparing squared side lengths (d12 > d20 && d12 > d01) absorbed the short edge of thin rectangles (for example 2^-12 against 1.0, where 1.0 + 2^-24 rounds to 1.0 in f32), picking v1 instead of v0 and flipping the fourth corner to the wrong side. Keep the strict squared-length comparison and pair it with a dot-product rescue (d01 > 4 * dot(e01, e02) && d20 > 4 * dot(e01, e02) for v0, and symmetrically with e02 and e12 for v2) that guarantees opp^2 > max + 0.5 * min while keeping exact isosceles ties on v1. Add unit and GPU/WARP tests for thin rectangles and exact isosceles ties.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:08 +00:00
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-25 16:50 UTC · original

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.

**@⁠DrDarkDK** on GitHub · 2026-11-25 16:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7168614280) 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.*
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 17:22 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 17:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7169402983) 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`.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:19 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 23:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7177403601) <!-- 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)_
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-25 23:52 UTC · original

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.
**@⁠futurepro62** on GitHub · 2026-11-25 23:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2603#issuecomment-7178149670) 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 can be merged automatically.
This branch is out-of-date with the base branch
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/2603/head:github-bot/gh-2603'
git switch 'github-bot/gh-2603'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2603'
git switch 'github-bot/gh-2603'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2603'
git switch 'github-bot/gh-2603'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2603'
git switch 'main'
git merge --squash 'github-bot/gh-2603'
git switch 'main'
git merge --ff-only 'github-bot/gh-2603'
git switch 'main'
git merge 'github-bot/gh-2603'
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!2603
No description provided.