d3d12: keep a Resizable BAR arena in VRAM when the driver excludes it from a very large buffer (RT-ARENA-REBAR-FIT) #1732

Manually merged
github merged 2 commits from gh-pr/1732/head into gh-pr/1732/base 2026-11-26 10:02:59 +00:00
Owner

@⁠kawaii-shakal on GitHub · 2026-11-25 10:02 UTC · original
kawaii-shakal:arena-rebar-cap → main · +101 −6 · 4 files · 2 commits · merged 2026-11-25 14:38 UTC

Claim: RT-ARENA-REBAR-FIT, #1608 (step 2 of #1702)

What and why

On the retail boot with LEONIDA_ARENA_MB=4096, which is #1537's workaround for the world-start device loss, the memory probe showed the 4,095 MiB arena in system memory (type 2, HOST_VISIBLE | HOST_COHERENT). That was on an RTX 5070 Ti that does have Resizable BAR: [rt] d3d12: upload heaps in VRAM (DEVICE_LOCAL | HOST_VISIBLE, Resizable BAR). The cost: 4 GiB more host commit (that run ended with commit and RAM exhausted, #1702), and every default-heap buffer, descriptor heap and shader program read over PCIe.

The cause is the buffer's memoryTypeBits, not the allocation. A standalone Vulkan probe on the same machine (RTX 5070 Ti, NVIDIA 617.42, Windows 11 Pro 26300) shows this:

  • a 4,095 MiB allocation succeeds in every host-visible type, the DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT one included;
  • but a buffer with the arena's usage flags reports memoryTypeBits 0x1f up to 4,090 MiB and 0xf from 4,092 MiB, so the ReBAR type is excluded;
  • memory_type_order then never offers that type, and the arena goes to the first host-visible coherent one.
arena-shaped buffer memoryTypeBits
256 MiB to 4,090 MiB 0x1f (type 4, ReBAR, allowed)
4,092, 4,093, 4,094, 4,095 MiB 0xf (type 4 excluded)
  • runtime/src/arena_size.h: device_local_arena_bytes(bytes, fits). It returns bytes when it fits. Otherwise it returns the largest size below, in 16 MiB steps and losing at most an eighth, that does. If nothing in reach fits it returns bytes unchanged. It also adds a new cap bit, arena_cap_bar, with its name.
  • runtime/shims/d3d12_vulkan_device.cpp, create_arena: with Resizable BAR (leonida::resizable_bar), fits creates a probe buffer of that size and checks whether its memoryTypeBits has a DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT type. Each size tried costs one probe buffer. The cap is logged like the existing ones. Without Resizable BAR nothing changes.
  • docs/d3d12_vulkan.md: the memory type policy (section 3.3) says this.

Test

  • arena_size_test: new resizable_bar_fit case with a synthetic fits. It covers:
    • the 5070 Ti limit (4,095 MiB becomes 4,080 MiB);
    • already fitting, and a small arena (both unchanged);
    • a limit more than an eighth away, and nothing fitting (both unchanged);
    • the number of probes per step;
    • the cap's name.
  • On the RTX 5070 Ti, d3d12_title_va_test with LEONIDA_ARENA_MB=4096 logs GPU address arena: 4080 MiB of the 4095 MiB asked for, capped by the device's Resizable BAR memory types for a buffer that large and passes. With LEONIDA_ARENA_MB=1024 it logs no cap and passes. At 4,080 MiB the probe above gives 0x1f, so memory_type_order picks the ReBAR type first.
  • scripts/check_no_game_data.py is clean.

Not done here: a retail boot with this branch, so the memory-probe line arena 4080 MiB (type 4) and the effect on host commit are not yet measured. Other vendors' limits are not measured either: a driver that keeps the type for every size takes the first branch and nothing changes.

