mem probe: a breakdown line of the runtime's Vulkan memory by consumer (RT-MEMORY-BREAKDOWN-AND-SPARSE-ARENA) #2401

Manually merged
github merged 1 commit from gh-pr/2401/head into gh-pr/2401/base 2026-11-26 19:51:43 +00:00
Owner

@⁠aslan2142 on GitHub · 2026-11-26 19:39 UTC · original
aslan2142:memory-breakdown-line → main · +376 −7 · 7 files · 1 commits · merged 2026-11-26 19:43 UTC

Adds one diagnostic line to the memory probe: our Vulkan allocations broken down by consumer, so the VRAM the game
uses in the world can be attributed before anything is changed. No behaviour changes.

Claimed in the coordination issue as: RT-MEMORY-BREAKDOWN-AND-SPARSE-ARENA

What was measured

  • The HUD's VRAM figure matches the sum of our own vkAllocateMemory bytes in the device-local heap to within
    about 0.1-0.3 GB, so what needs explaining is in our allocations.
  • In a late world run about 7.5 GB of device-local-only memory (textures, render targets, image blocks) and up
    to 1.2 GB of the Resizable BAR type cannot be split by today's log lines.
  • The GPU address arena (LEONIDA_ARENA_MB=4095) had about 0.76 GB in use (peak about 1 GB); the rest of its
    4 GiB was allocated and never touched.

What changes

One commit:

  • [rt] mem-probe breakdown t=<s>: on every LEONIDA_MEM_PROBE tick. It prints:

    • the allocated total;
    • the arena, with its reserved, bound and in-use MiB (in use split into buffers, descriptor heaps, shader code
      and heaps);
    • geometry copies;
    • title windows and shadows;
    • upload heaps;
    • staging;
    • texture blocks;
    • sampled textures;
    • render targets and depth;
    • other.

    The parts add up to the total. Imported memory is shown apart. Bindless tables, shader modules, pipelines and
    swapchain images are shown beside the total, not in it.

  • src/memory_breakdown.h (new, standard library only): the consumers, an Owner scope that names the
    allocations a thread makes while it lives, the tally and the text.

  • src/mem_probe.h: the vkAllocateMemory wrapper records the thread's owner. vkCreateImage,
    vkDestroyImage and vkBindImageMemory are wrapped too, so unnamed memory that a dedicated image is bound to
    counts as a render target/depth buffer or a sampled texture.

  • Owner scopes at the allocation sites of the arena, geometry copies, title windows, slot-133 shadow buffers,
    upload/readback heaps, staging and copy scratch, and the texture blocks.

  • runtime/README.md: a LEONIDA_MEM_PROBE row that names the three probe lines.

The second half of the item, a sparse arena (memory bound only where allocations are), is not in this pull
request. Step 0 found what blocks it (below).

Device support

The breakdown line works on any device. Findings for the sparse arena, from a test program (Mesa 26.3.1, natively
and through Wine 11.22):

RX 7900 XTX (RADV) Radeon iGPU 1002:164e (RADV) llvmpipe
sparseBinding / sparseResidencyBuffer yes / yes yes / yes yes / yes
residencyNonResidentStrict yes yes no
sparseAddressSpaceSize 128 TiB 128 TiB 2 GiB (smaller than a 4095 MiB arena)
sparse buffer with every arena usage and a device address created, 64 KiB pages, BAR type allowed same created
bind, GPU read/write through the device address, unbind (reads 0), rebind pass pass pass
VK_EXT_map_memory_placed natively / under Wine yes / no yes / no no / no

What blocks the sparse arena is the CPU side, not the GPU. Today the arena has one contiguous CPU mapping
(arena.mapped + offset, about 50 uses). A sparse buffer has no mapping of its own: each backing block is mapped
by itself. Only VK_EXT_map_memory_placed can place those mappings into one contiguous range, and Wine's
winevulkan does not offer it to Windows programs. The design has to change first. Either the arena's CPU
contract becomes one mapping per backing block, with no allocation crossing a block and the whole-arena users
reworked, or Wine starts offering placed mappings.

