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

Open
opened 2026-11-26 21:19:41 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1993
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-title-buffer-windows-bounds-and-index


📝 Commits (1)

  • 70aa27f d3d12: fix TitleBufferWindows cap/bias guards and add O(log B) lookup and tail tracking

📊 Changes

3 files changed (+315 additions, -25 deletions)

View changed files

📝 runtime/CMakeLists.txt (+4 -0)
📝 runtime/shims/d3d12_title_buffer_windows.h (+71 -25)
➕ runtime/test/title_buffer_windows_test.cpp (+240 -0)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1993 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-title-buffer-windows-bounds-and-index` --- ### 📝 Commits (1) - [`70aa27f`](https://github.com/ubiquitarianism/leonida/commit/70aa27ffd93e730b4f38f4ffa17ba303cc85b503) d3d12: fix TitleBufferWindows cap/bias guards and add O(log B) lookup and tail tracking ### 📊 Changes **3 files changed** (+315 additions, -25 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+4 -0) 📝 `runtime/shims/d3d12_title_buffer_windows.h` (+71 -25) ➕ `runtime/test/title_buffer_windows_test.cpp` (+240 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1999
No description provided.