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

Manually merged
github merged 1 commit from gh-pr/1413/head into gh-pr/1413/base 2026-11-26 09:50:39 +00:00
Owner

@⁠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 UTC

Claimed 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.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:

  • This GPU reports no limit for maxMemoryAllocationCount (4294967295), so no allocation-count failure was reproduced.
  • This change does not make a submission faster. With 10000 live textures an empty submission costs 1277 us on main and 111-198 us with #1284 merged locally: that per-texture cost is the tracker copy #1284 removes, not the number of allocations.
  • The zero-fill of a reused range before a shader reads it (note_shader_image) has no test; the test covers the copy path.
  • Not run: the Vulkan validation layers, other GPUs, and the retail title. VRAM use was not compared.

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)
**@⁠TheGeogeo** on GitHub · 2026-11-24 23:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1413) `TheGeogeo:perf/image-memory-pool` → `main` · +765 −6 · 9 files · 1 commits · merged 2026-11-25 00:03 UTC Claimed in the coordination issue as: VK-IMAGE-MEMORY-POOL — https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 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: - This GPU reports no limit for `maxMemoryAllocationCount` (4294967295), so no allocation-count failure was reproduced. - This change does not make a submission faster. With 10000 live textures an empty submission costs 1277 us on `main` and 111-198 us with #1284 merged locally: that per-texture cost is the tracker copy #1284 removes, not the number of allocations. - The zero-fill of a reused range before a shader reads it (`note_shader_image`) has no test; the test covers the copy path. - Not run: the Vulkan validation layers, other GPUs, and the retail title. VRAM use was not compared. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.24, Windows 11. AI-assisted: yes (Claude Code). ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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)
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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:36 +00:00
github manually merged commit 2b72fb53d1 into gh-pr/1413/base 2026-11-26 09:50:39 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 00:03 UTC · original

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

  • Claim: valid. VK-IMAGE-MEMORY-POOL was 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.
  • The allocator holds up: best fit with neighbour merging and alignment, blocks per memory type, one spare empty block kept. A texture above an eighth of a block, a render target, a depth buffer and a host-transfer texture keep their own allocation. Ranges come back through the deferred deletion queue, which is drained before the pool is destroyed, and a reused range is zero-filled.
  • Linux portable ctest on llvmpipe: 75 of 75 on main, 76 of 76 with this PR; image_memory is the new one.
  • Windows suite under Wine (clang-cl; llvmpipe and an RDNA2 iGPU, RADV), main 364ab4eab + this PR against a baseline of main: no test went from pass to fail on either device, and no texture, copy, render-target, present or readback test changed. image_memory and d3d12_texture_memory pass on both.
  • On today's main 3943b04d5 + 2b72fb53d: it merges cleanly; the 14 docs and writer tests pass; check_no_game_data.py is clean on the 9 changed files. Correction: between that suite run and this merge main also took in #980 and #528, which change runtime/src/vulkan_backend.h (a file this PR touches) and the zero-fill path in runtime/src/vulkan_replay.h that 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 merged main now and will post the result here.
  • Not measured here: a Windows driver that really reports maxMemoryAllocationCount 4096, the timings, the overhead on a 4 GB card, and the retail title.
  • CI had not finished on this head.

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** on GitHub · 2026-11-25 00:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1413#issuecomment-7148081925) Merged. Thanks: the allocator is small and well tested, and nothing regressed locally. - **Claim:** valid. `VK-IMAGE-MEMORY-POOL` was 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. - **The allocator holds up:** best fit with neighbour merging and alignment, blocks per memory type, one spare empty block kept. A texture above an eighth of a block, a render target, a depth buffer and a host-transfer texture keep their own allocation. Ranges come back through the deferred deletion queue, which is drained before the pool is destroyed, and a reused range is zero-filled. - **Linux portable `ctest` on llvmpipe:** 75 of 75 on `main`, 76 of 76 with this PR; `image_memory` is the new one. - **Windows suite under Wine** (clang-cl; llvmpipe and an RDNA2 iGPU, RADV), `main` `364ab4eab` + this PR against a baseline of `main`: no test went from pass to fail on either device, and no texture, copy, render-target, present or readback test changed. `image_memory` and `d3d12_texture_memory` pass on both. - **On today's `main` `3943b04d5` + `2b72fb53d`:** it merges cleanly; the 14 docs and writer tests pass; `check_no_game_data.py` is clean on the 9 changed files. **Correction:** between that suite run and this merge `main` also took in #980 and #528, which change `runtime/src/vulkan_backend.h` (a file this PR touches) and the zero-fill path in `runtime/src/vulkan_replay.h` that 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 merged `main` now and will post the result here. - **Not measured here:** a Windows driver that really reports `maxMemoryAllocationCount` 4096, the timings, the overhead on a 4 GB card, and the retail title. - **CI had not finished on this head.** **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).
Author
Owner

@⁠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 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).

**@⁠aslan2142** on GitHub · 2026-11-25 00:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1413#issuecomment-7148297433) 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.