runtime: guard draw zero-clears, AtomicCopyBuffer aliasing/overflow, and zero_image_aspect scratch (RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS) #1892

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1892 into main
IlyassRS3 commented 2026-11-25 14:44:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T14:44:13Z.


Item: RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7165529828

Summary

Fixes three replay correctness bugs in runtime/src/vulkan_replay.h:

  1. zero_clear_unavailable() skips Draw / IndirectRasterDraw attachments (null-pointer crash):
    • emit() calls detail::to_attachment() -> detail::initialize() on every bound color and depth/stencil attachment of a non-skipped draw, invoking vk.clear_color_image / vk.clear_depth_stencil_image for any UNDEFINED piece.
    • zero_clear_unavailable() previously inspected copies, clears, and barriers, omitting draws (draw_command(command).state) and omitting planes_for_layout() for coupled depth/stencil ranges — allowing emit() to dereference a null vk.clear_color_image or vk.clear_depth_stencil_image (SIGSEGV on main).
    • Now checks d.colors and d.depth for every non-skipped draw and uses detail::planes_for_layout(resource, range, vk.separate_depth_stencil_layouts) inside missing().
  2. AtomicCopyBuffer preparation misses overflow & same-VkBuffer aliasing self-overlap (VUID-vkCmdCopyBuffer-pRegions-00117):
    • CommandList::atomic_copy() only compares ResourceIds (copy.dst == copy.src), so two distinct placed/arena ResourceIds backed by overlapping byte ranges of the same VkBuffer bypassed overlap rejection; prepare() also lacked UINT64_MAX checks on buffer_offset + offset and offset + bytes, plus a zero-size guard.
    • Now enforces the exact same overflow and same-VkBuffer overlap checks as CopyBuffer.
  3. zero_image_aspect() drops zero-initialization on unaligned scratch offsets:
    • When vk.copy_scratch(fill_bytes, ...) returns a 4-byte-aligned offset for an 8-byte or 16-byte block/texel format (e.g. 1x1 BC1/BC7/RGBA32F where needed == align), pad leaves ((fill_bytes - pad) / align) * align == 0 < fmt.bytes, causing zero_image_aspect() to return false and leave the image uninitialized.
    • Now requests fill_bytes + (align - 1) (overflow-guarded, with fallback to fill_bytes for exact-sized aligned pools), checks scratch != VK_NULL_HANDLE, and caps usable to fill_bytes.

