d3d12: pool unaligned not_aliased buffers into shared L1/L0 heaps #3776

Open
github-bot wants to merge 1 commit from github-bot/gh-3774 into main AGit
Owner

@⁠Holastor on GitHub · 2026-12-01 18:13 UTC · original
Holastor:fix/d3d12-shared-buffer-heap-pool → main · +84 −3 · 1 files · 1 commits · open

Summary

Pool unaligned not_aliased buffer allocations in src/startup_stubs/d3d12_xs_device.cpp into shared L1/L0 D3D12 heaps instead of creating a dedicated heap per buffer.

  • Suballocates 64 KiB-aligned buffers (<= 16 MiB) from 64 MiB ID3D12Heap chunks tied to the active ID3D12Device*.
  • Automatically places read-only (!ALLOW_UNORDERED_ACCESS) buffers into D3D12_HEAP_TYPE_CUSTOM (D3D12_MEMORY_POOL_L0) on discrete GPUs with < 9 GiB local VRAM budget (controllable via X2W_D3D12_L0_BUFFER_POOL=0|1), keeping POOL_L1 (DEFAULT) for 12+ GiB GPUs and UAV buffers.
  • Adds check_device("before Device view", N) prior to view creation in Device::slot18 to distinguish asynchronous prior device removal from view creation failures.

Scope

  • src/startup_stubs/d3d12_xs_device.cpp (+84/-3)

Verification

  • cmake --build --preset release — passed (0 warnings)
  • Startup run (-Console x -Gpu amd, 8 GiB VRAM): reduces live ID3D12Heap count from ~18,500 to ~16 and cuts peak local VRAM usage by ~600 MiB.
**@⁠Holastor** on GitHub · 2026-12-01 18:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3774) `Holastor:fix/d3d12-shared-buffer-heap-pool` → `main` · +84 −3 · 1 files · 1 commits · open ## Summary Pool unaligned `not_aliased` buffer allocations in `src/startup_stubs/d3d12_xs_device.cpp` into shared L1/L0 D3D12 heaps instead of creating a dedicated heap per buffer. - Suballocates 64 KiB-aligned buffers (`<= 16 MiB`) from 64 MiB `ID3D12Heap` chunks tied to the active `ID3D12Device*`. - Automatically places read-only (`!ALLOW_UNORDERED_ACCESS`) buffers into `D3D12_HEAP_TYPE_CUSTOM` (`D3D12_MEMORY_POOL_L0`) on discrete GPUs with `< 9 GiB` local VRAM budget (controllable via `X2W_D3D12_L0_BUFFER_POOL=0|1`), keeping `POOL_L1` (`DEFAULT`) for `12+ GiB` GPUs and UAV buffers. - Adds `check_device("before Device view", N)` prior to view creation in `Device::slot18` to distinguish asynchronous prior device removal from view creation failures. ## Scope - `src/startup_stubs/d3d12_xs_device.cpp` (+84/-3) ## Verification - `cmake --build --preset release` — passed (0 warnings) - Startup run (`-Console x -Gpu amd`, 8 GiB VRAM): reduces live `ID3D12Heap` count from ~18,500 to ~16 and cuts peak local VRAM usage by ~600 MiB.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 18:16:06 +00:00
Author
Owner

@⁠Leyukaka on GitHub · 2026-12-01 21:57 UTC · original · reviewed

Reviewed 88bf81273. Thanks for the change. A few points:

  • src/startup_stubs/d3d12_xs_device.cpp:7103 selects custom L0 before checking pool eligibility. A 32 MiB non-UAV fallback buffer bypasses pooling and still goes to L0. If an L0 heap allocation fails, the committed retry also uses L0, which places those buffers in system memory on a discrete adapter. Please preserve the committed DEFAULT fallback for ineligible buffers and pool failures. D3D12 heap property mapping
  • Space released by pooled buffers is never reused, and the pool keeps every 64 MiB heap alive, so pooled memory only grows during a long run.
  • src/startup_stubs/d3d12_xs_device.cpp:7092 retains raw heap references until a different device enters the pool. Unloading the module drops the pointer vector without releasing those references. The 64 MiB backing allocation is also absent from the budget counters, while :6847 charges each slice as committed. Please give the pool explicit teardown and account for each heap once.
  • Please add a source-built regression that observes two small fallback buffers sharing one heap, checks independent readback, and covers the committed fallback after a forced pool allocation failure. The common fallback also covers mapped, capped and failed-alias buffers. Please clarify the intended scope and move the view check at :6417 to a separate change.
