Skip to content

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

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
kawaii-shakal:arena-rebar-cap
Nov 25, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
kawaii-shakal:arena-rebar-cap

Conversation

@kawaii-shakal

@kawaii-shakal kawaii-shakal commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claim: RT-ARENA-REBAR-FIT, #1608 (comment) (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

… from a very large buffer (RT-ARENA-REBAR-FIT)

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 ProjectLeonida#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 <noreply@anthropic.com>
@quartz-heron-58

Copy link
Copy Markdown
Contributor

The retail boot you list as not done, on a second card: 96bbe55 against its base 3de22a1, 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)

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread runtime/shims/d3d12_vulkan_device.cpp Outdated
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;

@lixonik lixonik Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@aslan2142
aslan2142 merged commit c3b58ef into ProjectLeonida:main Nov 25, 2026
2 of 5 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants