fix(vk): a host pointer import takes its memory type from its own pointer and the buffer's requirements #1555

Merged
HumanBean merged 1 commit from jcabarca/Longshot:fix/vk-import-memory-type into main 2026-11-19 07:10:10 +00:00
Contributor

Type: fix · Risk: low · Tested on: Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1 · Needs: none

TL;DR

When the game's memory is shared with the GPU directly, the emulator picked the kind of GPU memory once, from the first piece of memory, and used it for every later piece. It also never asked how much memory each GPU buffer needs. Vulkan forbids both: a later piece the driver does not accept that way, or a buffer bigger than its memory, can fail or misbehave. This PR asks the driver for each piece and checks each buffer; a new test fails on main and passes here.

Breakdown

  • Who it helps: players whose driver gives different memory types for different game memory ranges, or needs more bytes for a buffer than it covers
  • What was wrong: the memory type for imported game memory was chosen once and reused, and the buffer's own requirements were never read
  • What changes: each import takes a type its pointer and its buffer both allow, and an import whose buffer does not fit is refused and held like other import failures
  • What does not change: a driver that returns the same types for every pointer and asks no more than the imported size gets the same type as before; the Linux dma-buf path is untouched
  • How it was checked: a game-free test with three new cases fails on main and passes here (43 cases); the game runs normally on the test PC
  • What is still open: no driver that returns different types per pointer was at hand
  • Risk: low: a refused import falls back to the path any failed import already takes

Why

Host pointer imports in import_pool() reuse the memory type found for the first pointer and bind each buffer without asking its memory requirements, which breaks VUID-VkMemoryAllocateInfo-memoryTypeIndex-01744, VUID-vkBindBufferMemory-memory-01035 and VUID-vkBindBufferMemory-None-10741 whenever a later pointer or a buffer does not match.

How to reproduce

Seen on: not seen in the game: found by reading import_pool() in gpu/vk/vk_memory.cpp. It applies to any driver whose vkGetMemoryHostPointerPropertiesEXT returns different memoryTypeBits for different pointers, or whose vkGetBufferMemoryRequirements for an import buffer excludes that type or asks for more than the imported bytes
Settings and switches: defaults
Steps: game-free: python -I -B tests/test_import_buffer_binding.py --cxx clang++ --headers <Vulkan-Headers/include> on a main checkout with this PR's tests/test_import_buffer_binding.py and tests/import_buffer_binding_test.cpp; it compiles import_pool() against stand-in Vulkan calls
Expected: a second pointer whose host pointer properties allow only type 1 is imported with type 1, and the driver is asked once per pointer
Actual: on main the second import is allocated with type 0, the first pointer's type: call(route, p) && allocated_type == 1 && property_calls == 2 fails (case per-pointer-type)

What changed

The memory type of a host pointer import is now chosen for each import, from the bits its own pointer returns and the bits its buffer allows, and the buffer must fit in the imported bytes.

  • vk: import_pool() in gpu/vk/vk_memory.cpp:
    • vkGetMemoryHostPointerPropertiesEXT is called for every pointer, not once into a static type;
    • after vkCreateBuffer, vkGetBufferMemoryRequirements reads the buffer's memoryTypeBits and size;
    • the type is the first host-visible, host-coherent one in hp.memoryTypeBits & req.memoryTypeBits (the same property test as before);
    • with no such type, or req.size > bytes, the buffer is destroyed, p.buffer cleared, and the range held with import_remember_failure(), as for every other import failure.
  • tests: tests/import_buffer_binding_test.cpp: the stand-in vkGetBufferMemoryRequirements returns settable requirements and the host pointer properties can change per call; three new host cases, per-pointer-type, buffer-type-bits and buffer-too-big, which tests/test_import_buffer_binding.py runs.

The bug, step by step:

  1. import_pool() shares a range of the game's memory with the GPU: it imports the pointer with VkImportMemoryHostPointerInfoEXT into a VkDeviceMemory and binds a buffer to it.
  2. On main the memory type is a function-local static: the first call asks vkGetMemoryHostPointerPropertiesEXT for that pointer and keeps the first host-visible, coherent type in its memoryTypeBits; every later import, whatever its pointer, allocates with that type.
  3. The driver answers vkGetMemoryHostPointerPropertiesEXT for one pointer; a later pointer may allow other types. Allocating it with the kept type breaks 01744.
  4. The buffer is created with VkExternalMemoryBufferCreateInfo and bound at offset 0 without vkGetBufferMemoryRequirements, so neither its allowed types (01035) nor its size (10741) were checked against the memory.

