WIP: feat(vk): programs whose layouts are defined alike can share one (RX_VK_SHARED_LAYOUTS, off) #2046

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

Summary

bind_descriptors (gpu/vk/vk_draw.cpp) pushes only the descriptor writes that changed while the pipeline layout is the one it pushed with. Each program has its own VkPipelineLayout, so every switch to another program pushes its whole set again. This adds an experiment, off by default: with RX_VK_SHARED_LAYOUTS=1, ensure_layout takes its layouts from layout_take: one descriptor set layout and one pipeline layout per definition (the bindings in order with their types, counts and stages, the push flag, the push constant stages), counted, and destroyed with their last program by layout_release, which the three places that let a program's layouts go now call (prepare_run's warm of compute programs; program_cache_borrow and program_cache_borrow_all in gpu/vk/vk_progcache.cpp through program_layout_release). A layout not made whole stays the program's own. The log says vk: shared layouts: N definitions for M programs at 64, 128, 256 ... programs. The result expected with it on is fewer full pushes when draws switch between programs defined alike. make_layouts (the calls ensure_layout made, moved) now checks the driver's results: a set layout or pipeline layout the driver did not make is logged and left VK_NULL_HANDLE (Vulkan leaves a failed create's handle undefined), and a set layout not made makes no pipeline layout; on main those results were not checked.

Switch: RX_VK_SHARED_LAYOUTS (kind attempt, off by default, the branch's default; its row in gpu/vk/switches.def). Unset or =0: a layout a program, main's code (make_layouts, the same calls ensure_layout made). The switch is read once, at its first use (so HOS_AB cannot alternate it); any value but 0, false, off or no turns it on; with it on, its first read logs so.

From 2c9bb476 on exp/x86-asm-sync.

Read first: layout_key, LayoutShare, layout_take, layout_release and ensure_layout in gpu/vk/vk_draw.cpp; the release in gpu/vk/vk_progcache.cpp.

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head 364d2d05 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-shared-layouts):

  • Its own: tests/test_shared_layouts.py (new: make_layouts, layout_key, LayoutShare, layout_take and layout_release with a counting driver (a create that fails leaves no handle): same definition, same objects made once; each difference, others; the last release destroys once; a layout not made whole stays the program's own; and the call sites; and the switch's reader, cut from the source, runs in fresh processes: unset, 0, off, false, 1, on and another value, twice each, and set to the other answer after its first read): pass.
  • The 187 checks that read a file this change touches (--changed 75317e1f; its own are among them): 167 passed, 20 failed. Failed: tests/test_recorder_sync.py, tests/test_fsr_uniform_ownership.py, tests/test_sampler_admission.py, tests/test_pipeline_play_cache.py, tests/test_draw_stream_visibility.py, tests/test_device_lost_exit.py, tests/test_dev_packs.py, tests/test_draw_binds.py, tests/test_dev_ab.py, tests/test_crash_dialog.py, tests/test_frame_overlap.py, tests/test_linux_pressure.py, tests/test_mesh_conv.py, tests/test_prepare_cache_save.py, tests/test_present_queue_default.py, tests/test_recorder_own_idle.py, tests/test_target_reuse_memory.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 also failed in that run (its parallel case, "worker slot ownership") and passed when run again on this head (log t-dc4-vk-shared-layouts); main passed it in that batch and in five runs of the next (logs t-dc4-base-ra1 to t-dc4-base-ra5).

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, this PR with RX_VK_SHARED_LAYOUTS=0 and this PR with it =1, each warmed a minute in the scene first, then interleaved (main, =0, =1, =1, =0, main), two runs each, expecting the same picture all three ways, =0 as main, the shared layouts log line with =1, and with =1 the replay's time a frame the same as or lower than =0's and 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 with RX_VK_SHARED_LAYOUTS=0 and =1, 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 with RX_VK_SHARED_LAYOUTS=0 and =1, ABBA, the same picture)
  • Linux (any GPU): build the renderer and run tests/test_shared_layouts.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. RX_VK_SHARED_LAYOUTS ships off (kind attempt), the branch's default; it is not measured on main.

Risks

