WIP: perf(vk): a sparse mirror's block span by shifts for a power-of-two block #2002
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!2002
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/vk-sparse-block-span"
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?
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_spannow 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 - 1wrapped 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_spaningpu/vk/sparse_mirror.h; the new log line insparse_buffer_for(gpu/vk/vk_memory.cpp).Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head
e9bb47aaon main @40bfceb0(perf12def479, test8dd8626d, fixe9bb47aa). Build jobs of this PC's job queue, each check in a hidden console:fb-2002-1(on main @f1f2dc5b),fb-2002-2(on85a59e6f) andfb-2002-3(on40bfceb0, which adds only website files to85a59e6f):tests/test_block_span_shift.py(block_spanagainst a 128-bit expected span over 200,000 random spans, off + size up to 2^64, and edge cases written out by hand; andblock_spancut from the header with auint64_tthat counts divisions, run with blocks given at run time). At the test commit (the PR's ownblock_span; 7eee4ead on85a59e6f) 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).tests/run_all.py --strict --exactwith no compiler on PATH,CXXunset andLONGSHOT_MINGWset: at the PR's old head45320fc7it fails (not found: cxx (--strict)); at the head it passes.tests/run_all.py --strict --changed origin/main; its own among them), at the head on85a59e6fand on40bfceb0: 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 @85a59e6ftoo (the same checks in fb-2002-2; most for the Vulkan headers this PC lacks under--strict;tests/test_sparse_mirror.pystops at avk_replay.cppsource check before its C++ part, on main too).gpu/vk/vk_memory.cppcompiled (syntax) with the game build's own command.It has NOT run in the game on this PC yet.
Still to test:
python -I scripts/dev.py ab --scene kokiri-forest --runs 3and the same with--scene market(a crowded scene): main @40bfceb0against 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 thevk: sparse mirrors: N, ...line of each log (N above 0 on both sides: sparse mirrors are on); expecting the same picture, nonot a power of twoline, and a gain only where this PR's median and p99 frame times are lower than main's beyond the runs' spread (by hand)tests/test_block_span_shift.pywith--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
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). Not yet: only the checks in Testing ran.main, or Measurements says why there is nothing to measure. Not yet (Measurements).third_party/untouched; nothing GPL-incompatible.When it applies
tests/that reads changed source is updated, not weakened.vk_memory.cppcompiled (syntax) on Windows only (Testing).Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Possibly related, for context:
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.
@fixwill do (6)tests/test_block_span_shift.py:21: The runner resolves {cxx} before launching the test. (more on the line)tests/test_block_span_shift.py:74: Restoring division for either endpoint still passes every numeric comparison and leaves one>> shfor the text check to find. (more on the line)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)gpu/vk/vk_memory.cpp:4386: The alignment warning runs only after sparse allocation and binding succeed. (more on the line)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.For the author (1) (a run, a measurement or a decision)
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 between45320fc7and75317e1f. 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:
45320fc782against75317e1f31: 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:
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.
@ -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)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;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)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 blockRUN_ALL_ARGS = ["--cxx", "{cxx}"]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:L2 (minor): Restoring division for either endpoint still passes every numeric comparison and leaves one
>> shfor 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.45320fc782e9bb47aa46Thanks 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; heade9bb47aa. The checks below ran in build jobsfb-2002-1(onf1f2dc5b, with the mutants and the old heads) andfb-2002-2(on85a59e6f) andfb-2002-3(on40bfceb0, website files only since) of this PC's job queue (Windows 11, llvm-mingw clang 23). Nothing has run in the game yet.{cxx}: fixed in8dd8626d. The check no longer declaresRUN_ALL_ARGS, sotests/run_all.pyno 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++ (throughthird_party/a64-kit/tools/toolchain.py, linked static so the fixture runs without the toolchain's DLLs), then PATH. Run throughtests/run_all.py --strict --exactwith no compiler on PATH,CXXunset andLONGSHOT_MINGWset, the check failed at the PR's old head45320fc7(not found: cxx (--strict)) and passes at the new head.8dd8626d. The source-text check is gone;block_spanis cut from the header (tests/srccut.py) and compiled again with itsuint64_ta 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).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).e9bb47aa. It moved tosparse_buffer_for, right aftercreate_sparse_bufferreads 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.cppcompiled (syntax) with the game build's own command.scripts/dev.py ab(pinned options, warm-up, interleaved runs) in Kokiri Forest and the Market, main @f1f2dc5bagainst this PR, with replay time, average FPS, 1% lows, median and p99 frame times, RAM and VRAM, and each log'svk: sparse mirrorsline to confirm sparse mirrors are on in both builds. The runs themselves are pending for the author.tests/test_block_span_shift.pyis 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.
@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.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.