gpu: parallelize texture detiling across subresources and textures #184

Closed
lucastol3do wants to merge 1 commit from upstream-pr/184 into main
lucastol3do commented 2026-11-24 01:15:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lucastol3do on 2026-11-24T01:15:12Z.


Problem

Texture detiling — converting the console's tiled memory layouts into linear staging buffers — runs on a single thread at submission time. When the title loads a batch of new textures (~12.7 MB), the detiling costs ~22 ms, enough to drop the frame rate from 30 fps to 25-28 fps on a multi-core CPU that is otherwise idle.

Fix

Two levels of parallelism, both via std::thread::scope (no new dependencies):

  1. Within a texture (Staging::detile): spawns one thread per subresource (mip level / array slice). Each writes to a non-overlapping region of the mapped staging buffer, so the slices are independent.

  2. Across textures (run_deferred): collects all Step::Host closures and runs them in a scope instead of one by one. Step::Host does not interact with the shadow-buffer batch, so reordering relative to Copy / Run / Refresh steps is safe.

Test hardware

CPU 2× Intel Xeon E5-2680 v4 @ 2.40 GHz (28 cores)
RAM 64 GB
GPU NVIDIA GeForce RTX 3070 (8 GB VRAM)
OS Windows 11
Mode Xbox Series X, 4K (3840×2160)

Result

On the hardware above, with the title running in Xbox Series X mode at 4K:

before after
preparing spike ~23 ms ~16 ms
fps during spike 25-28 30
fps normal frames 30 30

The remaining ~16 ms is dominated by a single large mip 0 that cannot be split at the subresource level; parallelizing inside Layout::walk (row-level, in victory-formats) would close the rest of the gap.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572359209 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/184). Originally posted by [lucastol3do](https://github.com/lucastol3do) on 2026-11-24T01:15:12Z. --- ## Problem Texture detiling — converting the console's tiled memory layouts into linear staging buffers — runs on a single thread at submission time. When the title loads a batch of new textures (~12.7 MB), the detiling costs ~22 ms, enough to drop the frame rate from 30 fps to 25-28 fps on a multi-core CPU that is otherwise idle. ## Fix Two levels of parallelism, both via `std::thread::scope` (no new dependencies): 1. **Within a texture** (`Staging::detile`): spawns one thread per subresource (mip level / array slice). Each writes to a non-overlapping region of the mapped staging buffer, so the slices are independent. 2. **Across textures** (`run_deferred`): collects all `Step::Host` closures and runs them in a scope instead of one by one. `Step::Host` does not interact with the shadow-buffer batch, so reordering relative to `Copy` / `Run` / `Refresh` steps is safe. ## Test hardware | | | |---|---| | CPU | 2× Intel Xeon E5-2680 v4 @ 2.40 GHz (28 cores) | | RAM | 64 GB | | GPU | NVIDIA GeForce RTX 3070 (8 GB VRAM) | | OS | Windows 11 | | Mode | Xbox Series X, 4K (3840×2160) | ## Result On the hardware above, with the title running in Xbox Series X mode at 4K: | | before | after | |---|---|---| | preparing spike | ~23 ms | ~16 ms | | fps during spike | 25-28 | **30** | | fps normal frames | 30 | 30 | The remaining ~16 ms is dominated by a single large mip 0 that cannot be split at the subresource level; parallelizing inside `Layout::walk` (row-level, in `victory-formats`) would close the rest of the gap.
DaviTostes commented 2026-11-24 06:54:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:21:43Z.


Tested c9dbb6a under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with cargo xwin ... --target x86_64-pc-windows-msvc and a native Microsoft d3dcompiler_47: pass, no regression.

  • victory_gpu: 37 passed, 2 failed.
  • The two failures are the environment-only a_rejected_blob_gives_an_empty_library (vkd3d needs a non-empty blob) and #128.
  • The diff adds no test for the parallel detiling; this run only shows nothing else broke.

No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119023581 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7119023581). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:21:43Z. --- Tested `c9dbb6a` under **Wine 11.22 + vkd3d-proton 3.1** on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with `cargo xwin ... --target x86_64-pc-windows-msvc` and a native Microsoft `d3dcompiler_47`: **pass, no regression**. - `victory_gpu`: **37 passed, 2 failed**. - The two failures are the environment-only `a_rejected_blob_gives_an_empty_library` (vkd3d needs a non-empty blob) and #128. - The diff adds no test for the parallel detiling; this run only shows nothing else broke. No game data used.
Sunkiller-qc commented 2026-11-24 06:54:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Sunkiller-qc on 2026-11-24T01:29:26Z.


Two problems in run_deferred (crates/victory-gpu/src/host.rs:1183):

  1. Ordering: Step::Host work is collected (host.rs:1231) and only run after the whole loop (host.rs:1246). On main it runs in order with the list's copies (host.rs:1199). The rectangle expansion in draw.rs:871 is a defer_host step that reads title vertex memory, so a Copy or Run step recorded after the draw that writes that buffer now lands before the expansion reads it.
  2. Lifetime: for step in &self.deferred().copies only holds the guard for the loop, so the SyncFnRef pointers are used after the MutexGuard is dropped. Binding the guard (let deferred = self.deferred();) until the host work is done would make the SAFETY comment true.

Also, one OS thread per host step and per subresource (texture.rs:258) can mean dozens of spawns per submission for a mip chain. A bounded pool, or only splitting above a size threshold, would be safer for frame time. Without diagnostics, the second started is unused and will warn.

Static review, not built or run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119150258 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7119150258). Originally posted by [Sunkiller-qc](https://github.com/Sunkiller-qc) on 2026-11-24T01:29:26Z. --- Two problems in `run_deferred` (`crates/victory-gpu/src/host.rs:1183`): 1. Ordering: `Step::Host` work is collected (`host.rs:1231`) and only run after the whole loop (`host.rs:1246`). On main it runs in order with the list's copies (`host.rs:1199`). The rectangle expansion in `draw.rs:871` is a `defer_host` step that reads title vertex memory, so a `Copy` or `Run` step recorded after the draw that writes that buffer now lands before the expansion reads it. 2. Lifetime: `for step in &self.deferred().copies` only holds the guard for the loop, so the `SyncFnRef` pointers are used after the `MutexGuard` is dropped. Binding the guard (`let deferred = self.deferred();`) until the host work is done would make the SAFETY comment true. Also, one OS thread per host step and per subresource (`texture.rs:258`) can mean dozens of spawns per submission for a mip chain. A bounded pool, or only splitting above a size threshold, would be safer for frame time. Without `diagnostics`, the second `started` is unused and will warn. Static review, not built or run.
lucastol3do commented 2026-11-24 06:55:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lucastol3do on 2026-11-24T01:36:29Z.


Thanks for the review and the Wine test.

All four points are addressed in 9ddeffe (force-pushed):

1. Ordering. run_deferred now matches main exactly — Step::Host runs in order with Copy/Run, not collected and deferred. The SyncFnRef wrapper and the post-loop parallel section are gone. Only Staging::detile is parallelized (subresource loop), which is safe: each subresource writes to a non-overlapping region of the mapped staging buffer and does not interact with the shadow-buffer batch.

2. Lifetime. Moot — SyncFnRef is removed. There is no cross-texture parallelism in run_deferred anymore, so no pointer-outliving-guard issue.

3. Thread spawn. Staging::detile now only spawns when items.len() > 4 (texture.rs:258). A texture with 1–4 subresources detiles on the calling thread; only larger mip chains or arrays split. The std::thread::scope still creates one thread per subresource above the threshold, but the threshold keeps the common case (a single large mip 0) single-threaded and caps the spawn count for the rest.

4. Unused started. Moot — the second started and its #[cfg(feature = "diagnostics")] block are gone with the post-loop section.

The shader fallbacks in lower.rs (untracked masks → use current block; unknown stride → default 4) are unchanged in this push.

@daviTostes — thanks for the cross-platform check. You're right that the diff adds no test for the parallel detiling; I'll add one if this approach is kept.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119269373 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7119269373). Originally posted by [lucastol3do](https://github.com/lucastol3do) on 2026-11-24T01:36:29Z. --- Thanks for the review and the Wine test. All four points are addressed in `9ddeffe` (force-pushed): **1. Ordering.** `run_deferred` now matches main exactly — `Step::Host` runs in order with `Copy`/`Run`, not collected and deferred. The `SyncFnRef` wrapper and the post-loop parallel section are gone. Only `Staging::detile` is parallelized (subresource loop), which is safe: each subresource writes to a non-overlapping region of the mapped staging buffer and does not interact with the shadow-buffer batch. **2. Lifetime.** Moot — `SyncFnRef` is removed. There is no cross-texture parallelism in `run_deferred` anymore, so no pointer-outliving-guard issue. **3. Thread spawn.** `Staging::detile` now only spawns when `items.len() > 4` (`texture.rs:258`). A texture with 1–4 subresources detiles on the calling thread; only larger mip chains or arrays split. The `std::thread::scope` still creates one thread per subresource above the threshold, but the threshold keeps the common case (a single large mip 0) single-threaded and caps the spawn count for the rest. **4. Unused `started`.** Moot — the second `started` and its `#[cfg(feature = "diagnostics")]` block are gone with the post-loop section. The shader fallbacks in `lower.rs` (untracked masks → use current block; unknown stride → default 4) are unchanged in this push. @daviTostes — thanks for the cross-platform check. You're right that the diff adds no test for the parallel detiling; I'll add one if this approach is kept.
r4tedx commented 2026-11-24 06:55:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-24T03:29:44Z.


Review note: this merges cleanly onto current main (d2a4bae) but the result does not compile, because main gained a deferred step since the branch was cut:

error[E0004]: non-exhaustive patterns: `&host::Step::Texture(_)` not covered
    --> crates\victory-gpu\src\host.rs:1253:32
     |
1253 |             let copied = match step {

main alone builds here, so it needs a rebase with an arm for Step::Texture. The shader-crate half passes its tests on its own (RTX 5070).

From building only; I have no copy of the title.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121151344 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7121151344). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-24T03:29:44Z. --- Review note: this merges cleanly onto current `main` (`d2a4bae`) but the result does not compile, because `main` gained a deferred step since the branch was cut: ``` error[E0004]: non-exhaustive patterns: `&host::Step::Texture(_)` not covered --> crates\victory-gpu\src\host.rs:1253:32 | 1253 | let copied = match step { ``` `main` alone builds here, so it needs a rebase with an arm for `Step::Texture`. The shader-crate half passes its tests on its own (RTX 5070). From building only; I have no copy of the title.
TrashTalker3460 commented 2026-11-24 06:56:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TrashTalker3460 on 2026-11-24T05:50:43Z.


Review note on head 9ddeffe: besides the parallel detile, this PR changes crates/victory-shader/src/emit/lower.rs. It removes six refusals:

  • "a wave reduction's untracked mask" (two sites)
  • "a select on an untracked mask"
  • "a carry-in from an untracked mask"
  • "a lane mask holding lanes elsewhere read as data"
  • "a lane-mask join of an untracked mask"

Three sites now fall back to unwrap_or(Bool::TRUE) (lines 726, 746, 3922).

A mask the translator cannot track is then treated as "all lanes active". A shader that used to be refused with a reason now translates, possibly wrong, with no error or log. Could these hunks move to their own PR with tests showing the TRUE fallback is correct, or be dropped from this one? The detile change doesn't depend on them.

Read-only review, nothing built. AI-assisted (Claude Code); the claim above was checked against this head and main at d2a4bae. Context: the triage in #366.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123328593 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7123328593). Originally posted by [TrashTalker3460](https://github.com/TrashTalker3460) on 2026-11-24T05:50:43Z. --- Review note on head `9ddeffe`: besides the parallel detile, this PR changes `crates/victory-shader/src/emit/lower.rs`. It removes six refusals: - "a wave reduction's untracked mask" (two sites) - "a select on an untracked mask" - "a carry-in from an untracked mask" - "a lane mask holding lanes elsewhere read as data" - "a lane-mask join of an untracked mask" Three sites now fall back to `unwrap_or(Bool::TRUE)` (lines 726, 746, 3922). A mask the translator cannot track is then treated as "all lanes active". A shader that used to be refused with a reason now translates, possibly wrong, with no error or log. Could these hunks move to their own PR with tests showing the `TRUE` fallback is correct, or be dropped from this one? The detile change doesn't depend on them. _Read-only review, nothing built. AI-assisted (Claude Code); the claim above was checked against this head and `main` at `d2a4bae`. Context: the triage in #366._
upstream-sync closed this pull request 2026-11-24 09:05:05 +00:00

Pull request closed

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