Tests

  • memory_breakdown_test (CPU, new): the tally, owner scopes, image kinds and the text. It does not build on
    main.
  • Linux runtime build and ctest: 114 of 114 pass on llvmpipe and on the integrated GPU (RADV, Mesa 26.3.1).
  • Windows suite under Wine (clang-cl cross-build), compared test by test with main 2d43417d4:
    • llvmpipe: no test went from pass to fail. memory_breakdown is new and passes. d3d12_queue_handover
      failed on main and passed here.
    • iGPU: no test went from pass to fail. memory_breakdown is new and passes. The same 23 tests fail as on
      main (presentation tests under xvfb and other known failures).
  • tests/test_docs_*.py, tests/test_ci_registration.py, tests/test_text_write_line_endings.py and
    scripts/check_no_game_data.py on the changed files pass.

Not tested

  • The line in a game run: the consumer split is only checked on synthetic allocations.
  • No world run from this build. The line changes no behaviour; the next run here will read it at three points and report what the device-local memory consists of.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

**@⁠aslan2142** on GitHub · 2026-11-26 19:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2401) `aslan2142:memory-breakdown-line` → `main` · +376 −7 · 7 files · 1 commits · merged 2026-11-26 19:43 UTC Adds one diagnostic line to the memory probe: our Vulkan allocations broken down by consumer, so the VRAM the game uses in the world can be attributed before anything is changed. No behaviour changes. Claimed in the coordination issue as: [RT-MEMORY-BREAKDOWN-AND-SPARSE-ARENA](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ## What was measured - The HUD's VRAM figure matches the sum of our own `vkAllocateMemory` bytes in the device-local heap to within about 0.1-0.3 GB, so what needs explaining is in our allocations. - In a late world run about 7.5 GB of device-local-only memory (textures, render targets, image blocks) and up to 1.2 GB of the Resizable BAR type cannot be split by today's log lines. - The GPU address arena (`LEONIDA_ARENA_MB=4095`) had about 0.76 GB in use (peak about 1 GB); the rest of its 4 GiB was allocated and never touched. ## What changes One commit: - `[rt] mem-probe breakdown t=<s>:` on every `LEONIDA_MEM_PROBE` tick. It prints: - the allocated total; - the arena, with its reserved, bound and in-use MiB (in use split into buffers, descriptor heaps, shader code and heaps); - geometry copies; - title windows and shadows; - upload heaps; - staging; - texture blocks; - sampled textures; - render targets and depth; - `other`. The parts add up to the total. Imported memory is shown apart. Bindless tables, shader modules, pipelines and swapchain images are shown beside the total, not in it. - `src/memory_breakdown.h` (new, standard library only): the consumers, an `Owner` scope that names the allocations a thread makes while it lives, the tally and the text. - `src/mem_probe.h`: the `vkAllocateMemory` wrapper records the thread's owner. `vkCreateImage`, `vkDestroyImage` and `vkBindImageMemory` are wrapped too, so unnamed memory that a dedicated image is bound to counts as a render target/depth buffer or a sampled texture. - `Owner` scopes at the allocation sites of the arena, geometry copies, title windows, slot-133 shadow buffers, upload/readback heaps, staging and copy scratch, and the texture blocks. - `runtime/README.md`: a `LEONIDA_MEM_PROBE` row that names the three probe lines. The second half of the item, a sparse arena (memory bound only where allocations are), is **not** in this pull request. Step 0 found what blocks it (below). ## Device support The breakdown line works on any device. Findings for the sparse arena, from a test program (Mesa 26.3.1, natively and through Wine 11.22): | | RX 7900 XTX (RADV) | Radeon iGPU 1002:164e (RADV) | llvmpipe | |---|---|---|---| | `sparseBinding` / `sparseResidencyBuffer` | yes / yes | yes / yes | yes / yes | | `residencyNonResidentStrict` | yes | yes | no | | `sparseAddressSpaceSize` | 128 TiB | 128 TiB | 2 GiB (smaller than a 4095 MiB arena) | | sparse buffer with every arena usage and a device address | created, 64 KiB pages, BAR type allowed | same | created | | bind, GPU read/write through the device address, unbind (reads 0), rebind | pass | pass | pass | | `VK_EXT_map_memory_placed` natively / under Wine | yes / **no** | yes / **no** | no / no | What blocks the sparse arena is the CPU side, not the GPU. Today the arena has one contiguous CPU mapping (`arena.mapped + offset`, about 50 uses). A sparse buffer has no mapping of its own: each backing block is mapped by itself. Only `VK_EXT_map_memory_placed` can place those mappings into one contiguous range, and Wine's `winevulkan` does not offer it to Windows programs. The design has to change first. Either the arena's CPU contract becomes one mapping per backing block, with no allocation crossing a block and the whole-arena users reworked, or Wine starts offering placed mappings. ## Tests - `memory_breakdown_test` (CPU, new): the tally, owner scopes, image kinds and the text. It does not build on `main`. - Linux runtime build and `ctest`: 114 of 114 pass on llvmpipe and on the integrated GPU (RADV, Mesa 26.3.1). - Windows suite under Wine (clang-cl cross-build), compared test by test with `main` 2d43417d4: - llvmpipe: no test went from pass to fail. `memory_breakdown` is new and passes. `d3d12_queue_handover` failed on `main` and passed here. - iGPU: no test went from pass to fail. `memory_breakdown` is new and passes. The same 23 tests fail as on `main` (presentation tests under xvfb and other known failures). - `tests/test_docs_*.py`, `tests/test_ci_registration.py`, `tests/test_text_write_line_endings.py` and `scripts/check_no_game_data.py` on the changed files pass. ## Not tested - The line in a game run: the consumer split is only checked on synthetic allocations. - No world run from this build. The line changes no behaviour; the next run here will read it at three points and report what the device-local memory consists of. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
The `mem-probe t=` line sums our vkAllocateMemory bytes per memory type, but not who holds them: in a late world
run 7.5 GB of device-local memory and up to 1.2 GB of the Resizable BAR type could not be attributed. Every tick
now also prints

  [rt] mem-probe breakdown t=<s>: allocated N MiB = arena A (n) [reserved, bound, in use: buffers, descriptor
  heaps, shader code, heaps] + geometry copies + title windows and shadows + upload heaps + staging + texture
  blocks + sampled textures + render targets and depth + other; imported M MiB; not in the total: bindless
  tables, shader modules, pipelines and swapchain images

whose parts add up to the allocated bytes of the t= line. Diagnostic only: nothing changes what is allocated or
where, and with LEONIDA_MEM_PROBE unset nothing is installed.

- src/memory_breakdown.h (new, standard library only): the consumers, an Owner scope (thread-local) that names
  the allocations a thread makes while it lives, the tally and the line's text.
- src/mem_probe.h: the vkAllocateMemory wrapper records the thread's owner; vkCreateImage/vkDestroyImage/
  vkBindImageMemory are wrapped too, so memory nobody named that a dedicated image is bound to counts as a render
  target or depth buffer (attachment usage) or a sampled texture. Named blocks keep their name.
- Owner scopes at the allocation sites: the arena (create_arena), geometry copies (GeometryViews' pool), title
  windows outside the arena and slot-133 shadow buffers with their own memory, upload/readback heap buffers,
  texture staging and copy scratch, and the texture blocks (ImageMemory, TitleImagePool).
- The device adds the arena's reserved, bound and in-use bytes (by kind) and the bindless table bytes, read under
  the arena's and the descriptor locks; BindlessSet allocates through its own function table, so its bytes are
  shown beside the total, not in it.
- runtime/README.md: LEONIDA_MEM_PROBE's row names the three lines.

Test: memory_breakdown_test (CPU): the tally sums to the total with imported memory kept apart, owner scopes nest
and stay on their thread, image usage picks the kind, and the text lists every part. It does not build on main
(the header is new).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:51:42 +00:00
github manually merged commit aaff77b831 into gh-pr/2401/base 2026-11-26 19:51:43 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-26 19:43 UTC · original

Merging. This is a self-merge.

  • Why now: the runtime uses 16 to 19 GB of video memory with the title in play, and about 7.5 GB of it (device-local) cannot be attributed from today's log lines. This line names each consumer, so the next run shows what the memory is. It is the first step of RT-MEMORY-BREAKDOWN-AND-SPARSE-ARENA and changes no behaviour.
  • Tested on main 2d43417d4 + this PR (aaff77b83), Mesa 26.3.1:
    • memory_breakdown_test passes; it is new with the header it tests.
    • Linux runtime ctest: 114/114 on llvmpipe and on the integrated RDNA2 GPU (RADV).
    • Windows suite under Wine on both devices: no test went from pass to fail.
    • The docs, registration and line-ending tests and check_no_game_data.py pass.
  • Not tested: the consumer split on real allocations; that comes with the next run.
  • The sparse arena is not in this PR. RADV can back the arena with a sparse buffer (bind, read and write by address, unbind and rebind pass on both AMD devices), but the arena is reached through one contiguous CPU mapping in about 50 places, a sparse buffer has no mapping of its own, and Wine does not offer VK_EXT_map_memory_placed. It follows as its own PR with one mapping per backing block.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

**@⁠aslan2142** on GitHub · 2026-11-26 19:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2401#issuecomment-7201897840) Merging. This is a self-merge. - **Why now:** the runtime uses 16 to 19 GB of video memory with the title in play, and about 7.5 GB of it (device-local) cannot be attributed from today's log lines. This line names each consumer, so the next run shows what the memory is. It is the first step of RT-MEMORY-BREAKDOWN-AND-SPARSE-ARENA and changes no behaviour. - **Tested on `main` `2d43417d4` + this PR** (`aaff77b83`), Mesa 26.3.1: - `memory_breakdown_test` passes; it is new with the header it tests. - Linux runtime `ctest`: 114/114 on llvmpipe and on the integrated RDNA2 GPU (RADV). - Windows suite under Wine on both devices: no test went from pass to fail. - The docs, registration and line-ending tests and `check_no_game_data.py` pass. - **Not tested:** the consumer split on real allocations; that comes with the next run. - **The sparse arena is not in this PR.** RADV can back the arena with a sparse buffer (bind, read and write by address, unbind and rebind pass on both AMD devices), but the arena is reached through one contiguous CPU mapping in about 50 places, a sparse buffer has no mapping of its own, and Wine does not offer `VK_EXT_map_memory_placed`. It follows as its own PR with one mapping per backing block. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
Author
Owner

@⁠BadgersMC on GitHub · 2026-11-26 20:48 UTC · original

Native Windows verification at merge commit fac8b4bb, RTX 3060 Ti 8 GB (10de:2486), NVIDIA 610.88, MSVC 19.44. The upstream memory_breakdown_test passed 5/5.

I also ran the merged mem_probe.h wrappers in a standalone Vulkan fixture using generated resources. With Vulkan validation enabled, all three runs reported 18 MiB owned (4 sampled, 8 color/depth, 4 explicitly tagged texture-block memory, 2 staging) and 2 MiB of imported host pages separately. Both totals returned to zero after releasing the resources. No validation errors were reported. No game data or title run was involved.

For the sparse-arena design, this driver reports sparse buffer residency, strict nonresident behavior, and a 1 TiB sparse address space. The driver does not advertise VK_EXT_map_memory_placed. Those are capability queries. The fixture exercised allocation accounting and host import. AI-assisted.

**@⁠BadgersMC** on GitHub · 2026-11-26 20:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2401#issuecomment-7203093073) Native Windows verification at merge commit `fac8b4bb`, RTX 3060 Ti 8 GB (10de:2486), NVIDIA 610.88, MSVC 19.44. The upstream `memory_breakdown_test` passed 5/5. I also ran the merged `mem_probe.h` wrappers in a standalone Vulkan fixture using generated resources. With Vulkan validation enabled, all three runs reported **18 MiB owned** (4 sampled, 8 color/depth, 4 explicitly tagged texture-block memory, 2 staging) and **2 MiB of imported host pages** separately. Both totals returned to zero after releasing the resources. No validation errors were reported. No game data or title run was involved. For the sparse-arena design, this driver reports sparse buffer residency, strict nonresident behavior, and a 1 TiB sparse address space. The driver does not advertise `VK_EXT_map_memory_placed`. Those are capability queries. The fixture exercised allocation accounting and host import. AI-assisted.
This discussion has been locked. Commenting is limited to contributors.
No description provided.