vulkan_memory_budget: saturate multi-heap uint64_t sums in sum_vram_budget #2333

Closed
leonida/mandy
narekshaghoyandev wants to merge 0 commits from ubiq-vulkan-memory-budget-saturate into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

narekshaghoyandev commented 2026-11-26 14:00:29 +00:00

Prevents uint64_t wraparound when summing multiple Vulkan memory heaps in leonida::sum_vram_budget() (runtime/src/vulkan_memory_budget.h):

  • Previously, out.local_usage += ..., out.local_budget += ..., and out.shared_usage += ... could wrap around modulo 2^{64} when summing large heap figures, even wrapping local_budget to 0 and falsely marking out.available = false ("VRAM n/a").
  • Use saturating addition (UINT64_MAX cap) for local_usage, local_budget, and shared_usage.
  • Added standalone unit test runtime/test/vulkan_memory_budget_test.cpp.

Verification

  • g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp passed
  • g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp passed
  • Negative control: new test failed on line 44 (saturated.available, exit 1) against unmodified ubiq/main
  • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_memory_budget.h runtime/test/vulkan_memory_budget_test.cpp clean

Claimed in the coordination issue as: RT-VULKAN-MEMORY-BUDGET-SATURATE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7194106445)
AI-assisted: yes (Gemini)

Prevents `uint64_t` wraparound when summing multiple Vulkan memory heaps in `leonida::sum_vram_budget()` (`runtime/src/vulkan_memory_budget.h`): - Previously, `out.local_usage += ...`, `out.local_budget += ...`, and `out.shared_usage += ...` could wrap around modulo $2^{64}$ when summing large heap figures, even wrapping `local_budget` to `0` and falsely marking `out.available = false` (`"VRAM n/a"`). - Use saturating addition (`UINT64_MAX` cap) for `local_usage`, `local_budget`, and `shared_usage`. - Added standalone unit test `runtime/test/vulkan_memory_budget_test.cpp`. ### Verification - `g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp` passed - `g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp` passed - Negative control: new test failed on line 44 (`saturated.available`, exit 1) against unmodified `ubiq/main` - `python3 -I scripts/check_no_game_data.py runtime/src/vulkan_memory_budget.h runtime/test/vulkan_memory_budget_test.cpp` clean Claimed in the coordination issue as: RT-VULKAN-MEMORY-BUDGET-SATURATE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7194106445) AI-assisted: yes (Gemini)
lixonik reviewed 2026-11-26 15:40:34 +00:00
lixonik left a comment
No description provided.
Ran `vulkan_memory_budget_test.cpp` directly with GCC 11 and UBSan on Ubuntu/WSL. All cases, including saturated multi-heap totals, passed with no sanitizer report. Could this executable be registered in CTest? I found no current test target for the new source. AI-assisted.
wavehs reviewed 2026-11-26 19:40:52 +00:00
wavehs left a comment
No description provided.
**macOS arm64 check of #2333** (vulkan_memory_budget saturation). A two-file change with no device dependency; I built its new test by hand because the build system does not - see the finding. Tested head `6376bbc20` merged onto `main` `bdbc7e923` as merge `f7ba677a4` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, Apple clang 21.0.0, CMake 4.5.0, CPython 3.15.2, SPIRV-Tools v2026.4, Vulkan headers from `/opt/homebrew/include` (Vulkan-Headers 1.4.363.0). **The fix and its test** - `runtime/src/vulkan_memory_budget.h:19-37`: `add_sat` clamps each heap's `heapUsage`/`heapBudget` contribution at `UINT64_MAX` instead of wrapping, and `available` still keys off `local_budget != 0`, so wide multi-heap devices keep reporting a figure. That is the right shape: the only consumers are the HUD's display (a ratio) and the null-query guard, and a saturated numerator/denominator is a saner answer than a wrapped one. - I compiled and ran the new test directly (`clang++ -std=c++17 -I runtime -I /opt/homebrew/include runtime/test/vulkan_memory_budget_test.cpp`): prints `vulkan_memory_budget_test: ok`, exit 0. - NC (keep the new test, revert only `runtime/src/vulkan_memory_budget.h` to `main`): compile OK, run fails at `line 44: saturated.available` - the wrapped `local_budget` becomes 0 and the code reports "VRAM n/a". The regression is real and the test detects it. **Finding: the new test is never built or run - it is not in `runtime/CMakeLists.txt`** `runtime/test/vulkan_memory_budget_test.cpp` is a new file, but `runtime/CMakeLists.txt` has no `add_executable(vulkan_memory_budget_test ...)` or `add_test` for it (I grepped the source list and the test list; `ctest --test-dir build2304 -N` shows 112 tests and none mentions `memory_budget`). Every other test in `runtime/test` is registered explicitly (`title_hud_test` at `:852-859`, `list_value_wait_budget_test` at `:1508-1509`), and there is no glob, so this file is dead in every build: ctest, the Windows runtime job and Wine will all skip it, which is exactly the regression coverage the PR is adding. The existing guard `tests/test_ctest_registration.py` cannot see it - its `test_all_test_executables_have_an_add_test` compares *targets that exist* against `add_test` commands, so a file with no target at all is invisible (it passes on this merge; I ran it). Two things I would ask for in the same PR: 1. Register the target. It needs only the Vulkan headers (`target_link_libraries(vulkan_memory_budget_test PRIVATE Vulkan::Headers)`), so no `WIN32`/`MSVC` guard is needed and it runs on macOS, Linux and Windows alike. 2. Close the hole in `tests/test_ctest_registration.py`: compare `runtime/test/*_test.cpp` against the target names in the CMake source, and fail on a file with no `add_executable`. Otherwise this class of "test that never runs" will keep getting through, and the PR's own body can say "tested" while CI tests nothing. **Not run**: the Windows-only title HUD test (`title_hud_test` links `user32`/`psapi`) and the full ctest suite, neither of which is affected by this header. No GPU work here. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
zmasi commented 2026-11-28 20:40:00 +00:00

