Skip to content

D3D12.X: faster title texture uploads (reused upload memory, faster detiling) - #451

Open
1984742628 wants to merge 3 commits into
ivall:mainfrom
1984742628:perf/reuse-upload-memory
Open

1984742628 wants to merge 3 commits into
ivall:mainfrom
1984742628:perf/reuse-upload-memory

Conversation

@1984742628

Copy link
Copy Markdown

A retail title that rewrites textures with the CPU (kept in step with its memory by title_textures) fell to 1 – 3 frames a second on an NVIDIA RTX 3070 Ti once it streamed them. The time went to uploading those textures inside the title's own submit and present: D3D12 objects made and released for every upload, then detiling every texel through a full offset computation, one texture after another. This PR makes the uploads reuse their D3D12 objects and detiles faster and in parallel. The regression run below shows 1482 frames in 60 s against main's 720 (2.06×), and 30 frames a second at the end against about 14.

What is in it

  • D3D12.X: title texture uploads reuse their queue's buffer and list (gpu/texture.rs): upload_batch made an upload buffer per texture and an allocator, a list and a fence per batch – each a call that waits for the video memory manager, 200 – 650 ms apiece while the title streamed (timed in its runs). Each upload queue now keeps one persistently mapped upload buffer (grown only when a batch needs more; textures at 512-byte aligned offsets), its allocator and list (reset) and a fence. When a batch's wait times out, the set is retired and kept alive instead of released under the GPU, which removed the device. record_upload_states keeps its behaviour for its other callers.
  • Textures: detiling a mip level computes column and row parts once (gpu/tiling.rs): Chain::detile_level computed Layout::offset_z for every element (divisions, modulos, six lookups). The x-only parts are now computed once per level and the y/slice parts once per row: two lookups and an XOR per element, byte for byte the same result.
  • D3D12.X: the title textures of a big batch are detiled on several threads (hle/d3d12x/title_textures.rs): batches of 1 MB or more are detiled on up to 8 threads (half the cores), in order; smaller batches stay on the calling thread.
  • Tests: detile_level_matches_element_by_element (every mode, element size, pipe configuration and level including mip tails, a base swizzle, two slices, a source cut short), gpu_upload_batches_reuse_the_queues_upload_buffer (two batches of three textures – mips, an odd size, BC7 – read back exactly, and the second batch made no new upload buffer), a_batch_detiled_on_several_threads_matches_one_thread (2, 3 and 8 threads against one).

Tested

  • hardware (GPU + driver, CPU), Rust version, debug / release: NVIDIA GeForce RTX 3070 Ti 8 GB (driver 32.0.16.2124), AMD Ryzen 9 7950X (16 cores / 32 threads), 32 GB RAM, Windows 11 10.0.26100, rustc 1.98.0; tests in debug, title runs in release.
  • cargo test --workspace -- --test-threads=4 on a6f5f7a with these changes (the same code as 2cefc88): 679 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2, both "no wave64": gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). The XCL_SAMPLE_* variables were not set, so the tests on game data did nothing.
  • Each commit builds with its tests on its own (cargo check --release -p xcl --tests at c55783e and 7d60a48).
  • The retail title, 70 s runs with XCL_ACTIVITY=1, thread dumps at 25 / 45 / 60 s and per-thread CPU sampled every 5 s (the dumps pause the title, so these frame counts are lower than the regression run's), one run per step: main 355 frames, with the first commit 516, with the second 964, with all three 1352. In the streaming phase (after about 28 s) frames a second went from 1 – 3 to about 20, then 30. With all three, the busiest thread in that phase is one of the title's own workers in the title's code, not XCL.

Regression run
scripts/smoke.ps1 -Game <the title's folder> -Xcl target\release\xcl.exe (60 s), built from main and from this PR's head with no uncommitted changes. (Windows PowerShell 5.1 does not set $PSScriptRoot in the parameter default, hence -Xcl.)

main:

xcl-smoke v1
commit: a6f5f7a74cc620c1f88a9e3669a633d10c3c8212
gpu: NVIDIA GeForce RTX 3070 Ti
title: RetailGame
seconds: 60
exit: 0x0
frames shown: 720 (PresentX 721)
presents per 2 s (last 3): 30 29 26
device removals: 0
crashes: 0
peak commit: 14594 MB

PR:

xcl-smoke v1
commit: 2cefc889564d5216618f65f9f2c3c32ce1d64f64
gpu: NVIDIA GeForce RTX 3070 Ti
title: RetailGame
seconds: 60
exit: 0x0
frames shown: 1482 (PresentX 1482)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14706 MB

Not tested

  • One title, one run per configuration: no averages over repeated runs.
  • Not tested on AMD or Intel GPUs.
  • tests/texture_states.rs (upload barriers checked by the D3D12 debug layer) skips here: the debug layer is not installed.
  • The upload buffer is kept per queue for the process and only grows (up to a batch: BATCH_BYTES, 256 MB, plus one texture); it is not shrunk.

AI use

  • Claude Code with Claude Opus 5.5: profiled the title's runs (per-thread CPU, thread dumps named from a symbol build), wrote the three changes and their tests, ran the test suite, the title runs and the regression runs above, and wrote the commit messages and this description.

1984742628 and others added 3 commits November 24, 2026 01:26
`gpu::texture::upload_batch` made a new upload buffer for every texture
and a new command allocator, command list and fence for every batch,
and released them all again. Each of these D3D12 calls waits for the
video memory manager. While a retail title streamed textures the CPU
writes, they took 200 - 650 ms apiece (timed in the title's runs), and
its frames fell to 1 - 3 a second.

Each upload queue now keeps an `Uploader`: one persistently mapped
upload buffer that the batch's textures share (each at a 512-byte
aligned offset; it grows only when a batch needs more room), an
allocator and list that are reset, and a fence whose value counts up.
When the wait for a batch times out, the uploader is retired and kept
alive instead of released: the GPU may still read the buffer and run
the list, and releasing them under it removed the device.

`record_upload_states` is split into `layout` and `record_into`; it
keeps its behaviour (a buffer of its own) for its other callers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`Chain::detile_level` called `element_at` / `Layout::offset_z` for every
element: a `div_ceil`, several divisions and modulos and six table
lookups each. Once the upload buffers were reused, it was most of a
retail title's render thread while the title streamed textures the CPU
writes (thread samples, frames named from a symbol build).

The parts of the offset that depend on x alone (block column and the
x and high-x pattern bits) are now computed once per level, those that
depend on y and the slice once per row, leaving two lookups and an XOR
per element. The result is the same: a new test compares it with
`element_at` for every mode, element size, pipe configuration and level
(mip tails too), with a base swizzle, two slices and a source cut short.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eads

`title_textures::upload_all` read and detiled each texture of a batch
one after another, inside the title's own submit and present. Batches of
1 MB or more are now detiled on up to 8 threads (half the cores), each
taking the next texture; the results keep the batch's order. Smaller
batches stay on the calling thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@emirduran707

Copy link
Copy Markdown

This overlaps #440 in Chain::detile_level (they conflict textually), but the two speed up different layouts, so they combine rather than compete. detile_level alone, single thread, release, on a Ryzen 5 5600 (local timing test, not committed; same harness on both heads):

level main this PR (2cefc88) #440 (be722a7)
3840x2160 R8, linear (an intro-video plane) 56.9 ms 21.2 ms 1.2 ms
1920x1080 R8, linear 14.4 ms 5.0 ms 0.3 ms
2048x2048 RGBA8, 64KB_S, 8 pipes 48.4 ms 16.0 ms 48.4 ms (unchanged)

A linear level's rows are contiguous, so #440 copies them whole; your per-row form is what the swizzled modes need. Whichever lands first, I'll rebase #440 onto it as a linear fast path in front of your loop (a few lines plus its equivalence test) – or feel free to fold it into this PR if you prefer, and I'll close #440.

AI use: Claude Code with Claude Opus 5.5 ran the timings and wrote this comment.

@thehollowedone

Copy link
Copy Markdown

Tested

Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti (NVIDIA 621.35), Rust 1.99.1 MSVC, debug. Commit 2cefc889

cargo test --locked -p xcl --lib -- detile_level_matches_element_by_element gpu_upload_batches_reuse_the_queues_upload_buffer a_batch_detiled_on_several_threads_matches_one_thread --test-threads=1 --nocapture: 3 passed, 0 failed, 0 ignored, zero reported skips. The GPU check matched the readbacks from both batches and confirmed the second batch reused the staging buffer. The two CPU checks matched the element-by-element detiler and the single-threaded batch output.

Not tested

Full workspace suite, AMD, retail titles, and performance.

AI use

Codex with GPT-6.2 Sol ran the tests and drafted this report.

@1984742628

Copy link
Copy Markdown
Author

This overlaps #440 in Chain::detile_level (they conflict textually), but the two speed up different layouts, so they combine rather than compete. detile_level alone, single thread, release, on a Ryzen 5 5600 (local timing test, not committed; same harness on both heads):

level main this PR (2cefc88) #440 (be722a7)
3840x2160 R8, linear (an intro-video plane) 56.9 ms 21.2 ms 1.2 ms
1920x1080 R8, linear 14.4 ms 5.0 ms 0.3 ms
2048x2048 RGBA8, 64KB_S, 8 pipes 48.4 ms 16.0 ms 48.4 ms (unchanged)
A linear level's rows are contiguous, so #440 copies them whole; your per-row form is what the swizzled modes need. Whichever lands first, I'll rebase #440 onto it as a linear fast path in front of your loop (a few lines plus its equivalence test) – or feel free to fold it into this PR if you prefer, and I'll close #440.

AI use: Claude Code with Claude Opus 5.5 ran the timings and wrote this comment.

Do you want to merge our PRs?

@PHeitor1

Copy link
Copy Markdown

Tested 2cefc88 merged onto main 2997c9a.

RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 679 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). Build clean.

Run and written with Claude Code (Claude Opus 5.5).

@TheHoracio

Copy link
Copy Markdown
Contributor

Test report on AMD RDNA3: RX 7800 XT (16 GB, wave32 and wave64), driver 32.0.32015.7029, Ryzen 7 9800X3D, Windows 11 Pro 10.0.26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17, debug build. No retail data was used. The PR's own runs are all on NVIDIA, so this adds an AMD GPU with 64-lane waves.

Tested: the PR head 2cefc88 as it is (on its base a6f5f7a).

  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 679 passed, 0 failed, 41 ignored, no SKIPPED tests (the two no wave64 tests run here).
  • The three new tests pass: gpu_upload_batches_reuse_the_queues_upload_buffer (GPU), detile_level_matches_element_by_element and a_batch_detiled_on_several_threads_matches_one_thread.

Not tested: a regression run on a title, a release build, the head merged onto current main.

AI use: Claude Code ran the commands and drafted this comment; the runs are real runs on the hardware above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants