d3d12: evict geometry views on range release and retry refused ranges #2094

Merged
leonida/mandy
qhukz13 merged 9 commits from cont/geometry-view-eviction into main 2026-11-26 02:43:17 +00:00

Pull request successfully merged and closed

qhukz13 commented 2026-11-25 20:04:41 +00:00

Item: RT-GEOMETRY-VIEW-EVICTION

Problem

runtime/shims/d3d12_geometry_views.h: once LEONIDA_GEOMETRY_MB is used up, resolve() marks a range refused for good, and no view is freed before teardown, even after the title releases the memory it copies.

Change

  • Release events. xmem reports title ranges that go away: XMemFree/XMemFreeDefault, VirtualFree MEM_RELEASE/MEM_DECOMMIT, the XMemMapPhysicalPages unmap and the CopyPageMappingsBatchX unmap range. They go to a small queue in title_geometry_watch.h (note_released/take_released). Ranges outside every watched tile are not queued; a GPU-only range counts through its CPU view, where the views are keyed. When the queue is full it collapses into one covering range: retiring a few views too many is harmless (they are made again on the next resolve), dropping a release is not.
  • Retire, then reap. On the submit thread (before_execute_, before the geometry refresh) a released range retires every view over it at once: it leaves keys_, the address map, the fast lookup, the tile watches and the title VA aliases. Its storage is freed only once completed_retire_ticket() passes the submitted_retire_ticket() taken at retirement, so in-flight work keeps reading valid memory. The recording threads never drain, so they do not wait on GeometryViews::mutex_ while a refresh holds it (speed-26).
  • Retry. A refused key remembers the free generation it was refused in. resolve() answers it from the fast map without a lock while nothing was freed, and tries again once a reap has returned budget.
  • Storage. TitleBufferWindows keeps coalesced free ranges per block, destroys a block when its last range comes back, and keeps one empty 4 MiB block as a spare (dropped when a larger request needs the cap), so a title that frees and reallocates each frame does not allocate Vulkan memory each frame. The 2 s backoff after a driver refusal is kept.
  • refresh() walks the tiles by key (lower_bound) instead of holding a std::map iterator and raw Tile* across its periodic unlock, because a retirement can now erase a tile in that window.

Nothing changes while under the cap, and with LEONIDA_GEOMETRY_MB=0 (default) note_released returns on one atomic load.

Testing

