vulkan: the predicate reducer and the present composer pick memory through memory_type_order (RT-PREDICATE-COMPOSE-MEMORY-TYPES) #2548

Closed
github-bot wants to merge 1 commit from github-bot/gh-2548 into main AGit
Owner

@⁠Tyrannys on GitHub · 2026-11-28 01:46 UTC · original
Tyrannys:rt-predicate-compose-memory-types → main · +204 −19 · 5 files · 1 commits · closed without merging

Claimed in the coordination issue as: RT-PREDICATE-COMPOSE-MEMORY-TYPES (#1608)

What and why

Item 4 of @⁠kevincommits' Finding 19 in #1608 (#1608). Thanks for the report.

PredicateReducer::batch (runtime/src/vulkan_predicate.h) and PresentComposer::ensure_target (runtime/src/vulkan_present_compose.h) each chose memory with a loop of their own. The loop took the first DEVICE_LOCAL type the resource accepts and called vkAllocateMemory once:

  • No heap size check. It did not check that the type's heap holds the allocation (VUID-vkAllocateMemory-pAllocateInfo-01713). On a GPU without Resizable BAR, a small DEVICE_LOCAL | HOST_VISIBLE heap (256 MiB) often comes before the VRAM heap.
  • No 32-type bound. It had no bound on memoryTypeCount, so a count above VK_MAX_MEMORY_TYPES meant 1u << i past 31 and reads past the array.
  • No second choice. A type the driver refused was not followed by another, which can happen when VRAM is over budget, as our boots are. The composer also never fell back to a non-DEVICE_LOCAL type.

Change. Both now take memory_type_order (vulkan_memory.h), as vulkan_indirect_dispatch_count.h already does:

  • DEVICE_LOCAL types first, then any type the resource takes;
  • only types whose heap holds it;
  • at most VK_MAX_MEMORY_TYPES.

A new allocate_first_fitting there tries them in turn. The composer also refuses a zero extent before vkCreateImage (VUID-VkImageCreateInfo-extent-00944/00945), keeping its current target.

Overlap: no open PR touches vulkan_predicate.h or vulkan_present_compose.h.

Tested

Windows 11, MSVC (VS 18), RelWithDebInfo, on main 431ceba4f.

  • New CPU test predicate_memory_type, with PredicateReducer on fake device calls and a memory layout without Resizable BAR (a small BAR heap, VRAM, system memory):
    • a buffer larger than the BAR heap goes to VRAM without asking the BAR type;
    • a refused DEVICE_LOCAL type is followed by the next, and system memory is the last resort;
    • with every type refused there is no batch;
    • only memoryTypeBits types are asked;
    • a memoryTypeCount of 40 is read up to 32;
    • the composer's order (VRAM, then system memory) through allocate_first_fitting, including refusals.
  • With main's vulkan_predicate.h, 3 checks fail (the BAR type is asked first, and no type after a refused one); all pass with the change.
  • Existing GPU tests on the same paths: vk_predicate_capture, vk_predicated_copy and vk_present_dump (3 cases, which present through the composer) pass. gta6rt_d3d12_com builds.
  • scripts/check_no_game_data.py: clean.

Not tested: a real GPU without Resizable BAR, and a game boot.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, 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)

🤖 Generated with Claude Code

