vulkan_backend: erase removed resource from storage_pending_ (VK-REMOVE-RESOURCE-STORAGE-PENDING) #597

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

@⁠tahffo68hk-hash on GitHub · 2026-11-24 09:35 UTC · original
tahffo68hk-hash:fix/vk-remove-resource-storage-pending → main · +179 −1 · 3 files · 1 commits · open

Summary

When a storage image (UAV) or sparse image is created, the D3D12 layer registers its ResourceId via VulkanContext::note_shader_image(id) (storage_pending_.insert(id)). On the next shader submission (VulkanQueue::execute_now()), storage_pending_ is drained and every listed ResourceId is resolved and initialized into VK_IMAGE_LAYOUT_GENERAL via detail::initialize() (which emits vkCmdClearColorImage for committed images).

In VulkanContext::remove_resource(ResourceId id) (runtime/src/vulkan_backend.h), zero_pending_ was already cleaned under storage_mutex_ for buffers destroyed before any submission, but storage_pending_ was not. As a result:

  1. Destroyed storage images leaked stale ResourceId entries in storage_pending_ until a shader submission ran.
  2. If a newly created texture reused the same ResourceId (heap address reuse of COM ID3D12Resource pointers) before the next shader submission, execute_now() resolved the stale entry to the new image, spuriously transitioning it to VK_IMAGE_LAYOUT_GENERAL and clearing its contents with vkCmdClearColorImage even though it was never passed to note_shader_image().

Fix

  • Erase id from storage_pending_ under storage_mutex_ in VulkanContext::remove_resource() (runtime/src/vulkan_backend.h), right alongside zero_pending_.
  • Add a real Vulkan GPU regression test runtime/test/vk_remove_resource_storage_pending_test.cpp (registered as vk_remove_resource_storage_pending in runtime/CMakeLists.txt) that registers Image A at id=1, calls note_shader_image(1), removes id=1, registers a non-storage Image B at reused id=1, executes a real SPIR-V compute Dispatch on VulkanQueue, and verifies zero spurious vkCmdClearColorImage calls and that Image B's layout remains VK_IMAGE_LAYOUT_UNDEFINED.

Verification (Real Dual-GPU Hardware)

  • Before fix on NVIDIA GeForce RTX 5060 Ti:
    FAIL runtime/test/vk_remove_resource_storage_pending_test.cpp:153: driver::clear_color_calls.load() == 0
  • After fix on NVIDIA GeForce RTX 5060 Ti:
    vk_remove_resource_storage_pending_test: PASS (0 spurious clears, reused ResourceId untouched)
  • After fix on AMD Radeon(TM) Graphics (GFX1036):
    vk_remove_resource_storage_pending_test: PASS (0 spurious clears, reused ResourceId untouched)

Coordination

  • Claimed on #14 as VK-REMOVE-RESOURCE-STORAGE-PENDING (no overlapping open PRs or claims).
  • AI-assisted: yes.
中文翻译

摘要

当创建存储图像(UAV)或稀疏图像时,D3D12 兼容层会通过 VulkanContext::note_shader_image(id) 将其 ResourceId 加入 storage_pending_;在下一次带着色器的提交(VulkanQueue::execute_now())中,运行时会取出这些 id 并对对应图像执行 detail::initialize(..., VK_IMAGE_LAYOUT_GENERAL)(对已提交图像会发出 vkCmdClearColorImage)。

VulkanContext::remove_resource(ResourceId id)(runtime/src/vulkan_backend.h)在 storage_mutex_ 下清理了 zero_pending_,但遗漏了清理 storage_pending_。如果旧存储图像在着色器提交前被销毁、且新创建的非存储纹理复用了相同的 ResourceId(COM 堆地址复用),下一次 Dispatch/Draw 会误将该新纹理转换到 GENERAL 并执行 vkCmdClearColorImage 清零。

修复

  • 在 VulkanContext::remove_resource() 的 storage_mutex_ 临界区内增加 storage_pending_.erase(id);。
  • 新增真实 Vulkan GPU 回归测试 runtime/test/vk_remove_resource_storage_pending_test.cpp(CMake 注册名 vk_remove_resource_storage_pending),在真实 GPU 上提交最小 SPIR-V 计算着色器 Dispatch,验证不会对复用 ResourceId 的新图像发出误清零或误布局转换。

