GPU: retire regenerable memory under pressure #3848

Open
kotekompotek wants to merge 1 commit from upstream-pr/3848 into main
kotekompotek commented 2026-11-29 15:45:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-29T15:45:07Z.


Summary

  • Make host textures evictable when their contents can be reconstructed, rather than excluding every texture whose flags make it GPU-writable. A texture that has never actually been written on the host starts empty/with its clear value or can be uploaded from title memory again; one whose texels exist only on the host is still pinned. Render-target, depth, clear and unordered-access writes are now marked at the submission that performs them.
  • Eviction pressure now considers both the local video-memory budget and total system commit. Under pressure the least recently used reconstructible textures go first, with a short grace for copies still held by a recording list whose write marks may not have landed yet.
  • Give loose shadow-copy windows a frame-stamped lifecycle. Copies idle for 300 presented frames are retired least recently used at submission boundaries when live loose memory crosses 256 MiB, until it falls below 192 MiB; a 512 MiB hard bound covers the pathological all-recent case. Retirement/removal bumps the shadow generation so computed-title-memory page tables are rebuilt, while lists and heap slots keep their own references to copies already in use.
  • Track every synthetic resource generation made for title-placed texture memory. When the runtime reports memory unmapped/remapped, overlapping loose copies and placed-texture cache entries are invalidated after the watch-guard lock is dropped. Placed invalidation nulls heap slots viewing the old host copies, prunes matching shared-memory weak-map entries so stale copies cannot be resurrected, poisons the lazy host-copy slot, and defers the cache-owned object release for 300 presented frames as a conservative guard against CPU-side lookups already in flight.
  • Stop descriptor heap sync scans at the number of title slots representable in the capped host heap; slots past the cap have no host slot to synchronize.
  • Extend the existing periodic memory lines with eviction/remake, system-commit and loose-copy counters.

Leak mechanisms addressed

Before this change:

  • loose copies of title memory outside buffer objects were strong Arcs in a static map and were never freed;
  • synthetic resources made for textures placed directly in title memory were cached forever, keeping resource objects, host-texture sharing entries and descriptor-held resources alive after streaming reuse;
  • any texture whose host flags allowed render-target, depth or unordered access was ineligible for eviction even if the GPU had never written it, so streaming created ever more reconstructible host textures with no path back;
  • heap sync could scan hundreds of thousands of title slots beyond the capped host heap.

Validation

  • cargo check -p victory-gpu — pass (MSVC 1.98.1).
  • cargo check -p victory-gpu --features diagnostics — pass.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings — pass.
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings — pass.
  • cargo test -p victory-gpu --lib — 226 passed, 5 failed, 1 ignored. A clean upstream-main baseline run on the same machine produced exactly the same 226/5/1 result. The five pre-existing environment-dependent failures are dynamic_slots_are_held_until_released, a_texture_written_in_one_format_reads_in_another_through_a_placed_view, compute_view_marks_rebind_after_depth_twin_conversion, lists_share_a_table_and_copies_keep_their_views, and depth_bounds_control_a_title_draw on the GTX 1050 test system. The updated eviction unit test passes.

Stress-boot evidence from the investigation build of these changes:

  • the boot survived the full eight-minute window instead of dying in the streaming transition; comparable earlier boots reached allocation failure there (one partially mitigated reproduction died around frame 4600, while an older no-eviction final sample showed 10,126 MB process commit and 1,702 MB of host textures still growing);
  • frame=8192 draws=65536 drawn=8192 was reached and presentation continued through the end of the window;
  • 1,824 host textures / 4,724 MB were evicted, leaving only 4 host textures / 167 MB live in the final samples;
  • process commit plateaued at 9,454 MB after the streaming burst (the peak periodic sample during the burst was 12,645 MB), with no Rust allocation failure, panic or exception;
  • the loose-copy LRU retired 13 copies / 71 MB and ended at 46 live copies / 214 MB.

