WIP: perf(vk): a sparse mirror's block span by shifts for a power-of-two block #2002

Draft
siantm wants to merge 3 commits from siantm/Longshot:perf/vk-sparse-block-span into main
Contributor

Summary

sparse_mirror::block_span (gpu/vk/sparse_mirror.h: the mirror blocks a range reaches, asked on the bindings' path) made two 64-bit divisions a call. A sparse buffer's block is its memory requirements' alignment, which Vulkan makes a power of two: block_span now shifts for such a block and divides as before otherwise. Its end now comes from the range's last byte (off + size - 1), in both ways: off + size + block - 1 wrapped near 2^64 (far above any offset the renderer asks for, so its spans are the same). sparse_buffer_for (gpu/vk/vk_memory.cpp) logs, four times at most, a new sparse buffer whose block is not a power of two, which should never happen, before any allocation or bind. The result expected is a cheaper call; it is not measured yet.

No switch; a revert gives back main's code.

From f61b2820 on exp/x86-asm-sync.

Read first: block_span in gpu/vk/sparse_mirror.h; the new log line in sparse_buffer_for (gpu/vk/vk_memory.cpp).

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head e9bb47aa on main @ 40bfceb0 (perf 12def479, test 8dd8626d, fix e9bb47aa). Build jobs of this PC's job queue, each check in a hidden console: fb-2002-1 (on main @ f1f2dc5b), fb-2002-2 (on 85a59e6f) and fb-2002-3 (on 40bfceb0, which adds only website files to 85a59e6f):

  • Its own: tests/test_block_span_shift.py (block_span against a 128-bit expected span over 200,000 random spans, off + size up to 2^64, and edge cases written out by hand; and block_span cut from the header with a uint64_t that counts divisions, run with blocks given at run time). At the test commit (the PR's own block_span; 7eee4ead on 85a59e6f) it fails: the end wraps to 0 at the top of the 64-bit space ([8, 0) for off = 2^64 - 2, size 1, 8 blocks). At the head it passes (fb-2002-2 and fb-2002-3). Mutants of the head, expected to fail and failing: one end divided again (1 division for a power-of-two block) and main's division for both ends (2).
  • Through tests/run_all.py --strict --exact with no compiler on PATH, CXX unset and LONGSHOT_MINGW set: at the PR's old head 45320fc7 it fails (not found: cxx (--strict)); at the head it passes.
  • The checks that read a file this change touches (tests/run_all.py --strict --changed origin/main; its own among them), at the head on 85a59e6f and on 40bfceb0: 79 checks, 64 passed, 13 failed, 2 skipped (Linux only). Failed: tests/test_capture_read_once.py, tests/test_device_policy.py, tests/test_frame_overlap.py, tests/test_guest_imported.py, tests/test_import_buffer_binding.py, tests/test_import_reach.py, tests/test_linux_pressure.py, tests/test_pending_guest_import.py, tests/test_replay_write_order.py, tests/test_sparse_bind_ranges.py, tests/test_sparse_mirror.py, tests/test_texel_buffer_ownership.py, tests/test_vertex_bind.py: each fails on main @ 85a59e6f too (the same checks in fb-2002-2; most for the Vulkan headers this PC lacks under --strict; tests/test_sparse_mirror.py stops at a vk_replay.cpp source check before its C++ part, on main too).
  • gpu/vk/vk_memory.cpp compiled (syntax) with the game build's own command.

It has NOT run in the game on this PC yet.

Still to test:

  • Windows 11, i7-9700, RTX 4060 (this PC): python -I scripts/dev.py ab --scene kokiri-forest --runs 3 and the same with --scene market (a crowded scene): main @ 40bfceb0 against this PR, the same pinned options file, save, driver and build type, each warmed up, runs interleaved; report per run and as medians the replay's time a frame, average FPS, 1% lows, median and p99 frame times, the process's peak RAM and the driver's VRAM, and the vk: sparse mirrors: N, ... line of each log (N above 0 on both sides: sparse mirrors are on); expecting the same picture, no not a power of two line, and a gain only where this PR's median and p99 frame times are lower than main's beyond the runs' spread (by hand)
  • Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: start in Kokiri Forest from the scene's save, play a minute (walking, turning the camera), main against this PR, warmed, in ABBA order, expecting the same picture every way and no crash or validation error (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest a minute, main against this PR, ABBA, the same picture)
  • Linux (any GPU): build the renderer and run tests/test_block_span_shift.py with --cxx g++ and --cxx clang++, expecting PASS (by hand)

Measurements

Verdict: not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game A/B is in Still to test. No switch: the shifts are on for everyone once merged, and nothing shows yet that they help in play.

Risks

A wrong span would bind or sync the wrong part of a sparse buffer's mirror (wrong geometry or data); the test compares every case, the top of the 64-bit space included, against an expected span from 128-bit arithmetic. The change is always on (no switch) on the bindings' path, with no in-game measurement yet (Still to test); a revert is enough to undo it. The renderer is compiled again after updating; nothing else changes.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says. Not yet: it has not run in the game.
  • mise run test passes (any failure that also fails on main is named). Not yet: only the checks in Testing ran.
  • Measured against main, or Measurements says why there is nothing to measure. Not yet (Measurements).
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. Not yet: vk_memory.cpp compiled (syntax) on Windows only (Testing).
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
## Summary `sparse_mirror::block_span` (`gpu/vk/sparse_mirror.h`: the mirror blocks a range reaches, asked on the bindings' path) made two 64-bit divisions a call. A sparse buffer's block is its memory requirements' alignment, which Vulkan makes a power of two: `block_span` now shifts for such a block and divides as before otherwise. Its end now comes from the range's last byte (`off + size - 1`), in both ways: `off + size + block - 1` wrapped near 2^64 (far above any offset the renderer asks for, so its spans are the same). `sparse_buffer_for` (`gpu/vk/vk_memory.cpp`) logs, four times at most, a new sparse buffer whose block is not a power of two, which should never happen, before any allocation or bind. The result expected is a cheaper call; it is not measured yet. No switch; a revert gives back main's code. From f61b2820 on `exp/x86-asm-sync`. Read first: `block_span` in `gpu/vk/sparse_mirror.h`; the new log line in `sparse_buffer_for` (`gpu/vk/vk_memory.cpp`). ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head e9bb47aa on main @ 40bfceb0 (perf 12def479, test 8dd8626d, fix e9bb47aa). Build jobs of this PC's job queue, each check in a hidden console: `fb-2002-1` (on main @ f1f2dc5b), `fb-2002-2` (on 85a59e6f) and `fb-2002-3` (on 40bfceb0, which adds only website files to 85a59e6f): - Its own: `tests/test_block_span_shift.py` (`block_span` against a 128-bit expected span over 200,000 random spans, off + size up to 2^64, and edge cases written out by hand; and `block_span` cut from the header with a `uint64_t` that counts divisions, run with blocks given at run time). At the test commit (the PR's own `block_span`; 7eee4ead on 85a59e6f) it fails: the end wraps to 0 at the top of the 64-bit space ([8, 0) for off = 2^64 - 2, size 1, 8 blocks). At the head it passes (fb-2002-2 and fb-2002-3). Mutants of the head, expected to fail and failing: one end divided again (1 division for a power-of-two block) and main's division for both ends (2). - Through `tests/run_all.py --strict --exact` with no compiler on PATH, `CXX` unset and `LONGSHOT_MINGW` set: at the PR's old head 45320fc7 it fails (`not found: cxx (--strict)`); at the head it passes. - The checks that read a file this change touches (`tests/run_all.py --strict --changed origin/main`; its own among them), at the head on 85a59e6f and on 40bfceb0: 79 checks, 64 passed, 13 failed, 2 skipped (Linux only). Failed: `tests/test_capture_read_once.py`, `tests/test_device_policy.py`, `tests/test_frame_overlap.py`, `tests/test_guest_imported.py`, `tests/test_import_buffer_binding.py`, `tests/test_import_reach.py`, `tests/test_linux_pressure.py`, `tests/test_pending_guest_import.py`, `tests/test_replay_write_order.py`, `tests/test_sparse_bind_ranges.py`, `tests/test_sparse_mirror.py`, `tests/test_texel_buffer_ownership.py`, `tests/test_vertex_bind.py`: each fails on main @ 85a59e6f too (the same checks in fb-2002-2; most for the Vulkan headers this PC lacks under `--strict`; `tests/test_sparse_mirror.py` stops at a `vk_replay.cpp` source check before its C++ part, on main too). - `gpu/vk/vk_memory.cpp` compiled (syntax) with the game build's own command. It has NOT run in the game on this PC yet. **Still to test:** - Windows 11, i7-9700, RTX 4060 (this PC): `python -I scripts/dev.py ab --scene kokiri-forest --runs 3` and the same with `--scene market` (a crowded scene): main @ 40bfceb0 against this PR, the same pinned options file, save, driver and build type, each warmed up, runs interleaved; report per run and as medians the replay's time a frame, average FPS, 1% lows, median and p99 frame times, the process's peak RAM and the driver's VRAM, and the `vk: sparse mirrors: N, ...` line of each log (N above 0 on both sides: sparse mirrors are on); expecting the same picture, no `not a power of two` line, and a gain only where this PR's median and p99 frame times are lower than main's beyond the runs' spread (by hand) - Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: start in Kokiri Forest from the scene's save, play a minute (walking, turning the camera), main against this PR, warmed, in ABBA order, expecting the same picture every way and no crash or validation error (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest a minute, main against this PR, ABBA, the same picture) - Linux (any GPU): build the renderer and run `tests/test_block_span_shift.py` with `--cxx g++` and `--cxx clang++`, expecting PASS (by hand) ## Measurements **Verdict:** not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game A/B is in Still to test. No switch: the shifts are on for everyone once merged, and nothing shows yet that they help in play. ## Risks A wrong span would bind or sync the wrong part of a sparse buffer's mirror (wrong geometry or data); the test compares every case, the top of the 64-bit space included, against an expected span from 128-bit arithmetic. The change is always on (no switch) on the bindings' path, with no in-game measurement yet (Still to test); a revert is enough to undo it. The renderer is compiled again after updating; nothing else changes. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. Not yet: it has not run in the game. - [ ] `mise run test` passes (any failure that also fails on `main` is named). Not yet: only the checks in Testing ran. - [ ] Measured against `main`, or Measurements says why there is nothing to measure. Not yet (Measurements). - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A check under `tests/` that reads changed source is updated, not weakened. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. Not yet: `vk_memory.cpp` compiled (syntax) on Windows only (Testing). - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold.
perf(vk): a sparse mirror's block span by shifts for a power-of-two block
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 4s
Website / Website on Cloudflare (pull_request_target) Has been skipped
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
CI / Website build and checks (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
45320fc782
sparse_mirror::block_span (the mirror blocks a range reaches, asked on the bindings' path) made two 64-bit
divisions a call. A sparse buffer's block is its memory requirements' alignment, which Vulkan makes a power of two:
block_span now shifts for one, and divides as before otherwise. sparse_fill logs (4 times) a block that is not a
power of two, which should never happen.

Not measured for this port: no micro-benchmark, no run in play (the batch it came in on the branch, cc4c6c42,
showed no difference in play).

Test: tests/test_block_span_shift.py compiles sparse_mirror.h and runs block_span over 200,000 random spans and
blocks (powers of two and others) against the two divisions. No switch.

Assisted-by: Claude Code
Member

Possibly related, for context:

  • PR #250 perf(vk): sparse VRAM mirrors, memory only where a game pool is (on by default) — Introduced sparse mirror block_span helper that this PR optimizes
  • issue #585 Idea: sparse mirrors cost 1-4 ms of GPU a frame on an RX 5600 XT; whole mirrors are faster until their budget fills (runs wanted on other 6 GB cards) — Open report of sparse-mirror performance, especially AMD
  • PR #1624 perf(vk): combine contiguous sparse buffer mappings — Merged sparse-buffer mapping performance work in adjacent Vulkan path
Possibly related, for context: - PR #250 perf(vk): sparse VRAM mirrors, memory only where a game pool is (on by default) — Introduced sparse mirror block_span helper that this PR optimizes - issue #585 Idea: sparse mirrors cost 1-4 ms of GPU a frame on an RX 5600 XT; whole mirrors are faster until their budget fills (runs wanted on other 6 GB cards) — Open report of sparse-mirror performance, especially AMD - PR #1624 perf(vk): combine contiguous sparse buffer mappings — Merged sparse-buffer mapping performance work in adjacent Vulkan path
BeanBot left a comment

Recommendation: Request changes. The new check skips or fails in strict mode on a supported Windows setup where llvm-mingw is configured outside PATH. The shifts preserve the renderer’s block spans, but this head has no measured benefit in play.

Sparse-mirror bindings use shifts for power-of-two block sizes and retain division for other sizes. The code trades up to two divisions per lookup for a branch and a trailing-zero count. Players should see the same rendering. The change adds a diagnostic and a game-free arithmetic check.

@fix will do (6)

  1. L1 tests/test_block_span_shift.py:21: The runner resolves {cxx} before launching the test. (more on the line)
  2. L2 (minor) tests/test_block_span_shift.py:74: Restoring division for either endpoint still passes every numeric comparison and leaves one >> sh for the text check to find. (more on the line)
  3. L3 (minor) gpu/vk/sparse_mirror.h:48: The new expression repeats the old rounding overflow, and the fixture’s division oracle repeats it too. (more on the line)
  4. L4 (minor) gpu/vk/vk_memory.cpp:4386: The alignment warning runs only after sparse allocation and binding succeed. (more on the line)
  5. L5 (its part an edit can do) gpu/vk/sparse_mirror.h:45: Pin a repeatable route and settings in the test plan and use the existing replay, frame-time and memory counters.
  6. L6 (minor): The checklist omits the source-reading checks box and the Ready to merge section. Existing sparse-mirror checks read vk_memory.cpp. Restore the applicable box and the merge checklist so the author can record which checks were verified and which merge gates remain.

For the author (1) (a run, a measurement or a decision)

  • L5 (minor) gpu/vk/sparse_mirror.h:45: The planned hand-played Kokiri comparison looks only at replay time and cannot establish a gain beyond run-to-run noise. (more on the line)

Checks: Still running: CI / Conventional Commit title (pull_request), CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request), CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request), CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request), CI / Workflow lint (actionlint) (pull_request), CI / Website build and checks (pull_request), Guide screenshots / Guide images and local UI source review (pull_request), Third-party parts / As their notes say (pull_request), Third-party parts / The same as upstream (pull_request), PR description / Description follows the template (pull_request_target).

More: does it help, other systems, switches, what was read

Does it help: Unproven. The author reports that 200,000 arithmetic comparisons passed on Windows for 45320fc7. There is no timing comparison between 45320fc7 and 75317e1f. The commit message reports no visible performance difference for an earlier batch and supplies no numbers that isolate this change.

Other systems: The helper is shared by sparse residency checks, binding lifetime tracking, block requests, mirror probes and write-back gap checks. Devices using whole mirrors bypass the changed path. The diff leaves sparse allocation, queue waits, captures, renderer API and file formats intact. Rendering and frame pacing still need in-game coverage on Windows and Linux, including NVIDIA, AMD and Steam Deck, plus Intel where available.

Switches: No runtime switches changed. RX_VK_MIRROR_SPARSE still defaults to on where strict sparse buffer residency is supported and uses whole mirrors elsewhere. The new test accepts --cxx and reads CXX and LONGSHOT_MINGW for compiler selection.

Reviewed: 45320fc782 against 75317e1f31: Read AGENTS.md, CONTRIBUTING.md, the Forgejo template, the development standards, the full diff and commit message, sparse_mirror.h and test_block_span_shift.py in full. Read the affected sections of vk_memory.cpp and the relevant Vulkan allocation, logging, test-runner and build code. The rest of vk_memory.cpp was outside the reviewed paths. Reviewed the supplied PR, CI and run summaries. Raw run logs, Tooling and the referenced experiment commits were unavailable locally.; nothing run (a code review).

Checked and fine:

  • For supported renderer ranges, the shift count is valid, empty spans retain the same endpoint, and both endpoints are clamped to the caller’s block count.
  • The new log counter is atomic and constant-initialized. The diff adds no pool fields, per-thread storage or resource ownership changes.
  • One renderer topic, Conventional Commit title, and an Assisted-by trailer. The diff contains no game-derived data, build output, generated files, shaders or third-party changes. No renderer API version bump is needed.
  • The fixture uses synthetic data and local temporary files. Its compile and run steps are described in the PR.

Testing: Read-only review. The author reports 69 of 77 selected Windows checks passed, including the new fixture, and the same eight failed on base. CI is pending. The PR opened before an in-game run. Full checks, Linux GCC and Clang coverage, full renderer builds, and in-game validation on the affected hardware tiers are still outstanding.

Overlaps and merge order: None found in the supplied PR context and local history.


Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The new check skips or fails in strict mode on a supported Windows setup where llvm-mingw is configured outside PATH. The shifts preserve the renderer’s block spans, but this head has no measured benefit in play. Sparse-mirror bindings use shifts for power-of-two block sizes and retain division for other sizes. The code trades up to two divisions per lookup for a branch and a trailing-zero count. Players should see the same rendering. The change adds a diagnostic and a game-free arithmetic check. **`@fix` will do (6)** 1. **L1** `tests/test_block_span_shift.py:21`: The runner resolves {cxx} before launching the test. (more on the line) 2. **L2** (minor) `tests/test_block_span_shift.py:74`: Restoring division for either endpoint still passes every numeric comparison and leaves one `>> sh` for the text check to find. (more on the line) 3. **L3** (minor) `gpu/vk/sparse_mirror.h:48`: The new expression repeats the old rounding overflow, and the fixture’s division oracle repeats it too. (more on the line) 4. **L4** (minor) `gpu/vk/vk_memory.cpp:4386`: The alignment warning runs only after sparse allocation and binding succeed. (more on the line) 5. **L5** (its part an edit can do) `gpu/vk/sparse_mirror.h:45`: Pin a repeatable route and settings in the test plan and use the existing replay, frame-time and memory counters. 6. **L6** (minor): The checklist omits the source-reading checks box and the Ready to merge section. Existing sparse-mirror checks read vk_memory.cpp. Restore the applicable box and the merge checklist so the author can record which checks were verified and which merge gates remain. **For the author (1)** (a run, a measurement or a decision) - **L5** (minor) `gpu/vk/sparse_mirror.h:45`: The planned hand-played Kokiri comparison looks only at replay time and cannot establish a gain beyond run-to-run noise. (more on the line) **Checks:** Still running: `CI / Conventional Commit title (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request)`, `CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request)`, `CI / Workflow lint (actionlint) (pull_request)`, `CI / Website build and checks (pull_request)`, `Guide screenshots / Guide images and local UI source review (pull_request)`, `Third-party parts / As their notes say (pull_request)`, `Third-party parts / The same as upstream (pull_request)`, `PR description / Description follows the template (pull_request_target)`. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Unproven. The author reports that 200,000 arithmetic comparisons passed on Windows for 45320fc7. There is no timing comparison between 45320fc7 and 75317e1f. The commit message reports no visible performance difference for an earlier batch and supplies no numbers that isolate this change. **Other systems:** The helper is shared by sparse residency checks, binding lifetime tracking, block requests, mirror probes and write-back gap checks. Devices using whole mirrors bypass the changed path. The diff leaves sparse allocation, queue waits, captures, renderer API and file formats intact. Rendering and frame pacing still need in-game coverage on Windows and Linux, including NVIDIA, AMD and Steam Deck, plus Intel where available. **Switches:** No runtime switches changed. RX_VK_MIRROR_SPARSE still defaults to on where strict sparse buffer residency is supported and uses whole mirrors elsewhere. The new test accepts --cxx and reads CXX and LONGSHOT_MINGW for compiler selection. **Reviewed:** 45320fc7828b against 75317e1f31ff: Read AGENTS.md, CONTRIBUTING.md, the Forgejo template, the development standards, the full diff and commit message, sparse_mirror.h and test_block_span_shift.py in full. Read the affected sections of vk_memory.cpp and the relevant Vulkan allocation, logging, test-runner and build code. The rest of vk_memory.cpp was outside the reviewed paths. Reviewed the supplied PR, CI and run summaries. Raw run logs, Tooling and the referenced experiment commits were unavailable locally.; nothing run (a code review). **Checked and fine:** - For supported renderer ranges, the shift count is valid, empty spans retain the same endpoint, and both endpoints are clamped to the caller’s block count. - The new log counter is atomic and constant-initialized. The diff adds no pool fields, per-thread storage or resource ownership changes. - One renderer topic, Conventional Commit title, and an Assisted-by trailer. The diff contains no game-derived data, build output, generated files, shaders or third-party changes. No renderer API version bump is needed. - The fixture uses synthetic data and local temporary files. Its compile and run steps are described in the PR. **Testing:** Read-only review. The author reports 69 of 77 selected Windows checks passed, including the new fixture, and the same eight failed on base. CI is pending. The PR opened before an in-game run. Full checks, Linux GCC and Clang coverage, full renderer builds, and in-game validation on the affected hardware tiers are still outstanding. **Overlaps and merge order:** None found in the supplied PR context and local history. </details> --- <sub>Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -42,6 +42,12 @@ std::vector<uint8_t> live_blocks(uint64_t lo, uint64_t hi, uint64_t block, It fi
/// The blocks [off, off + size) reaches: [*first, *end)
inline void block_span(uint64_t off, uint64_t size, uint64_t block, size_t nblocks, size_t* first, size_t* end) {
if (block && !(block & (block - 1))) { // (a power of two, as a Vulkan alignment always is: shifts, not two divisions)
Member

L5 (minor): The planned hand-played Kokiri comparison looks only at replay time and cannot establish a gain beyond run-to-run noise. Compare the merge base and revised head on identical warmed routes and settings, with at least two interleaved runs per side in the target and a heavy scene. Report replay time, average FPS, 1% lows, median and p99 frame times, and RAM and VRAM for the affected path. Confirm sparse mirrors are active in both builds.

**L5** (minor): The planned hand-played Kokiri comparison looks only at replay time and cannot establish a gain beyond run-to-run noise. Compare the merge base and revised head on identical warmed routes and settings, with at least two interleaved runs per side in the target and a heavy scene. Report replay time, average FPS, 1% lows, median and p99 frame times, and RAM and VRAM for the affected path. Confirm sparse mirrors are active in both builds.
@ -45,0 +45,4 @@
if (block && !(block & (block - 1))) { // (a power of two, as a Vulkan alignment always is: shifts, not two divisions)
const unsigned sh = (unsigned)__builtin_ctzll(block);
*first = (size_t)std::min<uint64_t>(off >> sh, nblocks);
*end = size ? (size_t)std::min<uint64_t>((off + size + block - 1) >> sh, nblocks) : *first;
Member

L3 (minor): The new expression repeats the old rounding overflow, and the fixture’s division oracle repeats it too. With off = UINT64_MAX - 1, size = 1, block = 65536 and eight blocks, the helper returns [8, 0) instead of [8, 8). Current renderer ranges are bounded well below this case. Document the supported input bounds and keep the fixture within them, or use overflow-safe endpoint arithmetic and add a boundary case with an independent expected result.

**L3** (minor): The new expression repeats the old rounding overflow, and the fixture’s division oracle repeats it too. With off = UINT64_MAX - 1, size = 1, block = 65536 and eight blocks, the helper returns [8, 0) instead of [8, 8). Current renderer ranges are bounded well below this case. Document the supported input bounds and keep the fixture within them, or use overflow-safe endpoint arithmetic and add a boundary case with an independent expected result.
@ -4383,6 +4383,11 @@ bool sparse_fill(Pool& p, VkBuffer buffer, const VkMemoryRequirements& req, cons
}
p.sparse = true;
p.sparse_block_bytes = req.alignment, p.sparse_size = req.size, p.sparse_types = req.memoryTypeBits;
if (req.alignment & (req.alignment - 1)) { // (never: Vulkan's alignments are powers of two. block_span divides then, not shifts)
Member

L4 (minor): The alignment warning runs only after sparse allocation and binding succeed. If a driver reports a non-power-of-two alignment and the bind then fails, Longshot falls back to a whole mirror and the proposed log check misses the anomalous value. Check and log the alignment when the sparse buffer requirements become available, before the allocation and binding attempts.

**L4** (minor): The alignment warning runs only after sparse allocation and binding succeed. If a driver reports a non-power-of-two alignment and the bind then fails, Longshot falls back to a whole mirror and the proposed log check misses the anomalous value. Check and log the alignment when the sparse buffer requirements become available, before the allocation and binding attempts.
@ -0,0 +18,4 @@
sys.path.insert(0, str(Path(__file__).resolve().parent))
from srccut import block
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
Member

L1: The runner resolves {cxx} before launching the test. On Windows with llvm-mingw configured through LONGSHOT_MINGW or the checkout’s llvm-mingw directory, and with CXX unset and no compiler on PATH, the runner skips the test or fails under --strict before compiler() can find the installed compiler. Resolve the configured Windows compiler before the runner’s availability check, while preserving explicit compiler overrides.

**L1**: The runner resolves {cxx} before launching the test. On Windows with llvm-mingw configured through LONGSHOT_MINGW or the checkout’s llvm-mingw directory, and with CXX unset and no compiler on PATH, the runner skips the test or fails under --strict before compiler() can find the installed compiler. Resolve the configured Windows compiler before the runner’s availability check, while preserving explicit compiler overrides.
@ -0,0 +71,4 @@
a.cxx = compiler(a.cxx)
header = (ROOT / "gpu" / "vk" / "sparse_mirror.h").read_text(encoding="utf-8")
span = block(header, "inline void block_span(")
if "if (block && !(block & (block - 1))) {" not in span or ">> sh" not in span:
Member

L2 (minor): Restoring division for either endpoint still passes every numeric comparison and leaves one >> sh for the text check to find. Keep the arithmetic fixture for correctness and verify the emitted fast path for both endpoint calculations with a runtime block size.

**L2** (minor): Restoring division for either endpoint still passes every numeric comparison and leaves one `>> sh` for the text check to find. Keep the arithmetic fixture for correctness and verify the emitted fast path for both endpoint calculations with a runtime block size.
siantm force-pushed perf/vk-sparse-block-span from 45320fc782
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 4s
Website / Website on Cloudflare (pull_request_target) Has been skipped
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
CI / Website build and checks (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 e9bb47aa46
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
CI / Website build and checks (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) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 20s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
2026-11-21 06:08:52 +00:00
Compare
Author
Contributor

Thanks for the review. The branch is rebased on main @ 40bfceb0 (the PR's own commit unchanged, git range-diff), with a test commit and a fix commit on top; head e9bb47aa. The checks below ran in build jobs fb-2002-1 (on f1f2dc5b, with the mutants and the old heads) and fb-2002-2 (on 85a59e6f) and fb-2002-3 (on 40bfceb0, website files only since) of this PC's job queue (Windows 11, llvm-mingw clang 23). Nothing has run in the game yet.

  • L1 (#issuecomment-63375), runner and {cxx}: fixed in 8dd8626d. The check no longer declares RUN_ALL_ARGS, so tests/run_all.py no longer looks the compiler up on PATH first; the check finds it itself, one way for a direct run and the suite's: --cxx, CXX, LONGSHOT_MINGW's clang++ (through third_party/a64-kit/tools/toolchain.py, linked static so the fixture runs without the toolchain's DLLs), then PATH. Run through tests/run_all.py --strict --exact with no compiler on PATH, CXX unset and LONGSHOT_MINGW set, the check failed at the PR's old head 45320fc7 (not found: cxx (--strict)) and passes at the new head.
  • L2 (#issuecomment-63376), division at one end still passes: fixed in 8dd8626d. The source-text check is gone; block_span is cut from the header (tests/srccut.py) and compiled again with its uint64_t a type that counts divisions, then run with blocks given at run time: a power-of-two block must take none, for either end. A copy of the head with one end divided again fails (1 division for a power-of-two block), and main's code fails (2). This counts the operations the source asks for, not the emitted instructions (a compiler may still turn a division by a known power of two into a shift on its own).
  • L3 (#issuecomment-63377), the end wrapping near 2^64: fixed. The test (8dd8626d) takes its expected span from 128-bit arithmetic and adds edge cases written out by hand, yours among them (off = 2^64 - 2, size 1, block 65536, 8 blocks: [8, 8)), with off + size up to 2^64 everywhere; at the test commit it fails ([8, 0) there, and wrapped ends in the random cases), and at the head it passes. The fix (e9bb47aa) takes the end from the range's last byte, off + size - 1, plus one, clamped, in the shift and the division alike; the comment states the bound (off + size up to 2^64).
  • L4 (#issuecomment-63378), the warning after allocation and bind: fixed in e9bb47aa. It moved to sparse_buffer_for, right after create_sparse_buffer reads a new sparse buffer's requirements (spares reuse a created buffer's requirements), before any allocation or bind. No game-free check reaches that path (it needs a Vulkan device); vk_memory.cpp compiled (syntax) with the game build's own command.
  • L5 (#issuecomment-63379), a plan that can show a gain: Still to test now asks for scripts/dev.py ab (pinned options, warm-up, interleaved runs) in Kokiri Forest and the Market, main @ f1f2dc5b against this PR, with replay time, average FPS, 1% lows, median and p99 frame times, RAM and VRAM, and each log's vk: sparse mirrors line to confirm sparse mirrors are on in both builds. The runs themselves are pending for the author.
  • L6 (item 6 of the review), checklist: the source-reading checks box (ticked: tests/test_block_span_shift.py is updated, not weakened) and the Ready to merge section are back in the description.

The description's Summary, Testing, Measurements and Risks say what changed (the end's formula, where the log line is) and what ran at the new head; it still says plainly that nothing is measured in play and that the change has no switch.

Thanks for the review. The branch is rebased on main @ 40bfceb0 (the PR's own commit unchanged, `git range-diff`), with a test commit and a fix commit on top; head e9bb47aa. The checks below ran in build jobs `fb-2002-1` (on f1f2dc5b, with the mutants and the old heads) and `fb-2002-2` (on 85a59e6f) and `fb-2002-3` (on 40bfceb0, website files only since) of this PC's job queue (Windows 11, llvm-mingw clang 23). Nothing has run in the game yet. - **L1** ([#issuecomment-63375](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2002#issuecomment-63375)), runner and `{cxx}`: fixed in 8dd8626d. The check no longer declares `RUN_ALL_ARGS`, so `tests/run_all.py` no longer looks the compiler up on PATH first; the check finds it itself, one way for a direct run and the suite's: `--cxx`, `CXX`, `LONGSHOT_MINGW`'s clang++ (through `third_party/a64-kit/tools/toolchain.py`, linked static so the fixture runs without the toolchain's DLLs), then PATH. Run through `tests/run_all.py --strict --exact` with no compiler on PATH, `CXX` unset and `LONGSHOT_MINGW` set, the check failed at the PR's old head 45320fc7 (`not found: cxx (--strict)`) and passes at the new head. - **L2** ([#issuecomment-63376](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2002#issuecomment-63376)), division at one end still passes: fixed in 8dd8626d. The source-text check is gone; `block_span` is cut from the header (`tests/srccut.py`) and compiled again with its `uint64_t` a type that counts divisions, then run with blocks given at run time: a power-of-two block must take none, for either end. A copy of the head with one end divided again fails (1 division for a power-of-two block), and main's code fails (2). This counts the operations the source asks for, not the emitted instructions (a compiler may still turn a division by a known power of two into a shift on its own). - **L3** ([#issuecomment-63377](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2002#issuecomment-63377)), the end wrapping near 2^64: fixed. The test (8dd8626d) takes its expected span from 128-bit arithmetic and adds edge cases written out by hand, yours among them (off = 2^64 - 2, size 1, block 65536, 8 blocks: [8, 8)), with off + size up to 2^64 everywhere; at the test commit it fails ([8, 0) there, and wrapped ends in the random cases), and at the head it passes. The fix (e9bb47aa) takes the end from the range's last byte, `off + size - 1`, plus one, clamped, in the shift and the division alike; the comment states the bound (off + size up to 2^64). - **L4** ([#issuecomment-63378](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2002#issuecomment-63378)), the warning after allocation and bind: fixed in e9bb47aa. It moved to `sparse_buffer_for`, right after `create_sparse_buffer` reads a new sparse buffer's requirements (spares reuse a created buffer's requirements), before any allocation or bind. No game-free check reaches that path (it needs a Vulkan device); `vk_memory.cpp` compiled (syntax) with the game build's own command. - **L5** ([#issuecomment-63379](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2002#issuecomment-63379)), a plan that can show a gain: Still to test now asks for `scripts/dev.py ab` (pinned options, warm-up, interleaved runs) in Kokiri Forest and the Market, main @ f1f2dc5b against this PR, with replay time, average FPS, 1% lows, median and p99 frame times, RAM and VRAM, and each log's `vk: sparse mirrors` line to confirm sparse mirrors are on in both builds. The runs themselves are pending for the author. - **L6** (item 6 of the review), checklist: the source-reading checks box (ticked: `tests/test_block_span_shift.py` is updated, not weakened) and the Ready to merge section are back in the description. The description's Summary, Testing, Measurements and Risks say what changed (the end's formula, where the log line is) and what ran at the new head; it still says plainly that nothing is measured in play and that the change has no switch.
Author
Contributor

@Beanstalk This makes the renderer's sparse VRAM mirrors find the blocks a range reaches with a shift instead of two divisions, on the buffer bindings' path (gpu/vk/sparse_mirror.h); a block size that is not a power of two, which should never happen, is logged. No switch: a revert gives back main's code. It has not run in the game on these PCs yet. Please play Kokiri Forest and Hyrule Field, two minutes each, the PR against main on the same PC, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main, no crash, no validation error and no "not a power of two" line; frame time p99 equal to main's or better, within the run-to-run spread.

@Beanstalk This makes the renderer's sparse VRAM mirrors find the blocks a range reaches with a shift instead of two divisions, on the buffer bindings' path (gpu/vk/sparse_mirror.h); a block size that is not a power of two, which should never happen, is logged. No switch: a revert gives back main's code. It has not run in the game on these PCs yet. Please play Kokiri Forest and Hyrule Field, two minutes each, the PR against main on the same PC, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main, no crash, no validation error and no "not a power of two" line; frame time p99 equal to main's or better, within the run-to-run spread.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
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
CI / Website build and checks (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) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 20s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/vk-sparse-block-span:siantm-perf/vk-sparse-block-span'
git switch 'siantm-perf/vk-sparse-block-span'
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!2002
No description provided.