console_heap_gpu: share the pass's buffers across queue families and free removed heaps' metadata (RT-CONSOLE-HEAP-GPU-SHARING-AND-RETIRE) #2760

Closed
Christopher-Sundeby wants to merge 1 commit from upstream-pr/2760 into main
Christopher-Sundeby commented 2026-11-30 16:04:43 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Christopher-Sundeby on 2026-11-30T16:04:43Z.


Claimed in the coordination issue as: RT-CONSOLE-HEAP-GPU-SHARING-AND-RETIRE (item 3 of kevincommits' finding)

What and why

Two problems in ConsoleHeapGpu (runtime/src/console_heap_gpu.h):

  • Sharing mode. The translation pass is a prelude recorded into every queue's submissions, on both the direct and the async-compute families. But make_buffer created the key table, the heaps' metadata, the range tables and the pass buffers with the default VK_SHARING_MODE_EXCLUSIVE. The constructor now takes the device's queue families (DeviceState::families, passed in setup_console_heap_gpu), and make_buffer calls share_across_queue_families, as vulkan_bindless.h, vulkan_indirect_clamp.h and vulkan_scratch.h already do. With one family, or no list (the tests), nothing changes.
  • Removed heaps' metadata never freed. remove_heap moved the heap's metadata buffer (device-local, 32 bytes a slot) to retired_, and only the destructor freed retired_. So every removed heap kept its buffer for the device's lifetime. Each pass now gets a serial when record() makes it. A retired buffer keeps the newest serial at its removal, and drain() frees it once no pass up to that serial is in flight, because each one has either completed (drained) or was never submitted (commit(false)). A pass recorded after the removal no longer names that heap.

Separate from #2454 (erase_overlapping overflow and rollback in the same file) and from Tyrannys' RT-QUEUE-FAMILY-SHARING-DEPTH24-MARKERS (depth24 converter and marker buffers). I found no conflict with #2454's hunks.

Tested

  • vk_console_heap_gpu_test has a new section. The five heaps the previous section removes are freed by the next drain(). A heap removed while a pass recorded before it is in flight stays through a drain() that does not complete that pass, and is freed by the one that does. Negative control: with the freeing disabled, the section fails at its first check (gpu.retired() == 5), because the earlier sections' heaps pile up.
  • vk_console_heap_gpu_test passes with and without LEONIDA_VK_VALIDATION=1 (0 validation errors). ctest -R "console_heap|d3d12_existing_heap|heap_gpu|title_va": 8 of 8 pass.
  • Windows 11, MSVC 14.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo. gta6rt and the tests build with no new warnings from the changed files. scripts/check_no_game_data.py is clean on the changed files.
  • Rebased onto current main (5583b6ac5). The one conflict was with the cached Vulkan procs (load_procs, vk_), which are kept: the constructor still calls load_procs(), and make_buffer calls share_across_queue_families before vk_.create_buffer. After the rebase vk_console_heap_gpu_test passes again, including the new section (RTX 5090); the ctest set and the validation run above were on the earlier base.
  • The sharing change is not exercised by a test here: the test device has one queue family, so share_across_queue_families leaves EXCLUSIVE, as it does in the other pools' tests.

GPUs tested: NVIDIA GeForce RTX 3060 Ti, driver 621.24 (32.0.16.2124), Windows 11 (before the rebase); NVIDIA GeForce RTX 5090, driver 621.40, Windows 11 (after it)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5568924457 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2760). Originally posted by [Christopher-Sundeby](https://github.com/Christopher-Sundeby) on 2026-11-30T16:04:43Z. --- Claimed in the coordination issue as: [RT-CONSOLE-HEAP-GPU-SHARING-AND-RETIRE](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225420002) (item 3 of kevincommits' [finding](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225370093)) ### What and why Two problems in `ConsoleHeapGpu` (`runtime/src/console_heap_gpu.h`): - **Sharing mode.** The translation pass is a prelude recorded into every queue's submissions, on both the direct and the async-compute families. But `make_buffer` created the key table, the heaps' metadata, the range tables and the pass buffers with the default `VK_SHARING_MODE_EXCLUSIVE`. The constructor now takes the device's queue families (`DeviceState::families`, passed in `setup_console_heap_gpu`), and `make_buffer` calls `share_across_queue_families`, as `vulkan_bindless.h`, `vulkan_indirect_clamp.h` and `vulkan_scratch.h` already do. With one family, or no list (the tests), nothing changes. - **Removed heaps' metadata never freed.** `remove_heap` moved the heap's metadata buffer (device-local, 32 bytes a slot) to `retired_`, and only the destructor freed `retired_`. So every removed heap kept its buffer for the device's lifetime. Each pass now gets a serial when `record()` makes it. A retired buffer keeps the newest serial at its removal, and `drain()` frees it once no pass up to that serial is in flight, because each one has either completed (drained) or was never submitted (`commit(false)`). A pass recorded after the removal no longer names that heap. Separate from #2454 (`erase_overlapping` overflow and rollback in the same file) and from Tyrannys' RT-QUEUE-FAMILY-SHARING-DEPTH24-MARKERS (depth24 converter and marker buffers). I found no conflict with #2454's hunks. ### Tested - `vk_console_heap_gpu_test` has a new section. The five heaps the previous section removes are freed by the next `drain()`. A heap removed while a pass recorded before it is in flight stays through a `drain()` that does not complete that pass, and is freed by the one that does. Negative control: with the freeing disabled, the section fails at its first check (`gpu.retired() == 5`), because the earlier sections' heaps pile up. - `vk_console_heap_gpu_test` passes with and without `LEONIDA_VK_VALIDATION=1` (0 validation errors). `ctest -R "console_heap|d3d12_existing_heap|heap_gpu|title_va"`: 8 of 8 pass. - Windows 11, MSVC 14.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo. `gta6rt` and the tests build with no new warnings from the changed files. `scripts/check_no_game_data.py` is clean on the changed files. - Rebased onto current `main` (`5583b6ac5`). The one conflict was with the cached Vulkan procs (`load_procs`, `vk_`), which are kept: the constructor still calls `load_procs()`, and `make_buffer` calls `share_across_queue_families` before `vk_.create_buffer`. After the rebase `vk_console_heap_gpu_test` passes again, including the new section (RTX 5090); the `ctest` set and the validation run above were on the earlier base. - The sharing change is not exercised by a test here: the test device has one queue family, so `share_across_queue_families` leaves `EXCLUSIVE`, as it does in the other pools' tests. GPUs tested: NVIDIA GeForce RTX 3060 Ti, driver 621.24 (32.0.16.2124), Windows 11 (before the rebase); NVIDIA GeForce RTX 5090, driver 621.40, Windows 11 (after it) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
OreoFR commented 2026-11-30 16:08:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T16:08:56Z.


Verified by OreoFR on macOS 27.2.1, Apple M5 (a real GPU), MoltenVK 1.4.2 (Homebrew), main a21a62af1, head fd8b66cf6.

  • MEASURED, green (main + this PR, merged; runtime configured like the macos job: Ninja, RelWithDebInfo, LEONIDA_REQUIRE_VULKAN=ON): ctest -R console_heap_gpu: Passed. The test ran on the GPU (device: Apple M5, subgroups 4..32): every "console heap pass" step printed ok (every slot; erased/entered keys with moved ranges; backed and ask tables; range and filtered rescans; queued copies; rollback; failed submission queued again; a slot queued twice translated once).
  • MEASURED, red (main + only this PR's test and its CMake lines): does not build (no member named 'retired' in 'leonida::ConsoleHeapGpu', lines 630-641). So no red failure can be shown: the test needs the new member, which shows it uses the new code, not that it catches the bug without it.
  • Not covered here: d3d12_vulkan_device.cpp in the game, and Windows/NVIDIA/AMD drivers (MEASURED with vulkaninfo: MoltenVK on the M5 exposes 4 queue families, all graphics + compute + transfer; hardware whose families differ, such as a compute-only or transfer-only family, may exercise the sharing path differently).

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7284192463 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2760#issuecomment-7284192463). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T16:08:56Z. --- Verified by OreoFR on macOS 27.2.1, Apple M5 (a real GPU), MoltenVK 1.4.2 (Homebrew), `main` `a21a62af1`, head `fd8b66cf6`. - MEASURED, green (`main` + this PR, merged; runtime configured like the `macos` job: Ninja, RelWithDebInfo, `LEONIDA_REQUIRE_VULKAN=ON`): `ctest -R console_heap_gpu`: **Passed**. The test ran on the GPU (`device: Apple M5`, subgroups 4..32): every "console heap pass" step printed `ok` (every slot; erased/entered keys with moved ranges; backed and ask tables; range and filtered rescans; queued copies; rollback; failed submission queued again; a slot queued twice translated once). - MEASURED, red (`main` + only this PR's test and its CMake lines): **does not build** (`no member named 'retired' in 'leonida::ConsoleHeapGpu'`, lines 630-641). So no red failure can be shown: the test needs the new member, which shows it uses the new code, not that it catches the bug without it. - Not covered here: `d3d12_vulkan_device.cpp` in the game, and Windows/NVIDIA/AMD drivers (MEASURED with `vulkaninfo`: MoltenVK on the M5 exposes 4 queue families, all graphics + compute + transfer; hardware whose families differ, such as a compute-only or transfer-only family, may exercise the sharing path differently). AI-assisted: Claude Code
merzleckeier commented 2026-11-30 16:29:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T16:29:18Z.


Independent check on distinct graphics and compute queue families

MEASURED. I tested fd8b66cf merged onto main 1c78b8219 (tree 0a14a294) with native Vulkan on CachyOS and Khronos validation. A test-only vkCreateBuffer wrapper inspects the sharing mode and checks the exact family indices for allocations made by ConsoleHeapGpu. I ran the stock synthetic heap fixture and enabled its device's distinct families for the second run.

Device Graphics / compute / copy families main with distinct families PR with distinct families
Radeon 610M, RADV 26.3.1 0 / 1 / 1 20 exclusive, 0 concurrent 21 concurrent with indices [0, 1], 0 exclusive
RTX 4070 SUPER, NVIDIA 620.73.04 0 / 2 / 1 20 exclusive, 0 concurrent 21 concurrent with indices [0, 2, 1], 0 exclusive

The base cases stop at the sharing assertion after running on the graphics family. In the PR cases I also recorded the pass on the dedicated compute family after the graphics pass and handed it back via binary semaphores. On both devices, all 6,000 translated slots and the 2,377 miss positions matched the CPU reference on each family. The NVIDIA copy-family index was checked in the buffer create info.

The stock retirement checks passed on both GPUs. The default single-family harness used exclusive allocations on both revisions. All eight runs reported zero Vulkan validation errors, including the two expected sharing-assertion failures. A separate agent checked the raw logs and the harness. Synthetic inputs only. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7284748298 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2760#issuecomment-7284748298). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T16:29:18Z. --- # Independent check on distinct graphics and compute queue families **MEASURED.** I tested `fd8b66cf` merged onto `main` `1c78b8219` (tree `0a14a294`) with native Vulkan on CachyOS and Khronos validation. A test-only `vkCreateBuffer` wrapper inspects the sharing mode and checks the exact family indices for allocations made by `ConsoleHeapGpu`. I ran the stock synthetic heap fixture and enabled its device's distinct families for the second run. | Device | Graphics / compute / copy families | `main` with distinct families | PR with distinct families | | --- | --- | --- | --- | | Radeon 610M, RADV 26.3.1 | 0 / 1 / 1 | 20 exclusive, 0 concurrent | 21 concurrent with indices `[0, 1]`, 0 exclusive | | RTX 4070 SUPER, NVIDIA 620.73.04 | 0 / 2 / 1 | 20 exclusive, 0 concurrent | 21 concurrent with indices `[0, 2, 1]`, 0 exclusive | The base cases stop at the sharing assertion after running on the graphics family. In the PR cases I also recorded the pass on the dedicated compute family after the graphics pass and handed it back via binary semaphores. On both devices, all 6,000 translated slots and the 2,377 miss positions matched the CPU reference on each family. The NVIDIA copy-family index was checked in the buffer create info. The stock retirement checks passed on both GPUs. The default single-family harness used exclusive allocations on both revisions. All eight runs reported zero Vulkan validation errors, including the two expected sharing-assertion failures. A separate agent checked the raw logs and the harness. Synthetic inputs only. AI-assisted with OpenAI Codex.
KelboLive commented 2026-11-30 16:30:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T16:30:36Z.


Independent check on Windows/NVIDIA (not the author), at fd8b66cf6.

  • Configured the branch's runtime and built vk_console_heap_gpu_test (MSVC 14.52.37025, Vulkan SDK 1.4.363.0).
  • Ran it on the RTX 4070 Ti SUPER: exit 0 — console heap pass, miss list overflow: ok, a removed heap's metadata is freed once its passes are done: ok, clustered words: ok, summary 9 passes, 54007 slots, 6000 updates, 95543 misses, 1 overflows, 2 failed (the counts are the test's own simulated workload; the test itself reports ok).

Environment: NVIDIA RTX 4070 Ti SUPER, driver 621.01 (Windows 32.0.16.2101), Windows 11 Pro 10.0.26200.9711.

AI-assisted: yes (opencode).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7284783347 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2760#issuecomment-7284783347). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T16:30:36Z. --- Independent check on Windows/NVIDIA (not the author), at `fd8b66cf6`. - Configured the branch's `runtime` and built `vk_console_heap_gpu_test` (MSVC 14.52.37025, Vulkan SDK 1.4.363.0). - Ran it on the RTX 4070 Ti SUPER: **exit 0** — `console heap pass, miss list overflow: ok`, `a removed heap's metadata is freed once its passes are done: ok`, `clustered words: ok`, summary `9 passes, 54007 slots, 6000 updates, 95543 misses, 1 overflows, 2 failed` (the counts are the test's own simulated workload; the test itself reports `ok`). Environment: NVIDIA RTX 4070 Ti SUPER, driver 621.01 (Windows 32.0.16.2101), Windows 11 Pro 10.0.26200.9711. AI-assisted: yes (opencode).
ammonium231 commented 2026-11-30 21:09:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ammonium231 on 2026-11-30T21:09:07Z.


RTX 3090 / 621.40, validation on, fd8b66cf6 merged onto main 0f86bd594: vk_console_heap_gpu 1/1 passed, incl. "a removed heap's metadata is freed once its passes are done: ok". Not run on main (doesn't build there). No validation errors.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7291987374 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2760#issuecomment-7291987374). Originally posted by [ammonium231](https://github.com/ammonium231) on 2026-11-30T21:09:07Z. --- RTX 3090 / 621.40, validation on, fd8b66cf6 merged onto main 0f86bd594: vk_console_heap_gpu 1/1 passed, incl. "a removed heap's metadata is freed once its passes are done: ok". Not run on main (doesn't build there). No validation errors.
KelboLive commented 2026-12-01 05:34:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:34:51Z.

Review: COMMENTED


The retire half is solid. remove_heap stamps the newest serial (console_heap_gpu.h:414-421), record() puts the pass into in_flight_ before returning (:727), and drain() frees a retired buffer only when last_pass < min(in_flight serial) (:789); a pass recorded after the removal skips the cleared heap, commit(false) takes the pass off and re-queues its work, and I could not construct a path where metadata is freed while a pass that names it is in flight. Mutation: dropping the serial test fails the new section (the retired() == 5 / == 1 checks), and disabling the free fails the PR's own control — pinned both ways. The validation run is clean, but only because the test device has one family.

Two things on the sharing half:

  1. The ordering needs an argument the code does not show. share_across_queue_families sets VK_SHARING_MODE_CONCURRENT (queue_families.h:38-44, called at console_heap_gpu.h:1030), which makes use from the async-compute family legal, but it does not serialise the two queues' preludes. The key table is cleared in place, whole, by one queue's pass — record()'s vkCmdFillBuffer(..., key_table_.buffer, 0, VK_WHOLE_SIZE, 0) (:615-616) after init() (:391), reset_keys() (:501, from d3d12_vulkan_device.cpp:11783), and a failed submission's commit(false) (:763). If another family's prelude is in flight during that fill (two independent submissions, no fence between them, which is legal D3D12), it reads and updates the same buffer. Either clear the key table before any submission is in flight (init's own submit+wait) or version it on reset_keys() the way the range/backed tables already are.
  2. Say who orders two preludes, i.e. that the title's D3D12 fences become the layer's semaphore waits, so a reviewer can see the argument rather than infer it.

The test device has one queue family, so neither the new test nor the validation run exercises the sharing change at all: a two-family device test (or a synchronization-validation run with two families) is the missing case.

Minor: record() without commit() keeps the pass and any retired buffer it blocks — a leak, not a use-after-free, but worth one line on the set_prelude contract.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6125703842 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2760#pullrequestreview-6125703842). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T05:34:51Z. **Review:** COMMENTED --- The retire half is solid. `remove_heap` stamps the newest serial (`console_heap_gpu.h:414-421`), `record()` puts the pass into `in_flight_` **before returning** (`:727`), and `drain()` frees a retired buffer only when `last_pass < min(in_flight serial)` (`:789`); a pass recorded after the removal skips the cleared heap, `commit(false)` takes the pass off and re-queues its work, and I could not construct a path where metadata is freed while a pass that names it is in flight. Mutation: dropping the serial test fails the new section (the `retired() == 5` / `== 1` checks), and disabling the free fails the PR's own control — pinned both ways. The validation run is clean, but only because the test device has one family. Two things on the sharing half: 1. **The ordering needs an argument the code does not show.** `share_across_queue_families` sets `VK_SHARING_MODE_CONCURRENT` (`queue_families.h:38-44`, called at `console_heap_gpu.h:1030`), which makes use from the async-compute family legal, but it does not serialise the two queues' preludes. The key table is cleared in place, **whole**, by one queue's pass — `record()`'s `vkCmdFillBuffer(..., key_table_.buffer, 0, VK_WHOLE_SIZE, 0)` (`:615-616`) after `init()` (`:391`), `reset_keys()` (`:501`, from `d3d12_vulkan_device.cpp:11783`), and a failed submission's `commit(false)` (`:763`). If another family's prelude is in flight during that fill (two independent submissions, no fence between them, which is legal D3D12), it reads and updates the same buffer. Either clear the key table before any submission is in flight (init's own submit+wait) or version it on `reset_keys()` the way the range/backed tables already are. 2. **Say who orders two preludes**, i.e. that the title's D3D12 fences become the layer's semaphore waits, so a reviewer can see the argument rather than infer it. The test device has one queue family, so neither the new test nor the validation run exercises the sharing change at all: a two-family device test (or a synchronization-validation run with two families) is the missing case. Minor: `record()` without `commit()` keeps the pass and any retired buffer it blocks — a leak, not a use-after-free, but worth one line on the `set_prelude` contract.
aslan2142 commented 2026-12-01 16:46:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:46:54Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318220306 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2760#issuecomment-7318220306). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:46:54Z. --- 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2760
No description provided.