GPU: host textures are made on a title texture's first use, not with the resource #2219

Closed
0xkn0wh3r3 wants to merge 1 commit from upstream-pr/2221 into main
0xkn0wh3r3 commented 2026-11-25 08:12:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T08:12:14Z.


Problem

Every title texture got its host texture (CreateCommittedResource(3)) when the title created the resource: create_committed, create_committed3, create_placed and create_at_address all went through hand_out_resource → shared_texture → make_texture. Console titles create many textures up front (streaming pools, placed resources in big heaps) that nothing draws for a long time, or ever, so video memory fills with host textures nothing uses. texture_counts() already showed it on the frame-rate line (host texture bytes against the bytes some list used).

Measured with 64 committed title textures of the kinds titles stream: BC1 2048², BC7 1024² and RGBA8 1024² with every mip, and an RGBA16F 1920×1080 render target. All of them were created and never used. The test is resource::tests::unused_textures_take_no_host_memory (ignored); the same check was run on main for the first row.

host textures host texture memory
main, after creating them 64 421.0 MiB
this PR, after creating them 0 0.0 MiB (64 waiting for a first use)
this PR, after using each once 64 421.0 MiB

Fix

  • Resource::host is a OnceLock<Option<Arc<HostTexture>>>, filled by one function, host_texture, on first use. The resource keeps what is needed to make it later: the state and the clear value. The clear value is copied, because the title's pointer is only valid for the call.

  • Every reader goes through it: host_resource, host_copy, host_transition, view_format, texture_at and alias_source. So these all make the host texture on demand:

    • views (descriptor/host.rs)
    • list::ensure_state
    • copies and clears in list.rs
    • render target and depth bindings in draw.rs
    • presents in queue.rs
    • uploads and alias copies in texture.rs

    Recording runs on several threads: OnceLock makes the texture once, and threads that race for it wait for the first.

  • Things that only look do not make one:

    • Barriers (list::host_barrier) use the new host_resource_made. A texture nothing used has no state to leave. Its first use in a list moves it where that use needs, through the list's prelude, as for any first use in a list.
    • current: a texture not made yet will share the memory's live host texture.
    • alias_source: it looks for the title texture the GPU wrote. One never used was never written.
  • Nothing needs a host texture when the resource is created, so nothing is made eagerly:

    • Contents the title writes into memory were already uploaded on first use (texture::staging/upload, once per list per host texture). A texture made later still gets them.
    • Back buffers are made at their first render or present.
    • Placed views (texture_at) are made when a draw reads them, as before.

Kept the same

  • These are unchanged:
    • one host texture per texture memory (MEMORY, memory_key, takes)
    • replacing a memory's host texture, and the REPLACED count
    • TEXTURES_AT, alias_source and alias_copy
    • depth twins
    • staging uploads
    • lists holding the textures they use
  • Order of first use. When host textures were made at creation, a memory's host texture was its first title texture's. Titles usually create the one the GPU writes (the most uses) first, so later views shared it. With first use, the first title texture used may be a view with fewer uses.
    • To keep one host texture, it is made as the title texture alive at that memory with the most uses that include the first one's (lead).
    • If that host texture cannot take the one being used (the host refused a cast), it is dropped and the texture is made as its own, as before.
    • With lead disabled, draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view fails ("round 0: one host texture"). That shows this is needed.
  • Counters: TEXTURES, TEXTURE_BYTES, USED, SHARED, SAVED and REPLACED count what is made, when it is made. A new WAITING count (title textures alive with no host texture yet) is shown on the frame-rate line.

