WIP: feat(vk): log-only frame cost lines, a GPU timeline and the bench's A/B exactness arm, all off by default #704
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/upscaling
beanstalk: approved @ 1697603
beanstalk: approved @ 2c5a62e
beanstalk: approved @ 4fd82b7
beanstalk: approved @ 64d6d09
beanstalk: approved @ 6f91fae
beanstalk: approved @ 70a78f9
beanstalk: approved @ 8d361dc
beanstalk: approved @ 9d8356e
beanstalk: approved @ abf2227
beanstalk: approved @ d82f202
beanstalk: approved @ e5f1e40
beanstalk: approved @ ee8ab5f
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
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
testing wanted
ci: approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!704
Loading…
Reference in a new issue
No description provided.
Delete branch "mooki/Longshot:mac/perf-instr"
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: feat · Risk: low · Tested on: macOS 26.7.1, Apple M4 Max (MoltenVK 1.4.2), arm64 and x86_64 under Rosetta · Needs: macOS, Apple silicon (a start with the switches on); Windows and Linux, any GPU (build and start with the switches off and on)
Why
What the Mac's 33 ms frame is made of could not be read from a run: there was no reason for each render pass end, no attachment
traffic, no count of what MoltenVK splits, no timing of the frame's command buffers against the CPU's clock, and no way to show a
change exact on the same frame. These are the measurement lines the macOS performance work needs before it changes anything
(
reports/2026-11-14-mac-perf.md§5). All are log-only and off by default, on every platform. It is part of the macOS port. See #420.What changed
Three switches, read once before the first replay (
gpu/vk/vk_perf.cppperf_start); off, each hook is one load of a flag and anot-taken branch, and nothing recorded or drawn changes on or off.
RX_VK_PERF_LINES=1: every 120 presents L1 (why each render pass ended, and the passes begun again on the same attachments), L2(bytes loaded and stored by the passes, leading whole clears), L3 (MoltenVK only: 8-bit indexed draws, fans, occlusion passes), L4
(NVN's discards, recorded into the command stream only with this switch as
NOP_DISCARD, and what reads a discarded target beforeit is rewritten), L6 (mesh fallback draws, their splits and batchable runs); every 600 presents L5 (readback waits by thread and
kind, and how much of each the present poll slept through).
RX_VK_GPU_TIMELINE=1|2: the recordings' existing timestamps put on the CPU's clock withVK_KHR_calibrated_timestamps(asked foronly with the switch): per frame and frame type the GPU busy and idle times, gaps, submit to start, last part to present; at 2 a
timestamp pair per pass and dispatch and each type's top spans. Bounded memory, made only with the switch; the replay runs serial
under it, as with
RX_VK_PASS_TIMING.RX_VK_BENCH_AB=pass_barrier|dedupe_off|chunk:<n>withRX_VK_BENCH: the bench's replays in blocks A, B, A, each replay's GPU timeand a 64-bit hash of the window texture after it, and whether B's picture is A's bit for bit.
gpu/vk/perf_lines.h,perf_op.h,vk_perf.{h,cpp}(new); hooks invk_draw.cpp,vk_replay.cpp,vk_texture.cpp,vk_main.cpp,vk_fps.cpp; the host:gpu/render/rx_nvn.h(NOP_DISCARD),host/nvn_native.c(discards recorded only with theswitch),
host/hos_thread.c+host/rx_backend.c(the present poll's sleep clock includes the sleep in progress; read only whilethe FPS Breakdown or these switches time sleeps, and the Breakdown reads it when the poll is not asleep, so its figures are
unchanged).
docs/development.md(the three switches). Tests:tests/test_perf_lines.py+tests/perf_lines_test.cpp(new); thefixtures that cut the touched code get
perf_lines.hor stand-ins;test_readback_part,test_fps_statsfollow.Measurements
Verdict: nothing to measure in pixels: log-only. The cost with the switches off is unmeasurable in a game run: about 1 ns a hook
call, 7.6 µs on a frame of run #25's shape against a 33 ms frame (CPU fixture, best of 3 rounds of 20 million calls).
Baseline: the same fixture with the switches on.
Hardware: macOS 26.7.1, Apple M4 Max, 16-core CPU, 64 GB (a busy Mac: other suites running)
Scene:
tests/perf_lines_test.cpp's cost section: each hook as it sits in the replay,noinline, its flag read each timeMethod:
reports/PerfInstr-files/t6-test.outHow to reproduce:
python -I -B tests/test_perf_lines.py --cxx c++ --headers <Vulkan headers>end_pass'sFlags and settings
Three new switches, off by default, log-only, every platform.
RX_VK_PERF_LINES=1vk: L1…L6linesRX_VK_GPU_TIMELINE=1or2VK_KHR_calibrated_timestamps; the replay serial; at 2 two timestamps per pass and dispatchRX_VK_BENCH_AB=pass_barrier,dedupe_offorchunk:<n>RX_VK_BENCHTesting
Tested: on macOS 26.7.1, Apple M4 Max, at this head (main
6c9edffeplus #679 and what it carries); the runs below at7f6a39f9were one commit earlier, which differs from this head only bytests/test_inline_write_order.py:tests/test_perf_lines.pywith/usr/bin/clang++andclang++ -arch x86_64(Rosetta): pass, 178 checks, 0 failures, source checksok; with the head's tests on #679's head it fails (
vk_perf.cppmissing);tests/test_fps_stats.pyandtests/test_readback_part.py(both compilers): pass,test_fps_statsfails on the base with the head's test(
reports/PubWave-files/PubLate4/proof-01/pi-*,base-pi-*);test_seg_gathersandtest_seg_xfer(--arch arm64): pass(
proof-02/pi-*); main'stests/test_inline_write_order.py(#329) with both compilers: pass after the end commit (it did notcompile at
7f6a39f9: found by the wave's suite).gpu/vk/vk_draw.cpp,vk_fps.cpp,vk_main.cpp,vk_perf.cpp,vk_replay.cpp,vk_texture.cppcompiled whole for arm64 andx86_64 with main's renderer flags (#476's, #492's and #490's macOS compile pieces merged in a scratch tree only): 12 objects, exit 0
(
reports/PubWave-files/PubLate4/compile/perf-instr/);host/hos_thread.c,host/nvn_native.c,host/rx_backend.ccompiledwhole for both arches with the macOS host flags on a scratch tree of this head + #490 + #479 (
compile/host-perf-instr.out).unifdef -U__APPLE__): every changed file differs and the new headers are included: the hooks are every platform's, offby default (
reports/PubWave-files/PubLate4/offapple-perf-instr.txt).Not tested: a game run with the switches on (runner Revision 12 lists them); not built on Windows or Linux; its test request
covers it.
Must be tested:
RX_VK_PERF_LINES=1 RX_VK_GPU_TIMELINE=1, expecting thevk: L1-L6and T1 lines, and the same frame times as without them within noise (by hand: the parent's game run).ea90d3ca, it carries #679 and what it carries) and main6c9edffethe sameway; start each to the title with no switch, expecting every line as main's; then with
RX_VK_PERF_LINES=1 RX_VK_GPU_TIMELINE=1,expecting the L lines and T1 and the same picture (by hand: test request T-PA43).
Risks
What could break: with the switches off, a flag load at each hook. With them on: atomic adds and thread-local stores per op and
draw, L4's mutex while a discarded target waits;
RX_VK_GPU_TIMELINE=2adds two timestamps per pass and dispatch (on MoltenVK twoblit encoders each: read shares, not absolute times) and makes the replay serial.
Turning it off: leave the three switches unset (the default), or revert the PR.
Compatibility: Nothing changes for players with the switches unset, on any platform. With
RX_VK_PERF_LINESthe host recordsNOP_DISCARDinto the command stream (a new op the replay skips). No file in the shader stamp changed; noMKWVK_API_VERSIONchange.
Reviewer notes
c45636d2..ea90d3ca(2 commits):cherry-pick -xofmacos/perf-instr2c20c030onto #679, the minimal base bytrial (it names count draws' and SegBind's pass ends: 7 conflicting hunks there against 16 on main). Conflicts: the include lines
(only
vk_perf.hkept: the topic's others were its base's),vk_main.cpp's device extensions (only this topic's request kept),vk_replay.cppwith main's newerinvalidate()(RX_VK_READBACK_LATE,RX_VK_ASYNCkept; L5's wait kinds in its pending branch),gf_note_present()thenperf_present_called(),test_readback_part.py(main's late-rule stand-ins kept).ea90d3ca: main'stest_inline_write_order(#329, newer than the topic's base) compilesfull_barrier, which now asksperf_lines::bench_b: the test includesperf_lines.h, as the topic did fortest_dump_readback.in place). That prints on every driver by default, so it is its own PR on main, #700; here
tests/test_perf_lines.pychecks L5's wait kinds instead. The two meet ininvalidate()(one line each, keep both) and intest_readback_part.py.39da60e1):tests/test_perf_lines.pywith #701 and#702 (their census lines between a count and the perf hook: 34438870, 05e35178), #705 (a sixth counted
site, the hook after its census: 74da414a), #703 (
provoking_noteon the count lines: eb4d0204), #707(
count_rewrite_recordssays CountDraw,fallback_record_countsays MeshFallback before its own pass end: b4732392, which alsomoves this PR's
why(CountDraw)intocount_rewrite_records).invalidate()is this head's base).Issues
See #420
Checklist
Everyone
type(scope): description) of the status line's type.main(maine090cc48).python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). (On the wave's merge39da60e1= the nine late renderer PRs' heads (fan-key592cd8d3, fan-indirectfa6dd43a, provoking-firstb8194380, perf-instrea90d3ca, readback-whole-waits203b8b7c, fix/indirect-quadsc6e0c37f, indirect-quads978079d4, fix/mesh-count0a61a9e7, mesh-provoking56c09f1b) with their sibling fixes: 570 passed, 31 failed, 18 skipped against main 6c9edffe's 531/32/17 on this Mac; the 31 failures are main's own and nothing that passes on main fails;test_collect_widenow passes; 38 new tests pass;reports/PubWave-files/PubLate4/wave4/full-39da60e1/.)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.Every scan down vk_memory.cpp's import table by start (pool_lookup's covering import and merge plan, bg_drain, prefetch_chunks, import_in_background, memory_note_gpu_write and memory_guest_imported) stopped once an import ended more than 1 GiB below its address, on the belief that no import is larger than that. pool_lookup's own merge escape leaves an import above 1 GiB over the smaller ones inside it ("left overlapping"), and the device allows imports up to max_import_size: a scan stopped at a small import inside a large one, missed the large one, and the same bytes were imported again, two memories over the same bytes. import_reach.h counts the live imports by span class; its bound is above every live span, within twice the largest. imported_add and imported_take, now the table's only ways in and out, keep the count. A scan stops only where no import further down can reach its address, so it also stops sooner than before when the imports are small. The covering-import checks in pool_lookup and import_in_background look past the last import at or below the range for the same reason. Tests: test_import_reach.py runs the actual scans with a 4 GiB import over a 16 MiB chunk, left by the merge escape (imports are recorded ranges: no payload, no Vulkan, no game). Its lookup, query, note, drain, background and prefetch cases fail on main5e072fc8and pass here, and a source check keeps every change to the table in the two helpers. test_import_windows runs pool_lookup with the helpers; test_mirror_gpu_only names the new publication point. Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]>044635ea added vma_startup_rollback (vk_vma.cpp, run after a failed replay_start), which calls vmaDestroyAllocator. vk_vma.cpp includes vk_work.h last so that RX_VK_RECORDER=defer/thread drains before its destructions, and every such boundary call in a vk_work.h file must be in RX_REC_BOUNDARIES with a drain-first macro. This one was not, and test_recorder_sync caught it ("vk_vma.cpp calls ['vmaDestroyAllocator'], which vk_work.h does not drain before"): it passes at 58c5c5aa (044635ea's parent) and fails at 044635ea. Add it to the list and to vk_work.h, next to vmaDestroyBuffer and vmaDestroyImage. With the recorder off (the default) boundary() is a relaxed load and a branch. Rollback runs before any image or buffer exists, so nothing should be held there today, but the allocator is no longer destroyed while a recorder could still hold packets. The test is unchanged (sources: 42 boundaries drained, was 41). Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]>A guest span larger than one device allocation (the cap on MoltenVK/M4 Max is 41,747,087,360 bytes) is bound as pieces: one descriptor per allocation segment, at the descriptor's own binding (reports/2026-11-14-full-range-design.md sections 4.1-4.4). This is the shader half; the draw side (descriptors, layout, gathers) comes later. - runtime_info.h: SegEntry {Storage|Table|TexBuf|ImgBuf, desc, pieces} and RuntimeInfo::segmented. - Back end: a segmented storage buffer is an array of pieces at its binding (a table count * pieces, entry k's piece j at k * pieces + j); its loads, stores and atomics call DontInline helpers made after main, which find the piece by the pieces' OpArrayLength in order, with constant indices. The global memory fallbacks sum the pieces' lengths for a segmented descriptor's bound end and branch to its piece (single, native candidate, table written out and in the loop). Texel and image buffers are arrays too, their fetches, reads, writes and atomics through helpers that pick the piece by OpImageQuerySize, and a texel buffer's size query sums the pieces. ImageQuery was declared for every module already. With RuntimeInfo::segmented empty nothing new is made: the SPIR-V is byte-identical. - vk_shader: SegSig (entries, hash) on Program, seg_sig and seg_variant; program_segmented finds or translates the variant of a plain program with plain's texture questions and answers and checks its descriptors are plain's; program_pick, mesh_program_locked, program_find_plain, loaded_variants and portable_variant keep the plain and segmented variants apart; every stage's RuntimeInfo::segmented comes from Program::seg. - vk_progcache: a segmented variant is never written to the program cache nor listed among the loaded programs. - tests/test_seg_shader.py + tests/seg_shader_ir.cpp: synthetic IR programs through the real back end, plain and segmented; structure checks, spirv-val (Vulkan 1.3, uniform buffer standard layout), and with --base-tree a byte comparison of the plain modules against an older tree. Five source-reading checks follow the new Program field and the atomics' new branch. The translator stamp changes with this (eden/, vk_shader.cpp, sizeof(Program)): every program cache is rebuilt once. Land it in the same release as the vk_api.h change that already does so. Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit b7928876dab846485f667c1c934d34d25ecef035)The interfaces the four segmented-consumer lanes (SegBind, SegXfer, TexelDesc, NullShader; reports/2026-11-14-seg-consumer-plan.md section 3) code against, frozen before they fork. Inert: no caller of anything new, no SPIR-V, layout, push range or stream change, sizeof(Program) unchanged. - vk_internal.h / vk_memory.cpp, SegMem's consumer interface: gpu_span_any() (a relaxed count of the live domains, stored in seg_commit; domains are kept for the run), gpu_span (gpu_buffer's flavour of gpu_span_bind: import buffers, no written marks, no mirror), SpanLayout::bind_stamp (gpu_bind_stamp(): a segment can be imported again with its cuts kept, so cut_gen alone never keys piece buffers), span_unused_piece's semantics ({null_storage, 0, 1}), and the SegArena API (SegArenaRef, seg_arena_take, seg_gather, seg_scatter; stub bodies after stream_alloc that give no room and log, SegBind's to fill). - Review fixes in the same interface (renderer-core review P2-3, P3.2): every piece of gpu_span_bind carries its buffer's device address (the import's or the mirror's; 0 only with bufferDeviceAddress off), and its one-piece path stores the translated guest address, not a virtual one. - vk_texture.h/.cpp: texel_view_pieces / texel_piece_views, stubs that return 1 and false (TexelDesc's to fill). - vk_shader.h: Program::presence_words, in what was padding. - Back end: PRESENCE_PUSH_OFFSET (88), Profile::emulate_null_descriptors, Bindings::presence / presence_read with the bit numbering rule. - tests: test_resolver_coverage lists gpu_span_bind / gpu_span sites too (scoped to gpu/vk: gpu/render has an rx::gpu_span of its own), checks that gpu_span follows its own gpu_buffer, and splits SITES into SITES_BASE plus one block per lane. test_segmented_domains executes the contract (the count 0, 1, 2 -> 1 on a merge, 0; gpu_span's pieces; addresses with the fixture's gpu_buffer_bind returning none as the real one does; the bind stamp; a virtual address's guest). test_seg_shader accepts a base tree of this back end's generation and compares every module. - docs/segmented-binding.md: the gate (every new behaviour Apple-only, other platforms byte-identical) and one heading per lane. Conflict on this branch (not with main): tests/test_seg_shader.py's docstring, against the array_pointer_calls paragraph #P20's msl-arrayptr port added; both kept, this paragraph first. Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 95fa06d4e6526601bd2e1bc88c2f8f5720855350)The segmented single storage array's case (plan 4.3 item 5) had no negative: the check always cut its own tree. With --source <B2 archive> it fails as required ("moltenvk.segmented_single want=set got=push"). Conflict with #P28 (read_write textures; not with main): the docstring keeps #P28's paragraph with this usage line, and main() cuts from --source's vk_draw.cpp with #P28's read_write count (as in the macOS integration, Integration 17). Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 5824f6828e21cf810dcceaa6be425a9224b15994)plaine37c097da2plaina997b3629aKosmicKrisp has no fillModeNonSolid and MoltenVK no POINT polygons, and Metal restarts an indexed strip or fan at the all-ones index whatever Vulkan's restart enable says. Before anything is fixed, count whether the game needs it (mode_census.h, Apple only; nothing built or drawn changes): - the graphics pipelines built, where st_pipelines counts them, and the draws recorded, where st_draws counts them, by NVN's polygon mode, with the first 8 LINE and POINT pipelines (program, pipeline key, topology) and programs named; - the indexed strip and fan draws with restart off; a direct one's indices are read whole, a page at a time, through memory_read_as_submitted for the index type's all-ones value (0xFF, 0xFFFF, 0xFFFFFFFF). An indirect one's range is the GPU's and a range the CPU cannot read is not guessed: both are counted as not scanned. Any other draw costs restart_off_strip's compares and its polygon mode's count on the thread's own counters. Printed with the frame statistics every 120 presents ("at present N") and at close ("at close"). The two tests that compile build_graphics and draw()'s binds from the source get a stand-in for the counts. Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 1bd30dceb348909ca03c4cf12709dccbb0900f4a)PerfInstr's full_barrier asks perf_lines::bench_b("dedupe_off") (RX_VK_BENCH_AB's B arm; false unless the bench runs it). main's test_inline_write_order (#329) compiles full_barrier cut out of vk_texture.cpp, and did not compile at this head ("use of undeclared identifier 'perf_lines'"; found by the wave's full suite: the topic's base predates the test). It now includes perf_lines.h, as the topic did for test_dump_readback's fixture; its three variants (the current code and two negative controls) run as before, both compilers. Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <[email protected]>ea90d3ca7a46eabf24e446eabf24e4040b840d97Folded into #852 (perf(macos): Mac performance on MoltenVK)
Pull request closed