**@⁠Leyukaka** on GitHub · 2026-12-01 21:57 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3774#pullrequestreview-6140880839) · **reviewed** Reviewed `88bf81273`. Thanks for the change. A few points: - `src/startup_stubs/d3d12_xs_device.cpp:7103` selects custom L0 before checking pool eligibility. A 32 MiB non-UAV fallback buffer bypasses pooling and still goes to L0. If an L0 heap allocation fails, the committed retry also uses L0, which places those buffers in system memory on a discrete adapter. Please preserve the committed DEFAULT fallback for ineligible buffers and pool failures. [D3D12 heap property mapping](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device-getcustomheapproperties%28uint_d3d12_heap_type%29) - Space released by pooled buffers is never reused, and the pool keeps every 64 MiB heap alive, so pooled memory only grows during a long run. - `src/startup_stubs/d3d12_xs_device.cpp:7092` retains raw heap references until a different device enters the pool. Unloading the module drops the pointer vector without releasing those references. The 64 MiB backing allocation is also absent from the budget counters, while `:6847` charges each slice as committed. Please give the pool explicit teardown and account for each heap once. - Please add a source-built regression that observes two small fallback buffers sharing one heap, checks independent readback, and covers the committed fallback after a forced pool allocation failure. The common fallback also covers mapped, capped and failed-alias buffers. Please clarify the intended scope and move the view check at `:6417` to a separate change.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 19:53 UTC · original · reviewed

On 88bf8127, with X2W_D3D12_L0_BUFFER_POOL=1, I ran MSVC Debug ctest --preset dev --no-tests=error -j 1 -V -R '^startup_slot133_heap_cap_(hardware_debug1|capped_hardware)$'. The two existing slot 133 cases passed on Windows 11 build 26300 and RTX 4060 Laptop 32.0.16.1656, with the registered debug-layer settings. They check the existing alias/copy behavior. They do not distinguish a committed DEFAULT fallback from a committed L0 fallback or verify released ranges are reused, so the open review points still apply.

**@⁠R0Y00** on GitHub · 2026-12-02 19:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3774#pullrequestreview-6157851028) · **reviewed** On 88bf8127, with `X2W_D3D12_L0_BUFFER_POOL=1`, I ran MSVC Debug `ctest --preset dev --no-tests=error -j 1 -V -R '^startup_slot133_heap_cap_(hardware_debug1|capped_hardware)$'`. The two existing slot 133 cases passed on Windows 11 build 26300 and RTX 4060 Laptop 32.0.16.1656, with the registered debug-layer settings. They check the existing alias/copy behavior. They do not distinguish a committed DEFAULT fallback from a committed L0 fallback or verify released ranges are reused, so the open review points still apply.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-03 06:33 UTC · original · requested changes

Two issues in the slot 133 fallback on this head:

  1. PoolState owns every 64 MiB chunk until a different device is seen, and offset only increases. Releasing the last buffer in a chunk returns neither its range nor the chunk. Repeated serial allocate/release of 16 MiB fallback buffers can therefore retain a new 64 MiB heap every four allocations while the live buffer count stays at one. Please tie the chunk lifetime to the resources using it, or reuse retired ranges once safe, and add a small repeated allocate/release fixture that bounds retained pool bytes.
  2. When automatic L0 selection is enabled, a failed L0 pool attempt falls back to a committed allocation with the same custom L0 properties. The previously available DEFAULT committed path is never tried. Please keep that fallback so a rejected L0 allocation cannot turn a previously successful slot 133 call into a refusal. A controlled L0 failure should still return a usable default-backed resource.
**@⁠R0Y00** on GitHub · 2026-12-03 06:33 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3774#pullrequestreview-6164775016) · **requested changes** Two issues in the slot 133 fallback on this head: 1. PoolState owns every 64 MiB chunk until a different device is seen, and offset only increases. Releasing the last buffer in a chunk returns neither its range nor the chunk. Repeated serial allocate/release of 16 MiB fallback buffers can therefore retain a new 64 MiB heap every four allocations while the live buffer count stays at one. Please tie the chunk lifetime to the resources using it, or reuse retired ranges once safe, and add a small repeated allocate/release fixture that bounds retained pool bytes. 2. When automatic L0 selection is enabled, a failed L0 pool attempt falls back to a committed allocation with the same custom L0 properties. The previously available DEFAULT committed path is never tried. Please keep that fallback so a rejected L0 allocation cannot turn a previously successful slot 133 call into a refusal. A controlled L0 failure should still return a usable default-backed resource.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3776/head:github-bot/gh-3774'
git switch 'github-bot/gh-3774'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3774'
git switch 'github-bot/gh-3774'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3774'
git switch 'github-bot/gh-3774'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3774'
git switch 'main'
git merge --squash 'github-bot/gh-3774'
git switch 'main'
git merge --ff-only 'github-bot/gh-3774'
git switch 'main'
git merge 'github-bot/gh-3774'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.