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

Closed
tahffo68hk-hash wants to merge 1 commit from upstream-pr/539 into main
tahffo68hk-hash commented 2026-11-24 07:14:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tahffo68hk-hash on 2026-11-24T07:14:34Z.


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 辅助:是。
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5467803006 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/539). Originally posted by [tahffo68hk-hash](https://github.com/tahffo68hk-hash) on 2026-11-24T07:14:34Z. --- ## 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>
Sangonapou commented 2026-11-24 14:54:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Sangonapou on 2026-11-24T14:54:08Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7135134549 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/539#issuecomment-7135134549). Originally posted by [Sangonapou](https://github.com/Sangonapou) on 2026-11-24T14:54:08Z. --- 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`.
zmasi commented 2026-11-29 02:00:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-29T02:00:31Z.


Verification Report (9ef746972e merged into main 59e65849b0)

  • Environment: Windows 11, MSVC 19.44 (Ninja Release), NVIDIA GeForce RTX 3090 (driver 610.88), Vulkan SDK 1.4.363.1. No game data used (check_no_game_data.py rc=0).
  • Diff & Merge Audit: Merged 9ef746972e cleanly into origin/main (59e65849b0, 0 conflicts; 3 files changed: runtime/CMakeLists.txt, runtime/src/vulkan_replay.h, runtime/test/replay_index_align_test.cpp).
    • Checked composition with main's IndirectDrawArgs path in detail::prepare_draw(): for indirect indexed draws (arguments != nullptr), unaligned p.index_offset and null draw.index.va already take indirect_stats::skip(...) (p.skipped = true; return ReplayResult::success;) earlier in if (draw.indexed), while direct indexed draws (arguments == nullptr) previously fell through without checking p.index_offset % index_align on either resolved buffers or vk.zero_offset. Placing the VUID-vkCmdBindIndexBuffer-offset-08783 guard at the end of if (draw.indexed) closes direct-draw alignment without altering indirect-draw soft-skip behavior.

Red -> Green Results

Target Base main vulkan_replay.h + #539 test Merged main + #539 (9ef746972e)
replay_index_align_test (CPU replay) RED (rc=1: misaligned DXGI_FORMAT_R16_UINT offset 1 returns ReplayResult::success and invokes vkCmdBindIndexBuffer) GREEN (rc=0: replay_index_align: 10/10 cases OK across R16_UINT, R32_UINT, and misaligned zero_offset fallback; bind_index_calls == 0 on every rejected draw)
Combined merge with #778 + #1179 + #1443 (d3d12_indirect_draw, d3d12_msaa_draw, replay_index_align on RTX 3090, LEONIDA_VK_VALIDATION=1) — GREEN (rc=0, 0 validation errors)
  • Verdict: PASS (reproduces the misaligned vkCmdBindIndexBuffer bypass on main, passes 10/10 alignment cases, and composes cleanly with main's indirect-draw path).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249151517 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/539#issuecomment-7249151517). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-29T02:00:31Z. --- ### Verification Report (`9ef746972e` merged into `main` `59e65849b0`) - **Environment**: Windows 11, MSVC 19.44 (Ninja Release), NVIDIA GeForce RTX 3090 (driver `610.88`), Vulkan SDK `1.4.363.1`. No game data used (`check_no_game_data.py` `rc=0`). - **Diff & Merge Audit**: Merged `9ef746972e` cleanly into `origin/main` (`59e65849b0`, 0 conflicts; 3 files changed: `runtime/CMakeLists.txt`, `runtime/src/vulkan_replay.h`, `runtime/test/replay_index_align_test.cpp`). - Checked composition with `main`'s `IndirectDrawArgs` path in `detail::prepare_draw()`: for indirect indexed draws (`arguments != nullptr`), unaligned `p.index_offset` and null `draw.index.va` already take `indirect_stats::skip(...)` (`p.skipped = true; return ReplayResult::success;`) earlier in `if (draw.indexed)`, while direct indexed draws (`arguments == nullptr`) previously fell through without checking `p.index_offset % index_align` on either resolved buffers or `vk.zero_offset`. Placing the `VUID-vkCmdBindIndexBuffer-offset-08783` guard at the end of `if (draw.indexed)` closes direct-draw alignment without altering indirect-draw soft-skip behavior. #### Red -> Green Results | Target | Base `main` `vulkan_replay.h` + `#539` test | Merged `main` + `#539` (`9ef746972e`) | |---|---|---| | `replay_index_align_test` (CPU replay) | **RED** (`rc=1`: misaligned `DXGI_FORMAT_R16_UINT` offset `1` returns `ReplayResult::success` and invokes `vkCmdBindIndexBuffer`) | **GREEN** (`rc=0`: `replay_index_align: 10/10 cases OK` across `R16_UINT`, `R32_UINT`, and misaligned `zero_offset` fallback; `bind_index_calls == 0` on every rejected draw) | | Combined merge with `#778` + `#1179` + `#1443` (`d3d12_indirect_draw`, `d3d12_msaa_draw`, `replay_index_align` on RTX 3090, `LEONIDA_VK_VALIDATION=1`) | — | **GREEN** (`rc=0`, `0` validation errors) | - **Verdict**: **PASS** (reproduces the misaligned `vkCmdBindIndexBuffer` bypass on `main`, passes 10/10 alignment cases, and composes cleanly with `main`'s indirect-draw path). AI-assisted.
enlight3d commented 2026-11-29 08:59:30 +00:00 (Migrated from github.com)

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


Validation run of this pull request.

  • PR head: 9ef746972e. main: 54b2a81e95. Merged cleanly (git merge --no-edit --no-ff pr-539, no conflicts); tested at the merge commit.
  • Commands:
    • cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON
    • cmake --build runtime/build --target gta6rt replay_index_align_test replay_copy_offset_test vk_command_replay_test command_list_test -j 8
    • ctest -R "^(replay_index_align|replay_copy_offset|vk_command_replay|command_list)$" --output-on-failure -j1
  • Green (merged): 4 of 4 passed, none skipped. replay_index_align is a CPU-only mock-callback test (no GPU is involved); the other three share vulkan_replay.h or the command list with the change.
  • Red (behavioural, no API change): runtime/src/vulkan_replay.h taken back from main, test and runtime/CMakeLists.txt kept. The test builds and replay_index_align fails at replay_index_align_test.cpp:88 (replay(list, VK_NULL_HANDLE, no_resolve, vk) == expected: the misaligned offset is not rejected with invalid_draw).
  • Link: no new imports vs main (gta6rt.exe, 714 imports on both).
  • Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7253462809 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/539#issuecomment-7253462809). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-11-29T08:59:30Z. --- Validation run of this pull request. - PR head: 9ef746972eb08236b4b2d78ed87230365fa18a15. main: 54b2a81e95c470c953ddee6177fd6255cbaf041d. Merged cleanly (`git merge --no-edit --no-ff pr-539`, no conflicts); tested at the merge commit. - Commands: - `cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON` - `cmake --build runtime/build --target gta6rt replay_index_align_test replay_copy_offset_test vk_command_replay_test command_list_test -j 8` - `ctest -R "^(replay_index_align|replay_copy_offset|vk_command_replay|command_list)$" --output-on-failure -j1` - Green (merged): 4 of 4 passed, none skipped. `replay_index_align` is a CPU-only mock-callback test (no GPU is involved); the other three share `vulkan_replay.h` or the command list with the change. - Red (behavioural, no API change): `runtime/src/vulkan_replay.h` taken back from main, test and `runtime/CMakeLists.txt` kept. The test builds and `replay_index_align` fails at `replay_index_align_test.cpp:88` (`replay(list, VK_NULL_HANDLE, no_resolve, vk) == expected`: the misaligned offset is not rejected with `invalid_draw`). - Link: no new imports vs main (gta6rt.exe, 714 imports on both). - Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.
fiestorra commented 2026-11-30 09:51:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-11-30T09:51:50Z.


Independent verification on this machine (Windows 11, MSVC 19.44.35226), with a negative control that bites.

1. It applies. merge-tree clean; the commits cherry-pick onto main 4014af7fe without a conflict. Three files: runtime/CMakeLists.txt, runtime/src/vulkan_replay.h (the change) and the new runtime/test/replay_index_align_test.cpp.

2. Its test is a CPU test - the header says so ("no GPU") - and with the branch it passes:

ctest -R 'replay_index_align'
100% tests passed, 0 tests failed out of 1

3. The control - revert only the header, keep the test:

git checkout origin/main -- runtime/src/vulkan_replay.h
cmake --build … --target replay_index_align_test
ctest -R 'replay_index_align' --output-on-failure
0% tests passed, 1 tests failed out of 1
FAIL …/replay_index_align_test.cpp:88: replay(list, VK_NULL_HANDLE, no_resolve, vk) == expected

So the assertion is about the change: an indexed draw whose index-buffer offset is not aligned to the index size (VUID-vkCmdBindIndexBuffer-offset-08783) must be refused by the replay, and without the header change the replay returns something else. The failure is at line 88, on the null-handle case, which is the cheapest case to reason about - no device needed.

What I did not test: the rest of vulkan_replay.h's behaviour, and whether the VUID's own conditions match what the test feeds it. I checked that the test fails without the change, not that the change is right.

Why I am commenting: I verify open pull requests that replay onto current main by applying them in a scratch worktree, building the targets their tests name in ctest --show-only=json-v1, running them, and then reverting only the source change to see whether the tests still pass.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7274789507 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/539#issuecomment-7274789507). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-11-30T09:51:50Z. --- Independent verification on this machine (Windows 11, MSVC 19.44.35226), with a negative control that bites. **1. It applies.** `merge-tree` clean; the commits cherry-pick onto `main` `4014af7fe` without a conflict. Three files: `runtime/CMakeLists.txt`, `runtime/src/vulkan_replay.h` (the change) and the new `runtime/test/replay_index_align_test.cpp`. **2. Its test is a CPU test - the header says so ("no GPU") - and with the branch it passes:** ``` ctest -R 'replay_index_align' 100% tests passed, 0 tests failed out of 1 ``` **3. The control - revert only the header, keep the test:** ``` git checkout origin/main -- runtime/src/vulkan_replay.h cmake --build … --target replay_index_align_test ctest -R 'replay_index_align' --output-on-failure 0% tests passed, 1 tests failed out of 1 FAIL …/replay_index_align_test.cpp:88: replay(list, VK_NULL_HANDLE, no_resolve, vk) == expected ``` **So the assertion is about the change**: an indexed draw whose index-buffer offset is not aligned to the index size (`VUID-vkCmdBindIndexBuffer-offset-08783`) must be refused by the replay, and **without the header change the replay returns something else**. The failure is at line 88, on the null-handle case, which is the cheapest case to reason about - no device needed. **What I did not test:** the rest of `vulkan_replay.h`'s behaviour, and whether the VUID's own conditions match what the test feeds it. **I checked that the test fails without the change, not that the change is right.** **Why I am commenting:** I verify open pull requests that replay onto current `main` by applying them in a scratch worktree, building the targets their tests name in `ctest --show-only=json-v1`, running them, and then reverting **only** the source change to see whether the tests still pass. Written with an AI assistant.
aslan2142 commented 2026-12-01 16:30:37 +00:00 (Migrated from github.com)

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


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