Skip to content

runtime: preamble-only flat scan, safe type lookups, and portable CPU test for rect_list_geometry - #1970

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-rect-list-geometry-preamble-and-tests
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-rect-list-geometry-preamble-and-tests

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

Claim: RT-RECT-LIST-GEOMETRY-PREAMBLE-AND-TESTS (claim comment #7168816576 on #1608).

Fixes & Optimizations (runtime/src/rect_list_geometry.h)

  1. Stop scanning the vertex shader at OpFunction (preamble-only): OpType*, OpDecorate, and global Output OpVariable declarations all precede the first OpFunction (SPIR-V logical layout §2.4), so scanning tens of thousands of body instructions during graphics PSO creation was wasted work.
  2. Overflow-safe framing check: replaced i + n > vertex_words.size() with n > vertex_words.size() - i.
  3. Eliminate 5 std::map/std::set RB-trees and silent operator[] lookups:
    • Replaced types, variable_type, location, builtin, and outputs with 4 flat sorted vectors (stable_sort + upper_bound preserving exact last-wins semantics) and explicit find_type() / find_kv() checks instead of types[...] silent default-insertion on missing IDs.
  4. Remove dead head vector and pre-reserve output buffers: pre-reserves annotations, globals, body, vars, io_vars, corners, emitted, and out, and emits OpEntryPoint directly into out without a temporary entry vector.
  5. Add portable CPU-only CTest rect_list_geometry (runtime/test/rect_list_geometry_test.cpp): previously only the GPU test d3d12_rect_list_test.cpp exercised rect_list_geometry.h.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Added runtime/test/rect_list_geometry_test.cpp (8 test groups verifying generated SPIR-V header/bound/capability/memory-model/entry-point/all-4-execution-modes/decorations/6-EmitVertex/2-EndPrimitive, position-only shaders, PointSize allowed, unsorted/duplicate Locations, large body skipped identically, non-SPIR-V/short/zero-len/truncated refusal, missing Position refusal, ClipDistance/Layer refusal, vec3/uvec4/dangling-type-id refusal).
  • Ran rect_list_geometry_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Bit-exact A/B benchmark at -O2 vs main (best of 5 runs, Apple Silicon):
small_vs_2loc_200w     (   199 words): base=   1.60 us  new=  0.93 us  speedup= 1.73x
med_vs_8loc_8kw        (  8097 words): base=   5.23 us  new=  1.71 us  speedup= 3.07x
large_vs_16loc_50kw    ( 50161 words): base=  17.20 us  new=  2.76 us  speedup= 6.24x
  • Ran python3 -I scripts/check_no_game_data.py on all 3 changed files and python3 -I tests/test_text_write_line_endings.py: all passed.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

lixonik added a commit to lixonik/leonida that referenced this pull request Nov 27, 2026
…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.
@RenoThaGutta

Copy link
Copy Markdown
Contributor

Independent RTX 4080 validation of 57b853a46ed1dc614cbaf7d03afaa8c84bc6c9d6 on Windows 11 Pro 25H2, NVIDIA 621.01, x64 MSVC 19.51.36244 (RelWithDebInfo). Inputs were generated from handwritten synthetic fixtures. No GTA VI files were used.

MEASURED on baseline 02f71ccfe78160997a02950607be583af87e1180 with and without the three PR changes:

  • The original d3d12_rect_list test passed on both builds under Khronos Vulkan validation. The patched build passed rect_list_geometry (1/1).
  • Identical capture-only instrumentation recorded eight 64 × 64 RGBA32F frames per run. All 32,768 pixels matched bit for bit in each paired run: default lazy creation, eager creation, and default with Vulkan validation. That is zero changed pixels across 98,304 comparisons.
  • Four 10-minute lifecycle runs with validation enabled completed 466 create/render/destroy cycles. Lazy completed 116 on each build and eager completed 117 on each build. Each run matched its own first readback throughout and logged zero validation errors.
  • Repeated the core comparison on newer main ef8fbf09f3ba00460e1c08102b096b76702a70b6. All eight frames again matched bit for bit. The two synthetic fallback-pipeline tests passed on each build.

Independent evidence review completed. AI-assisted: OpenAI Codex.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 57b853a46 merged locally with main c1ecf04ed for the rectangle-list package in #2464. The flat lookups preserve the previous last-wins behavior, and the generated geometry module was byte-identical to main across 60 synthetic cases, varying locations, PointSize, and the function body length. The new rect_list_geometry_test passes against both implementations, consistent with its role as a behavior-preservation test.

CPU microbenchmark of the generator on Ubuntu 22.04 under WSL2, GCC 11.4 -O2, four locations, median of seven alternating A/B batches of 2,500 calls each. Each call also checked its output against main. A repeat produced the same pattern.

Synthetic function body Main Main + PR
8 OpNops 3.07 us/call 1.93 us/call
16,384 OpNops 31.56 us/call 1.91 us/call
65,536 OpNops 119.19 us/call 1.94 us/call

This measures CPU-side geometry shader generation only. The same changed files pass check_no_game_data.py and git diff --check. The heads of #1970 and #2478 also merge cleanly in git merge-tree.

AI-assisted.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 29, 2026
@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR

  • Base: main aa918e5d9 + PR head 57b853a46
  • Test: ctest -R ^rect_list_geometry$
  • Red on main: passes (the new test covers behaviour already on main)
  • Green with the PR: passes; package builds, full ctest shows no new failure (4 d3d12 GPU tests timed out once under load and pass on a serial rerun)
  • Verdict: NO RED (optimization; the new test also passes on main). Since the test, main moved to c8bf0e7b6 and the PR now conflicts with it in runtime/CMakeLists.txt
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

Note: shares runtime/CMakeLists.txt with the open #2566; a rebase may be needed if #2566 lands first.

AI-assisted (Claude Code, Claude Opus 5.5).

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants