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

Closed
leonida/mandy
quietfold wants to merge 0 commits from perf/rt-title-buffer-windows-bounds-and-index into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 17:38:52 +00:00

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)
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)
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 17:39:03 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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.
aslan2142 commented 2026-12-01 16:40:53 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.