Skip to content
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

Backends: Vulkan: Create a custom pipeline for viewports #6325

Closed
wants to merge 3 commits into from

Conversation

skaman
Copy link

@skaman skaman commented Apr 12, 2023

The main render pass may have a different configuration than the viewport window render pass. Vulkan can reuse the pipeline between different render passes only if the render pass configuration is the same. To fix it, when it's time to RenderDrawData for the viewport window and a pipeline is not yet available, a new pipeline is created for that window to be sure that is compatible with the render pass.

This is related to the issue #6305

The main render pass may have a different configuration than the viewport window render pass.
Vulkan can reuse the pipeline between different render passes only if the render pass configuration is the same.
To fix it, when it's time to RenderDrawData for the viewport window and a pipeline is not yet available, a new pipeline is created for that window to be sure that is compatible with the render pass.
… destroyed

During window resize the pipeline is destroyed but non correctly reset to nullptr. So if reused it cause a memory error.
@ocornut
Copy link
Owner

ocornut commented Apr 13, 2023

Hello,
Thanks for your PR.

  • Could you post a patch (or separate commit which I won't merge) to repro the issue in one of the existing Vulkan example?
  • It seems like a shared pipeline could be used for all secondary viewports, in which case it may make more sense to create it during Init and reuse that?

@skaman
Copy link
Author

skaman commented Apr 13, 2023

Hello, Thanks for your PR.

  • Could you post a patch (or separate commit which I won't merge) to repro the issue in one of the existing Vulkan example?

I created a commit into a different branch: skaman@f974caa

Is it ok?

I created a custom render pass that i pass to the ImGui_ImplVulkan_Init function. To reproduce the issue the render pass should differ from the internal one, so i changed the color format from UNORM to SRGB (the colors are weird but it's just for test). In the real world probably the user have a depth buffer or multiple render passes.

  • It seems like a shared pipeline could be used for all secondary viewports, in which case it may make more sense to create it during Init and reuse that?

Yes you're right. I'll change the fix to have a shared pipeline for all secondary windows. I'll let you know when it's done.

Create a shared pipeline for all the secondary windows (they always have the same configuration for their render passes).
The pipeline is created only once when the first window is created.
@skaman
Copy link
Author

skaman commented Apr 13, 2023

Ok, committed the shared pipeline. Now there's only one pipeline for all the secondary windows.

@raaavioli
Copy link

Is this being merged? I'm experiencing an issue that would be solved with this. Particularly the RenderPass I'm using in ImGui_ImplVulkan_Init has a VK_FORMAT_B8G8R8A8_SRGB format, but the created window defaults to VK_FORMAT_B8G8R8A8_UNORM, or any of const VkFormat requestSurfaceImageFormat[] = { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM };.

I could see this as a solution, or some way to request a custom surface format when creating the multi-viewport renderpass.

@theunwisewolf
Copy link

Any update on this?

@ocornut
Copy link
Owner

ocornut commented Apr 30, 2024

I created a commit into a different branch: skaman@f974caa

Thank you @skaman and sorry for slow answer.

Why is the if (wd->Pipeline) { vkDestroyPipeline(device, wd->Pipeline, allocator); } code being removed?

@ocornut
Copy link
Owner

ocornut commented Apr 30, 2024

Why is the if (wd->Pipeline) { vkDestroyPipeline(device, wd->Pipeline, allocator); } code being removed?

I realized that:

  • wd->Pipeline is misleadingly essentially never used by the backend so far, and therefore always null.
  • If anything, it can then only be potentially fed by the application using the ImGui_ImplVulkanH_Window helper, but our own example apps don't use it, our backend doesn't use it anymore, and we discourage users from using those helpers anyway.

Therefore the whole thing is a fishy leftover that once worked. Initially was added in 41e2aa2 for #3459.
That initial version had the ImGui_ImplVulkan_CreatePipeline() call in ImGui_ImplVulkanH_CreateWindowSwapChain(), it made sense.

However it was immediately removed by e8447de also for #3459 and I believe I messed up there. @FunMiles themselves updated later and eventually reacted to it #3459 (comment) but I guess it went unnoticed then :(

While it seems to be my fault I believe it highlight that I need clear repros of the more intricate use cases of Vulkan. (I maintain them in private branches and could probably aim to publish them somewhere). Thanks for providing one here!

ocornut added a commit that referenced this pull request Apr 30, 2024
…indow::Pipeline (#6325, #6305, #7398, #3459, #3253, #3522)

As this is currently unused and misleading. Next commit will add a separate pipeline for secondary viewport.
ocornut pushed a commit that referenced this pull request Apr 30, 2024
…6325, #6305, #7398, #3459, #3253, #3522)

Edited from original commit: moved ImGui_ImplVulkan_CreatePipeline() call from ImGui_ImplVulkanH_CreateOrResizeWindow() to ImGui_ImplVulkan_CreateWindow().
@ocornut
Copy link
Owner

ocornut commented Apr 30, 2024

The PR is incorrect as is, as ImGui_ImplVulkanH_XXXX() functions aren't allowed to use backend data. In fact it makes the main.cpp examples asserts because they call ImGui_ImplVulkanH_CreateOrResizeWindow() first.

I have moved the creation in ImGui_ImplVulkan_CreateWindow() instead: ebb8d78 and it works.
I have also vaguely confirmed that it works with my setup for using dynamic rendering.

Apologies @FunMiles ! Thanks all!

@ocornut ocornut closed this Apr 30, 2024
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.

4 participants