vulkan_replay: enforce 4-byte vkCmdFillBuffer alignment in zero_compressed_image #528

Manually merged
github merged 1 commit from gh-pr/528/head into gh-pr/528/base 2026-11-26 09:06:20 +00:00
Owner

@⁠tahffo68hk-hash on GitHub · 2026-11-24 06:58 UTC · original
tahffo68hk-hash:fix/vk-zero-init-fill-align → main · +89 −6 · 2 files · 1 commits · merged 2026-11-25 00:02 UTC

Summary

(AI-assisted) Fixes VK-ZERO-INIT-FILL-ALIGN in runtime/src/vulkan_replay.h:

detail::initialize() routes not only block-compressed (fmt.block > 1) textures, but also uncompressed color textures on copy-only queues (!can_clear && color) into detail::zero_compressed_image(). When vk.zero_buffer is absent (the vk.copy_scratch + vk.fill_buffer fallback), zero_compressed_image() previously rounded fill_bytes and align only to fmt.bytes, assuming fmt.bytes was 8 or 16.

For 1-byte (R8_UNORM, e.g. 3x3 = 9 B) or 2-byte (R16_UNORM, e.g. 3x1 = 6 B) color textures with non-4-byte-multiple sizes, this passed a non-4-byte-aligned size (and, with non-4-aligned scratch offsets, a non-4-byte-aligned dstOffset) to vkCmdFillBuffer, violating VUID-vkCmdFillBuffer-dstOffset-00024 and VUID-vkCmdFillBuffer-size-00028.

Changes

  • runtime/src/vulkan_replay.h: In zero_compressed_image(), use align = std::max<VkDeviceSize>(4, fmt.bytes) when sizing the scratch request, computing pad, and rounding usable down to a multiple of align before calling vk.fill_buffer().
  • runtime/test/vk_bc_zero_init_test.cpp:
    • Add real-GPU Vulkan copy-queue (VulkanQueue::QueueKind::copy) zero-init readback tests for poisoned (0x5A) 3x3 VK_FORMAT_R8_UNORM and 3x1 VK_FORMAT_R16_UNORM textures via copy_scratch (no zero_buffer).
    • Add a direct ReplayFunctions alignment check verifying dstOffset % 4 == 0 and size % 4 == 0 for 3x3 R8_UNORM even with a deliberately unaligned scratch offset (offset = 2).

Verification

  • Real Vulkan GPU execution & readback (vk_bc_zero_init_test.exe, MSVC 19.44 + Ninja):
    • NVIDIA GeForce RTX 5060 Ti: all cases including copy-queue zero-init (R8_UNORM 3x3 via copy_scratch) and copy-queue zero-init (R16_UNORM 3x1 via copy_scratch) passed (all readback texels 0x00, 0 validation errors).
    • AMD Radeon(TM) Graphics (gfx1036, RDNA2): all cases passed identically.
中文翻译

摘要

(AI 辅助)修复 runtime/src/vulkan_replay.h 中的 VK-ZERO-INIT-FILL-ALIGN:

detail::initialize() 不仅会将 BC 压缩纹理送入 detail::zero_compressed_image(),在仅拷贝队列(!can_clear && color)上也会将未压缩彩色纹理送入该函数。当未配置 vk.zero_buffer 而走 copy_scratch + vk.fill_buffer 回退路径时,原实现假设 fmt.bytes 恒为 8 或 16,对 1 字节(如 R8_UNORM 3x3 = 9B)或 2 字节(如 R16_UNORM 3x1 = 6B)纹理会向 vkCmdFillBuffer 传入非 4 字节对齐的 size / dstOffset,违反 VUID-vkCmdFillBuffer-dstOffset-00024 与 VUID-vkCmdFillBuffer-size-00028。

改动

  • runtime/src/vulkan_replay.h:在 zero_compressed_image() 中将 align 提升为 std::max<VkDeviceSize>(4, fmt.bytes),确保 vk.fill_buffer 的偏移与长度均满足 4 字节与 fmt.bytes 对齐。
  • runtime/test/vk_bc_zero_init_test.cpp:新增仅拷贝队列下 R8_UNORM 3x3 与 R16_UNORM 3x1 的真实 GPU 零初始化读回测试,以及非对齐暂存偏移下的 vkCmdFillBuffer 对齐断言。

验证

  • 真实 Vulkan GPU 执行与读回(vk_bc_zero_init_test.exe):在 NVIDIA RTX 5060 Ti 与 AMD Radeon(TM) Graphics(gfx1036) 上均全部通过,读回纹素全为 0x00。
**@⁠tahffo68hk-hash** on GitHub · 2026-11-24 06:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/528) `tahffo68hk-hash:fix/vk-zero-init-fill-align` → `main` · +89 −6 · 2 files · 1 commits · merged 2026-11-25 00:02 UTC ### Summary (AI-assisted) Fixes `VK-ZERO-INIT-FILL-ALIGN` in `runtime/src/vulkan_replay.h`: `detail::initialize()` routes not only block-compressed (`fmt.block > 1`) textures, but also uncompressed color textures on copy-only queues (`!can_clear && color`) into `detail::zero_compressed_image()`. When `vk.zero_buffer` is absent (the `vk.copy_scratch` + `vk.fill_buffer` fallback), `zero_compressed_image()` previously rounded `fill_bytes` and `align` only to `fmt.bytes`, assuming `fmt.bytes` was 8 or 16. For 1-byte (`R8_UNORM`, e.g. `3x3 = 9 B`) or 2-byte (`R16_UNORM`, e.g. `3x1 = 6 B`) color textures with non-4-byte-multiple sizes, this passed a non-4-byte-aligned `size` (and, with non-4-aligned scratch offsets, a non-4-byte-aligned `dstOffset`) to `vkCmdFillBuffer`, violating `VUID-vkCmdFillBuffer-dstOffset-00024` and `VUID-vkCmdFillBuffer-size-00028`. ### Changes - `runtime/src/vulkan_replay.h`: In `zero_compressed_image()`, use `align = std::max<VkDeviceSize>(4, fmt.bytes)` when sizing the scratch request, computing `pad`, and rounding `usable` down to a multiple of `align` before calling `vk.fill_buffer()`. - `runtime/test/vk_bc_zero_init_test.cpp`: - Add real-GPU Vulkan copy-queue (`VulkanQueue::QueueKind::copy`) zero-init readback tests for poisoned (`0x5A`) `3x3` `VK_FORMAT_R8_UNORM` and `3x1` `VK_FORMAT_R16_UNORM` textures via `copy_scratch` (no `zero_buffer`). - Add a direct `ReplayFunctions` alignment check verifying `dstOffset % 4 == 0` and `size % 4 == 0` for `3x3` `R8_UNORM` even with a deliberately unaligned scratch offset (`offset = 2`). ### Verification - **Real Vulkan GPU execution & readback (`vk_bc_zero_init_test.exe`, MSVC 19.44 + Ninja)**: - **NVIDIA GeForce RTX 5060 Ti**: all cases including `copy-queue zero-init (R8_UNORM 3x3 via copy_scratch)` and `copy-queue zero-init (R16_UNORM 3x1 via copy_scratch)` passed (all readback texels `0x00`, 0 validation errors). - **AMD Radeon(TM) Graphics (`gfx1036`, RDNA2)**: all cases passed identically. <details> <summary>中文翻译</summary> ### 摘要 (AI 辅助)修复 `runtime/src/vulkan_replay.h` 中的 `VK-ZERO-INIT-FILL-ALIGN`: `detail::initialize()` 不仅会将 BC 压缩纹理送入 `detail::zero_compressed_image()`,在仅拷贝队列(`!can_clear && color`)上也会将未压缩彩色纹理送入该函数。当未配置 `vk.zero_buffer` 而走 `copy_scratch` + `vk.fill_buffer` 回退路径时,原实现假设 `fmt.bytes` 恒为 8 或 16,对 1 字节(如 `R8_UNORM 3x3 = 9B`)或 2 字节(如 `R16_UNORM 3x1 = 6B`)纹理会向 `vkCmdFillBuffer` 传入非 4 字节对齐的 `size` / `dstOffset`,违反 `VUID-vkCmdFillBuffer-dstOffset-00024` 与 `VUID-vkCmdFillBuffer-size-00028`。 ### 改动 - `runtime/src/vulkan_replay.h`:在 `zero_compressed_image()` 中将 `align` 提升为 `std::max<VkDeviceSize>(4, fmt.bytes)`,确保 `vk.fill_buffer` 的偏移与长度均满足 4 字节与 `fmt.bytes` 对齐。 - `runtime/test/vk_bc_zero_init_test.cpp`:新增仅拷贝队列下 `R8_UNORM 3x3` 与 `R16_UNORM 3x1` 的真实 GPU 零初始化读回测试,以及非对齐暂存偏移下的 `vkCmdFillBuffer` 对齐断言。 ### 验证 - **真实 Vulkan GPU 执行与读回(`vk_bc_zero_init_test.exe`)**:在 **NVIDIA RTX 5060 Ti** 与 **AMD Radeon(TM) Graphics(`gfx1036`)** 上均全部通过,读回纹素全为 `0x00`。 </details>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:06:17 +00:00
github manually merged commit 1d4310bc71 into gh-pr/528/base 2026-11-26 09:06:19 +00:00
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-24 07:55 UTC · original

