2 * Copyright © 2017 Intel Corporation
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:
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
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
24 #include "wsi_common_private.h"
25 #include "drm-uapi/drm_fourcc.h"
26 #include "util/macros.h"
27 #include "util/xmlconfig.h"
37 wsi_device_init(struct wsi_device
*wsi
,
38 VkPhysicalDevice pdevice
,
39 WSI_FN_GetPhysicalDeviceProcAddr proc_addr
,
40 const VkAllocationCallbacks
*alloc
,
42 const struct driOptionCache
*dri_options
)
44 const char *present_mode
;
47 memset(wsi
, 0, sizeof(*wsi
));
49 wsi
->instance_alloc
= *alloc
;
50 wsi
->pdevice
= pdevice
;
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
);
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
,
65 GetPhysicalDeviceProperties2(pdevice
, &pdp2
);
67 wsi
->maxImageDimension2D
= pdp2
.properties
.limits
.maxImageDimension2D
;
68 wsi
->override_present_mode
= VK_PRESENT_MODE_MAX_ENUM_KHR
;
70 GetPhysicalDeviceMemoryProperties(pdevice
, &wsi
->memory_props
);
71 GetPhysicalDeviceQueueFamilyProperties(pdevice
, &wsi
->queue_family_count
, NULL
);
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
);
103 #ifdef VK_USE_PLATFORM_XCB_KHR
104 result
= wsi_x11_init_wsi(wsi
, alloc
, dri_options
);
105 if (result
!= VK_SUCCESS
)
109 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
110 result
= wsi_wl_init_wsi(wsi
, alloc
, pdevice
);
111 if (result
!= VK_SUCCESS
)
115 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
116 result
= wsi_display_init_wsi(wsi
, alloc
, display_fd
);
117 if (result
!= VK_SUCCESS
)
121 present_mode
= getenv("MESA_VK_WSI_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
;
130 fprintf(stderr
, "Invalid MESA_VK_WSI_PRESENT_MODE value!\n");
135 if (driCheckOption(dri_options
, "adaptive_sync", DRI_BOOL
))
136 wsi
->enable_adaptive_sync
= driQueryOptionb(dri_options
,
143 wsi_device_finish(wsi
, alloc
);
148 wsi_device_finish(struct wsi_device
*wsi
,
149 const VkAllocationCallbacks
*alloc
)
151 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
152 wsi_display_finish_wsi(wsi
, alloc
);
154 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
155 wsi_wl_finish_wsi(wsi
, alloc
);
157 #ifdef VK_USE_PLATFORM_XCB_KHR
158 wsi_x11_finish_wsi(wsi
, alloc
);
163 wsi_device_matches_drm_fd(const struct wsi_device
*wsi
, int drm_fd
)
165 drmDevicePtr fd_device
;
166 int ret
= drmGetDevice2(drm_fd
, 0, &fd_device
);
171 switch (fd_device
->bustype
) {
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
;
183 drmFreeDevice(&fd_device
);
189 wsi_swapchain_init(const struct wsi_device
*wsi
,
190 struct wsi_swapchain
*chain
,
192 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
193 const VkAllocationCallbacks
*pAllocator
)
197 memset(chain
, 0, sizeof(*chain
));
200 chain
->device
= device
;
201 chain
->alloc
= *pAllocator
;
202 chain
->use_prime_blit
= false;
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
;
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
,
215 .queueFamilyIndex
= i
,
217 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
218 &chain
->cmd_pools
[i
]);
219 if (result
!= VK_SUCCESS
)
226 wsi_swapchain_finish(chain
);
231 wsi_swapchain_is_present_mode_supported(struct wsi_device
*wsi
,
232 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
233 VkPresentModeKHR mode
)
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;
242 result
= iface
->get_present_modes(surface
, &present_mode_count
, NULL
);
243 if (result
!= VK_SUCCESS
)
246 present_modes
= malloc(present_mode_count
* sizeof(*present_modes
));
250 result
= iface
->get_present_modes(surface
, &present_mode_count
,
252 if (result
!= VK_SUCCESS
)
255 for (uint32_t i
= 0; i
< present_mode_count
; i
++) {
256 if (present_modes
[i
] == mode
) {
267 enum VkPresentModeKHR
268 wsi_swapchain_get_present_mode(struct wsi_device
*wsi
,
269 const VkSwapchainCreateInfoKHR
*pCreateInfo
)
271 if (wsi
->override_present_mode
== VK_PRESENT_MODE_MAX_ENUM_KHR
)
272 return pCreateInfo
->presentMode
;
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
;
280 return wsi
->override_present_mode
;
284 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
287 for (unsigned i
= 0; i
< chain
->image_count
; i
++)
288 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
290 vk_free(&chain
->alloc
, chain
->fences
);
293 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
294 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
297 vk_free(&chain
->alloc
, chain
->cmd_pools
);
301 select_memory_type(const struct wsi_device
*wsi
,
302 VkMemoryPropertyFlags props
,
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
)
311 unreachable("No memory type found");
315 vk_format_size(VkFormat format
)
318 case VK_FORMAT_B8G8R8A8_UNORM
:
319 case VK_FORMAT_B8G8R8A8_SRGB
:
322 unreachable("Unknown WSI Format");
326 static inline uint32_t
327 align_u32(uint32_t v
, uint32_t a
)
329 assert(a
!= 0 && a
== (a
& -a
));
330 return (v
+ a
- 1) & ~(a
- 1);
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
)
341 const struct wsi_device
*wsi
= chain
->wsi
;
344 memset(image
, 0, sizeof(*image
));
345 for (int i
= 0; i
< ARRAY_SIZE(image
->fds
); i
++)
348 struct wsi_image_create_info image_wsi_info
= {
349 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
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;
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
,
366 VkFormatProperties2 format_props
= {
367 .sType
= VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2
,
368 .pNext
= &modifier_props_list
,
370 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
371 pCreateInfo
->imageFormat
,
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
,
378 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
379 if (!modifier_props
) {
380 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
384 modifier_props_list
.modifier_properties
= modifier_props
;
385 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
386 pCreateInfo
->imageFormat
,
388 modifier_prop_count
= modifier_props_list
.modifier_count
;
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
]);
394 image_modifiers
= vk_alloc(&chain
->alloc
,
395 sizeof(*image_modifiers
) *
398 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
399 if (!image_modifiers
) {
400 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
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
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
];
416 /* We only want to take the modifiers from the first list */
417 if (image_modifier_count
> 0)
421 if (image_modifier_count
> 0) {
422 image_wsi_info
.modifier_count
= image_modifier_count
;
423 image_wsi_info
.modifiers
= image_modifiers
;
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
;
433 const VkImageCreateInfo image_info
= {
434 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
435 .pNext
= &image_wsi_info
,
437 .imageType
= VK_IMAGE_TYPE_2D
,
438 .format
= pCreateInfo
->imageFormat
,
440 .width
= pCreateInfo
->imageExtent
.width
,
441 .height
= pCreateInfo
->imageExtent
.height
,
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
,
454 result
= wsi
->CreateImage(chain
->device
, &image_info
,
455 &chain
->alloc
, &image
->image
);
456 if (result
!= VK_SUCCESS
)
459 VkMemoryRequirements reqs
;
460 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
462 const struct wsi_memory_allocate_info memory_wsi_info
= {
463 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
465 .implicit_sync
= true,
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
,
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
,
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
),
485 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
486 &chain
->alloc
, &image
->memory
);
487 if (result
!= VK_SUCCESS
)
490 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
492 if (result
!= VK_SUCCESS
)
495 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
496 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
498 .memory
= image
->memory
,
499 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
502 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
503 if (result
!= VK_SUCCESS
)
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
);
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
;
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
,
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
;
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
]);
542 const VkImageSubresource image_subresource
= {
543 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
547 VkSubresourceLayout image_layout
;
548 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
549 &image_subresource
, &image_layout
);
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;
559 vk_free(&chain
->alloc
, modifier_props
);
560 vk_free(&chain
->alloc
, image_modifiers
);
565 vk_free(&chain
->alloc
, modifier_props
);
566 vk_free(&chain
->alloc
, image_modifiers
);
567 wsi_destroy_image(chain
, image
);
572 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
575 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
576 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
578 struct wsi_image
*image
)
580 const struct wsi_device
*wsi
= chain
->wsi
;
583 memset(image
, 0, sizeof(*image
));
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
);
589 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
590 linear_size
= align_u32(linear_size
, 4096);
592 const VkExternalMemoryBufferCreateInfo prime_buffer_external_info
= {
593 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO
,
595 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
597 const VkBufferCreateInfo prime_buffer_info
= {
598 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
599 .pNext
= &prime_buffer_external_info
,
601 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
602 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
604 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
605 &chain
->alloc
, &image
->prime
.buffer
);
606 if (result
!= VK_SUCCESS
)
609 VkMemoryRequirements reqs
;
610 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
611 assert(reqs
.size
<= linear_size
);
613 const struct wsi_memory_allocate_info memory_wsi_info
= {
614 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
616 .implicit_sync
= true,
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
,
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
,
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
),
635 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
636 &chain
->alloc
, &image
->prime
.memory
);
637 if (result
!= VK_SUCCESS
)
640 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
641 image
->prime
.memory
, 0);
642 if (result
!= VK_SUCCESS
)
645 const VkImageCreateInfo image_info
= {
646 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
649 .imageType
= VK_IMAGE_TYPE_2D
,
650 .format
= pCreateInfo
->imageFormat
,
652 .width
= pCreateInfo
->imageExtent
.width
,
653 .height
= pCreateInfo
->imageExtent
.height
,
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
,
666 result
= wsi
->CreateImage(chain
->device
, &image_info
,
667 &chain
->alloc
, &image
->image
);
668 if (result
!= VK_SUCCESS
)
671 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
673 const VkMemoryDedicatedAllocateInfo memory_dedicated_info
= {
674 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
676 .image
= image
->image
,
677 .buffer
= VK_NULL_HANDLE
,
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
),
686 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
687 &chain
->alloc
, &image
->memory
);
688 if (result
!= VK_SUCCESS
)
691 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
693 if (result
!= VK_SUCCESS
)
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
;
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
,
709 .commandPool
= chain
->cmd_pools
[i
],
710 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
711 .commandBufferCount
= 1,
713 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
714 &image
->prime
.blit_cmd_buffers
[i
]);
715 if (result
!= VK_SUCCESS
)
718 const VkCommandBufferBeginInfo begin_info
= {
719 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
721 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
723 struct VkBufferImageCopy buffer_image_copy
= {
725 .bufferRowLength
= linear_stride
/ cpp
,
726 .bufferImageHeight
= 0,
727 .imageSubresource
= {
728 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
733 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
735 .width
= pCreateInfo
->imageExtent
.width
,
736 .height
= pCreateInfo
->imageExtent
.height
,
740 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
742 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
744 1, &buffer_image_copy
);
746 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
747 if (result
!= VK_SUCCESS
)
751 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
752 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
754 .memory
= image
->prime
.memory
,
755 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
758 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
759 if (result
!= VK_SUCCESS
)
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;
772 wsi_destroy_image(chain
, image
);
778 wsi_destroy_image(const struct wsi_swapchain
*chain
,
779 struct wsi_image
*image
)
781 const struct wsi_device
*wsi
= chain
->wsi
;
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
]);
788 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
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
);
798 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
799 uint32_t queueFamilyIndex
,
800 VkSurfaceKHR _surface
,
801 VkBool32
* pSupported
)
803 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
804 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
806 return iface
->get_support(surface
, wsi_device
,
807 queueFamilyIndex
, pSupported
);
811 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
812 VkSurfaceKHR _surface
,
813 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
815 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
816 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
818 VkSurfaceCapabilities2KHR caps2
= {
819 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
822 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
824 if (result
== VK_SUCCESS
)
825 *pSurfaceCapabilities
= caps2
.surfaceCapabilities
;
831 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
832 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
833 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
835 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
836 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
838 return iface
->get_capabilities2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
839 pSurfaceCapabilities
);
843 wsi_common_get_surface_capabilities2ext(
844 struct wsi_device
*wsi_device
,
845 VkSurfaceKHR _surface
,
846 VkSurfaceCapabilities2EXT
*pSurfaceCapabilities
)
848 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
849 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
851 assert(pSurfaceCapabilities
->sType
==
852 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT
);
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,
860 VkSurfaceCapabilities2KHR caps2
= {
861 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
865 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
867 if (result
== VK_SUCCESS
) {
868 VkSurfaceCapabilities2EXT
*ext_caps
= pSurfaceCapabilities
;
869 VkSurfaceCapabilitiesKHR khr_caps
= caps2
.surfaceCapabilities
;
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
;
888 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
889 VkSurfaceKHR _surface
,
890 uint32_t *pSurfaceFormatCount
,
891 VkSurfaceFormatKHR
*pSurfaceFormats
)
893 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
894 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
896 return iface
->get_formats(surface
, wsi_device
,
897 pSurfaceFormatCount
, pSurfaceFormats
);
901 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
902 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
903 uint32_t *pSurfaceFormatCount
,
904 VkSurfaceFormat2KHR
*pSurfaceFormats
)
906 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
907 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
909 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
910 pSurfaceFormatCount
, pSurfaceFormats
);
914 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
915 VkSurfaceKHR _surface
,
916 uint32_t *pPresentModeCount
,
917 VkPresentModeKHR
*pPresentModes
)
919 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
920 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
922 return iface
->get_present_modes(surface
, pPresentModeCount
,
927 wsi_common_get_present_rectangles(struct wsi_device
*wsi_device
,
928 VkSurfaceKHR _surface
,
929 uint32_t* pRectCount
,
932 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
933 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
935 return iface
->get_present_rectangles(surface
, wsi_device
,
940 wsi_common_create_swapchain(struct wsi_device
*wsi
,
942 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
943 const VkAllocationCallbacks
*pAllocator
,
944 VkSwapchainKHR
*pSwapchain
)
946 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
947 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
948 struct wsi_swapchain
*swapchain
;
950 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
,
951 pCreateInfo
, pAllocator
,
953 if (result
!= VK_SUCCESS
)
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
;
965 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
971 wsi_common_destroy_swapchain(VkDevice device
,
972 VkSwapchainKHR _swapchain
,
973 const VkAllocationCallbacks
*pAllocator
)
975 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
979 swapchain
->destroy(swapchain
, pAllocator
);
983 wsi_common_get_images(VkSwapchainKHR _swapchain
,
984 uint32_t *pSwapchainImageCount
,
985 VkImage
*pSwapchainImages
)
987 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
988 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
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
;
996 return vk_outarray_status(&images
);
1000 wsi_common_acquire_next_image2(const struct wsi_device
*wsi
,
1002 const VkAcquireNextImageInfoKHR
*pAcquireInfo
,
1003 uint32_t *pImageIndex
)
1005 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pAcquireInfo
->swapchain
);
1007 VkResult result
= swapchain
->acquire_next_image(swapchain
, pAcquireInfo
,
1009 if (result
!= VK_SUCCESS
)
1012 if (pAcquireInfo
->semaphore
!= VK_NULL_HANDLE
&&
1013 wsi
->signal_semaphore_for_memory
!= NULL
) {
1014 struct wsi_image
*image
=
1015 swapchain
->get_wsi_image(swapchain
, *pImageIndex
);
1016 wsi
->signal_semaphore_for_memory(device
, pAcquireInfo
->semaphore
,
1020 if (pAcquireInfo
->fence
!= VK_NULL_HANDLE
&&
1021 wsi
->signal_fence_for_memory
!= NULL
) {
1022 struct wsi_image
*image
=
1023 swapchain
->get_wsi_image(swapchain
, *pImageIndex
);
1024 wsi
->signal_fence_for_memory(device
, pAcquireInfo
->fence
,
1032 wsi_common_queue_present(const struct wsi_device
*wsi
,
1035 int queue_family_index
,
1036 const VkPresentInfoKHR
*pPresentInfo
)
1038 VkResult final_result
= VK_SUCCESS
;
1040 const VkPresentRegionsKHR
*regions
=
1041 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
1043 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
1044 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
1045 uint32_t image_index
= pPresentInfo
->pImageIndices
[i
];
1048 if (swapchain
->fences
[image_index
] == VK_NULL_HANDLE
) {
1049 const VkFenceCreateInfo fence_info
= {
1050 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
1054 result
= wsi
->CreateFence(device
, &fence_info
,
1056 &swapchain
->fences
[image_index
]);
1057 if (result
!= VK_SUCCESS
)
1060 wsi
->WaitForFences(device
, 1, &swapchain
->fences
[image_index
],
1063 wsi
->ResetFences(device
, 1, &swapchain
->fences
[image_index
]);
1066 struct wsi_image
*image
=
1067 swapchain
->get_wsi_image(swapchain
, image_index
);
1069 struct wsi_memory_signal_submit_info mem_signal
= {
1070 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA
,
1072 .memory
= image
->memory
,
1075 VkSubmitInfo submit_info
= {
1076 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
1077 .pNext
= &mem_signal
,
1080 VkPipelineStageFlags
*stage_flags
= NULL
;
1082 /* We only need/want to wait on semaphores once. After that, we're
1083 * guaranteed ordering since it all happens on the same queue.
1085 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
;
1086 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
;
1088 /* Set up the pWaitDstStageMasks */
1089 stage_flags
= vk_alloc(&swapchain
->alloc
,
1090 sizeof(VkPipelineStageFlags
) *
1091 pPresentInfo
->waitSemaphoreCount
,
1093 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1095 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1098 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
1099 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
1101 submit_info
.pWaitDstStageMask
= stage_flags
;
1104 if (swapchain
->use_prime_blit
) {
1105 /* If we are using prime blits, we need to perform the blit now. The
1106 * command buffer is attached to the image.
1108 submit_info
.commandBufferCount
= 1;
1109 submit_info
.pCommandBuffers
=
1110 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
1111 mem_signal
.memory
= image
->prime
.memory
;
1114 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[image_index
]);
1115 vk_free(&swapchain
->alloc
, stage_flags
);
1116 if (result
!= VK_SUCCESS
)
1119 const VkPresentRegionKHR
*region
= NULL
;
1120 if (regions
&& regions
->pRegions
)
1121 region
= ®ions
->pRegions
[i
];
1123 result
= swapchain
->queue_present(swapchain
, image_index
, region
);
1124 if (result
!= VK_SUCCESS
)
1128 if (pPresentInfo
->pResults
!= NULL
)
1129 pPresentInfo
->pResults
[i
] = result
;
1131 /* Let the final result be our first unsuccessful result */
1132 if (final_result
== VK_SUCCESS
)
1133 final_result
= result
;
1136 return final_result
;
1140 wsi_common_get_current_time(void)
1142 struct timespec current
;
1143 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
1144 return current
.tv_nsec
+ current
.tv_sec
* 1000000000ull;