d3d12/gpu: detect u64::MAX in host::mapped and ensure mapped() on upload fallback and depth twins (D3D12-RESIDENT-MAPPED-PARITY) #3199

Closed
Jyx0208 wants to merge 1 commit from upstream-pr/3199 into main
Jyx0208 commented 2026-12-02 18:31:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Jyx0208 on 2026-12-02T18:31:30Z.


Claimed in the coordination issue as: D3D12-RESIDENT-MAPPED-PARITY (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7353943683)

What and why

Closes three residency/mapping parity gaps in leonida-gpu:

  1. host::mapped() silent success on removed device (d3d12/crates/leonida-gpu/src/host.rs):
    • In D3D12, ID3D12Fence::GetCompletedValue() returns u64::MAX once the device is removed. Previously if a.fence.GetCompletedValue() < a.value evaluated to false, causing mapped() to silently return Ok(()) on a removed device.
    • mapped() now checks for u64::MAX both before and after waiting (and checks WaitForSingleObject for wait failures), propagating device.GetDeviceRemovedReason()? (with DXGI_ERROR_DEVICE_REMOVED fallback).
  2. Host::upload_buffer() non-pool fallback missing mapped() (d3d12/crates/leonida-gpu/src/host.rs):
    • Buffers larger than pool::small::MOST (4 MiB, e.g., 64 MiB staging blocks, large send/scratch buffers, or LEONIDA_SMALL_BUFFER_POOL=0) allocate with not_resident() + resident(), but omitted mapped(&self.device)? unlike shared_buffer(), mapped_card_buffer(), uav_buffer(), pool::small::block(), pool::texture_with_cast(), and resource::host_texture_as().
  3. resource::twin_texture() swallowing resident() errors and omitting mapped() (d3d12/crates/leonida-gpu/src/resource.rs):
    • Both the colour twin and depth plane twin allocate with not_resident(), discarded resident() with let _ = ..., and omitted mapped(&host.device). Both now check resident(...).and_then(|()| mapped(...)), log a warning on failure, and decline to wrap an unmapped twin.

Tested

  • Official red/green check (python -I scripts/red_green.py fix/resident-mapped-parity --no-fetch): exit 0 (verdict: red then green) — both new hermetic child-process tests (host::mapped_tests::mapped_reports_error_on_removed_device using an isolated WARP ID3D12Device5::RemoveDevice(), and host::mapped_tests::upload_buffer_fallback_waits_for_mapped) fail on origin/main (45dc41f9) and pass with this branch.
  • Full leonida-gpu unit suite (cargo test --locked -p leonida-gpu --lib -- --skip depth_bounds_pipeline_loads_and_filters_draws): 704 passed, 0 failed.
  • Replay test (cargo test --locked -p leonida-gpu --features diagnostics --test replay): 1 passed, 0 failed.
  • Hygiene checks: python -I scripts/check_no_game_data.py and python -I tools/d3d12_switches.py --check d3d12/docs/switches.md both pass (exit 0).

GPUs tested: NVIDIA GeForce RTX 4090 D 48 GB (driver 616.92, Windows 11 10.0.26200) and Microsoft Basic Render Driver (WARP, isolated device-removal test).

AI-assisted: yes (Antigravity using Gemini 4 Neon for analysis, implementation, and tests; separate read-only reviewer agent using Gemini 4 Neon audited the diff, edge cases, parallel test isolation, and clean-room rules).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — no translator rules changed)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5612445576 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3199). Originally posted by [Jyx0208](https://github.com/Jyx0208) on 2026-12-02T18:31:30Z. --- Claimed in the coordination issue as: D3D12-RESIDENT-MAPPED-PARITY (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7353943683) ### What and why Closes three residency/mapping parity gaps in `leonida-gpu`: 1. **`host::mapped()` silent success on removed device (`d3d12/crates/leonida-gpu/src/host.rs`)**: - In D3D12, `ID3D12Fence::GetCompletedValue()` returns `u64::MAX` once the device is removed. Previously `if a.fence.GetCompletedValue() < a.value` evaluated to `false`, causing `mapped()` to silently return `Ok(())` on a removed device. - `mapped()` now checks for `u64::MAX` both before and after waiting (and checks `WaitForSingleObject` for wait failures), propagating `device.GetDeviceRemovedReason()?` (with `DXGI_ERROR_DEVICE_REMOVED` fallback). 2. **`Host::upload_buffer()` non-pool fallback missing `mapped()` (`d3d12/crates/leonida-gpu/src/host.rs`)**: - Buffers larger than `pool::small::MOST` (4 MiB, e.g., 64 MiB staging blocks, large send/scratch buffers, or `LEONIDA_SMALL_BUFFER_POOL=0`) allocate with `not_resident()` + `resident()`, but omitted `mapped(&self.device)?` unlike `shared_buffer()`, `mapped_card_buffer()`, `uav_buffer()`, `pool::small::block()`, `pool::texture_with_cast()`, and `resource::host_texture_as()`. 3. **`resource::twin_texture()` swallowing `resident()` errors and omitting `mapped()` (`d3d12/crates/leonida-gpu/src/resource.rs`)**: - Both the colour twin and depth plane twin allocate with `not_resident()`, discarded `resident()` with `let _ = ...`, and omitted `mapped(&host.device)`. Both now check `resident(...).and_then(|()| mapped(...))`, log a warning on failure, and decline to wrap an unmapped twin. ### Tested - Official red/green check (`python -I scripts/red_green.py fix/resident-mapped-parity --no-fetch`): **exit 0 (`verdict: red then green`)** — both new hermetic child-process tests (`host::mapped_tests::mapped_reports_error_on_removed_device` using an isolated WARP `ID3D12Device5::RemoveDevice()`, and `host::mapped_tests::upload_buffer_fallback_waits_for_mapped`) fail on `origin/main` (`45dc41f9`) and pass with this branch. - Full `leonida-gpu` unit suite (`cargo test --locked -p leonida-gpu --lib -- --skip depth_bounds_pipeline_loads_and_filters_draws`): **704 passed, 0 failed**. - Replay test (`cargo test --locked -p leonida-gpu --features diagnostics --test replay`): **1 passed, 0 failed**. - Hygiene checks: `python -I scripts/check_no_game_data.py` and `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` both pass (exit 0). GPUs tested: NVIDIA GeForce RTX 4090 D 48 GB (driver 616.92, Windows 11 10.0.26200) and Microsoft Basic Render Driver (WARP, isolated device-removal test). AI-assisted: yes (Antigravity using Gemini 4 Neon for analysis, implementation, and tests; separate read-only reviewer agent using Gemini 4 Neon audited the diff, edge cases, parallel test isolation, and clean-room rules). ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — no translator rules changed)
Jyx0208 commented 2026-12-02 18:38:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Jyx0208 on 2026-12-02T18:38:06Z.


Temporarily closing due to the repository's 1-open-PR limit per external contributor so I can open the higher-impact \D3D12-TEXTURE-POOL-COMMIT-FLOOR\ PR first (keeping one topic per PR per CONTRIBUTING.md). The \Jyx0208:fix/resident-mapped-parity\ branch remains intact and I will reopen this as a separate follow-up PR once that lands.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7354911896 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3199#issuecomment-7354911896). Originally posted by [Jyx0208](https://github.com/Jyx0208) on 2026-12-02T18:38:06Z. --- Temporarily closing due to the repository's 1-open-PR limit per external contributor so I can open the higher-impact \D3D12-TEXTURE-POOL-COMMIT-FLOOR\ PR first (keeping one topic per PR per CONTRIBUTING.md). The \Jyx0208:fix/resident-mapped-parity\ branch remains intact and I will reopen this as a separate follow-up PR once that lands.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!3199
No description provided.