GPU: give buffer-to-texture copies slices of the lists' scratch chunks #135

Open
SamuelHaleDev wants to merge 1 commit from upstream-pr/135 into main
SamuelHaleDev commented 2026-11-27 15:43:25 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by SamuelHaleDev on 2026-11-27T15:43:25Z.


Hardware

  • GPU: AMD Radeon RX 6650 XT, 8 GB (driver 32.0.21045.5002)
  • CPU: AMD Ryzen 5 5600X3D (6 cores, 12 threads)
  • RAM: 32 GB
  • OS: Windows 11 Pro
  • Profile: the plain "DexNom RayTime" shortcut (no --rt, no FSR). The shader cache was warmed first with victory-warm.exe.

Problem statement

CommandList::upload (host.rs, marked "shortcut: one buffer per copy") makes, zeroes and frees a committed upload buffer for every copy from title memory into a texture (CopyTextureRegion with a buffer footprint as the source, list.rs), so every such copy pays the driver's allocation cost.

Solution

Buffer-to-texture copies now take a slice of the list's existing scratch chunks instead of a buffer of their own. The scratch chunks already come from the shared spare pool (SPARE) and go back to it when the list is reset after its submissions have run. So the slices inherit the existing fence-based reuse and the existing 128 MiB cap on spare chunks, and need no new lifetime logic. Copies larger than a chunk keep the old path.

What changed

  • crates/victory-gpu/src/host.rs
    • CommandList::upload first tries the new upload_slice, then falls back to a buffer of its own. It now returns (buffer, offset, pointer) instead of (buffer, pointer).
    • New CommandList::upload_slice hands out slices of the list's scratch chunks, starting at multiples of 512 bytes (D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT). Afterwards it leaves used on a 256-byte boundary for scratch.
    • New switch upload_slices(): VICTORY_UPLOAD_SLICES=0 restores one buffer per copy.
    • New counters UPLOAD_SLICES: slices and own buffers, with their bytes.
  • crates/victory-gpu/src/list.rs: the buffer-to-texture copy uses the returned offset as the source footprint's offset, and in diagnostics builds adds it to the read address.
  • crates/victory-gpu/src/copystat.rs: the memory line prints copy upload sources since startup: N slices of scratch chunks, M buffers of their own.

Design decisions

  • Share the scratch chunks instead of a separate pool.
    • Trade-off: one pool for constants and copy sources means they compete for the same 128 MiB of spare chunks, and the two can't be tuned separately.
    • Why: reuse, fence-based lifetime and the cap already exist and were measured (run 60). Most copies are small and fit in chunks the list takes anyway.
  • Slices start on 512-byte boundaries.
    • Trade-off: up to 511 bytes are wasted per copy.
    • Why: D3D12 requires texture data placed in a buffer to start at a multiple of 512 bytes.
  • Round used back up to a multiple of 256 after a slice.
    • Trade-off: up to 255 more bytes wasted.
    • Why: scratch hands out from used on, and its constant-buffer data needs 256-byte alignment.
  • Copies larger than a 4 MiB chunk keep a buffer of their own.
    • Trade-off: large copies still pay the allocation.
    • Why: keeping chunks big enough for them would hold far more spare memory. None bypassed in the runs below.
  • The old path stays behind VICTORY_UPLOAD_SLICES=0.
    • Trade-off: a little extra code.
    • Why: a visual difference can be checked against the old path in one run, without rebuilding.
  • New counters instead of reading "upload buffers made".
    • Trade-off: one more line in the log.
    • Why: "made so far: N upload buffers" counts every mapped_upload, not only this path, so it can't tell what this change saved.

Baseline (64bd58f5, one buffer per copy)

Same save on every run. The camera was still for minute 3–4 of the run, looking out of a window in the opening interior. The numbers are the runtime's own 5-second frame lines and memory lines over that minute.

Measurement Value
Median frame time 1,025 ms
CPU preparing 272 ms
GPU 688 ms
Copying back 801 ms
Upload buffers made (all paths) 161 in 50 s (3.25/s)
VRAM in use 8,340 MB (budget 6,830 MB)
Private memory about 22.7 GB

After change (d8b27659)

Same save, profile and camera.

Measurement Value
Median frame time 1,014 ms
CPU preparing 230 ms
GPU 764 ms
Copying back 852 ms
Upload buffers made (all paths) 25 in 56 s (0.45/s, −86%)
Copy sources since startup 609 slices, 0 own buffers
VRAM in use 8,264 MB (budget 6,820 MB)
Private memory about 22.5 GB, flat
  • What it shows: the allocations go away. Upload buffers made per second dropped by 86%, and every copy took a slice.
  • What it doesn't show: a frame-time change. On this card and save, frames take about 1 s and copy-back dominates. The CPU-prep difference is within run-to-run noise (GPU and copy-back moved by similar amounts, and this change doesn't touch them), so I'm not claiming a speed-up.

Correctness:

  • Three captured runs: slices on, slices off (VICTORY_UPLOAD_SLICES=0) and slices on again.
  • Each saved every 10th presented frame from the first heavy frame (VICTORY_CAPTURE, VICTORY_CAPTURE_HEAVY_FRAMES), compared side by side.
  • The window view looked the same in all three.
  • The opening cutscene's exposure and timing varied from run to run whether slices were on or off. A speckle on one surface in the first slices-on run did not come back in the second.
  • No errors in the logs, and cargo test -p victory-gpu passed (239 passed, 4 ignored).
  • The frames themselves aren't attached, because they are game content.

Not checked:

  • The --rt / FSR Quality 4K profile.
  • NVIDIA.
  • Runs longer than about 5 minutes.

Notes

  • AI-assisted (Claude Code), built and tested by me on the hardware above.
  • cargo xtask policy passes. cargo fmt --check reports many existing differences across these files; the new code follows the style around it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:DexNom/VIctory:issue:5529333921 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/135). Originally posted by [SamuelHaleDev](https://github.com/SamuelHaleDev) on 2026-11-27T15:43:25Z. --- ## Hardware - GPU: AMD Radeon RX 6650 XT, 8 GB (driver 32.0.21045.5002) - CPU: AMD Ryzen 5 5600X3D (6 cores, 12 threads) - RAM: 32 GB - OS: Windows 11 Pro - Profile: the plain "DexNom RayTime" shortcut (no `--rt`, no FSR). The shader cache was warmed first with `victory-warm.exe`. ## Problem statement `CommandList::upload` (`host.rs`, marked "shortcut: one buffer per copy") makes, zeroes and frees a committed upload buffer for every copy from title memory into a texture (`CopyTextureRegion` with a buffer footprint as the source, `list.rs`), so every such copy pays the driver's allocation cost. ## Solution Buffer-to-texture copies now take a slice of the list's existing scratch chunks instead of a buffer of their own. The scratch chunks already come from the shared spare pool (`SPARE`) and go back to it when the list is reset after its submissions have run. So the slices inherit the existing fence-based reuse and the existing 128 MiB cap on spare chunks, and need no new lifetime logic. Copies larger than a chunk keep the old path. ## What changed - `crates/victory-gpu/src/host.rs` - `CommandList::upload` first tries the new `upload_slice`, then falls back to a buffer of its own. It now returns `(buffer, offset, pointer)` instead of `(buffer, pointer)`. - New `CommandList::upload_slice` hands out slices of the list's scratch chunks, starting at multiples of 512 bytes (`D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT`). Afterwards it leaves `used` on a 256-byte boundary for `scratch`. - New switch `upload_slices()`: `VICTORY_UPLOAD_SLICES=0` restores one buffer per copy. - New counters `UPLOAD_SLICES`: slices and own buffers, with their bytes. - `crates/victory-gpu/src/list.rs`: the buffer-to-texture copy uses the returned offset as the source footprint's offset, and in diagnostics builds adds it to the read address. - `crates/victory-gpu/src/copystat.rs`: the memory line prints `copy upload sources since startup: N slices of scratch chunks, M buffers of their own`. ## Design decisions - **Share the scratch chunks instead of a separate pool.** - Trade-off: one pool for constants and copy sources means they compete for the same 128 MiB of spare chunks, and the two can't be tuned separately. - Why: reuse, fence-based lifetime and the cap already exist and were measured (run 60). Most copies are small and fit in chunks the list takes anyway. - **Slices start on 512-byte boundaries.** - Trade-off: up to 511 bytes are wasted per copy. - Why: D3D12 requires texture data placed in a buffer to start at a multiple of 512 bytes. - **Round `used` back up to a multiple of 256 after a slice.** - Trade-off: up to 255 more bytes wasted. - Why: `scratch` hands out from `used` on, and its constant-buffer data needs 256-byte alignment. - **Copies larger than a 4 MiB chunk keep a buffer of their own.** - Trade-off: large copies still pay the allocation. - Why: keeping chunks big enough for them would hold far more spare memory. None bypassed in the runs below. - **The old path stays behind `VICTORY_UPLOAD_SLICES=0`.** - Trade-off: a little extra code. - Why: a visual difference can be checked against the old path in one run, without rebuilding. - **New counters instead of reading "upload buffers made".** - Trade-off: one more line in the log. - Why: "made so far: N upload buffers" counts every `mapped_upload`, not only this path, so it can't tell what this change saved. ## Baseline (`64bd58f5`, one buffer per copy) Same save on every run. The camera was still for minute 3–4 of the run, looking out of a window in the opening interior. The numbers are the runtime's own 5-second frame lines and memory lines over that minute. | Measurement | Value | | --- | ---: | | Median frame time | 1,025 ms | | CPU preparing | 272 ms | | GPU | 688 ms | | Copying back | 801 ms | | Upload buffers made (all paths) | 161 in 50 s (3.25/s) | | VRAM in use | 8,340 MB (budget 6,830 MB) | | Private memory | about 22.7 GB | ## After change (`d8b27659`) Same save, profile and camera. | Measurement | Value | | --- | ---: | | Median frame time | 1,014 ms | | CPU preparing | 230 ms | | GPU | 764 ms | | Copying back | 852 ms | | Upload buffers made (all paths) | 25 in 56 s (0.45/s, −86%) | | Copy sources since startup | 609 slices, 0 own buffers | | VRAM in use | 8,264 MB (budget 6,820 MB) | | Private memory | about 22.5 GB, flat | - **What it shows:** the allocations go away. Upload buffers made per second dropped by 86%, and every copy took a slice. - **What it doesn't show:** a frame-time change. On this card and save, frames take about 1 s and copy-back dominates. The CPU-prep difference is within run-to-run noise (GPU and copy-back moved by similar amounts, and this change doesn't touch them), so I'm not claiming a speed-up. **Correctness:** - Three captured runs: slices on, slices off (`VICTORY_UPLOAD_SLICES=0`) and slices on again. - Each saved every 10th presented frame from the first heavy frame (`VICTORY_CAPTURE`, `VICTORY_CAPTURE_HEAVY_FRAMES`), compared side by side. - The window view looked the same in all three. - The opening cutscene's exposure and timing varied from run to run whether slices were on or off. A speckle on one surface in the first slices-on run did not come back in the second. - No errors in the logs, and `cargo test -p victory-gpu` passed (239 passed, 4 ignored). - The frames themselves aren't attached, because they are game content. **Not checked:** - The `--rt` / FSR Quality 4K profile. - NVIDIA. - Runs longer than about 5 minutes. ## Notes - AI-assisted (Claude Code), built and tested by me on the hardware above. - `cargo xtask policy` passes. `cargo fmt --check` reports many existing differences across these files; the new code follows the style around it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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/135:upstream-pr/135'
git switch 'upstream-pr/135'

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/135'
git switch 'upstream-pr/135'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/135'
git switch 'upstream-pr/135'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/135'
git switch 'main'
git merge --squash 'upstream-pr/135'
git switch 'main'
git merge --ff-only 'upstream-pr/135'
git switch 'main'
git merge 'upstream-pr/135'
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
DexNom/VIctory!135
No description provided.