WIP: perf(vk): a program keeps its vertex stage's place for vertex_info #2056

Draft
siantm wants to merge 1 commit from siantm/Longshot:perf/vk-vertex-place into main
Contributor

Summary

vertex_info (gpu/vk/vk_draw.cpp: the stage a graphics pipeline's vertex input comes from) walked the program's stages at every draw that missed pipe_fast, reading the first line of each stage (about 840 bytes apart). Its first call now finds the place and keeps it in the program (Program::vertex_at, gpu/vk/vk_shader.h, -1 until looked for); later calls read it. A program's stages never change once it is made, so the answer is the same; the result expected was fewer cache misses a draw, but a micro-benchmark shows no gain in time (Measurements).

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

From f61b2820 on exp/x86-asm-sync.

Read first: vertex_info in gpu/vk/vk_draw.cpp and Program::vertex_at in gpu/vk/vk_shader.h.

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head b92f4ef7 on main @ 75317e1f. Every check ran through tests/run_all.py --strict --exact in build jobs of the PC's job queue, in a hidden console (log t-dc3-vk-vertex-place):

  • Its own: tests/test_vertex_at.py (new: cuts vertex_info and runs it over 20,000 random stage lists against the scan it was); tests/test_fallback_pipeline_state.py and tests/test_pipeline_key_values.py (their stand-in Programs gain vertex_at, as they cut vertex_info): pass.
  • The 166 checks that read a file this change touches (--changed 75317e1f; its own are among them): 151 passed, 15 failed. Failed: tests/test_sampler_admission.py, tests/test_fsr_uniform_ownership.py, tests/test_pipeline_play_cache.py, tests/test_draw_stream_visibility.py, tests/test_device_lost_exit.py, tests/test_draw_binds.py, tests/test_dev_packs.py, tests/test_frame_overlap.py, tests/test_linux_pressure.py, tests/test_mesh_conv.py, tests/test_prepare_cache_save.py, tests/test_recorder_own_idle.py, tests/test_upscaler_rows.py, tests/test_vk_occlusion_queries.py: each fails on main @ 75317e1f too (the same checks on main in the same queue batch, log t-dc3-base). tests/test_recording_admission.py fails on main @ 75317e1f too, now and then: its "worker slot ownership" case failed on main in 7 of 10 runs made alternately with this branch's (logs t-dc5-base-ra-*), with the same message.

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

Still to test:

  • Windows 11, i7-9700, RTX 4060 (this PC): start in Kokiri Forest from the scene's save, play a minute (walking, turning the camera), main @ 75317e1f against this PR, each warmed a minute in the scene first, then interleaved (main, this PR, this PR, main), two runs each, expecting the same picture and the replay's time a frame the same as or lower than main's (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_vertex_at.py, tests/test_fallback_pipeline_state.py, tests/test_pipeline_key_values.py with --cxx g++ and --cxx clang++, expecting PASS (by hand)

Measurements

Verdict: no gain: the kept place is slower than the walk in 9 of the 10 cases (0.90-0.97x, 0.8-12 ns a call more) and level in one (cold, vertex third: 1.02x in the baseline build). On these figures the PR saves nothing; it is worth keeping only if the in-game A/B shows a gain, which these figures do not predict. A micro-benchmark on authored input; not measured in play.

Micro-benchmark (vk_vertex_place_bench.py: a script on this PC, not in this PR): vertex_info cut out of vk_draw.cpp at both commits, followed by what key_inputs reads of the Info it gives (the 16 generic loads); vk_shader.h's Stage with Eden's real Shader::Info (848 bytes a stage), the Program with the head's vertex_at; three rounds run alone in this PC's job queue (2026-11-22, 01:21-01:23 UTC; log bench-vk-walks.log), the builder's llvm-mingw clang 23.1.5 at -O2 for baseline x86-64 and at -O2 -march=native (this PC's i7-9700, as the game is built here), 301 repetitions a case, old and new taking turns on the same authored inputs, their results equal at every repetition. Each cell: ns per call, the median of the three rounds' medians, in parentheses the lowest and highest of them; Δ: old over new (above 1, this PR is quicker).

Case (input; build) Before: main @ 75317e1f After: this PR @ b92f4ef7 Δ (old/new)
hot-vertex-first: 16 programs in cache, vertex stage first of 2; 4,096 calls; x86-64 baseline 26.44 ns (26.44-26.61) 29.27 ns (29.27-29.32) 0.90x
hot-vertex-first: 16 programs in cache, vertex stage first of 2; 4,096 calls; -march=native 28.69 ns (28.69-29.05) 30.57 ns (30.15-30.61) 0.94x
hot-fallback-third: in cache, vertex stage third of 4; 4,096 calls; x86-64 baseline 27.93 ns (27.56-27.93) 30.47 ns (30.47-30.49) 0.92x
hot-fallback-third: in cache, vertex stage third of 4; 4,096 calls; -march=native 30.03 ns (29.98-30.15) 30.98 ns (30.00-31.35) 0.97x
cold-vertex-first: 256 programs flushed from every cache, vertex first; 256 calls; x86-64 baseline 119 ns (117-120) 123 ns (123-123) 0.97x
cold-vertex-first: 256 programs flushed from every cache, vertex first; 256 calls; -march=native 120 ns (118-132) 133 ns (129-143) 0.91x
cold-fallback-third: flushed, vertex third of 4; 256 calls; x86-64 baseline 127 ns (127-127) 124 ns (124-124) 1.02x
cold-fallback-third: flushed, vertex third of 4; 256 calls; -march=native 129 ns (127-130) 133 ns (132-134) 0.97x
hot-no-vertex: in cache, compute-only programs (no vertex stage); 4,096 calls; x86-64 baseline 27.61 ns (27.61-27.61) 29.54 ns (29.54-29.54) 0.93x
hot-no-vertex: in cache, compute-only programs (no vertex stage); 4,096 calls; -march=native 29.61 ns (29.61-29.64) 30.42 ns (30.08-30.42) 0.97x

Risks

A program whose stages changed after its first draw would keep a stale place: none does (a program is made whole before it is ready). What could show is a draw with the wrong vertex input; a revert is enough (no switch). The renderer is compiled again after updating; no cache or setting changes (the place is not saved with the program).

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: only the checks' cuts compiled (Testing).
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
## Summary `vertex_info` (`gpu/vk/vk_draw.cpp`: the stage a graphics pipeline's vertex input comes from) walked the program's stages at every draw that missed `pipe_fast`, reading the first line of each stage (about 840 bytes apart). Its first call now finds the place and keeps it in the program (`Program::vertex_at`, `gpu/vk/vk_shader.h`, -1 until looked for); later calls read it. A program's stages never change once it is made, so the answer is the same; the result expected was fewer cache misses a draw, but a micro-benchmark shows no gain in time (Measurements). No switch; a revert gives back main's code. From f61b2820 on `exp/x86-asm-sync`. Read first: `vertex_info` in `gpu/vk/vk_draw.cpp` and `Program::vertex_at` in `gpu/vk/vk_shader.h`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head b92f4ef7 on main @ 75317e1f. Every check ran through `tests/run_all.py --strict --exact` in build jobs of the PC's job queue, in a hidden console (log `t-dc3-vk-vertex-place`): - Its own: `tests/test_vertex_at.py` (new: cuts `vertex_info` and runs it over 20,000 random stage lists against the scan it was); `tests/test_fallback_pipeline_state.py` and `tests/test_pipeline_key_values.py` (their stand-in Programs gain `vertex_at`, as they cut `vertex_info`): pass. - The 166 checks that read a file this change touches (`--changed 75317e1f`; its own are among them): 151 passed, 15 failed. Failed: `tests/test_sampler_admission.py`, `tests/test_fsr_uniform_ownership.py`, `tests/test_pipeline_play_cache.py`, `tests/test_draw_stream_visibility.py`, `tests/test_device_lost_exit.py`, `tests/test_draw_binds.py`, `tests/test_dev_packs.py`, `tests/test_frame_overlap.py`, `tests/test_linux_pressure.py`, `tests/test_mesh_conv.py`, `tests/test_prepare_cache_save.py`, `tests/test_recorder_own_idle.py`, `tests/test_upscaler_rows.py`, `tests/test_vk_occlusion_queries.py`: each fails on main @ 75317e1f too (the same checks on main in the same queue batch, log `t-dc3-base`). `tests/test_recording_admission.py` fails on main @ 75317e1f too, now and then: its "worker slot ownership" case failed on main in 7 of 10 runs made alternately with this branch's (logs `t-dc5-base-ra-*`), with the same message. It has NOT run in the game on this PC yet. **Still to test:** - Windows 11, i7-9700, RTX 4060 (this PC): start in Kokiri Forest from the scene's save, play a minute (walking, turning the camera), main @ 75317e1f against this PR, each warmed a minute in the scene first, then interleaved (main, this PR, this PR, main), two runs each, expecting the same picture and the replay's time a frame the same as or lower than main's (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_vertex_at.py`, `tests/test_fallback_pipeline_state.py`, `tests/test_pipeline_key_values.py` with `--cxx g++` and `--cxx clang++`, expecting PASS (by hand) ## Measurements **Verdict:** no gain: the kept place is slower than the walk in 9 of the 10 cases (0.90-0.97x, 0.8-12 ns a call more) and level in one (cold, vertex third: 1.02x in the baseline build). On these figures the PR saves nothing; it is worth keeping only if the in-game A/B shows a gain, which these figures do not predict. A micro-benchmark on authored input; not measured in play. Micro-benchmark (`vk_vertex_place_bench.py`: a script on this PC, not in this PR): `vertex_info` cut out of `vk_draw.cpp` at both commits, followed by what `key_inputs` reads of the Info it gives (the 16 generic loads); vk_shader.h's `Stage` with Eden's real `Shader::Info` (848 bytes a stage), the Program with the head's `vertex_at`; three rounds run alone in this PC's job queue (2026-11-22, 01:21-01:23 UTC; log `bench-vk-walks.log`), the builder's llvm-mingw clang 23.1.5 at -O2 for baseline x86-64 and at -O2 -march=native (this PC's i7-9700, as the game is built here), 301 repetitions a case, old and new taking turns on the same authored inputs, their results equal at every repetition. Each cell: ns per call, the median of the three rounds' medians, in parentheses the lowest and highest of them; Δ: old over new (above 1, this PR is quicker). | Case (input; build) | Before: main @ 75317e1f | After: this PR @ b92f4ef7 | Δ (old/new) | |---|---|---|---| | hot-vertex-first: 16 programs in cache, vertex stage first of 2; 4,096 calls; x86-64 baseline | 26.44 ns (26.44-26.61) | 29.27 ns (29.27-29.32) | 0.90x | | hot-vertex-first: 16 programs in cache, vertex stage first of 2; 4,096 calls; -march=native | 28.69 ns (28.69-29.05) | 30.57 ns (30.15-30.61) | 0.94x | | hot-fallback-third: in cache, vertex stage third of 4; 4,096 calls; x86-64 baseline | 27.93 ns (27.56-27.93) | 30.47 ns (30.47-30.49) | 0.92x | | hot-fallback-third: in cache, vertex stage third of 4; 4,096 calls; -march=native | 30.03 ns (29.98-30.15) | 30.98 ns (30.00-31.35) | 0.97x | | cold-vertex-first: 256 programs flushed from every cache, vertex first; 256 calls; x86-64 baseline | 119 ns (117-120) | 123 ns (123-123) | 0.97x | | cold-vertex-first: 256 programs flushed from every cache, vertex first; 256 calls; -march=native | 120 ns (118-132) | 133 ns (129-143) | 0.91x | | cold-fallback-third: flushed, vertex third of 4; 256 calls; x86-64 baseline | 127 ns (127-127) | 124 ns (124-124) | 1.02x | | cold-fallback-third: flushed, vertex third of 4; 256 calls; -march=native | 129 ns (127-130) | 133 ns (132-134) | 0.97x | | hot-no-vertex: in cache, compute-only programs (no vertex stage); 4,096 calls; x86-64 baseline | 27.61 ns (27.61-27.61) | 29.54 ns (29.54-29.54) | 0.93x | | hot-no-vertex: in cache, compute-only programs (no vertex stage); 4,096 calls; -march=native | 29.61 ns (29.61-29.64) | 30.42 ns (30.08-30.42) | 0.97x | ## Risks A program whose stages changed after its first draw would keep a stale place: none does (a program is made whole before it is ready). What could show is a draw with the wrong vertex input; a revert is enough (no switch). The renderer is compiled again after updating; no cache or setting changes (the place is not saved with the program). ## 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: only the checks' cuts compiled (Testing). - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers.
perf(vk): a program keeps its vertex stage's place for vertex_info
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_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 29s
beanstalk/approval waiting for a BigBean's 👍 on the request
b92f4ef7af
vertex_info (the stage a graphics pipeline's vertex input comes from) walked the program's stages at every draw that
missed pipe_fast, reading the first line of each ~840-byte Stage. The first call now finds the place and keeps it in
the program (Program::vertex_at, -1 until looked for); later calls read it. A program's stages never change once it
is made, so the answer is the same.

Branch measurement (f61b2820, the batch "per-draw cache misses overlapped or removed"): part of draw()'s cache misses;
no number of its own. The batch was measured in play (cc4c6c42): no difference shown. Not measured in play alone.

Test: tests/test_vertex_at.py cuts vertex_info and runs it over 20,000 random stage lists against the scan it was.
test_fallback_pipeline_state.py and test_pipeline_key_values.py stand-in Programs gain vertex_at (they cut
vertex_info). No switch.

Assisted-by: Claude Code
Author
Contributor

@Beanstalk This changes the renderer's pipeline lookups for draws past the fast path (gpu/vk). 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, device lost, validation error or FATAL line; frame time p99 equal to main's or better, within the run-to-run spread.

@Beanstalk This changes the renderer's pipeline lookups for draws past the fast path (gpu/vk). 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, device lost, validation error or FATAL 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.
Author
Contributor

@Beanstalk This replaces my request 65769 on this head, so that it follows the farm's picture and hardware rules. This makes a program keep its vertex stage's place for vertex_info, on the pipeline lookups of draws past the fast path (gpu/vk). No switch: a revert gives back main's code. Please start from the kokiri-start save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line.

@Beanstalk This replaces my request 65769 on this head, so that it follows the farm's picture and hardware rules. This makes a program keep its vertex stage's place for vertex_info, on the pipeline lookups of draws past the fast path (gpu/vk). No switch: a revert gives back main's code. Please start from the kokiri-start save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line.
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.
Author
Contributor

Measurements: a micro-benchmark on authored input (old against new, three rounds, x86-64 baseline and -march=native; not measured in play) and the Verdict rewritten to match: no gain; the Summary's expectation sentence says so.

Measurements: a micro-benchmark on authored input (old against new, three rounds, x86-64 baseline and -march=native; not measured in play) and the Verdict rewritten to match: no gain; the Summary's expectation sentence says so.
Author
Contributor

@Beanstalk One correction to my request 69700 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.

@Beanstalk One correction to my request 69700 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.
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.
Contributor

@siantm — your own Measurements say "no gain: the kept place is slower than the walk in 9 of the 10 cases (0.90-0.97x …). On these figures the PR saves nothing", and the PR has not run in the game yet.
Do you want to close/drop #2056 rather than carry it further, or is a second measurement (or the in-game A/B) planned?
No action from us either way; just flagging it so it does not sit in the queue looking like a merge candidate.

@siantm — your own Measurements say "no gain: the kept place is slower than the walk in 9 of the 10 cases (0.90-0.97x …). On these figures the PR saves nothing", and the PR has not run in the game yet. Do you want to close/drop #2056 rather than carry it further, or is a second measurement (or the in-game A/B) planned? No action from us either way; just flagging it so it does not sit in the queue looking like a merge candidate.
Author
Contributor

@dazemc Thanks, and that reading is right: on the micro-benchmark this saves nothing (the kept place is slower than the walk in 9 of 10 cases). One more measurement is planned before I decide: an in-game comparison of main with and without this change (8 runs a side, Kokiri Forest, read on draw and game time), which waits for its build. If that shows no gain either, I will close this. It is not a merge candidate as it stands.

@dazemc Thanks, and that reading is right: on the micro-benchmark this saves nothing (the kept place is slower than the walk in 9 of 10 cases). One more measurement is planned before I decide: an in-game comparison of main with and without this change (8 runs a side, Kokiri Forest, read on draw and game time), which waits for its build. If that shows no gain either, I will close this. It is not a merge candidate as it stands.
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_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 29s
beanstalk/approval waiting for a BigBean's 👍 on the request
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-vertex-place:siantm-perf/vk-vertex-place'
git switch 'siantm-perf/vk-vertex-place'
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!2056
No description provided.