Skip to content

D3D12.X: recycle released reserved sampled textures by description and state - #2436

Open
saintwolf wants to merge 1 commit into
xcl-dev:mainfrom
saintwolf:vram/recycle-reserved-textures
Open

saintwolf wants to merge 1 commit into
xcl-dev:mainfrom
saintwolf:vram/recycle-reserved-textures

Conversation

@saintwolf

@saintwolf saintwolf commented Nov 25, 2026 •

Copy link
Copy Markdown

Recycles released reserved sampled textures in a bounded per-device pool keyed by exact (D3D12_RESOURCE_DESC, uniform D3D12_RESOURCE_STATES) so streamed texture placements skip CreateReservedResource and GetResourceTiling. Fixes #1268. Part of #1833.

What is in it

  • crates/xcl/src/hle/d3d12x/title_textures.rs: forget(res) returns the removed texture's last tracked uniform D3D12_RESOURCE_STATES when all subresources share a single state (states.len() == 1), or None for mixed-state / untracked textures.
  • crates/xcl/src/hle/d3d12x/real.rs: on final Release (n == 0) while late_release holds the last PC ref, parks eligible place_pages sampled textures with their actual uniform D3D12 state; exposes note_reserved so reused pointers re-register in RESERVED.
  • crates/xcl/src/hle/d3d12x/late_release.rs: once every live GPU queue is past a batch's fence, hands parked textures to vram::recycle_ready instead of destroying them.
  • crates/xcl/src/hle/d3d12x/vram.rs: adds ReservedKey and a bounded per-device ReservedPool (512 idle entries/device, generation-tombstoned FIFO eviction with bounded order compaction, evicted entries dropped outside PAGES.lock(), opt-out via XCL_RESERVED_POOL=0). recycle_ready unmaps any still-mapped tiles (pages.unmap) on the background late_release thread so retired 64 MiB VRAM pool heaps never leave stale page-table references. place_on_pages pops matching pooled reserved textures via QueryInterface and reuses their cached tile count, skipping CreateReservedResource and GetResourceTiling. Adds unit tests (reuse hit, state isolation, FIFO eviction, cap=0, repeated take/repark tombstoning + cap-FIFO ordering) and a GPU recycle/rebind test.

Tested

  • Windows 11 (build 26300), NVIDIA GeForce RTX 4070 SUPER (driver 32.0.16.2140), AMD Ryzen 7 7800X3D, rustc 1.100.0 (0ef1bc386 2026-11-09), DXC/DXIL 1.8.2502.11.
  • .\scripts\test.ps1 (cargo test --workspace -- --test-threads=4, debug) on head 2f92cc8: 1426 passed, 0 failed, 46 ignored across 37 test binaries, including all hle::d3d12x::vram::tests (reserved_pool_reuses_matching_key_isolates_state_and_evicts_fifo, reserved_pool_repark_tombstones_stale_order_and_preserves_fifo, and reserved_resource_recycle_unmaps_reuses_and_remaps_on_gpu).
  • SKIPPED GPU tests on this adapter (27 total): 21 no wave64, 3 per-lane form #1426 refusals (V_CVT_F16_F32, V_MUL_F16, V_PK_MUL_F16), 1 cannot disable the driver cache (Windows Developer Mode is required) (0x887A002D), 1 no xgameruntime.dll, 1 no heap windows.
  • Isolated #1268 microbenchmark on RTX 4070 SUPER (release, 2,000 iterations each):
    • 256x256 BC1 (9 mips): CreateReservedResource + GetResourceTiling + Release 222.33 ms (111.17 µs/tex) → recycled pool 0.04 ms (0.019 µs/tex) (5,730× faster).
    • 1024x1024 BC7 (11 mips): 279.58 ms (139.79 µs/tex) → recycled pool 0.04 ms (0.019 µs/tex) (7,262× faster).
  • Paired retail world-load A/B run (XCL_DIRECT_DXIL=1, XCL_PSO_WAIT_MS=50, XCL_PRESS_A_AT=15,20,25,30,35,40,45, isolated caches): XCL_RESERVED_POOL=1 cleanly recycled 32+ streamed reserved textures live before both arms hit the same pre-existing xboxbvhbuilder.dll 40-bit VA exception (~112–116 s), with 0 device removals and -352 MB lower peak commit (28,903 MB vs 29,255 MB).

Regression run

xcl-smoke v1
commit: 3f4495a2b325ae5a9df314dd86d10327502b5cf5
gpu: NVIDIA GeForce RTX 4070 SUPER
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 971 (PresentX 971)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 14961 MB
xcl-smoke v1
commit: 2f92cc841ad65e2dc7e63fea17d227a71e44f16b
gpu: NVIDIA GeForce RTX 4070 SUPER
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 914 (PresentX 914)
presents per 2 s (last 3): 56 54 53
device removals: 0
crashes: 0
peak commit: 15405 MB

Not tested

AI use

  • Antigravity (Gemini 4 Neon) for issue diagnosis, implementation, tests, benchmarking, and PR drafting; all builds, tests, benchmarks, and retail runs were executed locally on the hardware above.

@saintwolf
saintwolf force-pushed the vram/recycle-reserved-textures branch from e2cf935 to bebf2dd Compare November 25, 2026 18:53
@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) test report on exact head e2cf9357ba193507dfbc19d4e3b040b63ee957e7 (clean detached checkout, no local changes).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17; tests debug, smoke release.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1275 passed, 2 failed, 46 ignored; 8 SKIPPED (6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu refused by per-lane form; heap_window_out_of_memory_falls_back_to_the_switch_form; pso_measurement_compiles_an_exact_synthetic_record). All native wave64 GPU tests ran and passed.
    • Both PR tests passed (no skips): hle::d3d12x::vram::tests::reserved_pool_reuses_matching_key_isolates_state_and_evicts_fifo and reserved_resource_recycle_unmaps_reuses_and_remaps_on_gpu.
    • The 2 failures (gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 seed 722 and fuzz_validate_and_gpu_3 seed 1599) are pre-existing on unmodified main on this GPU.

Regression run (60 s retail-title smoke, XCL_NETWORK=0, release)

xcl-smoke v1
commit: e2cf9357ba193507dfbc19d4e3b040b63ee957e7
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1598 (PresentX 1598)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13927 MB

(Matched main 68a284a baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • D3D12 debug layer; Intel GPUs.

AI use

  • Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.

raylitebull added a commit that referenced this pull request Nov 25, 2026
Port upstream PR #2436 (by Saintwolf, adapted for #2306 lazy level pages and #2275 multi-queue retirement) onto amd-dev.

Streamed sampled textures placed on shared 64 KiB video-memory pages previously called CreateReservedResource and GetResourceTiling on every placement, costing 0.6-1.7 ms per texture on AMD drivers (#1268).

Keep a bounded per-device pool (512 idle entries, FIFO eviction, opt-out via XCL_RESERVED_POOL=0) of released reserved sampled textures keyed by exact (D3D12_RESOURCE_DESC, uniform D3D12_RESOURCE_STATES), also caching the #2306 Levels tiling layout so XCL_TEXTURE_LEVEL_PAGES=1 placements skip both CreateReservedResource and GetResourceTiling on reuse. When late_release finishes draining all live GPU queues past a texture's batch, vram::recycle_ready unmaps any still-mapped tiles on the background thread (skipping unmap for lazy textures that had zero mapped levels) and parks the ID3D12Resource for reuse.

Fixes #1268. Part of #1833.

Assisted-by: Antigravity (Gemini 4 Neon High)
raylitebull added a commit that referenced this pull request Nov 25, 2026
Port upstream PR #1835 (commits 2441d59..5005620) cleanly onto amd-dev with #2436 reserved-texture recycling and #2306 lazy levels.

Small sampled textures (< 64 KiB) previously each consumed a full 64 KiB tile plus CreateReservedResource, GetResourceTiling, and UpdateTileMappings overhead. Pack eligible sub-64 KiB sampled textures (when not aliasing an existing live 64 KiB GPU-owned page) into shared 4 KiB-aligned 4 MiB default heaps via CreatePlacedResource in vram_small.rs, reclaiming their 4 KiB slots on release in vram::forget_for_recycle before falling through to reserved-page recycling.

Fixes #1835.

Assisted-by: Antigravity (Gemini 4 Neon High)
raylitebull added a commit that referenced this pull request Nov 25, 2026
…ation

Keep #[cfg(test)] helpers compiling cleanly after #2181 (PsInterface::early_depth), 32f0967 (Effects::bvh_intersect), #2345 (sparse State quarantine/queue_fences), and #1514/#2436 (Pages::bind 6-arg 3-tuple).

Assisted-by: Antigravity (Gemini 4 Neon High)
@SlopTart

Copy link
Copy Markdown

gpu_ suite (cold cache): 160 passed / 4 failed, no crashes. Three are the known baseline trio on this lane; the fourth is gpu_promoted_uav_clear_values_pass_create_committed_resource, newly red on main since #2280 (report), unrelated to this PR.
Targeted run of reserved_pool_reuses_matching_key_isolates_state_and_evicts_fifo: passed (the gpu-side recycle test ran inside the suite).

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: 1 real failure, deterministic (also fails --test-threads=1, 1219/1):
gpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier
  assertion failed: group barriers in the program (lower_dxil.rs:1155)

The lowered program is missing an expected group barrier (or emits one where none is expected). Needs a fix before merge.

— Devin

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: ReservedPool::take at vram.rs:196-204 removes an entry from by_key/live but does not remove or invalidate its matching VecDeque entry in order. Every reuse/repark adds another order entry (:225-227), so a pool below cap accumulates stale entries indefinitely. At cap, a stale entry can match a reparked pointer and evict it by its original rather than current parking time, violating the intended bounded FIFO behavior. The test takes entries only once and misses reuse->repark cycles. Please tombstone/remove by generation on take and add a repeated recycle/repark plus cap-FIFO test.

…d state

Streamed sampled textures placed on shared 64 KiB video-memory pages previously called CreateReservedResource and GetResourceTiling on every placement, costing 0.6-1.7 ms per texture on AMD drivers (xcl-dev#1268) and ~111-140 us on NVIDIA.

Keep a bounded per-device pool (512 idle entries, FIFO eviction, opt-out via XCL_RESERVED_POOL=0) of released reserved sampled textures keyed by exact (D3D12_RESOURCE_DESC, uniform D3D12_RESOURCE_STATES). When late_release finishes draining all live GPU queues past a texture's batch, vram::recycle_ready unmaps any still-mapped tiles on the background thread and parks the ID3D12Resource for reuse; matching place_pages calls QueryInterface on the pooled object and reuse its cached tile count, skipping CreateReservedResource and GetResourceTiling.

Fixes xcl-dev#1268. Part of xcl-dev#1833.

Assisted-by: Antigravity (Gemini 4 Neon)

# Conflicts:
#	crates/xcl/src/hle/d3d12x/vram.rs
@saintwolf
saintwolf force-pushed the vram/recycle-reserved-textures branch from bebf2dd to 2f92cc8 Compare November 25, 2026 20:28
@saintwolf

Copy link
Copy Markdown
Author

@bmjask Good catch — fixed in 2f92cc8 (also rebased onto main 6d12f58, picking up #2420 so the full workspace suite passes with 0 failures — cc @DavidM1911):

  • ReservedPool now tracks a monotonic next_gen: u64 and live_gen: HashMap<usize, u64> with generation-tagged order / by_key entries.
  • take() removes the pointer from live_gen (immediately tombstoning any stale order entry) and calls compact_order(), which prunes front tombstones and compacts order whenever order.len() > max(2 * live, 16) so repeated take/repark cycles below cap cannot accumulate unbounded stale entries.
  • push() skips tombstoned generations during cap eviction so a reparked pointer is ordered strictly by its current parking time.
  • Added reserved_pool_repark_tombstones_stale_order_and_preserves_fifo covering 100× below-cap take/repark cycles behind an idle front entry, cap=2 repark FIFO ordering, and cap=3 non-front tombstone skipping.

@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…extures)

Integrates xcl-dev#2436 cleanly alongside xcl-dev#2385 (top-mip shedding) and xcl-dev#2474
(foreign-thread release_cleanup).

Assisted-by: Antigravity (Gemini 4 Neon)
@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Independent AMD report for 2f92cc841ad65e2dc7e63fea17d227a71e44f16b.

Tested

  • AMD Radeon RX 7800 XT (driver 32.0.31035.1003), Ryzen 7 5700X3D, Windows 11 build 26200, rustc 1.100.0, debug profile, DXC from Windows SDK 10.0.26100.0, XCL_ADAPTER=radeon. Exact PR head (not a local merge onto main); main at the time was 6d12f58a.
  • cargo test --workspace --no-fail-fast -j 4 -- --test-threads=4: 1425 passed, 1 failed, 46 ignored. The one failure is gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 (seed 722: res[30] = 0x1, CPU 0x7fffffff); it fails identically on main 6d12f58a on this adapter (cargo test -p xcl --lib fuzz_validate_and_gpu_1), so it is not caused by this PR.
  • 7 SKIPPED lines: 6 f16_programs_on_the_gpu / f32_programs_on_the_gpu per-lane cases refused for V_MUL_F16, V_PK_MUL_F16 or V_CVT_F16_F32, and heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • The new vram::tests::reserved_pool_* and reserved_resource_recycle_unmaps_reuses_and_remaps_on_gpu (GPU) passed.

Not tested

  • scripts/smoke.ps1 / a retail title (no regression run), other adapters, NVIDIA. No game data was used.

AI use

Claude Code (Claude Sonnet 5.5) ran the tests and wrote this report. No game files, dumps or shader names are included.

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

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

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

D3D12.X: CreateReservedResource for each streamed title texture costs ~1 ms on AMD and stalls the title's render thread during world loading

7 participants