The rules (validusage.json):

  • VUID-VkMemoryAllocateInfo-memoryTypeIndex-01744: "If the parameters define an import operation and the external handle is a host pointer, the value of memoryTypeIndex must be one of those returned by vkGetMemoryHostPointerPropertiesEXT"
  • VUID-vkBindBufferMemory-memory-01035: "memory must have been allocated using one of the memory types allowed in the memoryTypeBits member of the VkMemoryRequirements structure returned from a call to vkGetBufferMemoryRequirements with buffer"
  • VUID-vkBindBufferMemory-None-10741: "If memory was not allocated from a memory heap with the VK_MEMORY_HEAP_TILE_MEMORY_BIT_QCOM property set, size member of the VkMemoryRequirements structure returned from a call to vkGetBufferMemoryRequirements with buffer must be less than or equal to the size of memory minus memoryOffset"

The added vkGetBufferMemoryRequirements call takes dev.device, the buffer just created from it and a VkMemoryRequirements on the stack, which meets its four VUIDs (valid device, valid buffer, valid pointer, buffer from device). vkGetMemoryHostPointerPropertiesEXT keeps main's arguments; it now runs for each pointer.

Before (main):

// (the host allocations' memory type: the same for every pointer, asked once)
static uint32_t type = UINT32_MAX;
if (type == UINT32_MAX) {
    VkMemoryHostPointerPropertiesEXT hp{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT};
    if (vkGetMemoryHostPointerPropertiesEXT(dev.device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, ptr,
                                            &hp) != VK_SUCCESS) { ... }
    for (uint32_t i = 0; i < dev.memory.memoryTypeCount && type == UINT32_MAX; i++)   // (host visible, coherent)
        if ((hp.memoryTypeBits >> i & 1) && ...)
            type = i;
    ...
}

After:

VkMemoryHostPointerPropertiesEXT hp{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT};
if (vkGetMemoryHostPointerPropertiesEXT(dev.device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, ptr, &hp) !=
    VK_SUCCESS) { ... }
... // vkCreateBuffer as before
VkMemoryRequirements req{};
vkGetBufferMemoryRequirements(dev.device, p.buffer, &req);
uint32_t type = UINT32_MAX;
for (uint32_t i = 0; i < dev.memory.memoryTypeCount && type == UINT32_MAX; i++)   // (host visible, coherent)
    if ((hp.memoryTypeBits & req.memoryTypeBits) >> i & 1 && ...)
        type = i;
if (type == UINT32_MAX || req.size > bytes) {
    destroy_import_buffer(p.buffer);
    p.buffer = VK_NULL_HANDLE;
    import_remember_failure(lo, hi);
    return false;
}

What the test checks: per-pointer-type: two imports whose pointers allow type 0 and then only type 1 get type 0 and then type 1, with two property queries; buffer-type-bits: a pointer allowing types 0 and 1 and a buffer allowing only type 1 get type 1; buffer-too-big: a buffer needing more than the imported bytes is neither allocated nor bound, its buffer is destroyed, p.buffer is cleared and the range is held.

Start reading at the comment "the memory types this pointer can be imported as" in import_pool().

Measurements

Verdict: it fixes the bug: test_import_buffer_binding fails on main and passes here. Both interleaved runs gave the same result. No speed change is expected: one vkGetMemoryHostPointerPropertiesEXT and one vkGetBufferMemoryRequirements per import, which already creates a buffer and allocates memory.

Hardware: Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1; the test is game-free (it compiles a cut-out of import_pool() with llvm-mingw, it does not use the GPU); Python 3.12
Scene: none, game-free
Method: this PR's test and fixture run against each side's checkout, 2 runs each, interleaved (main, PR, main, PR); the result is pass/fail, so the median is the result both runs gave
How to reproduce: python -I -B tests/test_import_buffer_binding.py --cxx clang++ --headers <Vulkan-Headers/include> from each side's checkout

