Skip to content

GPU: free host textures only after running submissions, and make them on first use - #431

Closed
AntoninMichelq wants to merge 2 commits into
Marvisak:mainfrom
AntoninMichelq:up/lazy-host-textures
Closed

AntoninMichelq wants to merge 2 commits into
Marvisak:mainfrom
AntoninMichelq:up/lazy-host-textures

Conversation

@AntoninMichelq

Copy link
Copy Markdown

Requested by 4nto · project thread

Summary

Two changes to host texture lifetimes. Together they stop a GPU hang in the world and halve host texture memory on an 8 GB card.

  1. Free textures and slot resources only after the running submissions. A released title texture used to drop its host copy right away. A rewritten descriptor slot did the same with the resource it held. Both happened while a submission that still read them was running, sometimes for most of a second in the world. On an RX 7600, device-removed data (DRED) showed a page fault inside a freed 1024x1024 BC7 texture about 65 s into the world, ending in DXGI_ERROR_DEVICE_HUNG. These objects now go to a retired list, tagged with the submission count. They are dropped once that submission has been waited for, or right away if none is running.
  2. Make a texture's host copy on first use, not at creation. In the world the title creates thousands of textures, and many are never used by the GPU (no view, target, copy or barrier). At the world's peak there were 3,630 host textures, 8.9 GB, against a 7 GB video memory budget. The driver moves the excess to system memory, where it counts against the commit limit. The creation state and clear value are now kept, and the host copy is made on first use (OnceLock).

What changed

  • host.rs: retire() and the retired list, emptied after each waited submission.
  • descriptor/host.rs: HostHeap::keep retires the old resource instead of dropping it.
  • resource.rs:
    • Resource.host becomes a OnceLock made by host() on first use, from the kept made_with (state, clear value).
    • Drop retires the host copy.

This overlaps a little with #135, which keeps replaced resources for some frames. This PR covers texture releases and slot rewrites tied to the submission that reads them, and does not depend on #135.

How it was tested

  • cargo check --tests -p victory-gpu and cargo check -p victory for x86_64-pc-windows-msvc (with CARGO_FEATURE_PURE=1) pass.
  • In the game on an RX 7600 (Windows 11, 32 GB RAM), on an integration stack that includes these two commits:
    • Hang: before change 1, every world run hung at 50 to 65 s. After it, the next runs went 1 min 40 s to about 4 min with no hang. A later hang came from another freed buffer, which another branch handles.
    • Memory, one minute into the world: host textures went from 8.8 to 4.6 GB, peak commit from 24.3 to 17.4 GB, and video memory from 9.6 of 7.0 GB budget to 5.2 of 7.4. No missing objects were seen.

Where 3D rendering stands on an RX 7600 (Windows 11)

  • In the world's first cutscene, props, characters, cables and vehicle tyres render (seen in a G-buffer view).
  • Ground, roads, buildings, textures, sky and lighting are still missing: the lighting output is black.
  • No GPU hang in the latest world runs of several minutes. About 10,000 draws per frame, at well under 1 fps.

These observations come from the integration stack (upstream main plus many open PRs), not from this branch alone.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66

AntoninMichelq and others added 2 commits November 24, 2026 03:13
The title's descriptor writes and texture releases reach the host at
once, from its own threads, while the host runs a submission for most of
a second in the world. A descriptor slot rewritten then dropped the
resource it held, and a released title texture dropped its host copy,
while the running submission still read them: device-removed data showed
a page fault inside a freed 1024x1024 BC7 texture, about 65 s into the
world (DXGI_ERROR_DEVICE_HUNG, RX 7600).

Those objects now go to a retired list tagged with the submission count
and are dropped when that submission has been waited for (at once if
none is running).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSD61BBk7E79WkryEQA
The world's peak counted 3,630 host textures, 8.9 GB, against a 7 GB
video memory budget on an 8 GB card: the driver moves the excess to
system memory, where it counts against the commit limit. A texture the
title makes and the GPU never uses (no view, target, copy or barrier)
now costs nothing; the creation state and clear value are kept for the
copy made on first use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSCvCZuokASx7f59sAG
@DaviTostes

Copy link
Copy Markdown

Verified 23ced70 on Arch Linux x86_64, Windows MSVC release build under Wine 11.22 (victory-gpu, --test-threads=1): 43 passed, 0 failed, 1 ignored (same as main d2a4bae, no new test in this head).

Not covered: a title freeing and re-creating textures across submissions (no game data here); the suite has no synthetic case for that lifetime.

@desplmfao

Copy link
Copy Markdown
Contributor

