d3d12: post-divide NDC dot-product corner selection and exact fourth corner for rectangle lists (RT-RECT-LIST-CORNER) #2478

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

@⁠lixonik on GitHub · 2026-11-27 16:10 UTC · original
lixonik:rt-rect-list-corner → main · +681 −41 · 4 files · 1 commits · open

Fixes the rectangle-list fourth-corner selection and reconstruction in runtime/src/rect_list_geometry.h for package RT-RECT-LIST-CORNER (#2464 Part B; builds on @⁠kevincommits's numerical analysis in #1608).

Root cause removed

  1. Post-divide NDC dot-product corner rule: Previously, rect_list_geometry() picked the corner opposite the longest edge using pre-divide clip-space squared lengths dx^2 + dy^2. That failed on:
    • thin axis-aligned strips (aspect > ~4100:1, where dx^2 + dy^2 == dx^2 in float32 breaks the tie-break and flips the rectangle);
    • large uniform w (e.g., w = 1e20, where dx^2 + dy^2 overflows to +Inf);
    • differing w across vertices (where pre-divide distances pick the wrong corner).
      Computing post-divide NDC (x/w, y/w) and selecting the vertex with the minimum signed incident-edge dot product (equivalently max dot(e_in, e_out), unordered >= so NaN / w == 0 deterministically falls back to v1) is algebraically identical to opposite-the-longest-side by the law of cosines for all triangles (including rotated rectangles, near-square, obtuse/acute, and degenerate) while keeping exact 0.0 dot product at any axis-aligned right angle.
  2. Exact non-drifting fourth corner & differing w: Replaces (((v0 + v1) + v2) - corner) - corner with direct (other_a, other_b, corner) selection and per-component bit-equality checks (OpBitcast + OpIEqual), preserving axis-aligned coordinates and constant/flat attributes bit-exact (0 ULP drift). When w is uniform across the three vertices, clip-space exact_fourth runs with zero divide/remultiply rounding; when w differs, position and varying attributes extrapolate in screen-space NDC and 1/w (when 1/w4 > 0), matching single-plane perspective interpolation across both triangles (falling back to w4 = 1 when 1/w4 <= 0).
  3. Note on #1970: Leaves the preamble SPIR-V parser (lines 65-118) untouched to stay orthogonal to #1970.

Testing

  • Negative control on unmodified main (ec91d695c): d3d12_rect_list_corner_test fails on 6 corner-rule cases (w=1e20 corner v2: 2080/4096 pixels off plane; differing w=(2,1,1): 2080/4096 off plane; differing w=(1,2,2): 3099/4096 missed; thin strip 5000:1 corner v2 & v1: 2048/4096 missed each; flat non-dyadic exact: 4096/4096 pixels drifted by 2 ULPs).
  • With this PR:
    • ctest -R "^(d3d12_rect_list_corner|indirect_topology_and_stats)$": 2/2 passed (d3d12_rect_list_corner_test: all 17 cases passed, 0.26 s).
    • python -I tests/test_ctest_registration.py -v: 6/6 passed.
    • python -I scripts/check_no_game_data.py ... and git diff --check: clean.
  • GPUs tested: NVIDIA GeForce RTX 5070 12 GB, driver 610.88, Vulkan 1.4.341, Windows 11, MSVC 19.44 (VK_LAYER_KHRONOS_validation not installed locally; d3d12_rect_list_corner_test is self-contained without llvm-mc and skips with 77 when geometryShader or Vulkan is unavailable).

AI-assisted (Antigravity, Gemini).

**@⁠lixonik** on GitHub · 2026-11-27 16:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2478) `lixonik:rt-rect-list-corner` → `main` · +681 −41 · 4 files · 1 commits · open Fixes the rectangle-list fourth-corner selection and reconstruction in `runtime/src/rect_list_geometry.h` for package `RT-RECT-LIST-CORNER` (#2464 Part B; builds on @⁠kevincommits's numerical analysis in https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). ### Root cause removed 1. **Post-divide NDC dot-product corner rule:** Previously, `rect_list_geometry()` picked the corner opposite the longest edge using pre-divide clip-space squared lengths `dx^2 + dy^2`. That failed on: - thin axis-aligned strips (aspect > ~4100:1, where `dx^2 + dy^2 == dx^2` in float32 breaks the tie-break and flips the rectangle); - large uniform `w` (e.g., `w = 1e20`, where `dx^2 + dy^2` overflows to `+Inf`); - differing `w` across vertices (where pre-divide distances pick the wrong corner). Computing post-divide NDC `(x/w, y/w)` and selecting the vertex with the minimum signed incident-edge dot product (equivalently max `dot(e_in, e_out)`, unordered `>=` so `NaN` / `w == 0` deterministically falls back to `v1`) is algebraically identical to opposite-the-longest-side by the law of cosines for all triangles (including rotated rectangles, near-square, obtuse/acute, and degenerate) while keeping exact `0.0` dot product at any axis-aligned right angle. 2. **Exact non-drifting fourth corner & differing `w`:** Replaces `(((v0 + v1) + v2) - corner) - corner` with direct `(other_a, other_b, corner)` selection and per-component bit-equality checks (`OpBitcast` + `OpIEqual`), preserving axis-aligned coordinates and constant/flat attributes bit-exact (0 ULP drift). When `w` is uniform across the three vertices, clip-space `exact_fourth` runs with zero divide/remultiply rounding; when `w` differs, position and varying attributes extrapolate in screen-space NDC and `1/w` (when `1/w4 > 0`), matching single-plane perspective interpolation across both triangles (falling back to `w4 = 1` when `1/w4 <= 0`). 3. **Note on #1970:** Leaves the preamble SPIR-V parser (lines 65-118) untouched to stay orthogonal to #1970. ### Testing - **Negative control on unmodified `main` (`ec91d695c`):** `d3d12_rect_list_corner_test` fails on 6 corner-rule cases (`w=1e20 corner v2`: 2080/4096 pixels off plane; `differing w=(2,1,1)`: 2080/4096 off plane; `differing w=(1,2,2)`: 3099/4096 missed; `thin strip 5000:1 corner v2` & `v1`: 2048/4096 missed each; `flat non-dyadic exact`: 4096/4096 pixels drifted by 2 ULPs). - **With this PR:** - `ctest -R "^(d3d12_rect_list_corner|indirect_topology_and_stats)$"`: 2/2 passed (`d3d12_rect_list_corner_test`: all 17 cases passed, 0.26 s). - `python -I tests/test_ctest_registration.py -v`: 6/6 passed. - `python -I scripts/check_no_game_data.py ...` and `git diff --check`: clean. - **GPUs tested:** NVIDIA GeForce RTX 5070 12 GB, driver 610.88, Vulkan 1.4.341, Windows 11, MSVC 19.44 (`VK_LAYER_KHRONOS_validation` not installed locally; `d3d12_rect_list_corner_test` is self-contained without `llvm-mc` and skips with 77 when `geometryShader` or Vulkan is unavailable). AI-assisted (Antigravity, Gemini).
Select the right-angle corner in post-divide NDC (x/w, y/w) via the minimum
signed edge dot product (law-of-cosines equivalent to opposite the longest
side, without float32 squared-length tie-break loss on thin axis-aligned
strips or pre-divide overflow/distortion when w is large or differs across
vertices). Compute the fourth corner bit-exact whenever either non-corner
vertex matches the corner (zero ULP drift on axis-aligned grid coordinates
and constant/flat attributes) and extrapolate linearly in NDC and 1/w when
w differs with positive 1/w4.

Adds self-contained GPU regression test d3d12_rect_list_corner_test covering
w != 1, large w = 1e20, differing w, 5000:1 thin strips, near-square, rotated
45-degree rectangles, bit-exact non-dyadic constant attributes, and degenerate
triangles.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 16:16:02 +00:00
Author
Owner

@⁠djrolfes on GitHub · 2026-11-27 17:15 UTC · original

Independent Linux/Wine + NVIDIA check at head 4283e3ffa (merged cleanly onto origin/main c0201a806 alongside #2480, #2477, #2476, #2474).

  • Environment: Linux 7.2.15, wine-11.22, clang-cl + xwin cross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Ryzen 7 5800X, 32 GB RAM. No game files.
  • CTest: d3d12_rect_list_corner passed (6.31 s) on RTX 3070 Ti under Wine.

AI-assisted.

**@⁠djrolfes** on GitHub · 2026-11-27 17:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2478#issuecomment-7224195905) Independent Linux/Wine + NVIDIA check at head `4283e3ffa` (merged cleanly onto `origin/main` `c0201a806` alongside `#2480`, `#2477`, `#2476`, `#2474`). - **Environment:** Linux `7.2.15`, `wine-11.22`, `clang-cl` + `xwin` cross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver `620.73.04`, Ryzen 7 5800X, 32 GB RAM. No game files. - **CTest:** `d3d12_rect_list_corner` **passed** (`6.31 s`) on RTX 3070 Ti under Wine. AI-assisted.
Author
Owner

@⁠MoonEyes on GitHub · 2026-11-27 18:40 UTC · original

Tested for #2464 as Aslanek asked: unit tests and a game run on my machine; CI ignored for now as asked.

RUN pr2478-court | 2026-11-27 | MoonEyes | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:        main ec91d695c + PR #2478 (head 4283e3ffa), merged locally without conflict, nothing pushed | shaders: main's own translation
MACHINE:     RTX 4060 8 GB, desktop, only GPU, display attached | NVIDIA 610.88 | Windows 11 Pro 26300 | Ryzen 5 3600 | 32 GB | Windows GPU timeout at the default (2 s)
RECIPE:      short recipe: docs/running.md Windows NVIDIA recipe, pipeline cache warm, 330 s cap, scripted button presses from 60 s | build RelWithDebInfo, only the PR's targets built
TESTS:
  - d3d12_rect_list_corner_test (new, ctest d3d12_rect_list_corner): pass on the PR | fails on main: yes (new test built with the PR's CMakeLists on top of main's sources: exit 1, 2080 of 4096 pixels off the exact constant)
GAME:        session=1 at 105 s | world entered at 160 s | last window >= 1 img/s ends at 235 s | 0 frames after world entry + 240 s | end: stopped by my local stall guard at 241 s (no new frame line for 180 s), not by the cap | device loss: no (no fault report) | private RAM max 17301 MB
COMPARED TO: w77 (main ec91d695c, same short recipe): session=1 at 145 s, world at 195 s, last window >= 1 img/s at 293 s, private RAM max 16043 MB, ended by the time limit (337 s)
             w79 (main, same short recipe): world at 200 s, last window >= 1 img/s at 286 s, private RAM max 16583 MB, ended by the time limit (350 s)
             one run each (n=1); on main the spread of world entry on this recipe is about 165 to 200 s, so no conclusion on timings
VERDICT:     new test passes and fails on main; game reached the world, stopped by the stall guard, no device loss; inconclusive for the game (n=1)

Reading (marked):

  • MEASURED: the display stops after the world is entered (75 s after world entry for the last window >= 1 img/s here; 98 s on w77, 86 s on w79). Present on main too, not caused by this PR (finding #1608).
  • MEASURED: the run was ended by my local stall guard, not by a crash or a device loss. A stall guard end is not comparable with the time-limit end of the main runs; compare the last-window figure, not the end time. I do not attribute the stop to this PR.

Limits: one machine, one run for the PR, one run for each main witness.

AI-assisted (Claude Code, Claude Opus 5.5)

**@⁠MoonEyes** on GitHub · 2026-11-27 18:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2478#issuecomment-7225750726) Tested for #2464 as Aslanek asked: unit tests and a game run on my machine; CI ignored for now as asked. ``` RUN pr2478-court | 2026-11-27 | MoonEyes | AI-assisted: Claude Code (Claude Opus 5.5) BASE: main ec91d695c + PR #2478 (head 4283e3ffa), merged locally without conflict, nothing pushed | shaders: main's own translation MACHINE: RTX 4060 8 GB, desktop, only GPU, display attached | NVIDIA 610.88 | Windows 11 Pro 26300 | Ryzen 5 3600 | 32 GB | Windows GPU timeout at the default (2 s) RECIPE: short recipe: docs/running.md Windows NVIDIA recipe, pipeline cache warm, 330 s cap, scripted button presses from 60 s | build RelWithDebInfo, only the PR's targets built TESTS: - d3d12_rect_list_corner_test (new, ctest d3d12_rect_list_corner): pass on the PR | fails on main: yes (new test built with the PR's CMakeLists on top of main's sources: exit 1, 2080 of 4096 pixels off the exact constant) GAME: session=1 at 105 s | world entered at 160 s | last window >= 1 img/s ends at 235 s | 0 frames after world entry + 240 s | end: stopped by my local stall guard at 241 s (no new frame line for 180 s), not by the cap | device loss: no (no fault report) | private RAM max 17301 MB COMPARED TO: w77 (main ec91d695c, same short recipe): session=1 at 145 s, world at 195 s, last window >= 1 img/s at 293 s, private RAM max 16043 MB, ended by the time limit (337 s) w79 (main, same short recipe): world at 200 s, last window >= 1 img/s at 286 s, private RAM max 16583 MB, ended by the time limit (350 s) one run each (n=1); on main the spread of world entry on this recipe is about 165 to 200 s, so no conclusion on timings VERDICT: new test passes and fails on main; game reached the world, stopped by the stall guard, no device loss; inconclusive for the game (n=1) ``` Reading (marked): - MEASURED: the display stops after the world is entered (75 s after world entry for the last window >= 1 img/s here; 98 s on w77, 86 s on w79). Present on main too, not caused by this PR (finding https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). - MEASURED: the run was ended by my local stall guard, not by a crash or a device loss. A stall guard end is not comparable with the time-limit end of the main runs; compare the last-window figure, not the end time. I do not attribute the stop to this PR. Limits: one machine, one run for the PR, one run for each main witness. AI-assisted (Claude Code, Claude Opus 5.5)
Author
Owner

@⁠MoonEyes on GitHub · 2026-11-27 18:56 UTC · original

Review of head 4283e3ffa (read against main 43a9d562d, which contains ec91d695c). The game run is in my RUN above. I suggest splitting this PR into two:

Ready to merge on its own: picking the corner in NDC with the dot product (I checked that it matches "longest side" through the law of cosines) and copying the vertices bit for bit. Both look sound to me.

Needs a source or a separate PR:

  1. runtime/src/rect_list_geometry.h:273-287: when w differs, the fourth corner is extrapolated in NDC and in 1/w. docs/d3d12_vulkan.md:3112 still says the hardware uses "v2 + v3 - v1 for the position and every interpolant", and this PR keeps that line while its new text in the bullet just below describes the NDC and 1/w rule, so the doc now states both. I found no hardware measurement behind the new rule, so the test checks behaviour that the PR itself defines. Could you either drop this path from the PR or point to a console reference that confirms it?
  2. runtime/src/rect_list_geometry.h:281-283: when 1/w4 <= 0, the position falls back to w4 = 1, but the attributes stay linearly extrapolated in clip space. Position and attributes then follow two different models. The differing w=(1,2,2) case (d3d12_rect_list_corner_test.cpp:253) uses a constant colour, so it checks coverage but not how varying attributes are extrapolated.

Minor points:

  • The for (std::size_t vi ... loop (rect_list_geometry.h:291) has a content conflict with #1970, which is still open, although the description says "orthogonal to #1970". The conflict is trivial: emitted.reserve against the indexed loop.
  • The header of d3d12_rect_list_corner_test.cpp:17 announces a case 7 "tile 160x90" that is not in the list of cases.

AI-assisted (Claude Code, Claude Opus 5.5)

**@⁠MoonEyes** on GitHub · 2026-11-27 18:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2478#issuecomment-7226027227) Review of head 4283e3ffa (read against main 43a9d562d, which contains ec91d695c). The game run is in my RUN above. I suggest splitting this PR into two: **Ready to merge on its own:** picking the corner in NDC with the dot product (I checked that it matches "longest side" through the law of cosines) and copying the vertices bit for bit. Both look sound to me. **Needs a source or a separate PR:** 1. `runtime/src/rect_list_geometry.h:273-287`: when `w` differs, the fourth corner is extrapolated in NDC and in 1/w. `docs/d3d12_vulkan.md:3112` still says the hardware uses "v2 + v3 - v1 for the position and every interpolant", and this PR keeps that line while its new text in the bullet just below describes the NDC and 1/w rule, so the doc now states both. I found no hardware measurement behind the new rule, so the test checks behaviour that the PR itself defines. Could you either drop this path from the PR or point to a console reference that confirms it? 2. `runtime/src/rect_list_geometry.h:281-283`: when `1/w4 <= 0`, the position falls back to `w4 = 1`, but the attributes stay linearly extrapolated in clip space. Position and attributes then follow two different models. The `differing w=(1,2,2)` case (`d3d12_rect_list_corner_test.cpp:253`) uses a constant colour, so it checks coverage but not how varying attributes are extrapolated. Minor points: - The `for (std::size_t vi ...` loop (`rect_list_geometry.h:291`) has a content conflict with #1970, which is still open, although the description says "orthogonal to #1970". The conflict is trivial: `emitted.reserve` against the indexed loop. - The header of `d3d12_rect_list_corner_test.cpp:17` announces a case 7 "tile 160x90" that is not in the list of cases. AI-assisted (Claude Code, Claude Opus 5.5)
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/2478/head:github-bot/gh-2478'
git switch 'github-bot/gh-2478'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2478'
git switch 'github-bot/gh-2478'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2478'
git switch 'github-bot/gh-2478'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2478'
git switch 'main'
git merge --squash 'github-bot/gh-2478'
git switch 'main'
git merge --ff-only 'github-bot/gh-2478'
git switch 'main'
git merge 'github-bot/gh-2478'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.