Skip to content

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

Open
narekshaghoyandev wants to merge 1 commit into
ProjectLeonida:mainfrom
narekshaghoyandev:ubiq-vulkan-memory-budget-saturate
Open

narekshaghoyandev wants to merge 1 commit into
ProjectLeonida:mainfrom
narekshaghoyandev:ubiq-vulkan-memory-budget-saturate

Conversation

@narekshaghoyandev

Copy link
Copy Markdown
Contributor

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 (#1608 (comment))
AI-assisted: yes (Gemini)

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 wavehs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2333 | head 6376bbc (merged cleanly with main 1bc055e) | 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 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

TAKEN by OreoFR (Windows, RTX 4070 8 GB)

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR — READY

  • Base: main c22a0a7 + this PR (head 6376bbc) 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 + runtime: verify offline compute pipeline journal hits (RT-NATIVE-COMPUTE-WARMUP) #2198 + vulkan_memory_budget: saturate multi-heap uint64_t sums in sum_vram_budget #2333 + vulkan_device_markers: clamp memoryTypeCount, guard null proc/target and unmap #2308 + Quantize RGB565 render-target clears at each channel width #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

Copy link
Copy Markdown
Contributor

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.

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.

7 participants