Skip to content

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

Open
tahffo68hk-hash wants to merge 1 commit into
ProjectLeonida:mainfrom
tahffo68hk-hash:fix/vk-draw-index-offset-align
Open

tahffo68hk-hash wants to merge 1 commit into
ProjectLeonida:mainfrom
tahffo68hk-hash:fix/vk-draw-index-offset-align

Conversation

@tahffo68hk-hash

Copy link
Copy Markdown
Contributor

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

中文翻译

摘要

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 执行)。

协同

demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
…et alignment in prepare_draw (VK-DRAW-INDEX-OFFSET-ALIGN)
@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 9ef7469 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants