# VK\_SUBOPTIMAL\_KHR is it safe to use it as window resize detection?

**URL:** <https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848>\
**Category:** Vulkan\
**Tags:** new-to-vulkan\
**Created:** [October 3, 2021, 9:12pm UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848 "2021-10-03T21:12:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danilw](https://community.khronos.org/user_avatar/community.khronos.org/danilw/32/5597_2.png) [@danilw](https://community.khronos.org/u/danilw)\
**Post date:** [October 3, 2021, 9:12pm UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848/1 "2021-10-03T21:12:35Z")

</div>

In latest (summer 2021+ idk version) Linux AMD Mesa drivers - **Vulkan window in X11 and Wayland on resize** (`vkAcquireNextImageKHR`) **in AMD GPU return** `VK_SUBOPTIMAL_KHR` or `VK_ERROR_OUT_OF_DATE_KHR` (randomly)

[https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkResult.html](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkResult.html)

> VK\_SUBOPTIMAL\_KHR A swapchain no longer matches the surface properties exactly, but can still be used to present to the surface successfully.

Is it safe/expected to use `VK_SUBOPTIMAL_KHR` to detect window resize? Or its unsafe?

---

<div class="post-metadata">

**Author:** ![Alfonse\_Reinheart](https://community.khronos.org/user_avatar/community.khronos.org/alfonse_reinheart/32/4357_2.png) [@Alfonse\_Reinheart](https://community.khronos.org/u/Alfonse_Reinheart)\
**Post date:** [October 3, 2021, 9:27pm UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848/2 "2021-10-03T21:27:30Z")

</div>

It could be returned for reasons other than size. Changes in format, for example.

The windowing system ought to inform you when the size changes, so trying to rely on indirect mechanisms is not helpful.

---

<div class="post-metadata">

**Author:** ![danilw](https://community.khronos.org/user_avatar/community.khronos.org/danilw/32/5597_2.png) [@danilw](https://community.khronos.org/u/danilw)\
**Post date:** [October 3, 2021, 9:43pm UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848/3 "2021-10-03T21:43:09Z")

</div>

> The windowing system ought to inform you when the size changes, so trying to rely on indirect mechanisms is not helpful.

ofc I do process Window system event before Vulkan render. And check memory and everything before Vulkan framebuffer resize.

my question is - **is it safe to recreate everything (framebuffers etc) on VK\_SUBOPTIMAL\_KHR**, because I want to have Vulkan framebuffers size equal to window size?

can VK\_SUBOPTIMAL\_KHR be looped forever? (for example on scaling part of screen, and some internal optimization of Wayland or X11 will allocate something with VK\_SUBOPTIMAL\_KHR and put there part of my window…)

or how to do it correctly?

---

<div class="post-metadata">

**Author:** ![Alfonse\_Reinheart](https://community.khronos.org/user_avatar/community.khronos.org/alfonse_reinheart/32/4357_2.png) [@Alfonse\_Reinheart](https://community.khronos.org/u/Alfonse_Reinheart)\
**Post date:** [October 3, 2021, 10:25pm UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848/4 "2021-10-03T22:25:49Z")

</div>

Technically, you don’t have to wait for a suboptimal swapchain. You can recreate the swapchain every frame if you like.

I mean, performance may suffer if you do, because you’re redoing work that you don’t need to do. But it is “safe”.

I’d say you should recreate the swapchain when the API tells you that you must, and maybe periodically if it is suboptimal. But I wouldn’t do it every frame, and definitely not in a loop.

---

<div class="post-metadata">

**Author:** ![buzmeg](https://community.khronos.org/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@buzmeg](https://community.khronos.org/u/buzmeg)\
**Post date:** [October 9, 2021, 3:49am UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848/5 "2021-10-09T03:49:53Z")

</div>

> [@danilw](#):
>
> Is it safe/expected to use `VK_SUBOPTIMAL_KHR` to detect window resize? Or its unsafe?

It is safe but not necessarily sufficient. In addition, you may or may not want to do it.

I believe that nVidia PC hardware on Windows doesn’t ever throw `VK_SUBOPTIMAL_KHR` so it wouldn’t be useful to detect resize there. MoltenVK throws `VK_SUBOPTIMAL_KHR` like crazy while the window is being resized–you could reallocate but you may want to wait until the resizing stops.

---

<div class="post-metadata">

**Author:** ![danilw](https://community.khronos.org/user_avatar/community.khronos.org/danilw/32/5597_2.png) [@danilw](https://community.khronos.org/u/danilw)\
**Post date:** [October 9, 2021, 4:35am UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848/6 "2021-10-09T04:35:05Z")

</div>

> [@buzmeg](#):
>
> MoltenVK throws VK\_SUBOPTIMAL\_KHR like crazy while the window is being resized–you could reallocate but you may want to wait until the resizing stops.

this was also reason for this question - too many hardware and OS and WM and every act differently

on AMD you can resize window and like you say “just wait” it never throw VK\_ERROR\_OUT\_OF\_DATE\_KHR, but on Nvidia its always VK\_ERROR\_OUT\_OF\_DATE\_KHR no matter what(and crash if you dont process this destroying/freeing/recreating your allocated data). (VK\_SUBOPTIMAL\_KHR happens only on X11/XCB or/and xWayland)  
In Wayland resize logic completely different from other WM, and way too many platforms to support if you rly care about crossplatform. (and Wayland has too many features that I left untested, I have no idea how application will react to those WM features(exampel I was talking about “zoom” part of screen, what application do in this case how it rendered… no idea))

And because its GUI app its multithreaded obviously (OS send resize event in random time to your app UI thread(only X11/XCB can collect events and drop them to your app to single thread)), you can not “expect resize event to be first before render frame”.  
So you have to “expect to stop your rendering at any time in your render loop when it resized”. (obviously I have framebuffers that have its own rendering thread)

For now im fine with only Nvidia and AMD support, everything else untested.  
If my “opensource tool” actually useful - someome will bugreport its problem if there will be any, testing “how it actually works” need too much time and hardware that I do not have.

---

<div class="post-metadata">

**Author:** ![system](https://community.khronos.org/uploads/default/original/2X/b/ba89cb49313a0e869198c8be354afde8bcae027e.png) [@system](https://community.khronos.org/u/system)\
**Post date:** [April 10, 2022, 4:36am UTC](https://community.khronos.org/t/vk-suboptimal-khr-is-it-safe-to-use-it-as-window-resize-detection/107848/7 "2022-04-10T04:36:00Z")

</div>

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.
