vulkan: coalesce equal image layout ranges (VK-IMAGE-LAYOUT-RANGE-COALESCING) #1713

Closed
akimfomin wants to merge 1 commit from upstream-pr/1713 into main
akimfomin commented 2026-11-25 09:16:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by akimfomin on 2026-11-25T09:16:19Z.


Claimed in the coordination issue as: VK-IMAGE-LAYOUT-RANGE-COALESCING

What and why

Merge adjacent initialized rectangles with the same prior Vulkan image layout and aspect. The replay still issues the same ordered calls and synchronization scopes. Mixed first-use cells keep individual pieces. Uniform, small mixed, and singleton-only ranges retain fast paths. There is a regression case for a two-mip, two-layer stripe beside first-use cells.

DadaCap suggested a separate follow-up in #1284. This change applies to main itself, and a local combination with #1284 also passed.

Tested

On Windows, Clang 23.1.6 built and ran d3d12_state_map_test, replay_texture_barrier_test, and 96 seeded per-cell geometry cases on current main 626d1281 and on this patch. The same tests and a rollback/commit fixture passed on both arms of the combined tree with #1284 head 2c178819. The combined partial-release case uses 3 instead of 12 image barriers. The ordered fixture uses 5 instead of 14 with the same per-cell layouts and scopes. Same-layout recording allocates zero in both arms.

The earlier two-core CPU benchmark measured 640 to 2 records and 41.7–47.1% less partition time on stripes. Mostly-undefined and checkerboard partitions did not improve in that benchmark. Pinned benchmark and fixture sources

GPUs tested: none on this exact head. The linked earlier Vulkan readback used RTX 4070 Ti SUPER and tested a different revision.

AI-assisted: yes (Codex). PortBridge

Checklist

  • Based on current main and merges cleanly.
  • One topic and a separate follow-up.
  • No game inputs, keys or decryption.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5488467421 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1713). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-25T09:16:19Z. --- Claimed in the coordination issue as: [VK-IMAGE-LAYOUT-RANGE-COALESCING](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157843511) ### What and why Merge adjacent initialized rectangles with the same prior Vulkan image layout and aspect. The replay still issues the same ordered calls and synchronization scopes. Mixed first-use cells keep individual pieces. Uniform, small mixed, and singleton-only ranges retain fast paths. There is a regression case for a two-mip, two-layer stripe beside first-use cells. DadaCap suggested a separate follow-up in #1284. This change applies to main itself, and a local combination with #1284 also passed. ### Tested On Windows, Clang 23.1.6 built and ran `d3d12_state_map_test`, `replay_texture_barrier_test`, and 96 seeded per-cell geometry cases on current main `626d1281` and on this patch. The same tests and a rollback/commit fixture passed on both arms of the combined tree with #1284 head `2c178819`. The combined partial-release case uses 3 instead of 12 image barriers. The ordered fixture uses 5 instead of 14 with the same per-cell layouts and scopes. Same-layout recording allocates zero in both arms. The earlier two-core CPU benchmark measured 640 to 2 records and 41.7–47.1% less partition time on stripes. Mostly-undefined and checkerboard partitions did not improve in that benchmark. [Pinned benchmark and fixture sources](https://github.com/akimfomin/PortBridge/tree/049b46286518c20d98dc74ae700e66599110d964/research/continued-layout-range-coalescing) GPUs tested: none on this exact head. The linked earlier Vulkan readback used RTX 4070 Ti SUPER and tested a different revision. AI-assisted: yes (Codex). [PortBridge](https://github.com/akimfomin/PortBridge) ### Checklist - [x] Based on current main and merges cleanly. - [x] One topic and a separate follow-up. - [x] No game inputs, keys or decryption.
kawaii-shakal commented 2026-11-25 09:45:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kawaii-shakal on 2026-11-25T09:45:33Z.

Review: COMMENTED


Review (read-only, not run). I traced the merge and the early-out:

  • index - 1 is the previous mip of the same layer, and index - e.mips is the previous layer of the same plane, only read when mip/layer is past the range base, so both are in bounds;
  • last_run is keyed by mip with the aspect compared, so a depth run and a stencil run never merge;
  • UNDEFINED cells keep one piece each, so initialize() still gets its per-piece transfer regions.

Looks correct to me. Two small things:

  • area with VK_REMAINING_MIP_LEVELS / VK_REMAINING_ARRAY_LAYERS is about 2^64. It only feeds the <= 2 test, so it is harmless, but visit clamps the range and this doesn't, which reads oddly. Clamping it the same way would make the intent obvious.
  • "GPUs tested: none on this exact head". The replay emits fewer, larger barriers now, so one replay_texture_barrier + d3d12_msaa_draw run on a GPU with the sync validation layer would be worth it. I ran both on an RTX 5070 Ti for #778's merge with main; I can run this head the same way if useful.

AI-assisted review (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6055209960 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1713#pullrequestreview-6055209960). Originally posted by [kawaii-shakal](https://github.com/kawaii-shakal) on 2026-11-25T09:45:33Z. **Review:** COMMENTED --- Review (read-only, not run). I traced the merge and the early-out: - `index - 1` is the previous mip of the same layer, and `index - e.mips` is the previous layer of the same plane, only read when `mip`/`layer` is past the range base, so both are in bounds; - `last_run` is keyed by mip with the aspect compared, so a depth run and a stencil run never merge; - `UNDEFINED` cells keep one piece each, so `initialize()` still gets its per-piece transfer regions. Looks correct to me. Two small things: - `area` with `VK_REMAINING_MIP_LEVELS` / `VK_REMAINING_ARRAY_LAYERS` is about 2^64. It only feeds the `<= 2` test, so it is harmless, but `visit` clamps the range and this doesn't, which reads oddly. Clamping it the same way would make the intent obvious. - "GPUs tested: none on this exact head". The replay emits fewer, larger barriers now, so one `replay_texture_barrier` + `d3d12_msaa_draw` run on a GPU with the sync validation layer would be worth it. I ran both on an RTX 5070 Ti for #778's merge with main; I can run this head the same way if useful. AI-assisted review (Claude Code, Claude Opus 5.5).
akimfomin commented 2026-11-25 09:59:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by akimfomin on 2026-11-25T09:59:16Z.


Thanks for tracing the merge, @kawaii-shakal. Your RTX 5070 Ti run would be useful. Please use the unchanged ef9ae1fd54d5c452eb31236c1ca86debe0a5fda9 head and run replay_texture_barrier and d3d12_msaa_draw with Vulkan synchronization validation enabled. Please record the exact SHA, driver and OS, pass/fail/skip for each test, and any synchronization-validation diagnostics or confirm there were none.

Agreed that clamping the VK_REMAINING_* range in the area check the same way as visit would make the early-out clearer. I’ll leave this head pinned for your run and revisit that readability point afterward.

AI-assisted (Codex drafted this reply).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7159070010 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1713#issuecomment-7159070010). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-25T09:59:16Z. --- Thanks for tracing the merge, @kawaii-shakal. Your RTX 5070 Ti run would be useful. Please use the unchanged `ef9ae1fd54d5c452eb31236c1ca86debe0a5fda9` head and run `replay_texture_barrier` and `d3d12_msaa_draw` with Vulkan synchronization validation enabled. Please record the exact SHA, driver and OS, pass/fail/skip for each test, and any synchronization-validation diagnostics or confirm there were none. Agreed that clamping the `VK_REMAINING_*` range in the `area` check the same way as `visit` would make the early-out clearer. I’ll leave this head pinned for your run and revisit that readability point afterward. AI-assisted (Codex drafted this reply).
potatoe0100 commented 2026-11-25 11:46:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by potatoe0100 on 2026-11-25T11:46:10Z.


Tested the unchanged head ef9ae1fd5 merged onto main bcaa13dad (clean), native Windows 11 Pro 26H2 (build 26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 RelWithDebInfo + Ninja, Vulkan SDK 1.4.363.0.

  • With LEONIDA_VK_VALIDATION=1 VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1: replay_texture_barrier, d3d12_msaa_draw and d3d12_state_map pass, with no VUIDs and no sync hazards. vk_sync_validation passes in the same run (it reports its deliberate WRITE_AFTER_WRITE), so synchronization validation was really on.
  • Full ctest -j 6: 409 of 409 passed, same as main. run_gpu_tests.py: 209/263, identical to main.

AI-assisted testing (non-developer tester).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7161475134 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1713#issuecomment-7161475134). Originally posted by [potatoe0100](https://github.com/potatoe0100) on 2026-11-25T11:46:10Z. --- Tested the unchanged head `ef9ae1fd5` merged onto `main` `bcaa13dad` (clean), native Windows 11 Pro 26H2 (build 26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 RelWithDebInfo + Ninja, Vulkan SDK 1.4.363.0. - With `LEONIDA_VK_VALIDATION=1 VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1`: `replay_texture_barrier`, `d3d12_msaa_draw` and `d3d12_state_map` pass, with no VUIDs and no sync hazards. `vk_sync_validation` passes in the same run (it reports its deliberate WRITE_AFTER_WRITE), so synchronization validation was really on. - Full `ctest -j 6`: 409 of 409 passed, same as `main`. `run_gpu_tests.py`: 209/263, identical to `main`. AI-assisted testing (non-developer tester).
akimfomin commented 2026-11-26 02:27:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by akimfomin on 2026-11-26T02:27:57Z.


DadaCap, thank you for suggesting that the equal-layout change stand on its own. kawaii-shakal, your review caught the difference between the range used by the early-out and the clamped range used for the visit. potatoe0100, the deliberate validation hazard in your RTX 4060 control makes your clean replay and MSAA results particularly valuable.

I’m moving PortBridge to passive support and leaving this head at ef9ae1fd54d5c452eb31236c1ca86debe0a5fda9 for comparison. The small fixtures and benchmark and independent GPU run are preserved. A future reviewer can check whether to align the early-out with the clamped range and whether the change is useful on today's replay path. The measured barrier counts and CPU partition timings cover the fixtures; whole-frame timing is still open. Please feel free to carry this through your usual review process. Incoming questions are welcome here. Codex helped prepare the handover.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7180829192 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1713#issuecomment-7180829192). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-26T02:27:57Z. --- DadaCap, thank you for suggesting that the equal-layout change stand on its own. kawaii-shakal, your review caught the difference between the range used by the early-out and the clamped range used for the visit. potatoe0100, the deliberate validation hazard in your RTX 4060 control makes your clean replay and MSAA results particularly valuable. I’m moving [PortBridge](https://github.com/akimfomin/PortBridge) to passive support and leaving this head at `ef9ae1fd54d5c452eb31236c1ca86debe0a5fda9` for comparison. The [small fixtures and benchmark](https://github.com/akimfomin/PortBridge/tree/049b46286518c20d98dc74ae700e66599110d964/research/continued-layout-range-coalescing) and [independent GPU run](https://github.com/ubiquitarianism/leonida/pull/1713#issuecomment-7161475134) are preserved. A future reviewer can check whether to align the early-out with the clamped range and whether the change is useful on today's replay path. The measured barrier counts and CPU partition timings cover the fixtures; whole-frame timing is still open. Please feel free to carry this through your usual review process. Incoming questions are welcome here. Codex helped prepare the handover.
zmasi commented 2026-11-29 02:13:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-29T02:13:52Z.


Independently verified head ef9ae1fd54d5c452eb31236c1ca86debe0a5fda9 merged cleanly onto origin/main (59e65849b0927ee8c435baf1c74aecd08104fd60) on Windows 11 (10.0.26200), MSVC 19.44.35207 (RelWithDebInfo), NVIDIA GeForce RTX 3090 (driver 595.79), with VK_LAYER_KHRONOS_validation 1.4.363.1 (LEONIDA_VK_VALIDATION=1 and VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1 synchronization validation enabled). No game files used.

Red -> Green Verification

  • Red (main's runtime/src/vulkan_replay.h with PR's d3d12_state_map_test.cpp):
    • ctest -C RelWithDebInfo -R "^d3d12_state_map$": FAILED (rc=8) at line 520: separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, pieces) && pieces.size() == 3 (main emits 4 uncoalesced pieces instead of coalescing the 2 equal depth mips into 1 piece).
  • Green (#1713 merged with origin/main 59e65849b0, LEONIDA_VK_VALIDATION=1 + VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1 on RTX 3090):
    • 5/5 CTests Passed (2.77 s, 0 VUIDs, 0 sync hazards):
      • vk_command_replay (0.81 s), d3d12_draw, d3d12_msaa_draw (0.75 s, 4x draws resolved, 4096 pixels matched), replay_texture_barrier (0.08 s), d3d12_state_map (0.08 s).
    • Combined #1713 + #1284 (7358f3261f) + origin/main (59e65849b0): merges with 0 conflicts (git merge-tree rc=0), and d3d12_state_map, replay_texture_barrier, and d3d12_msaa_draw all pass under VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1 (vk_sync_validation control also run to confirm synchronization validation is active).
    • scripts/check_no_game_data.py: Passed.

Code Audit Notes

  • Verified last_run[mip] across two_aspects (e.two_planes depth + stencil): because visit() finishes plane 0 before plane 1 and flush_run() checks previous.range.aspectMask == run.range.aspectMask, stale plane-0 slots never coalesce across aspects and are cleanly overwritten on plane 1's first layer.
  • First-use VK_IMAGE_LAYOUT_UNDEFINED subresources keep individual 1x1 pieces so first-use clear/init region granularity is preserved unchanged, and clamping area for VK_REMAINING_MIP_LEVELS / VK_REMAINING_ARRAY_LAYERS as discussed in the thread is a clean readability follow-up (correctness is unaffected since the neighbor scan still handles it).

AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249293541 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1713#issuecomment-7249293541). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-29T02:13:52Z. --- Independently verified head `ef9ae1fd54d5c452eb31236c1ca86debe0a5fda9` merged cleanly onto `origin/main` (`59e65849b0927ee8c435baf1c74aecd08104fd60`) on Windows 11 (`10.0.26200`), MSVC `19.44.35207` (`RelWithDebInfo`), **NVIDIA GeForce RTX 3090** (driver `595.79`), with `VK_LAYER_KHRONOS_validation` `1.4.363.1` (`LEONIDA_VK_VALIDATION=1` and `VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1` synchronization validation enabled). No game files used. ### Red -> Green Verification - **Red (`main`'s `runtime/src/vulkan_replay.h` with PR's `d3d12_state_map_test.cpp`)**: - `ctest -C RelWithDebInfo -R "^d3d12_state_map$"`: **FAILED (`rc=8`)** at `line 520: separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, pieces) && pieces.size() == 3` (`main` emits 4 uncoalesced pieces instead of coalescing the 2 equal depth mips into 1 piece). - **Green (`#1713` merged with `origin/main` `59e65849b0`, `LEONIDA_VK_VALIDATION=1` + `VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1` on RTX 3090)**: - **`5/5` CTests Passed (`2.77 s`, `0` VUIDs, `0` sync hazards)**: - `vk_command_replay` (`0.81 s`), `d3d12_draw`, `d3d12_msaa_draw` (`0.75 s`, `4x draws resolved, 4096 pixels matched`), `replay_texture_barrier` (`0.08 s`), `d3d12_state_map` (`0.08 s`). - **Combined `#1713` + `#1284` (`7358f3261f`) + `origin/main` (`59e65849b0`)**: merges with `0` conflicts (`git merge-tree` `rc=0`), and `d3d12_state_map`, `replay_texture_barrier`, and `d3d12_msaa_draw` all pass under `VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1` (`vk_sync_validation` control also run to confirm synchronization validation is active). - `scripts/check_no_game_data.py`: **Passed**. ### Code Audit Notes - Verified `last_run[mip]` across `two_aspects` (`e.two_planes` depth + stencil): because `visit()` finishes plane 0 before plane 1 and `flush_run()` checks `previous.range.aspectMask == run.range.aspectMask`, stale plane-0 slots never coalesce across aspects and are cleanly overwritten on plane 1's first layer. - First-use `VK_IMAGE_LAYOUT_UNDEFINED` subresources keep individual `1x1` pieces so first-use clear/init region granularity is preserved unchanged, and clamping `area` for `VK_REMAINING_MIP_LEVELS` / `VK_REMAINING_ARRAY_LAYERS` as discussed in the thread is a clean readability follow-up (correctness is unaffected since the neighbor scan still handles it). AI-assisted: yes
OreoFR commented 2026-11-29 05:47:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T05:47:30Z.


Verified by OreoFR — READY

  • Base: main c8bf0e7b6 + this PR (head ef9ae1fd5) merged with --no-ff
  • Red (main + only this PR's tests): d3d12_state_map fails on main at line 520 (pieces.size() == 3: main gives 4 pieces).
  • Green (merge): d3d12_state_map passes; ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p2 = main + #1874 + #1894 + #1713 (no shared files): 495 tests, new failures vs main: none (main alone: 494 tests, 5 failures: loader, xs_proxy_mock, xmem_no_write_watch, api_ref_state timeout, xbox_present; xbox_present fails in every package as on main)
  • gta6rt.exe links without the XMA decoder (LEONIDA_CARGO=OFF, LEONIDA_XMA2_DECODER_LIB empty), main + this PR alone; imports unchanged vs main (25 DLLs, 655 functions, no WS2_32)
  • Diff read: no issue. UNDEFINED cells stay single for first-use init, and the consumers accept pieces that span several levels.
  • Other runs: Consistent with zmasi's and potatoe0100's runs.
  • Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7251461996 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1713#issuecomment-7251461996). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T05:47:30Z. --- **Verified by OreoFR** — READY - Base: main c8bf0e7b6 + this PR (head ef9ae1fd5) merged with --no-ff - Red (main + only this PR's tests): `d3d12_state_map` fails on main at line 520 (`pieces.size() == 3`: main gives 4 pieces). - Green (merge): `d3d12_state_map` passes; ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p2 = main + #1874 + #1894 + #1713 (no shared files): 495 tests, new failures vs main: none (main alone: 494 tests, 5 failures: loader, xs_proxy_mock, xmem_no_write_watch, api_ref_state timeout, xbox_present; xbox_present fails in every package as on main) - gta6rt.exe links without the XMA decoder (`LEONIDA_CARGO=OFF`, `LEONIDA_XMA2_DECODER_LIB` empty), main + this PR alone; imports unchanged vs main (25 DLLs, 655 functions, no WS2_32) - Diff read: no issue. UNDEFINED cells stay single for first-use init, and the consumers accept pieces that span several levels. - Other runs: Consistent with zmasi's and potatoe0100's runs. - Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44 AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.
aslan2142 commented 2026-12-01 16:37:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:37:20Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317910304 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1713#issuecomment-7317910304). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:37:20Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1713
No description provided.