WIP: fix(vk): prioritize driver-returning frees and bound synchronous block-share drains in reap_now (#2905) #3097

Closed
Jer9 wants to merge 2 commits from Jer9/Longshot:fix/vk-vma-reap-stall-2905 into main
Member

Summary

What a player notices: When texture memory approaches the driver budget (for example, entering Kakariko Village or Hyrule Market on 8 GB GPUs or integrated GPUs), frame times no longer hitch from draining every queued block-allocated texture synchronously on the render thread.

  • Helps: Constrained-VRAM GPUs and iGPUs (such as AMD RX 6650 XT 8 GB / Radeon 860M on Windows Adrenalin 26.10.1) hitting mem_account arbiter pressure (#2905).
  • No measurable impact: High-VRAM GPUs that never reach the texture budget or trigger reap_now.

Root Cause & Fix (gpu/vk/vk_vma.cpp)

Under VRAM pressure (mem_account arbiter level 1/2), reap_now(want, vram) looped while got < want, incrementing got += it->returns[l]. For block-allocated Reaped images, returns[l] == 0, so got stayed 0 < want and reap_now stole and synchronously destroyed every queued block-allocated image on the render thread in a single frame instead of letting the background mkw_vk reaper thread drain them, and destroyed block-allocated images ahead of dedicated allocations that actually return VRAM to the driver.

  • Pass 1: Take entries that return memory directly to the driver (returns_to_driver(l)) first until got >= want.
  • Pass 2 (only if got < want): For normal arbiter pressure (want < UINT64_MAX), bound synchronous block-share frees to block_freed < want - got and at most kReapNowBlockCap = 16 entries per call, leaving the remainder for the background reaper thread.
  • Emergency OOM (want == UINT64_MAX, memory_refused): Sets drain_all = true and still drains 100% of the ledger unconditionally. RX_VK_REAPER=0 and compact_in_pass guards are unchanged.

Testing

  • How to test:
    1. Run with constrained VRAM (RX_VK_TEST_ALLOWED_MB=2800 on Radeon 860M or 8 GB dGPU in Market / Hyrule Field).
    2. Verify 0 VK_ERROR_DEVICE_LOST / OOM and lower create_ms / >50 ms frame spikes.
  • Game-free test (tests/test_image_reaper_threads.py): Added 4 cases in bccf053ac (150-image burst cap, small-want byte bound, dedicated-first priority, UINT64_MAX OOM full drain; fails on forge/main, passes with c46174be5).
  • Test suite: tests/test_image_reaper.py (6/6), tests/test_image_reaper_threads.py, tests/test_submit_oom.py, and python -I -B tests/run_all.py --changed forge/main (21 passed, 0 failed, 5 skipped) all pass.

Measurements

Measured standalone on Windows 11, AMD Ryzen AI 7 350 / Radeon 860M (RDNA 3.5, Adrenalin 26.10.1), windowed 1920x1080 (100% render scale), RX_VK_TEST_ALLOWED_MB=2800 (1260 MB texture budget):

Where it makes a difference

Metric (Market_Child / HyruleField_Adult) forge/main (b4d56f526) fix/vk-vma-reap-stall-2905 (c46174be5) Delta
Market_Child Mean FPS / 1% Low 44.27 / 32.14 FPS 46.35 / 34.15 FPS +4.7% / +6.3%
Market_Child p99 / max frame time 28.0 / 39.2 ms 25.6 / 34.7 ms -8.6% / -11.5%
HyruleField_Adult 1% Low 19.40 FPS 22.01 FPS +13.5%
HyruleField_Adult >50 ms spikes 5 1 -80.0%
HyruleField_Adult create_ms p99 / max 4.10 / 11.37 ms 2.63 / 6.55 ms -35.9% / -42.4%

Where it does NOT make a difference (within run-to-run variance)

Metric forge/main (b4d56f526) fix/vk-vma-reap-stall-2905 (c46174be5) Delta
HyruleField_Adult Mean FPS 38.57 FPS 38.55 FPS -0.1% (variance)

Checklist

  • Tested on current main (b4d56f526)
  • Game-free test added (tests/test_image_reaper_threads.py, failing on main, passing here)
  • python -I -B tests/run_all.py --changed forge/main passes (21 passed, 0 failed, 5 skipped)
  • Full mise run test / tests/run_all.py on this branch

Assisted-by: Antigravity (Gemini 4 Neon)
Co-Authored-By: Antigravity [email protected]

## Summary **What a player notices:** When texture memory approaches the driver budget (for example, entering Kakariko Village or Hyrule Market on 8 GB GPUs or integrated GPUs), frame times no longer hitch from draining every queued block-allocated texture synchronously on the render thread. - **Helps:** Constrained-VRAM GPUs and iGPUs (such as AMD RX 6650 XT 8 GB / Radeon 860M on Windows Adrenalin `26.10.1`) hitting `mem_account` arbiter pressure (`#2905`). - **No measurable impact:** High-VRAM GPUs that never reach the texture budget or trigger `reap_now`. ### Root Cause & Fix (`gpu/vk/vk_vma.cpp`) Under VRAM pressure (`mem_account` arbiter level 1/2), `reap_now(want, vram)` looped while `got < want`, incrementing `got += it->returns[l]`. For block-allocated `Reaped` images, `returns[l] == 0`, so `got` stayed `0 < want` and `reap_now` stole and synchronously destroyed **every** queued block-allocated image on the render thread in a single frame instead of letting the background `mkw_vk reaper` thread drain them, and destroyed block-allocated images ahead of dedicated allocations that actually return VRAM to the driver. - **Pass 1:** Take entries that return memory directly to the driver (`returns_to_driver(l)`) first until `got >= want`. - **Pass 2 (only if `got < want`):** For normal arbiter pressure (`want < UINT64_MAX`), bound synchronous block-share frees to `block_freed < want - got` and at most `kReapNowBlockCap = 16` entries per call, leaving the remainder for the background reaper thread. - **Emergency OOM (`want == UINT64_MAX`, `memory_refused`):** Sets `drain_all = true` and still drains 100% of the ledger unconditionally. `RX_VK_REAPER=0` and `compact_in_pass` guards are unchanged. ## Testing - **How to test:** 1. Run with constrained VRAM (`RX_VK_TEST_ALLOWED_MB=2800` on Radeon 860M or 8 GB dGPU in Market / Hyrule Field). 2. Verify `0` `VK_ERROR_DEVICE_LOST` / OOM and lower `create_ms` / `>50 ms` frame spikes. - **Game-free test (`tests/test_image_reaper_threads.py`):** Added 4 cases in `bccf053ac` (150-image burst cap, small-`want` byte bound, dedicated-first priority, `UINT64_MAX` OOM full drain; fails on `forge/main`, passes with `c46174be5`). - **Test suite:** `tests/test_image_reaper.py` (`6/6`), `tests/test_image_reaper_threads.py`, `tests/test_submit_oom.py`, and `python -I -B tests/run_all.py --changed forge/main` (`21 passed, 0 failed, 5 skipped`) all pass. ## Measurements Measured standalone on **Windows 11, AMD Ryzen AI 7 350 / Radeon 860M (RDNA 3.5, Adrenalin 26.10.1)**, windowed `1920x1080` (`100% render scale`), `RX_VK_TEST_ALLOWED_MB=2800` (`1260 MB` texture budget): ### Where it makes a difference | Metric (`Market_Child` / `HyruleField_Adult`) | `forge/main` (`b4d56f526`) | `fix/vk-vma-reap-stall-2905` (`c46174be5`) | Delta | |---|---:|---:|---:| | `Market_Child` Mean FPS / 1% Low | 44.27 / 32.14 FPS | **46.35 / 34.15 FPS** | **+4.7% / +6.3%** | | `Market_Child` p99 / max frame time | 28.0 / 39.2 ms | **25.6 / 34.7 ms** | **-8.6% / -11.5%** | | `HyruleField_Adult` 1% Low | 19.40 FPS | **22.01 FPS** | **+13.5%** | | `HyruleField_Adult` `>50 ms` spikes | 5 | **1** | **-80.0%** | | `HyruleField_Adult` `create_ms` p99 / max | 4.10 / 11.37 ms | **2.63 / 6.55 ms** | **-35.9% / -42.4%** | ### Where it does NOT make a difference (within run-to-run variance) | Metric | `forge/main` (`b4d56f526`) | `fix/vk-vma-reap-stall-2905` (`c46174be5`) | Delta | |---|---:|---:|---:| | `HyruleField_Adult` Mean FPS | 38.57 FPS | 38.55 FPS | -0.1% (variance) | ## Checklist - [x] Tested on current `main` (`b4d56f526`) - [x] Game-free test added (`tests/test_image_reaper_threads.py`, failing on `main`, passing here) - [x] `python -I -B tests/run_all.py --changed forge/main` passes (`21 passed, 0 failed, 5 skipped`) - [ ] Full `mise run test` / `tests/run_all.py` on this branch Assisted-by: Antigravity (Gemini 4 Neon) Co-Authored-By: Antigravity <[email protected]>
fix(vk): prioritize driver-returning frees and bound synchronous block-share drains in reap_now (#2905)
Some checks failed
PR description / Title and description (pull_request_target) Successful in 7s
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
c46174be55
Author
Member

Tested in game: Windows 11 Radeon 860M 1920x1080 RX_VK_TEST_ALLOWED_MB=2800: Hyrule +13.5% 1% low, >50ms spikes -80%, 0 DEVICE_LOST

Tested in game: Windows 11 Radeon 860M 1920x1080 RX_VK_TEST_ALLOWED_MB=2800: Hyrule +13.5% 1% low, >50ms spikes -80%, 0 DEVICE_LOST
Member

Possibly related, for context:

  • PR #1914 perf(vk): the image reaper's queue is bounded and gives its memory back near the VRAM allowance — Earlier merged reaper pressure handling; this extends its reap_now behavior
  • PR #2081 fix(vk): host-visible buffers stay within their heap's budget, and a submit refused for want of memory is made again — Merged OOM recovery drains the reaper queue after submit refusal
  • issue #586 Idea: Smaller Texture Size for 4GB VRAM — Open low-VRAM texture reduction idea; adjacent VRAM constraint context
Possibly related, for context: - PR #1914 perf(vk): the image reaper's queue is bounded and gives its memory back near the VRAM allowance — Earlier merged reaper pressure handling; this extends its reap_now behavior - PR #2081 fix(vk): host-visible buffers stay within their heap's budget, and a submit refused for want of memory is made again — Merged OOM recovery drains the reaper queue after submit refusal - issue #586 Idea: Smaller Texture Size for 4GB VRAM — Open low-VRAM texture reduction idea; adjacent VRAM constraint context
Author
Member

Closing this WIP PR after adversarial self-review:

  1. Issue #2905's own log (6110/6920 MB = 88.3% < 90% VRAM allowance, vk: reaper: 205 images destroyed, 0 of them where they were handed over) confirms reap_now() was not active during #2905's Kakariko/Market stalls.
  2. Capping Pass 2 at kReapNowBlockCap = 16 under genuine >= 90% VRAM pressure risks starving texture_pool block reuse (when small texture_small_pool entries sit ahead of large textures) and double-counting queued reaper_images[VRAM] inside texture_frame's pressure calculation.

Will investigate #2905's actual < 90% VRAM stall path separately if warranted.

Closing this WIP PR after adversarial self-review: 1. Issue #2905's own log (`6110/6920 MB` = `88.3% < 90%` VRAM allowance, `vk: reaper: 205 images destroyed, 0 of them where they were handed over`) confirms `reap_now()` was not active during #2905's Kakariko/Market stalls. 2. Capping Pass 2 at `kReapNowBlockCap = 16` under genuine `>= 90%` VRAM pressure risks starving `texture_pool` block reuse (when small `texture_small_pool` entries sit ahead of large textures) and double-counting queued `reaper_images[VRAM]` inside `texture_frame`'s `pressure` calculation. Will investigate #2905's actual `< 90%` VRAM stall path separately if warranted.
Jer9 closed this pull request 2026-12-03 22:46:44 +00:00
Some checks failed
PR description / Title and description (pull_request_target) Successful in 7s
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
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
MagicBeanDev/Longshot!3097
No description provided.