mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 21:45:54 +01:00
[PR #1413] [MERGED] d3d12_vulkan: textures share blocks of device memory (VK-IMAGE-MEMORY-POOL) #1444
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#1444
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
📋 Pull Request Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/1413
Author: @TheGeogeo
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142
Base:
main← Head:perf/image-memory-pool📝 Commits (1)
2b72fb5d3d12_vulkan: textures share blocks of device memory (VK-IMAGE-MEMORY-POOL)📊 Changes
9 files changed (+765 additions, -6 deletions)
View changed files
📝
docs/d3d12_vulkan.md(+7 -6)📝
runtime/CMakeLists.txt(+13 -0)📝
runtime/shims/d3d12_com.h(+9 -0)📝
runtime/shims/d3d12_vulkan_device.cpp(+58 -0)➕
runtime/src/image_memory.h(+231 -0)📝
runtime/src/vulkan_backend.h(+6 -0)📝
runtime/src/vulkan_memory.h(+6 -0)➕
runtime/test/d3d12_texture_memory_test.cpp(+247 -0)➕
runtime/test/image_memory_test.cpp(+188 -0)📄 Description
Claimed in the coordination issue as: VK-IMAGE-MEMORY-POOL — https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7146176960
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)🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.