No overlap with open PRs #787, #1123, #1179, or #1877.

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU-only macOS arm64 host with Khronos Vulkan-Headers)
  • New unit test runtime/test/replay_draw_zero_and_atomic_guards_test.cpp (CTest replay_draw_zero_and_atomic_guards):
    • clang++ -std=c++20 -Wall -Wextra -Wno-missing-field-initializers -Wno-switch -Wno-unused-private-field -Werror -O1 -g -fsanitize=address,undefined: passed
    • clang++ -std=c++20 -O1 -g -fsanitize=thread: passed
    • clang++ -std=c++20 -O2 (3 runs): passed
  • Existing replay unit tests compiled and passed: replay_clear_gate_test, replay_copy_offset_test, replay_texture_barrier_test, replay_query_edges_test
  • Per-bug mutation tests against main:
    • test_draw_zero_clear_unavailable_color_and_depth: SIGSEGV (returncode=-11) on main -> passes on branch
    • test_atomic_copy_overflow_and_aliasing_overlap: fails (returncode=1) on main -> passes on branch
    • test_zero_image_aspect_unaligned_scratch_offset: fails (returncode=1) on main -> passes on branch
  • python3 -I scripts/check_no_game_data.py on all changed files: passed
  • python3 -I tests/test_text_write_line_endings.py: passed (3 tests)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5493551776 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1892). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T14:44:13Z. --- Item: `RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7165529828 ### Summary Fixes three replay correctness bugs in `runtime/src/vulkan_replay.h`: 1. **`zero_clear_unavailable()` skips `Draw` / `IndirectRasterDraw` attachments (null-pointer crash)**: - `emit()` calls `detail::to_attachment()` -> `detail::initialize()` on every bound color and depth/stencil attachment of a non-skipped draw, invoking `vk.clear_color_image` / `vk.clear_depth_stencil_image` for any `UNDEFINED` piece. - `zero_clear_unavailable()` previously inspected copies, clears, and barriers, omitting draws (`draw_command(command).state`) and omitting `planes_for_layout()` for coupled depth/stencil ranges — allowing `emit()` to dereference a null `vk.clear_color_image` or `vk.clear_depth_stencil_image` (SIGSEGV on `main`). - Now checks `d.colors` and `d.depth` for every non-skipped draw and uses `detail::planes_for_layout(resource, range, vk.separate_depth_stencil_layouts)` inside `missing()`. 2. **`AtomicCopyBuffer` preparation misses overflow & same-`VkBuffer` aliasing self-overlap (`VUID-vkCmdCopyBuffer-pRegions-00117`)**: - `CommandList::atomic_copy()` only compares `ResourceId`s (`copy.dst == copy.src`), so two distinct placed/arena `ResourceId`s backed by overlapping byte ranges of the same `VkBuffer` bypassed overlap rejection; `prepare()` also lacked `UINT64_MAX` checks on `buffer_offset + offset` and `offset + bytes`, plus a zero-size guard. - Now enforces the exact same overflow and same-`VkBuffer` overlap checks as `CopyBuffer`. 3. **`zero_image_aspect()` drops zero-initialization on unaligned scratch offsets**: - When `vk.copy_scratch(fill_bytes, ...)` returns a 4-byte-aligned offset for an 8-byte or 16-byte block/texel format (e.g. 1x1 BC1/BC7/RGBA32F where `needed == align`), `pad` leaves `((fill_bytes - pad) / align) * align == 0 < fmt.bytes`, causing `zero_image_aspect()` to return `false` and leave the image uninitialized. - Now requests `fill_bytes + (align - 1)` (overflow-guarded, with fallback to `fill_bytes` for exact-sized aligned pools), checks `scratch != VK_NULL_HANDLE`, and caps `usable` to `fill_bytes`. No overlap with open PRs `#787`, `#1123`, `#1179`, or `#1877`. ### Verification - **AI-assisted**: yes - **GPUs tested**: none (CPU-only macOS arm64 host with Khronos Vulkan-Headers) - New unit test `runtime/test/replay_draw_zero_and_atomic_guards_test.cpp` (CTest `replay_draw_zero_and_atomic_guards`): - `clang++ -std=c++20 -Wall -Wextra -Wno-missing-field-initializers -Wno-switch -Wno-unused-private-field -Werror -O1 -g -fsanitize=address,undefined`: passed - `clang++ -std=c++20 -O1 -g -fsanitize=thread`: passed - `clang++ -std=c++20 -O2` (3 runs): passed - Existing replay unit tests compiled and passed: `replay_clear_gate_test`, `replay_copy_offset_test`, `replay_texture_barrier_test`, `replay_query_edges_test` - **Per-bug mutation tests against `main`**: - `test_draw_zero_clear_unavailable_color_and_depth`: **SIGSEGV (`returncode=-11`) on `main`** -> passes on branch - `test_atomic_copy_overflow_and_aliasing_overlap`: **fails (`returncode=1`) on `main`** -> passes on branch - `test_zero_image_aspect_unaligned_scratch_offset`: **fails (`returncode=1`) on `main`** -> passes on branch - `python3 -I scripts/check_no_game_data.py` on all changed files: passed - `python3 -I tests/test_text_write_line_endings.py`: passed (3 tests)
copilot-pull-request-reviewer[bot] commented 2026-11-25 14:44:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T14:44:24Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6059780406 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1892#pullrequestreview-6059780406). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T14:44:24Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Timon360SB commented 2026-11-25 15:32:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Timon360SB on 2026-11-25T15:32:34Z.


Independent verification — Turing and Blackwell

  • Base: 71a769d7d85208f86c6e2e50263aca7df1becce9
  • Head: 461ecf77ddc4da4f057f33d4b82de395adf5935b

Environment

GPU Windows 11 build
RTX 5070 (Blackwell) 26200
RTX 2060 SUPER (Turing) 28000

NVIDIA 621.14, Vulkan SDK 1.4.363.0,
MSVC 19.44.35230, Ninja RelWithDebInfo.

Three repetitions per GPU, revision and validation mode.
The new mock test was run once per system on HEAD.

Results

Test Base Head
replay_clear_gate 12/12 12/12
replay_copy_offset 12/12 12/12
replay_texture_barrier 12/12 12/12
replay_query_edges 12/12 12/12
d3d12_predication_atomic_copy 12/12 12/12
d3d12_draw 12/12 12/12
replay_draw_zero_and_atomic_guards N/A 2/2
Total 72/72 74/74

