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

Closed
tahffo68hk-hash wants to merge 1 commit from upstream-pr/597 into main
tahffo68hk-hash commented 2026-11-24 09:35:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tahffo68hk-hash on 2026-11-24T09:35:48Z.


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 辅助:是。
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5469630440 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/597). Originally posted by [tahffo68hk-hash](https://github.com/tahffo68hk-hash) on 2026-11-24T09:35:48Z. --- ## 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>
trigger337 commented 2026-11-24 09:49:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by trigger337 on 2026-11-24T09:49:13Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7127933907 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/597#issuecomment-7127933907). Originally posted by [trigger337](https://github.com/trigger337) on 2026-11-24T09:49:13Z. --- 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
MoonEyes commented 2026-11-27 23:20:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-27T23:20:06Z.


Hi @tahffo68hk-hash, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/CMakeLists.txt, runtime/src/vulkan_backend.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7230059906 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/597#issuecomment-7230059906). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-27T23:20:06Z. --- Hi @tahffo68hk-hash, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/CMakeLists.txt`, `runtime/src/vulkan_backend.h`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
DadaCap commented 2026-11-28 10:34:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-28T10:34:29Z.


Thanks for the contribution! This PR was reviewed by the maintainers and is being closed as part of a large batch of closures.

Reason: superseded by main.

If this looks like a mistake, mention @DadaCap here at any time and it will be looked at again.

AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237984819 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/597#issuecomment-7237984819). Originally posted by [DadaCap](https://github.com/DadaCap) on 2026-11-28T10:34:29Z. --- Thanks for the contribution! This PR was reviewed by the maintainers and is being closed as part of a large batch of closures. Reason: superseded by main. If this looks like a mistake, mention @DadaCap here at any time and it will be looked at again. AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.

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!597
No description provided.