Formats, GPU: a streamed texture's small mips are read before its large ones are there #2857

Open
0xkn0wh3r3 wants to merge 2 commits from upstream-pr/2859 into main
0xkn0wh3r3 commented 2026-11-25 22:32:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T22:32:25Z.


What

When a title streams a texture, it maps the start of the texture's memory first. In GFX10 layouts the mip tail and the smaller mips lie at the start, and the larger mips come after them. On main:

  • Layout::detile (and tile) wanted the whole texture's bytes for any mip.
  • A refresh of a texture without write tracking that found its memory not all committed uploaded zeros for every subresource. So the small mips the title had already put in place stayed black until the whole texture arrived.

This PR changes two things:

  • victory-formats: new Layout::bytes_from(mip) returns the bytes from the texture's start that the mip and every smaller one take. It returns the whole layout for arrays, thick, 3D and multisampled layouts, where each slice holds its own chain. detile and tile check the tiled length against it. The copies were already bounds-checked slice copies, so a short slice still can't be read past.
  • victory-gpu: when such a refresh finds the memory not all committed, first_resident_mip finds the largest mip whose bytes_from range is committed. That mip and the smaller ones are detiled from those bytes and uploaded; the other subresources keep what they hold. A texture with nothing committed still takes the zeros path as before. That costs one extra VirtualQuery pass, over the tail block only. Textures with write tracking already read through copy_mapped, which stands in for unmapped pages, so they're unchanged.

Where it came from

Layout::bytes_from and its formats test are from DexNom's fork, DexNom/VIctory@570a941, credited with Co-authored-by: DexNom. Their GPU caller (placed_bytes_from) sits in their own placed-texture code; the refresh-path change here is reworked for this tree.

Tested

  • texture::tests::smaller_mips_need_only_the_start_of_the_texture (formats): for a 1024×1024 BC1 SW_4KB_S texture with 9 mips, each mip detiles from its bytes_from bytes exactly as from the whole texture, and one byte fewer gives TooSmall. Array textures need the whole layout.
  • texture::tests::a_texture_with_only_its_small_mips_committed_reads_them (GPU): a reservation of that texture's size, committed from the start one mip range at a time, gives first_resident_mip = 4, 3, 2, 1. With nothing committed it gives None.
  • cargo test -p victory-formats: 47 passed, 5 ignored.
  • cargo test -p victory-gpu on Windows 11 with an RTX 5070 Ti: 227 passed. The 2 failures (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) fail identically on main on NVIDIA; #2121 fixes them.
  • cargo clippy --all-targets -- -D warnings for victory-formats, and for victory-gpu with and without --features diagnostics: clean.

Not tested

  • Not run with the title, so the effect on streamed textures in a real scene isn't measured. I also haven't measured how often untracked textures are partly committed.

