Skip to content

d3d12_vulkan: textures share blocks of device memory (VK-IMAGE-MEMORY-POOL) - #1413

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
TheGeogeo:perf/image-memory-pool
Nov 25, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
TheGeogeo:perf/image-memory-pool

Conversation

@TheGeogeo

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: VK-IMAGE-MEMORY-POOL — #14 (comment)

Depends on: #443, #1100, #1291. It builds on none of them, but each changes the same lines of runtime/shims/d3d12_vulkan_device.cpp and conflicts with this one: #1100 and #443 the texture allocation in Resource::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/vkFreeMemory are taken from the function table it wraps. On main 79d044e5, #1100 and #443 already conflict there on their own (the host-transfer retry of create_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 vkAllocateMemory and freed it with its own vkFreeMemory. 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.md section 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, and ImageMemoryPool, 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 in create_texture makes again without that usage takes the shared path. The block size is 64 MiB, or a sixteenth of a smaller heap (image_block_bytes).
  • A range goes back to its block through the deferred deletion queue (Kind::image_memory), after the last submission that could use the image has completed.
  • A texture in a range an earlier texture used reads as zero, as a committed resource does: the first command that touches it zero-fills it (as before), and note_shader_image covers a shader that reads it before any command names it. D3D12_HEAP_FLAG_CREATE_NOT_ZEROED skips that.
  • rt::d3d12::image_memory_stats (for tests), and docs/d3d12_vulkan.md updated where it said every texture has its own VkDeviceMemory (the memory model table, placed textures, ID3D12Heap and CreateCommittedResource).

Tested

Windows 11 (10.0.26300), MSVC 14.52, NVIDIA GeForce RTX 3080, driver 621.24, head 2b72fb53 on main 79d044e5 (it also merges cleanly with main 507029d1).

  • New 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 of image_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.
  • New 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.
  • Full ctest, RelWithDebInfo, same machine: with this change 282 of 282 pass, on unmodified main 79d044e5 280 of 280 pass; the difference is the two new tests. The same 15 do not run on both: 13 skipped, and gameinput and gameinput_script disabled. d3d12_vulkan_device and d3d12_subresource_copy, which cover the host-transfer retry of create_texture, pass.
  • tests/test_ctest_registration.py, test_docs_links.py, test_text_write_line_endings.py, test_runtime_paths.py and test_cmake_vulkan_missing.py pass; git diff --check and python -I scripts/check_no_game_data.py on the 9 files are clean.

Measured with a small timing program (not in this pull request), three runs each, on main 51a6074d before 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 in ImageMemoryPool::release):

Texture Create, before Create, after Release, before Release, after
64x64 RGBA8 125-131 us 3.4-3.5 us 244-254 us 1.7-2.0 us
256x256 RGBA8 125-129 us 3.9-6.2 us 239-263 us 4.1-5.4 us

Limits:

GPUs tested: NVIDIA GeForce RTX 3080, driver 621.24, Windows 11.

AI-assisted: yes (Claude Code).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

…-POOL)

Every committed and placed texture made its own vkAllocateMemory and
released it with its own vkFreeMemory, about 100 us and 200 us on an
RTX 3080. Small device-local textures now take ranges of 64 MiB blocks
(runtime/src/image_memory.h). Render targets, depth buffers, CPU-visible
textures and textures over an eighth of a block keep their own
allocation. A range goes back to its block through the deferred
deletion queue, and a texture in a reused range is zeroed before a
shader reads it.

Tests: image_memory (CPU) and d3d12_texture_memory (GPU).
@aslan2142
aslan2142 merged commit 1154976 into ProjectLeonida:main Nov 25, 2026
4 of 5 checks passed
@aslan2142

aslan2142 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Merged. Thanks: the allocator is small and well tested, and nothing regressed locally.

Follow-ups, please:

  • An environment switch that sends every texture back to its own allocation. This changes how every sampled texture gets its memory, and main is 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.md says a CPU-visible texture keeps its own allocation, but only host-transfer textures do; one on the staging path takes a range.
  • The pool's mutex is held across vkAllocateMemory while 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

Copy link
Copy Markdown
Collaborator

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 of main before this merge: no test went from pass to fail on either device. image_memory and d3d12_texture_memory pass 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).

@TheGeogeo
TheGeogeo deleted the perf/image-memory-pool branch November 25, 2026 00:22
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.

2 participants