GPU: use smaller staging chunks after allocation failure #3259

Open
github-bot wants to merge 2 commits from github-bot/gh-3259 into main AGit
Owner

@⁠yumaste on GitHub · 2026-11-26 14:48 UTC · original
yumaste:fix/texture-staging-smaller-chunks → main · +186 −21 · 2 files · 2 commits · open

D3D12 texture staging currently requests a 32 MiB upload buffer whenever the current chunk fills. Under memory pressure, the driver can reject that allocation even though a smaller buffer could still hold the pending upload.

Try progressively smaller chunks down to the size required by the upload, rounded to 64 KiB. Reuse any space in existing chunks first, keep the successful smaller growth size for the current submission, and retry the default size after recycling. If smaller chunks would crowd out larger ones, recycling keeps the 16 largest so full-size capacity can return after memory pressure subsides. If all sizes fail, the allocator still returns None, which preserves the interface used by the retry path proposed in #3174. The fallback uses the same D3D12 upload heap and has no adapter-vendor condition.

Validation

  • With a simulated 2 MiB per-allocation ceiling, the previous 32 MiB request would fail on all 64 uploads. The patched allocator accepts all 64 with four rejected growth attempts total.
  • WARP GPU suite passed with 250 passed and one ignored. The smaller-buffer suballocation test also passed on physical NVIDIA RTX 5080 Laptop and Intel graphics. The existing heap-texture upload test passed on the NVIDIA adapter. No AMD hardware or game assets were used.
  • Verified trial merges with #3174, #3136, #3230, and #3228.

Inspired by codec_d's staging growth backoff in cool_fox. Coordination comment.

**@⁠yumaste** on GitHub · 2026-11-26 14:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3259) `yumaste:fix/texture-staging-smaller-chunks` → `main` · +186 −21 · 2 files · 2 commits · open D3D12 texture staging currently requests a 32 MiB upload buffer whenever the current chunk fills. Under memory pressure, the driver can reject that allocation even though a smaller buffer could still hold the pending upload. Try progressively smaller chunks down to the size required by the upload, rounded to 64 KiB. Reuse any space in existing chunks first, keep the successful smaller growth size for the current submission, and retry the default size after recycling. If smaller chunks would crowd out larger ones, recycling keeps the 16 largest so full-size capacity can return after memory pressure subsides. If all sizes fail, the allocator still returns `None`, which preserves the interface used by the retry path proposed in #3174. The fallback uses the same D3D12 upload heap and has no adapter-vendor condition. Validation - With a simulated 2 MiB per-allocation ceiling, the previous 32 MiB request would fail on all 64 uploads. The patched allocator accepts all 64 with four rejected growth attempts total. - WARP GPU suite passed with 250 passed and one ignored. The smaller-buffer suballocation test also passed on physical NVIDIA RTX 5080 Laptop and Intel graphics. The existing heap-texture upload test passed on the NVIDIA adapter. No AMD hardware or game assets were used. - Verified trial merges with #3174, #3136, #3230, and #3228. Inspired by codec_d's [staging growth backoff in cool_fox](https://github.com/DoubleGhosty/cool_fox/commit/ab75ac3e02ba9fe5b45297c6254bafe5660a1a68). [Coordination comment](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:55:59 +00:00
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 14:50 UTC · original · reviewed

The backoff in take looks right: it always ends (at the latest once bytes == minimum fails), and both tests trace correctly. One concern:

Small chunks stick around after the pressure is gone. recycle still does all.truncate(CHUNKS_KEPT), which keeps the first 16 chunks in creation order, so chunks created small during a refusal stay at the front for good. After that:

  • the bump pointer walks the kept chunks in order, and a request bigger than a small chunk skips it, so that chunk goes unused for the rest of the submission;
  • the kept set can hold much less than 16 × 32 MiB, so later submissions overflow it, allocate fresh 32 MiB chunks past index 16, and drop them at the next recycle. That's the per-submission allocation churn the chunks exist to avoid.

Dropping kept chunks smaller than CHUNK in recycle (or keeping the 16 largest) would fix both. A test that does recycle after a refusal and checks the next submission doesn't reallocate would cover it.

**@⁠AurumCarlos** on GitHub · 2026-11-26 14:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3259#pullrequestreview-6076181618) · **reviewed** The backoff in `take` looks right: it always ends (at the latest once `bytes == minimum` fails), and both tests trace correctly. One concern: **Small chunks stick around after the pressure is gone.** `recycle` still does `all.truncate(CHUNKS_KEPT)`, which keeps the *first* 16 chunks in creation order, so chunks created small during a refusal stay at the front for good. After that: - the bump pointer walks the kept chunks in order, and a request bigger than a small chunk skips it, so that chunk goes unused for the rest of the submission; - the kept set can hold much less than 16 × 32 MiB, so later submissions overflow it, allocate fresh 32 MiB chunks past index 16, and drop them at the next `recycle`. That's the per-submission allocation churn the chunks exist to avoid. Dropping kept chunks smaller than `CHUNK` in `recycle` (or keeping the 16 largest) would fix both. A test that does `recycle` after a refusal and checks the next submission doesn't reallocate would cover it.
Author
Owner

@⁠yumaste on GitHub · 2026-11-26 15:08 UTC · original

Addressed in f034d89. Recycling now retains the 16 largest chunks if smaller entries would otherwise displace them, and can still reuse small chunks while they are the only ones available. The regression starts with 16 small chunks, lets 16 full-size allocations succeed after recovery, then verifies that the next submission reuses all 16 without allocating. The WARP GPU suite passed (250 passed, one ignored).

**@⁠yumaste** on GitHub · 2026-11-26 15:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3259#issuecomment-7195833856) Addressed in f034d89. Recycling now retains the 16 largest chunks if smaller entries would otherwise displace them, and can still reuse small chunks while they are the only ones available. The regression starts with 16 small chunks, lets 16 full-size allocations succeed after recovery, then verifies that the next submission reuses all 16 without allocating. The WARP GPU suite passed (250 passed, one ignored).
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 15:30 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Halving staging chunk allocation size down to minimum on failure, resetting growth on recycle(), and retaining the largest CHUNKS_KEPT chunks via select_nth_unstable_by_key once larger allocations recover is clean and well-tested.
  • cargo test -p victory-gpu --lib texture:: -- --test-threads=1 (RTX 5090): 6 passed, 0 failed (including all 4 staging_tests).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 15:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/3259#pullrequestreview-6076791888) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Halving staging chunk allocation size down to `minimum` on failure, resetting `growth` on `recycle()`, and retaining the largest `CHUNKS_KEPT` chunks via `select_nth_unstable_by_key` once larger allocations recover is clean and well-tested. - `cargo test -p victory-gpu --lib texture:: -- --test-threads=1` (RTX 5090): 6 passed, 0 failed (including all 4 `staging_tests`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 192 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3259/head:github-bot/gh-3259'
git switch 'github-bot/gh-3259'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3259'
git switch 'github-bot/gh-3259'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3259'
git switch 'github-bot/gh-3259'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3259'
git switch 'main'
git merge --squash 'github-bot/gh-3259'
git switch 'main'
git merge --ff-only 'github-bot/gh-3259'
git switch 'main'
git merge 'github-bot/gh-3259'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3259
No description provided.