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

Open
opened 2026-11-26 20:18:22 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/539
Author: @tahffo68hk-hash
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: fix/vk-draw-index-offset-align


📝 Commits (1)

  • 9ef7469 vulkan_replay: enforce index buffer offset alignment in prepare_draw (VK-DRAW-INDEX-OFFSET-ALIGN)

📊 Changes

3 files changed (+124 additions, -0 deletions)

View changed files

📝 runtime/CMakeLists.txt (+7 -0)
📝 runtime/src/vulkan_replay.h (+3 -0)
➕ runtime/test/replay_index_align_test.cpp (+114 -0)

📄 Description

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 辅助:是。

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/539 **Author:** [@tahffo68hk-hash](https://github.com/tahffo68hk-hash) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/vk-draw-index-offset-align` --- ### 📝 Commits (1) - [`9ef7469`](https://github.com/ubiquitarianism/leonida/commit/9ef746972eb08236b4b2d78ed87230365fa18a15) vulkan_replay: enforce index buffer offset alignment in prepare_draw (VK-DRAW-INDEX-OFFSET-ALIGN) ### 📊 Changes **3 files changed** (+124 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+7 -0) 📝 `runtime/src/vulkan_replay.h` (+3 -0) ➕ `runtime/test/replay_index_align_test.cpp` (+114 -0) </details> ### 📄 Description ## 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> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#587
No description provided.