Skip to content

runtime: add O(1) envelope fast path and explicit wrap to title_texture_refresh (RT-TITLE-TEXTURE-REFRESH-ENVELOPE-AND-WRAP) - #2012

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-title-texture-refresh-envelope
Nov 27, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-title-texture-refresh-envelope

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

Claim: RT-TITLE-TEXTURE-REFRESH-ENVELOPE-AND-WRAP (claim comment #7170422103 on #1608).

Problem

In runtime/src/title_texture_refresh.h:

  1. title_write_overlaps() always executed std::upper_bound before checking begin >= end, writes.empty(), or whether [begin, end) falls outside the merged dirty envelope [writes.front().first, writes.back().second) — paying binary search overhead for thousands of unaffected waiting textures on every refresh pass.
  2. merge_title_writes() unconditionally invoked std::sort even on empty/single-entry or already-sorted write-range lists.
  3. poll_title_textures() incremented cursor = address + 1, relying on implicit unsigned wrap if address == UINT64_MAX.

Fix

  • Short-circuit title_write_overlaps() on empty/inverted queries, outside-envelope ranges (end <= writes.front().first || begin >= writes.back().second), and single-range merges before falling back to std::upper_bound.
  • Short-circuit merge_title_writes() on 0/1-element inputs and skip std::sort when already ordered.
  • Make poll_title_textures() cursor wrap at UINT64_MAX explicit (address == UINT64_MAX ? 0u : address + 1u).
  • Add portable CPU-only CTest title_texture_refresh (runtime/test/title_texture_refresh_test.cpp).

Benchmark (clang++ -std=c++20 -O2, bit-exact A/B, 20,000 waiting textures x 2,000 passes)

  • 64 localized dirty ranges: 154.67 ms -> 18.72 ms (8.26x faster)
  • 1 dirty block (64 KiB): 43.08 ms -> 21.48 ms (2.01x faster)
  • merge_title_writes() on 256 already-sorted ranges (50,000 calls): 17.72 ms -> 12.48 ms (1.42x faster)

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran runtime/test/title_texture_refresh_test.cpp 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
  • Ran python3 -I scripts/check_no_game_data.py runtime/src/title_texture_refresh.h runtime/test/title_texture_refresh_test.cpp runtime/CMakeLists.txt 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 18:06

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.

@Tyrannys

Copy link
Copy Markdown
Contributor

Profile M data point with #1705 and #2012 applied together (RTX 4070 SUPER 12 GB, NVIDIA 621.01, Windows 11). Counts only. AI-assisted (Claude Code).

The textures submit phase (Device::refresh_title_textures) after 900 s, means per submission:

Not a one-variable A/B: G1d also changed LEONIDA_KEYBOARD_PAD (always -> 1), LEONIDA_CAPTURE_LIMIT (40 -> 80) and ran on caches one run warmer, and the two pull requests are not separated. One run each, PROVISIONAL; refuted if the same tree without the two pull requests also shows tens of ms. Not run: #1705's new case in d3d12_existing_heap_test, because that test file does not compile in this local stack for an unrelated earlier merge; title_texture_refresh_test (#2012) passes.

RUN c3-G1d | 2026-11-27 | Tyrannys | AI-assisted: Claude Code
BASE:        2859d0c9b + #2233 #2275 #2418 #2440 #2020 #2166 #2432 #2224 #2179 #2228 #1851 #1882 #2260 #2267 #1592 (+ local test fix) + #443 + #1705 + #2012 | shaders: library re-translated with w64-one-lane-fold; DirectStorage predictor profile regenerated after 2a491668a
MACHINE:     RTX 4070 SUPER 12 GB, desktop | NVIDIA 621.01 | Windows 11 | 64 GB
RECIPE:      OreoFR's world recipe (#1608, 2026-11-27 08:23) + DEVIATIONS: COMPILE_GATE=0 PIPELINE_PREWARM_ORDER=1 SHADOW_ALIAS_MS=1000 SKIP_COMPUTE=<1,009 ray-hit programs> RT_AS=off DEVICE_FAULT=1 TITLE_HUD=1 MEM_PROBE=60 FRAME_PACING=1 PRESENT_NO_TEAR=1 KEYBOARD_PAD=1 CAPTURE_LIMIT=80 | precompile: yes, library programs except the skip list
OS/DRIVER:   TdrDelay=60
TARGET:      textures phase ms per submission after 900 s; how far | PREDICTED: textures at least halved vs c3-G1c (474.6); < 1 frame/s; autosave prompt or gameplay
MEASURED:    full 1,804 s, PresentX 3,191 ok / 0 failed, 11.0 M D3D12 calls, 0 decompression errors, 0 device stalls, 0 presses besides the script (235)
             world-start pipeline burst ~270 s; letterboxed near-white cutscene from ~600 s; by ~1,200 s the game's own control hint
             ("move around / move the camera") over a screen that is almost all white (mean RGB 252-254) to the end
             after 900 s (means): textures 24.0 ms per submission (c3-G1c 474.6), heaps 12.8, shadows 19.7; 7.7 submissions/s; 0.30 frames/s
             process dedicated / shared (MiB, means): burst+0..180 s 10,777 / 1,446; +180..240 s 10,695 / 1,696; +240 s..end 10,759 / 2,345
             (shared max 7,107 after a +5,120 MiB rise at ~1,664 s); HUD VRAM 10.0-13.6 GB used vs a 10.7 GB budget
             3D engine mean: 8.4% (0..300 s), 9.8%, 59.9%, 29.8%; compute 0.0%, copy 0.0-1.0%
             skip list: 250 of the 1,009 skipped programs were requested, each first by ~600 s, none first requested during gameplay;
             their dispatches are dropped. Loop-guard hits: not observable (the translator's guard exits silently, no counter)
END:         time limit
CACHES:      warm (journal + session/heavy caches after G1b, G1b-ctl, G1c) | RUN 1 of 1
INSTRUMENTS: TITLE_HUD, MEM_PROBE=60, DEVICE_FAULT=1, Windows GPU performance counters every 10 s, gputaskman --json every 30 s
VERDICT:     prediction held (textures down ~95%; 0.30 frames/s; gameplay control reached, picture white)

@aslan2142
aslan2142 merged commit bab6356 into ProjectLeonida:main Nov 27, 2026
5 of 12 checks passed
aslan2142 added a commit that referenced this pull request Nov 27, 2026
d3d12: merge bundle of eight open runtime PRs, conflicts fixed (#2020 #2166 #1851 #2465 #1705 #2117 #2012 #407)
DadaCap added a commit to DadaCap/leonida that referenced this pull request Nov 27, 2026
Conflicts, main's code kept and the branch's change applied on top:
- d3d12_title_image_upload.h, write(): takes both the branch's `busy` and
  ProjectLeonida#2012's `from` (partial mips); the range starts at `from` and the copy
  still gets the queue and the earlier queues' progress to wait for on the GPU.
- d3d12_vulkan_device.cpp, upload_staged_title() and its two callers
  (fill_unlocked, fill_title_texture): pass `busy` and `from`.
- fill_title_texture(): main's comment and its span = fill_bytes(t) after
  the readability check; the branch's upload_room() helper and its no-slot
  put-off kept, without the branch's old span = layout.size line.
- title texture log: the branch's "put off for an upload slot" count and
  main's partial-mips line both kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwSKP7weM2P7sk5skto
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.

4 participants