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

Open
opened 2026-11-26 20:18:33 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/597
Author: @tahffo68hk-hash
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: fix/vk-remove-resource-storage-pending


📝 Commits (1)

  • 0b3a5c9 vulkan_backend: erase removed resource from storage_pending_ (VK-REMOVE-RESOURCE-STORAGE-PENDING)

📊 Changes

3 files changed (+179 additions, -1 deletions)

View changed files

📝 runtime/CMakeLists.txt (+15 -0)
📝 runtime/src/vulkan_backend.h (+2 -1)
➕ runtime/test/vk_remove_resource_storage_pending_test.cpp (+162 -0)

📄 Description

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 辅助:是。

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/597 **Author:** [@tahffo68hk-hash](https://github.com/tahffo68hk-hash) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/vk-remove-resource-storage-pending` --- ### 📝 Commits (1) - [`0b3a5c9`](https://github.com/ubiquitarianism/leonida/commit/0b3a5c9f31c7a60a5a6722b06f67fd205587222b) vulkan_backend: erase removed resource from storage_pending_ (VK-REMOVE-RESOURCE-STORAGE-PENDING) ### 📊 Changes **3 files changed** (+179 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+15 -0) 📝 `runtime/src/vulkan_backend.h` (+2 -1) ➕ `runtime/test/vk_remove_resource_storage_pending_test.cpp` (+162 -0) </details> ### 📄 Description ## 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> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#638
No description provided.