CreateSwapchain asks the driver for the present modes twice at every rebuild: on Mesa's Wayland that is two sets of round trips, 24 ms of every resize on the phone #202

Open
opened 2026-11-20 06:59:06 +00:00 by jorijnvdgraaf · 1 comment

Summary

Window::CreateSwapchain calls vkGetPhysicalDeviceSurfacePresentModesKHR twice, for the count and for the list, every time it makes a swapchain, so at every resize. On Mesa's Wayland WSI each of the two calls sets up connection state of its own: a registry, seven globals bound, the dmabuf feedback fetched, three round trips with the compositor, and all of it destroyed again. On a Fairphone 6 the two together take a median 24 ms of the 87 ms between the configure of a resize and the first frame at the new size. The list they return is the same every time. Line numbers refer to 89111e0.

Details

  • implementations/Crafter.Graphics-Window.cpp:1749-1751: the two calls. :1752-1761 use the list for one thing, to pick swapchainPresentMode from presentMode and CRAFTER_PRESENT_IMMEDIATE.
  • Nothing else in the library asks for the present modes: those are the only two lines that name the function.
  • Every rebuild comes through there (RebuildSwapchain, :794): a new size, from Resize or from a configure by way of RebuildIfDeferred; a swapchain out of date; SetPresentMode (:903). Only the last changes what the list is asked for.
  • SwapchainCapabilities, called right after, shows no round trip of its own in a resize. At a window's start the trace also has queries on queues the driver names mesa formats query and mesa image count query; in a resize it has the two mesa present modes query ones and nothing else before vkCreateSwapchainKHR.

Concrete case

A Fairphone 6 (KWin 6.8.2, Mesa 26.2.6 Turnip, scale 2.85) running a scratch program with one text field against the library at 89111e0, under WAYLAND_DEBUG=1. The on-screen keyboard was brought up and dismissed by hand 22 times; each time KWin resizes the maximized window between 392 x 796 and 392 x 534, and the library makes a swapchain. Over the 23 resizes to 392 x 796, in ms from the configure:

median least most
the first request of the present-mode queries 1.6 0.6 54.1
the two queries, first message to last 24.0 8.8 31.9
the first buffer of the new swapchain created 55.7 40.8 102.5
the frame's commit, with that buffer attached 86.7 50.2 135.3

Over the 22 resizes to 392 x 534 the queries take a median 26.2 ms of 70.2.

One of them, with the time since its configure. libwayland prints the name of each message's queue, and the driver names this one. Only the requests are listed; the registry's globals and the other events come in between.

  0.0 ms  xdg_toplevel#16.configure(392, 796, array[8])
  1.5 ms  -> wl_display#1.get_registry(new id wl_registry#23)                      {mesa present modes query}, the first call
  1.5 ms  -> wl_display#1.sync(new id wl_callback#52)
  4.7 ms  -> wl_registry#23.bind(42, "wp_tearing_control_manager_v1", 1, ...)      and wp_presentation, wp_color_manager_v1, wl_fixes,
                                                                                   wp_fifo_manager_v1, zwp_linux_dmabuf_v1, wp_linux_drm_syncobj_manager_v1
  5.9 ms  -> zwp_linux_dmabuf_v1#48.get_default_feedback(new id ...#52)
  5.9 ms  -> wl_display#1.sync(new id wl_callback#40)
 13.9 ms  -> wl_display#1.sync(new id wl_callback#40)
 14.8 ms  -> wl_fixes#46.destroy_registry(wl_registry#23)                           and the feedback and the seven globals destroyed
 15.2 ms  -> wl_display#1.get_registry(new id wl_registry#40)                      the second call: the same again
 24.4 ms  -> wp_color_manager_v1#47.destroy()                                       its last request
 24.5 ms  -> wp_fifo_v1#43.destroy()                                                {mesa vk display queue}: vkCreateSwapchainKHR

Not measured: what the two calls cost with NVIDIA's driver, or on Win32.

Suggested fix

Ask once and keep the list: at the window's first swapchain, and again in SetPresentMode, the one moment the list decides something new and not one where 24 ms matter. A rebuild for a new size or for a swapchain out of date uses the list the window has.

What that gives up: a mode the surface gains or loses under a running window is not seen until the next SetPresentMode. In the trace the query binds wp_tearing_control_manager_v1 and wp_fifo_manager_v1 among the compositor's globals, so on Mesa's Wayland the list would follow those; what it depends on in each driver was not read.

A smaller step that gives up nothing: one call instead of two, into an array large enough for every VkPresentModeKHR there is. Half the time.

Either way the query has still been made for the surface before its first swapchain. SwapchainImageCount makes swapchains in every present mode under CRAFTER_GRAPHICS_VALIDATION and would show the validation layer objecting to a mode it has not seen listed.

A test for it could come from ResizeViewport, which already replays its WAYLAND_DEBUG log: on a Mesa driver the log names the queue, so the replay can count the registries a rebuild binds on mesa present modes query.

Found measuring where the time of a keyboard resize goes on the phone, with the fix for #199 in place, 2026-11-20.

## Summary `Window::CreateSwapchain` calls `vkGetPhysicalDeviceSurfacePresentModesKHR` twice, for the count and for the list, every time it makes a swapchain, so at every resize. On Mesa's Wayland WSI each of the two calls sets up connection state of its own: a registry, seven globals bound, the dmabuf feedback fetched, three round trips with the compositor, and all of it destroyed again. On a Fairphone 6 the two together take a median 24 ms of the 87 ms between the configure of a resize and the first frame at the new size. The list they return is the same every time. Line numbers refer to `89111e0`. ## Details - `implementations/Crafter.Graphics-Window.cpp:1749-1751`: the two calls. `:1752-1761` use the list for one thing, to pick `swapchainPresentMode` from `presentMode` and `CRAFTER_PRESENT_IMMEDIATE`. - Nothing else in the library asks for the present modes: those are the only two lines that name the function. - Every rebuild comes through there (`RebuildSwapchain`, `:794`): a new size, from `Resize` or from a configure by way of `RebuildIfDeferred`; a swapchain out of date; `SetPresentMode` (`:903`). Only the last changes what the list is asked for. - `SwapchainCapabilities`, called right after, shows no round trip of its own in a resize. At a window's start the trace also has queries on queues the driver names `mesa formats query` and `mesa image count query`; in a resize it has the two `mesa present modes query` ones and nothing else before `vkCreateSwapchainKHR`. ## Concrete case A Fairphone 6 (KWin 6.8.2, Mesa 26.2.6 Turnip, scale 2.85) running a scratch program with one text field against the library at `89111e0`, under `WAYLAND_DEBUG=1`. The on-screen keyboard was brought up and dismissed by hand 22 times; each time KWin resizes the maximized window between 392 x 796 and 392 x 534, and the library makes a swapchain. Over the 23 resizes to 392 x 796, in ms from the configure: | | median | least | most | |---|---|---|---| | the first request of the present-mode queries | 1.6 | 0.6 | 54.1 | | the two queries, first message to last | 24.0 | 8.8 | 31.9 | | the first buffer of the new swapchain created | 55.7 | 40.8 | 102.5 | | the frame's commit, with that buffer attached | 86.7 | 50.2 | 135.3 | Over the 22 resizes to 392 x 534 the queries take a median 26.2 ms of 70.2. One of them, with the time since its configure. libwayland prints the name of each message's queue, and the driver names this one. Only the requests are listed; the registry's globals and the other events come in between. ``` 0.0 ms xdg_toplevel#16.configure(392, 796, array[8]) 1.5 ms -> wl_display#1.get_registry(new id wl_registry#23) {mesa present modes query}, the first call 1.5 ms -> wl_display#1.sync(new id wl_callback#52) 4.7 ms -> wl_registry#23.bind(42, "wp_tearing_control_manager_v1", 1, ...) and wp_presentation, wp_color_manager_v1, wl_fixes, wp_fifo_manager_v1, zwp_linux_dmabuf_v1, wp_linux_drm_syncobj_manager_v1 5.9 ms -> zwp_linux_dmabuf_v1#48.get_default_feedback(new id ...#52) 5.9 ms -> wl_display#1.sync(new id wl_callback#40) 13.9 ms -> wl_display#1.sync(new id wl_callback#40) 14.8 ms -> wl_fixes#46.destroy_registry(wl_registry#23) and the feedback and the seven globals destroyed 15.2 ms -> wl_display#1.get_registry(new id wl_registry#40) the second call: the same again 24.4 ms -> wp_color_manager_v1#47.destroy() its last request 24.5 ms -> wp_fifo_v1#43.destroy() {mesa vk display queue}: vkCreateSwapchainKHR ``` Not measured: what the two calls cost with NVIDIA's driver, or on Win32. ## Suggested fix Ask once and keep the list: at the window's first swapchain, and again in `SetPresentMode`, the one moment the list decides something new and not one where 24 ms matter. A rebuild for a new size or for a swapchain out of date uses the list the window has. What that gives up: a mode the surface gains or loses under a running window is not seen until the next `SetPresentMode`. In the trace the query binds `wp_tearing_control_manager_v1` and `wp_fifo_manager_v1` among the compositor's globals, so on Mesa's Wayland the list would follow those; what it depends on in each driver was not read. A smaller step that gives up nothing: one call instead of two, into an array large enough for every `VkPresentModeKHR` there is. Half the time. Either way the query has still been made for the surface before its first swapchain. `SwapchainImageCount` makes swapchains in every present mode under `CRAFTER_GRAPHICS_VALIDATION` and would show the validation layer objecting to a mode it has not seen listed. A test for it could come from `ResizeViewport`, which already replays its `WAYLAND_DEBUG` log: on a Mesa driver the log names the queue, so the replay can count the registries a rebuild binds on `mesa present modes query`. Found measuring where the time of a keyboard resize goes on the phone, with the fix for #199 in place, 2026-11-20.
Author
Owner

The rest of that time, the driver allocating the buffers of the new swapchain, is #203: a resize that needs no new swapchain needs neither.

The rest of that time, the driver allocating the buffers of the new swapchain, is #203: a resize that needs no new swapchain needs neither.
Sign in to join this conversation.
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
Catcrafts/Crafter.Graphics#202
No description provided.