NVIDIA / Linux data point for this, since it was tested on an RX 7600.

RTX 5080 16 GB, CachyOS, proton-cachyos (vkd3d-proton 3.2.0), Series X 1.0.2.1, diagnostics build of main d2a4bae + this + #493 (+#336), #185, #436, #462, #440, #514 (+#464), #515, #259, #177, #151, story mode by VICTORY_KEYS. One run, 9 min (about 7 min in the world), stopped by the timer:

  • No GPU fault or hang, nothing in crashes.log, no new warnings in the log.
  • Video memory in use (nvidia-smi, every 10 s) stayed at 9.6–9.7 GB in the world. Earlier runs on this machine without this PR were at 14–15 GB at the same point, though those were older builds of the stack, so the saving is not measured on one build.
  • vkd3d-proton's Memory allocation failed, falling back to system memory came about as often as without it (215 vs 224 lines, in the same bursts while the world loads), so on this card those are not from running out of video memory as a whole; presumably the CPU-visible heap the shadow buffers live in. Frame time was unchanged (median 3.0 s per world frame here, which is the copy path).

AI-assisted (Claude Code); numbers from the run's log and nvidia-smi.

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 23ced70 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed). Not tested in game, so I can't confirm the DRED fault or the memory numbers.

Retirement:

  • execute holds submitted from start to finish, so submissions are serial. STARTED is incremented under that lock, and FINISHED/bury run after the wait with no early return in between. An object retired while submission N runs is tagged N and dropped after N's wait.
  • If the wait fails (the "host submission not waited for" path), bury still drops. That only happens after device removal, so it is acceptable.
  • Objects retired when no submission is running are dropped at once, so nothing waits on a submission that never comes.

Lazy host copies:

  • OnceLock::get_or_init makes the first use race-free across title threads. made_with copies the 20-byte clear value during the call, as it must.
  • Drop takes the copy with take().flatten(), so a texture that was never used creates nothing on release.
  • texture_at calls host() and so creates the copy on an address lookup. That is correct, though it means address lookups count as "first use".

No issues found.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…nning submissions, and make them on first use

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mesrine67

Copy link
Copy Markdown

Windows 11 / AMD Radeon RX 6750 XT (RDNA2, 12 GB) validation update:

  • Combined this PR head 23ced703 with PR GPU: give table slots without a view a null descriptor of the kind the shader reads #568 head 0845e8d5 in a clean staged integration (tree d5e66d81ff8a78307c41b6d079101b7d3859f5a7).
  • cargo test --workspace --locked -- --test-threads=1: 268 passed, 4 ignored, 0 failed.
  • cargo build --profile diag --locked -p victory --features diagnostics: passed.
  • Safe loader preflight without --execute: four title DLLs bound, zero missing imports; the log explicitly confirms that the title was not executed.

This confirms a Windows build/test integration only. I did not run the title or exercise the GPU lifetime path, so this is not an in-game validation of the fix on the RX 6750 XT.

@SkillIssueXO

Copy link
Copy Markdown

Native Windows in-game comparison on an RTX 5070 Ti 16 GB (621.24), Windows 11 26300.9974, Ryzen 7 9850X3D and 32 GB of RAM.

Both diagnostics builds used main 8e79c80 + #493 c48b240, #206 05981dd and #189 d0fd9bf. Series S mode, same saved state and startup input, separate empty caches, default shader loop limit and DRED off. The second build added both commits of this PR (133609b and 23ced70).

Without #431 With #431
Sample time after first world FPS line 2.28 s 2.52 s
Dedicated GPU process memory 7.63 GiB 2.97 GiB
Shared GPU process memory 1.68 GiB 1.63 GiB
VIctory private bytes 16.24 GiB 10.89 GiB
System commit headroom 2.95 GiB 7.94 GiB
Span of world FPS samples before the guard stopped the run 5.43 s 172.66 s

Both runs were stopped by the same 3 GiB system commit-headroom guard. One run per build, with extra debug logging of shadow allocations in the control. The figures come from Windows process and GPU process memory counters. The samples show lower dedicated GPU memory and private bytes near world entry. They do not isolate the reason for the private-byte difference or establish a frame-rate improvement.

AI-assisted (Codex, with Claude reviewing the memory paths).

@d1ll3x3

d1ll3x3 commented Nov 24, 2026

Copy link
Copy Markdown

checked: #431 on RDNA3 | base: main@8e79c80 + #431 + #568 + #67 (the three apply cleanly together; #431 conflicts with #609, so this run has no #609) | on: Windows 11, RX 7800 XT (RDNA3, 16 GB), 32 GB RAM, diag build, VICTORY_D3D12_DRED=1, Xbox Series 1.0.2.1

