Skip to content

Fix Vulkan validation error during window detach in multi-viewport mode (#8176) #8600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ChrisTom-94
Copy link
Contributor

Summary

This PR fixes the Vulkan validation error reported in issue #8176, which occurs when detaching a window from the main window in multi-viewport mode.


Changes Included

  • Explicitly transition swapchain images to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR immediately after creation.
  • During dynamic rendering, correctly transition swapchain images from VK_IMAGE_LAYOUT_PRESENT_SRC_KHR to VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, using appropriate pipeline stages and access masks.

These changes make the swapchain image layout transitions explicit and properly synchronized, resolving the Vulkan validation warnings/errors during window detach and resize events.


Reference

A minimal reproducible demo can be found here:
👉 ChrisTom-94/imgui_fix_8176


Notes

  • The initial transition to PRESENT_SRC_KHR could also be applied even when not using dynamic rendering.
  • However, for minimal impact, this PR applies the change only when dynamic rendering is enabled.

…r stages while transitioning for dynamic rendering
@ChrisTom-94 ChrisTom-94 changed the title add transition layout on swapchain images creation and fix the barrie… Fix Vulkan Validation Error During Window Detach in Multi-Viewport Mode (#8176) Apr 29, 2025
@ChrisTom-94 ChrisTom-94 changed the title Fix Vulkan Validation Error During Window Detach in Multi-Viewport Mode (#8176) Fix Vulkan validation error during window detach in multi-viewport mode (#8176) Apr 29, 2025
@ocornut
Copy link
Owner

ocornut commented Apr 30, 2025

Hello Chris,

Thanks for the careful and detailed PR.
It's going to be a little hard for me to parse that whole repro, and I don't understand much of Vulkan. My observation is that I don't get the issue on stock examples. I would appreciate a repro based on stock examples, if that's feasible, because this is going to be more self-explanatory in term of narrowing the cause, and would be information we can rely on for years as if we frequently come back on those issues.

Right now if I try to enable dynamic rendering using this patch + grab the whole VulkanValidationSettings block from your code, I do get a bunch of
SYNC-HAZARD-WRITE-AFTER-READ errors regardless of using multi-viewports.

[vulkan] Debug report from ObjectType: 4
Message: Validation Error: [ SYNC-HAZARD-WRITE-AFTER-READ ] Object 0: handle = 0x1a86a8cdc20, type = VK_OBJECT_TYPE_QUEUE; | MessageID = 0x376bc9df | vkQueueSubmit():  Hazard WRITE_AFTER_READ for entry 0, VkCommandBuffer 0x1a86d1d80b0[], Submitted access info (submitted_usage: SYNC_IMAGE_LAYOUT_TRANSITION, command: vkCmdPipelineBarrier, seq_no: 1, reset_no: 308). Access info (prior_usage: SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_ACQUIRE_READ_SYNCVAL, read_barriers: VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT|VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT, , batch_tag: 3672, vkAcquireNextImageKHR aquire_tag:3672: VkSwapchainKHR 0xf56c9b0000000004[], image_index: 0image: VkImage 0xe7f79a0000000005[]).

SYNC-HAZARD-WRITE-AFTER-READ(ERROR / SPEC): msgNum: 929810911 - Validation Error: [ SYNC-HAZARD-WRITE-AFTER-READ ] Object 0: handle = 0x1a86a8cdc20, type = VK_OBJECT_TYPE_QUEUE; | MessageID = 0x376bc9df | vkQueueSubmit():  Hazard WRITE_AFTER_READ for entry 0, VkCommandBuffer 0x1a86d1deb80[], Submitted access info (submitted_usage: SYNC_IMAGE_LAYOUT_TRANSITION, command: vkCmdPipelineBarrier, seq_no: 1, reset_no: 308). Access info (prior_usage: SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_ACQUIRE_READ_SYNCVAL, read_barriers: VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT|VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT, , batch_tag: 3684, vkAcquireNextImageKHR aquire_tag:3684: VkSwapchainKHR 0xf56c9b0000000004[], image_index: 1image: VkImage 0xf443490000000006[]).
    Objects: 1
        [0] 0x1a86a8cdc20, type: 4, name: NULL

And they don't go away with your patch. So I am hardly even able to look at the primary issue #8176.

This is generally so utterly confusing to me that, given the much appreciated meticulousness of your PR, I'm tempted to trust you and merge and see what other reports come next from users, but it would be great if I could repro something locally and on a known test bed.

@ChrisTom-94
Copy link
Contributor Author

Hello Omar,
Thanks for your reply.

I’ve created a patch (from my fork, branch fix/vulkan-validation-error-pipeline-barrier) where I updated both the glfw_vulkan and sdl3_vulkan examples to test the fix.
PR-8600_imgui_examples_and_fix_impl_vulkan.patch

While working on it, I encountered issues with dynamic rendering function loading. Specifically, dynamic rendering functions are currently loaded using vkGetInstanceProcAddr, but since functions like vkCmdXXXX are device-level commands, they must be loaded using vkGetDeviceProcAddr.

Current (incorrect) line:
ImGui_ImplVulkan_LoadDynamicRenderingFunctions(info->ApiVersion, [](const char* function_name, void* user_data) { return vkGetInstanceProcAddr((VkInstance)user_data, function_name); }, (void*)info->Instance);

Correct version:
ImGui_ImplVulkan_LoadDynamicRenderingFunctions(info->ApiVersion, [](const char* function_name, void* user_data) { return vkGetDeviceProcAddr((VkDevice)user_data, function_name); }, (void*)info->Device);

I also encountered a problem related to layout transitions i added. Since the examples use ImGui_ImplVulkanH_* methods (which aren’t typically used in end-users code), the ImGui_ImplVulkan_Data and InitInfo I relied on weren’t always available (this should never happen if end-users don't use these helpers' functions). So, to avoid any possible issue, I moved the transition logic into ImGui_ImplVulkanH_CreateOrResizeWindow, where we have access to a queue family and can get a queue for command submission.

All these changes are included in the patch to test in the examples.

ocornut pushed a commit that referenced this pull request May 7, 2025
…rocAddr() + try both non-KHR and KHR versions. (#8600, #8326, #8365)

# Conflicts:
#	backends/imgui_impl_vulkan.cpp
#	docs/CHANGELOG.txt
@ocornut
Copy link
Owner

ocornut commented May 7, 2025

Thank you Christian for the thoughful and useful amount of details!
I've merged the loading parts now : d1dc2a3 and will look at the rest later.

ocornut pushed a commit that referenced this pull request May 7, 2025
ocornut added a commit that referenced this pull request May 7, 2025
@ocornut
Copy link
Owner

ocornut commented May 7, 2025

Thank you Christian. I have merged this as bbc89b6 (part thats in master) + 37fba4b.

Thanks a lot for the careful amount of details and repro, this is very much appreciated and useful.

@ChrisTom-94
Copy link
Contributor Author

Happy to have contributed!
Dear ImGui it's really a great project. keep up the good work!

elbadcode added a commit to elbadcode/imgui that referenced this pull request Jul 3, 2025
* origin/docking-dev: (1059 commits)
  Backends: Vulkan: Fix failing assertion for platforms where viewports are not supported (ocornut#8734)
  Backends: GLFW: Fixed not installing WndProc hook in all GLFW version, so AddMouseSourceEvent() logic was missing for some viewports.
  Backends: GLFW: Fixed crash when using GLFW 3.3 (ocornut#8713, ocornut#8676, ocornut#8239, ocornut#8069)
  Backends: warning fixes (for docking branch).
  Backends: GLFW: amend for multi-context support with multi-viewport. (ocornut#8676, ocornut#8239, ocornut#8069)
  Backends: OSX: ImGui_ImplOSX_HandleEvent() only process event for window containing our viewports. Amend 7ac99a4 for docking. (ocornut#8644)
  Fixed duplicate symbols in some compile-time configurations.
  Fonts: Misc merge fixes.
  Examples: set ConfigDpiScaleFonts / ConfigDpiScaleViewports in all examples already setup for scaling.
  Backends: GLFW, SDL2, SDL3, update for docking to use helpers.
  (Breaking) renamed/moved ImGuiConfigFlags_DpiEnableScaleFonts -> ioConfigDpiScaleFonts, ImGuiConfigFlags_DpiEnableScaleViewports -> io.ConfigDpiScaleViewports
  Backends: Win32: Viewports: handle WM_DPICHANGED in backend when ImGuiConfigFlags_DpiEnableScaleViewports flag is enabled.
  Viewports: fixed handling of simultaneous move + resize (e.g. toggling maximized) when ImGuiConfigFlags_DpiEnableScaleViewports is enabled.
  Refactor: move SetCurrentFont(), PushFont(), PopFont() to a section.
  Platform IME: Fixed multi-viewports IME support, affecting SDL backends. (ocornut#8648, ocornut#8584, ocornut#7492, ocornut#6341)
  Viewports: added per-viewport FramebufferScale, Platform_GetWindowFramebufferScale() + Backends: GLFW, SDL2, SDL3, Apple: added support. (ocornut#1065, ocornut#1542, ocornut#1676, ocornut#1786, ocornut#2826, ocornut#3757, ocornut#5081, ocornut#5580, ocornut#5592, ocornut#6465, ocornut#7273, ocornut#7779 etc.) )
  Backends: OSX: rename internal struct for consistency with other backends.
  Viewports: fallback DpiScale pulled from fallback Monitor for consistency.
  Backends: Vulkan: fixed build with VK_NO_PROTOTYPES.
  Backends: Vulkan: fixed validation errors during window detach in multi-viewport mode. [docking branch amend] (ocornut#8600, ocornut#8176)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants