vulkan_replay: enforce index buffer offset alignment in prepare_draw (VK-DRAW-INDEX-OFFSET-ALIGN) #539

Open
github-bot wants to merge 1 commit from github-bot/gh-539 into main AGit
Owner

@⁠tahffo68hk-hash on GitHub · 2026-11-24 07:14 UTC · original
tahffo68hk-hash:fix/vk-draw-index-offset-align → main · +124 −0 · 3 files · 1 commits · open

Summary

detail::prepare_draw() in runtime/src/vulkan_replay.h resolves an indexed draw's draw.index.va via vk.buffer_at(...) into p.index_offset (or falls back to vk.zero_offset when unbound) and later emits vk.bind_index_buffer(command_buffer, d.index_buffer, d.index_offset, d.index_type) without checking alignment of p.index_offset against the index element size.

Vulkan requires offset in vkCmdBindIndexBuffer to be a multiple of the index type's size (2 bytes for VK_INDEX_TYPE_UINT16, 4 bytes for VK_INDEX_TYPE_UINT32; VUID-vkCmdBindIndexBuffer-offset-08783), matching D3D12's IASetIndexBuffer alignment requirement. Passing a misaligned offset previously bypassed prepare_draw() and reached vkCmdBindIndexBuffer directly.

Fix

  • Check p.index_offset % index_align != 0 (index_align = 4 for VK_INDEX_TYPE_UINT32, 2 for VK_INDEX_TYPE_UINT16) in detail::prepare_draw() (runtime/src/vulkan_replay.h), returning ReplayResult::invalid_draw before any Vulkan command is recorded.
  • Add CPU-only replay unit test runtime/test/replay_index_align_test.cpp (registered as replay_index_align in runtime/CMakeLists.txt) covering aligned and misaligned offsets for DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R32_UINT, and unbound zero_buffer fallback, verifying vkCmdBindIndexBuffer is never invoked on rejection.

Verification

  • Built and ran replay_index_align_test.exe and replay_copy_offset_test.exe (MSVC 19.44, Ninja, C++20, CPU-only mock-callback replay tests — not real GPU execution):
    • replay_index_align: 10/10 cases OK
    • replay_copy_offset: CopyResource and CopyTexture apply ResourceBinding::buffer_offset

Coordination

  • Claimed on #14 as VK-DRAW-INDEX-OFFSET-ALIGN (no overlapping open PRs or claims; does not touch ExecuteIndirect / #499).
  • AI-assisted: yes.
中文翻译

摘要

runtime/src/vulkan_replay.h 中的 detail::prepare_draw() 在解析索引绘制的 draw.index.va(或未绑定时回退至 vk.zero_offset)得到 p.index_offset 后,未校验其与索引元素大小的对齐关系,直接交给 vkCmdBindIndexBuffer。

根据 Vulkan 规范 VUID-vkCmdBindIndexBuffer-offset-08783(与 D3D12 IASetIndexBuffer 要求一致),offset 必须按索引类型大小对齐(VK_INDEX_TYPE_UINT16 为 2 字节,VK_INDEX_TYPE_UINT32 为 4 字节)。此前未对齐的偏移会绕过准备阶段直接触发 Vulkan 验证错误。

修复

  • 在 detail::prepare_draw() 中增加 p.index_offset % index_align 校验(UINT32 为 4、UINT16 为 2),未对齐时返回 ReplayResult::invalid_draw,阻止录制非法 vkCmdBindIndexBuffer。
  • 新增 CPU 单元测试 runtime/test/replay_index_align_test.cpp(注册为 replay_index_align),覆盖 R16_UINT、R32_UINT 及 zero_buffer 回退路径的对齐/未对齐用例,并确认拒绝路径不会调用 vkCmdBindIndexBuffer。

验证

  • 本地编译并运行 replay_index_align_test.exe(10/10 通过)与 replay_copy_offset_test.exe(通过)(均为 CPU 回调测试,非真实 GPU 执行)。

协同

  • 已在 #14 认领 VK-DRAW-INDEX-OFFSET-ALIGN(无重复 PR/认领,未改动 #499 涉及的 ExecuteIndirect)。
  • AI 辅助:是。
**@⁠tahffo68hk-hash** on GitHub · 2026-11-24 07:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/539) `tahffo68hk-hash:fix/vk-draw-index-offset-align` → `main` · +124 −0 · 3 files · 1 commits · open ## Summary `detail::prepare_draw()` in `runtime/src/vulkan_replay.h` resolves an indexed draw's `draw.index.va` via `vk.buffer_at(...)` into `p.index_offset` (or falls back to `vk.zero_offset` when unbound) and later emits `vk.bind_index_buffer(command_buffer, d.index_buffer, d.index_offset, d.index_type)` without checking alignment of `p.index_offset` against the index element size. Vulkan requires `offset` in `vkCmdBindIndexBuffer` to be a multiple of the index type's size (`2` bytes for `VK_INDEX_TYPE_UINT16`, `4` bytes for `VK_INDEX_TYPE_UINT32`; `VUID-vkCmdBindIndexBuffer-offset-08783`), matching D3D12's `IASetIndexBuffer` alignment requirement. Passing a misaligned offset previously bypassed `prepare_draw()` and reached `vkCmdBindIndexBuffer` directly. ### Fix - Check `p.index_offset % index_align != 0` (`index_align = 4` for `VK_INDEX_TYPE_UINT32`, `2` for `VK_INDEX_TYPE_UINT16`) in `detail::prepare_draw()` (`runtime/src/vulkan_replay.h`), returning `ReplayResult::invalid_draw` before any Vulkan command is recorded. - Add CPU-only replay unit test `runtime/test/replay_index_align_test.cpp` (registered as `replay_index_align` in `runtime/CMakeLists.txt`) covering aligned and misaligned offsets for `DXGI_FORMAT_R16_UINT`, `DXGI_FORMAT_R32_UINT`, and unbound `zero_buffer` fallback, verifying `vkCmdBindIndexBuffer` is never invoked on rejection. ### Verification - Built and ran `replay_index_align_test.exe` and `replay_copy_offset_test.exe` (MSVC 19.44, Ninja, C++20, CPU-only mock-callback replay tests — not real GPU execution): - `replay_index_align: 10/10 cases OK` - `replay_copy_offset: CopyResource and CopyTexture apply ResourceBinding::buffer_offset` ### Coordination - Claimed on #14 as `VK-DRAW-INDEX-OFFSET-ALIGN` (no overlapping open PRs or claims; does not touch `ExecuteIndirect` / #499). - AI-assisted: yes. <details> <summary>中文翻译</summary> ## 摘要 `runtime/src/vulkan_replay.h` 中的 `detail::prepare_draw()` 在解析索引绘制的 `draw.index.va`(或未绑定时回退至 `vk.zero_offset`)得到 `p.index_offset` 后,未校验其与索引元素大小的对齐关系,直接交给 `vkCmdBindIndexBuffer`。 根据 Vulkan 规范 `VUID-vkCmdBindIndexBuffer-offset-08783`(与 D3D12 `IASetIndexBuffer` 要求一致),`offset` 必须按索引类型大小对齐(`VK_INDEX_TYPE_UINT16` 为 2 字节,`VK_INDEX_TYPE_UINT32` 为 4 字节)。此前未对齐的偏移会绕过准备阶段直接触发 Vulkan 验证错误。 ### 修复 - 在 `detail::prepare_draw()` 中增加 `p.index_offset % index_align` 校验(`UINT32` 为 4、`UINT16` 为 2),未对齐时返回 `ReplayResult::invalid_draw`,阻止录制非法 `vkCmdBindIndexBuffer`。 - 新增 CPU 单元测试 `runtime/test/replay_index_align_test.cpp`(注册为 `replay_index_align`),覆盖 `R16_UINT`、`R32_UINT` 及 `zero_buffer` 回退路径的对齐/未对齐用例,并确认拒绝路径不会调用 `vkCmdBindIndexBuffer`。 ### 验证 - 本地编译并运行 `replay_index_align_test.exe`(10/10 通过)与 `replay_copy_offset_test.exe`(通过)(均为 CPU 回调测试,非真实 GPU 执行)。 ### 协同 - 已在 #14 认领 `VK-DRAW-INDEX-OFFSET-ALIGN`(无重复 PR/认领,未改动 #499 涉及的 `ExecuteIndirect`)。 - AI 辅助:是。 </details>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:07:19 +00:00
Author
Owner

@⁠Sangonapou on GitHub · 2026-11-24 14:54 UTC · original

Tested 9ef74697 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files.

ctest -R ^replay_index_align$ -V: replay_index_align: 10/10 cases OK.

**@⁠Sangonapou** on GitHub · 2026-11-24 14:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/539#issuecomment-7135134549) Tested 9ef74697 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass Runtime built at this head with `scripts\build_runtime.ps1` (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files. `ctest -R ^replay_index_align$ -V`: `replay_index_align: 10/10 cases OK`.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/539/head:github-bot/gh-539'
git switch 'github-bot/gh-539'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-539'
git switch 'github-bot/gh-539'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-539'
git switch 'github-bot/gh-539'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-539'
git switch 'main'
git merge --squash 'github-bot/gh-539'
git switch 'main'
git merge --ff-only 'github-bot/gh-539'
git switch 'main'
git merge 'github-bot/gh-539'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.