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
,
139 if (driCheckOption(dri_options
, "vk_wsi_force_bgra8_unorm_first", DRI_BOOL
)) {
140 wsi
->force_bgra8_unorm_first
=
141 driQueryOptionb(dri_options
, "vk_wsi_force_bgra8_unorm_first");
148 wsi_device_finish(wsi
, alloc
);
153 wsi_device_finish(struct wsi_device
*wsi
,
154 const VkAllocationCallbacks
*alloc
)
156 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
157 wsi_display_finish_wsi(wsi
, alloc
);
159 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
160 wsi_wl_finish_wsi(wsi
, alloc
);
162 #ifdef VK_USE_PLATFORM_XCB_KHR
163 wsi_x11_finish_wsi(wsi
, alloc
);
168 wsi_device_matches_drm_fd(const struct wsi_device
*wsi
, int drm_fd
)
170 drmDevicePtr fd_device
;
171 int ret
= drmGetDevice2(drm_fd
, 0, &fd_device
);
176 switch (fd_device
->bustype
) {
178 match
= wsi
->pci_bus_info
.pciDomain
== fd_device
->businfo
.pci
->domain
&&
179 wsi
->pci_bus_info
.pciBus
== fd_device
->businfo
.pci
->bus
&&
180 wsi
->pci_bus_info
.pciDevice
== fd_device
->businfo
.pci
->dev
&&
181 wsi
->pci_bus_info
.pciFunction
== fd_device
->businfo
.pci
->func
;
188 drmFreeDevice(&fd_device
);
194 wsi_swapchain_init(const struct wsi_device
*wsi
,
195 struct wsi_swapchain
*chain
,
197 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
198 const VkAllocationCallbacks
*pAllocator
)
202 memset(chain
, 0, sizeof(*chain
));
205 chain
->device
= device
;
206 chain
->alloc
= *pAllocator
;
207 chain
->use_prime_blit
= false;
210 vk_zalloc(pAllocator
, sizeof(VkCommandPool
) * wsi
->queue_family_count
, 8,
211 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
212 if (!chain
->cmd_pools
)
213 return VK_ERROR_OUT_OF_HOST_MEMORY
;
215 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
216 const VkCommandPoolCreateInfo cmd_pool_info
= {
217 .sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
,
220 .queueFamilyIndex
= i
,
222 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
223 &chain
->cmd_pools
[i
]);
224 if (result
!= VK_SUCCESS
)
231 wsi_swapchain_finish(chain
);
236 wsi_swapchain_is_present_mode_supported(struct wsi_device
*wsi
,
237 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
238 VkPresentModeKHR mode
)
240 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
241 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
242 VkPresentModeKHR
*present_modes
;
243 uint32_t present_mode_count
;
244 bool supported
= false;
247 result
= iface
->get_present_modes(surface
, &present_mode_count
, NULL
);
248 if (result
!= VK_SUCCESS
)
251 present_modes
= malloc(present_mode_count
* sizeof(*present_modes
));
255 result
= iface
->get_present_modes(surface
, &present_mode_count
,
257 if (result
!= VK_SUCCESS
)
260 for (uint32_t i
= 0; i
< present_mode_count
; i
++) {
261 if (present_modes
[i
] == mode
) {
272 enum VkPresentModeKHR
273 wsi_swapchain_get_present_mode(struct wsi_device
*wsi
,
274 const VkSwapchainCreateInfoKHR
*pCreateInfo
)
276 if (wsi
->override_present_mode
== VK_PRESENT_MODE_MAX_ENUM_KHR
)
277 return pCreateInfo
->presentMode
;
279 if (!wsi_swapchain_is_present_mode_supported(wsi
, pCreateInfo
,
280 wsi
->override_present_mode
)) {
281 fprintf(stderr
, "Unsupported MESA_VK_WSI_PRESENT_MODE value!\n");
282 return pCreateInfo
->presentMode
;
285 return wsi
->override_present_mode
;
289 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
292 for (unsigned i
= 0; i
< chain
->image_count
; i
++)
293 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
295 vk_free(&chain
->alloc
, chain
->fences
);
298 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
299 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
302 vk_free(&chain
->alloc
, chain
->cmd_pools
);
306 select_memory_type(const struct wsi_device
*wsi
,
307 VkMemoryPropertyFlags props
,
310 for (uint32_t i
= 0; i
< wsi
->memory_props
.memoryTypeCount
; i
++) {
311 const VkMemoryType type
= wsi
->memory_props
.memoryTypes
[i
];
312 if ((type_bits
& (1 << i
)) && (type
.propertyFlags
& props
) == props
)
316 unreachable("No memory type found");
320 vk_format_size(VkFormat format
)
323 case VK_FORMAT_B8G8R8A8_UNORM
:
324 case VK_FORMAT_B8G8R8A8_SRGB
:
327 unreachable("Unknown WSI Format");
331 static inline uint32_t
332 align_u32(uint32_t v
, uint32_t a
)
334 assert(a
!= 0 && a
== (a
& -a
));
335 return (v
+ a
- 1) & ~(a
- 1);
339 wsi_create_native_image(const struct wsi_swapchain
*chain
,
340 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
341 uint32_t num_modifier_lists
,
342 const uint32_t *num_modifiers
,
343 const uint64_t *const *modifiers
,
344 struct wsi_image
*image
)
346 const struct wsi_device
*wsi
= chain
->wsi
;
349 memset(image
, 0, sizeof(*image
));
350 for (int i
= 0; i
< ARRAY_SIZE(image
->fds
); i
++)
353 struct wsi_image_create_info image_wsi_info
= {
354 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
358 VkImageCreateInfo image_info
= {
359 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
361 .imageType
= VK_IMAGE_TYPE_2D
,
362 .format
= pCreateInfo
->imageFormat
,
364 .width
= pCreateInfo
->imageExtent
.width
,
365 .height
= pCreateInfo
->imageExtent
.height
,
370 .samples
= VK_SAMPLE_COUNT_1_BIT
,
371 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
372 .usage
= pCreateInfo
->imageUsage
,
373 .sharingMode
= pCreateInfo
->imageSharingMode
,
374 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
375 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
376 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
379 __vk_append_struct(&image_info
, &image_wsi_info
);
381 uint32_t image_modifier_count
= 0, modifier_prop_count
= 0;
382 struct wsi_format_modifier_properties
*modifier_props
= NULL
;
383 uint64_t *image_modifiers
= NULL
;
384 if (num_modifier_lists
== 0) {
385 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
386 image_wsi_info
.scanout
= true;
388 /* The winsys can't request modifiers if we don't support them. */
389 assert(wsi
->supports_modifiers
);
390 struct wsi_format_modifier_properties_list modifier_props_list
= {
391 .sType
= VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA
,
394 VkFormatProperties2 format_props
= {
395 .sType
= VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2
,
396 .pNext
= &modifier_props_list
,
398 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
399 pCreateInfo
->imageFormat
,
401 assert(modifier_props_list
.modifier_count
> 0);
402 modifier_props
= vk_alloc(&chain
->alloc
,
403 sizeof(*modifier_props
) *
404 modifier_props_list
.modifier_count
,
406 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
407 if (!modifier_props
) {
408 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
412 modifier_props_list
.modifier_properties
= modifier_props
;
413 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
414 pCreateInfo
->imageFormat
,
416 modifier_prop_count
= modifier_props_list
.modifier_count
;
418 uint32_t max_modifier_count
= 0;
419 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++)
420 max_modifier_count
= MAX2(max_modifier_count
, num_modifiers
[l
]);
422 image_modifiers
= vk_alloc(&chain
->alloc
,
423 sizeof(*image_modifiers
) *
426 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
427 if (!image_modifiers
) {
428 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
432 image_modifier_count
= 0;
433 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++) {
434 /* Walk the modifier lists and construct a list of supported
437 for (uint32_t i
= 0; i
< num_modifiers
[l
]; i
++) {
438 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
439 if (modifier_props
[j
].modifier
== modifiers
[l
][i
])
440 image_modifiers
[image_modifier_count
++] = modifiers
[l
][i
];
444 /* We only want to take the modifiers from the first list */
445 if (image_modifier_count
> 0)
449 if (image_modifier_count
> 0) {
450 image_wsi_info
.modifier_count
= image_modifier_count
;
451 image_wsi_info
.modifiers
= image_modifiers
;
453 /* TODO: Add a proper error here */
454 assert(!"Failed to find a supported modifier! This should never "
455 "happen because LINEAR should always be available");
456 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
461 result
= wsi
->CreateImage(chain
->device
, &image_info
,
462 &chain
->alloc
, &image
->image
);
463 if (result
!= VK_SUCCESS
)
466 VkMemoryRequirements reqs
;
467 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
469 const struct wsi_memory_allocate_info memory_wsi_info
= {
470 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
472 .implicit_sync
= true,
474 const VkExportMemoryAllocateInfo memory_export_info
= {
475 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO
,
476 .pNext
= &memory_wsi_info
,
477 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
479 const VkMemoryDedicatedAllocateInfo memory_dedicated_info
= {
480 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
481 .pNext
= &memory_export_info
,
482 .image
= image
->image
,
483 .buffer
= VK_NULL_HANDLE
,
485 const VkMemoryAllocateInfo memory_info
= {
486 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
487 .pNext
= &memory_dedicated_info
,
488 .allocationSize
= reqs
.size
,
489 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
490 reqs
.memoryTypeBits
),
492 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
493 &chain
->alloc
, &image
->memory
);
494 if (result
!= VK_SUCCESS
)
497 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
499 if (result
!= VK_SUCCESS
)
502 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
503 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
505 .memory
= image
->memory
,
506 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
509 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
510 if (result
!= VK_SUCCESS
)
513 if (num_modifier_lists
> 0) {
514 image
->drm_modifier
= wsi
->image_get_modifier(image
->image
);
515 assert(image
->drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
517 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
518 if (modifier_props
[j
].modifier
== image
->drm_modifier
) {
519 image
->num_planes
= modifier_props
[j
].modifier_plane_count
;
524 for (uint32_t p
= 0; p
< image
->num_planes
; p
++) {
525 const VkImageSubresource image_subresource
= {
526 .aspectMask
= VK_IMAGE_ASPECT_PLANE_0_BIT
<< p
,
530 VkSubresourceLayout image_layout
;
531 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
532 &image_subresource
, &image_layout
);
533 image
->sizes
[p
] = image_layout
.size
;
534 image
->row_pitches
[p
] = image_layout
.rowPitch
;
535 image
->offsets
[p
] = image_layout
.offset
;
539 image
->fds
[p
] = dup(fd
);
540 if (image
->fds
[p
] == -1) {
541 for (uint32_t i
= 0; i
< p
; i
++)
542 close(image
->fds
[p
]);
549 const VkImageSubresource image_subresource
= {
550 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
554 VkSubresourceLayout image_layout
;
555 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
556 &image_subresource
, &image_layout
);
558 image
->drm_modifier
= DRM_FORMAT_MOD_INVALID
;
559 image
->num_planes
= 1;
560 image
->sizes
[0] = reqs
.size
;
561 image
->row_pitches
[0] = image_layout
.rowPitch
;
562 image
->offsets
[0] = 0;
566 vk_free(&chain
->alloc
, modifier_props
);
567 vk_free(&chain
->alloc
, image_modifiers
);
572 vk_free(&chain
->alloc
, modifier_props
);
573 vk_free(&chain
->alloc
, image_modifiers
);
574 wsi_destroy_image(chain
, image
);
579 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
582 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
583 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
585 struct wsi_image
*image
)
587 const struct wsi_device
*wsi
= chain
->wsi
;
590 memset(image
, 0, sizeof(*image
));
592 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
593 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
594 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
596 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
597 linear_size
= align_u32(linear_size
, 4096);
599 const VkExternalMemoryBufferCreateInfo prime_buffer_external_info
= {
600 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO
,
602 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
604 const VkBufferCreateInfo prime_buffer_info
= {
605 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
606 .pNext
= &prime_buffer_external_info
,
608 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
609 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
611 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
612 &chain
->alloc
, &image
->prime
.buffer
);
613 if (result
!= VK_SUCCESS
)
616 VkMemoryRequirements reqs
;
617 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
618 assert(reqs
.size
<= linear_size
);
620 const struct wsi_memory_allocate_info memory_wsi_info
= {
621 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
623 .implicit_sync
= true,
625 const VkExportMemoryAllocateInfo prime_memory_export_info
= {
626 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO
,
627 .pNext
= &memory_wsi_info
,
628 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
630 const VkMemoryDedicatedAllocateInfo prime_memory_dedicated_info
= {
631 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
632 .pNext
= &prime_memory_export_info
,
633 .image
= VK_NULL_HANDLE
,
634 .buffer
= image
->prime
.buffer
,
636 const VkMemoryAllocateInfo prime_memory_info
= {
637 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
638 .pNext
= &prime_memory_dedicated_info
,
639 .allocationSize
= linear_size
,
640 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
642 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
643 &chain
->alloc
, &image
->prime
.memory
);
644 if (result
!= VK_SUCCESS
)
647 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
648 image
->prime
.memory
, 0);
649 if (result
!= VK_SUCCESS
)
652 const VkImageCreateInfo image_info
= {
653 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
656 .imageType
= VK_IMAGE_TYPE_2D
,
657 .format
= pCreateInfo
->imageFormat
,
659 .width
= pCreateInfo
->imageExtent
.width
,
660 .height
= pCreateInfo
->imageExtent
.height
,
665 .samples
= VK_SAMPLE_COUNT_1_BIT
,
666 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
667 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
668 .sharingMode
= pCreateInfo
->imageSharingMode
,
669 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
670 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
671 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
673 result
= wsi
->CreateImage(chain
->device
, &image_info
,
674 &chain
->alloc
, &image
->image
);
675 if (result
!= VK_SUCCESS
)
678 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
680 const VkMemoryDedicatedAllocateInfo memory_dedicated_info
= {
681 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
683 .image
= image
->image
,
684 .buffer
= VK_NULL_HANDLE
,
686 const VkMemoryAllocateInfo memory_info
= {
687 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
688 .pNext
= &memory_dedicated_info
,
689 .allocationSize
= reqs
.size
,
690 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
691 reqs
.memoryTypeBits
),
693 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
694 &chain
->alloc
, &image
->memory
);
695 if (result
!= VK_SUCCESS
)
698 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
700 if (result
!= VK_SUCCESS
)
703 image
->prime
.blit_cmd_buffers
=
704 vk_zalloc(&chain
->alloc
,
705 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
706 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
707 if (!image
->prime
.blit_cmd_buffers
) {
708 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
712 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
713 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
714 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
716 .commandPool
= chain
->cmd_pools
[i
],
717 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
718 .commandBufferCount
= 1,
720 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
721 &image
->prime
.blit_cmd_buffers
[i
]);
722 if (result
!= VK_SUCCESS
)
725 const VkCommandBufferBeginInfo begin_info
= {
726 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
728 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
730 struct VkBufferImageCopy buffer_image_copy
= {
732 .bufferRowLength
= linear_stride
/ cpp
,
733 .bufferImageHeight
= 0,
734 .imageSubresource
= {
735 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
740 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
742 .width
= pCreateInfo
->imageExtent
.width
,
743 .height
= pCreateInfo
->imageExtent
.height
,
747 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
749 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
751 1, &buffer_image_copy
);
753 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
754 if (result
!= VK_SUCCESS
)
758 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
759 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
761 .memory
= image
->prime
.memory
,
762 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
765 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
766 if (result
!= VK_SUCCESS
)
769 image
->drm_modifier
= use_modifier
? DRM_FORMAT_MOD_LINEAR
: DRM_FORMAT_MOD_INVALID
;
770 image
->num_planes
= 1;
771 image
->sizes
[0] = linear_size
;
772 image
->row_pitches
[0] = linear_stride
;
773 image
->offsets
[0] = 0;
779 wsi_destroy_image(chain
, image
);
785 wsi_destroy_image(const struct wsi_swapchain
*chain
,
786 struct wsi_image
*image
)
788 const struct wsi_device
*wsi
= chain
->wsi
;
790 if (image
->prime
.blit_cmd_buffers
) {
791 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
792 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
793 1, &image
->prime
.blit_cmd_buffers
[i
]);
795 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
798 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
799 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
800 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
801 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
805 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
806 uint32_t queueFamilyIndex
,
807 VkSurfaceKHR _surface
,
808 VkBool32
* pSupported
)
810 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
811 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
813 return iface
->get_support(surface
, wsi_device
,
814 queueFamilyIndex
, pSupported
);
818 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
819 VkSurfaceKHR _surface
,
820 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
822 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
823 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
825 VkSurfaceCapabilities2KHR caps2
= {
826 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
829 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
831 if (result
== VK_SUCCESS
)
832 *pSurfaceCapabilities
= caps2
.surfaceCapabilities
;
838 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
839 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
840 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
842 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
843 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
845 return iface
->get_capabilities2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
846 pSurfaceCapabilities
);
850 wsi_common_get_surface_capabilities2ext(
851 struct wsi_device
*wsi_device
,
852 VkSurfaceKHR _surface
,
853 VkSurfaceCapabilities2EXT
*pSurfaceCapabilities
)
855 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
856 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
858 assert(pSurfaceCapabilities
->sType
==
859 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT
);
861 struct wsi_surface_supported_counters counters
= {
862 .sType
= VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA
,
863 .pNext
= pSurfaceCapabilities
->pNext
,
864 .supported_surface_counters
= 0,
867 VkSurfaceCapabilities2KHR caps2
= {
868 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
872 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
874 if (result
== VK_SUCCESS
) {
875 VkSurfaceCapabilities2EXT
*ext_caps
= pSurfaceCapabilities
;
876 VkSurfaceCapabilitiesKHR khr_caps
= caps2
.surfaceCapabilities
;
878 ext_caps
->minImageCount
= khr_caps
.minImageCount
;
879 ext_caps
->maxImageCount
= khr_caps
.maxImageCount
;
880 ext_caps
->currentExtent
= khr_caps
.currentExtent
;
881 ext_caps
->minImageExtent
= khr_caps
.minImageExtent
;
882 ext_caps
->maxImageExtent
= khr_caps
.maxImageExtent
;
883 ext_caps
->maxImageArrayLayers
= khr_caps
.maxImageArrayLayers
;
884 ext_caps
->supportedTransforms
= khr_caps
.supportedTransforms
;
885 ext_caps
->currentTransform
= khr_caps
.currentTransform
;
886 ext_caps
->supportedCompositeAlpha
= khr_caps
.supportedCompositeAlpha
;
887 ext_caps
->supportedUsageFlags
= khr_caps
.supportedUsageFlags
;
888 ext_caps
->supportedSurfaceCounters
= counters
.supported_surface_counters
;
895 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
896 VkSurfaceKHR _surface
,
897 uint32_t *pSurfaceFormatCount
,
898 VkSurfaceFormatKHR
*pSurfaceFormats
)
900 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
901 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
903 return iface
->get_formats(surface
, wsi_device
,
904 pSurfaceFormatCount
, pSurfaceFormats
);
908 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
909 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
910 uint32_t *pSurfaceFormatCount
,
911 VkSurfaceFormat2KHR
*pSurfaceFormats
)
913 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
914 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
916 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
917 pSurfaceFormatCount
, pSurfaceFormats
);
921 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
922 VkSurfaceKHR _surface
,
923 uint32_t *pPresentModeCount
,
924 VkPresentModeKHR
*pPresentModes
)
926 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
927 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
929 return iface
->get_present_modes(surface
, pPresentModeCount
,
934 wsi_common_get_present_rectangles(struct wsi_device
*wsi_device
,
935 VkSurfaceKHR _surface
,
936 uint32_t* pRectCount
,
939 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
940 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
942 return iface
->get_present_rectangles(surface
, wsi_device
,
947 wsi_common_create_swapchain(struct wsi_device
*wsi
,
949 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
950 const VkAllocationCallbacks
*pAllocator
,
951 VkSwapchainKHR
*pSwapchain
)
953 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
954 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
955 struct wsi_swapchain
*swapchain
;
957 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
,
958 pCreateInfo
, pAllocator
,
960 if (result
!= VK_SUCCESS
)
963 swapchain
->fences
= vk_zalloc(pAllocator
,
964 sizeof (*swapchain
->fences
) * swapchain
->image_count
,
965 sizeof (*swapchain
->fences
),
966 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
967 if (!swapchain
->fences
) {
968 swapchain
->destroy(swapchain
, pAllocator
);
969 return VK_ERROR_OUT_OF_HOST_MEMORY
;
972 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
978 wsi_common_destroy_swapchain(VkDevice device
,
979 VkSwapchainKHR _swapchain
,
980 const VkAllocationCallbacks
*pAllocator
)
982 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
986 swapchain
->destroy(swapchain
, pAllocator
);
990 wsi_common_get_images(VkSwapchainKHR _swapchain
,
991 uint32_t *pSwapchainImageCount
,
992 VkImage
*pSwapchainImages
)
994 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
995 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
997 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
998 vk_outarray_append(&images
, image
) {
999 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
1003 return vk_outarray_status(&images
);
1007 wsi_common_acquire_next_image2(const struct wsi_device
*wsi
,
1009 const VkAcquireNextImageInfoKHR
*pAcquireInfo
,
1010 uint32_t *pImageIndex
)
1012 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pAcquireInfo
->swapchain
);
1014 VkResult result
= swapchain
->acquire_next_image(swapchain
, pAcquireInfo
,
1016 if (result
!= VK_SUCCESS
)
1019 if (pAcquireInfo
->semaphore
!= VK_NULL_HANDLE
&&
1020 wsi
->signal_semaphore_for_memory
!= NULL
) {
1021 struct wsi_image
*image
=
1022 swapchain
->get_wsi_image(swapchain
, *pImageIndex
);
1023 wsi
->signal_semaphore_for_memory(device
, pAcquireInfo
->semaphore
,
1027 if (pAcquireInfo
->fence
!= VK_NULL_HANDLE
&&
1028 wsi
->signal_fence_for_memory
!= NULL
) {
1029 struct wsi_image
*image
=
1030 swapchain
->get_wsi_image(swapchain
, *pImageIndex
);
1031 wsi
->signal_fence_for_memory(device
, pAcquireInfo
->fence
,
1039 wsi_common_queue_present(const struct wsi_device
*wsi
,
1042 int queue_family_index
,
1043 const VkPresentInfoKHR
*pPresentInfo
)
1045 VkResult final_result
= VK_SUCCESS
;
1047 const VkPresentRegionsKHR
*regions
=
1048 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
1050 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
1051 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
1052 uint32_t image_index
= pPresentInfo
->pImageIndices
[i
];
1055 if (swapchain
->fences
[image_index
] == VK_NULL_HANDLE
) {
1056 const VkFenceCreateInfo fence_info
= {
1057 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
1061 result
= wsi
->CreateFence(device
, &fence_info
,
1063 &swapchain
->fences
[image_index
]);
1064 if (result
!= VK_SUCCESS
)
1068 wsi
->WaitForFences(device
, 1, &swapchain
->fences
[image_index
],
1070 if (result
!= VK_SUCCESS
)
1074 wsi
->ResetFences(device
, 1, &swapchain
->fences
[image_index
]);
1075 if (result
!= VK_SUCCESS
)
1079 struct wsi_image
*image
=
1080 swapchain
->get_wsi_image(swapchain
, image_index
);
1082 struct wsi_memory_signal_submit_info mem_signal
= {
1083 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA
,
1085 .memory
= image
->memory
,
1088 VkSubmitInfo submit_info
= {
1089 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
1090 .pNext
= &mem_signal
,
1093 VkPipelineStageFlags
*stage_flags
= NULL
;
1095 /* We only need/want to wait on semaphores once. After that, we're
1096 * guaranteed ordering since it all happens on the same queue.
1098 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
;
1099 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
;
1101 /* Set up the pWaitDstStageMasks */
1102 stage_flags
= vk_alloc(&swapchain
->alloc
,
1103 sizeof(VkPipelineStageFlags
) *
1104 pPresentInfo
->waitSemaphoreCount
,
1106 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1108 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1111 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
1112 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
1114 submit_info
.pWaitDstStageMask
= stage_flags
;
1117 if (swapchain
->use_prime_blit
) {
1118 /* If we are using prime blits, we need to perform the blit now. The
1119 * command buffer is attached to the image.
1121 submit_info
.commandBufferCount
= 1;
1122 submit_info
.pCommandBuffers
=
1123 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
1124 mem_signal
.memory
= image
->prime
.memory
;
1127 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[image_index
]);
1128 vk_free(&swapchain
->alloc
, stage_flags
);
1129 if (result
!= VK_SUCCESS
)
1132 const VkPresentRegionKHR
*region
= NULL
;
1133 if (regions
&& regions
->pRegions
)
1134 region
= ®ions
->pRegions
[i
];
1136 result
= swapchain
->queue_present(swapchain
, image_index
, region
);
1137 if (result
!= VK_SUCCESS
)
1141 if (pPresentInfo
->pResults
!= NULL
)
1142 pPresentInfo
->pResults
[i
] = result
;
1144 /* Let the final result be our first unsuccessful result */
1145 if (final_result
== VK_SUCCESS
)
1146 final_result
= result
;
1149 return final_result
;
1153 wsi_common_get_current_time(void)
1155 struct timespec current
;
1156 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
1157 return current
.tv_nsec
+ current
.tv_sec
* 1000000000ull;