Before: main @ 6fbeca96 After: this PR @ 16830ad0
tests/test_import_buffer_binding.py, run 1 fails passes
tests/test_import_buffer_binding.py, run 2 fails passes
tests/test_import_buffer_binding.py, median fails passes
run 1, main @ 6fbeca96, test_import_buffer_binding: exit 1
Assertion failed: call(route, p) && allocated_type == 1 && property_calls == 2, file fixture.cpp, line 360
run 1, this PR @ 16830ad0, test_import_buffer_binding: exit 0
43 factory/rollback/fallback/hold cases passed
run 2, main @ 6fbeca96, test_import_buffer_binding: exit 1
Assertion failed: call(route, p) && allocated_type == 1 && property_calls == 2, file fixture.cpp, line 360
run 2, this PR @ 16830ad0, test_import_buffer_binding: exit 0
43 factory/rollback/fallback/hold cases passed

Flags and settings

None.

Testing

Tested: on this branch, Windows, Python 3.12, llvm-mingw clang++: test_import_buffer_binding passes its 43 cases (fails on main), and test_mirror_trim, the other check that reads vk_memory.cpp, passes. test_import_reach also reads import_pool() but does not compile on main nor here (its stand-ins lag behind main's vk_memory.cpp; #993 updates them).

Built and played: this commit builds on Windows (llvm-mingw back end) and on Linux (longshot_vk.so, without and with AMD FSR 4). Played on Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1: the game runs normally.

Not tested: a driver that returns different memory types per pointer, or a buffer needing more than its imported bytes: the RX 9070 XT's driver did neither in play, so the refusal path did not run on a GPU; the test covers both. Linux was built, not run.

Must be tested: None.

Risks

What could break: on a driver where a pointer and its buffer share no host-visible, coherent type, or the buffer needs more than the imported bytes, that range is no longer imported: it takes the path every failed import takes (held, served without the import). Before, such an import was allocated and bound against the rules above. A driver that returns the same types for every pointer and fits each buffer gets the same type as before.

Turning it off: revert the commit.

Compatibility: Nothing changes: no saves, options, caches or ABI are touched.

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched). (no issue fits)
  • Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what wasn't is in Not tested.
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and named in Measurements or Risks.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named). (the checks that read the changed code, on this PR, Windows: test_import_buffer_binding and test_mirror_trim pass; test_import_reach does not compile on main either)
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here.
  • A check under tests/ that reads changed source updated in this PR, not weakened. (tests/import_buffer_binding_test.cpp gets settable buffer requirements and per-call host pointer properties)
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
**Type:** fix · **Risk:** low · **Tested on:** Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1 · **Needs:** none ## TL;DR When the game's memory is shared with the GPU directly, the emulator picked the kind of GPU memory once, from the first piece of memory, and used it for every later piece. It also never asked how much memory each GPU buffer needs. Vulkan forbids both: a later piece the driver does not accept that way, or a buffer bigger than its memory, can fail or misbehave. This PR asks the driver for each piece and checks each buffer; a new test fails on main and passes here. ## Breakdown - **Who it helps:** players whose driver gives different memory types for different game memory ranges, or needs more bytes for a buffer than it covers - **What was wrong:** the memory type for imported game memory was chosen once and reused, and the buffer's own requirements were never read - **What changes:** each import takes a type its pointer and its buffer both allow, and an import whose buffer does not fit is refused and held like other import failures - **What does not change:** a driver that returns the same types for every pointer and asks no more than the imported size gets the same type as before; the Linux dma-buf path is untouched - **How it was checked:** a game-free test with three new cases fails on main and passes here (43 cases); the game runs normally on the test PC - **What is still open:** no driver that returns different types per pointer was at hand - **Risk:** low: a refused import falls back to the path any failed import already takes ## Why Host pointer imports in `import_pool()` reuse the memory type found for the first pointer and bind each buffer without asking its memory requirements, which breaks VUID-VkMemoryAllocateInfo-memoryTypeIndex-01744, VUID-vkBindBufferMemory-memory-01035 and VUID-vkBindBufferMemory-None-10741 whenever a later pointer or a buffer does not match. ## How to reproduce **Seen on:** not seen in the game: found by reading `import_pool()` in `gpu/vk/vk_memory.cpp`. It applies to any driver whose `vkGetMemoryHostPointerPropertiesEXT` returns different `memoryTypeBits` for different pointers, or whose `vkGetBufferMemoryRequirements` for an import buffer excludes that type or asks for more than the imported bytes **Settings and switches:** defaults **Steps:** game-free: `python -I -B tests/test_import_buffer_binding.py --cxx clang++ --headers <Vulkan-Headers/include>` on a main checkout with this PR's `tests/test_import_buffer_binding.py` and `tests/import_buffer_binding_test.cpp`; it compiles `import_pool()` against stand-in Vulkan calls **Expected:** a second pointer whose host pointer properties allow only type 1 is imported with type 1, and the driver is asked once per pointer **Actual:** on main the second import is allocated with type 0, the first pointer's type: `call(route, p) && allocated_type == 1 && property_calls == 2` fails (case `per-pointer-type`) ## What changed The memory type of a host pointer import is now chosen for each import, from the bits its own pointer returns and the bits its buffer allows, and the buffer must fit in the imported bytes. - vk: `import_pool()` in `gpu/vk/vk_memory.cpp`: - `vkGetMemoryHostPointerPropertiesEXT` is called for every pointer, not once into a `static` type; - after `vkCreateBuffer`, `vkGetBufferMemoryRequirements` reads the buffer's `memoryTypeBits` and `size`; - the type is the first host-visible, host-coherent one in `hp.memoryTypeBits & req.memoryTypeBits` (the same property test as before); - with no such type, or `req.size > bytes`, the buffer is destroyed, `p.buffer` cleared, and the range held with `import_remember_failure()`, as for every other import failure. - tests: `tests/import_buffer_binding_test.cpp`: the stand-in `vkGetBufferMemoryRequirements` returns settable requirements and the host pointer properties can change per call; three new host cases, `per-pointer-type`, `buffer-type-bits` and `buffer-too-big`, which `tests/test_import_buffer_binding.py` runs. The bug, step by step: 1. `import_pool()` shares a range of the game's memory with the GPU: it imports the pointer with `VkImportMemoryHostPointerInfoEXT` into a `VkDeviceMemory` and binds a buffer to it. 2. On main the memory type is a function-local `static`: the first call asks `vkGetMemoryHostPointerPropertiesEXT` for that pointer and keeps the first host-visible, coherent type in its `memoryTypeBits`; every later import, whatever its pointer, allocates with that type. 3. The driver answers `vkGetMemoryHostPointerPropertiesEXT` for one pointer; a later pointer may allow other types. Allocating it with the kept type breaks 01744. 4. The buffer is created with `VkExternalMemoryBufferCreateInfo` and bound at offset 0 without `vkGetBufferMemoryRequirements`, so neither its allowed types (01035) nor its size (10741) were checked against the memory. The rules (validusage.json): - VUID-VkMemoryAllocateInfo-memoryTypeIndex-01744: "If the parameters define an import operation and the external handle is a host pointer, the value of memoryTypeIndex must be one of those returned by vkGetMemoryHostPointerPropertiesEXT" - VUID-vkBindBufferMemory-memory-01035: "memory must have been allocated using one of the memory types allowed in the memoryTypeBits member of the VkMemoryRequirements structure returned from a call to vkGetBufferMemoryRequirements with buffer" - VUID-vkBindBufferMemory-None-10741: "If memory was not allocated from a memory heap with the VK_MEMORY_HEAP_TILE_MEMORY_BIT_QCOM property set, size member of the VkMemoryRequirements structure returned from a call to vkGetBufferMemoryRequirements with buffer must be less than or equal to the size of memory minus memoryOffset" The added `vkGetBufferMemoryRequirements` call takes `dev.device`, the buffer just created from it and a `VkMemoryRequirements` on the stack, which meets its four VUIDs (valid device, valid buffer, valid pointer, buffer from device). `vkGetMemoryHostPointerPropertiesEXT` keeps main's arguments; it now runs for each pointer. Before (main): ```cpp // (the host allocations' memory type: the same for every pointer, asked once) static uint32_t type = UINT32_MAX; if (type == UINT32_MAX) { VkMemoryHostPointerPropertiesEXT hp{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT}; if (vkGetMemoryHostPointerPropertiesEXT(dev.device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, ptr, &hp) != VK_SUCCESS) { ... } for (uint32_t i = 0; i < dev.memory.memoryTypeCount && type == UINT32_MAX; i++) // (host visible, coherent) if ((hp.memoryTypeBits >> i & 1) && ...) type = i; ... } ``` After: ```cpp VkMemoryHostPointerPropertiesEXT hp{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT}; if (vkGetMemoryHostPointerPropertiesEXT(dev.device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, ptr, &hp) != VK_SUCCESS) { ... } ... // vkCreateBuffer as before VkMemoryRequirements req{}; vkGetBufferMemoryRequirements(dev.device, p.buffer, &req); uint32_t type = UINT32_MAX; for (uint32_t i = 0; i < dev.memory.memoryTypeCount && type == UINT32_MAX; i++) // (host visible, coherent) if ((hp.memoryTypeBits & req.memoryTypeBits) >> i & 1 && ...) type = i; if (type == UINT32_MAX || req.size > bytes) { destroy_import_buffer(p.buffer); p.buffer = VK_NULL_HANDLE; import_remember_failure(lo, hi); return false; } ``` What the test checks: `per-pointer-type`: two imports whose pointers allow type 0 and then only type 1 get type 0 and then type 1, with two property queries; `buffer-type-bits`: a pointer allowing types 0 and 1 and a buffer allowing only type 1 get type 1; `buffer-too-big`: a buffer needing more than the imported bytes is neither allocated nor bound, its buffer is destroyed, `p.buffer` is cleared and the range is held. Start reading at the comment "the memory types this pointer can be imported as" in `import_pool()`. ## Measurements **Verdict:** it fixes the bug: `test_import_buffer_binding` fails on main and passes here. Both interleaved runs gave the same result. No speed change is expected: one `vkGetMemoryHostPointerPropertiesEXT` and one `vkGetBufferMemoryRequirements` per import, which already creates a buffer and allocates memory. Hardware: Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1; the test is game-free (it compiles a cut-out of `import_pool()` with llvm-mingw, it does not use the GPU); Python 3.12 Scene: none, game-free Method: this PR's test and fixture run against each side's checkout, 2 runs each, interleaved (main, PR, main, PR); the result is pass/fail, so the median is the result both runs gave How to reproduce: `python -I -B tests/test_import_buffer_binding.py --cxx clang++ --headers <Vulkan-Headers/include>` from each side's checkout | | Before: main @ 6fbeca96 | After: this PR @ 16830ad0 | |---|---|---| | `tests/test_import_buffer_binding.py`, run 1 | fails | passes | | `tests/test_import_buffer_binding.py`, run 2 | fails | passes | | `tests/test_import_buffer_binding.py`, median | fails | passes | ``` run 1, main @ 6fbeca96, test_import_buffer_binding: exit 1 Assertion failed: call(route, p) && allocated_type == 1 && property_calls == 2, file fixture.cpp, line 360 run 1, this PR @ 16830ad0, test_import_buffer_binding: exit 0 43 factory/rollback/fallback/hold cases passed run 2, main @ 6fbeca96, test_import_buffer_binding: exit 1 Assertion failed: call(route, p) && allocated_type == 1 && property_calls == 2, file fixture.cpp, line 360 run 2, this PR @ 16830ad0, test_import_buffer_binding: exit 0 43 factory/rollback/fallback/hold cases passed ``` ### Flags and settings None. ## Testing **Tested:** on this branch, Windows, Python 3.12, llvm-mingw clang++: `test_import_buffer_binding` passes its 43 cases (fails on main), and `test_mirror_trim`, the other check that reads `vk_memory.cpp`, passes. `test_import_reach` also reads `import_pool()` but does not compile on main nor here (its stand-ins lag behind main's `vk_memory.cpp`; #993 updates them). **Built and played:** this commit builds on Windows (llvm-mingw back end) and on Linux (`longshot_vk.so`, without and with AMD FSR 4). Played on Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1: the game runs normally. **Not tested:** a driver that returns different memory types per pointer, or a buffer needing more than its imported bytes: the RX 9070 XT's driver did neither in play, so the refusal path did not run on a GPU; the test covers both. Linux was built, not run. **Must be tested:** None. ## Risks **What could break:** on a driver where a pointer and its buffer share no host-visible, coherent type, or the buffer needs more than the imported bytes, that range is no longer imported: it takes the path every failed import takes (held, served without the import). Before, such an import was allocated and bound against the rules above. A driver that returns the same types for every pointer and fits each buffer gets the same type as before. **Turning it off:** revert the commit. **Compatibility:** Nothing changes: no saves, options, caches or ABI are touched. ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). (no issue fits) - [x] Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what wasn't is in Not tested. - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and named in Measurements or Risks. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). (the checks that read the changed code, on this PR, Windows: `test_import_buffer_binding` and `test_mirror_trim` pass; `test_import_reach` does not compile on `main` either) - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here. - [x] A check under `tests/` that reads changed source updated in this PR, not weakened. (`tests/import_buffer_binding_test.cpp` gets settable buffer requirements and per-call host pointer properties) - [x] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
Author
Contributor

