fix(vk): keep mesh indirect argument ranges from overlapping #1243

Open
jcabarca wants to merge 1 commit from jcabarca/Longshot:fix/vk-mesh-args-ring into main
Contributor

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() in gpu/vk/vk_draw.cpp; only with RX_VK_MESH_ARGS=1 (off by default)
Settings and switches: defaults plus RX_VK_MESH_ARGS=1
Steps: game-free: python -I -B tests/test_mesh_args_cleanup.py --cxx clang++ with this PR's test on a main checkout
Expected: ranges handed out back to back never overlap entries still in use
Actual: on main take_ranges fails: "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 with n, and the ring is doubled so every range still fits.

  • vk: gpu/vk/vk_draw.cpp: kMeshArgs 2^18 to 2^19; mesh_args_take() clamps n to kMeshArgs / 2 and takes next % (kMeshArgs / 2); draw() clamps d.max to kMeshArgs / 2.
  • tests: tests/mesh_args_cleanup_test.cpp and tests/test_mesh_args_cleanup.py: a take_ranges case that takes ranges of different sizes and checks none overlaps one still in use.

The bug, step by step:

  1. mesh_args_take(n) returned next.fetch_add(n) % (kMeshArgs - n) + 1.
  2. The modulus depends on n, so two calls with different n map the running counter onto the ring differently: a range can start inside the previous one.
  3. Each draw's counts are copied into its range just before the draw; an overlapping later copy overwrites counts the earlier draw has not read yet.

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: n at most kMeshArgs / 2, starting below kMeshArgs / 2 + 1.

Measurements

Verdict: it fixes the bug: tests/test_mesh_args_cleanup.py fails 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 side

Before: main @ ac778751 After: this PR @ 93abada1
tests/test_mesh_args_cleanup.py, run 1 fails (1 of 6 cases fails (take_ranges)) passes (6 of 6 pass)
tests/test_mesh_args_cleanup.py, run 2 fails (1 of 6 cases fails (take_ranges)) passes (6 of 6 pass)
Median fails passes
run 1, main @ ac778751: exit 1: mesh argument initialization: 6 cases, 1 failed
run 1, this PR @ 93abada1: exit 0: mesh argument initialization: 6 cases, 0 failed
run 2, main @ ac778751: exit 1: mesh argument initialization: 6 cases, 1 failed
run 2, this PR @ 93abada1: exit 0: mesh argument initialization: 6 cases, 0 failed

Flags and settings

None.

Testing

Tested: on this branch, Windows, Python 3.12, clang++ from llvm-mingw: tests/test_mesh_args_cleanup.py passes (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

  • 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)
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • 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 check that reads the changed code, on this PR, Windows: tests/test_mesh_args_cleanup.py)
  • 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. (tests/test_mesh_args_cleanup.py)
  • A check under tests/ that reads changed source updated in this PR, not weakened.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. (built on Windows with llvm-mingw and on Linux; no OS-specific code or paths)