#2333 | head 6376bbc20f655879e0256ebd40eae19acc440a93 (merged cleanly with main 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vulkan_memory_budget|d3d12_compute)$" --output-on-failure (LEONIDA_VK_VALIDATION=1, /W4 /WX /permissive- /EHsc /utf-8 on vulkan_memory_budget_test) | red on main: vulkan_memory_budget fails at line 44: saturated.available (rc=8, multi-heap uint64_t budget sum wraps to 0) | green on PR+main: 2/2 CTests pass (vulkan_memory_budget vulkan_memory_budget_test: ok under /W4 /WX + d3d12_compute on RTX 3090 with 0 Khronos validation errors); check_no_game_data.py 0 violations. Non-blocking note: runtime/test/vulkan_memory_budget_test.cpp is not yet registered via add_executable / add_test in runtime/CMakeLists.txt, so CTest will not build or run it automatically until wired up | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2333 | head 6376bbc20f655879e0256ebd40eae19acc440a93 (merged cleanly with main 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vulkan_memory_budget|d3d12_compute)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`, `/W4 /WX /permissive- /EHsc /utf-8` on `vulkan_memory_budget_test`) | red on main: `vulkan_memory_budget` fails at `line 44: saturated.available` (`rc=8`, multi-heap `uint64_t` budget sum wraps to `0`) | green on PR+main: 2/2 CTests pass (`vulkan_memory_budget` `vulkan_memory_budget_test: ok` under `/W4 /WX` + `d3d12_compute` on RTX 3090 with `0` Khronos validation errors); `check_no_game_data.py` `0` violations. Non-blocking note: `runtime/test/vulkan_memory_budget_test.cpp` is not yet registered via `add_executable` / `add_test` in `runtime/CMakeLists.txt`, so CTest will not build or run it automatically until wired up | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
timbot103 reviewed 2026-11-29 04:28:45 +00:00
timbot103 left a comment
No description provided.
Static audit of `6376bbc20` only. No builds, tests, or GPU runs. `runtime/src/vulkan_memory_budget.h:22` correctly caps local `heapBudget`, local `heapUsage`, and shared `heapUsage`. The loop stops at `VK_MAX_MEMORY_HEAPS` even if `memoryHeapCount` is larger, and heap `size` does not enter these totals. The arithmetic uses `<cstdint>` and standard unsigned operations on MSVC and Linux. The HUD and overlay format each total independently by dividing by a fixed GiB constant in `runtime/src/memory_figures.h:42`. - `runtime/test/vulkan_memory_budget_test.cpp:43` covers overflow in all three totals, but `runtime/CMakeLists.txt` has no executable or CTest entry for it. Please register it where `Vulkan::Headers` is available so runtime CI exercises the regression. - `runtime/test/d3d12_vram_budget_test.cpp:90` adds the two usage totals to check whether either is nonzero. If local usage saturates and shared usage is `1`, the sum wraps to zero and the assertion fails. Please compare the fields independently with `EXPECT(after.local_usage != 0 || after.shared_usage != 0)`. AI-assisted.
OreoFR commented 2026-11-29 14:57:44 +00:00