146 executions: 146 passed, 0 failed, 0 skipped.

Findings

  • The new mock test passes under Windows/MSVC on both
    systems, covering missing clear functions, atomic-copy
    aliasing and overflow, and scratch-buffer alignment.
  • Predication and atomic-copy tests reported 59 cases,
    zero failures and zero Vulkan validation errors in
    every execution.
  • Draw tests retained exact 4096/4096 pixel matches
    for MSAA, instance step rate and strip-cut checks.
  • No VUID diagnostics in the captured logs.
    Validation-layer feature-adjustment warnings were present.

No regressions observed on either GPU.
The new targeted fixture passes on HEAD; its negative
control against unpatched BASE was not independently run.

Synthetic tests only. No game files used.
AI-assisted: yes.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167004269 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1892#issuecomment-7167004269). Originally posted by [Timon360SB](https://github.com/Timon360SB) on 2026-11-25T15:32:34Z. --- ## Independent verification — Turing and Blackwell - **Base:** `71a769d7d85208f86c6e2e50263aca7df1becce9` - **Head:** `461ecf77ddc4da4f057f33d4b82de395adf5935b` ### Environment | GPU | Windows 11 build | |---|---| | RTX 5070 (Blackwell) | 26200 | | RTX 2060 SUPER (Turing) | 28000 | NVIDIA 621.14, Vulkan SDK 1.4.363.0, MSVC 19.44.35230, Ninja RelWithDebInfo. Three repetitions per GPU, revision and validation mode. The new mock test was run once per system on HEAD. ### Results | Test | Base | Head | |---|---:|---:| | `replay_clear_gate` | 12/12 | 12/12 | | `replay_copy_offset` | 12/12 | 12/12 | | `replay_texture_barrier` | 12/12 | 12/12 | | `replay_query_edges` | 12/12 | 12/12 | | `d3d12_predication_atomic_copy` | 12/12 | 12/12 | | `d3d12_draw` | 12/12 | 12/12 | | `replay_draw_zero_and_atomic_guards` | N/A | 2/2 | | **Total** | **72/72** | **74/74** | 146 executions: 146 passed, 0 failed, 0 skipped. ### Findings - The new mock test passes under Windows/MSVC on both systems, covering missing clear functions, atomic-copy aliasing and overflow, and scratch-buffer alignment. - Predication and atomic-copy tests reported 59 cases, zero failures and zero Vulkan validation errors in every execution. - Draw tests retained exact 4096/4096 pixel matches for MSAA, instance step rate and strip-cut checks. - No VUID diagnostics in the captured logs. Validation-layer feature-adjustment warnings were present. No regressions observed on either GPU. The new targeted fixture passes on HEAD; its negative control against unpatched BASE was not independently run. Synthetic tests only. No game files used. AI-assisted: yes.
zmasi commented 2026-11-29 00:59:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-29T00:59:47Z.


#1892 | head 461ecf77dd (merged cleanly with main 48bae6cade — no conflicts on current main, and merges cleanly with #1905) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(d3d12_predication_atomic_copy|d3d12_draw|replay_copy_offset|replay_texture_barrier|replay_draw_zero_and_atomic_guards|replay_clear_gate|replay_query_edges)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: building replay_draw_zero_and_atomic_guards_test against main's runtime/src/vulkan_replay.h crashes with Exception: SegFault (zero_clear_unavailable skips Draw attachments and detail::initialize dereferences null vk.clear_color_image) | green on PR+main: 7/7 CTests (d3d12_predication_atomic_copy, d3d12_draw, replay_copy_offset, replay_texture_barrier, replay_draw_zero_and_atomic_guards, replay_clear_gate, replay_query_edges) pass in 2.74 s on RTX 3090 with 0 Khronos validation errors; 10/10 test_ctest_registration.py + test_text_write_line_endings.py OK and check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7248489509 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1892#issuecomment-7248489509). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-29T00:59:47Z. --- #1892 | head 461ecf77ddc4da4f057f33d4b82de395adf5935b (merged cleanly with main 48bae6cade49aea69f63eb8871572dcd6931cd4b — no conflicts on current `main`, and merges cleanly with `#1905`) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(d3d12_predication_atomic_copy|d3d12_draw|replay_copy_offset|replay_texture_barrier|replay_draw_zero_and_atomic_guards|replay_clear_gate|replay_query_edges)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: building `replay_draw_zero_and_atomic_guards_test` against `main`'s `runtime/src/vulkan_replay.h` crashes with `Exception: SegFault` (`zero_clear_unavailable` skips `Draw` attachments and `detail::initialize` dereferences null `vk.clear_color_image`) | green on PR+main: `7/7` CTests (`d3d12_predication_atomic_copy`, `d3d12_draw`, `replay_copy_offset`, `replay_texture_barrier`, `replay_draw_zero_and_atomic_guards`, `replay_clear_gate`, `replay_query_edges`) pass in `2.74 s` on RTX 3090 with `0` Khronos validation errors; `10/10` `test_ctest_registration.py` + `test_text_write_line_endings.py` OK and `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
enlight3d commented 2026-11-29 08:56:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-29T08:56:44Z.