Off by default: unset, nothing changes. On: a layout destroyed while a program still used it (a crash or a device loss), or two programs given one layout though their definitions differ (wrong bindings); the count and the key are what the test checks. program_cache_borrow_all's release goes through layout_release either way (unset it destroys as before). The renderer is compiled again after updating. A layout the driver does not make is now logged and left null (on main its handle was used as the driver left it); what a program does without its layout is as on main.

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 switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def. Its row is in; not measured yet.
  • 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 `bind_descriptors` (`gpu/vk/vk_draw.cpp`) pushes only the descriptor writes that changed while the pipeline layout is the one it pushed with. Each program has its own `VkPipelineLayout`, so every switch to another program pushes its whole set again. This adds an experiment, off by default: with `RX_VK_SHARED_LAYOUTS=1`, `ensure_layout` takes its layouts from `layout_take`: one descriptor set layout and one pipeline layout per definition (the bindings in order with their types, counts and stages, the push flag, the push constant stages), counted, and destroyed with their last program by `layout_release`, which the three places that let a program's layouts go now call (`prepare_run`'s warm of compute programs; `program_cache_borrow` and `program_cache_borrow_all` in `gpu/vk/vk_progcache.cpp` through `program_layout_release`). A layout not made whole stays the program's own. The log says `vk: shared layouts: N definitions for M programs` at 64, 128, 256 ... programs. The result expected with it on is fewer full pushes when draws switch between programs defined alike. `make_layouts` (the calls `ensure_layout` made, moved) now checks the driver's results: a set layout or pipeline layout the driver did not make is logged and left `VK_NULL_HANDLE` (Vulkan leaves a failed create's handle undefined), and a set layout not made makes no pipeline layout; on main those results were not checked. Switch: `RX_VK_SHARED_LAYOUTS` (kind attempt, off by default, the branch's default; its row in `gpu/vk/switches.def`). Unset or `=0`: a layout a program, main's code (`make_layouts`, the same calls `ensure_layout` made). The switch is read once, at its first use (so `HOS_AB` cannot alternate it); any value but `0`, `false`, `off` or `no` turns it on; with it on, its first read logs so. From 2c9bb476 on `exp/x86-asm-sync`. Read first: `layout_key`, `LayoutShare`, `layout_take`, `layout_release` and `ensure_layout` in `gpu/vk/vk_draw.cpp`; the release in `gpu/vk/vk_progcache.cpp`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head 364d2d05 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-shared-layouts`): - Its own: `tests/test_shared_layouts.py` (new: `make_layouts`, `layout_key`, `LayoutShare`, `layout_take` and `layout_release` with a counting driver (a create that fails leaves no handle): same definition, same objects made once; each difference, others; the last release destroys once; a layout not made whole stays the program's own; and the call sites; and the switch's reader, cut from the source, runs in fresh processes: unset, `0`, `off`, `false`, `1`, `on` and another value, twice each, and set to the other answer after its first read): pass. - The 187 checks that read a file this change touches (`--changed 75317e1f`; its own are among them): 167 passed, 20 failed. Failed: `tests/test_recorder_sync.py`, `tests/test_fsr_uniform_ownership.py`, `tests/test_sampler_admission.py`, `tests/test_pipeline_play_cache.py`, `tests/test_draw_stream_visibility.py`, `tests/test_device_lost_exit.py`, `tests/test_dev_packs.py`, `tests/test_draw_binds.py`, `tests/test_dev_ab.py`, `tests/test_crash_dialog.py`, `tests/test_frame_overlap.py`, `tests/test_linux_pressure.py`, `tests/test_mesh_conv.py`, `tests/test_prepare_cache_save.py`, `tests/test_present_queue_default.py`, `tests/test_recorder_own_idle.py`, `tests/test_target_reuse_memory.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` also failed in that run (its `parallel` case, "worker slot ownership") and passed when run again on this head (log `t-dc4-vk-shared-layouts`); main passed it in that batch and in five runs of the next (logs `t-dc4-base-ra1` to `t-dc4-base-ra5`). 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, this PR with `RX_VK_SHARED_LAYOUTS=0` and this PR with it =1, each warmed a minute in the scene first, then interleaved (main, =0, =1, =1, =0, main), two runs each, expecting the same picture all three ways, =0 as main, the `shared layouts` log line with =1, and with =1 the replay's time a frame the same as or lower than =0's and 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 with `RX_VK_SHARED_LAYOUTS=0` and `=1`, 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 with RX_VK_SHARED_LAYOUTS=0 and =1, ABBA, the same picture) - Linux (any GPU): build the renderer and run `tests/test_shared_layouts.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. `RX_VK_SHARED_LAYOUTS` ships off (kind attempt), the branch's default; it is not measured on main. ## Risks Off by default: unset, nothing changes. On: a layout destroyed while a program still used it (a crash or a device loss), or two programs given one layout though their definitions differ (wrong bindings); the count and the key are what the test checks. `program_cache_borrow_all`'s release goes through `layout_release` either way (unset it destroys as before). The renderer is compiled again after updating. A layout the driver does not make is now logged and left null (on main its handle was used as the driver left it); what a program does without its layout is as on main. ## 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 - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. Its row is in; not measured yet. - [ ] 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.
feat(vk): programs whose layouts are defined alike can share one (RX_VK_SHARED_LAYOUTS, off)
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
PR description / Description follows the template (pull_request_target) Successful in 5s
Website / Website on Cloudflare (pull_request_target) Has been skipped
364d2d05f9
bind_descriptors pushes only the descriptor writes that changed while the pipeline layout is the one it pushed with.
Each program has its own VkPipelineLayout, so every switch to another program pushes its whole set again: in the
branch's profiles of Kokiri Forest (2c9bb476) most of vkCmdPushDescriptorSetKHR's time was such full pushes.

