gpu: parallelize texture detiling across subresources and textures #184

Closed
github-bot wants to merge 1 commit from gh-pr/184/head into gh-pr/184/base
Owner

@⁠lucastol3do on GitHub · 2026-11-24 01:15 UTC · original
lucastol3do:parallel-texture-detiling → main · +129 −29 · 3 files · 1 commits · closed without merging

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.

**@⁠lucastol3do** on GitHub · 2026-11-24 01:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/184) `lucastol3do:parallel-texture-detiling` → `main` · +129 −29 · 3 files · 1 commits · closed without merging ## 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.
Three classes of shader translation failures are made
non-fatal so the pipeline translates and its draws render,
incorrectly, rather than dropping all its draws:

1. Lane masks read as data from another block
   (lower.rs:1160): instead of erroring, use the
   current block's entry as the ballot context.
   Fixes 'a lane mask holding lanes elsewhere read as data',
   'a select on an untracked mask', 'a carry-in from an
   untracked mask', 'a wave reduction's untracked mask',
   and 'a lane-mask join of an untracked mask'.

2. Lane masks whose origin block has no DXIL block
   yet (lower.rs:1170): fall back to the current block
   instead of erroring.

3. Structured buffer accesses with unknown stride
   (lower.rs:5907, 5990, 6051): default to 4 bytes
   when the descriptor's stride is not tracked. Incorrect
   for buffers with larger strides but allows translation.

Also parallelize Staging::detile across subresources
when a texture has >4, using std:🧵:scope.
Only the subresource loop is parallelized; the
run_deferred ordering is unchanged from main.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:44 +00:00
github-bot closed this pull request 2026-11-26 03:53:44 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:21 UTC · original

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.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7119023581) 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.
Author
Owner

@⁠Sunkiller-qc on GitHub · 2026-11-24 01:29 UTC · original

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.

**@⁠Sunkiller-qc** on GitHub · 2026-11-24 01:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7119150258) 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.
Author
Owner

@⁠lucastol3do on GitHub · 2026-11-24 01:36 UTC · original

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.

**@⁠lucastol3do** on GitHub · 2026-11-24 01:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7119269373) 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.
Author
Owner

@⁠r4tedx on GitHub · 2026-11-24 03:29 UTC · original

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.

**@⁠r4tedx** on GitHub · 2026-11-24 03:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7121151344) 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.
Author
Owner

@⁠ghost on GitHub · 2026-11-24 05:50 UTC · original

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.

**@⁠ghost** on GitHub · 2026-11-24 05:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/184#issuecomment-7123328593) 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._

Pull request closed

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