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

Open
github-bot wants to merge 2 commits from github-bot/gh-2859 into main AGit
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 22:32 UTC · original
0xkn0wh3r3:formats/smaller-mips-detile-from-their-bytes → main · +300 −8 · 2 files · 2 commits · open

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.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 22:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859) `0xkn0wh3r3:formats/smaller-mips-detile-from-their-bytes` → `main` · +300 −8 · 2 files · 2 commits · open ## 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`.
A title that streams a texture maps the start of its memory first: in
GFX10 layouts the mip tail and the smaller mips lie at the start, the
larger ones after them. Layout::detile wanted the whole texture's bytes
for any mip, and a texture refresh that found the 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.

Layout::bytes_from(mip) gives the bytes from the texture's start that a
mip and the smaller ones take (the whole layout for arrays, thick, 3D
and multisampled layouts, whose slices each hold a chain); detile and
tile check the tiled length against it. A refresh of an untracked
texture whose memory is not all committed now finds the largest mip
whose bytes are committed and detiles and uploads that mip and the
smaller ones; the rest keep what they hold.

Tests: smaller_mips_need_only_the_start_of_the_texture (formats; each
mip of a 1024x1024 BC1 SW_4KB_S texture detiles from its bytes_from
bytes alike to the whole texture, one byte less is TooSmall) and
a_texture_with_only_its_small_mips_committed_reads_them (GPU; a
reservation committed from the start mip by mip).

From DexNom/VIctory@570a941e9b
(Layout::bytes_from, its formats test); the refresh path is reworked for
this tree.

Co-authored-by: DexNom <[email protected]>
Review of the streamed small mips: a refresh of a texture's resident
small mips still reserved staging space for the whole texture (its
largest mips included), and the new refresh path did not count the bytes
it hashed and detiled.

Staging::detile now reserves the staging layout from the first
subresource it detiles on (subresources lie in their order, so the small
mips are last; footprint offsets are multiples of 512, as the
reservation's start) and returns where offset 0 of the layout would be,
wrapped below the reservation when it starts later; copies add the
footprint offsets with wrapping arithmetic, so each lands inside. The
tracked path's partial refreshes reserve less in the same way. The
resident-mips path counts TexHashed and TexChanged as the whole-texture
path does.

Test: a_streamed_texture_stages_its_resident_mips_then_the_larger_ones
refreshes a Staging of a 1024x1024 BC1 texture in an untracked
reservation as its mips arrive (from mip 4, from mip 2, all): each
refresh stages exactly those mips, and the bytes read back from the
upload buffer equal the formats crate's detile of each.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:16:06 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 22:54 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 22:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859#issuecomment-7176925504) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:24 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-26 04:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859#issuecomment-7182572601) <!-- 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)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 22:51 UTC · original · reviewed

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** on GitHub · 2026-11-25 22:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6066077795) · **reviewed** 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.
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 22:52 UTC · original · reviewed

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 22:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6066079361) · **reviewed** 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.
Author
Owner

@⁠AwsAli05 on GitHub · 2026-11-25 22:55 UTC · original · reviewed

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.

**@⁠AwsAli05** on GitHub · 2026-11-25 22:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6066112648) · **reviewed** 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.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 10:07 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 10:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6072109301) · **approved these changes** 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).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:04 UTC · original · approved these changes

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/2859#pullrequestreview-6073016806) · **approved these changes** 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 can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2859/head:github-bot/gh-2859'
git switch 'github-bot/gh-2859'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2859'
git switch 'github-bot/gh-2859'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2859'
git switch 'github-bot/gh-2859'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2859'
git switch 'main'
git merge --squash 'github-bot/gh-2859'
git switch 'main'
git merge --ff-only 'github-bot/gh-2859'
git switch 'main'
git merge 'github-bot/gh-2859'
git push origin 'main'
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!2859
No description provided.