WIP: perf(vk): dead render targets are let go as they die, and a texture's end invalidates handles only when it must (RX_VK_RETIRE_LAZY=0 to disable) #2054

Draft
siantm wants to merge 1 commit from siantm/Longshot:perf/vk-retire-lazy into main
Contributor

Summary

Between the eviction's passes the dead render targets were found by walking every rendered target each present
(dead_targets_sweep), and under the budget by the pass over the whole cache every tenth present; and every texture's
end raised the destroy generation, so every resolved handle of every thread was looked up again after each
retirement.

With this, the candidates are noted when one can first become dead: a texture whose GPU contents begin, or begin again
after a frame without a write (texture_written, texture_rendered: retire_note_run), is looked up among the
textures the GPU wrote, kept in order of address, and it and the textures whose storage it overlaps are candidates.
Each present looks at the candidates alone, by the pass's own rules, and lets the dead ones go through the pass's own
end (retire_chosen). The pass still runs over the budget as before, and under it every 600 presents for what this may
have left (said in the log). A texture's end raises the destroy generation only when the texture was handed out in the
current generation. Main's pool and image reaper are untouched. The result expected: less CPU time a present for the
dead targets, and fewer handle lookups after a retirement.

The switch: RX_VK_RETIRE_LAZY (new, a kill switch, bool, its row in gpu/vk/switches.def), on by default as the
branch shipped it, read once; =0 gives back main's code: the sweep, the pass every RX_VK_DEAD_EVERY presents and a
new generation at every texture's end.

From bd3ea307 on exp/x86-asm-sync, with 8e9cf4ac's test.

Read first: retire_note_run, retire_lazy, retire_chosen and destroy_later in gpu/vk/vk_texture.cpp, then
tests/test_target_overlap.py and tests/test_target_image_reuse.py.

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head 4fb14183 on main 75317e1f. Build jobs of this PC's job queue, the checks in a hidden console: tests/run_all.py --strict --exact on tests/test_dead_targets_sweep.py, tests/test_drawn_page_map.py, tests/test_readback_timeout_reuse.py, tests/test_readback_timeout_source.py, tests/test_target_image_pool.py, tests/test_target_image_reuse.py, tests/test_target_overlap.py and every check --changed picks for the files this changes (batch CapCut4-b1, log t-CapCut4-vk-retire-lazy.log): 89 checks, 75 passed, 14 failed, 0 skipped; its own checks pass, the switch's read included (fresh processes), and tests/test_target_retention.py with them, but for tests/test_readback_timeout_source.py, which this change updates and which fails on main the same way before reaching its cases (its fixture has no stand-in for vkDestroyCommandPool: pre-existing); the failing ones fail on their base too, run from a base worktree (t-CapCut3-base-main.log (batch CapCut3-b1)): tests/test_crash_dialog.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_dev_ab.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_frame_overlap.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_linux_pressure.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_low_vram_texture_mips.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_pass_barrier.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_perf_report.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_present_queue_default.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_readback_timeout_source.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_staging_burst.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_target_reuse_memory.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_texel_buffer_ownership.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_ub_update_batch.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log), tests/test_vk_occlusion_queries.py (fails on main @ 75317e1f too, log t-CapCut3-base-main.log).

It has NOT run in the game on this PC yet.

Still to test:

  • Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest, Castle Town's market and Hyrule Field, play three minutes each turning the camera: main @ 75317e1f, this branch with RX_VK_RETIRE_LAZY=0 and this branch with it unset (on), each warmed up a minute in the scene first, interleaved (ABBA), at least two runs each, expecting the same picture (no stale or corrupted target), no FATAL or device lost, VRAM about the same, few textures in the 600-present sweep's line, and a median frame time unset lower than with RX_VK_RETIRE_LAZY=0 and than main, and RX_VK_RETIRE_LAZY=0 the same as main (by hand)
  • Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest and Castle Town's market, play three minutes each: main @ 75317e1f, this branch with RX_VK_RETIRE_LAZY=0 and this branch with it unset (on), warmed up, ABBA, expecting no FATAL or device lost, the same picture and VRAM about the same, and a median frame time unset lower than with RX_VK_RETIRE_LAZY=0 and than main, and RX_VK_RETIRE_LAZY=0 the same as main (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest and Castle Town's market three minutes each: main @ 75317e1f, this branch with RX_VK_RETIRE_LAZY=0 and this branch with it unset (on), warmed up, ABBA, the log, frame times and a screenshot)
  • Linux: build the renderer and run python3 -I -B tests/test_dead_targets_sweep.py, expecting PASS (by hand)

Measurements

Verdict: not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game A/B is
in Still to test. RX_VK_RETIRE_LAZY ships on, as the branch shipped it; no measurement chose that default yet, the in-game A/B in Still to test does.

Risks

What could break: a dead target never noted would hold its VRAM until the 600-present pass (more VRAM, more eviction
on a small card); a handle kept past its texture's end would draw a freed image (a corrupted texture or a device lost).
The candidates are judged by the pass's own rules, and a texture handed out since the last raise still waits for
RX_VK_DEAD_EVERY. The tests run dead_shifted beside the pass's drawn_over_shifted on the same textures,
texture_written's note, retire_chosen and the generation rule. Turn it off with RX_VK_RETIRE_LAZY=0. Renderer
code: the runtime is rebuilt; nothing changes for players' saves, options or caches.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def.
  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
## Summary Between the eviction's passes the dead render targets were found by walking every rendered target each present (`dead_targets_sweep`), and under the budget by the pass over the whole cache every tenth present; and every texture's end raised the destroy generation, so every resolved handle of every thread was looked up again after each retirement. With this, the candidates are noted when one can first become dead: a texture whose GPU contents begin, or begin again after a frame without a write (`texture_written`, `texture_rendered`: `retire_note_run`), is looked up among the textures the GPU wrote, kept in order of address, and it and the textures whose storage it overlaps are candidates. Each present looks at the candidates alone, by the pass's own rules, and lets the dead ones go through the pass's own end (`retire_chosen`). The pass still runs over the budget as before, and under it every 600 presents for what this may have left (said in the log). A texture's end raises the destroy generation only when the texture was handed out in the current generation. Main's pool and image reaper are untouched. The result expected: less CPU time a present for the dead targets, and fewer handle lookups after a retirement. The switch: `RX_VK_RETIRE_LAZY` (new, a kill switch, bool, its row in `gpu/vk/switches.def`), on by default as the branch shipped it, read once; `=0` gives back main's code: the sweep, the pass every `RX_VK_DEAD_EVERY` presents and a new generation at every texture's end. From bd3ea307 on `exp/x86-asm-sync`, with 8e9cf4ac's test. Read first: `retire_note_run`, `retire_lazy`, `retire_chosen` and `destroy_later` in `gpu/vk/vk_texture.cpp`, then `tests/test_target_overlap.py` and `tests/test_target_image_reuse.py`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head `4fb14183` on main `75317e1f`. Build jobs of this PC's job queue, the checks in a hidden console: `tests/run_all.py --strict --exact` on `tests/test_dead_targets_sweep.py`, `tests/test_drawn_page_map.py`, `tests/test_readback_timeout_reuse.py`, `tests/test_readback_timeout_source.py`, `tests/test_target_image_pool.py`, `tests/test_target_image_reuse.py`, `tests/test_target_overlap.py` and every check `--changed` picks for the files this changes (batch `CapCut4-b1`, log `t-CapCut4-vk-retire-lazy.log`): 89 checks, 75 passed, 14 failed, 0 skipped; its own checks pass, the switch's read included (fresh processes), and `tests/test_target_retention.py` with them, but for `tests/test_readback_timeout_source.py`, which this change updates and which fails on main the same way before reaching its cases (its fixture has no stand-in for `vkDestroyCommandPool`: pre-existing); the failing ones fail on their base too, run from a base worktree (`t-CapCut3-base-main.log` (batch `CapCut3-b1`)): `tests/test_crash_dialog.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_dev_ab.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_frame_overlap.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_linux_pressure.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_low_vram_texture_mips.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_pass_barrier.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_perf_report.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_present_queue_default.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_readback_timeout_source.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_staging_burst.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_target_reuse_memory.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_texel_buffer_ownership.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_ub_update_batch.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`), `tests/test_vk_occlusion_queries.py` (fails on main @ 75317e1f too, log `t-CapCut3-base-main.log`). **It has NOT run in the game on this PC yet.** **Still to test:** - Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest, Castle Town's market and Hyrule Field, play three minutes each turning the camera: main @ 75317e1f, this branch with `RX_VK_RETIRE_LAZY=0` and this branch with it unset (on), each warmed up a minute in the scene first, interleaved (ABBA), at least two runs each, expecting the same picture (no stale or corrupted target), no FATAL or device lost, VRAM about the same, few textures in the 600-present sweep's line, and a median frame time unset lower than with `RX_VK_RETIRE_LAZY=0` and than main, and `RX_VK_RETIRE_LAZY=0` the same as main (by hand) - Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest and Castle Town's market, play three minutes each: main @ 75317e1f, this branch with `RX_VK_RETIRE_LAZY=0` and this branch with it unset (on), warmed up, ABBA, expecting no FATAL or device lost, the same picture and VRAM about the same, and a median frame time unset lower than with `RX_VK_RETIRE_LAZY=0` and than main, and `RX_VK_RETIRE_LAZY=0` the same as main (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest and Castle Town's market three minutes each: main @ 75317e1f, this branch with RX_VK_RETIRE_LAZY=0 and this branch with it unset (on), warmed up, ABBA, the log, frame times and a screenshot) - Linux: build the renderer and run `python3 -I -B tests/test_dead_targets_sweep.py`, expecting PASS (by hand) ## Measurements **Verdict:** not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game A/B is in Still to test. `RX_VK_RETIRE_LAZY` ships on, as the branch shipped it; no measurement chose that default yet, the in-game A/B in Still to test does. ## Risks What could break: a dead target never noted would hold its VRAM until the 600-present pass (more VRAM, more eviction on a small card); a handle kept past its texture's end would draw a freed image (a corrupted texture or a device lost). The candidates are judged by the pass's own rules, and a texture handed out since the last raise still waits for `RX_VK_DEAD_EVERY`. The tests run `dead_shifted` beside the pass's `drawn_over_shifted` on the same textures, `texture_written`'s note, `retire_chosen` and the generation rule. Turn it off with `RX_VK_RETIRE_LAZY=0`. Renderer code: the runtime is rebuilt; nothing changes for players' saves, options or caches. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). - [ ] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. - [x] A check under `tests/` that reads changed source is updated, not weakened. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold.
perf(vk): dead render targets are let go as they die, and a texture's end invalidates resolved handles only when it must (RX_VK_RETIRE_LAZY)
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 6s
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
4fb141834a
Between the eviction's passes the dead render targets were found by walking every rendered target each present
(dead_targets_sweep), and under the budget by the pass over the whole cache every tenth present; and every texture's end
raised the destroy generation, so every resolved handle of every thread was looked up again after each retirement.

Now the candidates are noted when one can first become dead: a texture whose GPU contents begin, or begin again after a
frame without a write (texture_written, texture_rendered: retire_note_run), is looked up among the textures the GPU wrote,
kept in order of address (written_at, the long ones apart), and it and the textures whose storage it overlaps are
candidates. Each present looks at the candidates alone, by the pass's own rules (unused long enough, the GPU past them,
drawn over at another start or their own: dead_shifted, dead_same_start) and lets the dead ones go through the pass's own
end, now one function (retire_chosen), each out of the cache by its key (key_of). The pass still runs over the budget as
before, and under it every 600 presents for what this may have left (said in the log).

A texture's end raises the destroy generation only when the texture was handed out (texture_get, texture_for_present:
resolved_gen) in the current generation: one handed out before the last raise is named by nothing still good. A
candidate handed out since the last raise waits for RX_VK_DEAD_EVERY's present, as all did. texture_gone() keeps the old
count for the performance report's textures_gone.

From bd3ea307 (and 8e9cf4ac's test) on exp/x86-asm-sync, adapted to main: main's #1681 pool and image reaper are
untouched; retire_chosen takes the completed value as a parameter; the sweep stays for RX_VK_RETIRE_LAZY=0. Not
measured: the branch's note is the pass's choice at ~0.5 ms for 1,930 textures on a Ryzen 7 9800X3D (not this port).

Tests: test_target_overlap runs the lazy retirement's dead_shifted beside the pass's drawn_over_shifted on the same stub
textures (with long ones and reverse address order); test_drawn_page_map runs texture_written's note of a run of GPU
writes; test_readback_timeout_reuse runs retire_chosen; test_target_image_reuse runs destroy_later's generation rule
(on, handed out before or in this generation, and =0); test_readback_timeout_source, test_dead_targets_sweep and
test_target_image_pool take the new code.

Switch: RX_VK_RETIRE_LAZY (kill), on; =0 the sweep, the pass every RX_VK_DEAD_EVERY presents and a new generation at
every texture's end, as before.

Switch reads: the test also runs RX_VK_RETIRE_LAZY as the source reads it, in fresh processes (unset, 0, 1, 2, off, each twice, the variable flipped after the first read: read once).

Assisted-by: Claude Code
Member

Possibly related, for context:

  • PR #1484 perf(vk): parts of 240 draws, uniform updates in one copy, a cheaper write-back forget, and the frame's GPU idle measured — Merged Vulkan perf work includes dead-target sweep and texture write-back retirement path
  • PR #1255 perf(vk): the dead render targets go at every present, as RX_VK_DEAD_EVERY=1 meant, and a pooled image waits 8 presents (RX_VK_POOL_IDLE) — Merged change runs dead-target sweep between eviction passes; same mechanism
  • PR #943 fix(vk): let render targets go as newer ones together cover them, and pool their images at once — Merged target-overlap retirement rules this PR reuses
  • PR #2048 WIP: perf(vk): a draw takes its targets by reference and their colour types as worked out at their resolution — Open renderer perf change from same author/branch area; potential shared texture/render-target code overlap
  • issue #1606 The upscalers give up a frame whenever any texture is destroyed anywhere, though their own depth and colour were not touched — Issue discusses global texture-destroy generation invalidation, which this PR changes
Possibly related, for context: - PR #1484 perf(vk): parts of 240 draws, uniform updates in one copy, a cheaper write-back forget, and the frame's GPU idle measured — Merged Vulkan perf work includes dead-target sweep and texture write-back retirement path - PR #1255 perf(vk): the dead render targets go at every present, as RX_VK_DEAD_EVERY=1 meant, and a pooled image waits 8 presents (RX_VK_POOL_IDLE) — Merged change runs dead-target sweep between eviction passes; same mechanism - PR #943 fix(vk): let render targets go as newer ones together cover them, and pool their images at once — Merged target-overlap retirement rules this PR reuses - PR #2048 WIP: perf(vk): a draw takes its targets by reference and their colour types as worked out at their resolution — Open renderer perf change from same author/branch area; potential shared texture/render-target code overlap - issue #1606 The upscalers give up a frame whenever any texture is destroyed anywhere, though their own depth and colour were not touched — Issue discusses global texture-destroy generation invalidation, which this PR changes
Author
Contributor

@Beanstalk This adds RX_VK_RETIRE_LAZY, a kill switch on by default: dead render targets are let go as they die instead of in a later sweep (gpu/vk); RX_VK_RETIRE_LAZY=0 is main's way. It has not run in the game on these PCs yet. Please play Kokiri Forest, then Castle Town Market started from the market-entrance save, then Hyrule Field, two minutes each, the PR against main on the same PC, on the PR's side with the switch unset and with RX_VK_RETIRE_LAZY=0, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture, no crash, device lost or FATAL line; peak VRAM about main's; unset, frame time p99 equal to main's or better and to the switch off.

@Beanstalk This adds RX_VK_RETIRE_LAZY, a kill switch on by default: dead render targets are let go as they die instead of in a later sweep (gpu/vk); RX_VK_RETIRE_LAZY=0 is main's way. It has not run in the game on these PCs yet. Please play Kokiri Forest, then Castle Town Market started from the market-entrance save, then Hyrule Field, two minutes each, the PR against main on the same PC, on the PR's side with the switch unset and with RX_VK_RETIRE_LAZY=0, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture, no crash, device lost or FATAL line; peak VRAM about main's; unset, frame time p99 equal to main's or better and to the switch off.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 6s
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
This pull request has changes conflicting with the target branch.
  • tests/test_dead_targets_sweep.py
Some workflows are waiting to be reviewed.
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/vk-retire-lazy:siantm-perf/vk-retire-lazy'
git switch 'siantm-perf/vk-retire-lazy'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 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!2054
No description provided.