util: Add a mapping from VkFormat to PIPE_FORMAT.
[mesa.git] / src / vulkan / wsi / wsi_common.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "wsi_common_private.h"
25 #include "drm-uapi/drm_fourcc.h"
26 #include "util/macros.h"
27 #include "util/xmlconfig.h"
28 #include "vk_util.h"
29
30 #include <time.h>
31 #include <unistd.h>
32 #include <xf86drm.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 VkResult
37 wsi_device_init(struct wsi_device *wsi,
38 VkPhysicalDevice pdevice,
39 WSI_FN_GetPhysicalDeviceProcAddr proc_addr,
40 const VkAllocationCallbacks *alloc,
41 int display_fd,
42 const struct driOptionCache *dri_options)
43 {
44 const char *present_mode;
45 VkResult result;
46
47 memset(wsi, 0, sizeof(*wsi));
48
49 wsi->instance_alloc = *alloc;
50 wsi->pdevice = pdevice;
51
52 #define WSI_GET_CB(func) \
53 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
54 WSI_GET_CB(GetPhysicalDeviceProperties2);
55 WSI_GET_CB(GetPhysicalDeviceMemoryProperties);
56 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties);
57 #undef WSI_GET_CB
58
59 wsi->pci_bus_info.sType =
60 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT;
61 VkPhysicalDeviceProperties2 pdp2 = {
62 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
63 .pNext = &wsi->pci_bus_info,
64 };
65 GetPhysicalDeviceProperties2(pdevice, &pdp2);
66
67 wsi->maxImageDimension2D = pdp2.properties.limits.maxImageDimension2D;
68 wsi->override_present_mode = VK_PRESENT_MODE_MAX_ENUM_KHR;
69
70 GetPhysicalDeviceMemoryProperties(pdevice, &wsi->memory_props);
71 GetPhysicalDeviceQueueFamilyProperties(pdevice, &wsi->queue_family_count, NULL);
72
73 #define WSI_GET_CB(func) \
74 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
75 WSI_GET_CB(AllocateMemory);
76 WSI_GET_CB(AllocateCommandBuffers);
77 WSI_GET_CB(BindBufferMemory);
78 WSI_GET_CB(BindImageMemory);
79 WSI_GET_CB(BeginCommandBuffer);
80 WSI_GET_CB(CmdCopyImageToBuffer);
81 WSI_GET_CB(CreateBuffer);
82 WSI_GET_CB(CreateCommandPool);
83 WSI_GET_CB(CreateFence);
84 WSI_GET_CB(CreateImage);
85 WSI_GET_CB(DestroyBuffer);
86 WSI_GET_CB(DestroyCommandPool);
87 WSI_GET_CB(DestroyFence);
88 WSI_GET_CB(DestroyImage);
89 WSI_GET_CB(EndCommandBuffer);
90 WSI_GET_CB(FreeMemory);
91 WSI_GET_CB(FreeCommandBuffers);
92 WSI_GET_CB(GetBufferMemoryRequirements);
93 WSI_GET_CB(GetImageMemoryRequirements);
94 WSI_GET_CB(GetImageSubresourceLayout);
95 WSI_GET_CB(GetMemoryFdKHR);
96 WSI_GET_CB(GetPhysicalDeviceFormatProperties);
97 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR);
98 WSI_GET_CB(ResetFences);
99 WSI_GET_CB(QueueSubmit);
100 WSI_GET_CB(WaitForFences);
101 #undef WSI_GET_CB
102
103 #ifdef VK_USE_PLATFORM_XCB_KHR
104 result = wsi_x11_init_wsi(wsi, alloc, dri_options);
105 if (result != VK_SUCCESS)
106 goto fail;
107 #endif
108
109 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
110 result = wsi_wl_init_wsi(wsi, alloc, pdevice);
111 if (result != VK_SUCCESS)
112 goto fail;
113 #endif
114
115 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
116 result = wsi_display_init_wsi(wsi, alloc, display_fd);
117 if (result != VK_SUCCESS)
118 goto fail;
119 #endif
120
121 present_mode = getenv("MESA_VK_WSI_PRESENT_MODE");
122 if (present_mode) {
123 if (!strcmp(present_mode, "fifo")) {
124 wsi->override_present_mode = VK_PRESENT_MODE_FIFO_KHR;
125 } else if (!strcmp(present_mode, "mailbox")) {
126 wsi->override_present_mode = VK_PRESENT_MODE_MAILBOX_KHR;
127 } else if (!strcmp(present_mode, "immediate")) {
128 wsi->override_present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR;
129 } else {
130 fprintf(stderr, "Invalid MESA_VK_WSI_PRESENT_MODE value!\n");
131 }
132 }
133
134 if (dri_options) {
135 if (driCheckOption(dri_options, "adaptive_sync", DRI_BOOL))
136 wsi->enable_adaptive_sync = driQueryOptionb(dri_options,
137 "adaptive_sync");
138 }
139
140 return VK_SUCCESS;
141
142 fail:
143 wsi_device_finish(wsi, alloc);
144 return result;
145 }
146
147 void
148 wsi_device_finish(struct wsi_device *wsi,
149 const VkAllocationCallbacks *alloc)
150 {
151 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
152 wsi_display_finish_wsi(wsi, alloc);
153 #endif
154 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
155 wsi_wl_finish_wsi(wsi, alloc);
156 #endif
157 #ifdef VK_USE_PLATFORM_XCB_KHR
158 wsi_x11_finish_wsi(wsi, alloc);
159 #endif
160 }
161
162 bool
163 wsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd)
164 {
165 drmDevicePtr fd_device;
166 int ret = drmGetDevice2(drm_fd, 0, &fd_device);
167 if (ret)
168 return false;
169
170 bool match = false;
171 switch (fd_device->bustype) {
172 case DRM_BUS_PCI:
173 match = wsi->pci_bus_info.pciDomain == fd_device->businfo.pci->domain &&
174 wsi->pci_bus_info.pciBus == fd_device->businfo.pci->bus &&
175 wsi->pci_bus_info.pciDevice == fd_device->businfo.pci->dev &&
176 wsi->pci_bus_info.pciFunction == fd_device->businfo.pci->func;
177 break;
178
179 default:
180 break;
181 }
182
183 drmFreeDevice(&fd_device);
184
185 return match;
186 }
187
188 VkResult
189 wsi_swapchain_init(const struct wsi_device *wsi,
190 struct wsi_swapchain *chain,
191 VkDevice device,
192 const VkSwapchainCreateInfoKHR *pCreateInfo,
193 const VkAllocationCallbacks *pAllocator)
194 {
195 VkResult result;
196
197 memset(chain, 0, sizeof(*chain));
198
199 chain->wsi = wsi;
200 chain->device = device;
201 chain->alloc = *pAllocator;
202 chain->use_prime_blit = false;
203
204 chain->cmd_pools =
205 vk_zalloc(pAllocator, sizeof(VkCommandPool) * wsi->queue_family_count, 8,
206 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
207 if (!chain->cmd_pools)
208 return VK_ERROR_OUT_OF_HOST_MEMORY;
209
210 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
211 const VkCommandPoolCreateInfo cmd_pool_info = {
212 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
213 .pNext = NULL,
214 .flags = 0,
215 .queueFamilyIndex = i,
216 };
217 result = wsi->CreateCommandPool(device, &cmd_pool_info, &chain->alloc,
218 &chain->cmd_pools[i]);
219 if (result != VK_SUCCESS)
220 goto fail;
221 }
222
223 return VK_SUCCESS;
224
225 fail:
226 wsi_swapchain_finish(chain);
227 return result;
228 }
229
230 static bool
231 wsi_swapchain_is_present_mode_supported(struct wsi_device *wsi,
232 const VkSwapchainCreateInfoKHR *pCreateInfo,
233 VkPresentModeKHR mode)
234 {
235 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
236 struct wsi_interface *iface = wsi->wsi[surface->platform];
237 VkPresentModeKHR *present_modes;
238 uint32_t present_mode_count;
239 bool supported = false;
240 VkResult result;
241
242 result = iface->get_present_modes(surface, &present_mode_count, NULL);
243 if (result != VK_SUCCESS)
244 return supported;
245
246 present_modes = malloc(present_mode_count * sizeof(*present_modes));
247 if (!present_modes)
248 return supported;
249
250 result = iface->get_present_modes(surface, &present_mode_count,
251 present_modes);
252 if (result != VK_SUCCESS)
253 goto fail;
254
255 for (uint32_t i = 0; i < present_mode_count; i++) {
256 if (present_modes[i] == mode) {
257 supported = true;
258 break;
259 }
260 }
261
262 fail:
263 free(present_modes);
264 return supported;
265 }
266
267 enum VkPresentModeKHR
268 wsi_swapchain_get_present_mode(struct wsi_device *wsi,
269 const VkSwapchainCreateInfoKHR *pCreateInfo)
270 {
271 if (wsi->override_present_mode == VK_PRESENT_MODE_MAX_ENUM_KHR)
272 return pCreateInfo->presentMode;
273
274 if (!wsi_swapchain_is_present_mode_supported(wsi, pCreateInfo,
275 wsi->override_present_mode)) {
276 fprintf(stderr, "Unsupported MESA_VK_WSI_PRESENT_MODE value!\n");
277 return pCreateInfo->presentMode;
278 }
279
280 return wsi->override_present_mode;
281 }
282
283 void
284 wsi_swapchain_finish(struct wsi_swapchain *chain)
285 {
286 if (chain->fences) {
287 for (unsigned i = 0; i < chain->image_count; i++)
288 chain->wsi->DestroyFence(chain->device, chain->fences[i], &chain->alloc);
289
290 vk_free(&chain->alloc, chain->fences);
291 }
292
293 for (uint32_t i = 0; i < chain->wsi->queue_family_count; i++) {
294 chain->wsi->DestroyCommandPool(chain->device, chain->cmd_pools[i],
295 &chain->alloc);
296 }
297 vk_free(&chain->alloc, chain->cmd_pools);
298 }
299
300 static uint32_t
301 select_memory_type(const struct wsi_device *wsi,
302 VkMemoryPropertyFlags props,
303 uint32_t type_bits)
304 {
305 for (uint32_t i = 0; i < wsi->memory_props.memoryTypeCount; i++) {
306 const VkMemoryType type = wsi->memory_props.memoryTypes[i];
307 if ((type_bits & (1 << i)) && (type.propertyFlags & props) == props)
308 return i;
309 }
310
311 unreachable("No memory type found");
312 }
313
314 static uint32_t
315 vk_format_size(VkFormat format)
316 {
317 switch (format) {
318 case VK_FORMAT_B8G8R8A8_UNORM:
319 case VK_FORMAT_B8G8R8A8_SRGB:
320 return 4;
321 default:
322 unreachable("Unknown WSI Format");
323 }
324 }
325
326 static inline uint32_t
327 align_u32(uint32_t v, uint32_t a)
328 {
329 assert(a != 0 && a == (a & -a));
330 return (v + a - 1) & ~(a - 1);
331 }
332
333 VkResult
334 wsi_create_native_image(const struct wsi_swapchain *chain,
335 const VkSwapchainCreateInfoKHR *pCreateInfo,
336 uint32_t num_modifier_lists,
337 const uint32_t *num_modifiers,
338 const uint64_t *const *modifiers,
339 struct wsi_image *image)
340 {
341 const struct wsi_device *wsi = chain->wsi;
342 VkResult result;
343
344 memset(image, 0, sizeof(*image));
345 for (int i = 0; i < ARRAY_SIZE(image->fds); i++)
346 image->fds[i] = -1;
347
348 struct wsi_image_create_info image_wsi_info = {
349 .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
350 .pNext = NULL,
351 };
352
353 uint32_t image_modifier_count = 0, modifier_prop_count = 0;
354 struct wsi_format_modifier_properties *modifier_props = NULL;
355 uint64_t *image_modifiers = NULL;
356 if (num_modifier_lists == 0) {
357 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
358 image_wsi_info.scanout = true;
359 } else {
360 /* The winsys can't request modifiers if we don't support them. */
361 assert(wsi->supports_modifiers);
362 struct wsi_format_modifier_properties_list modifier_props_list = {
363 .sType = VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA,
364 .pNext = NULL,
365 };
366 VkFormatProperties2 format_props = {
367 .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
368 .pNext = &modifier_props_list,
369 };
370 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
371 pCreateInfo->imageFormat,
372 &format_props);
373 assert(modifier_props_list.modifier_count > 0);
374 modifier_props = vk_alloc(&chain->alloc,
375 sizeof(*modifier_props) *
376 modifier_props_list.modifier_count,
377 8,
378 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
379 if (!modifier_props) {
380 result = VK_ERROR_OUT_OF_HOST_MEMORY;
381 goto fail;
382 }
383
384 modifier_props_list.modifier_properties = modifier_props;
385 wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
386 pCreateInfo->imageFormat,
387 &format_props);
388 modifier_prop_count = modifier_props_list.modifier_count;
389
390 uint32_t max_modifier_count = 0;
391 for (uint32_t l = 0; l < num_modifier_lists; l++)
392 max_modifier_count = MAX2(max_modifier_count, num_modifiers[l]);
393
394 image_modifiers = vk_alloc(&chain->alloc,
395 sizeof(*image_modifiers) *
396 max_modifier_count,
397 8,
398 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
399 if (!image_modifiers) {
400 result = VK_ERROR_OUT_OF_HOST_MEMORY;
401 goto fail;
402 }
403
404 image_modifier_count = 0;
405 for (uint32_t l = 0; l < num_modifier_lists; l++) {
406 /* Walk the modifier lists and construct a list of supported
407 * modifiers.
408 */
409 for (uint32_t i = 0; i < num_modifiers[l]; i++) {
410 for (uint32_t j = 0; j < modifier_prop_count; j++) {
411 if (modifier_props[j].modifier == modifiers[l][i])
412 image_modifiers[image_modifier_count++] = modifiers[l][i];
413 }
414 }
415
416 /* We only want to take the modifiers from the first list */
417 if (image_modifier_count > 0)
418 break;
419 }
420
421 if (image_modifier_count > 0) {
422 image_wsi_info.modifier_count = image_modifier_count;
423 image_wsi_info.modifiers = image_modifiers;
424 } else {
425 /* TODO: Add a proper error here */
426 assert(!"Failed to find a supported modifier! This should never "
427 "happen because LINEAR should always be available");
428 result = VK_ERROR_OUT_OF_HOST_MEMORY;
429 goto fail;
430 }
431 }
432
433 const VkImageCreateInfo image_info = {
434 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
435 .pNext = &image_wsi_info,
436 .flags = 0,
437 .imageType = VK_IMAGE_TYPE_2D,
438 .format = pCreateInfo->imageFormat,
439 .extent = {
440 .width = pCreateInfo->imageExtent.width,
441 .height = pCreateInfo->imageExtent.height,
442 .depth = 1,
443 },
444 .mipLevels = 1,
445 .arrayLayers = 1,
446 .samples = VK_SAMPLE_COUNT_1_BIT,
447 .tiling = VK_IMAGE_TILING_OPTIMAL,
448 .usage = pCreateInfo->imageUsage,
449 .sharingMode = pCreateInfo->imageSharingMode,
450 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
451 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
452 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
453 };
454 result = wsi->CreateImage(chain->device, &image_info,
455 &chain->alloc, &image->image);
456 if (result != VK_SUCCESS)
457 goto fail;
458
459 VkMemoryRequirements reqs;
460 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
461
462 const struct wsi_memory_allocate_info memory_wsi_info = {
463 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
464 .pNext = NULL,
465 .implicit_sync = true,
466 };
467 const VkExportMemoryAllocateInfo memory_export_info = {
468 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
469 .pNext = &memory_wsi_info,
470 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
471 };
472 const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
473 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
474 .pNext = &memory_export_info,
475 .image = image->image,
476 .buffer = VK_NULL_HANDLE,
477 };
478 const VkMemoryAllocateInfo memory_info = {
479 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
480 .pNext = &memory_dedicated_info,
481 .allocationSize = reqs.size,
482 .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
483 reqs.memoryTypeBits),
484 };
485 result = wsi->AllocateMemory(chain->device, &memory_info,
486 &chain->alloc, &image->memory);
487 if (result != VK_SUCCESS)
488 goto fail;
489
490 result = wsi->BindImageMemory(chain->device, image->image,
491 image->memory, 0);
492 if (result != VK_SUCCESS)
493 goto fail;
494
495 const VkMemoryGetFdInfoKHR memory_get_fd_info = {
496 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
497 .pNext = NULL,
498 .memory = image->memory,
499 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
500 };
501 int fd;
502 result = wsi->GetMemoryFdKHR(chain->device, &memory_get_fd_info, &fd);
503 if (result != VK_SUCCESS)
504 goto fail;
505
506 if (num_modifier_lists > 0) {
507 image->drm_modifier = wsi->image_get_modifier(image->image);
508 assert(image->drm_modifier != DRM_FORMAT_MOD_INVALID);
509
510 for (uint32_t j = 0; j < modifier_prop_count; j++) {
511 if (modifier_props[j].modifier == image->drm_modifier) {
512 image->num_planes = modifier_props[j].modifier_plane_count;
513 break;
514 }
515 }
516
517 for (uint32_t p = 0; p < image->num_planes; p++) {
518 const VkImageSubresource image_subresource = {
519 .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT << p,
520 .mipLevel = 0,
521 .arrayLayer = 0,
522 };
523 VkSubresourceLayout image_layout;
524 wsi->GetImageSubresourceLayout(chain->device, image->image,
525 &image_subresource, &image_layout);
526 image->sizes[p] = image_layout.size;
527 image->row_pitches[p] = image_layout.rowPitch;
528 image->offsets[p] = image_layout.offset;
529 if (p == 0) {
530 image->fds[p] = fd;
531 } else {
532 image->fds[p] = dup(fd);
533 if (image->fds[p] == -1) {
534 for (uint32_t i = 0; i < p; i++)
535 close(image->fds[p]);
536
537 goto fail;
538 }
539 }
540 }
541 } else {
542 const VkImageSubresource image_subresource = {
543 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
544 .mipLevel = 0,
545 .arrayLayer = 0,
546 };
547 VkSubresourceLayout image_layout;
548 wsi->GetImageSubresourceLayout(chain->device, image->image,
549 &image_subresource, &image_layout);
550
551 image->drm_modifier = DRM_FORMAT_MOD_INVALID;
552 image->num_planes = 1;
553 image->sizes[0] = reqs.size;
554 image->row_pitches[0] = image_layout.rowPitch;
555 image->offsets[0] = 0;
556 image->fds[0] = fd;
557 }
558
559 vk_free(&chain->alloc, modifier_props);
560 vk_free(&chain->alloc, image_modifiers);
561
562 return VK_SUCCESS;
563
564 fail:
565 vk_free(&chain->alloc, modifier_props);
566 vk_free(&chain->alloc, image_modifiers);
567 wsi_destroy_image(chain, image);
568
569 return result;
570 }
571
572 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
573
574 VkResult
575 wsi_create_prime_image(const struct wsi_swapchain *chain,
576 const VkSwapchainCreateInfoKHR *pCreateInfo,
577 bool use_modifier,
578 struct wsi_image *image)
579 {
580 const struct wsi_device *wsi = chain->wsi;
581 VkResult result;
582
583 memset(image, 0, sizeof(*image));
584
585 const uint32_t cpp = vk_format_size(pCreateInfo->imageFormat);
586 const uint32_t linear_stride = align_u32(pCreateInfo->imageExtent.width * cpp,
587 WSI_PRIME_LINEAR_STRIDE_ALIGN);
588
589 uint32_t linear_size = linear_stride * pCreateInfo->imageExtent.height;
590 linear_size = align_u32(linear_size, 4096);
591
592 const VkExternalMemoryBufferCreateInfo prime_buffer_external_info = {
593 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
594 .pNext = NULL,
595 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
596 };
597 const VkBufferCreateInfo prime_buffer_info = {
598 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
599 .pNext = &prime_buffer_external_info,
600 .size = linear_size,
601 .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
602 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
603 };
604 result = wsi->CreateBuffer(chain->device, &prime_buffer_info,
605 &chain->alloc, &image->prime.buffer);
606 if (result != VK_SUCCESS)
607 goto fail;
608
609 VkMemoryRequirements reqs;
610 wsi->GetBufferMemoryRequirements(chain->device, image->prime.buffer, &reqs);
611 assert(reqs.size <= linear_size);
612
613 const struct wsi_memory_allocate_info memory_wsi_info = {
614 .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
615 .pNext = NULL,
616 .implicit_sync = true,
617 };
618 const VkExportMemoryAllocateInfo prime_memory_export_info = {
619 .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
620 .pNext = &memory_wsi_info,
621 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
622 };
623 const VkMemoryDedicatedAllocateInfo prime_memory_dedicated_info = {
624 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
625 .pNext = &prime_memory_export_info,
626 .image = VK_NULL_HANDLE,
627 .buffer = image->prime.buffer,
628 };
629 const VkMemoryAllocateInfo prime_memory_info = {
630 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
631 .pNext = &prime_memory_dedicated_info,
632 .allocationSize = linear_size,
633 .memoryTypeIndex = select_memory_type(wsi, 0, reqs.memoryTypeBits),
634 };
635 result = wsi->AllocateMemory(chain->device, &prime_memory_info,
636 &chain->alloc, &image->prime.memory);
637 if (result != VK_SUCCESS)
638 goto fail;
639
640 result = wsi->BindBufferMemory(chain->device, image->prime.buffer,
641 image->prime.memory, 0);
642 if (result != VK_SUCCESS)
643 goto fail;
644
645 const VkImageCreateInfo image_info = {
646 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
647 .pNext = NULL,
648 .flags = 0,
649 .imageType = VK_IMAGE_TYPE_2D,
650 .format = pCreateInfo->imageFormat,
651 .extent = {
652 .width = pCreateInfo->imageExtent.width,
653 .height = pCreateInfo->imageExtent.height,
654 .depth = 1,
655 },
656 .mipLevels = 1,
657 .arrayLayers = 1,
658 .samples = VK_SAMPLE_COUNT_1_BIT,
659 .tiling = VK_IMAGE_TILING_OPTIMAL,
660 .usage = pCreateInfo->imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
661 .sharingMode = pCreateInfo->imageSharingMode,
662 .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
663 .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
664 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
665 };
666 result = wsi->CreateImage(chain->device, &image_info,
667 &chain->alloc, &image->image);
668 if (result != VK_SUCCESS)
669 goto fail;
670
671 wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
672
673 const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
674 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
675 .pNext = NULL,
676 .image = image->image,
677 .buffer = VK_NULL_HANDLE,
678 };
679 const VkMemoryAllocateInfo memory_info = {
680 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
681 .pNext = &memory_dedicated_info,
682 .allocationSize = reqs.size,
683 .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
684 reqs.memoryTypeBits),
685 };
686 result = wsi->AllocateMemory(chain->device, &memory_info,
687 &chain->alloc, &image->memory);
688 if (result != VK_SUCCESS)
689 goto fail;
690
691 result = wsi->BindImageMemory(chain->device, image->image,
692 image->memory, 0);
693 if (result != VK_SUCCESS)
694 goto fail;
695
696 image->prime.blit_cmd_buffers =
697 vk_zalloc(&chain->alloc,
698 sizeof(VkCommandBuffer) * wsi->queue_family_count, 8,
699 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
700 if (!image->prime.blit_cmd_buffers) {
701 result = VK_ERROR_OUT_OF_HOST_MEMORY;
702 goto fail;
703 }
704
705 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
706 const VkCommandBufferAllocateInfo cmd_buffer_info = {
707 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
708 .pNext = NULL,
709 .commandPool = chain->cmd_pools[i],
710 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
711 .commandBufferCount = 1,
712 };
713 result = wsi->AllocateCommandBuffers(chain->device, &cmd_buffer_info,
714 &image->prime.blit_cmd_buffers[i]);
715 if (result != VK_SUCCESS)
716 goto fail;
717
718 const VkCommandBufferBeginInfo begin_info = {
719 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
720 };
721 wsi->BeginCommandBuffer(image->prime.blit_cmd_buffers[i], &begin_info);
722
723 struct VkBufferImageCopy buffer_image_copy = {
724 .bufferOffset = 0,
725 .bufferRowLength = linear_stride / cpp,
726 .bufferImageHeight = 0,
727 .imageSubresource = {
728 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
729 .mipLevel = 0,
730 .baseArrayLayer = 0,
731 .layerCount = 1,
732 },
733 .imageOffset = { .x = 0, .y = 0, .z = 0 },
734 .imageExtent = {
735 .width = pCreateInfo->imageExtent.width,
736 .height = pCreateInfo->imageExtent.height,
737 .depth = 1,
738 },
739 };
740 wsi->CmdCopyImageToBuffer(image->prime.blit_cmd_buffers[i],
741 image->image,
742 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
743 image->prime.buffer,
744 1, &buffer_image_copy);
745
746 result = wsi->EndCommandBuffer(image->prime.blit_cmd_buffers[i]);
747 if (result != VK_SUCCESS)
748 goto fail;
749 }
750
751 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info = {
752 .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
753 .pNext = NULL,
754 .memory = image->prime.memory,
755 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
756 };
757 int fd;
758 result = wsi->GetMemoryFdKHR(chain->device, &linear_memory_get_fd_info, &fd);
759 if (result != VK_SUCCESS)
760 goto fail;
761
762 image->drm_modifier = use_modifier ? DRM_FORMAT_MOD_LINEAR : DRM_FORMAT_MOD_INVALID;
763 image->num_planes = 1;
764 image->sizes[0] = linear_size;
765 image->row_pitches[0] = linear_stride;
766 image->offsets[0] = 0;
767 image->fds[0] = fd;
768
769 return VK_SUCCESS;
770
771 fail:
772 wsi_destroy_image(chain, image);
773
774 return result;
775 }
776
777 void
778 wsi_destroy_image(const struct wsi_swapchain *chain,
779 struct wsi_image *image)
780 {
781 const struct wsi_device *wsi = chain->wsi;
782
783 if (image->prime.blit_cmd_buffers) {
784 for (uint32_t i = 0; i < wsi->queue_family_count; i++) {
785 wsi->FreeCommandBuffers(chain->device, chain->cmd_pools[i],
786 1, &image->prime.blit_cmd_buffers[i]);
787 }
788 vk_free(&chain->alloc, image->prime.blit_cmd_buffers);
789 }
790
791 wsi->FreeMemory(chain->device, image->memory, &chain->alloc);
792 wsi->DestroyImage(chain->device, image->image, &chain->alloc);
793 wsi->FreeMemory(chain->device, image->prime.memory, &chain->alloc);
794 wsi->DestroyBuffer(chain->device, image->prime.buffer, &chain->alloc);
795 }
796
797 VkResult
798 wsi_common_get_surface_support(struct wsi_device *wsi_device,
799 uint32_t queueFamilyIndex,
800 VkSurfaceKHR _surface,
801 VkBool32* pSupported)
802 {
803 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
804 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
805
806 return iface->get_support(surface, wsi_device,
807 queueFamilyIndex, pSupported);
808 }
809
810 VkResult
811 wsi_common_get_surface_capabilities(struct wsi_device *wsi_device,
812 VkSurfaceKHR _surface,
813 VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
814 {
815 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
816 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
817
818 VkSurfaceCapabilities2KHR caps2 = {
819 .sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
820 };
821
822 VkResult result = iface->get_capabilities2(surface, wsi_device, NULL, &caps2);
823
824 if (result == VK_SUCCESS)
825 *pSurfaceCapabilities = caps2.surfaceCapabilities;
826
827 return result;
828 }
829
830 VkResult
831 wsi_common_get_surface_capabilities2(struct wsi_device *wsi_device,
832 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
833 VkSurfaceCapabilities2KHR *pSurfaceCapabilities)
834 {
835 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pSurfaceInfo->surface);
836 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
837
838 return iface->get_capabilities2(surface, wsi_device, pSurfaceInfo->pNext,
839 pSurfaceCapabilities);
840 }
841
842 VkResult
843 wsi_common_get_surface_capabilities2ext(
844 struct wsi_device *wsi_device,
845 VkSurfaceKHR _surface,
846 VkSurfaceCapabilities2EXT *pSurfaceCapabilities)
847 {
848 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
849 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
850
851 assert(pSurfaceCapabilities->sType ==
852 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT);
853
854 struct wsi_surface_supported_counters counters = {
855 .sType = VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA,
856 .pNext = pSurfaceCapabilities->pNext,
857 .supported_surface_counters = 0,
858 };
859
860 VkSurfaceCapabilities2KHR caps2 = {
861 .sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
862 .pNext = &counters,
863 };
864
865 VkResult result = iface->get_capabilities2(surface, wsi_device, NULL, &caps2);
866
867 if (result == VK_SUCCESS) {
868 VkSurfaceCapabilities2EXT *ext_caps = pSurfaceCapabilities;
869 VkSurfaceCapabilitiesKHR khr_caps = caps2.surfaceCapabilities;
870
871 ext_caps->minImageCount = khr_caps.minImageCount;
872 ext_caps->maxImageCount = khr_caps.maxImageCount;
873 ext_caps->currentExtent = khr_caps.currentExtent;
874 ext_caps->minImageExtent = khr_caps.minImageExtent;
875 ext_caps->maxImageExtent = khr_caps.maxImageExtent;
876 ext_caps->maxImageArrayLayers = khr_caps.maxImageArrayLayers;
877 ext_caps->supportedTransforms = khr_caps.supportedTransforms;
878 ext_caps->currentTransform = khr_caps.currentTransform;
879 ext_caps->supportedCompositeAlpha = khr_caps.supportedCompositeAlpha;
880 ext_caps->supportedUsageFlags = khr_caps.supportedUsageFlags;
881 ext_caps->supportedSurfaceCounters = counters.supported_surface_counters;
882 }
883
884 return result;
885 }
886
887 VkResult
888 wsi_common_get_surface_formats(struct wsi_device *wsi_device,
889 VkSurfaceKHR _surface,
890 uint32_t *pSurfaceFormatCount,
891 VkSurfaceFormatKHR *pSurfaceFormats)
892 {
893 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
894 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
895
896 return iface->get_formats(surface, wsi_device,
897 pSurfaceFormatCount, pSurfaceFormats);
898 }
899
900 VkResult
901 wsi_common_get_surface_formats2(struct wsi_device *wsi_device,
902 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
903 uint32_t *pSurfaceFormatCount,
904 VkSurfaceFormat2KHR *pSurfaceFormats)
905 {
906 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pSurfaceInfo->surface);
907 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
908
909 return iface->get_formats2(surface, wsi_device, pSurfaceInfo->pNext,
910 pSurfaceFormatCount, pSurfaceFormats);
911 }
912
913 VkResult
914 wsi_common_get_surface_present_modes(struct wsi_device *wsi_device,
915 VkSurfaceKHR _surface,
916 uint32_t *pPresentModeCount,
917 VkPresentModeKHR *pPresentModes)
918 {
919 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
920 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
921
922 return iface->get_present_modes(surface, pPresentModeCount,
923 pPresentModes);
924 }
925
926 VkResult
927 wsi_common_get_present_rectangles(struct wsi_device *wsi_device,
928 VkSurfaceKHR _surface,
929 uint32_t* pRectCount,
930 VkRect2D* pRects)
931 {
932 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
933 struct wsi_interface *iface = wsi_device->wsi[surface->platform];
934
935 return iface->get_present_rectangles(surface, wsi_device,
936 pRectCount, pRects);
937 }
938
939 VkResult
940 wsi_common_create_swapchain(struct wsi_device *wsi,
941 VkDevice device,
942 const VkSwapchainCreateInfoKHR *pCreateInfo,
943 const VkAllocationCallbacks *pAllocator,
944 VkSwapchainKHR *pSwapchain)
945 {
946 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
947 struct wsi_interface *iface = wsi->wsi[surface->platform];
948 struct wsi_swapchain *swapchain;
949
950 VkResult result = iface->create_swapchain(surface, device, wsi,
951 pCreateInfo, pAllocator,
952 &swapchain);
953 if (result != VK_SUCCESS)
954 return result;
955
956 swapchain->fences = vk_zalloc(pAllocator,
957 sizeof (*swapchain->fences) * swapchain->image_count,
958 sizeof (*swapchain->fences),
959 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
960 if (!swapchain->fences) {
961 swapchain->destroy(swapchain, pAllocator);
962 return VK_ERROR_OUT_OF_HOST_MEMORY;
963 }
964
965 *pSwapchain = wsi_swapchain_to_handle(swapchain);
966
967 return VK_SUCCESS;
968 }
969
970 void
971 wsi_common_destroy_swapchain(VkDevice device,
972 VkSwapchainKHR _swapchain,
973 const VkAllocationCallbacks *pAllocator)
974 {
975 WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
976 if (!swapchain)
977 return;
978
979 swapchain->destroy(swapchain, pAllocator);
980 }
981
982 VkResult
983 wsi_common_get_images(VkSwapchainKHR _swapchain,
984 uint32_t *pSwapchainImageCount,
985 VkImage *pSwapchainImages)
986 {
987 WSI_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
988 VK_OUTARRAY_MAKE(images, pSwapchainImages, pSwapchainImageCount);
989
990 for (uint32_t i = 0; i < swapchain->image_count; i++) {
991 vk_outarray_append(&images, image) {
992 *image = swapchain->get_wsi_image(swapchain, i)->image;
993 }
994 }
995
996 return vk_outarray_status(&images);
997 }
998
999 VkResult
1000 wsi_common_acquire_next_image2(const struct wsi_device *wsi,
1001 VkDevice device,
1002 const VkAcquireNextImageInfoKHR *pAcquireInfo,
1003 uint32_t *pImageIndex)
1004 {
1005 WSI_FROM_HANDLE(wsi_swapchain, swapchain, pAcquireInfo->swapchain);
1006
1007 return swapchain->acquire_next_image(swapchain, pAcquireInfo, pImageIndex);
1008 }
1009
1010 VkResult
1011 wsi_common_queue_present(const struct wsi_device *wsi,
1012 VkDevice device,
1013 VkQueue queue,
1014 int queue_family_index,
1015 const VkPresentInfoKHR *pPresentInfo)
1016 {
1017 VkResult final_result = VK_SUCCESS;
1018
1019 const VkPresentRegionsKHR *regions =
1020 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
1021
1022 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
1023 WSI_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
1024 uint32_t image_index = pPresentInfo->pImageIndices[i];
1025 VkResult result;
1026
1027 if (swapchain->fences[image_index] == VK_NULL_HANDLE) {
1028 const VkFenceCreateInfo fence_info = {
1029 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1030 .pNext = NULL,
1031 .flags = 0,
1032 };
1033 result = wsi->CreateFence(device, &fence_info,
1034 &swapchain->alloc,
1035 &swapchain->fences[image_index]);
1036 if (result != VK_SUCCESS)
1037 goto fail_present;
1038 } else {
1039 wsi->WaitForFences(device, 1, &swapchain->fences[image_index],
1040 true, 1);
1041
1042 wsi->ResetFences(device, 1, &swapchain->fences[image_index]);
1043 }
1044
1045 VkSubmitInfo submit_info = {
1046 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
1047 .pNext = NULL,
1048 };
1049
1050 VkPipelineStageFlags *stage_flags = NULL;
1051 if (i == 0) {
1052 /* We only need/want to wait on semaphores once. After that, we're
1053 * guaranteed ordering since it all happens on the same queue.
1054 */
1055 submit_info.waitSemaphoreCount = pPresentInfo->waitSemaphoreCount;
1056 submit_info.pWaitSemaphores = pPresentInfo->pWaitSemaphores;
1057
1058 /* Set up the pWaitDstStageMasks */
1059 stage_flags = vk_alloc(&swapchain->alloc,
1060 sizeof(VkPipelineStageFlags) *
1061 pPresentInfo->waitSemaphoreCount,
1062 8,
1063 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1064 if (!stage_flags) {
1065 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1066 goto fail_present;
1067 }
1068 for (uint32_t s = 0; s < pPresentInfo->waitSemaphoreCount; s++)
1069 stage_flags[s] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
1070
1071 submit_info.pWaitDstStageMask = stage_flags;
1072 }
1073
1074 if (swapchain->use_prime_blit) {
1075 /* If we are using prime blits, we need to perform the blit now. The
1076 * command buffer is attached to the image.
1077 */
1078 struct wsi_image *image =
1079 swapchain->get_wsi_image(swapchain, image_index);
1080 submit_info.commandBufferCount = 1;
1081 submit_info.pCommandBuffers =
1082 &image->prime.blit_cmd_buffers[queue_family_index];
1083 }
1084
1085 result = wsi->QueueSubmit(queue, 1, &submit_info, swapchain->fences[image_index]);
1086 vk_free(&swapchain->alloc, stage_flags);
1087 if (result != VK_SUCCESS)
1088 goto fail_present;
1089
1090 const VkPresentRegionKHR *region = NULL;
1091 if (regions && regions->pRegions)
1092 region = &regions->pRegions[i];
1093
1094 result = swapchain->queue_present(swapchain, image_index, region);
1095 if (result != VK_SUCCESS)
1096 goto fail_present;
1097
1098 fail_present:
1099 if (pPresentInfo->pResults != NULL)
1100 pPresentInfo->pResults[i] = result;
1101
1102 /* Let the final result be our first unsuccessful result */
1103 if (final_result == VK_SUCCESS)
1104 final_result = result;
1105 }
1106
1107 return final_result;
1108 }
1109
1110 uint64_t
1111 wsi_common_get_current_time(void)
1112 {
1113 struct timespec current;
1114 clock_gettime(CLOCK_MONOTONIC, &current);
1115 return current.tv_nsec + current.tv_sec * 1000000000ull;
1116 }