d3d12: fix TitleBufferWindows cap/bias guards and add O(log B) lookup and tail tracking (RT-TITLE-BUFFER-WINDOWS-BOUNDS-AND-INDEX) #1993

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

@⁠IlyassRS3 on GitHub · 2026-11-25 17:38 UTC · original
IlyassRS3:perf/rt-title-buffer-windows-bounds-and-index → main · +315 −25 · 3 files · 1 commits · open

Item ID: RT-TITLE-BUFFER-WINDOWS-BOUNDS-AND-INDEX (claimed in #1608)

Summary

Hardens and optimizes rt::d3d12::TitleBufferWindows (runtime/shims/d3d12_title_buffer_windows.h), backing both DeviceState::external_windows (slot-133 shadow mirrors) and GeometryViews::pool_ (geometry copies):

  1. Cap underflow guard: b.capacity > cap - reserved_ and req.size > cap - reserved_ previously underflowed uint64_t whenever reserved_ > cap, bypassing the memory cap. Now checks reserved_ > cap || ... before subtracting.
  2. Signed-direction BDA bias guard: requires b.address >= d.arena.address before checking b.address - d.arena.address < (1ull << 47), preventing a low block address against a high arena VA from wrapping modulo 2⁶⁴ into the positive 47-bit shader VA window.
  3. Alignment overflow & zero-byte guards: rejects bytes == 0 up front and guards b.used > UINT64_MAX - (alignment - 1) in take() before rounding up.
  4. O(log B) lookup() via sorted device-address index: maintains a sorted (address, block_index) vector updated once per 4 MiB slab allocation so lookup() uses std::upper_bound instead of scanning every slab linearly.
  5. Tracked tail reuse (open_tails_ + max_open_tail_): preserves exact most-recent-first reuse of earlier slab tails on device_local allocations while skipping full slabs and requests larger than any remaining tail in O(1), pruning exhausted slabs as they fill.
  6. Clean release() state reset and a new portable pure-CPU unit test runtime/test/title_buffer_windows_test.cpp wired into runtime/CMakeLists.txt (title_buffer_windows).

Benchmarks (-O2, 256 × 4 MiB slabs, bit-exact A/B vs upstream/main)

  • lookup() (256 slabs, 200k queries): 7.853 ms → 1.181 ms (6.65× faster)
  • At-cap allocate() (256 full slabs, 50k requests): 3.843 ms → 0.133 ms (28.88× faster)

Testing & Disclosure

  • AI-assisted: yes
  • GPUs tested: none (macOS host, CPU-only portable unit test and A/B benchmark)
  • clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/title_buffer_windows_test.cpp: PASS
  • clang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/title_buffer_windows_test.cpp: PASS
  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/title_buffer_windows_test.cpp: PASS
  • python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_title_buffer_windows.h runtime/test/title_buffer_windows_test.cpp runtime/CMakeLists.txt: PASS
  • python3 -I tests/test_text_write_line_endings.py: PASS (3/3)
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1993) `IlyassRS3:perf/rt-title-buffer-windows-bounds-and-index` → `main` · +315 −25 · 3 files · 1 commits · open Item ID: `RT-TITLE-BUFFER-WINDOWS-BOUNDS-AND-INDEX` (claimed in #1608) ## Summary Hardens and optimizes `rt::d3d12::TitleBufferWindows` (`runtime/shims/d3d12_title_buffer_windows.h`), backing both `DeviceState::external_windows` (slot-133 shadow mirrors) and `GeometryViews::pool_` (geometry copies): 1. **Cap underflow guard**: `b.capacity > cap - reserved_` and `req.size > cap - reserved_` previously underflowed `uint64_t` whenever `reserved_ > cap`, bypassing the memory cap. Now checks `reserved_ > cap || ...` before subtracting. 2. **Signed-direction BDA bias guard**: requires `b.address >= d.arena.address` before checking `b.address - d.arena.address < (1ull << 47)`, preventing a low block address against a high arena VA from wrapping modulo 2⁶⁴ into the positive 47-bit shader VA window. 3. **Alignment overflow & zero-byte guards**: rejects `bytes == 0` up front and guards `b.used > UINT64_MAX - (alignment - 1)` in `take()` before rounding up. 4. **O(log B) `lookup()` via sorted device-address index**: maintains a sorted `(address, block_index)` vector updated once per 4 MiB slab allocation so `lookup()` uses `std::upper_bound` instead of scanning every slab linearly. 5. **Tracked tail reuse (`open_tails_` + `max_open_tail_`)**: preserves exact most-recent-first reuse of earlier slab tails on `device_local` allocations while skipping full slabs and requests larger than any remaining tail in O(1), pruning exhausted slabs as they fill. 6. **Clean `release()` state reset** and a **new portable pure-CPU unit test** `runtime/test/title_buffer_windows_test.cpp` wired into `runtime/CMakeLists.txt` (`title_buffer_windows`). ## Benchmarks (`-O2`, 256 × 4 MiB slabs, bit-exact A/B vs `upstream/main`) - `lookup()` (256 slabs, 200k queries): 7.853 ms → **1.181 ms** (**6.65×** faster) - At-cap `allocate()` (256 full slabs, 50k requests): 3.843 ms → **0.133 ms** (**28.88×** faster) ## Testing & Disclosure - AI-assisted: yes - GPUs tested: none (macOS host, CPU-only portable unit test and A/B benchmark) - `clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/title_buffer_windows_test.cpp`: PASS - `clang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/title_buffer_windows_test.cpp`: PASS - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/title_buffer_windows_test.cpp`: PASS - `python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_title_buffer_windows.h runtime/test/title_buffer_windows_test.cpp runtime/CMakeLists.txt`: PASS - `python3 -I tests/test_text_write_line_endings.py`: PASS (3/3)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:09 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 17:39 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 17:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1993#pullrequestreview-6062215055) · **reviewed** 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.
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_title_buffer_windows.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1993/head:github-bot/gh-1993'
git switch 'github-bot/gh-1993'

Merge

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