GPU: make host textures on first use instead of at resource creation #1526

Closed
github-bot wants to merge 1 commit from gh-pr/1526/head into gh-pr/1526/base
Owner

@⁠0xKaji on GitHub · 2026-11-24 18:47 UTC · original
0xKaji:feat/lazy-host-textures → main · +310 −28 · 2 files · 1 commits · closed without merging

Why

Buffers (shadow.rs) and shader-visible descriptor heaps (descriptor.rs) are already created lazily on first use, but hand_out_resource() in crates/victory-gpu/src/resource.rs still calls host_texture() (CreateCommittedResource in D3D12_HEAP_TYPE_DEFAULT) eagerly for every texture the title registers.

During startup and world streaming, the title registers thousands of placed/address textures before drawing them (in our RTX 3060 8 GB trace, 3,289 textures totaling ~8,233 MiB of estimated footprint before Stage (e) even begins). On 8 GB GPUs this saturates VRAM (7,851 / 8,192 MiB) within ~44 s and triggers NV_ERR_NO_MEMORY / vkAllocateMemory OOM faults — the pre-lazy commit cannot reach Stage (e) on an 8 GB GPU with warm caches (OOM at frame=1123), which is itself the motivating case (related to #1226, and orthogonal to mip-drop quality reduction in #1296).

What Changed

  • crates/victory-gpu/src/resource.rs:
    • Store initial_state: u32 and clear_value: Option<[u32; 5]> on Resource (copied while the caller's pointer is still live), and change Resource::host from Option<Arc<HostTexture>> to OnceLock<Option<Arc<HostTexture>>>.
    • Materialize the host ID3D12Resource on first use via Resource::host_entry() when host_resource(), host_copy(), host_transition(), view_format(), or texture_at() accesses it.
    • Add existing_host_resource() to query an already-materialized host texture without creating one, and avoid materializing in Drop for Resource.
    • Add unit tests resource::tests::host_texture_is_made_on_first_use and resource::tests::clear_value_survives_lazy_materialization.
  • crates/victory-gpu/src/list.rs:
    • Use existing_host_resource() in host_barrier() so resource barriers on textures never yet touched by a view, copy, clear, or draw do not materialize them into VRAM. (When a texture is later materialized on first use, HostList::transition / prelude() transitions it from its initial_state.)

Aliasing

The host layer has never shared ID3D12Heap or used CreatePlacedResource (host_texture() at crates/victory-gpu/src/resource.rs:887 gives every Resource its own committed allocation on main); placed/address backing (resource.rs:1172, resource.rs:1261) is CPU-side only, so lazy materialization does not change aliasing semantics.

Verification

Tested on CachyOS Linux (7.3.5-2), Wine 11.22, vkd3d-proton 3.2.0 (GE-Proton11-14), NVIDIA RTX 3060 8 GB (driver 620.73.04), cross-built with cargo xwin (x86_64-pc-windows-msvc):

  • cargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc -- -D warnings: 0 warnings.
  • cargo test -p xtask (asset policy) + Linux-compatible crates: 0 failed.
  • victory-gpu under Wine (--test-threads=1):
    • resource::tests: 8 passed, 0 failed — including host_texture_is_made_on_first_use and clear_value_survives_lazy_materialization (overwrites caller stack storage before lazy materialization, runs ClearRenderTargetView + GPU readback, asserts pixel values match the original clear color).
    • Full victory-gpu suite stacked with #177 + #190 + #347: 51 passed, 0 failed, 1 ignored (plus clear_value_survives_lazy_materialization verified under Wine).
  • Controlled live title A/B test on RTX 3060 8 GB (warm caches, identical 120s window, VKD3D_DISABLE_EXTENSIONS=VK_NV_raw_access_chains, VICTORY_D3D12_DRED=1, stacked on #177 + #190 + #347):
    • Same-frame peak at frame=1123: 7,851 vs 5,392 MiB (−2.40 GiB).
    • Peak over window: 7,851 vs 5,639 MiB (−2.16 GiB). VRAM stays flat ~5,390 MiB across frames 512–1123 with lazy, vs spiking to 7,851 MiB between frames 512–1024 on main.
    • Per-frame VRAM progression (nvidia-smi):
Frame Pre-lazy (run5, warm) With lazy (run6, warm)
frame=128 5,333 MiB 5,312 MiB
frame=512 5,379 MiB 5,344 MiB
frame=1024 7,851 MiB 5,384 MiB
frame=1123 (last common) 7,851 MiB (OOM crash at +66 s: vkAllocateMemory 0xc0000005, NV_ERR_NO_MEMORY) 5,392 MiB (0 kernel NVRM/Xid errors)
Window end (120 s) (crashed before Stage (e)) 5,639 MiB peak (frame=1366, 1,415 draws/frame, 488 pipelines incl. 127 mesh, 0 crashes, 0 Xids)

On GPUs with more VRAM this OOM may not occur; on 8 GB GPUs, eager host texture allocation hits the VRAM ceiling before Stage (e) once compilation stalls are gone, whereas lazy materialization keeps full mip quality with >2 GiB of headroom.

**@⁠0xKaji** on GitHub · 2026-11-24 18:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/1526) `0xKaji:feat/lazy-host-textures` → `main` · +310 −28 · 2 files · 1 commits · closed without merging ## Why Buffers (`shadow.rs`) and shader-visible descriptor heaps (`descriptor.rs`) are already created lazily on first use, but `hand_out_resource()` in `crates/victory-gpu/src/resource.rs` still calls `host_texture()` (`CreateCommittedResource` in `D3D12_HEAP_TYPE_DEFAULT`) eagerly for every texture the title registers. During startup and world streaming, the title registers thousands of placed/address textures before drawing them (in our RTX 3060 8 GB trace, `3,289` textures totaling `~8,233 MiB` of estimated footprint before Stage (e) even begins). On 8 GB GPUs this saturates VRAM (`7,851 / 8,192 MiB`) within ~44 s and triggers `NV_ERR_NO_MEMORY` / `vkAllocateMemory` OOM faults — the pre-lazy commit cannot reach Stage (e) on an 8 GB GPU with warm caches (OOM at `frame=1123`), which is itself the motivating case (related to #1226, and orthogonal to mip-drop quality reduction in #1296). ## What Changed - **`crates/victory-gpu/src/resource.rs`**: - Store `initial_state: u32` and `clear_value: Option<[u32; 5]>` on `Resource` (copied while the caller's pointer is still live), and change `Resource::host` from `Option<Arc<HostTexture>>` to `OnceLock<Option<Arc<HostTexture>>>`. - Materialize the host `ID3D12Resource` on first use via `Resource::host_entry()` when `host_resource()`, `host_copy()`, `host_transition()`, `view_format()`, or `texture_at()` accesses it. - Add `existing_host_resource()` to query an already-materialized host texture without creating one, and avoid materializing in `Drop for Resource`. - Add unit tests `resource::tests::host_texture_is_made_on_first_use` and `resource::tests::clear_value_survives_lazy_materialization`. - **`crates/victory-gpu/src/list.rs`**: - Use `existing_host_resource()` in `host_barrier()` so resource barriers on textures never yet touched by a view, copy, clear, or draw do not materialize them into VRAM. (When a texture is later materialized on first use, `HostList::transition` / `prelude()` transitions it from its `initial_state`.) ## Aliasing The host layer has never shared `ID3D12Heap` or used `CreatePlacedResource` (`host_texture()` at `crates/victory-gpu/src/resource.rs:887` gives every `Resource` its own committed allocation on `main`); placed/address backing (`resource.rs:1172`, `resource.rs:1261`) is CPU-side only, so lazy materialization does not change aliasing semantics. ## Verification Tested on CachyOS Linux (`7.3.5-2`), Wine `11.22`, `vkd3d-proton 3.2.0` (`GE-Proton11-14`), NVIDIA RTX 3060 8 GB (driver `620.73.04`), cross-built with `cargo xwin` (`x86_64-pc-windows-msvc`): - `cargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc -- -D warnings`: **0 warnings**. - `cargo test -p xtask` (asset policy) + Linux-compatible crates: **0 failed**. - `victory-gpu` under Wine (`--test-threads=1`): - `resource::tests`: **8 passed, 0 failed** — including `host_texture_is_made_on_first_use` and `clear_value_survives_lazy_materialization` (overwrites caller stack storage before lazy materialization, runs `ClearRenderTargetView` + GPU readback, asserts pixel values match the original clear color). - Full `victory-gpu` suite stacked with `#177 + #190 + #347`: **51 passed, 0 failed, 1 ignored** (plus `clear_value_survives_lazy_materialization` verified under Wine). - **Controlled live title A/B test on RTX 3060 8 GB** (warm caches, identical 120s window, `VKD3D_DISABLE_EXTENSIONS=VK_NV_raw_access_chains`, `VICTORY_D3D12_DRED=1`, stacked on `#177 + #190 + #347`): - **Same-frame peak at `frame=1123`:** `7,851` vs `5,392 MiB` (**−2.40 GiB**). - **Peak over window:** `7,851` vs `5,639 MiB` (**−2.16 GiB**). VRAM stays flat `~5,390 MiB` across frames `512–1123` with lazy, vs spiking to `7,851 MiB` between frames `512–1024` on `main`. - Per-frame VRAM progression (`nvidia-smi`): | Frame | Pre-lazy (`run5`, warm) | With lazy (`run6`, warm) | |---|---|---| | `frame=128` | `5,333 MiB` | `5,312 MiB` | | `frame=512` | `5,379 MiB` | `5,344 MiB` | | `frame=1024` | `7,851 MiB` | `5,384 MiB` | | `frame=1123` (last common) | `7,851 MiB` (OOM crash at `+66 s`: `vkAllocateMemory 0xc0000005`, `NV_ERR_NO_MEMORY`) | `5,392 MiB` (**0 kernel `NVRM`/`Xid` errors**) | | Window end (`120 s`) | *(crashed before Stage (e))* | `5,639 MiB` peak (`frame=1366`, `1,415` draws/frame, `488` pipelines incl. `127` mesh, **0 crashes, 0 Xids**) | On GPUs with more VRAM this OOM may not occur; on 8 GB GPUs, eager host texture allocation hits the VRAM ceiling before Stage (e) once compilation stalls are gone, whereas lazy materialization keeps full mip quality with >2 GiB of headroom.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:13 +00:00
github-bot closed this pull request 2026-11-26 04:35:13 +00:00
Author
Owner

@⁠antikohza on GitHub · 2026-11-24 19:58 UTC · original

@⁠0xKaji — saw your reference to #1296 and your RTX 3060 8 GB NV_ERR_NO_MEMORY / vkAllocateMemory measurements here.

Just a heads-up that #1296 (a6f3ccb) is updated with discrete adapter selection, LUID-matched <= 8.5 GiB auto mip-drop, ResourceMinLODClamp rebasing, and subresource-granular HostWritten tracking — and we also verified a clean stack combining #431 (lazy first-use textures) + #967 + #1296 on antikohza:feat/texture-mip-drop-on-1542 (0d79788, Windows CI run 45473319819). Stacking mip-drop with lazy materialization gives 8 GB Linux/Proton and Windows setups even more headroom.

**@⁠antikohza** on GitHub · 2026-11-24 19:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1526#issuecomment-7142876782) @⁠0xKaji — saw your reference to **#1296** and your RTX 3060 8 GB `NV_ERR_NO_MEMORY` / `vkAllocateMemory` measurements here. Just a heads-up that #1296 (`a6f3ccb`) is updated with discrete adapter selection, LUID-matched `<= 8.5 GiB` auto mip-drop, `ResourceMinLODClamp` rebasing, and subresource-granular `HostWritten` tracking — and we also verified a clean stack combining **#431** (lazy first-use textures) + **#967** + **#1296** on [`antikohza:feat/texture-mip-drop-on-1542`](https://github.com/antikohza/VIctory/tree/feat/texture-mip-drop-on-1542) (`0d79788`, [Windows CI run 45473319819](https://github.com/antikohza/VIctory/actions/runs/45473319819)). Stacking mip-drop with lazy materialization gives 8 GB Linux/Proton and Windows setups even more headroom.
Author
Owner

@⁠0xKaji on GitHub · 2026-11-24 22:01 UTC · original

Closing as a duplicate of #431 (@⁠AntoninMichelq), which I missed when checking for overlap. It makes host textures on first use the same way (OnceLock + clear value kept), opened earlier, and also defers frees past running submissions. For reference, the same idea on an RTX 3060 8 GB under Linux/Wine + vkd3d-proton 3.2.0 took the world's same-frame host-texture peak from ~7.85 GiB to ~5.39 GiB and turned an OOM at +66 s into a clean 120 s run. Thanks @⁠DenRakEiw for the review.

**@⁠0xKaji** on GitHub · 2026-11-24 22:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1526#issuecomment-7145697548) Closing as a duplicate of #431 (@⁠AntoninMichelq), which I missed when checking for overlap. It makes host textures on first use the same way (`OnceLock` + clear value kept), opened earlier, and also defers frees past running submissions. For reference, the same idea on an RTX 3060 8 GB under Linux/Wine + vkd3d-proton 3.2.0 took the world's same-frame host-texture peak from ~7.85 GiB to ~5.39 GiB and turned an OOM at +66 s into a clean 120 s run. Thanks @⁠DenRakEiw for the review.

Pull request closed

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!1526
No description provided.