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