真实双卡 GPU 验证

  • 修复前(NVIDIA RTX 5060 Ti):直接复现 clear_color_calls != 0 失败。
  • 修复后(NVIDIA RTX 5060 Ti):PASS (0 spurious clears, reused ResourceId untouched)。
  • 修复后(AMD Radeon Graphics / GFX1036):PASS (0 spurious clears, reused ResourceId untouched)。

协同

  • 已在 #14 认领 VK-REMOVE-RESOURCE-STORAGE-PENDING。
  • AI 辅助:是。
**@⁠tahffo68hk-hash** on GitHub · 2026-11-24 09:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/597) `tahffo68hk-hash:fix/vk-remove-resource-storage-pending` → `main` · +179 −1 · 3 files · 1 commits · open ## Summary When a storage image (UAV) or sparse image is created, the D3D12 layer registers its `ResourceId` via `VulkanContext::note_shader_image(id)` (`storage_pending_.insert(id)`). On the next shader submission (`VulkanQueue::execute_now()`), `storage_pending_` is drained and every listed `ResourceId` is resolved and initialized into `VK_IMAGE_LAYOUT_GENERAL` via `detail::initialize()` (which emits `vkCmdClearColorImage` for committed images). In `VulkanContext::remove_resource(ResourceId id)` (`runtime/src/vulkan_backend.h`), `zero_pending_` was already cleaned under `storage_mutex_` for buffers destroyed before any submission, **but `storage_pending_` was not**. As a result: 1. Destroyed storage images leaked stale `ResourceId` entries in `storage_pending_` until a shader submission ran. 2. If a newly created texture reused the same `ResourceId` (heap address reuse of COM `ID3D12Resource` pointers) before the next shader submission, `execute_now()` resolved the stale entry to the **new** image, spuriously transitioning it to `VK_IMAGE_LAYOUT_GENERAL` and clearing its contents with `vkCmdClearColorImage` even though it was never passed to `note_shader_image()`. ### Fix - Erase `id` from `storage_pending_` under `storage_mutex_` in `VulkanContext::remove_resource()` (`runtime/src/vulkan_backend.h`), right alongside `zero_pending_`. - Add a real Vulkan GPU regression test `runtime/test/vk_remove_resource_storage_pending_test.cpp` (registered as `vk_remove_resource_storage_pending` in `runtime/CMakeLists.txt`) that registers Image A at `id=1`, calls `note_shader_image(1)`, removes `id=1`, registers a non-storage Image B at reused `id=1`, executes a real SPIR-V compute `Dispatch` on `VulkanQueue`, and verifies zero spurious `vkCmdClearColorImage` calls and that Image B's layout remains `VK_IMAGE_LAYOUT_UNDEFINED`. ### Verification (Real Dual-GPU Hardware) - **Before fix** on NVIDIA GeForce RTX 5060 Ti: `FAIL runtime/test/vk_remove_resource_storage_pending_test.cpp:153: driver::clear_color_calls.load() == 0` - **After fix** on **NVIDIA GeForce RTX 5060 Ti**: `vk_remove_resource_storage_pending_test: PASS (0 spurious clears, reused ResourceId untouched)` - **After fix** on **AMD Radeon(TM) Graphics (GFX1036)**: `vk_remove_resource_storage_pending_test: PASS (0 spurious clears, reused ResourceId untouched)` ### Coordination - Claimed on #14 as `VK-REMOVE-RESOURCE-STORAGE-PENDING` (no overlapping open PRs or claims). - AI-assisted: yes. <details> <summary>中文翻译</summary> ## 摘要 当创建存储图像(UAV)或稀疏图像时,D3D12 兼容层会通过 `VulkanContext::note_shader_image(id)` 将其 `ResourceId` 加入 `storage_pending_`;在下一次带着色器的提交(`VulkanQueue::execute_now()`)中,运行时会取出这些 `id` 并对对应图像执行 `detail::initialize(..., VK_IMAGE_LAYOUT_GENERAL)`(对已提交图像会发出 `vkCmdClearColorImage`)。 `VulkanContext::remove_resource(ResourceId id)`(`runtime/src/vulkan_backend.h`)在 `storage_mutex_` 下清理了 `zero_pending_`,**但遗漏了清理 `storage_pending_`**。如果旧存储图像在着色器提交前被销毁、且新创建的非存储纹理复用了相同的 `ResourceId`(COM 堆地址复用),下一次 `Dispatch`/`Draw` 会误将该新纹理转换到 `GENERAL` 并执行 `vkCmdClearColorImage` 清零。 ### 修复 - 在 `VulkanContext::remove_resource()` 的 `storage_mutex_` 临界区内增加 `storage_pending_.erase(id);`。 - 新增真实 Vulkan GPU 回归测试 `runtime/test/vk_remove_resource_storage_pending_test.cpp`(CMake 注册名 `vk_remove_resource_storage_pending`),在真实 GPU 上提交最小 SPIR-V 计算着色器 `Dispatch`,验证不会对复用 `ResourceId` 的新图像发出误清零或误布局转换。 ### 真实双卡 GPU 验证 - **修复前(NVIDIA RTX 5060 Ti)**:直接复现 `clear_color_calls != 0` 失败。 - **修复后(NVIDIA RTX 5060 Ti)**:`PASS (0 spurious clears, reused ResourceId untouched)`。 - **修复后(AMD Radeon Graphics / GFX1036)**:`PASS (0 spurious clears, reused ResourceId untouched)`。 ### 协同 - 已在 #14 认领 `VK-REMOVE-RESOURCE-STORAGE-PENDING`。 - AI 辅助:是。 </details>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:10:50 +00:00
Author
Owner