**@⁠Tyrannys** on GitHub · 2026-11-28 01:46 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548) `Tyrannys:rt-predicate-compose-memory-types` → `main` · +204 −19 · 5 files · 1 commits · closed without merging Claimed in the coordination issue as: RT-PREDICATE-COMPOSE-MEMORY-TYPES (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why Item 4 of @⁠kevincommits' Finding 19 in #1608 (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). Thanks for the report. `PredicateReducer::batch` (`runtime/src/vulkan_predicate.h`) and `PresentComposer::ensure_target` (`runtime/src/vulkan_present_compose.h`) each chose memory with a loop of their own. The loop took the first `DEVICE_LOCAL` type the resource accepts and called `vkAllocateMemory` once: - **No heap size check.** It did not check that the type's heap holds the allocation (`VUID-vkAllocateMemory-pAllocateInfo-01713`). On a GPU without Resizable BAR, a small `DEVICE_LOCAL | HOST_VISIBLE` heap (256 MiB) often comes before the VRAM heap. - **No 32-type bound.** It had no bound on `memoryTypeCount`, so a count above `VK_MAX_MEMORY_TYPES` meant `1u << i` past 31 and reads past the array. - **No second choice.** A type the driver refused was not followed by another, which can happen when VRAM is over budget, as our boots are. The composer also never fell back to a non-`DEVICE_LOCAL` type. **Change.** Both now take `memory_type_order` (`vulkan_memory.h`), as `vulkan_indirect_dispatch_count.h` already does: - `DEVICE_LOCAL` types first, then any type the resource takes; - only types whose heap holds it; - at most `VK_MAX_MEMORY_TYPES`. A new `allocate_first_fitting` there tries them in turn. The composer also refuses a zero extent before `vkCreateImage` (`VUID-VkImageCreateInfo-extent-00944/00945`), keeping its current target. Overlap: no open PR touches `vulkan_predicate.h` or `vulkan_present_compose.h`. ### Tested Windows 11, MSVC (VS 18), RelWithDebInfo, on `main` 431ceba4f. - New CPU test `predicate_memory_type`, with `PredicateReducer` on fake device calls and a memory layout without Resizable BAR (a small BAR heap, VRAM, system memory): - a buffer larger than the BAR heap goes to VRAM without asking the BAR type; - a refused `DEVICE_LOCAL` type is followed by the next, and system memory is the last resort; - with every type refused there is no batch; - only `memoryTypeBits` types are asked; - a `memoryTypeCount` of 40 is read up to 32; - the composer's order (VRAM, then system memory) through `allocate_first_fitting`, including refusals. - With `main`'s `vulkan_predicate.h`, 3 checks fail (the BAR type is asked first, and no type after a refused one); all pass with the change. - Existing GPU tests on the same paths: `vk_predicate_capture`, `vk_predicated_copy` and `vk_present_dump` (3 cases, which present through the composer) pass. `gta6rt_d3d12_com` builds. - `scripts/check_no_game_data.py`: clean. Not tested: a real GPU without Resizable BAR, and a game boot. GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, 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) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
From kevincommits' Finding 19 (item 4) in #1608.

PredicateReducer::batch and PresentComposer::ensure_target each had a
loop of their own: the first DEVICE_LOCAL type the resource accepts, with no
check that its heap holds the allocation (VUID-vkAllocateMemory-pAllocateInfo-
01713, e.g. the 256 MiB BAR heap of a GPU without Resizable BAR), no bound on
memoryTypeCount (1u << i past 31), and one vkAllocateMemory with no second
choice. The composer did not fall back to a non-DEVICE_LOCAL type at all.

Both now take memory_type_order (vulkan_memory.h), as
vulkan_indirect_dispatch_count.h does: DEVICE_LOCAL types first, then any
type the resource takes, only types whose heap holds it, at most
VK_MAX_MEMORY_TYPES. The new allocate_first_fitting there tries them in
turn. The composer also refuses a zero extent before vkCreateImage
(VUID-VkImageCreateInfo-extent-00944/00945).

New CPU test predicate_memory_type: the reducer with fake device calls on a
layout without Resizable BAR (a buffer larger than the BAR heap skips it, a
refused type is followed by the next, system memory last, memoryTypeBits
respected), a memoryTypeCount above 32, and allocate_first_fitting for the
composer's order. 3 checks fail with main's reducer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 01:48:57 +00:00
Author
Owner

@⁠zmasi on GitHub · 2026-11-28 03:18 UTC · original

#2548 | head 61822f7c5e (merged cleanly with main 02f71ccfe7) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(predicate_memory_type|d3d12_predicated_copy)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: predicate_memory_type_test.cpp fails to compile (C2039: missing leonida::allocate_first_fitting), and running the PredicateReducer::batch() assertions alone against main fails 3/6 checks (picks the undersized BAR heap, stops on a refused DEVICE_LOCAL type, and never falls back to host memory) | green on PR+main: 2/2 CTests pass (predicate_memory_type 10/10 checks, d3d12_predicated_copy on RTX 3090 with 0 Khronos validation errors); test_ci_registration.py 18/18 OK, check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

Also verified the zero-extent PresentComposer::ensure_target guard (VUID-VkImageCreateInfo-extent-00944 / 00945) and all PresentComposer compose: paths on RTX 3090 under VK_LAYER_KHRONOS_validation.

**@⁠zmasi** on GitHub · 2026-11-28 03:18 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548#issuecomment-7232964045) #2548 | head 61822f7c5e5e836623e63317d6cdcf758836ce3c (merged cleanly with main 02f71ccfe78160997a02950607be583af87e1180) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(predicate_memory_type|d3d12_predicated_copy)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `predicate_memory_type_test.cpp` fails to compile (`C2039`: missing `leonida::allocate_first_fitting`), and running the `PredicateReducer::batch()` assertions alone against `main` fails `3/6` checks (picks the undersized BAR heap, stops on a refused `DEVICE_LOCAL` type, and never falls back to host memory) | green on PR+main: `2/2` CTests pass (`predicate_memory_type` `10/10` checks, `d3d12_predicated_copy` on RTX 3090 with `0` Khronos validation errors); `test_ci_registration.py` `18/18` OK, `check_no_game_data.py` `0` violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes Also verified the zero-extent `PresentComposer::ensure_target` guard (`VUID-VkImageCreateInfo-extent-00944` / `00945`) and all `PresentComposer` `compose:` paths on RTX 3090 under `VK_LAYER_KHRONOS_validation`.
Author
Owner

@⁠lixonik on GitHub · 2026-11-28 08:51 UTC · original · approved these changes

Reviewed the diff and tested merged onto main (493867366) with Ninja Release (MSVC).

Extracting allocate_first_fitting into vulkan_memory.h and routing both vulkan_predicate.h and vulkan_present_compose.h through it cleanly handles memory-type fallback without duplicating allocation loops, and the new unit test thoroughly exercises fallback selection and failure paths.

Tests run

  • ctest --output-on-failure -R "^predicate_memory_type$": 1/1 passed (predicate_memory_type 0.03 s).
  • python -m unittest tests/test_ctest_registration.py -v: 7/7 passed (1.12 s).

AI-assisted (Antigravity, Gemini).

**@⁠lixonik** on GitHub · 2026-11-28 08:51 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548#pullrequestreview-6096839356) · **approved these changes** Reviewed the diff and tested merged onto `main` (`493867366`) with Ninja Release (MSVC). Extracting `allocate_first_fitting` into `vulkan_memory.h` and routing both `vulkan_predicate.h` and `vulkan_present_compose.h` through it cleanly handles memory-type fallback without duplicating allocation loops, and the new unit test thoroughly exercises fallback selection and failure paths. ### Tests run - `ctest --output-on-failure -R "^predicate_memory_type$"`: **1/1 passed** (`predicate_memory_type` 0.03 s). - `python -m unittest tests/test_ctest_registration.py -v`: **7/7 passed** (1.12 s). AI-assisted (Antigravity, Gemini).
Author
Owner

@⁠Maticcm on GitHub · 2026-11-28 21:35 UTC · original

Independent Windows Radeon integrated Vulkan result for exact head 61822f7c5e5e836623e63317d6cdcf758836ce3c versus parent 431ceba4f4a55c69c26fcf0e36fd83e86e1b9a32. Windows 11, AMD Radeon(TM) Graphics selected explicitly, MSVC RelWithDebInfo, Khronos Vulkan validation enabled.

  • d3d12_predicated_copy on the physical Radeon passed on both revisions. Each reported 180 exact predicated-buffer cases against Vulkan, WARP and literal-byte oracles, plus 16 query-to-copy cases and four recapture controls. Neither log reported a validation error or VUID.
  • The same seven-case mock reducer fixture compiled against both revisions. Parent passed 3/7 and head passed 7/7. Both chose the small heap for an exact 1 KiB fit. Adding one 16-byte slot still attempted the small heap on parent and selected VRAM directly on head. The three remaining parent failures were the larger allocation, retry after refusal, and host-memory fallback.
  • The head's stock predicate_memory_type test passed all 10 mock checks, including the type-count limit and the composer's allocation helper. The stock Radeon test covers predicated copies. The heap-boundary and composer results come from synthetic mocks.

Focused CTest totals were parent 1/1 and head 2/2. Synthetic inputs only. AI-assisted with OpenAI Codex.

**@⁠Maticcm** on GitHub · 2026-11-28 21:35 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548#issuecomment-7246132099) Independent Windows Radeon integrated Vulkan result for exact head `61822f7c5e5e836623e63317d6cdcf758836ce3c` versus parent `431ceba4f4a55c69c26fcf0e36fd83e86e1b9a32`. Windows 11, AMD Radeon(TM) Graphics selected explicitly, MSVC RelWithDebInfo, Khronos Vulkan validation enabled. - `d3d12_predicated_copy` on the physical Radeon passed on both revisions. Each reported 180 exact predicated-buffer cases against Vulkan, WARP and literal-byte oracles, plus 16 query-to-copy cases and four recapture controls. Neither log reported a validation error or VUID. - The same seven-case mock reducer fixture compiled against both revisions. Parent passed 3/7 and head passed 7/7. Both chose the small heap for an exact 1 KiB fit. Adding one 16-byte slot still attempted the small heap on parent and selected VRAM directly on head. The three remaining parent failures were the larger allocation, retry after refusal, and host-memory fallback. - The head's stock `predicate_memory_type` test passed all 10 mock checks, including the type-count limit and the composer's allocation helper. The stock Radeon test covers predicated copies. The heap-boundary and composer results come from synthetic mocks. Focused CTest totals were parent 1/1 and head 2/2. Synthetic inputs only. AI-assisted with OpenAI Codex.
Author
Owner

@⁠timbot103 on GitHub · 2026-11-29 01:06 UTC · original · reviewed

Static audit of 61822f7c5 against 48bae6cad.

  • runtime/src/vulkan_memory.h:68 — After ordinary VRAM allocations fail, the retry can try an AMD device-coherent type that is present in the resource mask but forbidden on the logical device (VUID 02790). Filter those types unless the feature is enabled and cover that ordering in the mock.
  • runtime/test/predicate_memory_type_test.cpp:86 — The generic create and no-op stubs are called through incompatible PFN types. Please use signature-matched stubs.
  • Baseline follow-ups at runtime/src/vulkan_present_compose.h:137 and runtime/src/vulkan_predicate.h:68: commit output handles only on successful creation. At runtime/src/vulkan_present.h:1180, require a successful idle wait before replacing the target.

The supplied GPU results cover the normal paths. Forced allocation refusal is covered by mocks.

AI-assisted.

**@⁠timbot103** on GitHub · 2026-11-29 01:06 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548#pullrequestreview-6100840339) · **reviewed** Static audit of `61822f7c5` against `48bae6cad`. - `runtime/src/vulkan_memory.h:68` — After ordinary VRAM allocations fail, the retry can try an AMD device-coherent type that is present in the resource mask but forbidden on the logical device (VUID 02790). Filter those types unless the feature is enabled and cover that ordering in the mock. - `runtime/test/predicate_memory_type_test.cpp:86` — The generic create and no-op stubs are called through incompatible PFN types. Please use signature-matched stubs. - Baseline follow-ups at `runtime/src/vulkan_present_compose.h:137` and `runtime/src/vulkan_predicate.h:68`: commit output handles only on successful creation. At `runtime/src/vulkan_present.h:1180`, require a successful idle wait before replacing the target. The supplied GPU results cover the normal paths. Forced allocation refusal is covered by mocks. AI-assisted.
Author
Owner

@⁠lixonik on GitHub · 2026-11-29 02:50 UTC · original

I confirmed the device-coherent fallback with a signature-matched allocator mock on head 61822f7c5e5e836623e63317d6cdcf758836ce3c merged into main c8bf0e7b65 (Windows 10, MSVC 19.29, no GPU needed).

The resource mask allowed ordinary VRAM (type 0), AMD device-coherent VRAM (type 1), and host memory (type 2). After type 0 returned VK_ERROR_OUT_OF_DEVICE_MEMORY, the PR attempted 0, 1, 2. With the unavailable coherent type filtered out, it attempted 0, 2 and still succeeded. The final successful allocation can therefore hide the invalid intermediate call in this case. The Vulkan requirement is VUID 02790.

AI-assisted. Codex (GPT-6).

**@⁠lixonik** on GitHub · 2026-11-29 02:50 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548#issuecomment-7249683332) I confirmed the device-coherent fallback with a signature-matched allocator mock on head `61822f7c5e5e836623e63317d6cdcf758836ce3c` merged into `main` `c8bf0e7b65` (Windows 10, MSVC 19.29, no GPU needed). The resource mask allowed ordinary VRAM (type 0), AMD device-coherent VRAM (type 1), and host memory (type 2). After type 0 returned `VK_ERROR_OUT_OF_DEVICE_MEMORY`, the PR attempted **0, 1, 2**. With the unavailable coherent type filtered out, it attempted **0, 2** and still succeeded. The final successful allocation can therefore hide the invalid intermediate call in this case. The Vulkan requirement is [VUID 02790](https://docs.vulkan.org/refpages/latest/refpages/source/vkAllocateMemory.html#VUID-vkAllocateMemory-deviceCoherentMemory-02790). AI-assisted. Codex (GPT-6).
Author
Owner

@⁠KelboLive on GitHub · 2026-11-30 17:01 UTC · original

Independent check on Windows (not the author), at 61822f7c5.

  • Built predicate_memory_type_test and ran it: 0 failures — 4 KiB image prefers VRAM then system memory (not the BAR), allocate_first_fitting moves to system memory when VRAM is refused, returns none when every type is refused, and none without vkAllocateMemory.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Vulkan SDK 1.4.363.0.

AI-assisted: yes (opencode).

**@⁠KelboLive** on GitHub · 2026-11-30 17:01 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548#issuecomment-7285598316) Independent check on Windows (not the author), at `61822f7c5`. - Built `predicate_memory_type_test` and ran it: **0 failures** — 4 KiB image prefers VRAM then system memory (not the BAR), `allocate_first_fitting` moves to system memory when VRAM is refused, returns none when every type is refused, and none without `vkAllocateMemory`. Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Vulkan SDK 1.4.363.0. AI-assisted: yes (opencode).
Author
Owner

@⁠aslan2142 on GitHub · 2026-12-01 16:46 UTC · original

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.

**@⁠aslan2142** on GitHub · 2026-12-01 16:46 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2548#issuecomment-7318188812) 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.
github-bot closed this pull request 2026-12-01 16:56:14 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
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
github/ubiquitarianism--leonida!2548
No description provided.