With RX_VK_SHARED_LAYOUTS=1, ensure_layout takes its layouts from layout_take: one descriptor set layout and one
pipeline layout per definition (the bindings in order with their types, counts and stages, the push flag, the push
constant stages), counted, and destroyed with their last program by layout_release, which the three places that let a
program's layouts go now call (prepare_run's warm_compute; program_cache_borrow and program_cache_borrow_all through
program_layout_release). A layout not made whole stays the program's own. make_layouts (ensure_layout's calls, moved)
now checks the driver's results: a set layout or a pipeline layout not made is logged and left VK_NULL_HANDLE (a
failed create leaves its handle undefined), and a set layout not made makes no pipeline layout. Off: a layout a
program, as before (make_layouts, the same calls ensure_layout made). Logs `vk: shared layouts: N definitions for M
programs` at 64, 128, 256 ... programs.

Switch: RX_VK_SHARED_LAYOUTS, attempt, off by default (the branch's default: in play on the branch, d1944959, it showed
no fps gain beyond the runs' spread). Read once, at its first use; any value but 0, false, off or no is on. Row in
gpu/vk/switches.def. Not measured on main.

Test: tests/test_shared_layouts.py cuts make_layouts, layout_key, LayoutShare, layout_take and layout_release and runs
them with a counting driver (same definition: same objects, made once; each difference: others; the last release
destroys once; a layout not made whole stays the program's own; a create that fails leaves no handle), runs the
switch's reader in fresh processes (unset, 0, off, false, 1, on and another value, twice each, set again after its
first read), and reads ensure_layout, the release sites and bind_descriptors.

Assisted-by: Claude Code
Member

Possibly related, for context:

  • PR #800 WIP: perf(vk): programs with the same bindings share one set layout and pipeline layout, so a program change pushes only what changed — Same Vulkan shared-layout performance change; overlapping implementation
Possibly related, for context: - PR #800 WIP: perf(vk): programs with the same bindings share one set layout and pipeline layout, so a program change pushes only what changed — Same Vulkan shared-layout performance change; overlapping implementation
Author
Contributor

On BeanBot's note: this pull request overlaps #800 (dyst's, open first), which makes programs with the same layout definitions share one set layout and pipeline layout in the same files. It is on hold in #800's favour and gets no further push while #800 is open.

What the two diffs differ in: #800 shares by default (RX_VK_LAYOUT_EACH=1 restores a layout per program) and keeps the shared layouts for the device's life; this one is off by default (RX_VK_SHARED_LAYOUTS=1 turns it on) and counts each shared layout, destroying it with its last program, with its row in gpu/vk/switches.def.

On [BeanBot's note](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2046#issuecomment-65185): this pull request overlaps #800 (dyst's, open first), which makes programs with the same layout definitions share one set layout and pipeline layout in the same files. It is on hold in #800's favour and gets no further push while #800 is open. What the two diffs differ in: #800 shares by default (`RX_VK_LAYOUT_EACH=1` restores a layout per program) and keeps the shared layouts for the device's life; this one is off by default (`RX_VK_SHARED_LAYOUTS=1` turns it on) and counts each shared layout, destroying it with its last program, with its row in `gpu/vk/switches.def`.
Contributor

@siantm — on your note that this "overlaps #800 … and gets no further push while #800 is open": #800 is still open and unmerged, and both change the same shared set-layout/pipeline-layout path in the same files.
Would you rather close #2046 as a duplicate of #800, or keep it parked and rebase it onto #800 once #800 lands?
Either is fine; this is only so the queue shows the right owner, and nothing needs doing until you decide.

@siantm — on your note that this "overlaps #800 … and gets no further push while #800 is open": #800 is still open and unmerged, and both change the same shared set-layout/pipeline-layout path in the same files. Would you rather close #2046 as a duplicate of #800, or keep it parked and rebase it onto #800 once #800 lands? Either is fine; this is only so the queue shows the right owner, and nothing needs doing until you decide.
Author
Contributor

@dazemc Thanks for checking. It stays parked for now: nothing is pushed to it while #800 is open, and it is not a merge candidate. Whether it is closed as a duplicate of #800 or rebased onto #800 once that lands is still to be decided on my side; I will post here when it is, and close it myself if it goes.

@dazemc Thanks for checking. It stays parked for now: nothing is pushed to it while #800 is open, and it is not a merge candidate. Whether it is closed as a duplicate of #800 or rebased onto #800 once that lands is still to be decided on my side; I will post here when it is, and close it myself if it goes.
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
PR description / Description follows the template (pull_request_target) Successful in 5s
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-shared-layouts:siantm-perf/vk-shared-layouts'
git switch 'siantm-perf/vk-shared-layouts'
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!2046
No description provided.