runtime: report D3D12 resource budget and lifetimes #2248

Merged
Jess-Doit merged 1 commit from upstream-pr/2256 into main 2026-11-25 08:37:08 +00:00
Jess-Doit commented 2026-11-25 08:16:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-25T08:16:52Z.


Refs #874. Budget telemetry request on #870

Change

  • Opt in with X2W_D3D12_BUDGET=1 to log local and non-local DXGI usage and budget beside each D3D12 heartbeat. The sample uses the presenting queue's device LUID.
  • Count placed resources, committed textures and buffers, upload and readback buffers, and explicit heaps using desktop-reported sizes. Charge each shared heap once. Placed resource sizes describe what occupies those heaps.
  • Separate active allocations from desktop backing retained by the shim after final guest release. A native object's final release removes its accounting entry. The snapshot export makes the same counters available to the fixture.

Tests

The source-built fixture exercises committed resources, two explicit heaps with placed buffers and a texture, two slot 133 buffers sharing one mapped heap, slot 133 committed fallbacks, and two slot 148 requests sharing one upload page. It checks every category's byte and object delta, the shared heap after one child is released, page retirement after fence completion and allocator reuse, and exact restoration of the active baseline. It verifies retained backing separately.

The missing-release control leaves one 65,536-byte committed buffer active and fails with the expected diagnostic. The direct GTX 1060 run returned exit 6. A two-present smoke check on the GTX 1060 emitted one x2w-budget line with numeric values for both memory segments when enabled and only the regular heartbeat when unset.

Verification

Tested source matches b5fd19f9a6, based on e94e14ff. The build revision is e94e14ff-dirty because linked worktree metadata is read-only.

Release adapter Driver Debug layer Pass Fail Skip
Microsoft Basic Render Driver (in-box WARP) 10.0.19041.5794 Off 2 0 0
Microsoft Basic Render Driver (in-box WARP) 10.0.19041.5794 On 2 0 0
NVIDIA GeForce GTX 1060 6GB 32.0.15.6094 Off 2 0 0
NVIDIA GeForce GTX 1060 6GB 32.0.15.6094 On 2 0 0

Windows 10 build 19045.6456. Each row includes one baseline check and one expected-failure control. The debug-enabled tests reported zero D3D12 warnings or errors. Both adapters exercised the mapped-heap subcase. The neighbouring slot 148 and committed heap selection returned six passes and one failure. The no-debug WARP slot 148 test timed out waiting for its observer and returned exit 18 both on this patch and on a separate build of e94e14ff.

With a prepared requirements-dev.txt Python environment:

. .\tools\dev.ps1
$env:VULKAN_SDK = 'C:\VulkanSDK\1.4.363.0'
$env:XBOX2WINDOWS_ADAPTER = '1060'
$py = (Resolve-Path .\out\pe-env\Scripts\python.exe).Path
cmake --preset release "-DINTAKE_PYTHON=$py"
cmake --build --preset release --target startup_resource_budget startup_slot148_acceptance startup_committed_heap_state_check -j 2
ctest --preset release --no-tests=error -j 1 -R '^startup_resource_budget_' -V
ctest --preset release --no-tests=error -j 1 -R '^startup_(resource_budget|slot148_acceptance|committed_heap_state)_' -V
ctest --preset release --no-tests=error -j 1 -R '^startup_slot148_acceptance_nodebug_warp$' -V
& $py -B tests/docs_links_check.py
& $py -B tests/bidi_controls_check.py
git diff --check

Limits

DXGI usage includes allocations outside the instrumented categories. The counters report allocation sizes and shim ownership, while DXGI reports the adapter's usage and budget. The change leaves resource ownership and retirement behavior as it was.