Review of head 1d4310bc71ed62a86156a26950ff96fe560ccbf2.

Environment: Linux (WSL2), g++ C++20 with -fsanitize=address,undefined, Vulkan-Headers only. No GPU, no MSVC.

What ran:

  • vk_bc_zero_init_test returns 77 (skip) without a Vulkan device before it reaches the new mock check. So I copied the "Direct ReplayFunctions check" block unchanged into a standalone file that only includes vulkan_replay.h, and ran it:
    • PR head: passes. The fill is dstOffset=4, size=8 for R8 3x3 with scratch offset 2. No ASan/UBSan reports.
    • vulkan_replay.h from origin/main: fails the % 4 / size assertion, so the check catches the bug.
  • Read the fix against VUID-vkCmdFillBuffer-dstOffset-00024 / -size-00028. align = max(4, fmt.bytes) is still a multiple of fmt.bytes for every block size (1, 2, 4, 8, 16), so bufferOffset stays valid for vkCmdCopyBufferToImage.

Finding (non-blocking, from reading the code, not executed): fill_bytes is requested without room for pad. With an unaligned scratch offset, zero_cap can end up smaller than needed: 8 < 9 in the mock case. The image is still fully covered because the row-chunk path takes over (rows_per_copy = 2), so it costs extra regions, not correctness. Requesting fill_bytes + align - 4 (or + align) would keep the single-region path. The old code had the same shortfall.

Not tested: the new real-GPU copy-queue readback cases (no GPU here), validation-layer output, and the MSVC build.


Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account.

