d3d12_vulkan: textures share blocks of device memory (VK-IMAGE-MEMORY-POOL) #1413
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!1413
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1413/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@TheGeogeo on GitHub · 2026-11-24 23:42 UTC · original
TheGeogeo:perf/image-memory-pool→main· +765 −6 · 9 files · 1 commits · merged 2026-11-25 00:03 UTCClaimed in the coordination issue as: VK-IMAGE-MEMORY-POOL — #14
Depends on: #443, #1100, #1291. It builds on none of them, but each changes the same lines of
runtime/shims/d3d12_vulkan_device.cppand conflicts with this one: #1100 and #443 the texture allocation inResource::create_texture(#1100 defers the binding of placed attachments and UAV textures, #443 gives the allocation a priority), #1291 the device setup where the blocks'vkAllocateMemory/vkFreeMemoryare taken from the function table it wraps. Onmain79d044e5, #1100 and #443 already conflict there on their own (the host-transfer retry ofcreate_texture); #1291 merges cleanly. This one merges cleanly with #1284 (VK-TRACKER-NO-COPY), which removes the other per-texture cost of a submission.What and why
Every committed and placed texture made its own
vkAllocateMemoryand freed it with its ownvkFreeMemory. On an RTX 3080 creating a 64x64 texture took about 125 us and releasing one about 250 us, of which about 100 us and 200 us were those two calls, and a title that streams textures pays both on every texture. Vulkan also guarantees only 4096 live allocations (maxMemoryAllocationCount).Small textures now take ranges of shared 64 MiB device-local blocks, as
docs/d3d12_vulkan.mdsection 3.3 already described ("or a suballocated device-local image heap"):runtime/src/image_memory.h(new, no Vulkan or Windows dependency):BlockRanges, best fit with merging of free neighbours, andImageMemoryPool, blocks per memory type, thread-safe. One empty block per memory type is kept; after a failed block allocation the driver is not asked again for the next 64 textures or until a range is released.Resource::create_texture: render targets, depth buffers, host-transfer textures (the CPU copies into them directly) and textures larger than an eighth of a block keep their own allocation, as does any texture when device-local memory has no room for another block. A host-transfer texture that the retry increate_texturemakes again without that usage takes the shared path. The block size is 64 MiB, or a sixteenth of a smaller heap (image_block_bytes).Kind::image_memory), after the last submission that could use the image has completed.note_shader_imagecovers a shader that reads it before any command names it.D3D12_HEAP_FLAG_CREATE_NOT_ZEROEDskips that.rt::d3d12::image_memory_stats(for tests), anddocs/d3d12_vulkan.mdupdated where it said every texture has its ownVkDeviceMemory(the memory model table, placed textures,ID3D12HeapandCreateCommittedResource).Tested
Windows 11 (10.0.26300), MSVC 14.52, NVIDIA GeForce RTX 3080, driver 621.24, head
2b72fb53onmain79d044e5(it also merges cleanly withmain507029d1).image_memory(CPU): fixed cases for the block policy, 20 rounds of 4000 random allocations and releases checked for overlap, alignment and bounds, and 4 threads. Passes, and 30 repeated runs passed. Built on its own with MSVC 14.52, eight mutants ofimage_memory.h(no merge with the next free piece, no alignment, never marked reused, no back-off, never free an empty block, free every empty block, one pool for all memory types, overlapping ranges) each make it fail.d3d12_texture_memory(GPU): 256 64x64 textures land in 1 block of 64 MiB, each keeps its own texels after an upload, a released half is reused without a new block, a texture in a reused range reads back as zero and its neighbours keep their texels, a render target and a 4096x4096 texture are not in a block, and at most one spare block remains at the end. Passes.RelWithDebInfo, same machine: with this change 282 of 282 pass, on unmodifiedmain79d044e5280 of 280 pass; the difference is the two new tests. The same 15 do not run on both: 13 skipped, andgameinputandgameinput_scriptdisabled.d3d12_vulkan_deviceandd3d12_subresource_copy, which cover the host-transfer retry ofcreate_texture, pass.tests/test_ctest_registration.py,test_docs_links.py,test_text_write_line_endings.py,test_runtime_paths.pyandtest_cmake_vulkan_missing.pypass;git diff --checkandpython -I scripts/check_no_game_data.pyon the 9 files are clean.Measured with a small timing program (not in this pull request), three runs each, on
main51a6074dbefore and after the change (same machine, before the rebase and before the final edits to the tests, the CMake order and an empty-block count inImageMemoryPool::release):Limits:
maxMemoryAllocationCount(4294967295), so no allocation-count failure was reproduced.mainand 111-198 us with #1284 merged locally: that per-texture cost is the tracker copy #1284 removes, not the number of allocations.note_shader_image) has no test; the test covers the copy path.GPUs tested: NVIDIA GeForce RTX 3080, driver 621.24, Windows 11.
AI-assisted: yes (Claude Code).
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)@aslan2142 on GitHub · 2026-11-25 00:03 UTC · original
Merged. Thanks: the allocator is small and well tested, and nothing regressed locally.
VK-IMAGE-MEMORY-POOLwas posted at 22:23 UTC, before this PR, and is unedited. Nobody had claimed it earlier, and none of #443, #1100 and #1291 is an alternative to it.cteston llvmpipe: 75 of 75 onmain, 76 of 76 with this PR;image_memoryis the new one.main364ab4eab+ this PR against a baseline ofmain: no test went from pass to fail on either device, and no texture, copy, render-target, present or readback test changed.image_memoryandd3d12_texture_memorypass on both.main3943b04d5+2b72fb53d: it merges cleanly; the 14 docs and writer tests pass;check_no_game_data.pyis clean on the 9 changed files. Correction: between that suite run and this mergemainalso took in #980 and #528, which changeruntime/src/vulkan_backend.h(a file this PR touches) and the zero-fill path inruntime/src/vulkan_replay.hthat a reused range relies on. The merge is clean, but that combination was not tested before merging. I am running the full suite on the mergedmainnow and will post the result here.maxMemoryAllocationCount4096, the timings, the overhead on a 4 GB card, and the retail title.Follow-ups, please:
mainis taking in the stack to the retail frame loop right now, so a way to rule the pool out when bisecting a boot regression would help a lot.docs/d3d12_vulkan.mdsays a CPU-visible texture keeps its own allocation, but only host-transfer textures do; one on the staging path takes a range.vkAllocateMemorywhile a new block is allocated.Note for #443 and #1100: after this, their texture allocation should go through
allocate_image, and #443's priority onto the blocks. #428 and #602 touch the same function and are being brought up to date now.AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).
@aslan2142 on GitHub · 2026-11-25 00:14 UTC · original
Follow-up to the correction above: the full Windows suite on the merged
main(1154976d4, with #528 and #980 in it), under Wine on llvmpipe and an RDNA2 iGPU (RADV), against the run ofmainbefore this merge: no test went from pass to fail on either device.image_memoryandd3d12_texture_memorypass on both (261 pass and 15 fail on llvmpipe, 257 and 21 on the iGPU; the failures are the known ones).AI-assisted (Claude Code, Claude Fable 5.2).