fix(vk): keep mesh indirect argument ranges from overlapping #1243
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!1243
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:fix/vk-mesh-args-ring"
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, AMD Radeon RX 9070 XT · Needs: none
Why
With
RX_VK_MESH_ARGS=1, two indirect mesh draws close together can be given overlapping entries of the argument ring, so the copy for one overwrites the other's counts before its draw reads them: draws with wrong counts.The same fix was first sent as part of #287 together with other changes; this PR sends it alone, one fix per PR.
How to reproduce
Seen on: not seen in the game: found by reading
mesh_args_take()ingpu/vk/vk_draw.cpp; only withRX_VK_MESH_ARGS=1(off by default)Settings and switches: defaults plus
RX_VK_MESH_ARGS=1Steps: game-free:
python -I -B tests/test_mesh_args_cleanup.py --cxx clang++with this PR's test on a main checkoutExpected: ranges handed out back to back never overlap entries still in use
Actual: on main
take_rangesfails: "range reuses entries still in use" (1 of 6 cases)What changed
mesh_args_take()uses a fixed modulus (half the ring) instead of one that changed withn, and the ring is doubled so every range still fits.gpu/vk/vk_draw.cpp:kMeshArgs2^18 to 2^19;mesh_args_take()clampsntokMeshArgs / 2and takesnext % (kMeshArgs / 2);draw()clampsd.maxtokMeshArgs / 2.tests/mesh_args_cleanup_test.cppandtests/test_mesh_args_cleanup.py: atake_rangescase that takes ranges of different sizes and checks none overlaps one still in use.The bug, step by step:
mesh_args_take(n)returnednext.fetch_add(n) % (kMeshArgs - n) + 1.n, so two calls with differentnmap the running counter onto the ring differently: a range can start inside the previous one.No Vulkan rule is broken on main: it is a data race on the ring's contents. The copy sizes and the indirect draw stay inside the (now bigger) buffer:
nat mostkMeshArgs / 2, starting belowkMeshArgs / 2 + 1.Measurements
Verdict: it fixes the bug:
tests/test_mesh_args_cleanup.pyfails on main and passes with this PR, in both interleaved runs. No speed change is expected: the change is on a failure or check path.Hardware: AMD Radeon RX 9070 XT, Windows; the test itself is game-free (it runs on the CPU, compiled with clang++ from llvm-mingw where it builds a fixture); Python 3.12
Scene: none, game-free
Method: this PR's test files on 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_mesh_args_cleanup.py --cxx clang++on each sideac77875193abada1tests/test_mesh_args_cleanup.py, run 1take_ranges))tests/test_mesh_args_cleanup.py, run 2take_ranges))Flags and settings
None.
Testing
Tested: on this branch, Windows, Python 3.12, clang++ from llvm-mingw:
tests/test_mesh_args_cleanup.pypasses (fails on main).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, AMD Radeon RX 9070 XT: the game runs normally.Not tested: the failure path itself in the game (it needs a driver failure or a format pair the RX 9070 XT does not produce on demand). Linux was built, not run.
Must be tested: None: the change is the same on every GPU and OS, and the game-free test covers it.
Risks
What could break: the ring buffer doubles (2^19 entries of 12 bytes, about 6 MiB of device memory) and only when
RX_VK_MESH_ARGS=1.Turning it off: revert the commit.
Compatibility: Nothing changes.
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 check that reads the changed code, on this PR, Windows:tests/test_mesh_args_cleanup.py)third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here. (tests/test_mesh_args_cleanup.py)tests/that reads changed source updated in this PR, not weakened.Tested in game: Played on Windows, AMD Radeon RX 9070 XT: the game runs normally.
Possibly related, for context:
I am juliotec, the original author of this fix. It was first sent as part of #287 together with other changes; this PR sends it alone, one fix per PR, rebased on current main.
WIP: fix(vk): keep mesh indirect argument ranges from overlappingto fix(vk): keep mesh indirect argument ranges from overlappingWith
RX_VK_MESH_ARGS=1, two mesh draws close together could be handed overlapping argument-ring entries, because the old modulus changed with the size of the draw. I read head93abada1. Approved.The ring is now
2^19entries. Each take is clamped to half of that, and the running counter is reduced modulo that half, then shifted up by one so entry 0 stays unused. A range that runs past the half spills into the second half instead of wrapping onto a neighbour. Back-to-back ranges, including a large one just before the half and a small one after it, do not share entries until a further half-ring has been handed out. The fixture runs that sequence on the realmesh_args_take.draw()clampsd.maxto the same half.The switch is off by default, so the Windows play did not take this path. The fixture is what covers it. The risk line says 12 bytes an entry, about 6 MiB; the allocation is 4
uint32_ts an entry, 8 MiB, and only while the switch is on. #1160 also has this change; this pull request is the one to take. The full-suite box names onlytests/test_mesh_args_cleanup.py.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.