Windows 10 Pro 19045, GeForce GTX 1050 Ti (Pascal, 4 GB, no Resizable BAR), NVIDIA 582.66, MSVC (VS 18 Build Tools), RelWithDebInfo, base 9f9a7c5f3.

  • geometry_views_test (extended: small cap, refusal and retry, deferred reap before/at the ticket, block reclamation, watched-bounds filter, GPU-only twin, queue overflow, spare block): 51 checks, 3/3 passes.
  • Full ctest (with #2059 applied): 449 tests, 13 failed. All 13 also fail on plain 9f9a7c5f3 (16 failures there); the other three are d3d12_compute and d3d12_geometry_views, which #2059 fixes, and xmem, which is flaky on this host (5/5 on rerun, also without this branch). No test that passes on main fails here.
  • 3x, plain and with LEONIDA_VK_VALIDATION=1: geometry_views, xmem*, d3d12_title_va*, d3d12_geometry_views, d3d12_title_window_cap, d3d12_api_descriptor_copy*, d3d12_rebound*, d3d12_compute, d3d12_xmem_placed: 17/17 pass, no validation messages.
  • On Windows 10, d3d12_geometry_views only reaches the geometry code with #2059 (root signature serialization); the integration numbers above were taken with #2059 applied locally.

AI-assisted (Claude Code, Opus 5.5, with Antigravity / Gemini 4 Neon for the first draft of the code; every change reviewed and the GPU-only filter case fixed by hand).

🤖 Generated with Claude Code

Item: `RT-GEOMETRY-VIEW-EVICTION` ### Problem `runtime/shims/d3d12_geometry_views.h`: once `LEONIDA_GEOMETRY_MB` is used up, `resolve()` marks a range refused for good, and no view is freed before teardown, even after the title releases the memory it copies. ### Change - **Release events.** `xmem` reports title ranges that go away: `XMemFree`/`XMemFreeDefault`, `VirtualFree` `MEM_RELEASE`/`MEM_DECOMMIT`, the `XMemMapPhysicalPages` unmap and the `CopyPageMappingsBatchX` unmap range. They go to a small queue in `title_geometry_watch.h` (`note_released`/`take_released`). Ranges outside every watched tile are not queued; a GPU-only range counts through its CPU view, where the views are keyed. When the queue is full it collapses into one covering range: retiring a few views too many is harmless (they are made again on the next resolve), dropping a release is not. - **Retire, then reap.** On the submit thread (`before_execute_`, before the geometry refresh) a released range retires every view over it at once: it leaves `keys_`, the address map, the fast lookup, the tile watches and the title VA aliases. Its storage is freed only once `completed_retire_ticket()` passes the `submitted_retire_ticket()` taken at retirement, so in-flight work keeps reading valid memory. The recording threads never drain, so they do not wait on `GeometryViews::mutex_` while a refresh holds it (speed-26). - **Retry.** A refused key remembers the free generation it was refused in. `resolve()` answers it from the fast map without a lock while nothing was freed, and tries again once a reap has returned budget. - **Storage.** `TitleBufferWindows` keeps coalesced free ranges per block, destroys a block when its last range comes back, and keeps one empty 4 MiB block as a spare (dropped when a larger request needs the cap), so a title that frees and reallocates each frame does not allocate Vulkan memory each frame. The 2 s backoff after a driver refusal is kept. - `refresh()` walks the tiles by key (`lower_bound`) instead of holding a `std::map` iterator and raw `Tile*` across its periodic unlock, because a retirement can now erase a tile in that window. Nothing changes while under the cap, and with `LEONIDA_GEOMETRY_MB=0` (default) `note_released` returns on one atomic load. ### Testing Windows 10 Pro 19045, GeForce GTX 1050 Ti (Pascal, 4 GB, no Resizable BAR), NVIDIA 582.66, MSVC (VS 18 Build Tools), RelWithDebInfo, base `9f9a7c5f3`. - `geometry_views_test` (extended: small cap, refusal and retry, deferred reap before/at the ticket, block reclamation, watched-bounds filter, GPU-only twin, queue overflow, spare block): 51 checks, 3/3 passes. - Full ctest (with #2059 applied): 449 tests, 13 failed. All 13 also fail on plain `9f9a7c5f3` (16 failures there); the other three are `d3d12_compute` and `d3d12_geometry_views`, which #2059 fixes, and `xmem`, which is flaky on this host (5/5 on rerun, also without this branch). No test that passes on main fails here. - 3x, plain and with `LEONIDA_VK_VALIDATION=1`: `geometry_views`, `xmem*`, `d3d12_title_va*`, `d3d12_geometry_views`, `d3d12_title_window_cap`, `d3d12_api_descriptor_copy*`, `d3d12_rebound*`, `d3d12_compute`, `d3d12_xmem_placed`: 17/17 pass, no validation messages. - On Windows 10, `d3d12_geometry_views` only reaches the geometry code with #2059 (root signature serialization); the integration numbers above were taken with #2059 applied locally. AI-assisted (Claude Code, Opus 5.5, with Antigravity / Gemini 4 Neon for the first draft of the code; every change reviewed and the GPU-only filter case fixed by hand). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lixonik reviewed 2026-11-25 20:46:43 +00:00
lixonik left a comment
No description provided.
Native Windows review of this head. `geometry_views` passed its existing 51 checks and `xmem` passed on retry. The additional two-device case below failed. AI-assisted.
qhukz13 commented 2026-11-25 22:23:47 +00:00

Thanks, confirmed and fixed in 6fd430ebc. The release queue was process-wide while every DeviceState owns its own GeometryViews, so the first device to drain took every release.

  • title_geometry_watch.h: each GeometryViews now owns a ReleaseQueue and subscribes it on construction (unsubscribes in its destructor). note_released keeps the same filters (watched bounds, GPU-only twin) and then appends the range to every subscribed queue, each with the 65536-entry collapse on its own. Lock order is subscribers -> queue; take_released takes only its own queue.
  • Each owner drains its own queue in its before_execute_ and retires against its own submitted_retire_ticket() / completed_retire_ticket(), as you suggested.
  • geometry_views_test case 12 is your scenario: two instances over the same synthetic extent, one note_released, both drained -> both retire and reap the view and return to bytes() == 0. It also checks that an instance destroyed in a scope stops receiving releases while the survivor still does.

GTX 1050 Ti, Windows 10 19045, NVIDIA 582.66, MSVC RelWithDebInfo: geometry_views_test 60 checks, 3/3 passes. Full ctest with #2059 applied (for d3d12_geometry_views on Windows 10): 449 tests, the same 13 failures as before this commit, all of which fail on plain 9f9a7c5f3 too; geometry_views, xmem*, d3d12_title_va*, d3d12_geometry_views, d3d12_compute and the other title-VA cases pass 3x plain and 3x with LEONIDA_VK_VALIDATION=1, no validation messages.

AI-assisted (Claude Code).

Thanks, confirmed and fixed in 6fd430ebc. The release queue was process-wide while every `DeviceState` owns its own `GeometryViews`, so the first device to drain took every release. - `title_geometry_watch.h`: each `GeometryViews` now owns a `ReleaseQueue` and subscribes it on construction (unsubscribes in its destructor). `note_released` keeps the same filters (watched bounds, GPU-only twin) and then appends the range to every subscribed queue, each with the 65536-entry collapse on its own. Lock order is subscribers -> queue; `take_released` takes only its own queue. - Each owner drains its own queue in its `before_execute_` and retires against its own `submitted_retire_ticket()` / `completed_retire_ticket()`, as you suggested. - `geometry_views_test` case 12 is your scenario: two instances over the same synthetic extent, one `note_released`, both drained -> both retire and reap the view and return to `bytes() == 0`. It also checks that an instance destroyed in a scope stops receiving releases while the survivor still does. GTX 1050 Ti, Windows 10 19045, NVIDIA 582.66, MSVC RelWithDebInfo: `geometry_views_test` 60 checks, 3/3 passes. Full ctest with #2059 applied (for `d3d12_geometry_views` on Windows 10): 449 tests, the same 13 failures as before this commit, all of which fail on plain `9f9a7c5f3` too; `geometry_views`, `xmem*`, `d3d12_title_va*`, `d3d12_geometry_views`, `d3d12_compute` and the other title-VA cases pass 3x plain and 3x with `LEONIDA_VK_VALIDATION=1`, no validation messages. AI-assisted (Claude Code).
moffington commented 2026-11-26 01:31:01 +00:00

Checked 6fd430ebc on Linux/Wine (clang++ --target=x86_64-w64-windows-gnu): geometry_views_test passes all 60 checks (0 failures), confirming the per-GeometryViews ReleaseQueue subscription fix for multi-device release delivery.

Three findings still remain on 6fd430ebc:

  1. Confirmed in local probe (RetireTickets/GeometryViews) + code inspection — Cross-queue retirement ticket race (DeviceState::drain_geometry_releases): retire_ticket = context->submitted_retire_ticket() is sampled before unlinking outside the replay/submit critical section. Another queue that already captured a geometry VA during replay and publishes its submission afterward can get a higher ticket than the retirement cutoff; if the prior ticket is already complete, reap reclaims the backing while the newer submission is still pending (FAIL backing still holds queue A's data while ticket 6 is incomplete).
  2. Code inspection — before_execute_ ordering (refresh_shadows before drain_geometry_releases): fix_console_slot can resolve a title V# into a live geometry view, and drain_geometry_releases in the same before_execute_ can immediately unlink and reap it before the batch executes (and without an alias entry, TitleVaAliases::remove will not bump the generation).
  3. Code inspection — 65,537-entry overflow collapse in raw address space: When a subscriber's ReleaseQueue overflows, note_released collapses [min_begin, max_end) in the original address space, and drain_released only translates the collapsed range if its start is a GPU address. An unrelated CPU release below a GPU twin can mask the GPU-only event's CPU twin if the CPU twin lies outside [min_begin, max_end).

AI-assisted.

Checked `6fd430ebc` on Linux/Wine (`clang++ --target=x86_64-w64-windows-gnu`): `geometry_views_test` passes all 60 checks (0 failures), confirming the per-`GeometryViews` `ReleaseQueue` subscription fix for multi-device release delivery. Three findings still remain on `6fd430ebc`: 1. **Confirmed in local probe (`RetireTickets`/`GeometryViews`) + code inspection — Cross-queue retirement ticket race (`DeviceState::drain_geometry_releases`)**: `retire_ticket = context->submitted_retire_ticket()` is sampled before unlinking outside the replay/submit critical section. Another queue that already captured a geometry VA during replay and publishes its submission afterward can get a higher ticket than the retirement cutoff; if the prior ticket is already complete, `reap` reclaims the backing while the newer submission is still pending (`FAIL backing still holds queue A's data while ticket 6 is incomplete`). 2. **Code inspection — `before_execute_` ordering (`refresh_shadows` before `drain_geometry_releases`)**: `fix_console_slot` can resolve a title V# into a live geometry view, and `drain_geometry_releases` in the same `before_execute_` can immediately unlink and reap it before the batch executes (and without an alias entry, `TitleVaAliases::remove` will not bump the generation). 3. **Code inspection — 65,537-entry overflow collapse in raw address space**: When a subscriber's `ReleaseQueue` overflows, `note_released` collapses `[min_begin, max_end)` in the original address space, and `drain_released` only translates the collapsed range if its start is a GPU address. An unrelated CPU release below a GPU twin can mask the GPU-only event's CPU twin if the CPU twin lies outside `[min_begin, max_end)`. AI-assisted.
aslan2142 commented 2026-11-26 02:43:20 +00:00

Merged, thanks @qhukz13! Freeing geometry views when the title releases their range is a real gain under a cap. We merged main in and fixed what the review found:

  • Reap race: a retired view is freed only after every replay that may name it has recorded and completed its submissions (a new after-execute hook), and the drain runs before the console heaps are translated.
  • Answers without an alias: retirement now invalidates them too (TitleVaAliases::forget_copy), so heap slots and the GPU key table drop a reaped view.
  • Destroyed blocks: a destroyed block leaves #2098's published_ and the backed table (layout_generation, title_block_gone).
  • Tests: geometry_views_test keeps its views off the stack; its new case 13 covers the race, the invalidation and the block removal, and fails without these fixes.
  • Tested: the full Windows suite under Wine on the gfx1036 iGPU (RADV), merged onto main 3abb99cad: area tests 28/28, and nothing failing that doesn't fail on main too.
  • Left for later: a DeviceState-level test, and command lists executed again keep the root VAs resolved when they were recorded.
  • Checked on main 86dc3d86e + bbc8ef17c: it merges cleanly; the semantic suite passes (496 files, 6,336 tests, 0 files failed); the 15 docs and writer tests pass; check_no_game_data.py is clean on the 8 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

Merged, thanks @qhukz13! Freeing geometry views when the title releases their range is a real gain under a cap. We merged `main` in and fixed what the review found: - **Reap race:** a retired view is freed only after every replay that may name it has recorded and completed its submissions (a new after-execute hook), and the drain runs before the console heaps are translated. - **Answers without an alias:** retirement now invalidates them too (`TitleVaAliases::forget_copy`), so heap slots and the GPU key table drop a reaped view. - **Destroyed blocks:** a destroyed block leaves #2098's `published_` and the backed table (`layout_generation`, `title_block_gone`). - **Tests:** `geometry_views_test` keeps its views off the stack; its new case 13 covers the race, the invalidation and the block removal, and fails without these fixes. - **Tested:** the full Windows suite under Wine on the gfx1036 iGPU (RADV), merged onto `main` `3abb99cad`: area tests 28/28, and nothing failing that doesn't fail on `main` too. - **Left for later:** a DeviceState-level test, and command lists executed again keep the root VAs resolved when they were recorded. - **Checked on `main` `86dc3d86e` + `bbc8ef17c`:** it merges cleanly; the semantic suite passes (496 files, 6,336 tests, 0 files failed); the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 8 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No description provided.