TAKEN by OreoFR (Windows, RTX 4070 8 GB)

TAKEN by OreoFR (Windows, RTX 4070 8 GB)
OreoFR commented 2026-11-29 21:33:02 +00:00

Verified by OreoFR — READY

  • Base: main c22a0a732 + this PR (head 6376bbc20) merged with --no-ff
  • Red (main + only this PR's test): runtime/test/vulkan_memory_budget_test.cpp is not registered in runtime/CMakeLists.txt, so it was compiled by hand (cl /std:c++20, Vulkan SDK headers) against main's vulkan_memory_budget.h: it fails at line 44 (saturated.available): the two device-local budgets wrap to 0 and the sum reports no budget.
  • Green (merge): the same test against this PR's header prints vulkan_memory_budget_test: ok. ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p1 = main + #2198 + #2333 + #2308 + #2268 (no shared files): 506 tests, new failures vs main: none. d3d12_queue_wait_timeout, d3d12_pipeline_workers and dxgi_waitable_latency failed in the parallel run and pass when re-run alone (main alone: 505 tests, 6 failures: big_compile_gate and api_ref_state and d3d12_queue_wait_timeout timeouts, xs_proxy_mock, vk_compile_gate_calibration, xbox_present; xbox_present fails in every package as on main)
  • gta6rt.exe links without the XMA decoder (LEONIDA_CARGO=OFF, LEONIDA_XMA2_DECODER_LIB empty) in package p1; imports unchanged vs main (25 DLLs, 755 functions, no WS2_32)
  • Diff read: no issue. The saturating add caps all three sums. Follow-up: the new test is never built by CMake, so neither ctest nor CI runs it; an add_executable + add_test next to vulkan_device_markers would keep it running.
  • Other runs: Consistent with zmasi's run and lixonik's, wavehs's and timbot103's reviews.
  • Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44

Ready to merge per the maintainers' plan.

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

**Verified by OreoFR** — READY - Base: main c22a0a732 + this PR (head 6376bbc20) merged with --no-ff - Red (main + only this PR's test): `runtime/test/vulkan_memory_budget_test.cpp` is not registered in `runtime/CMakeLists.txt`, so it was compiled by hand (cl /std:c++20, Vulkan SDK headers) against main's `vulkan_memory_budget.h`: it fails at line 44 (`saturated.available`): the two device-local budgets wrap to 0 and the sum reports no budget. - Green (merge): the same test against this PR's header prints `vulkan_memory_budget_test: ok`. ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p1 = main + #2198 + #2333 + #2308 + #2268 (no shared files): 506 tests, new failures vs main: none. `d3d12_queue_wait_timeout`, `d3d12_pipeline_workers` and `dxgi_waitable_latency` failed in the parallel run and pass when re-run alone (main alone: 505 tests, 6 failures: big_compile_gate and api_ref_state and d3d12_queue_wait_timeout timeouts, xs_proxy_mock, vk_compile_gate_calibration, xbox_present; xbox_present fails in every package as on main) - gta6rt.exe links without the XMA decoder (`LEONIDA_CARGO=OFF`, `LEONIDA_XMA2_DECODER_LIB` empty) in package p1; imports unchanged vs main (25 DLLs, 755 functions, no WS2_32) - Diff read: no issue. The saturating add caps all three sums. Follow-up: the new test is never built by CMake, so neither ctest nor CI runs it; an `add_executable` + `add_test` next to `vulkan_device_markers` would keep it running. - Other runs: Consistent with zmasi's run and lixonik's, wavehs's and timbot103's reviews. - Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44 Ready to merge per the maintainers' plan. AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.
fiestorra commented 2026-11-30 06:29:13 +00:00

Verified on this machine (Windows 11, MSVC 19.44.35226), and the finding is about the wiring, not the code: this pull request's own test is never built or run by anyone.

1. It applies. merge-tree clean; d2a4f7e5e cherry-picks onto main 0ae217ceb without a conflict. Two files: runtime/src/vulkan_memory_budget.h (new) and runtime/test/vulkan_memory_budget_test.cpp (new).

2. The test is not registered anywhere. runtime/CMakeLists.txt is not in the diff, and:

grep vulkan_memory_budget runtime/CMakeLists.txt   -> 0 occurrences, on main and on this branch

Every other vulkan_*_test is added one by one (add_executable(vulkan_device_filter_test …), add_executable(vulkan_device_markers_test …)), and the only foreach loops in that file cover specific sets that this name is not in. So ctest never builds it, BUILD_TESTING never enumerates it, and the pull request ships a test that nobody runs.

3. It compiles and passes once registered, which I checked by adding the two lines locally (in my worktree only, nothing pushed):

add_executable(vulkan_memory_budget_test test/vulkan_memory_budget_test.cpp)
target_link_libraries(vulkan_memory_budget_test PRIVATE Vulkan::Headers)
cmake --build … --target vulkan_memory_budget_test   -> linked
vulkan_memory_budget_test.exe                        -> exit 0, 0, 0

So the code is fine and the omission is the whole problem - three lines in runtime/CMakeLists.txt, next to the other vulkan_*_test entries.

Why this one matters beyond itself: this is exactly what #2668's test_all_test_sources_on_disk_are_wired exists to catch - a *_test.cpp on disk with no add_executable. After #2668 lands, this pull request would fail that check instead of passing silently. Until then, nothing in CI notices.

What I did not test: the behaviour. The test feeds made-up heaps to the two Vulkan structures; whether the HUD shows the right budget under VK_EXT_memory_budget needs the title with LEONIDA_TITLE_HUD=1, which is a run-level check.

Written with an AI assistant.

Verified on this machine (Windows 11, MSVC 19.44.35226), and **the finding is about the wiring, not the code: this pull request's own test is never built or run by anyone.** **1. It applies.** `merge-tree` clean; `d2a4f7e5e` cherry-picks onto `main` `0ae217ceb` without a conflict. Two files: `runtime/src/vulkan_memory_budget.h` (**new**) and `runtime/test/vulkan_memory_budget_test.cpp` (**new**). **2. The test is not registered anywhere.** `runtime/CMakeLists.txt` is **not in the diff**, and: ``` grep vulkan_memory_budget runtime/CMakeLists.txt -> 0 occurrences, on main and on this branch ``` Every other `vulkan_*_test` is added one by one (`add_executable(vulkan_device_filter_test …)`, `add_executable(vulkan_device_markers_test …)`), and the only `foreach` loops in that file cover specific sets that this name is not in. **So `ctest` never builds it, `BUILD_TESTING` never enumerates it, and the pull request ships a test that nobody runs.** **3. It compiles and passes once registered, which I checked by adding the two lines locally (in my worktree only, nothing pushed):** ```cmake add_executable(vulkan_memory_budget_test test/vulkan_memory_budget_test.cpp) target_link_libraries(vulkan_memory_budget_test PRIVATE Vulkan::Headers) ``` ``` cmake --build … --target vulkan_memory_budget_test -> linked vulkan_memory_budget_test.exe -> exit 0, 0, 0 ``` So the code is fine and the omission is the whole problem - three lines in `runtime/CMakeLists.txt`, next to the other `vulkan_*_test` entries. **Why this one matters beyond itself:** this is exactly what **#2668**'s `test_all_test_sources_on_disk_are_wired` exists to catch - a `*_test.cpp` on disk with no `add_executable`. After #2668 lands, this pull request would fail that check instead of passing silently. Until then, nothing in CI notices. **What I did not test:** the behaviour. The test feeds made-up heaps to the two Vulkan structures; whether the HUD shows the right budget under `VK_EXT_memory_budget` needs the title with `LEONIDA_TITLE_HUD=1`, which is a run-level check. Written with an AI assistant.
KelboLive commented 2026-11-30 17:34:48 +00:00

Review finding on 6376bbc20: the new test is not wired into CMake, so CI will never build or run it.

  • The PR adds runtime/test/vulkan_memory_budget_test.cpp but touches no runtime/CMakeLists.txt entry.
  • Select-String for vulkan_memory_budget_test / memory_budget in runtime/CMakeLists.txt finds only the unrelated d3d12_vram_budget_test target (different file, device path).
  • So the saturation cases in the new file are unbuilt and untested. Suggested:
add_executable(vulkan_memory_budget_test test/vulkan_memory_budget_test.cpp)
target_include_directories(vulkan_memory_budget_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_test(NAME vulkan_memory_budget COMMAND vulkan_memory_budget_test)

(or register it inside the existing Vulkan block if it needs the device). I could not run it for this reason; the header change itself is untouched by this note.

AI-assisted: yes (opencode).

Review finding on `6376bbc20`: the new test is not wired into CMake, so CI will never build or run it. - The PR adds `runtime/test/vulkan_memory_budget_test.cpp` but touches no `runtime/CMakeLists.txt` entry. - `Select-String` for `vulkan_memory_budget_test` / `memory_budget` in `runtime/CMakeLists.txt` finds only the unrelated `d3d12_vram_budget_test` target (different file, device path). - So the saturation cases in the new file are unbuilt and untested. Suggested: ```cmake add_executable(vulkan_memory_budget_test test/vulkan_memory_budget_test.cpp) target_include_directories(vulkan_memory_budget_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) add_test(NAME vulkan_memory_budget COMMAND vulkan_memory_budget_test) ``` (or register it inside the existing Vulkan block if it needs the device). I could not run it for this reason; the header change itself is untouched by this note. AI-assisted: yes (opencode).
KelboLive commented 2026-11-30 17:45:03 +00:00

Follow-up: the test itself is good — only the CMake wiring is missing.

Compiled it directly with MSVC (/std:c++20 /EHsc /W4, runtime\src + Vulkan SDK include) and ran it:

vulkan_memory_budget: ok

exit 0. So adding the add_executable/add_test block from the previous comment is enough; the saturation cases pass as written.

AI-assisted: yes (opencode).

Follow-up: the test itself is good — only the CMake wiring is missing. Compiled it directly with MSVC (`/std:c++20 /EHsc /W4`, `runtime\src` + Vulkan SDK include) and ran it: ``` vulkan_memory_budget: ok ``` exit 0. So adding the `add_executable`/`add_test` block from the previous comment is enough; the saturation cases pass as written. AI-assisted: yes (opencode).
aslan2142 commented 2026-12-01 16:44:03 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.