Skip to content

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

Open
lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-rect-list-corner
Open

lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-rect-list-corner

Conversation

@lixonik

@lixonik lixonik commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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

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. Bit-exact non-drifting fourth corner: 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) while keeping the documented clip-space other_a + other_b - corner rule uniformly for Position and all interpolants (docs/d3d12_vulkan.md).
  3. Orthogonal to runtime: preamble-only flat scan, safe type lookups, and portable CPU test for rect_list_geometry #1970: Leaves the preamble SPIR-V parser and the for (const Corners& c : corners) loop header intact (git merge-tree with runtime: preamble-only flat scan, safe type lookups, and portable CPU test for rect_list_geometry #1970 is conflict-free).

Testing

  • Negative control on unmodified main (ec91d695c): d3d12_rect_list_corner_test fails on 7 corner-rule cases (w=1e20 corner v2: 2080/4096 off plane; differing w=(1.875,1,1) corner v0 & w=(1,1,1.875) corner v2: 2080/4096 missed each; thin strip 5000:1 corner v2 & v1: 2048/4096 missed each; flat non-dyadic exact: 4096/4096 pixels drifted; differing w=(1.875,1,1) flat color: 2080/4096 missed).
  • With this PR (220aa322e):
    • ctest -R "^(d3d12_rect_list_corner|indirect_topology_and_stats)$": 2/2 passed (d3d12_rect_list_corner_test: all 17 cases passed, 0.27 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).

…ts (RT-RECT-LIST-CORNER)

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.
clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 27, 2026
…m + idle upload trim

- Port RT-BINDLESS-DIMENSION-LIMITS (ubiquitarianism/leonida#2406, GPL-3.0,
  author aslan2142): grow 2D, 2D-array, 3D, and 1D sampled bindless arrays
  alike to max_images (65,536, growing 2D-array/3D/1D together under device
  limits), add BindlessSet::image_refusal() + per-dimension refusal counters
  and richer full-array diagnostics in d3d12_vulkan_device.cpp, and add
  vk_bindless_dimensions_test.
- Port RT-RECT-LIST-CORNER (ubiquitarianism/leonida#2478, GPL-3.0, author
  lixonik): post-divide NDC signed edge dot-product corner selection,
  bit-exact fourth-corner reconstruction with perspective 1/w extrapolation
  for differing w, v1-leading triangle emission, and d3d12_rect_list_corner_test.
- Reclaim 640+ MiB of idle sub-arena VRAM on <=10.5 GiB GPUs in
  d3d12_vulkan_device.cpp: default gpu_devlocal_arena to 0 MiB when KHR/hybrid
  RT is inactive (!state_.info.ray_tracing, saving 384 MiB), and cap auto
  rebar_devlocal_arena at 128 MiB on <=10.5 GiB GPUs (saving 256+ MiB);
  explicit LEONIDA_DEVLOCAL_BUFFER_ARENA_MB / LEONIDA_DEVLOCAL_ARENA_MB still
  override.
- Port RT-TITLE-UPLOAD-TRIM (ubiquitarianism/leonida#2463, GPL-3.0, author
  kawaii-shakal): add runtime/src/title_upload_trim.h and title_upload_trim_test,
  and wire non-blocking try_lock idle trimming (>4 MiB buffers after 2000 ms)
  into DeviceState's staging pool via LEONIDA_TITLE_UPLOAD_IDLE_MS /
  LEONIDA_TITLE_UPLOAD_KEEP_MB.

Verified on RTX 3080 with LEONIDA_VK_VALIDATION=1 (10/10 CTests pass, 0
validation errors, 5/5 test_docs_* suites pass, 0 game-data violations).
@djrolfes

Copy link
Copy Markdown
Contributor

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.

@MoonEyes

Copy link
Copy Markdown
Collaborator

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 Coordinating who works on what (continued from #14) #1608 (comment)).
  • 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

Copy link
Copy Markdown
Collaborator

Review of head 4283e3f (read against main 43a9d56, which contains ec91d69). 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:

AI-assisted (Claude Code, Claude Opus 5.5)

…ry (RT-RECT-LIST-CORNER)

Addresses review feedback on ProjectLeonida#2478:
- Drop the unmeasured NDC + 1/w differing-w fourth-corner extrapolation and w4=1 fallback, keeping the documented clip-space other_a + other_b - corner rule (with bit-exact component selection) uniformly for Position and all attributes.
- Restore the original range-based corners loop so rect_list_geometry.h merges cleanly with ProjectLeonida#1970.
- Update d3d12_rect_list_corner_test cases and header comment to match.
@lixonik

lixonik commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@MoonEyes Thanks for the thorough review and testing! Addressed all four points in 220aa322e:

  1. Dropped the unmeasured NDC + 1/w fourth-corner path (and its 1/w4 <= 0 -> w4 = 1 fallback): rect_list_geometry() now applies exact_fourth(other_a, other_b, corner) uniformly to both Position and every interpolant, matching docs/d3d12_vulkan.md (v2 + v3 - v1) with no mixed position/attribute model and a much smaller SPIR-V body. Updated docs/d3d12_vulkan.md to note that differing-w fourth-corner behavior beyond v2 + v3 - v1 is unmeasured on console hardware.
  2. Conflict-free with runtime: preamble-only flat scan, safe type lookups, and portable CPU test for rect_list_geometry #1970: Restored for (const Corners& c : corners) verbatim; verified git merge-tree against runtime: preamble-only flat scan, safe type lookups, and portable CPU test for rect_list_geometry #1970 exits 0 with no conflicts.
  3. Updated d3d12_rect_list_corner_test.cpp: Removed the stale case-7 tile note from the header comment and updated the differing-w cases to w = (1.875, 1, 1) (corner v0) and w = (1, 1, 1.875) (corner v2), where pre-divide clip-space lengths on main pick the wrong corner while post-divide NDC picks the right corner and clip-space other_a + other_b - corner produces positive w4 = 0.125.

Re-tested on RTX 5070 (Windows 11, driver 610.88, Vulkan 1.4.341, VK_LAYER_KHRONOS_validation not installed locally):

  • Negative control on unmodified main (rect_list_geometry.h): 7/17 cases fail (w=1e20, both differing-w solid cases, both 5000:1 thin strips, flat non-dyadic exact, and differing-w flat color).
  • With 220aa322e: d3d12_rect_list_corner (17/17 cases, 0.27 s) and indirect_topology_and_stats (0.03 s) both pass; test_ctest_registration.py (6/6) and check_no_game_data.py clean.

AI-assisted (Antigravity, Gemini).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants