d3d12: reclaim shader-visible descriptor slots via lease and fence pinning #172

Manually merged
github merged 2 commits from gh-pr/172/head into gh-pr/172/base 2026-11-26 06:36:11 +00:00
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 13:53 UTC · original
Vladimir-Human:d3d12/reclaim-descriptor-slots → main · +1875 −256 · 14 files · 2 commits · merged 2026-11-25 14:32 UTC

Summary

Reclaim shader-visible descriptor slots in runtime/d3d12/src/device/descriptor_paging.rs instead of allocating monotonically until exhaustion (ProjectScarlett Priority #2 in docs/PROGRESS.md), while preserving descriptor lifetimes across open command lists, ClearState, bundle adoption, Reset() while in-flight, multi-queue out-of-order retirement, and ResourceDescriptorHeap index_map stability:

  • device/descriptor_paging.rs:
    • Add FreeSpans best-fit coalescing span allocator with left/right neighbor merging and used tail shrinking.
    • Track TableEntry { id, slot, pins, direct_pinned, last_used }, by_id, LRU tick, and in_flight queue submissions (RetireFence) for non-blocking out-of-order retire_completed() (never treating GetCompletedValue() == u64::MAX device-removed sentinel as completed).
    • On gpu_table miss, try FreeSpans::alloc -> bump used -> retire_completed() -> evict oldest unpinned (pins == 0 && !direct_pinned) entries -> log full diagnostic and return Err(E_OUTOFMEMORY) if pinned/direct usage still exceeds limit. Drop displaced RetiredViews outside the tables mutex and clean resident_records and host view tracking (imported, placed, scenes, display::texture_view).
    • Enforce DescriptorLeases deduplicated per (Arc<Arena>, id) on all gpu_table/gpu callers and RAII PendingSubmissions batch pins in execute_batch.
  • device/descriptor_paging/index_map.rs & device/placement/descriptors.rs:
    • Mark longest-first adopted ranges entries direct_pinned = true (leaving fully covered redundant aliases reclaimable), allow ensure_direct_capacity to retire/evict unpinned tables before reporting E_OUTOFMEMORY, and add DescriptorRange::clear_deferred / copy_to_deferred so displaced view metadata drops outside external locks.
  • commands.rs & commands/*.rs:
    • Thread &mut DescriptorLeases through descriptors::table, compute_bindings (with_reflected_table, refresh_inline_tables), and depth::clear_uav; propagate leases in adopt_bundle; clear on Recording::reset and Drop, not in clear_state.
    • Append retire_fence: OnceLock<RetireQueueFence> at the tail of QueueState (preserving kind at +0x10) and commit fence tokens after memory_wait::submit only when PendingSubmissions is non-empty, rolling back pre-submit errors via RAII drop.
  • device.rs:
    • Add opt-in TUXBOX_DESCRIPTOR_HEAP_LIMIT positive u32 clamp for shader-visible CBV_SRV_UAV heaps (leaving default MaxViewDescriptorHeapSize behavior unchanged when unset).
  • examples/lanes/ & project_tuxbox/verify.py:
    • Add descriptor-reclaim selftest (reclaim.hlsl, selftest.cpp, README.md, build.py, verify.py) exercising an oversized 2,097,152-descriptor shader-visible heap (> 2x RTX 4070 SUPER's 1,000,000 limit) across sustained mixed-size turnover, overlapping in-flight Reset(), ClearState pin retention, post-eviction rebinding, and fail-closed E_OUTOFMEMORY.

Verified on Lanes

  • python tuxbox.py check passed on this branch (branch is based on 682a0e4 prior to #169 MSVC /W4 buffer_import_guest.cpp and #ordinal import lookup fixes, tests/ contains pre-existing Linux-only fcntl/os.sched_getaffinity test modules on Windows, and lanes-shader-cache is Proton-only; equivalent Rust suites, Windows-compatible Python suites, native MSVC builds with cl /W4 /WX on examples/lanes/selftest.cpp, and all lanes.exe / d3d12-tables.exe GPU checks plus negative control passed below)
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below (lanes.exe --selftest descriptor-reclaim verifies all 5 reclamation and pin-retention behaviors on a 2,097,152-slot paged heap on GPU)

GPU, driver and backend: NVIDIA GeForce RTX 4070 SUPER (pci=10de:2783), NVIDIA driver 32.0.16.2114, Windows 10.0.26300.0 native backend (MSVC 19.44.35230, Windows SDK 10.0.26100.0 DXC, Rust 1.100.0, Python 3.13.9).

Paste the summary printed at the end:

lanes.exe --selftest descriptor-reclaim: lanes-selftest PASS pci=10de:2783 scenario=descriptor-reclaim passed=1
lanes.exe --selftest all:                lanes-selftest PASS pci=10de:2783 scenario=all passed=11
lanes.exe --replay:                      lanes-replay PASS pci=10de:2783 course=16/16 win=16/16 safe-miss=1 collision=1 frames=2 input=gameinput
d3d12-tables.exe:                        d3d12-tables PASS pci=10de:2783 words=16/16 guards=4 heaps=2 rejected=3 placed-srv=16/16 range-copies=4 descriptor-mode=1
(lanes-shader-cache is Proton-only)

Negative control (retire_completed/evict_oldest bypassed in Tables::allocate_contiguous):
lanes.exe --selftest descriptor-reclaim: lanes-selftest FAIL descriptor-reclaim: close list failed: 0x8007000e (exit 1)

Checks

  • cargo test --locked -p tuxbox-d3d12: 107 passed, 0 failed
  • cargo test --locked -p tuxbox-dxbc --features native-bindings: 53 passed, 0 failed
  • cargo test --locked -p tuxbox-xg-layout: 14 passed, 0 failed
  • cargo test --locked --manifest-path tools/shader-compiler/Cargo.toml: 279 unit + 2 integration passed, 0 failed
  • cargo check --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12 -p tuxbox-bvh: exit 0
  • python -m unittest tests.test_verify tests.test_check tests.test_repository_hygiene tests.test_cli tests.test_d3d12_regressions tests.test_d3d12_status tests.test_build_msvc -v: 32 passed, 0 failed
  • cl /W4 /WX on examples/lanes/selftest.cpp: 0 warnings, exit 0

AI-assisted implementation and verification.

**@⁠Vladimir-Human** on GitHub · 2026-11-25 13:53 UTC · [original](https://github.com/brcly/TuxBox/pull/172) `Vladimir-Human:d3d12/reclaim-descriptor-slots` → `main` · +1875 −256 · 14 files · 2 commits · merged 2026-11-25 14:32 UTC ## Summary Reclaim shader-visible descriptor slots in `runtime/d3d12/src/device/descriptor_paging.rs` instead of allocating monotonically until exhaustion (ProjectScarlett Priority #2 in `docs/PROGRESS.md`), while preserving descriptor lifetimes across open command lists, `ClearState`, bundle adoption, `Reset()` while in-flight, multi-queue out-of-order retirement, and `ResourceDescriptorHeap` `index_map` stability: - **`device/descriptor_paging.rs`**: - Add `FreeSpans` best-fit coalescing span allocator with left/right neighbor merging and `used` tail shrinking. - Track `TableEntry { id, slot, pins, direct_pinned, last_used }`, `by_id`, LRU `tick`, and `in_flight` queue submissions (`RetireFence`) for non-blocking out-of-order `retire_completed()` (never treating `GetCompletedValue() == u64::MAX` device-removed sentinel as completed). - On `gpu_table` miss, try `FreeSpans::alloc` -> bump `used` -> `retire_completed()` -> evict oldest unpinned (`pins == 0 && !direct_pinned`) entries -> log full diagnostic and return `Err(E_OUTOFMEMORY)` if pinned/direct usage still exceeds `limit`. Drop displaced `RetiredViews` outside the `tables` mutex and clean `resident_records` and host view tracking (`imported`, `placed`, `scenes`, `display::texture_view`). - Enforce `DescriptorLeases` deduplicated per `(Arc<Arena>, id)` on all `gpu_table`/`gpu` callers and RAII `PendingSubmissions` batch pins in `execute_batch`. - **`device/descriptor_paging/index_map.rs` & `device/placement/descriptors.rs`**: - Mark longest-first adopted `ranges` entries `direct_pinned = true` (leaving fully covered redundant aliases reclaimable), allow `ensure_direct_capacity` to retire/evict unpinned tables before reporting `E_OUTOFMEMORY`, and add `DescriptorRange::clear_deferred` / `copy_to_deferred` so displaced view metadata drops outside external locks. - **`commands.rs` & `commands/*.rs`**: - Thread `&mut DescriptorLeases` through `descriptors::table`, `compute_bindings` (`with_reflected_table`, `refresh_inline_tables`), and `depth::clear_uav`; propagate leases in `adopt_bundle`; clear on `Recording::reset` and `Drop`, **not** in `clear_state`. - Append `retire_fence: OnceLock<RetireQueueFence>` at the tail of `QueueState` (preserving `kind` at `+0x10`) and commit fence tokens after `memory_wait::submit` only when `PendingSubmissions` is non-empty, rolling back pre-submit errors via RAII drop. - **`device.rs`**: - Add opt-in `TUXBOX_DESCRIPTOR_HEAP_LIMIT` positive `u32` clamp for shader-visible `CBV_SRV_UAV` heaps (leaving default `MaxViewDescriptorHeapSize` behavior unchanged when unset). - **`examples/lanes/` & `project_tuxbox/verify.py`**: - Add `descriptor-reclaim` selftest (`reclaim.hlsl`, `selftest.cpp`, `README.md`, `build.py`, `verify.py`) exercising an oversized `2,097,152`-descriptor shader-visible heap (`> 2x` RTX 4070 SUPER's `1,000,000` limit) across sustained mixed-size turnover, overlapping in-flight `Reset()`, `ClearState` pin retention, post-eviction rebinding, and fail-closed `E_OUTOFMEMORY`. ## Verified on Lanes - [ ] `python tuxbox.py check` passed on this branch *(branch is based on `682a0e4` prior to `#169` MSVC `/W4` `buffer_import_guest.cpp` and `#ordinal` import lookup fixes, `tests/` contains pre-existing Linux-only `fcntl`/`os.sched_getaffinity` test modules on Windows, and `lanes-shader-cache` is Proton-only; equivalent Rust suites, Windows-compatible Python suites, native MSVC builds with `cl /W4 /WX` on `examples/lanes/selftest.cpp`, and all `lanes.exe` / `d3d12-tables.exe` GPU checks plus negative control passed below)* - [x] Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below *(`lanes.exe --selftest descriptor-reclaim` verifies all 5 reclamation and pin-retention behaviors on a `2,097,152`-slot paged heap on GPU)* GPU, driver and backend: NVIDIA GeForce RTX 4070 SUPER (`pci=10de:2783`), NVIDIA driver `32.0.16.2114`, Windows `10.0.26300.0` native backend (MSVC `19.44.35230`, Windows SDK `10.0.26100.0` DXC, Rust `1.100.0`, Python `3.13.9`). Paste the summary printed at the end: ```text lanes.exe --selftest descriptor-reclaim: lanes-selftest PASS pci=10de:2783 scenario=descriptor-reclaim passed=1 lanes.exe --selftest all: lanes-selftest PASS pci=10de:2783 scenario=all passed=11 lanes.exe --replay: lanes-replay PASS pci=10de:2783 course=16/16 win=16/16 safe-miss=1 collision=1 frames=2 input=gameinput d3d12-tables.exe: d3d12-tables PASS pci=10de:2783 words=16/16 guards=4 heaps=2 rejected=3 placed-srv=16/16 range-copies=4 descriptor-mode=1 (lanes-shader-cache is Proton-only) Negative control (retire_completed/evict_oldest bypassed in Tables::allocate_contiguous): lanes.exe --selftest descriptor-reclaim: lanes-selftest FAIL descriptor-reclaim: close list failed: 0x8007000e (exit 1) ``` ## Checks - `cargo test --locked -p tuxbox-d3d12`: 107 passed, 0 failed - `cargo test --locked -p tuxbox-dxbc --features native-bindings`: 53 passed, 0 failed - `cargo test --locked -p tuxbox-xg-layout`: 14 passed, 0 failed - `cargo test --locked --manifest-path tools/shader-compiler/Cargo.toml`: 279 unit + 2 integration passed, 0 failed - `cargo check --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12 -p tuxbox-bvh`: exit 0 - `python -m unittest tests.test_verify tests.test_check tests.test_repository_hygiene tests.test_cli tests.test_d3d12_regressions tests.test_d3d12_status tests.test_build_msvc -v`: 32 passed, 0 failed - `cl /W4 /WX` on `examples/lanes/selftest.cpp`: 0 warnings, exit 0 AI-assisted implementation and verification.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:36:09 +00:00
github manually merged commit 96783e44f1 into gh-pr/172/base 2026-11-26 06:36:11 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.