Skip to content

D3D12.X: reuse allocator, fence and staging buffer in title texture uploads - #465

Closed
kxiserino wants to merge 2 commits into
xcl-dev:mainfrom
kxiserino:perf/title-textures-batch-uploader
Closed

kxiserino wants to merge 2 commits into
xcl-dev:mainfrom
kxiserino:perf/title-textures-batch-uploader

Conversation

@kxiserino

@kxiserino kxiserino commented Nov 24, 2026 •

Copy link
Copy Markdown

What is in it

  • Add gpu::texture::BatchUploader in crates/xcl/src/gpu/texture.rs to reuse a per-device ID3D12CommandAllocator, ID3D12GraphicsCommandList, ID3D12Fence, wait HANDLE, and a persistently-mapped upload staging buffer (capped at 64 MiB; larger one-off batches fall back to a temporary staging buffer).
  • Retain the pending fence target and any temporary staging allocation across fence::wait timeouts so a later upload_batch call cannot overwrite staging or reset the command allocator while an earlier copy is still in flight; clear pending state and mark the uploader unusable on device removal.
  • Copy subresources with a single contiguous copy when row_bytes == RowPitch, falling back to row-by-row copies otherwise.
  • Wire hle::d3d12x::title_textures::upload_all to keep one BatchUploader per device in UPLOADERS instead of creating and destroying an allocator, command list, fence, event, and one upload buffer per texture on every ExecuteCommandLists refresh.
  • Add gpu_batch_uploader_reuses_staging_and_reports_partial_failures and gpu_batch_uploader_retains_pending_batch_and_blocks_reuse_until_completion in crates/xcl/src/gpu/texture.rs.

Tested

  • Windows 11 25H2 (build 26200), AMD Ryzen 9 9950X3D, NVIDIA GeForce RTX 3090 (driver 32.0.16.1714, wave32-only), DXC 1.9.2609.5.
  • cargo test -p xcl --lib gpu::texture -- --nocapture: 13 passed, 0 failed (including gpu_batch_uploader_reuses_staging_and_reports_partial_failures, gpu_batch_uploader_retains_pending_batch_and_blocks_reuse_until_completion, and gpu_console_textures_upload_every_mip_and_slice on the RTX 3090).
  • cargo test -p xcl --lib title_textures -- --nocapture: 7 passed, 0 failed (including gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes and placed_title_textures_share_vram_and_preserve_pixels on the RTX 3090).
  • cargo test --workspace -- --test-threads=4 (lib, on base 1152852): 636 passed, 1 failed (hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory, pre-existing failure in this environment; confirmed passing on RDNA3 by reviewer), 21 ignored; 2 GPU tests reported SKIPPED: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64 and gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64.
  • cargo test --workspace --test "*" -- --test-threads=4: 40 passed, 0 failed across all 12 integration test binaries.

Regression run

xcl-smoke v1
commit: a6f5f7a74cc620c1f88a9e3669a633d10c3c8212
gpu: NVIDIA GeForce RTX 3090
title: Game
seconds: 30
exit: 0x0
frames shown: 626 (PresentX 626)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14471 MB
xcl-smoke v1
commit: 13aa0ac57d9758773786f7dfb55140c5297101c5
gpu: NVIDIA GeForce RTX 3090
title: Game
seconds: 30
exit: 0x0
frames shown: 627 (PresentX 627)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14519 MB

Not tested

  • Wave64-capable GPUs (AMD / Intel) locally — RTX 3090 is wave32-only (RDNA3 tested in comments).
  • Real GPU hang/timeout induction (tested via synthetic pending fence in gpu_batch_uploader_retains_pending_batch_and_blocks_reuse_until_completion).
  • hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory failed in this environment (unrelated to this PR).

AI use

  • Implemented, tested, and verified with Antigravity (Gemini 4 Neon).

…ploads

Title texture batch uploads created and tore down a D3D12 command allocator, command list, fence, wait event, and one upload buffer per texture on every ExecuteCommandLists refresh (hundreds of times per second during video playback and streaming). Add gpu::texture::BatchUploader to reuse the allocator, list, fence, event, and a persistently-mapped upload staging buffer (capped at 64 MiB; larger one-off batches use a temporary buffer) per device, with contiguous copies when row pitch matches.

Assisted-by: Antigravity (Gemini 4 Neon)
@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 1152852 as it is (on its base 2997c9a).

  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 677 passed, 0 failed, 41 ignored, no SKIPPED tests (the two no wave64 tests run here).
  • The new gpu_batch_uploader_reuses_staging_and_reports_partial_failures passes. So do the GPU tests named in the description (gpu_console_textures_upload_every_mip_and_slice, gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes, placed_title_textures_share_vram_and_preserve_pixels). xmem_virtual_query_marks_cpu_readable_memory, which failed in your environment, passes here.

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.

@thehollowedone

Copy link
Copy Markdown

One upload-lifetime issue in 1152852. The fence helper can return a timeout with the submitted copy still pending. The caller logs the error and keeps the uploader cached. A later upload can then overwrite the staging buffer or reset the allocator before the previous copy has finished. Staging and allocator reuse · D3D12 allocator reset requirements

Could the uploader retain the submitted batch and gate reuse on confirmed fence completion, with device removal handled separately? A focused test could check that a timed-out batch keeps its staging allocation and blocks reuse until completion. I verified this path by reading the source and D3D12 contract. I did not induce a GPU timeout.

AI use
Codex with GPT-6.2 Sol traced the error path and drafted this review.

If fence::wait times out while a submitted texture copy is still in flight, BatchUploader previously dropped any temporary staging buffer and allowed the next upload_batch call to overwrite staging or reset the command allocator before the GPU finished. Retain the pending fence target and temporary staging allocation across timeouts, gate subsequent uploads on confirmed completion, handle device removal separately, and add a focused GPU test covering pending-batch retention and recovery.

Assisted-by: Antigravity (Gemini 4 Neon)
@kxiserino

Copy link
Copy Markdown
Author

Good catch @thehollowedone! Fixed in 13aa0ac: BatchUploader now keeps a PendingBatch (fence target + any temporary staging buffer) across timeouts and checks inish_pending before touching staging or resetting the command allocator, while handling FenceError::Removed separately by releasing pending staging and marking the uploader removed. Added gpu_batch_uploader_retains_pending_batch_and_blocks_reuse_until_completion covering pending-batch retention across timeout and recovery once the fence completes, plus a fresh 30s smoke run.

@grahamster2

grahamster2 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Tested on an RTX 3080 against main and the two other PRs for the same fix (#451, #455). #465 doesn't change the intro video here: it plays at ~4.5 fps, the same as main, and world streaming starts at 136–138 s instead of 140 s. There were no device removals.

Setup: RTX 3080 10 GB, driver 610.88, 32 GB RAM with ~15–17 GB used by other programs. Release builds of main 2997c9a (code-identical to a6f5f7a) and of #451 2cefc88, #455 07e6199 and #465 1152852. Each PR was tested on its own. The retail title ran 180 s per run with XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_PRESS_A_AT=14,17,21 on a warm cache. Numbers are from the activity (2 s) lines; the earlier patch test on db1ddb2 is here.

main #451 #455 #465
Runs 2 2 3 (2 ended in a device removal) 2
Intro video, presents per 2 s 8–10 (≈4.5 fps) 37–43 (≈20 fps) 8–11 (≈4.7 fps) 8–10 (≈4.5 fps)
Intro video, start → end 20 → 119 / 121 s 20 → 41 / 41 s 20 → 113 s 20 → 118 / 117 s
World streaming starts 140 / 140 s 60 / 60 s 134 s 138 / 136 s
Max presents per 2 s after that 35 / 35 45 / 58 47 55 / 48
Commit at the end 23.7 / 24.0 GB 24.5 / 24.4 GB 24.1 GB 24.5 / 25.1 GB
  • Every completed run was stopped by my watchdog (it kills XCL when free RAM falls below 1.5 GB). That happened 15–25 s after world streaming started, at about 24 GB commit, in every build. It's this machine's RAM limit, so the "after that" row covers only those seconds.
  • D3D12.X: title texture uploads run on the submitting queue, without a CPU wait #455: 2 of 3 runs ended in a device removal. The removal was 0x887a0006 (DEVICE_HUNG), at 13.5 s and 11.0 s, before the intro video. The log line is D3D12 device removed: 0x887a0006 … – the title is stopped, and then the process ends with status 0x887a0006. main and the other PRs had no removals in their runs.
  • smoke.ps1, 60 s: it presses nothing, so every build stays on the menu, and all four look the same there.
smoke commit frames shown last 3 removals crashes peak commit
main 2997c9a 1532 60 60 60 0 0 14613 MB
#451 2cefc88 1590 60 61 60 0 0 14723 MB
#455 07e6199 1518 61 60 59 0 0 14589 MB
#465 1152852 1528 60 59 61 0 0 14663 MB

All smoke runs reported exit: 0x0 and gpu: NVIDIA GeForce RTX 3080. I left the title line out of the table.

Not tested:

  • Picture correctness: I can't see the screen.
  • Anything past ~20 s of world streaming, because of the RAM limit.
  • Combinations of the PRs.

The same numbers are posted on the other PRs: #451, #455.

Test runs and this report: Claude Code (Claude Opus 5.5).

@kxiserino

Copy link
Copy Markdown
Author

Closing as superseded by merged #451 and #602, which already reuse upload memory and detile/hash in place.

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.

4 participants