Validation run of this PR on a Windows PC with a real GPU.

  • PR head: 461ecf77dd
  • main: 54b2a81e95
  • Tested merged: yes, git merge --no-ff of the PR head into main was clean.

Commands (RelWithDebInfo, Ninja, LEONIDA_REQUIRE_VULKAN=ON):

git fetch origin pull/1892/head:pr-1892
git checkout --detach origin/main && git merge --no-edit --no-ff pr-1892
cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build runtime/build -j 8 --target replay_draw_zero_and_atomic_guards_test replay_clear_gate_test replay_uav_clear_offset_test replay_copy_offset_test clear_color_test vk_command_replay_test gta6rt
ctest -R "^(replay_draw_zero_and_atomic_guards|replay_clear_gate|replay_uav_clear_offset|replay_copy_offset|clear_color|vk_command_replay)$" --output-on-failure -j1
  • Green: 6/6 passed, including replay_draw_zero_and_atomic_guards (the PR's test). The other five tests include vulkan_replay.h, which the PR changes (zero_image_aspect, zero_clear_unavailable, AtomicCopyBuffer checks); vk_command_replay runs on the GPU.
  • Red (behavioural): the PR's test file and CMake entry kept, runtime/src/vulkan_replay.h reverted to main (the test still builds). replay_draw_zero_and_atomic_guards fails with a SEGFAULT; running its three cases one by one with progress prints shows the crash in the first one (the draw zero-clear gate case).
  • Link: no new imports vs main (dumpbin /imports of gta6rt.exe, 703 DLL!symbol entries, identical to a main build).

Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7253433530 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1892#issuecomment-7253433530). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-11-29T08:56:44Z. --- Validation run of this PR on a Windows PC with a real GPU. - PR head: 461ecf77ddc4da4f057f33d4b82de395adf5935b - main: 54b2a81e95c470c953ddee6177fd6255cbaf041d - Tested merged: yes, `git merge --no-ff` of the PR head into main was clean. Commands (RelWithDebInfo, Ninja, `LEONIDA_REQUIRE_VULKAN=ON`): ``` git fetch origin pull/1892/head:pr-1892 git checkout --detach origin/main && git merge --no-edit --no-ff pr-1892 cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON cmake --build runtime/build -j 8 --target replay_draw_zero_and_atomic_guards_test replay_clear_gate_test replay_uav_clear_offset_test replay_copy_offset_test clear_color_test vk_command_replay_test gta6rt ctest -R "^(replay_draw_zero_and_atomic_guards|replay_clear_gate|replay_uav_clear_offset|replay_copy_offset|clear_color|vk_command_replay)$" --output-on-failure -j1 ``` - Green: 6/6 passed, including `replay_draw_zero_and_atomic_guards` (the PR's test). The other five tests include `vulkan_replay.h`, which the PR changes (`zero_image_aspect`, `zero_clear_unavailable`, AtomicCopyBuffer checks); `vk_command_replay` runs on the GPU. - Red (behavioural): the PR's test file and CMake entry kept, `runtime/src/vulkan_replay.h` reverted to main (the test still builds). `replay_draw_zero_and_atomic_guards` fails with a SEGFAULT; running its three cases one by one with progress prints shows the crash in the first one (the draw zero-clear gate case). - Link: no new imports vs main (`dumpbin /imports` of `gta6rt.exe`, 703 DLL!symbol entries, identical to a main build). Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.
aslan2142 commented 2026-12-01 16:38:42 +00:00 (Migrated from github.com)

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


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:7317952528 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1892#issuecomment-7317952528). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:38:42Z. --- 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!1892
No description provided.