vulkan_replay: validate backing buffer_offset alignment in buffer UAV clear (VK-UAV-CLEAR-ALIGN) #418

Manually merged
github merged 1 commit from gh-pr/418/head into gh-pr/418/base 2026-11-26 08:59:04 +00:00
Owner

@⁠tahffo68hk-hash on GitHub · 2026-11-24 04:53 UTC · original
tahffo68hk-hash:fix/vk-uav-clear-align → main · +13 −0 · 2 files · 1 commits · merged 2026-11-24 16:24 UTC

Claimed in the coordination issue as: VK-UAV-CLEAR-ALIGN

Problem

prepare_buffer_uav_clear() in runtime/src/vulkan_replay.h checked offset % std::min(e, 4u) before adding p.binding.buffer_offset. When a sub-allocated buffer has a misaligned buffer_offset:

  1. For 8- or 16-byte UAV elements (e > 4, e.g., R32G32B32A32_UINT), emit_buffer_fill() called vkCmdUpdateBuffer with dstOffset % 4 != 0, violating VUID-vkCmdUpdateBuffer-dstOffset-00032.
  2. For 2- or 4-byte UAV elements (e == 2 or e == 4), emit_buffer_fill() filled 4-byte-aligned words with an unrotated pattern, byte-swapping/rotating every cleared element.

(Compare prepare_texture_copy(), which explicitly checks backing_offset % alignment after adding buffer.buffer_offset.)

Fix

  • In prepare_buffer_uav_clear(), check if (offset % std::min(e, 4u)) return ReplayResult::unsupported; after adding p.binding.buffer_offset, while still allowing odd buffer_offset for 1-byte (R8_*) elements.
  • Add regression tests in runtime/test/replay_uav_clear_offset_test.cpp covering misaligned buffer_offset for 16-byte, 4-byte, 2-byte, and 1-byte UAV clears.

Verification

  • Built with MSVC 19.44 + Ninja (-DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF) on Windows 11.
  • ctest -R "^replay_uav_clear_offset$" --output-on-failure -> 1/1 PASS (no game files).
  • AI-assisted: Yes (Antigravity).
中文翻译

在协调帖 #14 中认领的 ID:VK-UAV-CLEAR-ALIGN

问题描述

runtime/src/vulkan_replay.h 的 prepare_buffer_uav_clear() 仅在叠加 p.binding.buffer_offset 之前检查 offset % std::min(e, 4u)。当子分配缓冲区的 buffer_offset 未对齐时:

  1. 对 8/16 字节元素(e > 4)会以未 4 字节对齐的 dstOffset 调用 vkCmdUpdateBuffer,违反 VUID-vkCmdUpdateBuffer-dstOffset-00032;
  2. 对 2/4 字节元素会以未旋转字填充,导致写入元素字节错位。

修复方案

  • 在叠加 p.binding.buffer_offset 后检查 offset % std::min(e, 4u),未对齐时返回 ReplayResult::unsupported(同时保留 1 字节 R8_* 元素的合法性)。
  • 在 runtime/test/replay_uav_clear_offset_test.cpp 中补充 16/4/2/1 字节元素的回归测试。

验证结果

  • Windows 11 + MSVC 19.44 + Ninja 编译并通过 ctest -R "^replay_uav_clear_offset$"(无游戏文件)。
  • AI 辅助:是(Antigravity)。
**@⁠tahffo68hk-hash** on GitHub · 2026-11-24 04:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/418) `tahffo68hk-hash:fix/vk-uav-clear-align` → `main` · +13 −0 · 2 files · 1 commits · merged 2026-11-24 16:24 UTC Claimed in the coordination issue as: `VK-UAV-CLEAR-ALIGN` ## Problem `prepare_buffer_uav_clear()` in `runtime/src/vulkan_replay.h` checked `offset % std::min(e, 4u)` *before* adding `p.binding.buffer_offset`. When a sub-allocated buffer has a misaligned `buffer_offset`: 1. For 8- or 16-byte UAV elements (`e > 4`, e.g., `R32G32B32A32_UINT`), `emit_buffer_fill()` called `vkCmdUpdateBuffer` with `dstOffset % 4 != 0`, violating `VUID-vkCmdUpdateBuffer-dstOffset-00032`. 2. For 2- or 4-byte UAV elements (`e == 2` or `e == 4`), `emit_buffer_fill()` filled 4-byte-aligned words with an unrotated pattern, byte-swapping/rotating every cleared element. (Compare `prepare_texture_copy()`, which explicitly checks `backing_offset % alignment` after adding `buffer.buffer_offset`.) ## Fix - In `prepare_buffer_uav_clear()`, check `if (offset % std::min(e, 4u)) return ReplayResult::unsupported;` after adding `p.binding.buffer_offset`, while still allowing odd `buffer_offset` for 1-byte (`R8_*`) elements. - Add regression tests in `runtime/test/replay_uav_clear_offset_test.cpp` covering misaligned `buffer_offset` for 16-byte, 4-byte, 2-byte, and 1-byte UAV clears. ## Verification - Built with MSVC 19.44 + Ninja (`-DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF`) on Windows 11. - `ctest -R "^replay_uav_clear_offset$" --output-on-failure` -> 1/1 PASS (no game files). - AI-assisted: Yes (Antigravity). <details> <summary>中文翻译</summary> 在协调帖 #14 中认领的 ID:`VK-UAV-CLEAR-ALIGN` ## 问题描述 `runtime/src/vulkan_replay.h` 的 `prepare_buffer_uav_clear()` 仅在叠加 `p.binding.buffer_offset` 之前检查 `offset % std::min(e, 4u)`。当子分配缓冲区的 `buffer_offset` 未对齐时: 1. 对 8/16 字节元素(`e > 4`)会以未 4 字节对齐的 `dstOffset` 调用 `vkCmdUpdateBuffer`,违反 `VUID-vkCmdUpdateBuffer-dstOffset-00032`; 2. 对 2/4 字节元素会以未旋转字填充,导致写入元素字节错位。 ## 修复方案 - 在叠加 `p.binding.buffer_offset` 后检查 `offset % std::min(e, 4u)`,未对齐时返回 `ReplayResult::unsupported`(同时保留 1 字节 `R8_*` 元素的合法性)。 - 在 `runtime/test/replay_uav_clear_offset_test.cpp` 中补充 16/4/2/1 字节元素的回归测试。 ## 验证结果 - Windows 11 + MSVC 19.44 + Ninja 编译并通过 `ctest -R "^replay_uav_clear_offset$"`(无游戏文件)。 - AI 辅助:是(Antigravity)。 </details>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:59:04 +00:00
github manually merged commit c9099b0cad into gh-pr/418/base 2026-11-26 08:59:04 +00:00
Author
Owner

@⁠moffington on GitHub · 2026-11-24 05:21 UTC · original

Tested c9099b0c on Linux. replay_uav_clear_offset passed. I ran the same test against the merge-base replay code (d2cd6c0d), and all three new rejection assertions failed as expected for misaligned backing offsets with 16-, 4-, and 2-byte elements. The R8 case passed on both revisions. This was a CPU-only test with mocked Vulkan commands.

**@⁠moffington** on GitHub · 2026-11-24 05:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/418#issuecomment-7122867632) Tested c9099b0c on Linux. replay_uav_clear_offset passed. I ran the same test against the merge-base replay code (d2cd6c0d), and all three new rejection assertions failed as expected for misaligned backing offsets with 16-, 4-, and 2-byte elements. The R8 case passed on both revisions. This was a CPU-only test with mocked Vulkan commands.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 16:24 UTC · original

Merging.

Why merged: A buffer UAV clear whose backing buffer_offset is misaligned for 2-, 4- or 16-byte elements is now refused before vkCmdFillBuffer/vkCmdUpdateBuffer, instead of recording an invalid command. R8 still replays. moffington showed the new assertions fail on the merge base.

Why it's good: replay_uav_clear_offset passes both plain and under validation, with 0 errors.

Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Alone on main, its tests passed both plain and with LEONIDA_VK_VALIDATION=1. Cumulative: #388, #418, #421, #434, #470, #484, #485, #486 and #488 merged onto main 1504c1af6 (a225e593e). There the repo-consistency set gives 566 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 306/308. The 2 failures are the same pre-existing ones as main (d3d12_execute_indirect_validation, d3d12_depth_resolve), with the same skip set. git merge-tree against current main 98a977b4b: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes.

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

**@⁠QemmHD** on GitHub · 2026-11-24 16:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/418#issuecomment-7137621674) Merging. **Why merged:** A buffer UAV clear whose backing buffer_offset is misaligned for 2-, 4- or 16-byte elements is now refused before vkCmdFillBuffer/vkCmdUpdateBuffer, instead of recording an invalid command. R8 still replays. moffington showed the new assertions fail on the merge base. **Why it's good:** replay_uav_clear_offset passes both plain and under validation, with 0 errors. Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Alone on main, its tests passed both plain and with LEONIDA_VK_VALIDATION=1. Cumulative: #388, #418, #421, #434, #470, #484, #485, #486 and #488 merged onto main 1504c1af6 (a225e593e). There the repo-consistency set gives 566 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 306/308. The 2 failures are the same pre-existing ones as main (d3d12_execute_indirect_validation, d3d12_depth_resolve), with the same skip set. git merge-tree against current main 98a977b4b: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.