zink: add VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA for WSI allocations

Since Zink doesn't use swapchains to create presentable images, drivers
lose the capacity to identify memory allocations for them, which is a problem
when the underlying platform has special requirements for these, such as
needing to allocate them on a particular device. Including this struct in the
pNext chain, which is the same thing that the Mesa Vulkan WSI code does when
allocating memory for swapchain images, gives drivers a chance to identify
and handle these memory allocations properly.

v2: follow Zink's conventions for pNext chains (Mike)
v3: add scanout parameter for VkImage creation (Daniel)
v4: don't add a dependency on vulkan util (Erik)
v5: include vulkan directory for Zink builds

Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com> (v2)
Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7378>
diff --git a/src/gallium/drivers/zink/meson.build b/src/gallium/drivers/zink/meson.build
index 0790b48..4b90f22 100644
--- a/src/gallium/drivers/zink/meson.build
+++ b/src/gallium/drivers/zink/meson.build
@@ -64,7 +64,7 @@
   'zink',
   [files_libzink, zink_device_info, zink_nir_algebraic_c],
   gnu_symbol_visibility : 'hidden',
-  include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux],
+  include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux, inc_vulkan_wsi, inc_vulkan_util],
   dependencies: [dep_vulkan, idep_nir_headers],
 )
 
diff --git a/src/gallium/drivers/zink/zink_resource.c b/src/gallium/drivers/zink/zink_resource.c
index 88bc347..99bcd57 100644
--- a/src/gallium/drivers/zink/zink_resource.c
+++ b/src/gallium/drivers/zink/zink_resource.c
@@ -27,6 +27,8 @@
 #include "zink_context.h"
 #include "zink_screen.h"
 
+#include "vulkan/wsi/wsi_common.h"
+
 #include "util/slab.h"
 #include "util/u_debug.h"
 #include "util/format/u_format.h"
@@ -225,6 +227,15 @@
       ici.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
       res->layout = VK_IMAGE_LAYOUT_UNDEFINED;
 
+      struct wsi_image_create_info image_wsi_info = {
+         VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
+         NULL,
+         .scanout = true,
+      };
+
+      if (templ->bind & PIPE_BIND_SCANOUT)
+         ici.pNext = &image_wsi_info;
+
       VkResult result = vkCreateImage(screen->dev, &ici, NULL, &res->image);
       if (result != VK_SUCCESS) {
          FREE(res);
@@ -250,6 +261,8 @@
    if (templ->bind & PIPE_BIND_SHARED) {
       emai.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
       emai.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
+
+      emai.pNext = mai.pNext;
       mai.pNext = &emai;
    }
 
@@ -263,7 +276,20 @@
       imfi.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
       imfi.fd = whandle->handle;
 
-      emai.pNext = &imfi;
+      imfi.pNext = mai.pNext;
+      mai.pNext = &imfi;
+   }
+
+   struct wsi_memory_allocate_info memory_wsi_info = {
+      VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
+      NULL,
+   };
+
+   if (templ->bind & PIPE_BIND_SCANOUT) {
+      memory_wsi_info.implicit_sync = true;
+
+      memory_wsi_info.pNext = mai.pNext;
+      mai.pNext = &memory_wsi_info;
    }
 
    if (vkAllocateMemory(screen->dev, &mai, NULL, &res->mem) != VK_SUCCESS)
diff --git a/src/meson.build b/src/meson.build
index a175e33..895d627 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -67,7 +67,7 @@
 if with_platform_wayland
   subdir('egl/wayland/wayland-drm')
 endif
-if with_any_vk
+if with_any_vk or with_gallium_zink
   subdir('vulkan')
 endif
 if with_gallium_radeonsi or with_amd_vk
diff --git a/src/vulkan/meson.build b/src/vulkan/meson.build
index 7a06613..abb6617 100644
--- a/src/vulkan/meson.build
+++ b/src/vulkan/meson.build
@@ -22,6 +22,7 @@
 vulkan_icd_symbols = files('vulkan-icd-symbols.txt')
 
 inc_vulkan_wsi = include_directories('wsi')
+inc_vulkan_util = include_directories('util')
 
 vulkan_wsi_args = []
 vulkan_wsi_deps = []