Tests

  • New resource::tests::host_textures_are_made_on_first_use. It makes three title textures at one memory: an R32_UINT unordered-access texture and two RGBA8 views. It checks that:
    • none has a host texture after creation, and a list barrier on one still makes none;
    • the first use (a view) makes exactly one host texture, and it has unordered access (made as the title texture with the most uses);
    • the second title texture shares it (Arc::ptr_eq, counted in SHARED);
    • the third, never used, still has none;
    • reference counts show one texture, held only by the two title textures that used it, and the counters cover what was made.
  • New ignored resource::tests::unused_textures_take_no_host_memory: the measurement above, with exact counter checks. It reads process-wide counters, so run it alone: cargo test -p victory-gpu --lib unused_textures -- --ignored --nocapture --test-threads=1.
  • cargo test --workspace --exclude victory-shader --no-fail-fast: everything passes except descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views. These fail the same way on unmodified main on this machine (the GPU's shader-visible heap is smaller than HEAP_VIEWS).
  • cargo test -p victory-shader --no-fail-fast -- --test-threads=1: 244 passed, 3 failed. The failures are hardware checks: two emit::stride_tests and gathers_at_a_level_match_the_hardware. victory-shader is not changed and does not depend on victory-gpu.
  • cargo clippy --workspace --all-targets and cargo clippy --workspace --all-targets --features victory-gpu/diagnostics: no warnings.

Not covered

  • Views written into shader-visible heaps are made when the title writes them, because shaders can index the heap directly. A texture whose view the title writes into such a heap at load still gets its host texture then. Deferring that too would need heap slots made on first read, which this PR does not do.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6600117242 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2221). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T08:12:14Z. --- ## Problem Every title texture got its host texture (`CreateCommittedResource(3)`) when the title created the resource: `create_committed`, `create_committed3`, `create_placed` and `create_at_address` all went through `hand_out_resource` → `shared_texture` → `make_texture`. Console titles create many textures up front (streaming pools, placed resources in big heaps) that nothing draws for a long time, or ever, so video memory fills with host textures nothing uses. `texture_counts()` already showed it on the frame-rate line (host texture bytes against the bytes some list used). Measured with 64 committed title textures of the kinds titles stream: BC1 2048², BC7 1024² and RGBA8 1024² with every mip, and an RGBA16F 1920×1080 render target. All of them were created and never used. The test is `resource::tests::unused_textures_take_no_host_memory` (ignored); the same check was run on `main` for the first row. | | host textures | host texture memory | |---|---|---| | `main`, after creating them | 64 | 421.0 MiB | | this PR, after creating them | 0 | 0.0 MiB (64 waiting for a first use) | | this PR, after using each once | 64 | 421.0 MiB | ## Fix - `Resource::host` is a `OnceLock<Option<Arc<HostTexture>>>`, filled by one function, `host_texture`, on first use. The resource keeps what is needed to make it later: the state and the clear value. The clear value is copied, because the title's pointer is only valid for the call. - Every reader goes through it: `host_resource`, `host_copy`, `host_transition`, `view_format`, `texture_at` and `alias_source`. So these all make the host texture on demand: - views (`descriptor/host.rs`) - `list::ensure_state` - copies and clears in `list.rs` - render target and depth bindings in `draw.rs` - presents in `queue.rs` - uploads and alias copies in `texture.rs` Recording runs on several threads: `OnceLock` makes the texture once, and threads that race for it wait for the first. - Things that only look do not make one: - **Barriers** (`list::host_barrier`) use the new `host_resource_made`. A texture nothing used has no state to leave. Its first use in a list moves it where that use needs, through the list's prelude, as for any first use in a list. - **`current`**: a texture not made yet will share the memory's live host texture. - **`alias_source`**: it looks for the title texture the GPU wrote. One never used was never written. - **Nothing needs a host texture when the resource is created**, so nothing is made eagerly: - Contents the title writes into memory were already uploaded on first use (`texture::staging`/`upload`, once per list per host texture). A texture made later still gets them. - Back buffers are made at their first render or present. - Placed views (`texture_at`) are made when a draw reads them, as before. ## Kept the same - These are unchanged: - one host texture per texture memory (`MEMORY`, `memory_key`, `takes`) - replacing a memory's host texture, and the `REPLACED` count - `TEXTURES_AT`, `alias_source` and `alias_copy` - depth twins - staging uploads - lists holding the textures they use - **Order of first use.** When host textures were made at creation, a memory's host texture was its first title texture's. Titles usually create the one the GPU writes (the most uses) first, so later views shared it. With first use, the first title texture used may be a view with fewer uses. - To keep one host texture, it is made as the title texture alive at that memory with the most uses that include the first one's (`lead`). - If that host texture cannot take the one being used (the host refused a cast), it is dropped and the texture is made as its own, as before. - With `lead` disabled, `draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view` fails ("round 0: one host texture"). That shows this is needed. - Counters: `TEXTURES`, `TEXTURE_BYTES`, `USED`, `SHARED`, `SAVED` and `REPLACED` count what is made, when it is made. A new `WAITING` count (title textures alive with no host texture yet) is shown on the frame-rate line. ## Tests - New `resource::tests::host_textures_are_made_on_first_use`. It makes three title textures at one memory: an R32_UINT unordered-access texture and two RGBA8 views. It checks that: - none has a host texture after creation, and a list barrier on one still makes none; - the first use (a view) makes exactly one host texture, and it has unordered access (made as the title texture with the most uses); - the second title texture shares it (`Arc::ptr_eq`, counted in `SHARED`); - the third, never used, still has none; - reference counts show one texture, held only by the two title textures that used it, and the counters cover what was made. - New ignored `resource::tests::unused_textures_take_no_host_memory`: the measurement above, with exact counter checks. It reads process-wide counters, so run it alone: `cargo test -p victory-gpu --lib unused_textures -- --ignored --nocapture --test-threads=1`. - `cargo test --workspace --exclude victory-shader --no-fail-fast`: everything passes except `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views`. These fail the same way on unmodified `main` on this machine (the GPU's shader-visible heap is smaller than `HEAP_VIEWS`). - `cargo test -p victory-shader --no-fail-fast -- --test-threads=1`: 244 passed, 3 failed. The failures are hardware checks: two `emit::stride_tests` and `gathers_at_a_level_match_the_hardware`. `victory-shader` is not changed and does not depend on `victory-gpu`. - `cargo clippy --workspace --all-targets` and `cargo clippy --workspace --all-targets --features victory-gpu/diagnostics`: no warnings. ## Not covered - Views written into shader-visible heaps are made when the title writes them, because shaders can index the heap directly. A texture whose view the title writes into such a heap at load still gets its host texture then. Deferring that too would need heap slots made on first read, which this PR does not do.
DenRakEiw commented 2026-11-25 09:05:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T08:22:50Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib host_textures_are_made_on_first_use — passed on RTX 5090
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean lazy creation of host textures on first use rather than at resource creation. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6054066035 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2221#pullrequestreview-6054066035). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T08:22:50Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, NVIDIA GeForce RTX 5090, Rust 1.98.1 MSVC): - `cargo test -p victory-gpu --lib host_textures_are_made_on_first_use` — passed on RTX 5090 - `cargo clippy -p victory-gpu --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean lazy creation of host textures on first use rather than at resource creation. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 09:05:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T08:48:24Z.

Review: COMMENTED


Host textures are made on a title texture's first use; barriers no longer force creation (host_resource_made). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Nice lazy-allocation fix; the new waiting texture metric helps visibility.

<!-- greyfinch-sync:Marvisak/VIctory:review:6054379307 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2221#pullrequestreview-6054379307). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T08:48:24Z. **Review:** COMMENTED --- Host textures are made on a title texture's first use; barriers no longer force creation (`host_resource_made`). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Nice lazy-allocation fix; the new `waiting` texture metric helps visibility.
Marvisak commented 2026-11-25 14:07:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T13:30:02Z.


Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-gpu/src/host.rs, crates/victory-gpu/src/resource.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs-work) was for an older state of main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163865780 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2221#issuecomment-7163865780). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T13:30:02Z. --- <!-- victory-triage v1 head=df15bd1e0e2db6c416991d7aade527f4c2bc99ac verdict=needs-rebase at=2026-11-25T16:16:30Z --> Automated triage for the maintainers. This pull request no longer merges cleanly into `main` (conflicts in `crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/resource.rs`). It will be reviewed once it is rebased onto the current `main`. The earlier verdict (needs-work) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 19:05:10 +00:00
0xkn0wh3r3 commented 2026-11-25 19:06:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T18:51:13Z.


Closing: upstream a7381ed (host textures made on first use) covers this, and 52d14b1, cea278c and ab2e2b2 build on it. The one difference is that upstream counts a list's barrier as a use, where this PR didn't make a host texture for a barrier alone. That's a deliberate upstream choice, so I'm not carrying it on.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171492564 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2221#issuecomment-7171492564). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T18:51:13Z. --- Closing: upstream a7381ed (host textures made on first use) covers this, and 52d14b1, cea278c and ab2e2b2 build on it. The one difference is that upstream counts a list's barrier as a use, where this PR didn't make a host texture for a barrier alone. That's a deliberate upstream choice, so I'm not carrying it on.

Pull request closed

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