Changes after review (b4d4b4c)

  • Staging size: Staging::detile now reserves the staging layout only from the first subresource it detiles. Subresources lie in order, so the small mips are last, and footprint offsets are multiples of 512. It returns where offset 0 of the layout would be, wrapped below the reservation when that starts later; copies adds footprint offsets with wrapping arithmetic, so each one lands inside. For the 1024×1024 BC1 texture in the test, a refresh of mips 4–8 now reserves 7,688 of the layout's 704,008 bytes, and one from mip 2 on 48,648. The tracked path's partial refreshes reserve less the same way.
  • Copystat: the resident-mips path counts TexHashed and TexChanged like the whole-texture path.
  • End-to-end test: texture::tests::a_streamed_texture_stages_its_resident_mips_then_the_larger_ones refreshes a real Staging of that texture in an untracked reservation as its mips arrive (from mip 4, then mip 2, then all). Each refresh stages exactly those mips. The bytes read back from the upload buffer equal victory-formats' detile of each mip, so the shifted reservation is checked too. It stops at the staging buffer: it doesn't copy into a host texture and sample it, and I haven't measured a live scene.
  • Rebased on main (842b0cb). cargo test -p victory-gpu: 228 passed, plus the 2 known NVIDIA failures (#2121). Clippy is clean with and without diagnostics.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6615108229 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T22:32:25Z. --- ## What When a title streams a texture, it maps the start of the texture's memory first. In GFX10 layouts the mip tail and the smaller mips lie at the start, and the larger mips come after them. On main: - `Layout::detile` (and `tile`) wanted the whole texture's bytes for any mip. - A refresh of a texture without write tracking that found its memory not all committed uploaded **zeros for every subresource**. So the small mips the title had already put in place stayed black until the whole texture arrived. This PR changes two things: - **`victory-formats`:** new `Layout::bytes_from(mip)` returns the bytes from the texture's start that the mip and every smaller one take. It returns the whole layout for arrays, thick, 3D and multisampled layouts, where each slice holds its own chain. `detile` and `tile` check the tiled length against it. The copies were already bounds-checked slice copies, so a short slice still can't be read past. - **`victory-gpu`:** when such a refresh finds the memory not all committed, `first_resident_mip` finds the largest mip whose `bytes_from` range is committed. That mip and the smaller ones are detiled from those bytes and uploaded; the other subresources keep what they hold. A texture with nothing committed still takes the zeros path as before. That costs one extra `VirtualQuery` pass, over the tail block only. Textures with write tracking already read through `copy_mapped`, which stands in for unmapped pages, so they're unchanged. ## Where it came from `Layout::bytes_from` and its formats test are from DexNom's fork, [DexNom/VIctory@570a941](https://github.com/DexNom/VIctory/commit/570a941e9b4e945121c26eb30dfc10c126845207), credited with `Co-authored-by: DexNom`. Their GPU caller (`placed_bytes_from`) sits in their own placed-texture code; the refresh-path change here is reworked for this tree. ## Tested - `texture::tests::smaller_mips_need_only_the_start_of_the_texture` (formats): for a 1024×1024 BC1 `SW_4KB_S` texture with 9 mips, each mip detiles from its `bytes_from` bytes exactly as from the whole texture, and one byte fewer gives `TooSmall`. Array textures need the whole layout. - `texture::tests::a_texture_with_only_its_small_mips_committed_reads_them` (GPU): a reservation of that texture's size, committed from the start one mip range at a time, gives `first_resident_mip` = 4, 3, 2, 1. With nothing committed it gives `None`. - `cargo test -p victory-formats`: 47 passed, 5 ignored. - `cargo test -p victory-gpu` on Windows 11 with an RTX 5070 Ti: 227 passed. The 2 failures (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`) fail identically on main on NVIDIA; #2121 fixes them. - `cargo clippy --all-targets -- -D warnings` for victory-formats, and for victory-gpu with and without `--features diagnostics`: clean. ## Not tested - Not run with the title, so the effect on streamed textures in a real scene isn't measured. I also haven't measured how often untracked textures are partly committed. ## Changes after review (b4d4b4c) - **Staging size:** `Staging::detile` now reserves the staging layout only from the first subresource it detiles. Subresources lie in order, so the small mips are last, and footprint offsets are multiples of 512. It returns where offset 0 of the layout would be, wrapped below the reservation when that starts later; `copies` adds footprint offsets with wrapping arithmetic, so each one lands inside. For the 1024×1024 BC1 texture in the test, a refresh of mips 4–8 now reserves 7,688 of the layout's 704,008 bytes, and one from mip 2 on 48,648. The tracked path's partial refreshes reserve less the same way. - **Copystat:** the resident-mips path counts `TexHashed` and `TexChanged` like the whole-texture path. - **End-to-end test:** `texture::tests::a_streamed_texture_stages_its_resident_mips_then_the_larger_ones` refreshes a real `Staging` of that texture in an untracked reservation as its mips arrive (from mip 4, then mip 2, then all). Each refresh stages exactly those mips. The bytes read back from the upload buffer equal `victory-formats`' detile of each mip, so the shifted reservation is checked too. It stops at the staging buffer: it doesn't copy into a host texture and sample it, and I haven't measured a live scene. - Rebased on main (842b0cb). `cargo test -p victory-gpu`: 228 passed, plus the 2 known NVIDIA failures (#2121). Clippy is clean with and without `diagnostics`.
kotekompotek commented 2026-11-25 23:05:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T22:51:57Z.

Review: COMMENTED


Sound: the whole-layout fallback for sliced/thick/3D/multisampled textures is the safe direction, and for single-slice 2D the max-over-smaller-mips extent (with tail-block mips needing only bytes, since the tail IS the texture's first block) matches the tile layout where small mips sit near the start. Capping at size keeps malformed descs in bounds. detile accepting bytes_from(mip) instead of size is exactly what a streamed-in-small-mips resource needs. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066077795 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6066077795). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T22:51:57Z. **Review:** COMMENTED --- Sound: the whole-layout fallback for sliced/thick/3D/multisampled textures is the safe direction, and for single-slice 2D the max-over-smaller-mips extent (with tail-block mips needing only bytes, since the tail IS the texture's first block) matches the tile layout where small mips sit near the start. Capping at size keeps malformed descs in bounds. detile accepting bytes_from(mip) instead of size is exactly what a streamed-in-small-mips resource needs. LGTM.
kotekompotek commented 2026-11-25 23:05:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T22:52:09Z.

Review: COMMENTED


Sound: the whole-layout fallback for sliced/thick/3D/multisampled textures is the safe direction, and for single-slice 2D the max-over-smaller-mips extent (with tail-block mips needing only alignment bytes, since the tail IS the texture's first block) matches the tile layout where small mips sit near the start. Capping at size keeps malformed descs in bounds. detile accepting bytes_from(mip) instead of size is exactly what a streamed-in-small-mips resource needs. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066079361 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6066079361). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T22:52:09Z. **Review:** COMMENTED --- Sound: the whole-layout fallback for sliced/thick/3D/multisampled textures is the safe direction, and for single-slice 2D the max-over-smaller-mips extent (with tail-block mips needing only `alignment` bytes, since the tail IS the texture's first block) matches the tile layout where small mips sit near the start. Capping at size keeps malformed descs in bounds. detile accepting bytes_from(mip) instead of size is exactly what a streamed-in-small-mips resource needs. LGTM.
AwsAli05 commented 2026-11-25 23:05:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AwsAli05 on 2026-11-25T22:55:45Z.

Review: COMMENTED


I ran the two new focused tests on Windows at e041984. The BC1 prefix detile test and the committed-mip selection test both passed.

One memory-budget concern in Staging::detile: the partial refresh still reserves staging_space(self.size), so the first upload of just the resident tail still needs the full D3D12 staging footprint. This matters when streaming is memory-limited. The current tests cover prefix layout and residency selection. An end-to-end test that samples a host mip before and after a larger mip arrives would also verify the upload path. Please record the staging bytes when measuring the change in a live scene.

AI-assisted review.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066112648 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6066112648). Originally posted by [AwsAli05](https://github.com/AwsAli05) on 2026-11-25T22:55:45Z. **Review:** COMMENTED --- I ran the two new focused tests on Windows at `e041984`. The BC1 prefix detile test and the committed-mip selection test both passed. One memory-budget concern in `Staging::detile`: the partial refresh still reserves `staging_space(self.size)`, so the first upload of just the resident tail still needs the full D3D12 staging footprint. This matters when streaming is memory-limited. The current tests cover prefix layout and residency selection. An end-to-end test that samples a host mip before and after a larger mip arrives would also verify the upload path. Please record the staging bytes when measuring the change in a live scene. AI-assisted review.
fuzeofficlal commented 2026-11-25 23:06:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T22:54:30Z.


Reviewed and verified e0419847 on Windows 11 (RTX 5090 + WARP) against main (842b0cb):

  • Layout::bytes_from(first) correctly bounds the required resident prefix for tiled non-array 2D textures (falling back to full self.size for first == 0, arrays, thick/3D, MSAA, or Swizzle::LINEAR), and walk() + first_resident_mip safely hash, detile, and upload only i % self.mips >= first while re-uploading automatically as larger mips become committed.
  • Local checks: victory-formats --lib texture::tests 17/17 passed, victory-gpu --lib texture::tests 3/3 passed on RTX 5090 and WARP (including a_texture_with_only_its_small_mips_committed_reads_them), cargo clippy --locked -p victory-formats -p victory-gpu --all-targets (default & --features diagnostics) 0 warnings, cargo xtask policy (190 files ok).
  • Tiny non-blocking nit: the first_resident_mip branch in Staging::refresh_now (texture.rs:636-651) skips copystat::C::TexHashed/TexChanged accounting unlike the full-upload path.

Looks clean and ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176925504 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859#issuecomment-7176925504). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T22:54:30Z. --- Reviewed and verified `e0419847` on Windows 11 (RTX 5090 + WARP) against `main` (`842b0cb`): - `Layout::bytes_from(first)` correctly bounds the required resident prefix for tiled non-array 2D textures (falling back to full `self.size` for `first == 0`, arrays, thick/3D, MSAA, or `Swizzle::LINEAR`), and `walk()` + `first_resident_mip` safely hash, detile, and upload only `i % self.mips >= first` while re-uploading automatically as larger mips become committed. - Local checks: `victory-formats --lib texture::tests` 17/17 passed, `victory-gpu --lib texture::tests` 3/3 passed on RTX 5090 and WARP (including `a_texture_with_only_its_small_mips_committed_reads_them`), `cargo clippy --locked -p victory-formats -p victory-gpu --all-targets` (default & `--features diagnostics`) 0 warnings, `cargo xtask policy` (`190 files ok`). - Tiny non-blocking nit: the `first_resident_mip` branch in `Staging::refresh_now` (`texture.rs:636-651`) skips `copystat::C::TexHashed`/`TexChanged` accounting unlike the full-upload path. Looks clean and ready to merge.
Marvisak commented 2026-11-26 05:04:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:24:48Z.


Automated triage for the maintainers.

Merge candidate. Layout::bytes_from takes the largest end offset over the requested mip and all smaller ones (mip tail as the first block), so it holds for any mip placement and falls back to the whole size for arrays, 3D, thick and MSAA; in victory-gpu, first_resident_mip and the partial-refresh branch of Staging::refresh_now upload the committed small mips instead of zeros, and Staging::detile reserves staging only from the first needed footprint. The offsets stay 512-aligned, footprint.Offset - from cannot underflow, and copies adds them back; the formats test and staged read-back test cover it.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182572601 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859#issuecomment-7182572601). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:24:48Z. --- <!-- victory-triage v1 head=b4d4b4c83049ec4b71a89c3fcde2d39bb0dbc530 verdict=merge-candidate at=2026-11-26T04:24:39Z --> Automated triage for the maintainers. **Merge candidate.** `Layout::bytes_from` takes the largest end offset over the requested mip and all smaller ones (mip tail as the first block), so it holds for any mip placement and falls back to the whole size for arrays, 3D, thick and MSAA; in `victory-gpu`, `first_resident_mip` and the partial-refresh branch of `Staging::refresh_now` upload the committed small mips instead of zeros, and `Staging::detile` reserves staging only from the first needed footprint. The offsets stay 512-aligned, `footprint.Offset - from` cannot underflow, and `copies` adds them back; the formats test and staged read-back test cover it. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 11:03:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T10:07:31Z.

Review: APPROVED


Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-formats --lib texture:: -- --test-threads=1: 17/17 passed (including smaller_mips_need_only_the_start_of_the_texture).
  • cargo test -p victory-gpu --lib texture:: -- --test-threads=1: passed.
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed on RTX 5090 and WARP.
  • cargo clippy -p victory-formats -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly allows streamed textures' smaller mips to be read before larger mips are mapped.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6072109301 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6072109301). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T10:07:31Z. **Review:** APPROVED --- Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-formats --lib texture:: -- --test-threads=1`: 17/17 passed (including `smaller_mips_need_only_the_start_of_the_texture`). - `cargo test -p victory-gpu --lib texture:: -- --test-threads=1`: passed. - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 20/20 passed on RTX 5090 and WARP. - `cargo clippy -p victory-formats -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly allows streamed textures' smaller mips to be read before larger mips are mapped. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-26 12:05:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:04:58Z.

Review: APPROVED


Verified b4d4b4c8 locally on Windows 11 (RTX 5090 and WARP, with VICTORY_D3D12_DEBUG=1):

  • cargo xtask policy and cargo clippy -p victory-formats -p victory-gpu --tests -- -D warnings (default and --all-features): clean.
  • victory-formats --lib texture::: 17/17 passed (including smaller_mips_need_only_the_start_of_the_texture).
  • victory-gpu --lib texture::: 4/4 passed (including a_texture_with_only_its_small_mips_committed_reads_them and a_streamed_texture_stages_its_resident_mips_then_the_larger_ones) on RTX 5090, RTX 5090 + VICTORY_D3D12_DEBUG=1, and WARP + VICTORY_D3D12_DEBUG=1.
  • victory-gpu --lib draw::tests:: (including a_write_into_one_mip_uploads_that_mip_alone exercising partial-subresource staging through CopyTextureRegion): 20/20 passed across all three configurations.

Layout::bytes_from handles tiled mip tails (self.alignment == block_bytes), regular/linear smallest-first chains, and whole-layout fallbacks (arrays, 3D, thick, MSAA) cleanly, and Staging::detile reserving self.size - from keeps 512-byte placement alignment and subresource_len bounds intact while cutting staging overhead for partial refreshes. Ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6073016806 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6073016806). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:04:58Z. **Review:** APPROVED --- Verified `b4d4b4c8` locally on Windows 11 (RTX 5090 and WARP, with `VICTORY_D3D12_DEBUG=1`): - `cargo xtask policy` and `cargo clippy -p victory-formats -p victory-gpu --tests -- -D warnings` (default and `--all-features`): clean. - `victory-formats --lib texture::`: 17/17 passed (including `smaller_mips_need_only_the_start_of_the_texture`). - `victory-gpu --lib texture::`: 4/4 passed (including `a_texture_with_only_its_small_mips_committed_reads_them` and `a_streamed_texture_stages_its_resident_mips_then_the_larger_ones`) on RTX 5090, RTX 5090 + `VICTORY_D3D12_DEBUG=1`, and WARP + `VICTORY_D3D12_DEBUG=1`. - `victory-gpu --lib draw::tests::` (including `a_write_into_one_mip_uploads_that_mip_alone` exercising partial-subresource staging through `CopyTextureRegion`): 20/20 passed across all three configurations. `Layout::bytes_from` handles tiled mip tails (`self.alignment == block_bytes`), regular/linear smallest-first chains, and whole-layout fallbacks (arrays, 3D, thick, MSAA) cleanly, and `Staging::detile` reserving `self.size - from` keeps 512-byte placement alignment and `subresource_len` bounds intact while cutting staging overhead for partial refreshes. Ready to merge.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2859:upstream-pr/2859'
git switch 'upstream-pr/2859'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2859'
git switch 'upstream-pr/2859'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2859'
git switch 'upstream-pr/2859'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2859'
git switch 'main'
git merge --squash 'upstream-pr/2859'
git switch 'main'
git merge --ff-only 'upstream-pr/2859'
git switch 'main'
git merge 'upstream-pr/2859'
git push origin 'main'
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!2857
No description provided.