AI-assisted: yes (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

**@⁠kawaii-shakal** on GitHub · 2026-11-25 10:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1732) `kawaii-shakal:arena-rebar-cap` → `main` · +101 −6 · 4 files · 2 commits · merged 2026-11-25 14:38 UTC Claim: RT-ARENA-REBAR-FIT, https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 (step 2 of #1702) ### What and why On the retail boot with `LEONIDA_ARENA_MB=4096`, which is #1537's workaround for the world-start device loss, the memory probe showed the 4,095 MiB arena in system memory (type 2, `HOST_VISIBLE | HOST_COHERENT`). That was on an RTX 5070 Ti that does have Resizable BAR: `[rt] d3d12: upload heaps in VRAM (DEVICE_LOCAL | HOST_VISIBLE, Resizable BAR)`. The cost: 4 GiB more host commit (that run ended with commit and RAM exhausted, #1702), and every default-heap buffer, descriptor heap and shader program read over PCIe. The cause is the buffer's `memoryTypeBits`, not the allocation. A standalone Vulkan probe on the same machine (RTX 5070 Ti, NVIDIA 617.42, Windows 11 Pro 26300) shows this: - a 4,095 MiB allocation succeeds in every host-visible type, the `DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT` one included; - but a buffer with the arena's usage flags reports `memoryTypeBits` `0x1f` up to 4,090 MiB and `0xf` from 4,092 MiB, so the ReBAR type is excluded; - `memory_type_order` then never offers that type, and the arena goes to the first host-visible coherent one. | arena-shaped buffer | memoryTypeBits | |---|---| | 256 MiB to 4,090 MiB | `0x1f` (type 4, ReBAR, allowed) | | 4,092, 4,093, 4,094, 4,095 MiB | `0xf` (type 4 excluded) | - `runtime/src/arena_size.h`: `device_local_arena_bytes(bytes, fits)`. It returns `bytes` when it fits. Otherwise it returns the largest size below, in 16 MiB steps and losing at most an eighth, that does. If nothing in reach fits it returns `bytes` unchanged. It also adds a new cap bit, `arena_cap_bar`, with its name. - `runtime/shims/d3d12_vulkan_device.cpp`, `create_arena`: with Resizable BAR (`leonida::resizable_bar`), `fits` creates a probe buffer of that size and checks whether its `memoryTypeBits` has a `DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT` type. Each size tried costs one probe buffer. The cap is logged like the existing ones. Without Resizable BAR nothing changes. - `docs/d3d12_vulkan.md`: the memory type policy (section 3.3) says this. ### Test - `arena_size_test`: new `resizable_bar_fit` case with a synthetic `fits`. It covers: - the 5070 Ti limit (4,095 MiB becomes 4,080 MiB); - already fitting, and a small arena (both unchanged); - a limit more than an eighth away, and nothing fitting (both unchanged); - the number of probes per step; - the cap's name. - On the RTX 5070 Ti, `d3d12_title_va_test` with `LEONIDA_ARENA_MB=4096` logs `GPU address arena: 4080 MiB of the 4095 MiB asked for, capped by the device's Resizable BAR memory types for a buffer that large` and passes. With `LEONIDA_ARENA_MB=1024` it logs no cap and passes. At 4,080 MiB the probe above gives `0x1f`, so `memory_type_order` picks the ReBAR type first. - `scripts/check_no_game_data.py` is clean. Not done here: a retail boot with this branch, so the memory-probe line `arena 4080 MiB (type 4)` and the effect on host commit are not yet measured. Other vendors' limits are not measured either: a driver that keeps the type for every size takes the first branch and nothing changes. AI-assisted: yes (Claude Code, Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On an RTX 5070 Ti (NVIDIA 617.42) a 4,090 MiB arena-shaped buffer may use
the DEVICE_LOCAL | HOST_VISIBLE type and a 4,092 MiB one may not, so
LEONIDA_ARENA_MB=4096 (4,095 MiB, the workaround from #1537) placed the
whole arena in system memory: 4 GiB more host commit, and every
default-heap buffer read over PCIe.

With Resizable BAR, create_arena now tries 16 MiB smaller sizes, by at
most an eighth, and keeps the largest whose buffer may still use that type
(device_local_arena_bytes in arena_size.h). The cap is logged like the
other device caps. Without Resizable BAR nothing changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AI-assisted by Codex.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:02:56 +00:00
github manually merged commit a607b779c4 into gh-pr/1732/base 2026-11-26 10:02:59 +00:00
Author
Owner

@⁠quartz-heron-58 on GitHub · 2026-11-25 10:21 UTC · original

The retail boot you list as not done, on a second card: 96bbe55ce against its base 3de22a190, RTX 4080 16 GB (NVIDIA 621.24, Resizable BAR on), Windows 11 build 26300, 32 GB RAM, own 1.0.2.1 copy. The placement is confirmed. The host-commit saving is not what I see, so please read the second half.

Tests on this head: arena_size passes. d3d12_title_va with LEONIDA_ARENA_MB=4096 logs GPU address arena: 4080 MiB of the 4095 MiB asked for, capped by the device's Resizable BAR memory types for a buffer that large and passes; with 1024 it logs no cap and passes. So this card and driver have the same limit as your 5070 Ti.

Boot, LEONIDA_ARENA_MB=4096 LEONIDA_MEM_PROBE=1, 240 s each, one run each. No SDL3 in this build, so no input reaches the title; it is the same unattended four minutes in both. Neither run lost the device. Memory-probe line at t=200 s:

arena type 2 (HV+HC) type 4 (DL+HV+HC) arena in use
base 4095 MiB (type 2) 2 x 4100 MiB 4 x 18 MiB 462 MiB
this PR 4080 MiB (type 4) 1 x 5 MiB 5 x 4098 MiB 458 MiB

Type 1 (device-local) is 1258 x 4882 MiB in both. So the arena moves from system memory to the Resizable BAR type, as intended.

Process counters, sampled from outside every 2 s:

private bytes at 10 / 61 / 121 / 201 s free physical RAM: start, then at 201 s
base 6.8 / 13.8 / 13.9 / 13.5 GB 18.1, 12.9 GB (5.2 GB less)
this PR 11.5 / 16.8 / 17.1 / 17.3 GB 21.8, 16.7 GB (5.1 GB less)

Private bytes are 3 to 4.7 GB higher with the PR, from the first sample on, and the drop in free physical memory is the same in both. On this machine, then, moving the arena to type 4 did not reduce host commit; it looks as if the driver commits host backing for a device-local host-visible allocation of that size, and with the arena in type 2 only the touched part (462 MiB in use, 655 MiB peak) costs RAM. That is my reading of two runs, not something I traced: I did not look at which allocation the extra private bytes belong to, and the two runs started from different amounts of free memory (the base run came second, right after a build).

What this does not measure: read speed over PCIe against VRAM, which is the other half of your motivation, and anything past the title's first screens. If the commit figure matters for #1702, working set and commit charge side by side on your machine would settle whether this is the 4080's driver or general.

Counters and log lines only, no game data. AI-assisted: yes (Claude Code)

**@⁠quartz-heron-58** on GitHub · 2026-11-25 10:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1732#issuecomment-7159574385) The retail boot you list as not done, on a second card: 96bbe55ce against its base 3de22a190, RTX 4080 16 GB (NVIDIA 621.24, Resizable BAR on), Windows 11 build 26300, 32 GB RAM, own 1.0.2.1 copy. The placement is confirmed. The host-commit saving is not what I see, so please read the second half. **Tests on this head:** `arena_size` passes. `d3d12_title_va` with `LEONIDA_ARENA_MB=4096` logs `GPU address arena: 4080 MiB of the 4095 MiB asked for, capped by the device's Resizable BAR memory types for a buffer that large` and passes; with `1024` it logs no cap and passes. So this card and driver have the same limit as your 5070 Ti. **Boot, `LEONIDA_ARENA_MB=4096 LEONIDA_MEM_PROBE=1`, 240 s each, one run each.** No SDL3 in this build, so no input reaches the title; it is the same unattended four minutes in both. Neither run lost the device. Memory-probe line at t=200 s: | | arena | type 2 (HV+HC) | type 4 (DL+HV+HC) | arena in use | |---|---|---|---|---| | base | 4095 MiB (type 2) | 2 x 4100 MiB | 4 x 18 MiB | 462 MiB | | this PR | 4080 MiB (type 4) | 1 x 5 MiB | 5 x 4098 MiB | 458 MiB | Type 1 (device-local) is 1258 x 4882 MiB in both. So the arena moves from system memory to the Resizable BAR type, as intended. **Process counters, sampled from outside every 2 s:** | | private bytes at 10 / 61 / 121 / 201 s | free physical RAM: start, then at 201 s | |---|---|---| | base | 6.8 / 13.8 / 13.9 / 13.5 GB | 18.1, 12.9 GB (5.2 GB less) | | this PR | 11.5 / 16.8 / 17.1 / 17.3 GB | 21.8, 16.7 GB (5.1 GB less) | Private bytes are 3 to 4.7 GB **higher** with the PR, from the first sample on, and the drop in free physical memory is the same in both. On this machine, then, moving the arena to type 4 did not reduce host commit; it looks as if the driver commits host backing for a device-local host-visible allocation of that size, and with the arena in type 2 only the touched part (462 MiB in use, 655 MiB peak) costs RAM. That is my reading of two runs, not something I traced: I did not look at which allocation the extra private bytes belong to, and the two runs started from different amounts of free memory (the base run came second, right after a build). What this does not measure: read speed over PCIe against VRAM, which is the other half of your motivation, and anything past the title's first screens. If the commit figure matters for #1702, working set and commit charge side by side on your machine would settle whether this is the 4080's driver or general. Counters and log lines only, no game data. AI-assisted: yes (Claude Code)
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 10:23 UTC · original · reviewed

The stock arena_size_test passed on Ubuntu under WSL at 96bbe55c and its merge base 3de22a19. A separate CPU-only test of the new probe block, using synthetic Vulkan requirements and the runtime's actual memory_type_order, reproduced one mismatch between the fit predicate and allocation policy. Details are inline.

AI-assisted. No GPU or game data used for this check.

**@⁠lixonik** on GitHub · 2026-11-25 10:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1732#pullrequestreview-6055779893) · **reviewed** The stock `arena_size_test` passed on Ubuntu under WSL at `96bbe55c` and its merge base `3de22a19`. A separate CPU-only test of the new probe block, using synthetic Vulkan requirements and the runtime's actual `memory_type_order`, reproduced one mismatch between the fit predicate and allocation policy. Details are inline. AI-assisted. No GPU or game data used for this check.
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 10:23 UTC · original
On runtime/shims/d3d12_vulkan_device.cpp line 10258 (outdated):

@@ -10247,6 +10247,37 @@ class Device final
                 return false;
             }
         }
+        // With Resizable BAR, keep the arena where its buffer may still use DEVICE_LOCAL | HOST_VISIBLE memory
+        // (arena_size.h, device_local_arena_bytes): a driver can leave that type out of a very large buffer.
+        if (leonida::resizable_bar(state_.memory)) {
+            const auto device_local = [&](std::uint32_t bits) {
+                constexpr VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
+                                                         VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
+                                                         VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
+                for (std::uint32_t i = 0; i < state_.memory.memoryTypeCount; ++i)
+                    if ((bits & (1u << i)) && (state_.memory.memoryTypes[i].propertyFlags & wanted) == wanted) return true;

The probe checks the type bit and flags here. Allocation also requires requirements.size to fit the selected type's heap. I reproduced the difference with a 4070 MiB BAR heap: the 4095 MiB buffer offers only system memory, and the 4080 and 4064 MiB buffers both offer the BAR type. The PR stops at 4080 MiB and marks the result BAR-capped, but memory_type_order drops the BAR type, so allocation can only try system memory. At 4064 MiB the same allocation policy selects BAR. Could this predicate reuse the allocation eligibility check with the full VkMemoryRequirements so the search continues when the heap is too small?

**@⁠lixonik** on GitHub · 2026-11-25 10:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1732#discussion_r4693487823) On `runtime/shims/d3d12_vulkan_device.cpp` line 10258 (outdated): ```diff @@ -10247,6 +10247,37 @@ class Device final return false; } } + // With Resizable BAR, keep the arena where its buffer may still use DEVICE_LOCAL | HOST_VISIBLE memory + // (arena_size.h, device_local_arena_bytes): a driver can leave that type out of a very large buffer. + if (leonida::resizable_bar(state_.memory)) { + const auto device_local = [&](std::uint32_t bits) { + constexpr VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + for (std::uint32_t i = 0; i < state_.memory.memoryTypeCount; ++i) + if ((bits & (1u << i)) && (state_.memory.memoryTypes[i].propertyFlags & wanted) == wanted) return true; ``` The probe checks the type bit and flags here. Allocation also requires `requirements.size` to fit the selected type's heap. I reproduced the difference with a 4070 MiB BAR heap: the 4095 MiB buffer offers only system memory, and the 4080 and 4064 MiB buffers both offer the BAR type. The PR stops at 4080 MiB and marks the result BAR-capped, but `memory_type_order` drops the BAR type, so allocation can only try system memory. At 4064 MiB the same allocation policy selects BAR. Could this predicate reuse the allocation eligibility check with the full `VkMemoryRequirements` so the search continues when the heap is too small?
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-25 11:03 UTC · original · approved these changes

### Clean-Room & Technical Verification

  • Mergeability: 3-way merge against upstream main (bcaa13dad) is 100% clean with zero merge conflicts.
  • Clean-Room Compliance: 100% clean-room. Vulkan memory sizing algorithms and unit test cases only; zero retail binaries, game data, or keys (check_no_game_data.py clean).
  • Technical Rigor & Hardware Reality:
    • Addresses driver memory type filtering on high-end GPUs (e.g. RTX 5070 Ti) where buffers exceeding ~4,090 MiB exclude DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT from memoryTypeBits, pushing the entire 4 GiB arena into system RAM and exhausting host commit.
    • Implements device_local_arena_bytes() in runtime/src/arena_size.h, iteratively probing sizes downward in 16 MiB steps (up to an 1/8 reduction floor) to locate the largest size eligible for ReBAR VRAM placement.
    • Correctly verifies heap capacity and allocation limits via device_local_arena_fits().
    • Comprehensive unit test coverage added in runtime/test/arena_size_test.cpp.
    • Thoroughly documented in docs/d3d12_vulkan.md.

LGTM! Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-25 11:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1732#pullrequestreview-6056395944) · **approved these changes** ### Clean-Room & Technical Verification - **Mergeability**: 3-way merge against upstream `main` (`bcaa13dad`) is 100% clean with zero merge conflicts. - **Clean-Room Compliance**: 100% clean-room. Vulkan memory sizing algorithms and unit test cases only; zero retail binaries, game data, or keys (`check_no_game_data.py` clean). - **Technical Rigor & Hardware Reality**: - Addresses driver memory type filtering on high-end GPUs (e.g. RTX 5070 Ti) where buffers exceeding ~4,090 MiB exclude `DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT` from `memoryTypeBits`, pushing the entire 4 GiB arena into system RAM and exhausting host commit. - Implements `device_local_arena_bytes()` in `runtime/src/arena_size.h`, iteratively probing sizes downward in 16 MiB steps (up to an 1/8 reduction floor) to locate the largest size eligible for ReBAR VRAM placement. - Correctly verifies heap capacity and allocation limits via `device_local_arena_fits()`. - Comprehensive unit test coverage added in `runtime/test/arena_size_test.cpp`. - Thoroughly documented in `docs/d3d12_vulkan.md`. LGTM! Approved.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 14:38 UTC · original

Merged. Thank you, @⁠kawaii-shakal, and thanks to @⁠lixonik for the heap-size point and to @⁠quartz-heron-58 for the RTX 4080 boot.

  • arena_size passes, and its 4070 MiB heap case fails if the heap-size check is removed, so the second commit covers lixonik's point.
  • Without Resizable BAR nothing changes. On an RX 7900 XTX (RADV 26.3.1, ReBAR on) an arena-shaped buffer keeps the DEVICE_LOCAL | HOST_VISIBLE type up to 4,095 MiB, so nothing changes there either.
  • Windows suite under Wine (llvmpipe and an RDNA2 iGPU), together with #1827 and #1785 on current main: nothing goes from pass to fail.
  • Two notes for next time: the description still gives the 4 GiB host commit as the saving, while quartz-heron-58's run saw private bytes go up (the docs already say so); and the second commit says Codex, so the AI note should name it too.
  • Checked on main 24a29f8b4 + a607b779c: it merges cleanly; the semantic suite passes (489 files, 6,288 tests, 0 files failed); the 14 docs and writer tests pass; check_no_game_data.py is clean on the 4 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 14:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1732#issuecomment-7165603309) Merged. Thank you, @⁠kawaii-shakal, and thanks to @⁠lixonik for the heap-size point and to @⁠quartz-heron-58 for the RTX 4080 boot. - `arena_size` passes, and its 4070 MiB heap case fails if the heap-size check is removed, so the second commit covers lixonik's point. - Without Resizable BAR nothing changes. On an RX 7900 XTX (RADV 26.3.1, ReBAR on) an arena-shaped buffer keeps the `DEVICE_LOCAL | HOST_VISIBLE` type up to 4,095 MiB, so nothing changes there either. - Windows suite under Wine (llvmpipe and an RDNA2 iGPU), together with #1827 and #1785 on current `main`: nothing goes from pass to fail. - Two notes for next time: the description still gives the 4 GiB host commit as the saving, while quartz-heron-58's run saw private bytes go up (the docs already say so); and the second commit says Codex, so the AI note should name it too. - **Checked on `main` `24a29f8b4` + `a607b779c`:** it merges cleanly; the semantic suite passes (489 files, 6,288 tests, 0 files failed); the 14 docs and writer tests pass; `check_no_game_data.py` is clean on the 4 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.