Skip to content

d3d12: a heap reserves its arena range when a buffer first needs it, not at CreateHeap - #6257

Open
AwkGrep256 wants to merge 2 commits into
ItsNotRexed:mainfrom
AwkGrep256:heap-arena-lazy
Open

AwkGrep256 wants to merge 2 commits into
ItsNotRexed:mainfrom
AwkGrep256:heap-arena-lazy

Conversation

@AwkGrep256

@AwkGrep256 AwkGrep256 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as a new D3D-layer item: #5781 (comment)

What and why

Heap::Heap reserved the heap's whole SizeInBytes in the GPU address arena for every heap, and CreateHeap failed with E_OUTOFMEMORY when it didn't fit. Only placed buffers (VA = heap place + offset) and buffer tiles that share the arena use that range. Placed textures never do: on main they get their own image memory and a null VA (CreatePlacedResource), and a D3D12 heap has no GPU VA of its own. So a texture-only heap cost its size in the arena for nothing. With the default 256 MiB arena (capped at 2 GiB on AMD's Windows driver), a title that sub-allocates textures from large heaps would run the arena out at CreateHeap.

The change (runtime/shims/d3d12_vulkan_device.cpp):

  • Heap::reserve_arena() reserves the range on first need, under the heap's backing lock; the constructor no longer reserves.
  • CreatePlacedResource reserves it for a buffer and fails with E_OUTOFMEMORY (and the existing "arena full" log line) when the arena can't take the heap. Textures don't touch it.
  • Heap::tiles() reserves it for buffer tiles that share the arena; when it can't, the mapping gets no backing, as when the heap can't back tiles today.
  • CreateHeap keeps every other check (arena present, desc refusals) but no longer fails on arena space.

Behaviour change: a buffer heap that doesn't fit the arena now fails at its first CreatePlacedResource (E_OUTOFMEMORY, with the same "arena full" log line) instead of at CreateHeap; a title that only checks CreateHeap gets a null resource there instead of the early failure. A placed buffer refused for another reason (the heap's texture tiles have their own sparse backing, E_NOTIMPL) reserves nothing: Heap::place_buffer() checks, reserves and marks the heap under one lock (review by @Razdva122, second commit).

No address the title sees changes: a buffer's VA is still the heap's place plus its offset, and every buffer in a heap shares the one range. docs/d3d12_vulkan.md: the heap row and the CreatePlacedResource note.

Interplay with #770 (D3D-88, open): there a default heap's placed textures share one heap-sized VkDeviceMemory of their own, still not the arena range, so this change applies the same way; the two touch neighbouring lines in Heap and will need a small rebase whichever lands second.

Tested

Windows cross-build (clang-cl + xwin), run under Wine 11 on CachyOS, RADV/Mesa 26.3.1, with VK_LAYER_KHRONOS_validation on the host Vulkan:

GPU d3d12_heap_arena_test (LEONIDA_ARENA_MB=64)
AMD Radeon RX 9070 XT (RADV GFX1201) PASS (8/8 at 4c7150a), no validation errors
AMD Radeon Graphics (Ryzen 7 9800X3D iGPU, RADV gfx1036) PASS (8/8 at 4c7150a), no validation errors

New runtime/test/d3d12_heap_arena_test.cpp (CTest d3d12_heap_arena, LEONIDA_ARENA_MB=64): four 128 MiB texture-only heaps each take a placed texture; a 128 MiB heap for buffers and textures takes a texture and refuses a buffer with E_OUTOFMEMORY; two buffers 64 KiB apart in a 1 MiB heap get VAs 64 KiB apart, and the same again after the first heap is released; a 48 MiB mixed heap with a texture tile mapped refuses a placed buffer, and a 32 MiB buffer heap then still fits (skipped without sparse texture tiles). Negative controls: with main's d3d12_vulkan_device.cpp 5 of the first 7 checks fail (CreateHeap refuses the 128 MiB heaps); with the first commit (63c5149) the refused-buffer check fails.

Also tested by @Hiiva (RTX 4080, Windows 11) and @Kam471 (RTX 5070 Ti, Windows 11): pass.

Other heap tests on the RX 9070 XT, before and after: d3d12_placed_dimensions, d3d12_buffer_tiles, d3d12_texture_tiles, d3d12_sparse_queue_family, d3d12_method_variants, d3d12_vulkan_device_test, d3d12_compute pass. d3d12_heap_properties (44 mismatches), d3d12_residency (14) and d3d12_indirect_rt (RayQueryKHR ARRAY hit=0) fail identically on main under Wine (host comparisons against vkd3d, and the known ray-query case), so they're unrelated.

Not tested by me: native Windows, NVIDIA, Intel (see the reviews above for Windows on NVIDIA).

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…not at CreateHeap

Textures placed in a heap never use its GPU address arena range (they get
their own image memory) and a D3D12 heap has no VA, so a texture-only heap
reserved its whole size in the arena for nothing: with the default 256 MiB
arena (2 GiB cap on AMD's Windows driver), large texture heaps ran the arena
out at CreateHeap. The range is now reserved by the first placed buffer, or
the first buffer tiles that share the arena; that CreatePlacedResource fails
with E_OUTOFMEMORY when it doesn't fit. New d3d12_heap_arena_test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Hiiva Hiiva 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.

Independent Windows review at 63c514929c8d6c0c9d870d9d6e32e2f4ec5e4460: the unchanged d3d12_heap_arena CTest passes all seven checks on an RTX 4080. No actionable finding in the first-use reservation change.

Environment: Windows 11 Pro build 26200, NVIDIA GeForce RTX 4080 driver 610.62 (32.0.16.1062), MSVC 19.44.35228, Windows SDK 10.0.26100, Vulkan SDK 1.4.363, RelWithDebInfo. Both runs used LEONIDA_ARENA_MB=64, allocation logging, and requested core/synchronization validation with output captured. The runtime logs identify the RTX 4080 and a 64.0 MiB arena. Neither run emitted VUID, synchronization-hazard, or validation-error diagnostics. This standalone fixture has no test-owned validation-error counter; the diagnostic result is from captured layer output.

  • Head: all seven checks pass. Four 128 MiB texture-only heaps each accept a placed texture. A mixed 128 MiB heap also accepts a texture, then returns the expected E_OUTOFMEMORY when a buffer first requires its full arena range.
  • Unchanged main 561b65a832218fbb9e6d2effd392a8eef093a3f8: the exact same test source, linked to the clean-main library, fails five checks because the large heaps are refused at CreateHeap. The two small-buffer checks still pass.
  • Address/release controls: both revisions give the two small buffers VAs 0x10000 and 0x20000, then reuse 0x10000 for the second heap after the first is released. The head's allocation log contains only the two 1 MiB buffer-heap reservations; the five texture-bearing heaps reserve no arena range. Peak live heap space is 1.0 MiB.

Source review: both first-use entry points hold backing_mutex_; the tiles path calls the already-locked helper, avoiding recursive locking. A successful reservation is retained and reused, and its shared allocation keeps the existing arena-release deleter. The only placed-resource offset access follows a successful reservation; texture creation still bypasses that path. I found no reverse acquisition from the arena allocator into the heap lock.

The head and clean-main production trees are unchanged. The control builds the stock test directly from the PR checkout and links the previously built, hash-verified clean-main library; no fixture, production, or oracle edits. This is a bounded allocation/VA review, with no concurrent stress run, buffer-tiles host comparison, #770 integration result, or retail-game claim. AI-assisted.

@Razdva122 Razdva122 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.

A rejected placed-buffer call can retain arena space for the rest of the heap's lifetime. At runtime/shims/d3d12_vulkan_device.cpp:6562, create_placed reserves the entire heap before checking use_as_placed_resource(true). A mixed heap already backing sparse image tiles has tile_allocation_ populated. The latter check therefore returns E_NOTIMPL, but the new arena reservation stays in allocation_ although no buffer was created.

I compiled the head’s reservation methods, compatibility check and call ordering into a CPU probe with a counted arena. With 64 MiB available and a 48 MiB mixed heap in the separate-image-tiles state, the refused placement left 16 MiB available. A subsequent valid 32 MiB buffer heap then failed. The GPU was not emulated in this probe. The existing fixture tests a successful texture placement followed by a buffer request on an oversized heap, which takes a different path.

Please check for incompatible sparse backing, reserve, and mark the heap as having a placed buffer under one backing_mutex_ acquisition. That also prevents a tile mapping from installing incompatible backing between the two calls. A rejected placement should leave the arena available to another heap.

AI-assisted review of 63c514929.

Razdva122 on ItsNotRexed#6257: the arena range was reserved before the check for a
heap whose texture tiles have their own sparse backing, so a refused
placement kept the range for the heap's lifetime. Heap::place_buffer now
checks, reserves and marks the heap under one backing_mutex_ acquisition;
place_texture keeps the texture-only check. The test adds that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AwkGrep256

Copy link
Copy Markdown
Contributor Author

@Razdva122 thanks, you're right: the range was reserved before the separate-tiles check, so a refused placement kept it. Fixed in 4c7150a: Heap::place_buffer() checks for separate sparse-tile backing, reserves the range and marks the heap as holding a buffer under one backing_mutex_ acquisition, so a refused placement reserves nothing and no tile mapping can slip in between. place_texture() keeps the texture-only check (use_as_placed_resource had only that caller left).

The test now covers your case: a 48 MiB mixed heap with a texture tile mapped (separate sparse backing) refuses a placed buffer with E_NOTIMPL, and a 32 MiB buffer heap then still fits the 64 MiB arena. It's skipped where reserved textures aren't sparse images.

Tested 4c7150a under Wine with VK_LAYER_KHRONOS_validation: d3d12_heap_arena_test passes all 8 checks on an RX 9070 XT (RADV GFX1201) and a gfx1036 iGPU, no validation errors. Negative control: the same test against 63c5149 fails the new check (1 of 8). d3d12_buffer_tiles, d3d12_texture_tiles, d3d12_placed_dimensions and d3d12_method_variants still pass with no validation errors.

@Hiiva thanks for the RTX 4080 run and the allocation-log check.

AI-assisted.

@Kam471

Kam471 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Head 4c7150ac31 (2 commits; the second is "a refused placed buffer reserves no arena space"), 4 files, +234/−33. git diff --check clean.

Tested Windows 11, RTX 5070 Ti (32-wide), MSVC 19.44, Vulkan 1.4.363, Ninja RelWithDebInfo with -DBUILD_TESTING=ON -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON:

  • d3d12_heap_arena_test with LEONIDA_ARENA_MB=64: PASS, 8/8 checks. Arena report: peak end 32.1 of 64.0 MiB, heaps 32.0, 0 refused. Four 128 MiB texture-only heaps are created; the 128 MiB buffer heap's placed buffer returns E_OUTOFMEMORY.
  • d3d12_placed_dimensions_test, d3d12_buffer_tiles_test, d3d12_texture_tiles_test, d3d12_method_variants_test: exit 0.
  • No Python tests touched.

Verdict: sound. Deferred reservation frees texture-only heaps and changes no address a placed buffer sees. One behaviour change worth stating in the PR: a buffer heap that no longer fits fails at CreatePlacedResource (E_OUTOFMEMORY) instead of CreateHeap; a title that only checks CreateHeap gets a null resource instead of an early failure.

Stack: applies cleanly to current main (4e42451) but not to exe-day-merge — that tree has an extra !arena.mapped refusal in CreateHeap, so one hunk conflicts (a ~10-line manual port). #770 is still open and edits the same Heap lines.

@AwkGrep256

Copy link
Copy Markdown
Contributor Author

@Kam471 thanks for the 5070 Ti run. I added the behaviour change (a heap that no longer fits fails at its first placed buffer, not at CreateHeap) to the description. For exe-day-merge, the extra !arena.mapped refusal in CreateHeap should stay as it is; only the heap->reserved() check after new Heap goes away there. Happy to push a port if a branch for it is wanted.

AI-assisted.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 4c7150a on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass

  • The head moved from 63c5149 to 4c7150a while I was testing; everything below is on 4c7150a.
  • ctest d3d12_vulkan_device, d3d12_buffer_tiles, d3d12_texture_tiles, d3d12_sparse_queue_family, d3d12_placed_dimensions, d3d12_heap_arena, d3d12_method_variants, d3d12_compute, validation 0 and 1: 7 of 8 pass in both runs. d3d12_texture_tiles fails ("row-major alias tile 0 at (0,0): got 0x20000cc, expected 0xcc020000", test line 240). It fails the same way on the merge base 561b65a, so this PR does not cause it.
  • d3d12_heap_arena_test.exe run by hand without LEONIDA_ARENA_MB=64 prints "FAIL (1)": the test needs that variable (ctest sets it). With the variable: PASS, exit 0, including "a buffer refused by a heap with texture tiles reserves nothing: a 32 MiB buffer heap fits".
  • Negative control: runtime/shims/d3d12_vulkan_device.cpp at 561b65a, tests kept. d3d12_heap_arena fails at both validation settings, 6 steps FAIL ("four 128 MiB texture-only heaps with a 64 MiB arena", "a placed texture in each", "a 128 MiB heap for buffers and textures", "a texture placed in it", "a buffer placed in it: E_OUTOFMEMORY, the arena can't take the heap", "a buffer refused by a heap with texture tiles reserves nothing: a 32 MiB buffer heap fits"). d3d12_buffer_tiles still passes on the base.
  • Not done: other GPUs; a title workload with real heap churn.

AI-assisted (Claude Code).

@trick-ye

Copy link
Copy Markdown

Tested 4c7150ac on AMD Radeon RX 7800 XT (RDNA3), AMD 26.11.2 (Windows driver 32.0.32029.2010), Windows 11 26200: the new d3d12_heap_arena passes; no d3d12_ test changes result against main.

  • ctest -R d3d12_ -j 1: 94 tests, 86 pass, 5 skipped, 3 fail. The three failures are the ones main 561b65a8 has on this card: d3d12_texture_tiles (D3D-597), d3d12_indirect_dispatch_count (D3D-594) and d3d12_pipeline_library_host (D3D-600). None of the 93 d3d12_ tests in my main run changed result.
  • The new d3d12_heap_arena passes on native Windows (run through ctest, so with its LEONIDA_ARENA_MB=64). The tests nearest the change also pass: d3d12_placed_dimensions, d3d12_buffer_tiles, d3d12_existing_heap, d3d12_heap_properties, d3d12_residency.
  • Read the diff; nothing to flag.
  • MSVC 14.42 (VS 2022), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, llvm-mc 23.1.6, RelWithDebInfo, LEONIDA_VK_DEVICE=7800; all runtime targets built (GameInput shim not built: no SDL3). Synthetic tests only, no game files.

AI-assisted (Claude Code).

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.

6 participants