WIP: fix(vk): hold the texture a present shows until the window has copied it #2064

Draft
siantm wants to merge 2 commits from siantm/Longshot:fix/vk-present-source-held into main
Contributor

Summary

The renderer could let go of the texture a present shows before the window had copied it. do_present picks the
present's source (texture_for_present), then texture_frame runs (the eviction pass, the dead render targets' sweep,
the retirement of images that served a new texture, the VRAM compaction), and only then does present_image copy the
source to the window. That copy is known to be done only when its frame slot's fence is waited, up to
kPresentSlotsMax (8) presents later. On main only the compaction leaves the source alone, and only the current
present's (present_source). A source picked by texture_for_present's fallback (a texture rendered at the presented
storage in another description) keeps its old last use, since a plain blit does not count as one; when another render
target has drawn over its storage, the sweep lets it go at the next present: its Texture erased, its image to the pool or
destroyed while the window may still copy it. A present of nothing and a dropped output (a held present after its
generation's release) left present_source naming the texture shown before.

With this, texture_for_present marks the texture it picks (Texture::presented): its own, the fallback, and the
Output preview's texture. The eviction pass (and its first pass for the compaction's hold, which counts what the dead
targets give back), the sweep, the retirement and the compaction leave a texture a present showed in the last
dead_targets::kPresentHold (9) presents, one more than the frame slots (a static_assert). A present of nothing and a
dropped output go through texture_for_present(nullptr): no source.

The result expected: nothing a player sees in normal play, where the shown texture is the game's own output, drawn every
frame and never a candidate to go; the window no longer copies from an image that may have been pooled or destroyed when
a stale fallback is shown.

Split from #1996, as dyst asked in its review (#1996 (comment)):
the hold fixes a lifetime gap that is on main and does not depend on #1996's grace; the rebase onto main's Output preview
and dropped output (dyst's #1996 (comment)) and the
compaction's two passes in step with the hold are from that review.

Read first: present_held in gpu/vk/dead_targets.h, texture_for_present in gpu/vk/vk_texture.cpp, the source
line in do_present (gpu/vk/vk_replay.cpp), then tests/test_present_source_held.py.

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head a9d04a2e on main 9dd41326, light
jobs of this PC's job queue (dy1996a, dy1996b), each check in a hidden console, RX_CPM set (the Vulkan headers
found). At the test commit e016aef5 (main's code): tests/test_present_source_held.py fails in all four cases (the
fallback source and the Output preview's texture let go at the next present, present_source not cleared for a dropped
output, kPresentHold missing), tests/test_target_retention.py fails cases 3 and 4 (the shown texture evicted; the
hold's pass counting it as given back) and tests/test_vram_compact.py fails (the compaction's exclusion). At the head
tests/run_all.py --strict --exact over tests/test_present_source_held.py, tests/test_target_retention.py,
tests/test_vram_compact.py, tests/test_dead_targets_sweep.py, tests/test_blocks_at_start.py,
tests/test_image_reaper.py, tests/test_target_image_pool.py, tests/test_drawn_page_map.py and
tests/test_present_source_crop.py: 8 pass; tests/test_present_source_crop.py fails here and on main's renderer
(dy1996b, at e016aef5) with the same error, its fixture's hud_source stand-in a parameter short. The held test also
passes with --cxx clang++. gpu/vk/vk_texture.cpp and gpu/vk/vk_replay.cpp compiled (syntax) with the game build's
own commands.

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

Still to test:

  • Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest and Hyrule Field, three minutes each turning the camera, main @
    9dd41326 against this branch, same save and settings, then a still screenshot at an input-free point on each side and
    main against itself, expecting the same picture, no FATAL or device lost (by hand)
  • Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: the same, expecting the same picture as main and no device
    lost (on the Test Farm, or by hand)
  • Windows: the Output resolution preview from the menu (start it, keep and revert it), expecting the same picture as main
    and no device lost (by hand)
  • Linux: build the renderer and run python3 -I -B tests/test_present_source_held.py, expecting PASS (by hand)

Measurements

Nothing to measure: a lifetime fix with no switch; a shown texture stays at most 9 presents longer (one texture's memory).

Risks

What could break: a texture the present showed stays up to 9 presents after it was last shown (its VRAM, one texture's,
for that long), and the eviction, the sweep, the retirement and the compaction pass it over meanwhile; a write-back such
an eviction would have done comes as late. No switch: a revert is enough. Nothing changes for players' saves, options
or caches. Overlap: #2054 (also siantm's) adds a lazy retirement path that checks present_source; whichever of the two
lands second adds dead_targets::present_held to it.

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 fix: a game-free test that fails on main and passes here.
  • A check under tests/ that reads changed source is updated, not weakened.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.

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 The renderer could let go of the texture a present shows before the window had copied it. `do_present` picks the present's source (`texture_for_present`), then `texture_frame` runs (the eviction pass, the dead render targets' sweep, the retirement of images that served a new texture, the VRAM compaction), and only then does `present_image` copy the source to the window. That copy is known to be done only when its frame slot's fence is waited, up to `kPresentSlotsMax` (8) presents later. On main only the compaction leaves the source alone, and only the current present's (`present_source`). A source picked by `texture_for_present`'s fallback (a texture rendered at the presented storage in another description) keeps its old last use, since a plain blit does not count as one; when another render target has drawn over its storage, the sweep lets it go at the next present: its Texture erased, its image to the pool or destroyed while the window may still copy it. A present of nothing and a dropped output (a held present after its generation's release) left `present_source` naming the texture shown before. With this, `texture_for_present` marks the texture it picks (`Texture::presented`): its own, the fallback, and the Output preview's texture. The eviction pass (and its first pass for the compaction's hold, which counts what the dead targets give back), the sweep, the retirement and the compaction leave a texture a present showed in the last `dead_targets::kPresentHold` (9) presents, one more than the frame slots (a `static_assert`). A present of nothing and a dropped output go through `texture_for_present(nullptr)`: no source. The result expected: nothing a player sees in normal play, where the shown texture is the game's own output, drawn every frame and never a candidate to go; the window no longer copies from an image that may have been pooled or destroyed when a stale fallback is shown. Split from #1996, as dyst asked in its review (https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1996#issuecomment-65721): the hold fixes a lifetime gap that is on main and does not depend on #1996's grace; the rebase onto main's Output preview and dropped output (dyst's https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1996#issuecomment-65719) and the compaction's two passes in step with the hold are from that review. Read first: `present_held` in `gpu/vk/dead_targets.h`, `texture_for_present` in `gpu/vk/vk_texture.cpp`, the source line in `do_present` (`gpu/vk/vk_replay.cpp`), then `tests/test_present_source_held.py`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head `a9d04a2e` on main `9dd41326`, light jobs of this PC's job queue (`dy1996a`, `dy1996b`), each check in a hidden console, `RX_CPM` set (the Vulkan headers found). At the test commit `e016aef5` (main's code): `tests/test_present_source_held.py` fails in all four cases (the fallback source and the Output preview's texture let go at the next present, `present_source` not cleared for a dropped output, `kPresentHold` missing), `tests/test_target_retention.py` fails cases 3 and 4 (the shown texture evicted; the hold's pass counting it as given back) and `tests/test_vram_compact.py` fails (the compaction's exclusion). At the head `tests/run_all.py --strict --exact` over `tests/test_present_source_held.py`, `tests/test_target_retention.py`, `tests/test_vram_compact.py`, `tests/test_dead_targets_sweep.py`, `tests/test_blocks_at_start.py`, `tests/test_image_reaper.py`, `tests/test_target_image_pool.py`, `tests/test_drawn_page_map.py` and `tests/test_present_source_crop.py`: 8 pass; `tests/test_present_source_crop.py` fails here and on main's renderer (`dy1996b`, at `e016aef5`) with the same error, its fixture's `hud_source` stand-in a parameter short. The held test also passes with `--cxx clang++`. `gpu/vk/vk_texture.cpp` and `gpu/vk/vk_replay.cpp` compiled (syntax) with the game build's own commands. **It has NOT run in the game on this PC yet.** **Still to test:** - Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest and Hyrule Field, three minutes each turning the camera, main @ 9dd41326 against this branch, same save and settings, then a still screenshot at an input-free point on each side and main against itself, expecting the same picture, no FATAL or device lost (by hand) - Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: the same, expecting the same picture as main and no device lost (on the Test Farm, or by hand) - Windows: the Output resolution preview from the menu (start it, keep and revert it), expecting the same picture as main and no device lost (by hand) - Linux: build the renderer and run `python3 -I -B tests/test_present_source_held.py`, expecting PASS (by hand) ## Measurements Nothing to measure: a lifetime fix with no switch; a shown texture stays at most 9 presents longer (one texture's memory). ## Risks What could break: a texture the present showed stays up to 9 presents after it was last shown (its VRAM, one texture's, for that long), and the eviction, the sweep, the retirement and the compaction pass it over meanwhile; a write-back such an eviction would have done comes as late. No switch: a revert is enough. Nothing changes for players' saves, options or caches. Overlap: #2054 (also siantm's) adds a lazy retirement path that checks `present_source`; whichever of the two lands second adds `dead_targets::present_held` to it. ## 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). - [x] 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 - [x] A fix: a game-free test that fails on `main` and passes here. - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. ### 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.
do_present picks its source (texture_for_present), texture_frame runs (the eviction pass, the dead targets' sweep, the
retirement of images that served a new texture, the compaction) before present_image copies it to the window, and that
copy is known to be done only when its frame slot's fence is waited, up to kPresentSlotsMax presents later. On main none of
those leave the source alone but the compaction (present_source, the current present's only), and a fallback source (a
texture rendered at the presented storage in another description) keeps its old last_frame and last_value, so a stale one
drawn over by another target is let go at the next present while the window may still copy it.

- tests/test_present_source_held.py (new): the real texture_for_present, dead_targets_sweep and reuse_retire_frame, and
  do_present's line that picks the source, cut and run: a fallback source and the Output preview's texture, drawn over
  by another target, stay through the 9 presents after they were shown (another texture shown, retirement asked), then
  go; a dropped output and a present of nothing have no source (present_source cleared); the hold is longer than the
  frame slots (kPresentSlotsMax).
- tests/test_target_retention.cpp: case 3, the eviction pass leaves a texture a present showed in the 9 presents before
  (its storage written over), then lets it go; case 4, in the compaction's hold such a dead target is not counted among
  what the dead targets give back, so a live texture long unused goes for the excess in its place.
- tests/test_vram_compact.py: compact_step does not move a texture a present showed lately.

These fail on main (the fix is the next commit). From dyst's review of #1996 (comment 65721), which asked for the hold as a
fix of its own.

Assisted-by: Claude Code
fix(vk): hold the texture a present shows until the window has copied it
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
a9d04a2e16
texture_for_present marks the texture it picks (Texture::presented), its fallback in another description and the Output
preview's own texture too. The eviction pass (and its first pass for the compaction's hold, which counts what the dead
targets give back), dead_targets_sweep, reuse_retire_frame and compact_step leave a texture a present showed in the last
dead_targets::kPresentHold (9) presents: the window's copy is done only when its frame slot's fence is waited,
kPresentSlotsMax (8) presents later at most, and after that present's texture_frame (static_assert). So its Texture, image
and views stay while the present's recording, the headless and frame-generation paths and the window's copy read them.

do_present sends a present of nothing and a dropped output through texture_for_present(nullptr): no texture is the source,
and present_source no longer names the one shown before.

Assisted-by: Claude Code
Author
Contributor

@Beanstalk This fixes which image the renderer presents from and keeps it held: the present source in gpu/vk/vk_texture.cpp (texture_for_present), the dead-target sweep, eviction, retirement and compaction. No switch: a revert gives back main's code. Please take each picture at a fixed point with no input: right after the start save loads and settles, before any stick input; and take one main-against-itself picture on the same PC too, so the noise level shows. Then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each with the camera turning, and open the Output resolution preview from the menu once; the PR against main on the same PC, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main within main's own noise, no crash, device lost or FATAL line; frame time p99 and peak VRAM equal to main's or better, within the run-to-run spread.

@Beanstalk This fixes which image the renderer presents from and keeps it held: the present source in gpu/vk/vk_texture.cpp (texture_for_present), the dead-target sweep, eviction, retirement and compaction. No switch: a revert gives back main's code. Please take each picture at a fixed point with no input: right after the start save loads and settles, before any stick input; and take one main-against-itself picture on the same PC too, so the noise level shows. Then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each with the camera turning, and open the Output resolution preview from the menu once; the PR against main on the same PC, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main within main's own noise, no crash, device lost or FATAL line; frame time p99 and peak VRAM equal to main's or better, within the run-to-run spread.
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 is marked as a work in progress.
Some workflows are waiting to be reviewed.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/vk-present-source-held:siantm-fix/vk-present-source-held'
git switch 'siantm-fix/vk-present-source-held'
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!2064
No description provided.