[PR #1413] [MERGED] d3d12_vulkan: textures share blocks of device memory (VK-IMAGE-MEMORY-POOL) #1444

Closed
opened 2026-11-26 20:21:16 +01:00 by 4ld0 · 0 comments
Owner

📋 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)

  • 2b72fb5 d3d12_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.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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1413 **Author:** [@TheGeogeo](https://github.com/TheGeogeo) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `perf/image-memory-pool` --- ### 📝 Commits (1) - [`2b72fb5`](https://github.com/ubiquitarianism/leonida/commit/2b72fb53d15c91d3cf5998f863c38854396ce03b) d3d12_vulkan: textures share blocks of device memory (VK-IMAGE-MEMORY-POOL) ### 📊 Changes **9 files changed** (+765 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 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.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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:21:16 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1444
No description provided.