That boot exercised the texture-eviction and loose-LRU paths heavily. It did not report title-memory remap callbacks, so the new remap-invalidation paths are included by design and compile/clippy validated but were not separately runtime-proven by this particular log.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5552332968 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3848). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-29T15:45:07Z. --- ## Summary - Make host textures evictable when their contents can be reconstructed, rather than excluding every texture whose flags make it GPU-writable. A texture that has never actually been written on the host starts empty/with its clear value or can be uploaded from title memory again; one whose texels exist only on the host is still pinned. Render-target, depth, clear and unordered-access writes are now marked at the submission that performs them. - Eviction pressure now considers both the local video-memory budget and total system commit. Under pressure the least recently used reconstructible textures go first, with a short grace for copies still held by a recording list whose write marks may not have landed yet. - Give loose shadow-copy windows a frame-stamped lifecycle. Copies idle for 300 presented frames are retired least recently used at submission boundaries when live loose memory crosses 256 MiB, until it falls below 192 MiB; a 512 MiB hard bound covers the pathological all-recent case. Retirement/removal bumps the shadow generation so computed-title-memory page tables are rebuilt, while lists and heap slots keep their own references to copies already in use. - Track every synthetic resource generation made for title-placed texture memory. When the runtime reports memory unmapped/remapped, overlapping loose copies and placed-texture cache entries are invalidated after the watch-guard lock is dropped. Placed invalidation nulls heap slots viewing the old host copies, prunes matching shared-memory weak-map entries so stale copies cannot be resurrected, poisons the lazy host-copy slot, and defers the cache-owned object release for 300 presented frames as a conservative guard against CPU-side lookups already in flight. - Stop descriptor heap sync scans at the number of title slots representable in the capped host heap; slots past the cap have no host slot to synchronize. - Extend the existing periodic memory lines with eviction/remake, system-commit and loose-copy counters. ## Leak mechanisms addressed Before this change: - loose copies of title memory outside buffer objects were strong `Arc`s in a static map and were never freed; - synthetic resources made for textures placed directly in title memory were cached forever, keeping resource objects, host-texture sharing entries and descriptor-held resources alive after streaming reuse; - any texture whose host flags allowed render-target, depth or unordered access was ineligible for eviction even if the GPU had never written it, so streaming created ever more reconstructible host textures with no path back; - heap sync could scan hundreds of thousands of title slots beyond the capped host heap. ## Validation - `cargo check -p victory-gpu` — pass (MSVC 1.98.1). - `cargo check -p victory-gpu --features diagnostics` — pass. - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` — pass. - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings` — pass. - `cargo test -p victory-gpu --lib` — 226 passed, 5 failed, 1 ignored. A clean upstream-main baseline run on the same machine produced exactly the same 226/5/1 result. The five pre-existing environment-dependent failures are `dynamic_slots_are_held_until_released`, `a_texture_written_in_one_format_reads_in_another_through_a_placed_view`, `compute_view_marks_rebind_after_depth_twin_conversion`, `lists_share_a_table_and_copies_keep_their_views`, and `depth_bounds_control_a_title_draw` on the GTX 1050 test system. The updated eviction unit test passes. Stress-boot evidence from the investigation build of these changes: - the boot survived the full eight-minute window instead of dying in the streaming transition; comparable earlier boots reached allocation failure there (one partially mitigated reproduction died around frame 4600, while an older no-eviction final sample showed 10,126 MB process commit and 1,702 MB of host textures still growing); - `frame=8192 draws=65536 drawn=8192` was reached and presentation continued through the end of the window; - 1,824 host textures / 4,724 MB were evicted, leaving only 4 host textures / 167 MB live in the final samples; - process commit plateaued at 9,454 MB after the streaming burst (the peak periodic sample during the burst was 12,645 MB), with no Rust allocation failure, panic or exception; - the loose-copy LRU retired 13 copies / 71 MB and ended at 46 live copies / 214 MB. That boot exercised the texture-eviction and loose-LRU paths heavily. It did not report title-memory remap callbacks, so the new remap-invalidation paths are included by design and compile/clippy validated but were not separately runtime-proven by this particular log.
kilolimaVI commented 2026-11-29 16:40:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-29T16:40:26Z.


Tested on a Radeon 780M (RDNA3 iGPU, shared system memory, Ryzen 7 8845HS), merged onto main 63bcca4, cargo test --profile diag -p victory-gpu --features diagnostics --lib, run twice. main passes all 246 both times; with this PR, three existing tests fail, a different set each run:

  • run 1: bindless::tests::heap_views_are_current
  • run 2: bindless::tests::heap_views_follow_writes_through_another_mapping, shadow::tests::partial_refresh_under_marked_range_updates_saved_bytes

#3831 and #3832 on the same machine and base pass every test both runs, so this looks specific to this change. Since the failures move between runs, my guess is the new eviction: on an iGPU the system commit pressure check may trip during the test run and retire loose shadow copies (or poison placed resources) that a test still reads. A dGPU with its own VRAM might never hit it. Happy to rerun with a log or a patch.

AI-assisted (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7259088056 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3848#issuecomment-7259088056). Originally posted by [kilolimaVI](https://github.com/kilolimaVI) on 2026-11-29T16:40:26Z. --- Tested on a Radeon 780M (RDNA3 iGPU, shared system memory, Ryzen 7 8845HS), merged onto main 63bcca4, `cargo test --profile diag -p victory-gpu --features diagnostics --lib`, run twice. main passes all 246 both times; with this PR, three existing tests fail, a different set each run: - run 1: `bindless::tests::heap_views_are_current` - run 2: `bindless::tests::heap_views_follow_writes_through_another_mapping`, `shadow::tests::partial_refresh_under_marked_range_updates_saved_bytes` #3831 and #3832 on the same machine and base pass every test both runs, so this looks specific to this change. Since the failures move between runs, my guess is the new eviction: on an iGPU the system commit pressure check may trip during the test run and retire loose shadow copies (or poison placed resources) that a test still reads. A dGPU with its own VRAM might never hit it. Happy to rerun with a log or a patch. AI-assisted (Claude).
AurumCarlos commented 2026-11-29 20:54:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-29T20:54:22Z.

Review: COMMENTED


Three points, the first two concrete:

1. The descriptor.rs hunk is #3512's change. sync_heaps now clamps last at host.count(), which is what #3512 (neonbyte1, open) does in the same lines. #3512 also clamps first and stops sync_slots early, and it has measurements. The two will conflict. Suggested: drop that hunk here and leave it to #3512.

2. Eviction now depends on the whole machine's commit charge, during tests too. evict_unused runs at every Host::execute (host.rs:1168), tests included. With this PR it also evicts when K32GetPerformanceInfo's CommitTotal (every process on the system) is above 85 % of CommitLimit (resource.rs:1599, COMMIT_HIGH). So whether a test's textures are given up mid-test depends on what else the machine is doing. That fits kilolimaVI's report: three different tests failing in two runs on the same machine, while main passes all 246 both times. I haven't reproduced those failures, so take this as the likeliest lead rather than a proven cause.

Suggested fix: keep the pressure sources out of unit tests, so tests that want eviction call evict(frame, now, over) with the over they mean:

let commit = if cfg!(test) {
    0
} else {
    system_commit().map_or(0, |(used, limit)| {
        if used > COMMIT_HIGH * limit { (used - COMMIT_LOW * limit) as u64 } else { 0 }
    })
};

The same applies to local (the card's budget), which on an iGPU is shared memory too.

3. Splitting would make the failures bisectable. This PR has five independent changes: texture evictability, commit pressure, the loose-copy lifecycle, remap invalidation of placed textures, and the heap-sync clamp. With all five in one commit, the flaky failures can't be traced to one of them. The loose-copy lifecycle also overlaps #3078 and #3457, which retire loose windows and shadow copies too. One PR per change, starting with the eviction rules, would let each be tested and measured on its own.

<!-- greyfinch-sync:Marvisak/VIctory:review:6104757456 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3848#pullrequestreview-6104757456). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-29T20:54:22Z. **Review:** COMMENTED --- Three points, the first two concrete: **1. The `descriptor.rs` hunk is #3512's change.** `sync_heaps` now clamps `last` at `host.count()`, which is what #3512 (neonbyte1, open) does in the same lines. #3512 also clamps `first` and stops `sync_slots` early, and it has measurements. The two will conflict. Suggested: drop that hunk here and leave it to #3512. **2. Eviction now depends on the whole machine's commit charge, during tests too.** `evict_unused` runs at every `Host::execute` (`host.rs:1168`), tests included. With this PR it also evicts when `K32GetPerformanceInfo`'s `CommitTotal` (every process on the system) is above 85 % of `CommitLimit` (`resource.rs:1599`, `COMMIT_HIGH`). So whether a test's textures are given up mid-test depends on what else the machine is doing. That fits kilolimaVI's report: three different tests failing in two runs on the same machine, while `main` passes all 246 both times. I haven't reproduced those failures, so take this as the likeliest lead rather than a proven cause. Suggested fix: keep the pressure sources out of unit tests, so tests that want eviction call `evict(frame, now, over)` with the `over` they mean: ```rust let commit = if cfg!(test) { 0 } else { system_commit().map_or(0, |(used, limit)| { if used > COMMIT_HIGH * limit { (used - COMMIT_LOW * limit) as u64 } else { 0 } }) }; ``` The same applies to `local` (the card's budget), which on an iGPU is shared memory too. **3. Splitting would make the failures bisectable.** This PR has five independent changes: texture evictability, commit pressure, the loose-copy lifecycle, remap invalidation of placed textures, and the heap-sync clamp. With all five in one commit, the flaky failures can't be traced to one of them. The loose-copy lifecycle also overlaps #3078 and #3457, which retire loose windows and shadow copies too. One PR per change, starting with the eviction rules, would let each be tested and measured on its own.
passerbyyhx commented 2026-11-30 23:12:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-30T23:12:31Z.


I reproduced both bindless failures reported above on 856f65355761be639acf46004fdbf90ad135bc8e on an RTX 4090 Laptop GPU and Microsoft WARP. I varied the inputs to the production eviction decision while running each existing test in its own process. The high values below are injected inputs, not observations from the Radeon 780M.

Pressure input heap_views_are_current and heap_views_follow_writes_through_another_mapping, both adapters combined
Live readings, below threshold, or exactly 85% system commit / 90% adjusted local usage 48/48 passed
Only system commit above 85% (85.01%), or only adjusted local usage above 90% (90.01%) 24/24 failed on round 0
Same high inputs, holding only the texture in slot 2 through submission 8/8 passed

Before the failing dispatch, slot 2 held the shader texture and its title texel was 64. Eviction selected that texture (65,536 bytes), wrote a null view to the same slot, and no remake occurred before dispatch. All 64 output words were 1000+i instead of 1064+i. Keeping just this texture alive restored all three rounds while the pressure calculation still requested the same reclamation.

The separate shadow partial-refresh test passed 24/24 in isolation and never polled eviction pressure. If the 780M failure recurs, the most useful details would be which test failed, the first mismatched word/byte, system commit and adjusted local usage at that submission, and whether it also fails when run alone.

A candidate for the two bindless failures is now open against your branch, commit 3397c6f356654ee89f043ac7418fb13357d2e48b. Both adapters passed the same 24 high-pressure runs with actual eviction and no pin. The targeted matrix passed 84/84, including the 48 live/low/boundary bindless runs and 12 placed-resource controls. The fix repairs evicted descriptor generations in heaps referenced by the submitted lists. In a separate two-slot control, a previously materialized but unsampled texture on the same heap was rebuilt and uploaded on the first pressured submission. It had no additional upload on the second. Active-heap memory savings have not been quantified.

I also reran the stock partial-refresh shadow test in 30 new test processes on the original SHA, including four full-library runs (serial and default scheduling on both adapters). The named test passed in all 30. Direct byte readback in the earlier instrumented probe was aa 66 00 11 bb 77. If it recurs on 780M, could you share the exact assertion line and actual bytes, plus whether the same test fails with --test-threads=1 or in isolation? That would let us distinguish partial copy length from incorrect saved bytes.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7294841758 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3848#issuecomment-7294841758). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-30T23:12:31Z. --- I reproduced both bindless failures reported above on `856f65355761be639acf46004fdbf90ad135bc8e` on an RTX 4090 Laptop GPU and Microsoft WARP. I varied the inputs to the production eviction decision while running each existing test in its own process. The high values below are injected inputs, not observations from the Radeon 780M. | Pressure input | `heap_views_are_current` and `heap_views_follow_writes_through_another_mapping`, both adapters combined | |---|---| | Live readings, below threshold, or exactly 85% system commit / 90% adjusted local usage | 48/48 passed | | Only system commit above 85% (85.01%), or only adjusted local usage above 90% (90.01%) | 24/24 failed on round 0 | | Same high inputs, holding only the texture in slot 2 through submission | 8/8 passed | Before the failing dispatch, slot 2 held the shader texture and its title texel was 64. Eviction selected that texture (65,536 bytes), wrote a null view to the same slot, and no remake occurred before dispatch. All 64 output words were `1000+i` instead of `1064+i`. Keeping just this texture alive restored all three rounds while the pressure calculation still requested the same reclamation. The separate shadow partial-refresh test passed 24/24 in isolation and never polled eviction pressure. If the 780M failure recurs, the most useful details would be which test failed, the first mismatched word/byte, system commit and adjusted local usage at that submission, and whether it also fails when run alone. A candidate for the two bindless failures is now [open against your branch](https://github.com/kotekompotek/VIctory/pull/1), commit `3397c6f356654ee89f043ac7418fb13357d2e48b`. Both adapters passed the same 24 high-pressure runs with actual eviction and no pin. The targeted matrix passed 84/84, including the 48 live/low/boundary bindless runs and 12 placed-resource controls. The fix repairs evicted descriptor generations in heaps referenced by the submitted lists. In a separate two-slot control, a previously materialized but unsampled texture on the same heap was rebuilt and uploaded on the first pressured submission. It had no additional upload on the second. Active-heap memory savings have not been quantified. I also reran the stock partial-refresh shadow test in 30 new test processes on the original SHA, including four full-library runs (serial and default scheduling on both adapters). The named test passed in all 30. Direct byte readback in the earlier instrumented probe was `aa 66 00 11 bb 77`. If it recurs on 780M, could you share the exact assertion line and actual bytes, plus whether the same test fails with `--test-threads=1` or in isolation? That would let us distinguish partial copy length from incorrect saved bytes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3848:upstream-pr/3848'
git switch 'upstream-pr/3848'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3848'
git switch 'upstream-pr/3848'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3848'
git switch 'upstream-pr/3848'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3848'
git switch 'main'
git merge --squash 'upstream-pr/3848'
git switch 'main'
git merge --ff-only 'upstream-pr/3848'
git switch 'main'
git merge 'upstream-pr/3848'
git push origin 'main'
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
Marvisak/VIctory!3848
No description provided.