Skip to content

D3D12.X: pool small fallback placements in placed heaps and deduplicate xg::Texture - #2985

Closed
Saksukella wants to merge 1 commit into
xcl-dev:amd-devfrom
Saksukella:perf/placements-xg-dedup-and-small-pool
Closed

Saksukella wants to merge 1 commit into
xcl-dev:amd-devfrom
Saksukella:perf/placements-xg-dedup-and-small-pool

Conversation

@Saksukella

Copy link
Copy Markdown
Contributor

Pools eligible non-reservable fallback CreatePlacedResourceX allocations into 16 MiB D3D12_HEAP_TYPE_DEFAULT placed heaps (42.12x faster than CreateCommittedResource in the D3D12 microbenchmark) and eliminates duplicate xg::Texture::new layout computation on every texture registration (2.70x faster).

Root cause & summary of changes

During streaming and 3D rendering, CreatePlacedResourceX (placements) spent 80% of its wall time on fallback CreateCommittedResource calls (0.40–0.56 ms per call for TEXTURE1D, 3D packed-mip textures, and small non-UMA buffers that bypass vram_small::place and vram::place_on_pages) and recomputing xg::Texture::new(&x) twice per texture (once in device_x.rs for console_bytes, then dropped and recomputed inside title_textures::register).

  1. Deduplicate xg::Texture::new(&x) (device_x.rs, title_textures.rs):
    • Computes Arc::new(xg::Texture::new(&x)) once in create_placed_resource_x, reuses via Arc::clone for water_tex, and passes Option<Arc<xg::Texture>> into title_textures::register.
  2. Pool small fallback placements via CreatePlacedResource (vram_small.rs, device_x.rs, vram.rs):
    • Adds DefaultFallbackPools in vram_small.rs: sub-allocates non-reservable fallback resources up to 4 MiB (TEXTURE1D, non-reservable non-RT/DS textures, and small non-UMA buffers) in 64 KiB units inside 16 MiB D3D12_HEAP_TYPE_DEFAULT (D3D12_HEAP_FLAG_NONE, cached ResourceHeapTier >= 2 check per device) heaps.
    • Creates backing heaps outside the pool mutex, preserves exact non-shared committed semantics (!is_placed_texture), rolls back offsets immediately on CreatePlacedResource failure, cools freed ranges for 2 s before reuse via size-class free lists, and uses a lock-free FALLBACK_LIVE fast-path from vram::forget_for_recycle -> vram_small::forget_fallback.
    • Includes XCL_VRAM_FALLBACK_POOL=0 kill-switch (default enabled) for easy A/B comparison.

Tested

  • Unit & D3D12 GPU tests on amd-dev (bf7c82ee + 321e1eba, --test-threads=1, 0 new warnings):
    • cargo test -p xcl --lib hle::d3d12x::vram_small -- --nocapture --test-threads=1: 6 passed; 0 failed; 1 ignored (including fallback_pool_packs_cools_recycles_and_rolls_back, fallback_candidate_filters_msaa_and_oversized_buffers, and live GPU test gpu_fallback_pool_places_texture1d_and_small_buffers_cleanly).
    • cargo test -p xcl --lib hle::d3d12x::title_textures::tests::register_reuses_precomputed_xg_texture_arc: 1 passed; 0 failed.
  • Offline D3D12 hardware microbenchmark on AMD Radeon RX 9070 XT (cargo test -p xcl --release --lib hle::d3d12x::vram_small::tests::bench_fallback_pool_vs_committed_and_xg_dedup -- --ignored --nocapture --test-threads=1):
    • Fallback pooling (256x mixed TEXTURE1D + 4 KiB buffer): CreateCommittedResource 102.17 ms (399.12 us/op) vs place_fallback 2.43 ms (9.48 us/op) -> 42.12x speedup.
    • xg::Texture::new dedup (2000x 1024x1024 10-mip BC1): double 1.36 ms (0.68 us/op) vs dedup 0.50 ms (0.25 us/op) -> 2.70x speedup.

Not tested

  • Non-AMD GPUs (NVIDIA / Intel) and Tier-1-only D3D12 devices (fallback pool automatically skips devices below D3D12_RESOURCE_HEAP_TIER_2).
  • Multi-hour soak runs beyond 60 s smoke.

Regression run
Baseline (main, b49be183):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: AMD Radeon RX 9070 XT
title: b8db18c2
seconds: 60
exit: 0x0
frames shown: 1624 (PresentX 1624)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19700 MB

This PR (321e1eba on amd-dev bf7c82ee):

xcl-smoke v1
commit: 321e1eba743543b93361a5c08884e41d202b8974
gpu: AMD Radeon RX 9070 XT
title: b8db18c2
seconds: 60
exit: 0x0
frames shown: 1648 (PresentX 1648)
presents per 2 s (last 3): 63 60 60
device removals: 0
crashes: 0
peak commit: 12927 MB

AI use

  • Profiling analysis, implementation, unit tests, and microbenchmark harness were assisted by AI; all unit tests, hardware benchmarks, and the 60 s smoke run were executed and verified locally on AMD Radeon RX 9070 XT.