@⁠trigger337 on GitHub · 2026-11-24 09:49 UTC · original

Hardware verification report for vk_remove_resource_storage_pending_test on native AMD RDNA2 GPU:

Environment:

  • GPU: AMD Radeon RX 6900 XT (RADV NAVI21, RDNA2, subgroup size 32..64)
  • Driver: Mesa 26.2.5-cachyos1.1 (Vulkan 1.4.333)
  • Host: Linux CachyOS (x86_64, kernel 7.3.1-2-cachyos)
  • Compiler: GCC 15.2.1 / CMake 4.2.0-rc2
  • Tree: PR #597 (fix/vk-remove-resource-storage-pending)

Execution & Results:

  • Target: cmake --build runtime/build/linux --target vk_remove_resource_storage_pending_test
  • Binary output:
    device: AMD Radeon RX 6900 XT (RADV NAVI21)
    subgroup sizes: 32..64 (default 64), requirable in stages 0xf0, subgroupSizeControl on, computeFullSubgroups on
    vk_remove_resource_storage_pending_test: PASS (0 spurious clears, reused ResourceId untouched)
    
  • CTest: ctest --test-dir runtime/build/linux -R vk_remove_resource_storage_pending: 1/1 passed (0.02s).

Confirms zero spurious clears and reused ResourceId remains intact on physical AMD Radeon RX 6900 XT under Vulkan 1.4.

agent: yes
AI-assisted: yes

**@⁠trigger337** on GitHub · 2026-11-24 09:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/597#issuecomment-7127933907) Hardware verification report for `vk_remove_resource_storage_pending_test` on native AMD RDNA2 GPU: **Environment:** - GPU: AMD Radeon RX 6900 XT (RADV NAVI21, RDNA2, subgroup size 32..64) - Driver: Mesa 26.2.5-cachyos1.1 (Vulkan 1.4.333) - Host: Linux CachyOS (x86_64, kernel 7.3.1-2-cachyos) - Compiler: GCC 15.2.1 / CMake 4.2.0-rc2 - Tree: PR #597 (`fix/vk-remove-resource-storage-pending`) **Execution & Results:** - Target: `cmake --build runtime/build/linux --target vk_remove_resource_storage_pending_test` - Binary output: ``` device: AMD Radeon RX 6900 XT (RADV NAVI21) subgroup sizes: 32..64 (default 64), requirable in stages 0xf0, subgroupSizeControl on, computeFullSubgroups on vk_remove_resource_storage_pending_test: PASS (0 spurious clears, reused ResourceId untouched) ``` - CTest: `ctest --test-dir runtime/build/linux -R vk_remove_resource_storage_pending`: 1/1 passed (0.02s). Confirms zero spurious clears and reused `ResourceId` remains intact on physical AMD Radeon RX 6900 XT under Vulkan 1.4. agent: yes AI-assisted: yes
This pull request has changes conflicting with the target branch.
  • runtime/CMakeLists.txt
  • runtime/src/vulkan_backend.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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