AI-assisted (Codex).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6600185401 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2256). Originally posted by [Jess-Doit](https://github.com/Jess-Doit) on 2026-11-25T08:16:52Z. --- Refs #874. [Budget telemetry request on #870](https://github.com/R0Y00/Xbox2Windows/issues/870#issuecomment-7156163956) ## Change - Opt in with X2W_D3D12_BUDGET=1 to log local and non-local DXGI usage and budget beside each D3D12 heartbeat. The sample uses the presenting queue's device LUID. - Count placed resources, committed textures and buffers, upload and readback buffers, and explicit heaps using desktop-reported sizes. Charge each shared heap once. Placed resource sizes describe what occupies those heaps. - Separate active allocations from desktop backing retained by the shim after final guest release. A native object's final release removes its accounting entry. The snapshot export makes the same counters available to the fixture. ## Tests The source-built fixture exercises committed resources, two explicit heaps with placed buffers and a texture, two slot 133 buffers sharing one mapped heap, slot 133 committed fallbacks, and two slot 148 requests sharing one upload page. It checks every category's byte and object delta, the shared heap after one child is released, page retirement after fence completion and allocator reuse, and exact restoration of the active baseline. It verifies retained backing separately. The missing-release control leaves one 65,536-byte committed buffer active and fails with the expected diagnostic. The direct GTX 1060 run returned exit 6. A two-present smoke check on the GTX 1060 emitted one x2w-budget line with numeric values for both memory segments when enabled and only the regular heartbeat when unset. ## Verification Tested source matches b5fd19f9a6dca2b2290180854157fce6368359e0, based on e94e14ff. The build revision is e94e14ff-dirty because linked worktree metadata is read-only. | Release adapter | Driver | Debug layer | Pass | Fail | Skip | |---|---|---|---:|---:|---:| | Microsoft Basic Render Driver (in-box WARP) | 10.0.19041.5794 | Off | 2 | 0 | 0 | | Microsoft Basic Render Driver (in-box WARP) | 10.0.19041.5794 | On | 2 | 0 | 0 | | NVIDIA GeForce GTX 1060 6GB | 32.0.15.6094 | Off | 2 | 0 | 0 | | NVIDIA GeForce GTX 1060 6GB | 32.0.15.6094 | On | 2 | 0 | 0 | Windows 10 build 19045.6456. Each row includes one baseline check and one expected-failure control. The debug-enabled tests reported zero D3D12 warnings or errors. Both adapters exercised the mapped-heap subcase. The neighbouring slot 148 and committed heap selection returned six passes and one failure. The no-debug WARP slot 148 test timed out waiting for its observer and returned exit 18 both on this patch and on a separate build of e94e14ff. With a prepared requirements-dev.txt Python environment: ```powershell . .\tools\dev.ps1 $env:VULKAN_SDK = 'C:\VulkanSDK\1.4.363.0' $env:XBOX2WINDOWS_ADAPTER = '1060' $py = (Resolve-Path .\out\pe-env\Scripts\python.exe).Path cmake --preset release "-DINTAKE_PYTHON=$py" cmake --build --preset release --target startup_resource_budget startup_slot148_acceptance startup_committed_heap_state_check -j 2 ctest --preset release --no-tests=error -j 1 -R '^startup_resource_budget_' -V ctest --preset release --no-tests=error -j 1 -R '^startup_(resource_budget|slot148_acceptance|committed_heap_state)_' -V ctest --preset release --no-tests=error -j 1 -R '^startup_slot148_acceptance_nodebug_warp$' -V & $py -B tests/docs_links_check.py & $py -B tests/bidi_controls_check.py git diff --check ``` ## Limits DXGI usage includes allocations outside the instrumented categories. The counters report allocation sizes and shim ownership, while DXGI reports the adapter's usage and budget. The change leaves resource ownership and retirement behavior as it was. AI-assisted (Codex). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-25 09:04:03 +00:00
tedin7 commented 2026-11-25 09:04:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T08:21:18Z.

Review: COMMENTED


Review of head b5fd19f9a6dca2b2290180854157fce6368359e0.

Result: no P1, no P2. Two nits.

Checked by reading the diff (11 files, +677/-10):

  • Ticket lifetime: the desktop object's private data owns the ticket, so a native final release drops the entry. Guest final release moves it from live to retained through end_use(). Wrapper blocks are zero-initialised in wrap() (memset), so the new budget and backing_budget fields start null.
  • Shared heaps: the heap ticket is charged once. Each placed child calls begin_use() and end_use(), so the heap stays live while any child is alive. existing_heaps keeps the heap reference and the ticket together, so the raw pointer stays valid.
  • Failure paths: a failed SetPrivateDataInterface or an oversize allocation only bumps unmeasured. The failed-Map upload ring case ends up in retained, which matches the leaked desktop reference described in the comment.
  • The opt-in is X2W_D3D12_BUDGET=1 only. With it off, enabled() short-circuits every hook and the export returns FALSE.
  • CMake: the 4 backend/debug cases each have an inline_control mutation guard with PASS_REGULAR_EXPRESSION.
  • Title and body are neutral and contain no model names.

Commands run locally on the worktree:

  • git diff --check origin/main...HEAD: clean
  • python3 tests/docs_links_check.py: self-test passed, 18 Markdown files and 431 links OK
  • python3 tests/bidi_controls_check.py: self-test passed, 1537 files, no bidirectional controls
  • gh pr checks 2256: Linux Python checks, Windows build and test, and gfx1013 fixtures pass. The /O2 build is skipped.

Not run: the new fixture is Windows-only C++, so I did not mutate or run it here. I did not queue the Windows VM. CI Windows is green on this head, and the fixture ships its own inline-control mutation test.

Nits (non-blocking):

  1. src/startup_stubs/resource_budget.h uses GetEnvironmentVariableA, SRWLOCK and Interlocked* with only <d3d12.h> included. It works through the transitive windows.h, but an explicit #include <windows.h> would make the header self-contained.
  2. report_budget (d3d12_xs_device.cpp, near the new function before queue_present) creates a new DXGI factory at every heartbeat. This is fine while it is opt-in and the heartbeat is slow, but caching the adapter would avoid the churn.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6054048866 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2256#pullrequestreview-6054048866). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T08:21:18Z. **Review:** COMMENTED --- Review of head `b5fd19f9a6dca2b2290180854157fce6368359e0`. **Result: no P1, no P2. Two nits.** Checked by reading the diff (11 files, +677/-10): - Ticket lifetime: the desktop object's private data owns the ticket, so a native final release drops the entry. Guest final release moves it from live to retained through `end_use()`. Wrapper blocks are zero-initialised in `wrap()` (`memset`), so the new `budget` and `backing_budget` fields start null. - Shared heaps: the heap ticket is charged once. Each placed child calls `begin_use()` and `end_use()`, so the heap stays live while any child is alive. `existing_heaps` keeps the heap reference and the ticket together, so the raw pointer stays valid. - Failure paths: a failed `SetPrivateDataInterface` or an oversize allocation only bumps `unmeasured`. The failed-`Map` upload ring case ends up in retained, which matches the leaked desktop reference described in the comment. - The opt-in is `X2W_D3D12_BUDGET=1` only. With it off, `enabled()` short-circuits every hook and the export returns FALSE. - CMake: the 4 backend/debug cases each have an `inline_control` mutation guard with `PASS_REGULAR_EXPRESSION`. - Title and body are neutral and contain no model names. Commands run locally on the worktree: - `git diff --check origin/main...HEAD`: clean - `python3 tests/docs_links_check.py`: self-test passed, 18 Markdown files and 431 links OK - `python3 tests/bidi_controls_check.py`: self-test passed, 1537 files, no bidirectional controls - `gh pr checks 2256`: Linux Python checks, Windows build and test, and gfx1013 fixtures pass. The /O2 build is skipped. Not run: the new fixture is Windows-only C++, so I did not mutate or run it here. I did not queue the Windows VM. CI Windows is green on this head, and the fixture ships its own inline-control mutation test. Nits (non-blocking): 1. `src/startup_stubs/resource_budget.h` uses `GetEnvironmentVariableA`, `SRWLOCK` and `Interlocked*` with only `<d3d12.h>` included. It works through the transitive `windows.h`, but an explicit `#include <windows.h>` would make the header self-contained. 2. `report_budget` (`d3d12_xs_device.cpp`, near the new function before `queue_present`) creates a new DXGI factory at every heartbeat. This is fine while it is opt-in and the heartbeat is slow, but caching the adapter would avoid the churn. AI-assisted; commands listed were executed locally.
Jess-Doit commented 2026-11-25 09:04:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-25T08:43:22Z.


Thanks for the review. Both nits make sense:

  • an explicit <windows.h> in resource_budget.h;
  • caching the adapter in report_budget instead of creating a factory every heartbeat.

I'll fold them into the next change that touches this code, rather than open a PR for them alone.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7157388132 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2256#issuecomment-7157388132). Originally posted by [Jess-Doit](https://github.com/Jess-Doit) on 2026-11-25T08:43:22Z. --- Thanks for the review. Both nits make sense: - an explicit `<windows.h>` in `resource_budget.h`; - caching the adapter in `report_budget` instead of creating a factory every heartbeat. I'll fold them into the next change that touches this code, rather than open a PR for them alone.
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
R0Y00/Xbox2Windows!2248
No description provided.