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

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

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 08:12 UTC · original
0xkn0wh3r3:gpu/host-textures-made-on-first-use → main · +360 −40 · 3 files · 1 commits · closed without merging

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.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 08:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2221) `0xkn0wh3r3:gpu/host-textures-made-on-first-use` → `main` · +360 −40 · 3 files · 1 commits · closed without merging ## 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.
Every title texture got its host texture when the title created the
resource (`create_committed`, `create_committed3`, `create_placed`,
`create_at_address`). Titles make many textures up front (streaming
pools, placed resources in big heaps) that nothing draws for a long time,
or ever, so video memory filled with host textures nothing used. 64 such
textures of the kinds titles stream (BC1/BC7/RGBA8 with every mip, an
RGBA16F render target) took 421 MiB of host textures at creation on
main; they now take none until something uses them.

`Resource::host` is a `OnceLock`, filled by `host_texture` on first use;
the resource keeps the state and clear value it was created with (the
clear value copied: the title's pointer is for the call only). Every
reader goes through it: `host_resource`, `host_copy`, `host_transition`,
`view_format`, `texture_at` and `alias_source`, so views, copies, clears,
targets, presents and list states all make it on demand. Recording
threads that race for one wait for the first to make it.

What only looks does not make one: barriers (a texture nothing used has
no state to leave; its first use in a list moves it where that use
needs), `current` (one not made yet will share the memory's live one),
and `alias_source`'s search for the title texture the GPU wrote (one
never used was never written).

Sharing stays one host texture per texture memory (`MEMORY`,
`memory_key`), but made on first use the first used may be a view with
fewer uses than another title texture of the memory: the host texture is
then made as the title texture alive at the memory with the most uses
that include the first one's (`lead`), as when titles made that one
first. Without it the draw test reading a texture written in one format
through a placed view in another got two host textures where it expects
one.

Title textures are listed by memory (`TEXTURES_AT`) when created, made
or not. Uploads from title memory were already made on first use and are
unchanged. The frame-rate line also counts title textures waiting for a
first use.

Tests: a created texture makes no host texture until a use (a barrier is
none); the first use makes exactly one, as the title texture with the
most uses, which a second title texture of the memory shares; a third,
never used, has none; the counters count only what was made. An ignored
measurement gives the numbers above.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:58:55 +00:00
github-bot closed this pull request 2026-11-26 04:58:55 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 13:30 UTC · original · edited 2026-11-25 16:04 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 13:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2221#issuecomment-7163865780) · edited 2026-11-25 16:04 UTC <!-- 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)_
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 18:51 UTC · original

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.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 18:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2221#issuecomment-7171492564) 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

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