fix(vk): a host pointer import takes its memory type from its own pointer and the buffer's requirements #1555
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1555
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:fix/vk-import-memory-type"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
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()ingpu/vk/vk_memory.cpp. It applies to any driver whosevkGetMemoryHostPointerPropertiesEXTreturns differentmemoryTypeBitsfor different pointers, or whosevkGetBufferMemoryRequirementsfor an import buffer excludes that type or asks for more than the imported bytesSettings 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'stests/test_import_buffer_binding.pyandtests/import_buffer_binding_test.cpp; it compilesimport_pool()against stand-in Vulkan callsExpected: 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 == 2fails (caseper-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.
import_pool()ingpu/vk/vk_memory.cpp:vkGetMemoryHostPointerPropertiesEXTis called for every pointer, not once into astatictype;vkCreateBuffer,vkGetBufferMemoryRequirementsreads the buffer'smemoryTypeBitsandsize;hp.memoryTypeBits & req.memoryTypeBits(the same property test as before);req.size > bytes, the buffer is destroyed,p.buffercleared, and the range held withimport_remember_failure(), as for every other import failure.tests/import_buffer_binding_test.cpp: the stand-invkGetBufferMemoryRequirementsreturns settable requirements and the host pointer properties can change per call; three new host cases,per-pointer-type,buffer-type-bitsandbuffer-too-big, whichtests/test_import_buffer_binding.pyruns.The bug, step by step:
import_pool()shares a range of the game's memory with the GPU: it imports the pointer withVkImportMemoryHostPointerInfoEXTinto aVkDeviceMemoryand binds a buffer to it.static: the first call asksvkGetMemoryHostPointerPropertiesEXTfor that pointer and keeps the first host-visible, coherent type in itsmemoryTypeBits; every later import, whatever its pointer, allocates with that type.vkGetMemoryHostPointerPropertiesEXTfor one pointer; a later pointer may allow other types. Allocating it with the kept type breaks 01744.VkExternalMemoryBufferCreateInfoand bound at offset 0 withoutvkGetBufferMemoryRequirements, so neither its allowed types (01035) nor its size (10741) were checked against the memory.The rules (validusage.json):
The added
vkGetBufferMemoryRequirementscall takesdev.device, the buffer just created from it and aVkMemoryRequirementson the stack, which meets its four VUIDs (valid device, valid buffer, valid pointer, buffer from device).vkGetMemoryHostPointerPropertiesEXTkeeps main's arguments; it now runs for each pointer.Before (main):
After:
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.bufferis 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_bindingfails on main and passes here. Both interleaved runs gave the same result. No speed change is expected: onevkGetMemoryHostPointerPropertiesEXTand onevkGetBufferMemoryRequirementsper 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.12Scene: 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 checkout6fbeca96tests/test_import_buffer_binding.py, run 1tests/test_import_buffer_binding.py, run 2tests/test_import_buffer_binding.py, medianFlags and settings
None.
Testing
Tested: on this branch, Windows, Python 3.12, llvm-mingw clang++:
test_import_buffer_bindingpasses its 43 cases (fails on main), andtest_mirror_trim, the other check that readsvk_memory.cpp, passes.test_import_reachalso readsimport_pool()but does not compile on main nor here (its stand-ins lag behind main'svk_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
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). (the checks that read the changed code, on this PR, Windows:test_import_buffer_bindingandtest_mirror_trimpass;test_import_reachdoes not compile onmaineither)third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here.tests/that reads changed source updated in this PR, not weakened. (tests/import_buffer_binding_test.cppgets settable buffer requirements and per-call host pointer properties)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.
Possibly related, for context:
WIP: fix(vk): a host pointer import takes its memory type from its own pointer and the buffer's requirementsto fix(vk): a host pointer import takes its memory type from its own pointer and the buffer's requirementsRebased onto main @
7daafa7a; the related tests pass on it.16830ad00158a48f0e99From the code and the evidence, this looks ready to merge.
The fix.
import_pool()now asksvkGetMemoryHostPointerPropertiesEXTfor every pointer, not once into a function-localstatic. It picks the first host-visible, coherent type inhp.memoryTypeBits & req.memoryTypeBits, and it refuses a buffer whosereq.sizeis 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, sodestroy_import_buffer(with its owner hook) andp.buffer = VK_NULL_HANDLEleave nothing behind. No memory has been allocated yet, so nothing needs freeing.import_remember_failureholds the range, the same as every other failure in the function. All three callers handle afalsereturn already:pool_forlogs it and returnsnullptr,bg_maindrops 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 withvkAllocateMemory, which the existing timing logs put in the milliseconds. One more property query and onevkGetBufferMemoryRequirementsper 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 leastminImportedHostPointerAlignment(lines 705-706 and 4640). Buffer alignments are no larger than that on the Tier 1 drivers, so a buffer'sreq.sizeshould equalbytesthere. The newreq.size > bytescheck should therefore refuse no import a player makes today.The test models the bug.
per-pointer-typeruns two imports in one process, and the second pointer allows only type 1. On main thestatickeeps type 0, and the assertion the description quotes fails.buffer-type-bitswould also fail on main, since main never reads the buffer's bits.buffer-too-bigchecks that the refused import allocates nothing, binds nothing, destroys the buffer, clearsp.bufferand 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_resultstaysVK_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) {Nit: when a range is refused here,
import_resultstaysVK_SUCCESS, sopool_foronly logs "not imported". A one-time log here (withbytes,req.size,req.alignment,hp.memoryTypeBitsandreq.memoryTypeBits) would show at once which check refused it if some driver ever turns down every import this way.