**@⁠kawaii-shakal** on GitHub · 2026-11-24 07:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/528#issuecomment-7125517946) Review of head `1d4310bc71ed62a86156a26950ff96fe560ccbf2`. Environment: Linux (WSL2), g++ C++20 with `-fsanitize=address,undefined`, Vulkan-Headers only. No GPU, no MSVC. What ran: - `vk_bc_zero_init_test` returns 77 (skip) without a Vulkan device before it reaches the new mock check. So I copied the "Direct ReplayFunctions check" block unchanged into a standalone file that only includes `vulkan_replay.h`, and ran it: - PR head: passes. The fill is `dstOffset=4`, `size=8` for R8 3x3 with scratch offset 2. No ASan/UBSan reports. - `vulkan_replay.h` from `origin/main`: fails the `% 4` / size assertion, so the check catches the bug. - Read the fix against `VUID-vkCmdFillBuffer-dstOffset-00024` / `-size-00028`. `align = max(4, fmt.bytes)` is still a multiple of `fmt.bytes` for every block size (1, 2, 4, 8, 16), so `bufferOffset` stays valid for `vkCmdCopyBufferToImage`. Finding (non-blocking, from reading the code, not executed): `fill_bytes` is requested without room for `pad`. With an unaligned scratch offset, `zero_cap` can end up smaller than `needed`: 8 < 9 in the mock case. The image is still fully covered because the row-chunk path takes over (`rows_per_copy = 2`), so it costs extra regions, not correctness. Requesting `fill_bytes + align - 4` (or `+ align`) would keep the single-region path. The old code had the same shortfall. Not tested: the new real-GPU copy-queue readback cases (no GPU here), validation-layer output, and the MSVC build. --- _Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account._
Author
Owner

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

Tested 1d4310bc on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows on real hardware: pass

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 ^vk_bc_zero_init$ -V: passed:

bc zero-init (copy_scratch): partial CopyTextureRegion and 2-mip CopyResource ok
copy-queue zero-init (R8_UNORM 3x3 via copy_scratch): ok
copy-queue zero-init (R16_UNORM 3x1 via copy_scratch): ok
vk_bc_zero_init_test: ok
**@⁠Sangonapou** on GitHub · 2026-11-24 19:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/528#issuecomment-7141739426) Tested 1d4310bc on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows on real hardware: pass 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 ^vk_bc_zero_init$ -V`: passed: ``` bc zero-init (copy_scratch): partial CopyTextureRegion and 2-mip CopyResource ok copy-queue zero-init (R8_UNORM 3x3 via copy_scratch): ok copy-queue zero-init (R16_UNORM 3x1 via copy_scratch): ok vk_bc_zero_init_test: ok ```
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 00:01 UTC · original

Merging.

Why merged: VK-ZERO-INIT-FILL-ALIGN: zero_compressed_image aligns the fill to max(4, fmt.bytes) and rounds the usable size down, so vkCmdFillBuffer's offset and size are 4-aligned. That also makes the copy offset 4-aligned on transfer queues, which closes the VUID-07737 gap noted on #434. kawaii-shakal (ASan mock) and Sangonapou (RTX 3080 Ti) checked it.

Why it's good: vk_bc_zero_init, vk_queue and the replay_* tests: 12/12 both plain and under validation.

Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Merged alone onto main 0f98d193c, its ctests pass both plain and with LEONIDA_VK_VALIDATION=1. Cumulative with the rest of this batch on main 507029d1e: the repo-consistency set gives 578 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 317/318 with the usual 11 skips; the only failure is main's pre-existing d3d12_depth_resolve. All d3d12_* tests under validation: 119/120 with 0 VUIDs. The semantic suite has only the known environment-only failure. git merge-tree against current main 9c5e70661: 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-25 00:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/528#issuecomment-7148062302) Merging. **Why merged:** VK-ZERO-INIT-FILL-ALIGN: zero_compressed_image aligns the fill to max(4, fmt.bytes) and rounds the usable size down, so vkCmdFillBuffer's offset and size are 4-aligned. That also makes the copy offset 4-aligned on transfer queues, which closes the VUID-07737 gap noted on #434. kawaii-shakal (ASan mock) and Sangonapou (RTX 3080 Ti) checked it. **Why it's good:** vk_bc_zero_init, vk_queue and the replay_* tests: 12/12 both plain and under validation. Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Merged alone onto main 0f98d193c, its ctests pass both plain and with LEONIDA_VK_VALIDATION=1. Cumulative with the rest of this batch on main 507029d1e: the repo-consistency set gives 578 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 317/318 with the usual 11 skips; the only failure is main's pre-existing d3d12_depth_resolve. All d3d12_* tests under validation: 119/120 with 0 VUIDs. The semantic suite has only the known environment-only failure. git merge-tree against current main 9c5e70661: 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.