d3d12: title windows reuse earlier blocks; destroyed blocks' reader slots are reused (RT-TITLE-BUFFER-WINDOWS-REUSE) #2518

Closed
leonida/mandy
Tyrannys wants to merge 0 commits from rt-title-buffer-windows-reuse into main

Pull request closed

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

Tyrannys commented 2026-11-28 00:12:04 +00:00

Package RT-TITLE-BUFFER-WINDOWS-REUSE: item 3 of @kevincommits' finding on #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230381887). Thank you for the find and the repro. Claim: https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230633968.

What changes (runtime/shims/d3d12_title_buffer_windows.h)

  1. Earlier blocks for every kind of request. allocate now looks in earlier blocks whether or not the request is device_local. Before, the device's title windows (external_windows.allocate(*this, size, offset, mapped), so device_local is false) only tried the last block. Once a second block existed, they made a new block instead of using:

    • the empty 4 MiB spare that free keeps, when it is not the last block;
    • a freed hole in an earlier block.

    This is safe because each instance gets only one kind of request. GeometryViews always asks for device_local; the device's external windows never do. So an earlier block was always made for the same kind of request.

  2. Reader slots are reused. published_ slots were only appended. After 4,096 blocks made over the pool's lifetime, a new block was live but missing from for_each_block (the console heap pass's backed table) and from find_mapped. Now a new block takes the first slot a destroyed block left. Since a slot can now be rewritten while a lock-free reader reads it:

    • each slot is a seqlock, with a version that is odd while the owner writes and atomic fields;
    • for_each_block and find_mapped drop a slot whose version changed during the read, so they never pair one block's address with another block's capacity, mapping or buffer;
    • destroy_block and release empty a slot through the same write.

    Only live blocks are now limited to 4,096 (16 GiB of 4 MiB blocks). If that limit is reached, it is logged once.

Tests

New title_buffer_windows_reuse_test (CPU only, Windows) runs on the same kind of fake device as geometry_views_test:

  • Your repro: 3 MiB in block 0, 3 MiB in block 1, free block 0 (it becomes the spare), then allocate 2 MiB with device_local = false. It reuses block 0, and no third block is made.
  • A freed 2 MiB hole at the start of a live block 0 is reused when the last block has no room.
  • 4,100 blocks are made and destroyed one after another (a 1 MiB cap makes free destroy each block). The next block is still seen by for_each_block and find_mapped.

On main, 8 of the 15 checks fail. With the fix, all 15 pass. ctest -R "geometry_views|title_buffer|title_window" passes 12/12. gta6rt_d3d12_com builds.

Profile M: RTX 4070 SUPER, Windows 11, Ryzen 7 9800X3D, MSVC RelWithDebInfo. The full suite was not run because a boot was running. git diff --check and scripts/check_no_game_data.py are clean.

#1993 also changes the earlier-block scan. It is based on an older version of this file (it already conflicts with main) and does neither fix here. Whichever PR lands second will need a small rebase there. No game files used.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

Package **RT-TITLE-BUFFER-WINDOWS-REUSE**: item 3 of @kevincommits' finding on #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230381887). Thank you for the find and the repro. Claim: https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230633968. ## What changes (`runtime/shims/d3d12_title_buffer_windows.h`) 1. **Earlier blocks for every kind of request.** `allocate` now looks in earlier blocks whether or not the request is `device_local`. Before, the device's title windows (`external_windows.allocate(*this, size, offset, mapped)`, so `device_local` is false) only tried the last block. Once a second block existed, they made a new block instead of using: - the empty 4 MiB spare that `free` keeps, when it is not the last block; - a freed hole in an earlier block. This is safe because each instance gets only one kind of request. GeometryViews always asks for `device_local`; the device's external windows never do. So an earlier block was always made for the same kind of request. 2. **Reader slots are reused.** `published_` slots were only appended. After 4,096 blocks made over the pool's lifetime, a new block was live but missing from `for_each_block` (the console heap pass's backed table) and from `find_mapped`. Now a new block takes the first slot a destroyed block left. Since a slot can now be rewritten while a lock-free reader reads it: - each slot is a seqlock, with a version that is odd while the owner writes and atomic fields; - `for_each_block` and `find_mapped` drop a slot whose version changed during the read, so they never pair one block's address with another block's capacity, mapping or buffer; - `destroy_block` and `release` empty a slot through the same write. Only *live* blocks are now limited to 4,096 (16 GiB of 4 MiB blocks). If that limit is reached, it is logged once. ## Tests New `title_buffer_windows_reuse_test` (CPU only, Windows) runs on the same kind of fake device as `geometry_views_test`: - Your repro: 3 MiB in block 0, 3 MiB in block 1, free block 0 (it becomes the spare), then allocate 2 MiB with `device_local = false`. It reuses block 0, and no third block is made. - A freed 2 MiB hole at the start of a live block 0 is reused when the last block has no room. - 4,100 blocks are made and destroyed one after another (a 1 MiB cap makes `free` destroy each block). The next block is still seen by `for_each_block` and `find_mapped`. On main, 8 of the 15 checks fail. With the fix, all 15 pass. `ctest -R "geometry_views|title_buffer|title_window"` passes 12/12. `gta6rt_d3d12_com` builds. Profile M: RTX 4070 SUPER, Windows 11, Ryzen 7 9800X3D, MSVC RelWithDebInfo. The full suite was not run because a boot was running. `git diff --check` and `scripts/check_no_game_data.py` are clean. #1993 also changes the earlier-block scan. It is based on an older version of this file (it already conflicts with main) and does neither fix here. Whichever PR lands second will need a small rebase there. No game files used. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
timbot103 commented 2026-11-28 00:53:16 +00:00

Independently checked head f2b6d4a1d on Windows 11 Pro 26200, MSVC 2022 (19.44.35230.0, RelWithDebInfo, Ninja), no game files.

Local build & test verification:

  • Clean MSVC build of title_buffer_windows_reuse_test with zero warnings in the PR-modified files.
  • title_buffer_windows_reuse_test: PASS (15 checks, 0 failures).

Code review notes:

  • The seqlock in write_slot / read_slot is memory-model sound (single writer under the owner's lock: odd version → release fence → relaxed stores → even version release; reader: acquire load rejecting odd → relaxed loads → acquire fence → version recheck), eliminating torn block snapshots. Reusing empty (address == 0) slots in publish() cleanly turns the 4,096 cap into a live-block limit with no new lifetime window.
  • Two optional follow-ups:
    1. Hardening: allocate() now scans earlier blocks for every request (replacing the device_local guard), and take() relies on the comment invariant that one instance never mixes block kinds (16 B device-local vs 64 KiB host-visible alignment/placement). Recording device_local/alignment per Block (and skipping mismatches or asserting homogeneity) would enforce that invariant in code.
    2. Test coverage: adding an interleaved reuse case (live blocks A, B, C → destroy B → allocate D reusing B's slot → verify for_each_block sees A/C/D and find_mapped finds D but not B's old mapping) would cover non-trivial slot recycling alongside the 4,100-cycle single-live-block loop.

Overall looks clean and solid. AI-assisted.

Independently checked head `f2b6d4a1d` on Windows 11 Pro 26200, MSVC 2022 (`19.44.35230.0`, RelWithDebInfo, Ninja), no game files. **Local build & test verification:** - Clean MSVC build of `title_buffer_windows_reuse_test` with zero warnings in the PR-modified files. - `title_buffer_windows_reuse_test`: **PASS** (`15 checks, 0 failures`). **Code review notes:** - The seqlock in `write_slot` / `read_slot` is memory-model sound (single writer under the owner's lock: odd version → release fence → relaxed stores → even version release; reader: acquire load rejecting odd → relaxed loads → acquire fence → version recheck), eliminating torn block snapshots. Reusing empty (`address == 0`) slots in `publish()` cleanly turns the 4,096 cap into a live-block limit with no new lifetime window. - **Two optional follow-ups:** 1. *Hardening:* `allocate()` now scans earlier blocks for every request (replacing the `device_local` guard), and `take()` relies on the comment invariant that one instance never mixes block kinds (16 B device-local vs 64 KiB host-visible alignment/placement). Recording `device_local`/alignment per `Block` (and skipping mismatches or asserting homogeneity) would enforce that invariant in code. 2. *Test coverage:* adding an interleaved reuse case (live blocks A, B, C → destroy B → allocate D reusing B's slot → verify `for_each_block` sees A/C/D and `find_mapped` finds D but not B's old mapping) would cover non-trivial slot recycling alongside the 4,100-cycle single-live-block loop. Overall looks clean and solid. AI-assisted.
lixonik approved these changes 2026-11-28 09:28:06 +00:00
lixonik left a comment

Reviewed the diff and tested merged onto main (493867366) with Ninja Release (MSVC).

Reusing earlier blocks regardless of device_local and turning published_ slots into seqlocks (version odd during writes with release/acquire fences and version recheck on read) so destroyed blocks' slots are recycled cleanly fixes both block bloat and the 4,096 lifetime cap.

Tests run

  • cmake --build <build> --target title_buffer_windows_reuse_test geometry_views_test gta6rt_d3d12_com: built cleanly.
  • ctest --output-on-failure -R "^(title_buffer_windows_reuse|geometry_views)$": 2/2 passed (geometry_views 0.10 s, title_buffer_windows_reuse 0.04 s).

AI-assisted (Antigravity, Gemini).

Reviewed the diff and tested merged onto `main` (`493867366`) with Ninja Release (MSVC). Reusing earlier blocks regardless of `device_local` and turning `published_` slots into seqlocks (`version` odd during writes with release/acquire fences and version recheck on read) so destroyed blocks' slots are recycled cleanly fixes both block bloat and the 4,096 lifetime cap. ### Tests run - `cmake --build <build> --target title_buffer_windows_reuse_test geometry_views_test gta6rt_d3d12_com`: built cleanly. - `ctest --output-on-failure -R "^(title_buffer_windows_reuse|geometry_views)$"`: **2/2 passed** (`geometry_views` 0.10 s, `title_buffer_windows_reuse` 0.04 s). AI-assisted (Antigravity, Gemini).
zmasi commented 2026-11-28 09:52:14 +00:00

#2518 | head f2b6d4a1dafcabd261af217a06b4040383184f5e (merged cleanly with main 3ca0707ddec7fd868ecd6df924d751901102fd9c) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(title_buffer_windows_reuse|geometry_views|d3d12_geometry_views)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: title_buffer_windows_reuse_test fails (15 checks, 8 failures: non-device_local title windows skipped spare/earlier blocks, and blocks created after 4,096 destroyed blocks missed for_each_block and find_mapped) | green on PR+main: 3/3 CTests pass (title_buffer_windows_reuse 15/15 in 0.08s, geometry_views 79/79 in 0.14s, and d3d12_geometry_views on RTX 3090 in 0.84s with LEONIDA_VK_VALIDATION=1 and 0 Khronos validation errors); check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2518 | head f2b6d4a1dafcabd261af217a06b4040383184f5e (merged cleanly with main 3ca0707ddec7fd868ecd6df924d751901102fd9c) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(title_buffer_windows_reuse|geometry_views|d3d12_geometry_views)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `title_buffer_windows_reuse_test` fails (`15 checks, 8 failures`: non-`device_local` title windows skipped spare/earlier blocks, and blocks created after 4,096 destroyed blocks missed `for_each_block` and `find_mapped`) | green on PR+main: `3/3` CTests pass (`title_buffer_windows_reuse` `15/15` in `0.08s`, `geometry_views` `79/79` in `0.14s`, and `d3d12_geometry_views` on RTX 3090 in `0.84s` with `LEONIDA_VK_VALIDATION=1` and `0` Khronos validation errors); `check_no_game_data.py` `0` violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes
Maticcm commented 2026-11-28 21:39:39 +00:00

Independent exact-head verification on Windows 11, AMD Radeon(TM) Graphics, MSVC RelWithDebInfo and Vulkan validation enabled. Tested f2b6d4a1dafcabd261af217a06b4040383184f5e against parent d44285197237cfe331684aa150fb9dec4b1d08b5.

  • The physical Radeon passed d3d12_geometry_views on both revisions. Read-write and write-combined paths matched all 64 words on the buffer-descriptor and root-view checks. The constant-ring checks matched 64/64 on each of the three routes. The corresponding negative controls passed, and neither log reported a validation error or VUID. CPU geometry_views passed 79/79 on both. The head's stock reuse test passed 15/15. Focused CTest totals were parent 2/2 and head 3/3.
  • To cover the interleaved scenario suggested above, the same fake-device test held A and C live, retired B while holding its former address out of reuse, then allocated and retired 4,100 replacements. On the parent, the last seven replacements were absent from both for_each_block and find_mapped, starting with replacement 4,094. On the head, both APIs saw all 4,100 replacements and both live neighbors throughout. Both revisions stopped reporting B after retirement and cleared the final published blocks on release.

The interleaved test used a single thread and synthetic buffers. AI-assisted with OpenAI Codex.

Independent exact-head verification on Windows 11, AMD Radeon(TM) Graphics, MSVC RelWithDebInfo and Vulkan validation enabled. Tested `f2b6d4a1dafcabd261af217a06b4040383184f5e` against parent `d44285197237cfe331684aa150fb9dec4b1d08b5`. - The physical Radeon passed `d3d12_geometry_views` on both revisions. Read-write and write-combined paths matched all 64 words on the buffer-descriptor and root-view checks. The constant-ring checks matched 64/64 on each of the three routes. The corresponding negative controls passed, and neither log reported a validation error or VUID. CPU `geometry_views` passed 79/79 on both. The head's stock reuse test passed 15/15. Focused CTest totals were parent 2/2 and head 3/3. - To cover the interleaved scenario suggested above, the same fake-device test held A and C live, retired B while holding its former address out of reuse, then allocated and retired 4,100 replacements. On the parent, the last seven replacements were absent from both `for_each_block` and `find_mapped`, starting with replacement 4,094. On the head, both APIs saw all 4,100 replacements and both live neighbors throughout. Both revisions stopped reporting B after retirement and cleared the final published blocks on release. The interleaved test used a single thread and synthetic buffers. AI-assisted with OpenAI Codex.
timbot103 reviewed 2026-11-29 01:07:07 +00:00
timbot103 left a comment
No description provided.
Static audit of `f2b6d4a1d` only. I ran no builds, tests, or GPU workloads. - Please account for pending GPU shadow copies before enabling earlier-block reuse in `runtime/shims/d3d12_title_buffer_windows.h:227`. With `LEONIDA_SHADOW_GPU=1` and `LEONIDA_SHADOW_KEEP=0`, refresh can queue a copy to an already-retired external window just before the same submission reaps it (`runtime/shims/d3d12_vulkan_device.cpp:15231`). If the earlier block still has a live window and the last block is full, another allocator call can reuse the freed hole before the prelude records the queued copy (`runtime/src/console_heap_gpu.h:572`). That command then targets the new window's bytes. Please tie queued and recorded destination copies to the range's retirement before exposing the hole. - At `runtime/shims/d3d12_title_buffer_windows.h:104`, a successful 4,097th live block stays invisible to both readers even after a published block is destroyed. Please backfill the vacancy from live unpublished blocks and add a test for returning to 4,096 live blocks. AI-assisted.
TheGeogeo commented 2026-11-29 05:58:28 +00:00

Tested f2b6d4a1d merged (no commit) with main c8bf0e7b6. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), Ninja, RelWithDebInfo. check_no_game_data.py is clean.

  • Green: title_buffer_windows_reuse passes.
  • Red: with runtime/shims/d3d12_title_buffer_windows.h back to main, it fails 8 of its 15 checks, for example it reuses the spare block 0, no third block is made and a 2 MiB window takes the freed hole.
  • No regression: the merge rebuilds 190 executables. Of their 311 other tests, only xbox_present fails, as on plain main here.
  • LINK: gta6rt.exe linked without ws2_32.lib gives main's 26 unresolved Rust-std symbols, nothing new. Its imports are identical to main's.

Still open from review (static reading, not tested here):

  • d3d12_title_buffer_windows.h:227: reusing an earlier block while GPU shadow copies are still pending (LEONIDA_SHADOW_GPU=1, LEONIDA_SHADOW_KEEP=0) could let a queued copy target a hole that another allocation reused.
  • :104: a 4,097th live block stays invisible after a published one is destroyed. A backfill and a test were asked.

AI-assisted (Claude Code).

Tested `f2b6d4a1d` merged (no commit) with `main` `c8bf0e7b6`. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), Ninja, RelWithDebInfo. `check_no_game_data.py` is clean. - Green: `title_buffer_windows_reuse` passes. - Red: with `runtime/shims/d3d12_title_buffer_windows.h` back to `main`, it fails 8 of its 15 checks, for example `it reuses the spare block 0, no third block is made` and `a 2 MiB window takes the freed hole`. - No regression: the merge rebuilds 190 executables. Of their 311 other tests, only `xbox_present` fails, as on plain `main` here. - LINK: `gta6rt.exe` linked without `ws2_32.lib` gives `main`'s 26 unresolved Rust-std symbols, nothing new. Its imports are identical to `main`'s. Still open from review (static reading, not tested here): - `d3d12_title_buffer_windows.h:227`: reusing an earlier block while GPU shadow copies are still pending (`LEONIDA_SHADOW_GPU=1`, `LEONIDA_SHADOW_KEEP=0`) could let a queued copy target a hole that another allocation reused. - `:104`: a 4,097th live block stays invisible after a published one is destroyed. A backfill and a test were asked. AI-assisted (Claude Code).
fiestorra commented 2026-11-30 06:02:08 +00:00

Independent verification on this machine (Windows 11, MSVC 19.44.35226), and the negative control bites with five failing checks.

1. It applies. merge-tree clean; f2b6d4a1d cherry-picks onto main 0ae217ceb without a conflict. Three files, 229 insertions(+), 21 deletions(-).

2. It builds and passes, three times: title_buffer_windows_reuse_test.exe -> exit 0, 0, 0.

3. With main's d3d12_title_buffer_windows.h the test fails five checks:

-> exit 1, 1
   FAIL it reuses the spare block 0, no third block is made
   FAIL at the start of block 0
   FAIL 2 MiB more does not fit block 1's tail and goes after it in block 0
   FAIL the first 2 MiB of block 0 are freed; its other window keeps it live
   FAIL a 2 MiB window takes the freed hole

Those five are the commit's subject - reusing an earlier block instead of making a third, and a freed range staying live while another window still references it. On main the third block is made and the hole is not reused, so this is a real red->green.

What I did not test: the title's own window traffic. The test drives the bookkeeping with synthetic allocations; whether the title ever asks for the pattern that used to grow the block count is a run-level question this machine cannot stage.

Written with an AI assistant.

Independent verification on this machine (Windows 11, MSVC 19.44.35226), and **the negative control bites** with five failing checks. **1. It applies.** `merge-tree` clean; `f2b6d4a1d` cherry-picks onto `main` `0ae217ceb` without a conflict. Three files, `229 insertions(+), 21 deletions(-)`. **2. It builds and passes, three times:** `title_buffer_windows_reuse_test.exe` -> exit 0, 0, 0. **3. With `main`'s `d3d12_title_buffer_windows.h` the test fails five checks:** ``` -> exit 1, 1 FAIL it reuses the spare block 0, no third block is made FAIL at the start of block 0 FAIL 2 MiB more does not fit block 1's tail and goes after it in block 0 FAIL the first 2 MiB of block 0 are freed; its other window keeps it live FAIL a 2 MiB window takes the freed hole ``` Those five are the commit's subject - **reusing an earlier block instead of making a third**, and **a freed range staying live while another window still references it**. On `main` the third block is made and the hole is not reused, so this is a real red->green. **What I did not test:** the title's own window traffic. The test drives the bookkeeping with synthetic allocations; whether the title ever asks for the pattern that used to grow the block count is a run-level question this machine cannot stage. Written with an AI assistant.
KelboLive commented 2026-11-30 17:02:51 +00:00

Independent check on Windows (not the author), at f2b6d4a1d.

  • Built title_buffer_windows_reuse_test and ran it: 15 checks, 0 failures — including one more live block, for_each_block/find_mapped seeing it, and release emptying the published blocks.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711.

AI-assisted: yes (opencode).

Independent check on Windows (not the author), at `f2b6d4a1d`. - Built `title_buffer_windows_reuse_test` and ran it: **15 checks, 0 failures** — including one more live block, `for_each_block`/`find_mapped` seeing it, and release emptying the published blocks. Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711. AI-assisted: yes (opencode).
aslan2142 commented 2026-12-01 16:45:46 +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.