Tested in game: Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1: played with this change; the game runs normally, no rendering errors or crashes seen.

Tested in game: Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1: played with this change; the game runs normally, no rendering errors or crashes seen.
Member

Possibly related, for context:

  • PR #431 fix(vk): scan the import table as far back as its largest live import — Earlier Vulkan import-table change in the same code area; merged.
Possibly related, for context: - PR #431 fix(vk): scan the import table as far back as its largest live import — Earlier Vulkan import-table change in the same code area; merged.
jcabarca changed title from WIP: fix(vk): a host pointer import takes its memory type from its own pointer and the buffer's requirements to fix(vk): a host pointer import takes its memory type from its own pointer and the buffer's requirements 2026-11-18 06:01:21 +00:00
Author
Contributor

Rebased onto main @ 7daafa7a; the related tests pass on it.

Rebased onto main @ 7daafa7a; the related tests pass on it.
jcabarca force-pushed fix/vk-import-memory-type from 16830ad001
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 4s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
to 58a48f0e99
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 5s
2026-11-18 08:49:22 +00:00
Compare
HumanBean left a comment

From the code and the evidence, this looks ready to merge.

The fix. import_pool() now asks vkGetMemoryHostPointerPropertiesEXT for every pointer, not once into a function-local static. It picks the first host-visible, coherent type in hp.memoryTypeBits & req.memoryTypeBits, and it refuses a buffer whose req.size is larger than the imported bytes. That covers 01744, 01035 and 10741 as the description says.

The error path (gpu/vk/vk_memory.cpp:509) is right. The buffer was created just before, so destroy_import_buffer (with its owner hook) and p.buffer = VK_NULL_HANDLE leave nothing behind. No memory has been allocated yet, so nothing needs freeing. import_remember_failure holds the range, the same as every other failure in the function. All three callers handle a false return already: pool_for logs it and returns nullptr, bg_main drops it, and the mirror trim rolls back the pieces it already made. The property query now comes first, so a failure there still happens before any buffer exists.

Cost. Imports happen when a pool is made, merged or trimmed, on the import thread or in pool_for. They don't happen every frame. Each one already creates a buffer and pins the pages with vkAllocateMemory, which the existing timing logs put in the milliseconds. One more property query and one vkGetBufferMemoryRequirements per import cost nothing next to that.

No new refusals on today's drivers. Every host-path range is rounded to align, which is at least 4096 and at least minImportedHostPointerAlignment (lines 705-706 and 4640). Buffer alignments are no larger than that on the Tier 1 drivers, so a buffer's req.size should equal bytes there. The new req.size > bytes check should therefore refuse no import a player makes today.

The test models the bug. per-pointer-type runs two imports in one process, and the second pointer allows only type 1. On main the static keeps type 0, and the assertion the description quotes fails. buffer-type-bits would also fail on main, since main never reads the buffer's bits. buffer-too-big checks that the refused import allocates nothing, binds nothing, destroys the buffer, clears p.buffer and holds the range. The count adds up: (12 + 7) × 2 + 5 = 43.