@thehollowedone

Copy link
Copy Markdown
Contributor

Independent NVIDIA check of 321e1eba on Windows 11 build 26300, RTX 3080 Ti (621.35), Ryzen 7 9800X3D, Rust 1.99.1 MSVC:

  • cargo build --offline --locked -j 4 --workspace: passed.
  • XCL_REQUIRE_GPU=1 cargo test --offline --locked -j 4 -p xcl --lib hle::d3d12x::vram_small -- --test-threads=1 --nocapture: 6 passed, 0 failed, 1 ignored; the native fallback-pool test ran with no GPU skip.
  • cargo test --offline --locked -j 4 --workspace --no-fail-fast -- --test-threads=4: 1,785 passed, 17 failed, 48 ignored, 41 GPU skip notices. This is not a green workspace result. The exact-parent control at bf7c82ee aborted its library test binary on a misaligned COM pointer while concurrent tests ran, so I cannot attribute the aggregate failures to this PR.

Two allocation-lifetime points need correction before the pool can safely serve title traffic:

  1. forget_for_recycle calls forget_fallback before late_release::hold. The new pool then makes the block reusable after two seconds, even if a queue is still waiting on a fence and late_release retains the old resource. Please return that block only when its deferred COM item is reaped; a gated-queue regression should show that a same-size allocation cannot reuse the offset until the gate opens.
  2. DefaultFallbackPools never retires a backing heap. A temporary burst of 256 four-MiB resources can leave 64 × 16-MiB heaps (1 GiB) committed after every resource is released; the free lists also cannot serve another size class. Please trim fully retired heaps after fence completion, or bound retained bytes with a committed-resource fallback. A peak/release/different-size-class test would verify that committed memory falls.

I did not run a title smoke test or a multi-hour soak. AI use: Codex (GPT-6.2 Sol) ran the tests and traced the release path; Codex (GPT-6.2 Astra) reviewed the allocator and proposed the regressions.

@ettehn

ettehn commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

I ran a native WARP queue control for the early slot reuse raised above, using the allocator extracted from 321e1eba743543b93361a5c08884e41d202b8974 (Windows 11 26200.9974, Microsoft Basic Render Driver, heap tier 2, Rust 1.97.1 MSVC). I submitted a copy to a 64 KiB placed buffer behind an unsignaled queue fence and kept the original COM reference alive while calling the allocator's forget, mirroring the release order in this head.

In three fresh processes, an immediate allocation got the next slot. After 2,100 ms the allocator handed out the original slot on the same heap, while the submitted copy's completion fence remained 0. The fence reached 1 after I opened the gate. This gives a native queue control for returning the slot at fence retirement rather than on elapsed cooldown alone.

The probe called the extracted allocator directly. It did not invoke XCL's release hook or submit work using the replacement slot. No physical GPU or title run. AI use: OpenAI Codex (GPT-6) prepared and ran the probe and drafted this report.

ivall added a commit that referenced this pull request Nov 27, 2026
Port #2985 so supported fallback resources can share backing heaps and reuse the decoded layout.

Assisted-by: Codex (exact model identifier unavailable)
@ClosetPie107

Copy link
Copy Markdown

Independent test report under Wine/Proton (not native Windows) for head 321e1eba.

Setup

  • AMD Radeon RX 9070 (RDNA4, RADV GFX1201, Mesa 26.3.1), Ryzen 5 9600X, CachyOS Linux 7.3.5
  • Proton Experimental experimental-11.0-20261112, vkd3d-proton master ec207194 with a local patch dropping the lpVtbl checks in d3d12_command_allocator_from_iface / d3d12_command_list_from_iface, DXC 1.9.2609.5, Rust 1.96.0, x86_64-pc-windows-gnu
  • The exact head, on its base amd-dev bf7c82ee, against amd-dev bf7c82ee itself

Tests (debug, XCL_REQUIRE_GPU=wave64, --test-threads=1)

  • hle::d3d12x::vram_small::: 6 passed, 0 failed, 1 ignored (amd-dev: 3 passed, 0 failed), including gpu_fallback_pool_places_texture1d_and_small_buffers_cleanly
  • hle::d3d12x::vram::: 30 passed, 0 failed, 1 ignored (amd-dev the same); hle::d3d12x::sparse::: 37 passed, 0 failed, 1 ignored (amd-dev the same)
  • hle::d3d12x::title_textures::: 32 passed, 3 failed, the same 3 as on amd-dev (31 passed, 3 failed); register_reuses_precomputed_xg_texture_arc passes
  • The full lib binary loses the device here on amd-dev and on this head alike (RADV The CS has been rejected, then a panic in an xcl-pso thread), about 1,057 tests in; so no full count.

Retail title, 300 s (release; amd-dev translates shaders differently from main, so the three amd-dev trees ran from one shared, partly filled amd-dev shader cache with the startup preparation off: XCL_PREPARE=0 XCL_PSO_PRELOAD=0, plus XCL_DIRECT_DXIL=1 XCL_PSO_WAIT_MS=50 XCL_TEXTURE_LEVEL_PAGES=1 XCL_COHERENT_MB=1024 VKD3D_CONFIG=ignore_shared_fence, A pressed every 10 s; one run each)

exit crash / device removed presents video memory at 90 s peak
amd-dev bf7c82ee 0 0 / 0 1,938 8,388 MiB 13,333 MiB
this PR 321e1eba 0 0 / 0 2,086 8,579 MiB 15,265 MiB
  • The pool (on by default) runs without problems here. Peak video memory is about 1.9 GiB higher in this one run; with one run per tree I can't tell whether that is the 16 MiB heaps or run-to-run spread (the amd-dev peak varies too).

Not tested

  • Native Windows, NVIDIA/Intel
  • XCL_VRAM_FALLBACK_POOL=0, more than one run, the timing of CreatePlacedResourceX

AI use

  • Claude Code (Claude Opus 5.5) wrote the test scripts, ran the tests and runs on this machine and wrote this report.

@iggyvilla

Copy link
Copy Markdown
Collaborator

Review of #2985, diff only; I did not build or run it.

  1. Already on amd-dev. 47cbd3aa carries this diff (checked: vram_small.rs has forget_fallback/COOL on amd-dev 7c795f5e). Once rebased the PR is nearly empty (EOF test ordering aside); close it as merged, or retarget it at the fix below.
  2. Possible early slot reuse. In real::res_release, forget_released_resource -> vram::forget_for_recycle -> vram_small::forget_fallback runs before late_release::hold (checked, real.rs ~2106 vs ~2139). The freed block is reusable after a wall-clock COOL = 2s (checked), while late_release retires by fence. If in-flight work on the old resource outlasts 2 s, place_fallback can hand out the same (heap, offset) too early. Gating the return on the late_release retire, with a gated-queue fence test, would fix it. (Inferred; I have not seen it happen.)
  3. Heaps never retire: DefaultFallbackPools only pushes 16 MiB heaps and buckets free by 1..=64 units; empty heaps are not released, even under commit_low.
  4. Tests: fallback_pool_packs_cools_recycles_and_rolls_back models the 2 s clock and would pass with the bug above; the GPU test submits no work and leaves its heap in FALLBACK_POOLS.

AI use: Gemini 4 Hydrogen High (agy2 for #2981, agy3 for #2985 and #2989; Antigravity) read the PR diff, its comments and the touched code and wrote the report; Claude Code (Claude Sonnet 5.5, coordinator) spot-checked the points marked "checked" against amd-dev 7c795f5e and wrote this text. Everything else is the agent's reading and was not re-verified. Nothing was built or run. The comment was posted without the account owner reading it first.

@DarkPanda69

Copy link
Copy Markdown

NVIDIA run (listed as untested), with the RX 9070 XT from the same PC: the new tests pass on both. There's also a note on flaky title_textures tests on the RX 9070 XT that are already flaky on the base.

Tested: at the PR head 321e1eb against its merge-base with amd-dev, bf7c82e (the diff no longer applies on today's amd-dev; GitHub shows a conflict). Debug, Rust 1.100.0, Windows 11 Pro 26200, Ryzen 7 5800X, card picked with XCL_ADAPTER (log: backend 'real' – AMD Radeon RX 9070 XT / – NVIDIA GeForce RTX 3060).

  • cargo test -p xcl --lib -- vram_small hle::d3d12x::vram register_reuses fallback --test-threads=4 --nocapture: 57 passed, 0 failed, 2 ignored on the RTX 3060 (32.0.16.1088) and on the RX 9070 XT (32.0.32029.2010, 2 of 2 runs). New and ok on both: gpu_fallback_pool_places_texture1d_and_small_buffers_cleanly, fallback_pool_packs_cools_recycles_and_rolls_back, fallback_candidate_filters_msaa_and_oversized_buffers, register_reuses_precomputed_xg_texture_arc.

title_textures on the RX 9070 XT (cargo test -p xcl --lib -- title_textures, 5 runs at --test-threads=4 and 2 at --test-threads=1 each):

  • title_bytes_are_used_in_place_when_all_are_readable fails in every run, with and without the PR: all 7 + 7 on the RX 9070 XT and the single run per stage on the RTX 3060 (a committed page is not copied).
  • gpu_uploads_on_the_title_queue_run_before_its_later_work_and_are_released and placed_title_textures_share_vram_and_preserve_pixels fail now and then on the RX 9070 XT only: merge-base 3 of 7 runs, PR head 6 of 7. On the same merge-base, D3D12.X: pool async upload command lists, raise staging item cap to 32 MiB, and lower parallel detile threshold #2989's runs had them in 7 of 7 base runs, so this looks like base flakiness more than this PR. I can't tell from 7 runs whether the PR makes it more frequent. They didn't fail in the single RTX 3060 run per stage (I didn't repeat runs on it).

Not tested: Tier-1-only devices, a soak beyond 60 s, a title, Intel.

AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

@Saksukella

Copy link
Copy Markdown
Contributor Author

Closing as superseded on amd-dev by 47cbd3aa (pool small fallback resource allocations). The fence-gated recycling / VRAM-pressure eviction follow-up continues cleanly in #3025.

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.

6 participants