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(GetImageDrmFormatModifierPropertiesEXT
);
94 WSI_GET_CB(GetImageMemoryRequirements
);
95 WSI_GET_CB(GetImageSubresourceLayout
);
96 WSI_GET_CB(GetMemoryFdKHR
);
97 WSI_GET_CB(GetPhysicalDeviceFormatProperties
);
98 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR
);
99 WSI_GET_CB(ResetFences
);
100 WSI_GET_CB(QueueSubmit
);
101 WSI_GET_CB(WaitForFences
);
104 #ifdef VK_USE_PLATFORM_XCB_KHR
105 result
= wsi_x11_init_wsi(wsi
, alloc
, dri_options
);
106 if (result
!= VK_SUCCESS
)
110 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
111 result
= wsi_wl_init_wsi(wsi
, alloc
, pdevice
);
112 if (result
!= VK_SUCCESS
)
116 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
117 result
= wsi_display_init_wsi(wsi
, alloc
, display_fd
);
118 if (result
!= VK_SUCCESS
)
122 present_mode
= getenv("MESA_VK_WSI_PRESENT_MODE");
124 if (!strcmp(present_mode
, "fifo")) {
125 wsi
->override_present_mode
= VK_PRESENT_MODE_FIFO_KHR
;
126 } else if (!strcmp(present_mode
, "mailbox")) {
127 wsi
->override_present_mode
= VK_PRESENT_MODE_MAILBOX_KHR
;
128 } else if (!strcmp(present_mode
, "immediate")) {
129 wsi
->override_present_mode
= VK_PRESENT_MODE_IMMEDIATE_KHR
;
131 fprintf(stderr
, "Invalid MESA_VK_WSI_PRESENT_MODE value!\n");
136 if (driCheckOption(dri_options
, "adaptive_sync", DRI_BOOL
))
137 wsi
->enable_adaptive_sync
= driQueryOptionb(dri_options
,
140 if (driCheckOption(dri_options
, "vk_wsi_force_bgra8_unorm_first", DRI_BOOL
)) {
141 wsi
->force_bgra8_unorm_first
=
142 driQueryOptionb(dri_options
, "vk_wsi_force_bgra8_unorm_first");
149 wsi_device_finish(wsi
, alloc
);
154 wsi_device_finish(struct wsi_device
*wsi
,
155 const VkAllocationCallbacks
*alloc
)
157 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
158 wsi_display_finish_wsi(wsi
, alloc
);
160 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
161 wsi_wl_finish_wsi(wsi
, alloc
);
163 #ifdef VK_USE_PLATFORM_XCB_KHR
164 wsi_x11_finish_wsi(wsi
, alloc
);
169 wsi_device_matches_drm_fd(const struct wsi_device
*wsi
, int drm_fd
)
171 drmDevicePtr fd_device
;
172 int ret
= drmGetDevice2(drm_fd
, 0, &fd_device
);
177 switch (fd_device
->bustype
) {
179 match
= wsi
->pci_bus_info
.pciDomain
== fd_device
->businfo
.pci
->domain
&&
180 wsi
->pci_bus_info
.pciBus
== fd_device
->businfo
.pci
->bus
&&
181 wsi
->pci_bus_info
.pciDevice
== fd_device
->businfo
.pci
->dev
&&
182 wsi
->pci_bus_info
.pciFunction
== fd_device
->businfo
.pci
->func
;
189 drmFreeDevice(&fd_device
);
195 wsi_swapchain_init(const struct wsi_device
*wsi
,
196 struct wsi_swapchain
*chain
,
198 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
199 const VkAllocationCallbacks
*pAllocator
)
203 memset(chain
, 0, sizeof(*chain
));
206 chain
->device
= device
;
207 chain
->alloc
= *pAllocator
;
208 chain
->use_prime_blit
= false;
211 vk_zalloc(pAllocator
, sizeof(VkCommandPool
) * wsi
->queue_family_count
, 8,
212 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
213 if (!chain
->cmd_pools
)
214 return VK_ERROR_OUT_OF_HOST_MEMORY
;
216 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
217 const VkCommandPoolCreateInfo cmd_pool_info
= {
218 .sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
,
221 .queueFamilyIndex
= i
,
223 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
224 &chain
->cmd_pools
[i
]);
225 if (result
!= VK_SUCCESS
)
232 wsi_swapchain_finish(chain
);
237 wsi_swapchain_is_present_mode_supported(struct wsi_device
*wsi
,
238 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
239 VkPresentModeKHR mode
)
241 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
242 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
243 VkPresentModeKHR
*present_modes
;
244 uint32_t present_mode_count
;
245 bool supported
= false;
248 result
= iface
->get_present_modes(surface
, &present_mode_count
, NULL
);
249 if (result
!= VK_SUCCESS
)
252 present_modes
= malloc(present_mode_count
* sizeof(*present_modes
));
256 result
= iface
->get_present_modes(surface
, &present_mode_count
,
258 if (result
!= VK_SUCCESS
)
261 for (uint32_t i
= 0; i
< present_mode_count
; i
++) {
262 if (present_modes
[i
] == mode
) {
273 enum VkPresentModeKHR
274 wsi_swapchain_get_present_mode(struct wsi_device
*wsi
,
275 const VkSwapchainCreateInfoKHR
*pCreateInfo
)
277 if (wsi
->override_present_mode
== VK_PRESENT_MODE_MAX_ENUM_KHR
)
278 return pCreateInfo
->presentMode
;
280 if (!wsi_swapchain_is_present_mode_supported(wsi
, pCreateInfo
,
281 wsi
->override_present_mode
)) {
282 fprintf(stderr
, "Unsupported MESA_VK_WSI_PRESENT_MODE value!\n");
283 return pCreateInfo
->presentMode
;
286 return wsi
->override_present_mode
;
290 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
293 for (unsigned i
= 0; i
< chain
->image_count
; i
++)
294 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
296 vk_free(&chain
->alloc
, chain
->fences
);
299 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
300 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
303 vk_free(&chain
->alloc
, chain
->cmd_pools
);
307 select_memory_type(const struct wsi_device
*wsi
,
308 VkMemoryPropertyFlags props
,
311 for (uint32_t i
= 0; i
< wsi
->memory_props
.memoryTypeCount
; i
++) {
312 const VkMemoryType type
= wsi
->memory_props
.memoryTypes
[i
];
313 if ((type_bits
& (1 << i
)) && (type
.propertyFlags
& props
) == props
)
317 unreachable("No memory type found");
321 vk_format_size(VkFormat format
)
324 case VK_FORMAT_B8G8R8A8_UNORM
:
325 case VK_FORMAT_B8G8R8A8_SRGB
:
328 unreachable("Unknown WSI Format");
332 static inline uint32_t
333 align_u32(uint32_t v
, uint32_t a
)
335 assert(a
!= 0 && a
== (a
& -a
));
336 return (v
+ a
- 1) & ~(a
- 1);
340 wsi_create_native_image(const struct wsi_swapchain
*chain
,
341 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
342 uint32_t num_modifier_lists
,
343 const uint32_t *num_modifiers
,
344 const uint64_t *const *modifiers
,
345 struct wsi_image
*image
)
347 const struct wsi_device
*wsi
= chain
->wsi
;
350 memset(image
, 0, sizeof(*image
));
351 for (int i
= 0; i
< ARRAY_SIZE(image
->fds
); i
++)
354 VkImageCreateInfo image_info
= {
355 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
357 .imageType
= VK_IMAGE_TYPE_2D
,
358 .format
= pCreateInfo
->imageFormat
,
360 .width
= pCreateInfo
->imageExtent
.width
,
361 .height
= pCreateInfo
->imageExtent
.height
,
366 .samples
= VK_SAMPLE_COUNT_1_BIT
,
367 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
368 .usage
= pCreateInfo
->imageUsage
,
369 .sharingMode
= pCreateInfo
->imageSharingMode
,
370 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
371 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
372 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
375 struct wsi_image_create_info image_wsi_info
;
376 VkImageDrmFormatModifierListCreateInfoEXT image_modifier_list
;
378 uint32_t image_modifier_count
= 0, modifier_prop_count
= 0;
379 struct VkDrmFormatModifierPropertiesEXT
*modifier_props
= NULL
;
380 uint64_t *image_modifiers
= NULL
;
381 if (num_modifier_lists
== 0) {
382 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
383 image_wsi_info
= (struct wsi_image_create_info
) {
384 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
387 __vk_append_struct(&image_info
, &image_wsi_info
);
389 /* The winsys can't request modifiers if we don't support them. */
390 assert(wsi
->supports_modifiers
);
391 struct VkDrmFormatModifierPropertiesListEXT modifier_props_list
= {
392 .sType
= VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT
,
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
.drmFormatModifierCount
> 0);
402 modifier_props
= vk_alloc(&chain
->alloc
,
403 sizeof(*modifier_props
) *
404 modifier_props_list
.drmFormatModifierCount
,
406 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
407 if (!modifier_props
) {
408 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
412 modifier_props_list
.pDrmFormatModifierProperties
= modifier_props
;
413 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
414 pCreateInfo
->imageFormat
,
416 modifier_prop_count
= modifier_props_list
.drmFormatModifierCount
;
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
].drmFormatModifier
== 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_modifier_list
= (VkImageDrmFormatModifierListCreateInfoEXT
) {
451 .sType
= VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT
,
452 .drmFormatModifierCount
= image_modifier_count
,
453 .pDrmFormatModifiers
= image_modifiers
,
455 image_info
.tiling
= VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT
;
456 __vk_append_struct(&image_info
, &image_modifier_list
);
458 /* TODO: Add a proper error here */
459 assert(!"Failed to find a supported modifier! This should never "
460 "happen because LINEAR should always be available");
461 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
466 result
= wsi
->CreateImage(chain
->device
, &image_info
,
467 &chain
->alloc
, &image
->image
);
468 if (result
!= VK_SUCCESS
)
471 VkMemoryRequirements reqs
;
472 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
474 const struct wsi_memory_allocate_info memory_wsi_info
= {
475 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
477 .implicit_sync
= true,
479 const VkExportMemoryAllocateInfo memory_export_info
= {
480 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO
,
481 .pNext
= &memory_wsi_info
,
482 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
484 const VkMemoryDedicatedAllocateInfo memory_dedicated_info
= {
485 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
486 .pNext
= &memory_export_info
,
487 .image
= image
->image
,
488 .buffer
= VK_NULL_HANDLE
,
490 const VkMemoryAllocateInfo memory_info
= {
491 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
492 .pNext
= &memory_dedicated_info
,
493 .allocationSize
= reqs
.size
,
494 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
495 reqs
.memoryTypeBits
),
497 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
498 &chain
->alloc
, &image
->memory
);
499 if (result
!= VK_SUCCESS
)
502 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
504 if (result
!= VK_SUCCESS
)
507 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
508 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
510 .memory
= image
->memory
,
511 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
514 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
515 if (result
!= VK_SUCCESS
)
518 if (num_modifier_lists
> 0) {
519 VkImageDrmFormatModifierPropertiesEXT image_mod_props
= {
520 .sType
= VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT
,
522 result
= wsi
->GetImageDrmFormatModifierPropertiesEXT(chain
->device
,
525 if (result
!= VK_SUCCESS
)
527 image
->drm_modifier
= image_mod_props
.drmFormatModifier
;
528 assert(image
->drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
530 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
531 if (modifier_props
[j
].drmFormatModifier
== image
->drm_modifier
) {
532 image
->num_planes
= modifier_props
[j
].drmFormatModifierPlaneCount
;
537 for (uint32_t p
= 0; p
< image
->num_planes
; p
++) {
538 const VkImageSubresource image_subresource
= {
539 .aspectMask
= VK_IMAGE_ASPECT_PLANE_0_BIT
<< p
,
543 VkSubresourceLayout image_layout
;
544 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
545 &image_subresource
, &image_layout
);
546 image
->sizes
[p
] = image_layout
.size
;
547 image
->row_pitches
[p
] = image_layout
.rowPitch
;
548 image
->offsets
[p
] = image_layout
.offset
;
552 image
->fds
[p
] = dup(fd
);
553 if (image
->fds
[p
] == -1) {
554 for (uint32_t i
= 0; i
< p
; i
++)
555 close(image
->fds
[p
]);
562 const VkImageSubresource image_subresource
= {
563 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
567 VkSubresourceLayout image_layout
;
568 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
569 &image_subresource
, &image_layout
);
571 image
->drm_modifier
= DRM_FORMAT_MOD_INVALID
;
572 image
->num_planes
= 1;
573 image
->sizes
[0] = reqs
.size
;
574 image
->row_pitches
[0] = image_layout
.rowPitch
;
575 image
->offsets
[0] = 0;
579 vk_free(&chain
->alloc
, modifier_props
);
580 vk_free(&chain
->alloc
, image_modifiers
);
585 vk_free(&chain
->alloc
, modifier_props
);
586 vk_free(&chain
->alloc
, image_modifiers
);
587 wsi_destroy_image(chain
, image
);
592 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
595 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
596 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
598 struct wsi_image
*image
)
600 const struct wsi_device
*wsi
= chain
->wsi
;
603 memset(image
, 0, sizeof(*image
));
605 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
606 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
607 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
609 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
610 linear_size
= align_u32(linear_size
, 4096);
612 const VkExternalMemoryBufferCreateInfo prime_buffer_external_info
= {
613 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO
,
615 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
617 const VkBufferCreateInfo prime_buffer_info
= {
618 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
619 .pNext
= &prime_buffer_external_info
,
621 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
622 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
624 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
625 &chain
->alloc
, &image
->prime
.buffer
);
626 if (result
!= VK_SUCCESS
)
629 VkMemoryRequirements reqs
;
630 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
631 assert(reqs
.size
<= linear_size
);
633 const struct wsi_memory_allocate_info memory_wsi_info
= {
634 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
636 .implicit_sync
= true,
638 const VkExportMemoryAllocateInfo prime_memory_export_info
= {
639 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO
,
640 .pNext
= &memory_wsi_info
,
641 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
643 const VkMemoryDedicatedAllocateInfo prime_memory_dedicated_info
= {
644 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
645 .pNext
= &prime_memory_export_info
,
646 .image
= VK_NULL_HANDLE
,
647 .buffer
= image
->prime
.buffer
,
649 const VkMemoryAllocateInfo prime_memory_info
= {
650 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
651 .pNext
= &prime_memory_dedicated_info
,
652 .allocationSize
= linear_size
,
653 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
655 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
656 &chain
->alloc
, &image
->prime
.memory
);
657 if (result
!= VK_SUCCESS
)
660 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
661 image
->prime
.memory
, 0);
662 if (result
!= VK_SUCCESS
)
665 const VkImageCreateInfo image_info
= {
666 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
669 .imageType
= VK_IMAGE_TYPE_2D
,
670 .format
= pCreateInfo
->imageFormat
,
672 .width
= pCreateInfo
->imageExtent
.width
,
673 .height
= pCreateInfo
->imageExtent
.height
,
678 .samples
= VK_SAMPLE_COUNT_1_BIT
,
679 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
680 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
681 .sharingMode
= pCreateInfo
->imageSharingMode
,
682 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
683 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
684 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
686 result
= wsi
->CreateImage(chain
->device
, &image_info
,
687 &chain
->alloc
, &image
->image
);
688 if (result
!= VK_SUCCESS
)
691 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
693 const VkMemoryDedicatedAllocateInfo memory_dedicated_info
= {
694 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
696 .image
= image
->image
,
697 .buffer
= VK_NULL_HANDLE
,
699 const VkMemoryAllocateInfo memory_info
= {
700 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
701 .pNext
= &memory_dedicated_info
,
702 .allocationSize
= reqs
.size
,
703 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
704 reqs
.memoryTypeBits
),
706 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
707 &chain
->alloc
, &image
->memory
);
708 if (result
!= VK_SUCCESS
)
711 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
713 if (result
!= VK_SUCCESS
)
716 image
->prime
.blit_cmd_buffers
=
717 vk_zalloc(&chain
->alloc
,
718 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
719 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
720 if (!image
->prime
.blit_cmd_buffers
) {
721 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
725 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
726 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
727 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
729 .commandPool
= chain
->cmd_pools
[i
],
730 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
731 .commandBufferCount
= 1,
733 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
734 &image
->prime
.blit_cmd_buffers
[i
]);
735 if (result
!= VK_SUCCESS
)
738 const VkCommandBufferBeginInfo begin_info
= {
739 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
741 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
743 struct VkBufferImageCopy buffer_image_copy
= {
745 .bufferRowLength
= linear_stride
/ cpp
,
746 .bufferImageHeight
= 0,
747 .imageSubresource
= {
748 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
753 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
755 .width
= pCreateInfo
->imageExtent
.width
,
756 .height
= pCreateInfo
->imageExtent
.height
,
760 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
762 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
764 1, &buffer_image_copy
);
766 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
767 if (result
!= VK_SUCCESS
)
771 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
772 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
774 .memory
= image
->prime
.memory
,
775 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
778 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
779 if (result
!= VK_SUCCESS
)
782 image
->drm_modifier
= use_modifier
? DRM_FORMAT_MOD_LINEAR
: DRM_FORMAT_MOD_INVALID
;
783 image
->num_planes
= 1;
784 image
->sizes
[0] = linear_size
;
785 image
->row_pitches
[0] = linear_stride
;
786 image
->offsets
[0] = 0;
792 wsi_destroy_image(chain
, image
);
798 wsi_destroy_image(const struct wsi_swapchain
*chain
,
799 struct wsi_image
*image
)
801 const struct wsi_device
*wsi
= chain
->wsi
;
803 if (image
->prime
.blit_cmd_buffers
) {
804 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
805 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
806 1, &image
->prime
.blit_cmd_buffers
[i
]);
808 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
811 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
812 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
813 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
814 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
818 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
819 uint32_t queueFamilyIndex
,
820 VkSurfaceKHR _surface
,
821 VkBool32
* pSupported
)
823 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
824 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
826 return iface
->get_support(surface
, wsi_device
,
827 queueFamilyIndex
, pSupported
);
831 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
832 VkSurfaceKHR _surface
,
833 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
835 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
836 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
838 VkSurfaceCapabilities2KHR caps2
= {
839 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
842 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
844 if (result
== VK_SUCCESS
)
845 *pSurfaceCapabilities
= caps2
.surfaceCapabilities
;
851 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
852 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
853 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
855 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
856 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
858 return iface
->get_capabilities2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
859 pSurfaceCapabilities
);
863 wsi_common_get_surface_capabilities2ext(
864 struct wsi_device
*wsi_device
,
865 VkSurfaceKHR _surface
,
866 VkSurfaceCapabilities2EXT
*pSurfaceCapabilities
)
868 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
869 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
871 assert(pSurfaceCapabilities
->sType
==
872 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT
);
874 struct wsi_surface_supported_counters counters
= {
875 .sType
= VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA
,
876 .pNext
= pSurfaceCapabilities
->pNext
,
877 .supported_surface_counters
= 0,
880 VkSurfaceCapabilities2KHR caps2
= {
881 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
885 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
887 if (result
== VK_SUCCESS
) {
888 VkSurfaceCapabilities2EXT
*ext_caps
= pSurfaceCapabilities
;
889 VkSurfaceCapabilitiesKHR khr_caps
= caps2
.surfaceCapabilities
;
891 ext_caps
->minImageCount
= khr_caps
.minImageCount
;
892 ext_caps
->maxImageCount
= khr_caps
.maxImageCount
;
893 ext_caps
->currentExtent
= khr_caps
.currentExtent
;
894 ext_caps
->minImageExtent
= khr_caps
.minImageExtent
;
895 ext_caps
->maxImageExtent
= khr_caps
.maxImageExtent
;
896 ext_caps
->maxImageArrayLayers
= khr_caps
.maxImageArrayLayers
;
897 ext_caps
->supportedTransforms
= khr_caps
.supportedTransforms
;
898 ext_caps
->currentTransform
= khr_caps
.currentTransform
;
899 ext_caps
->supportedCompositeAlpha
= khr_caps
.supportedCompositeAlpha
;
900 ext_caps
->supportedUsageFlags
= khr_caps
.supportedUsageFlags
;
901 ext_caps
->supportedSurfaceCounters
= counters
.supported_surface_counters
;
908 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
909 VkSurfaceKHR _surface
,
910 uint32_t *pSurfaceFormatCount
,
911 VkSurfaceFormatKHR
*pSurfaceFormats
)
913 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
914 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
916 return iface
->get_formats(surface
, wsi_device
,
917 pSurfaceFormatCount
, pSurfaceFormats
);
921 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
922 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
923 uint32_t *pSurfaceFormatCount
,
924 VkSurfaceFormat2KHR
*pSurfaceFormats
)
926 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
927 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
929 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
930 pSurfaceFormatCount
, pSurfaceFormats
);
934 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
935 VkSurfaceKHR _surface
,
936 uint32_t *pPresentModeCount
,
937 VkPresentModeKHR
*pPresentModes
)
939 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
940 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
942 return iface
->get_present_modes(surface
, pPresentModeCount
,
947 wsi_common_get_present_rectangles(struct wsi_device
*wsi_device
,
948 VkSurfaceKHR _surface
,
949 uint32_t* pRectCount
,
952 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
953 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
955 return iface
->get_present_rectangles(surface
, wsi_device
,
960 wsi_common_create_swapchain(struct wsi_device
*wsi
,
962 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
963 const VkAllocationCallbacks
*pAllocator
,
964 VkSwapchainKHR
*pSwapchain
)
966 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
967 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
968 struct wsi_swapchain
*swapchain
;
970 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
,
971 pCreateInfo
, pAllocator
,
973 if (result
!= VK_SUCCESS
)
976 swapchain
->fences
= vk_zalloc(pAllocator
,
977 sizeof (*swapchain
->fences
) * swapchain
->image_count
,
978 sizeof (*swapchain
->fences
),
979 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
980 if (!swapchain
->fences
) {
981 swapchain
->destroy(swapchain
, pAllocator
);
982 return VK_ERROR_OUT_OF_HOST_MEMORY
;
985 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
991 wsi_common_destroy_swapchain(VkDevice device
,
992 VkSwapchainKHR _swapchain
,
993 const VkAllocationCallbacks
*pAllocator
)
995 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
999 swapchain
->destroy(swapchain
, pAllocator
);
1003 wsi_common_get_images(VkSwapchainKHR _swapchain
,
1004 uint32_t *pSwapchainImageCount
,
1005 VkImage
*pSwapchainImages
)
1007 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
1008 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
1010 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
1011 vk_outarray_append(&images
, image
) {
1012 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
1016 return vk_outarray_status(&images
);
1020 wsi_common_acquire_next_image2(const struct wsi_device
*wsi
,
1022 const VkAcquireNextImageInfoKHR
*pAcquireInfo
,
1023 uint32_t *pImageIndex
)
1025 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pAcquireInfo
->swapchain
);
1027 VkResult result
= swapchain
->acquire_next_image(swapchain
, pAcquireInfo
,
1029 if (result
!= VK_SUCCESS
)
1032 if (pAcquireInfo
->semaphore
!= VK_NULL_HANDLE
&&
1033 wsi
->signal_semaphore_for_memory
!= NULL
) {
1034 struct wsi_image
*image
=
1035 swapchain
->get_wsi_image(swapchain
, *pImageIndex
);
1036 wsi
->signal_semaphore_for_memory(device
, pAcquireInfo
->semaphore
,
1040 if (pAcquireInfo
->fence
!= VK_NULL_HANDLE
&&
1041 wsi
->signal_fence_for_memory
!= NULL
) {
1042 struct wsi_image
*image
=
1043 swapchain
->get_wsi_image(swapchain
, *pImageIndex
);
1044 wsi
->signal_fence_for_memory(device
, pAcquireInfo
->fence
,
1052 wsi_common_queue_present(const struct wsi_device
*wsi
,
1055 int queue_family_index
,
1056 const VkPresentInfoKHR
*pPresentInfo
)
1058 VkResult final_result
= VK_SUCCESS
;
1060 const VkPresentRegionsKHR
*regions
=
1061 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
1063 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
1064 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
1065 uint32_t image_index
= pPresentInfo
->pImageIndices
[i
];
1068 if (swapchain
->fences
[image_index
] == VK_NULL_HANDLE
) {
1069 const VkFenceCreateInfo fence_info
= {
1070 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
1074 result
= wsi
->CreateFence(device
, &fence_info
,
1076 &swapchain
->fences
[image_index
]);
1077 if (result
!= VK_SUCCESS
)
1081 wsi
->WaitForFences(device
, 1, &swapchain
->fences
[image_index
],
1083 if (result
!= VK_SUCCESS
)
1087 wsi
->ResetFences(device
, 1, &swapchain
->fences
[image_index
]);
1088 if (result
!= VK_SUCCESS
)
1092 struct wsi_image
*image
=
1093 swapchain
->get_wsi_image(swapchain
, image_index
);
1095 struct wsi_memory_signal_submit_info mem_signal
= {
1096 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA
,
1098 .memory
= image
->memory
,
1101 VkSubmitInfo submit_info
= {
1102 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
1103 .pNext
= &mem_signal
,
1106 VkPipelineStageFlags
*stage_flags
= NULL
;
1108 /* We only need/want to wait on semaphores once. After that, we're
1109 * guaranteed ordering since it all happens on the same queue.
1111 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
;
1112 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
;
1114 /* Set up the pWaitDstStageMasks */
1115 stage_flags
= vk_alloc(&swapchain
->alloc
,
1116 sizeof(VkPipelineStageFlags
) *
1117 pPresentInfo
->waitSemaphoreCount
,
1119 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1121 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1124 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
1125 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
1127 submit_info
.pWaitDstStageMask
= stage_flags
;
1130 if (swapchain
->use_prime_blit
) {
1131 /* If we are using prime blits, we need to perform the blit now. The
1132 * command buffer is attached to the image.
1134 submit_info
.commandBufferCount
= 1;
1135 submit_info
.pCommandBuffers
=
1136 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
1137 mem_signal
.memory
= image
->prime
.memory
;
1140 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[image_index
]);
1141 vk_free(&swapchain
->alloc
, stage_flags
);
1142 if (result
!= VK_SUCCESS
)
1145 const VkPresentRegionKHR
*region
= NULL
;
1146 if (regions
&& regions
->pRegions
)
1147 region
= ®ions
->pRegions
[i
];
1149 result
= swapchain
->queue_present(swapchain
, image_index
, region
);
1150 if (result
!= VK_SUCCESS
)
1154 if (pPresentInfo
->pResults
!= NULL
)
1155 pPresentInfo
->pResults
[i
] = result
;
1157 /* Let the final result be our first unsuccessful result */
1158 if (final_result
== VK_SUCCESS
)
1159 final_result
= result
;
1162 return final_result
;
1166 wsi_common_get_current_time(void)
1168 struct timespec current
;
1169 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
1170 return current
.tv_nsec
+ current
.tv_sec
* 1000000000ull;