Suggestion, not blocking: when a range is refused for type or size, import_result stays VK_SUCCESS. The caller then logs a bare "not imported" with no reason, so a driver that turns down every import this way would be hard to spot from a log. See the line comment.

Measurements: this is a fix, and its evidence is right. The new test fails on main and passes here, two interleaved runs each with the same result, and the failing assertion is quoted. The cost is named: one property query and one requirements query per import, which is not per frame. No speed change is claimed, so none needs measuring. It was played on Windows with an RX 9070 XT (that driver never takes the refusal path) and built on Linux. The behaviour only changes on a driver that answers differently per pointer or asks for more bytes than the buffer's size. That case is covered by the test, and I see no gap that needs more hardware.

From the code and the evidence, this looks ready to merge. **The fix.** `import_pool()` now asks `vkGetMemoryHostPointerPropertiesEXT` for every pointer, not once into a function-local `static`. It picks the first host-visible, coherent type in `hp.memoryTypeBits & req.memoryTypeBits`, and it refuses a buffer whose `req.size` is larger than the imported bytes. That covers 01744, 01035 and 10741 as the description says. **The error path** (`gpu/vk/vk_memory.cpp:509`) is right. The buffer was created just before, so `destroy_import_buffer` (with its owner hook) and `p.buffer = VK_NULL_HANDLE` leave nothing behind. No memory has been allocated yet, so nothing needs freeing. `import_remember_failure` holds the range, the same as every other failure in the function. All three callers handle a `false` return already: `pool_for` logs it and returns `nullptr`, `bg_main` drops it, and the mirror trim rolls back the pieces it already made. The property query now comes first, so a failure there still happens before any buffer exists. **Cost.** Imports happen when a pool is made, merged or trimmed, on the import thread or in `pool_for`. They don't happen every frame. Each one already creates a buffer and pins the pages with `vkAllocateMemory`, which the existing timing logs put in the milliseconds. One more property query and one `vkGetBufferMemoryRequirements` per import cost nothing next to that. **No new refusals on today's drivers.** Every host-path range is rounded to `align`, which is at least 4096 and at least `minImportedHostPointerAlignment` (lines 705-706 and 4640). Buffer alignments are no larger than that on the Tier 1 drivers, so a buffer's `req.size` should equal `bytes` there. The new `req.size > bytes` check should therefore refuse no import a player makes today. **The test models the bug.** `per-pointer-type` runs two imports in one process, and the second pointer allows only type 1. On main the `static` keeps type 0, and the assertion the description quotes fails. `buffer-type-bits` would also fail on main, since main never reads the buffer's bits. `buffer-too-big` checks that the refused import allocates nothing, binds nothing, destroys the buffer, clears `p.buffer` and holds the range. The count adds up: (12 + 7) × 2 + 5 = 43. Suggestion, not blocking: when a range is refused for type or size, `import_result` stays `VK_SUCCESS`. The caller then logs a bare "not imported" with no reason, so a driver that turns down every import this way would be hard to spot from a log. See the line comment. Measurements: this is a fix, and its evidence is right. The new test fails on main and passes here, two interleaved runs each with the same result, and the failing assertion is quoted. The cost is named: one property query and one requirements query per import, which is not per frame. No speed change is claimed, so none needs measuring. It was played on Windows with an RX 9070 XT (that driver never takes the refusal path) and built on Linux. The behaviour only changes on a driver that answers differently per pointer or asks for more bytes than the buffer's size. That case is covered by the test, and I see no gap that needs more hardware.
@ -512,0 +506,4 @@
(dev.memory.memoryTypes[i].propertyFlags & (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) ==
(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
type = i;
if (type == UINT32_MAX || req.size > bytes) {
Owner

Nit: when a range is refused here, import_result stays VK_SUCCESS, so pool_for only logs "not imported". A one-time log here (with bytes, req.size, req.alignment, hp.memoryTypeBits and req.memoryTypeBits) would show at once which check refused it if some driver ever turns down every import this way.

Nit: when a range is refused here, `import_result` stays `VK_SUCCESS`, so `pool_for` only logs "not imported". A one-time log here (with `bytes`, `req.size`, `req.alignment`, `hp.memoryTypeBits` and `req.memoryTypeBits`) would show at once which check refused it if some driver ever turns down every import this way.
HumanBean deleted branch fix/vk-import-memory-type 2026-11-19 07:10:10 +00:00
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 5s
Some workflows are waiting to be reviewed.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
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
MagicBeanDev/Longshot!1555
No description provided.