**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()` in `gpu/vk/vk_draw.cpp`; only with `RX_VK_MESH_ARGS=1` (off by default) **Settings and switches:** defaults plus `RX_VK_MESH_ARGS=1` **Steps:** game-free: `python -I -B tests/test_mesh_args_cleanup.py --cxx clang++` with this PR's test on a main checkout **Expected:** ranges handed out back to back never overlap entries still in use **Actual:** on main `take_ranges` fails: "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 with `n`, and the ring is doubled so every range still fits. - vk: `gpu/vk/vk_draw.cpp`: `kMeshArgs` 2^18 to 2^19; `mesh_args_take()` clamps `n` to `kMeshArgs / 2` and takes `next % (kMeshArgs / 2)`; `draw()` clamps `d.max` to `kMeshArgs / 2`. - tests: `tests/mesh_args_cleanup_test.cpp` and `tests/test_mesh_args_cleanup.py`: a `take_ranges` case that takes ranges of different sizes and checks none overlaps one still in use. The bug, step by step: 1. `mesh_args_take(n)` returned `next.fetch_add(n) % (kMeshArgs - n) + 1`. 2. The modulus depends on `n`, so two calls with different `n` map the running counter onto the ring differently: a range can start inside the previous one. 3. Each draw's counts are copied into its range just before the draw; an overlapping later copy overwrites counts the earlier draw has not read yet. 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: `n` at most `kMeshArgs / 2`, starting below `kMeshArgs / 2 + 1`. ## Measurements **Verdict:** it fixes the bug: `tests/test_mesh_args_cleanup.py` fails 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 side | | Before: main @ ac778751 | After: this PR @ 93abada1 | |---|---|---| | `tests/test_mesh_args_cleanup.py`, run 1 | fails (1 of 6 cases fails (`take_ranges`)) | passes (6 of 6 pass) | | `tests/test_mesh_args_cleanup.py`, run 2 | fails (1 of 6 cases fails (`take_ranges`)) | passes (6 of 6 pass) | | Median | fails | passes | ``` run 1, main @ ac778751: exit 1: mesh argument initialization: 6 cases, 1 failed run 1, this PR @ 93abada1: exit 0: mesh argument initialization: 6 cases, 0 failed run 2, main @ ac778751: exit 1: mesh argument initialization: 6 cases, 1 failed run 2, this PR @ 93abada1: exit 0: mesh argument initialization: 6 cases, 0 failed ``` ### Flags and settings None. ## Testing **Tested:** on this branch, Windows, Python 3.12, clang++ from llvm-mingw: `tests/test_mesh_args_cleanup.py` passes (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 - [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] Measurements has a verdict, or one line saying why there is nothing to measure. - [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 check that reads the changed code, on this PR, Windows: `tests/test_mesh_args_cleanup.py`) - [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. (`tests/test_mesh_args_cleanup.py`) - [x] A check under `tests/` that reads changed source updated in this PR, not weakened. - [x] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. (built on Windows with llvm-mingw and on Linux; no OS-specific code or paths)
fix(vk): keep mesh indirect argument ranges from overlapping
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 / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 13s
93abada130
mesh_args_take placed a range at next % (kMeshArgs - n): the modulus changed with n, so a draw of many entries just before the wrap and a short draw after it were given overlapping entries the GPU had not read yet. The modulus is now fixed at kMeshArgs / 2 (n clamped to it) and the ring doubled to 1 << 19, so entries are still reused 262144 draws on. tests/test_mesh_args_cleanup.py now also runs mesh_args_take (take_ranges case) and checks the buffer size from kMeshArgs.

Assisted-by: GitHub Copilot
Author
Contributor

Tested in game: Played on Windows, AMD Radeon RX 9070 XT: the game runs normally.

Tested in game: Played on Windows, AMD Radeon RX 9070 XT: the game runs normally.
Collaborator

Possibly related, for context:

  • PR #864 fix(vk): RX_VK_PASS_TIMING names the mesh fallback's compute and the indirect mesh argument conversion — Touches adjacent indirect mesh conversion code, but adds profiling spans rather than changing range allocation.
Possibly related, for context: - PR #864 fix(vk): RX_VK_PASS_TIMING names the mesh fallback's compute and the indirect mesh argument conversion — Touches adjacent indirect mesh conversion code, but adds profiling spans rather than changing range allocation.
Author
Contributor

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.

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.
jcabarca changed title from WIP: fix(vk): keep mesh indirect argument ranges from overlapping to fix(vk): keep mesh indirect argument ranges from overlapping 2026-11-16 22:01:47 +00:00
TrashPanda approved these changes 2026-11-17 00:03:13 +00:00
TrashPanda left a comment

With 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 head 93abada1. Approved.

The ring is now 2^19 entries. 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 real mesh_args_take. draw() clamps d.max to 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 only tests/test_mesh_args_cleanup.py.

With `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 head `93abada1`. Approved. The ring is now `2^19` entries. 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 real `mesh_args_take`. `draw()` clamps `d.max` to 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_t`s an entry, 8 MiB, and only while the switch is on. https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1160 also has this change; this pull request is the one to take. The full-suite box names only `tests/test_mesh_args_cleanup.py`.
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 / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 13s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/vk-mesh-args-ring:jcabarca-fix/vk-mesh-args-ring'
git switch 'jcabarca-fix/vk-mesh-args-ring'
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!1243
No description provided.