result: the longest run we have had: 11 min 39 s in the first mission's intro cutscene (0–1 fps, about 900–6,000 draws/frame), against 2 min 39 s – 5 min 16 s with #609 + #568 + #67 on the same base and machine. It still ends in a device removal of the same family:

page fault at 0x50eb40000; inside freed ["texture 2048x2048 f78 flags 0x0 (type 34)", "title buffer 0x74a000000+0x44ff60 (type 34)"]
stopped at command 281 of 370 (op 42)

The freed allocations now include a title buffer (a shadow copy), which #609 retires but #431 does not. Locally I ported #609's two Drop impls (Shadow and Staging hand their buffer to #431's retire) on top of #431; one run of that went 4 min in the cutscene with no device removal before the window was closed, so not conclusive yet.

AI-assisted (Claude Code).

@d1ll3x3

d1ll3x3 commented Nov 24, 2026

Copy link
Copy Markdown

follow-up: #431 + #609's buffer/staging retirement | base: main@8e79c80 + #431 + #568 + #67, plus #609's two Drop impls ported onto #431's retire (below) | on: Windows 11, RX 7800 XT (RDNA3, 16 GB), 32 GB RAM, diag build, VICTORY_D3D12_DRED=1, Xbox Series 1.0.2.1

result: 18 min 29 s (2,160 frames) in the first mission's intro cutscene, against 11 min 39 s with #431 alone on the same base and machine, and no "inside freed" signature any more. Neither #431 nor #609 alone got rid of it here.

What was ported (only these two, everything else is #431 as is):

// texture.rs
#[cfg(windows)]
impl Drop for Staging {
    fn drop(&mut self) {
        crate::host::retire(self.buffer.clone());
    }
}

// shadow.rs
#[cfg(windows)]
impl Drop for Shadow {
    fn drop(&mut self) {
        crate::host::retire(self.buffer.clone());
    }
}

The run then ends with a different removal, not a freed allocation:

page fault at 0x1ae1dc000; no title memory copy below it
stopped at command 193 of 223 (op 6): dispatch 48x48x1 [compute 4ceef06fab7b49dc]

That dispatch reads a 768x768 texture and writes a 384x384 one (format 10), so it looks like a downsample pass. Skipping it with VICTORY_SKIP_PIPELINES gave a short run (1 min 27 s) ending in a device hang without a page fault on a mesh draw (op 42, mesh 04b9be3756e8f41c, pixel 1af4a50c7fb40c97), the kind reported in #71 and #264. Duration varies a lot between runs, so one run each is a weak signal; the "inside freed" disappearance is the solid part.

AI-assisted (Claude Code).

@rocobalt1

Copy link
Copy Markdown

Independent run of this PR on an RTX 4060 Ti (driver 32.0.16.1656), Windows 11, Ryzen 9 5900X: 15 minutes 12 seconds in the world with no device removal. Before it, the same tree and machine had not survived past 8 min 37 s, and the two runs before that ended at 2 min 27 s and 3 min.

Build: the integration tree at 2b1a569 (about 50 PRs on main) plus this branch 23ced70, merged locally. One conflict, in resource.rs: the tree's HostTexture carries a twin (the depth+UAV mirror made by host_twin), and moving the construction into the lazy host() dropped it. I kept it there - made_with is read, the host texture is made on first use, and BOTH (depth|x) flags still build the twin - and it compiles and runs. If you rebase onto the tree, that is the one place to carry over.

Run: diag (--profile diag --features diagnostics), VICTORY_NO_WATCHDOG=1, VICTORY_D3D12_DRED=1, VICTORY_SHADER_LOOP_LIMIT=32, driven with VICTORY_KEYS (Enter every 3 s for the first two minutes, then every 50 s), reached the first control prompt and stayed there. TdrDelay=60/TdrDdiDelay=60 are set machine-wide on this box, so a hang here would have taken 60 s to report rather than 2 s - the run was not removed early by a short TDR.

What it looked like before, same machine, same tree, no PR: two removals with two different DRED stops - a 1x1x1 compute dispatch (list carrying marker Some(21), command 4 of 9, op 6) and a mesh draw (command 720 of 2201, marker Some(9), op 42, mesh 38ad582f523fd7cf + pixel d42a2d8a9d2c954a), no page fault reported in either; and one run that reached 8 min 37 s before being removed. Those are on #71 with the breadcrumbs.

Caveat: one run, and the removal was never deterministic on this card, so this is "did not reproduce in 15 minutes", not "fixed". It does move the failure well past every previous run here.

It does not change the picture: the world is still black, 170 programs still refuse to translate, and the frame is 2.5-3.4 s at 0.3-0.4 fps in this build - but it is 15 minutes of a frame that runs, which is the first time this machine has produced one.

KainanP added a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…+ Marvisak#1418) + shader stack

No textual conflict; checked with cargo check --workspace --all-targets and the full
test suite (RTX, WARP, all features) when first merged on the previous Marvisak#1511
head (289f241); this replay with cargo check --workspace --all-targets
--all-features, and the full suite at the end of the line.
@some-du6e

Copy link
Copy Markdown

For anyone merging this onto r4tedx's integration/testers: it has one conflict there, in crates/victory-gpu/src/resource.rs. That tree makes a depth target's UAV twin at creation (host_twin, HostTexture.twin), and this PR moves the host copy to first use. What worked:

  • Keep this PR's host: OnceLock and made_with, and make the twin inside Resource::host()'s initializer, next to the copy: the same desc.flags & (2 | 4) == (2 | 4) check, the twin as HostTexture { texture, state: 0.into(), twin: None }, and twin set on the main copy.
  • host_unordered on that tree reads resource(this).host.as_deref() directly; it needs resource(this).host() instead.

Built and run in a diagnostics build of integration/testers update 8 with Xeltax/VIcky's commits and this PR, on an RTX 3050 6 GB with 7.9 GB of RAM, in Series S: at about 95 s, still on the loading screen, the process held about 2.6 GB of dedicated GPU memory, and where earlier runs without it, on Xeltax/VIcky alone, had crashed around world entry, the first run with it drew the world for about 4.5 minutes.

Note

This comment was written by an AI agent (Opus 5.5, Claude Code).

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1526. Two halves: retiring host objects until running submissions finish exists in main (host::retire/release_retired, slots and lists hold their textures); making host textures on first use is #1526, which also keeps barriers from creating them and has tests.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
PastiiJay added a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 25, 2026
…make them on first use

Conflict in resource.rs: the base (AnyXboxX) builds a depth/UAV twin with
the host copy at creation; Marvisak#431 makes the host copy lazily. Kept Marvisak#431's
OnceLock + made_with and moved the twin construction into Resource::host(),
so the twin is made with the host copy on first use. host_unordered now
goes through host() too.
PastiiJay added a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 25, 2026
@AgneyaK

AgneyaK commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@Marvisak the triage above may have missed that #1526 is closed too: its author closed it at 2026-11-24 22:01 UTC as a duplicate of this PR (comment). So the two closed each other, and no open pull request now makes host textures on first use.

Checked on main 940d32f: resource.rs still makes the host texture when the title creates the resource. Only the retire/release half is in main, as the triage says.

Why it matters on small cards, from the runs already posted here: on a 6 GB / 16 GB laptop, this PR took the story load's private memory from 22.7 to 18.2 GB and the GPU memory spilled into RAM from 7.2 to 3.7 GB. 0xKaji measured ~7.85 → ~5.39 GiB of host textures on an 8 GB card. That spill is what pushes these machines to out-of-memory (this laptop blue-screened in the NVIDIA driver at ~24 GB private on 16 GB RAM tonight, on main without it).

Could this be reopened (it needs a rebase onto current main), or #1526? If neither author has time, I can port it onto main with tests.

AI use: Claude Code (Claude Opus 5.5) checked the PR states and main and wrote this; the numbers are from the runs linked above.

@AgneyaK

AgneyaK commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Opened the port as #1969 (first-use half only; the retire half is in main), with credit to this PR. Happy to close it if you'd rather rebase this one.

AI use: Claude Code (Claude Opus 5.5) wrote this.

github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
Ports the first-use half of Marvisak#431 (AntoninMichelq) onto current main; the
other half (freeing host objects only after running submissions) is
already in main.

A title texture's host copy was made when the title created the
resource. Most are never used by a list, and on small cards the rest
spills into system RAM: with Marvisak#431 on the older base, a 6 GB / 16 GB
laptop's story load went from 22.7 to 18.2 GB private and from 7.2 to
3.7 GB of GPU memory in RAM.

`Resource::host` now makes it on first use (a view, a target, a copy, a
barrier: every `host_resource` / `host_copy` / `host_transition`), from
the creation state and clear value kept with the resource, and registers
the texture by memory then. Paths that must not create one (`current`,
`alias_source` while it holds `TEXTURES_AT`, `Drop`) read `host_made`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
@AntoninMichelq
AntoninMichelq deleted the up/lazy-host-textures branch November 25, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.