GPU: bound texture upload and capture slices to the allocated buffer #601

Open
github-bot wants to merge 7 commits from github-bot/gh-601 into main AGit
Owner

@⁠AwsAli05 on GitHub · 2026-11-24 05:21 UTC · original
AwsAli05:gpu/bound-footprint-slices → main · +69 −3 · 2 files · 7 commits · open

A 10×6 RGBA8 texture with two mips places its 5×3 mip at byte 1536. Native D3D12 on WARP reports a 256-byte pitch, 20 bytes of pixels per row and a 2068-byte upload buffer. The old detile code formed a 768-byte mutable slice at offset 1536, where the mapping has only 532 bytes of resource data. The texture upload diagnostics reported the same oversized range. Both capture paths also assumed a full pitch after the last row of their readback buffers.

The upload path now caps the pitched subresource span at the end of the staging buffer. Frame captures and render-target dumps use the readback buffer’s declared byte width. Row and depth strides stay intact. The regression test checks both mips so that the final-row fix also preserves the earlier mip’s span.

Verified on Windows with Rust 1.98.1 (GNU target). An independent call to ID3D12Device::GetCopyableFootprints on the WARP adapter confirmed the offsets and lengths above.

  • cargo check --locked -p victory-gpu
  • VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics -- --test-threads=1 (46 passed, one benchmark ignored)
  • cargo xtask policy

Testing covered synthetic resources. Gameplay impact is unmeasured.

**@⁠AwsAli05** on GitHub · 2026-11-24 05:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/601) `AwsAli05:gpu/bound-footprint-slices` → `main` · +69 −3 · 2 files · 7 commits · open A 10×6 RGBA8 texture with two mips places its 5×3 mip at byte 1536. Native D3D12 on WARP reports a 256-byte pitch, 20 bytes of pixels per row and a 2068-byte upload buffer. The old detile code formed a 768-byte mutable slice at offset 1536, where the mapping has only 532 bytes of resource data. The texture upload diagnostics reported the same oversized range. Both capture paths also assumed a full pitch after the last row of their readback buffers. The upload path now caps the pitched subresource span at the end of the staging buffer. Frame captures and render-target dumps use the readback buffer’s declared byte width. Row and depth strides stay intact. The regression test checks both mips so that the final-row fix also preserves the earlier mip’s span. Verified on Windows with Rust 1.98.1 (GNU target). An independent call to `ID3D12Device::GetCopyableFootprints` on the WARP adapter confirmed the offsets and lengths above. - `cargo check --locked -p victory-gpu` - `VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics -- --test-threads=1` (46 passed, one benchmark ignored) - `cargo xtask policy` Testing covered synthetic resources. Gameplay impact is unmeasured.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:09:39 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 10:51 UTC · original

Independent verification & code review on Windows 11 Pro (build 26100) + AMD Radeon RX 9060 XT (RDNA4, driver 32.0.32029.2010, Rust 1.98.1), cleanly merged onto main (ffe310e):

  • cargo xtask policy: PASS (154 files ok)
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: PASS
  • cargo clippy -p victory-gpu --all-targets --features diagnostics --locked -- -D warnings: PASS
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked: 51 passed, 0 failed, 1 ignored (including new texture::tests::upload_slice_stops_at_the_end_of_the_buffer)
  • VICTORY_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked -- texture: 4/4 PASS (1 ignored benchmark)

Diff review (capture.rs, texture.rs):

  • Fixes a real slice-length UB where GetCopyableFootprints sizes the allocation to end after the last row's active bytes rather than a full RowPitch.
  • Verified capture.rs's to_rgba / save only read w * pixel_bytes on the last row, so bounding rows to buffer.GetDesc().Width is safe and exact.
  • Minor non-blocking note: buffer_size.saturating_sub(footprint.Offset) in footprint_len is slightly more defensive than buffer_size - footprint.Offset if a synthetic/mock footprint ever passes an offset past buffer_size.

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 10:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/601#issuecomment-7129343484) Independent verification & code review on Windows 11 Pro (build 26100) + AMD Radeon RX 9060 XT (RDNA4, driver `32.0.32029.2010`, Rust `1.98.1`), cleanly merged onto `main` (`ffe310e`): - `cargo xtask policy`: PASS (`154 files ok`) - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`: PASS - `cargo clippy -p victory-gpu --all-targets --features diagnostics --locked -- -D warnings`: PASS - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked`: **51 passed, 0 failed, 1 ignored** (including new `texture::tests::upload_slice_stops_at_the_end_of_the_buffer`) - `VICTORY_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked -- texture`: **4/4 PASS** (1 ignored benchmark) Diff review (`capture.rs`, `texture.rs`): - Fixes a real slice-length UB where `GetCopyableFootprints` sizes the allocation to end after the last row's active bytes rather than a full `RowPitch`. - Verified `capture.rs`'s `to_rgba` / `save` only read `w * pixel_bytes` on the last row, so bounding `rows` to `buffer.GetDesc().Width` is safe and exact. - Minor non-blocking note: `buffer_size.saturating_sub(footprint.Offset)` in `footprint_len` is slightly more defensive than `buffer_size - footprint.Offset` if a synthetic/mock footprint ever passes an offset past `buffer_size`. *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠Yaekai on GitHub · 2026-11-24 16:02 UTC · original

Independent test & review on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head 4899516:

  • cargo test -p victory-gpu --lib: 44 passed, 0 failed (including upload_slice_stops_at_the_end_of_the_buffer).
  • cargo clippy -p victory-gpu -- -D warnings: 0 warnings.
  • One minor defensive nit in footprint_len(): using buffer_size.saturating_sub(footprint.Offset) instead of buffer_size - footprint.Offset avoids an underflow panic if a footprint offset ever exceeds buffer_size.

Clean fix for the last-row staging/readback overread — looks ready to merge!

**@⁠Yaekai** on GitHub · 2026-11-24 16:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/601#issuecomment-7137028505) Independent test & review on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head `4899516`: - `cargo test -p victory-gpu --lib`: 44 passed, 0 failed (including `upload_slice_stops_at_the_end_of_the_buffer`). - `cargo clippy -p victory-gpu -- -D warnings`: 0 warnings. - One minor defensive nit in `footprint_len()`: using `buffer_size.saturating_sub(footprint.Offset)` instead of `buffer_size - footprint.Offset` avoids an underflow panic if a footprint offset ever exceeds `buffer_size`. Clean fix for the last-row staging/readback overread — looks ready to merge!
Author
Owner

@⁠AwsAli05 on GitHub · 2026-11-24 20:18 UTC · original

Thanks for the independent checks. I addressed the out-of-range offset in 839be49. The slice length now uses saturating subtraction, and staging rejects a footprint at or beyond the buffer end before forming a pointer or recording a copy. The regression checks both boundary offsets as well as the two original mips.

On the branch merged with current main, the diagnostics suite passed 54 tests with one ignored benchmark. GPU Clippy passed with all targets and features.

**@⁠AwsAli05** on GitHub · 2026-11-24 20:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/601#issuecomment-7143330732) Thanks for the independent checks. I addressed the out-of-range offset in 839be49. The slice length now uses saturating subtraction, and staging rejects a footprint at or beyond the buffer end before forming a pointer or recording a copy. The regression checks both boundary offsets as well as the two original mips. On the branch merged with current main, the diagnostics suite passed 54 tests with one ignored benchmark. GPU Clippy passed with all targets and features.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:30 UTC · original · edited 2026-11-26 01:23 UTC

Automated triage for the maintainers.

Still a merge candidate. The only push since the last triage is 75135d7, a merge of current main. The PR's own diff is unchanged: the same subresource_len bound for the diagnostics note_read range in texture.rs::copies, the out-of-range footprint rejection in staging, and readback slices sized by the buffer's Width in capture.rs (save_read_back, dump_one), plus the device-backed BC4 test. The branch merges cleanly into current main, and CI (check) passes on this head.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/601#issuecomment-7148577126) · edited 2026-11-26 01:23 UTC <!-- victory-triage v1 head=75135d7a0448ee37cefb4c0b52f51fd457c98bf6 verdict=merge-candidate at=2026-11-26T01:23:17Z --> Automated triage for the maintainers. **Still a merge candidate.** The only push since the last triage is `75135d7`, a merge of current `main`. The PR's own diff is unchanged: the same `subresource_len` bound for the diagnostics `note_read` range in `texture.rs::copies`, the out-of-range footprint rejection in `staging`, and readback slices sized by the buffer's `Width` in `capture.rs` (`save_read_back`, `dump_one`), plus the device-backed BC4 test. The branch merges cleanly into current `main`, and CI (`check`) passes on this head. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠tyr0nsur on GitHub · 2026-11-25 01:49 UTC · original

An 8×4 BC4_UNORM texture with two mips gives a useful hardware case for the rebase. I called GetCopyableFootprints on main@940d32f on Windows 11 build 26300, desktop RTX 4060, NVIDIA driver 32.0.16.2114. Offsets were [0, 512], row counts [1, 1], row sizes [16, 8], and the entire upload allocation was 520 bytes.

Staging::detile already returns the correct 8 bytes for the last mip on main. The record diagnostic still computes 256 bytes for that mip, so its reported read extends 248 bytes past the allocation. Using the same bound for note_read would bring the trace into line with the copy. Diagnostic calculation on the tested revision

One locally added device-footprint test passed on that exact main revision with production code unchanged. The linked fixture contains the descriptor and D3D12 query. AI-assisted test and report (Codex). Reproducible BC4 fixture

**@⁠tyr0nsur** on GitHub · 2026-11-25 01:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/601#issuecomment-7149989674) An 8×4 BC4_UNORM texture with two mips gives a useful hardware case for the rebase. I called `GetCopyableFootprints` on `main@940d32f` on Windows 11 build 26300, desktop RTX 4060, NVIDIA driver 32.0.16.2114. Offsets were `[0, 512]`, row counts `[1, 1]`, row sizes `[16, 8]`, and the entire upload allocation was 520 bytes. `Staging::detile` already returns the correct 8 bytes for the last mip on main. The `record` diagnostic still computes 256 bytes for that mip, so its reported read extends 248 bytes past the allocation. Using the same bound for `note_read` would bring the trace into line with the copy. [Diagnostic calculation on the tested revision](https://github.com/Marvisak/VIctory/blob/940d32f69b22265690a0b5a15d6ded572ba33baf/crates/victory-gpu/src/texture.rs#L237-L263) One locally added device-footprint test passed on that exact main revision with production code unchanged. The linked fixture contains the descriptor and D3D12 query. AI-assisted test and report (Codex). [Reproducible BC4 fixture](https://github.com/tyr0nsur/PortVI/blob/3bc1e78a7368c40c9ec34406009b7d6c287e101c/patches/0012-bound-texture-staging-footprints.patch#L53-L81)
Author
Owner

@⁠AwsAli05 on GitHub · 2026-11-25 05:05 UTC · original

Thanks for the measured BC4 case. On current main the PR applies the same bounded length to note_read and the CPU upload. Commit 9235ae1 adds an 8×4 BC4 check that queries D3D12 and verifies that the last mip ends after its 8 active bytes. All three texture tests passed on an RTX 5070 Ti.

**@⁠AwsAli05** on GitHub · 2026-11-25 05:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/601#issuecomment-7153448085) Thanks for the measured BC4 case. On current main the PR applies the same bounded length to `note_read` and the CPU upload. Commit `9235ae1` adds an 8×4 BC4 check that queries D3D12 and verifies that the last mip ends after its 8 active bytes. All three texture tests passed on an RTX 5070 Ti.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:20 UTC · original

Code review and RTX 5090 + WARP verification at head 75135d7a0448 merged cleanly onto main (63bcca43c6a0):

  • Bounds audit (crates/victory-gpu/src/texture.rs:442-448, 697-701, 748-754, 831-834, capture.rs:220-222, 552):
    • subresource_len clamps pitch.saturating_mul(rows).saturating_mul(depth) to total.saturating_sub(offset), preventing out-of-bounds reads/writes on the unpadded final row of the last subresource (including 3D depth slices and compressed BC mips where RowPitch > row_bytes), and staging validates f.Offset < total across all footprints before building Staging.
    • capture.rs (save_read_back / dump_one) bounds mapped slices to buffer.GetDesc().Width as usize rather than RowPitch * Height.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-gpu --all-targets -- -D warnings (default + --features diagnostics): 0 warnings
    • RTX 5090 (texture:: capture::): 3/3 passed; WARP (--features diagnostics, texture:: capture::): 14/14 passed (including subresource_len_stays_inside_staging_total and last_bc4_mip_ends_after_the_active_block).

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/601#issuecomment-7179681582) Code review and RTX 5090 + WARP verification at head `75135d7a0448` merged cleanly onto `main` (`63bcca43c6a0`): - **Bounds audit (`crates/victory-gpu/src/texture.rs:442-448, 697-701, 748-754, 831-834`, `capture.rs:220-222, 552`)**: - `subresource_len` clamps `pitch.saturating_mul(rows).saturating_mul(depth)` to `total.saturating_sub(offset)`, preventing out-of-bounds reads/writes on the unpadded final row of the last subresource (including 3D depth slices and compressed BC mips where `RowPitch > row_bytes`), and `staging` validates `f.Offset < total` across all footprints before building `Staging`. - `capture.rs` (`save_read_back` / `dump_one`) bounds mapped slices to `buffer.GetDesc().Width as usize` rather than `RowPitch * Height`. - **Verified (Windows 11, NVIDIA RTX 5090 + WARP, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default + `--features diagnostics`): 0 warnings - RTX 5090 (`texture:: capture::`): **3/3 passed**; WARP (`--features diagnostics`, `texture:: capture::`): **14/14 passed** (including `subresource_len_stays_inside_staging_total` and `last_bc4_mip_ends_after_the_active_block`). LGTM.
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/601/head:github-bot/gh-601'
git switch 'github-bot/gh-601'

Merge

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