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_fourcc.h"
26 #include "util/macros.h"
33 wsi_device_init(struct wsi_device
*wsi
,
34 VkPhysicalDevice pdevice
,
35 WSI_FN_GetPhysicalDeviceProcAddr proc_addr
,
36 const VkAllocationCallbacks
*alloc
,
41 memset(wsi
, 0, sizeof(*wsi
));
43 wsi
->instance_alloc
= *alloc
;
44 wsi
->pdevice
= pdevice
;
46 #define WSI_GET_CB(func) \
47 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
48 WSI_GET_CB(GetPhysicalDeviceProperties2
);
49 WSI_GET_CB(GetPhysicalDeviceMemoryProperties
);
50 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties
);
53 wsi
->pci_bus_info
.sType
=
54 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
;
55 VkPhysicalDeviceProperties2 pdp2
= {
56 .sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
,
57 .pNext
= &wsi
->pci_bus_info
,
59 GetPhysicalDeviceProperties2(pdevice
, &pdp2
);
61 GetPhysicalDeviceMemoryProperties(pdevice
, &wsi
->memory_props
);
62 GetPhysicalDeviceQueueFamilyProperties(pdevice
, &wsi
->queue_family_count
, NULL
);
64 #define WSI_GET_CB(func) \
65 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
66 WSI_GET_CB(AllocateMemory
);
67 WSI_GET_CB(AllocateCommandBuffers
);
68 WSI_GET_CB(BindBufferMemory
);
69 WSI_GET_CB(BindImageMemory
);
70 WSI_GET_CB(BeginCommandBuffer
);
71 WSI_GET_CB(CmdCopyImageToBuffer
);
72 WSI_GET_CB(CreateBuffer
);
73 WSI_GET_CB(CreateCommandPool
);
74 WSI_GET_CB(CreateFence
);
75 WSI_GET_CB(CreateImage
);
76 WSI_GET_CB(DestroyBuffer
);
77 WSI_GET_CB(DestroyCommandPool
);
78 WSI_GET_CB(DestroyFence
);
79 WSI_GET_CB(DestroyImage
);
80 WSI_GET_CB(EndCommandBuffer
);
81 WSI_GET_CB(FreeMemory
);
82 WSI_GET_CB(FreeCommandBuffers
);
83 WSI_GET_CB(GetBufferMemoryRequirements
);
84 WSI_GET_CB(GetImageMemoryRequirements
);
85 WSI_GET_CB(GetImageSubresourceLayout
);
86 WSI_GET_CB(GetMemoryFdKHR
);
87 WSI_GET_CB(GetPhysicalDeviceFormatProperties
);
88 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR
);
89 WSI_GET_CB(ResetFences
);
90 WSI_GET_CB(QueueSubmit
);
91 WSI_GET_CB(WaitForFences
);
94 #ifdef VK_USE_PLATFORM_XCB_KHR
95 result
= wsi_x11_init_wsi(wsi
, alloc
);
96 if (result
!= VK_SUCCESS
)
100 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
101 result
= wsi_wl_init_wsi(wsi
, alloc
, pdevice
);
102 if (result
!= VK_SUCCESS
)
106 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
107 result
= wsi_display_init_wsi(wsi
, alloc
, display_fd
);
108 if (result
!= VK_SUCCESS
)
115 wsi_device_finish(wsi
, alloc
);
120 wsi_device_finish(struct wsi_device
*wsi
,
121 const VkAllocationCallbacks
*alloc
)
123 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
124 wsi_display_finish_wsi(wsi
, alloc
);
126 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
127 wsi_wl_finish_wsi(wsi
, alloc
);
129 #ifdef VK_USE_PLATFORM_XCB_KHR
130 wsi_x11_finish_wsi(wsi
, alloc
);
135 wsi_device_matches_drm_fd(const struct wsi_device
*wsi
, int drm_fd
)
137 drmDevicePtr fd_device
;
138 int ret
= drmGetDevice2(drm_fd
, 0, &fd_device
);
143 switch (fd_device
->bustype
) {
145 match
= wsi
->pci_bus_info
.pciDomain
== fd_device
->businfo
.pci
->domain
&&
146 wsi
->pci_bus_info
.pciBus
== fd_device
->businfo
.pci
->bus
&&
147 wsi
->pci_bus_info
.pciDevice
== fd_device
->businfo
.pci
->dev
&&
148 wsi
->pci_bus_info
.pciFunction
== fd_device
->businfo
.pci
->func
;
155 drmFreeDevice(&fd_device
);
161 wsi_swapchain_init(const struct wsi_device
*wsi
,
162 struct wsi_swapchain
*chain
,
164 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
165 const VkAllocationCallbacks
*pAllocator
)
169 memset(chain
, 0, sizeof(*chain
));
172 chain
->device
= device
;
173 chain
->alloc
= *pAllocator
;
174 chain
->use_prime_blit
= false;
177 vk_zalloc(pAllocator
, sizeof(VkCommandPool
) * wsi
->queue_family_count
, 8,
178 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
179 if (!chain
->cmd_pools
)
180 return VK_ERROR_OUT_OF_HOST_MEMORY
;
182 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
183 const VkCommandPoolCreateInfo cmd_pool_info
= {
184 .sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
,
187 .queueFamilyIndex
= i
,
189 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
190 &chain
->cmd_pools
[i
]);
191 if (result
!= VK_SUCCESS
)
198 wsi_swapchain_finish(chain
);
203 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
205 for (unsigned i
= 0; i
< ARRAY_SIZE(chain
->fences
); i
++)
206 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
208 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
209 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
212 vk_free(&chain
->alloc
, chain
->cmd_pools
);
216 select_memory_type(const struct wsi_device
*wsi
,
217 VkMemoryPropertyFlags props
,
220 for (uint32_t i
= 0; i
< wsi
->memory_props
.memoryTypeCount
; i
++) {
221 const VkMemoryType type
= wsi
->memory_props
.memoryTypes
[i
];
222 if ((type_bits
& (1 << i
)) && (type
.propertyFlags
& props
) == props
)
226 unreachable("No memory type found");
230 vk_format_size(VkFormat format
)
233 case VK_FORMAT_B8G8R8A8_UNORM
:
234 case VK_FORMAT_B8G8R8A8_SRGB
:
237 unreachable("Unknown WSI Format");
241 static inline uint32_t
242 align_u32(uint32_t v
, uint32_t a
)
244 assert(a
!= 0 && a
== (a
& -a
));
245 return (v
+ a
- 1) & ~(a
- 1);
249 wsi_create_native_image(const struct wsi_swapchain
*chain
,
250 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
251 uint32_t num_modifier_lists
,
252 const uint32_t *num_modifiers
,
253 const uint64_t *const *modifiers
,
254 struct wsi_image
*image
)
256 const struct wsi_device
*wsi
= chain
->wsi
;
259 memset(image
, 0, sizeof(*image
));
260 for (int i
= 0; i
< ARRAY_SIZE(image
->fds
); i
++)
263 struct wsi_image_create_info image_wsi_info
= {
264 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
268 uint32_t image_modifier_count
= 0, modifier_prop_count
= 0;
269 struct wsi_format_modifier_properties
*modifier_props
= NULL
;
270 uint64_t *image_modifiers
= NULL
;
271 if (num_modifier_lists
== 0) {
272 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
273 image_wsi_info
.scanout
= true;
275 /* The winsys can't request modifiers if we don't support them. */
276 assert(wsi
->supports_modifiers
);
277 struct wsi_format_modifier_properties_list modifier_props_list
= {
278 .sType
= VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA
,
281 VkFormatProperties2KHR format_props
= {
282 .sType
= VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2_KHR
,
283 .pNext
= &modifier_props_list
,
285 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
286 pCreateInfo
->imageFormat
,
288 assert(modifier_props_list
.modifier_count
> 0);
289 modifier_props
= vk_alloc(&chain
->alloc
,
290 sizeof(*modifier_props
) *
291 modifier_props_list
.modifier_count
,
293 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
294 if (!modifier_props
) {
295 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
299 modifier_props_list
.modifier_properties
= modifier_props
;
300 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
301 pCreateInfo
->imageFormat
,
303 modifier_prop_count
= modifier_props_list
.modifier_count
;
305 uint32_t max_modifier_count
= 0;
306 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++)
307 max_modifier_count
= MAX2(max_modifier_count
, num_modifiers
[l
]);
309 image_modifiers
= vk_alloc(&chain
->alloc
,
310 sizeof(*image_modifiers
) *
313 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
314 if (!image_modifiers
) {
315 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
319 image_modifier_count
= 0;
320 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++) {
321 /* Walk the modifier lists and construct a list of supported
324 for (uint32_t i
= 0; i
< num_modifiers
[l
]; i
++) {
325 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
326 if (modifier_props
[j
].modifier
== modifiers
[l
][i
])
327 image_modifiers
[image_modifier_count
++] = modifiers
[l
][i
];
331 /* We only want to take the modifiers from the first list */
332 if (image_modifier_count
> 0)
336 if (image_modifier_count
> 0) {
337 image_wsi_info
.modifier_count
= image_modifier_count
;
338 image_wsi_info
.modifiers
= image_modifiers
;
340 /* TODO: Add a proper error here */
341 assert(!"Failed to find a supported modifier! This should never "
342 "happen because LINEAR should always be available");
343 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
348 const VkImageCreateInfo image_info
= {
349 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
350 .pNext
= &image_wsi_info
,
352 .imageType
= VK_IMAGE_TYPE_2D
,
353 .format
= pCreateInfo
->imageFormat
,
355 .width
= pCreateInfo
->imageExtent
.width
,
356 .height
= pCreateInfo
->imageExtent
.height
,
361 .samples
= VK_SAMPLE_COUNT_1_BIT
,
362 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
363 .usage
= pCreateInfo
->imageUsage
,
364 .sharingMode
= pCreateInfo
->imageSharingMode
,
365 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
366 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
367 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
369 result
= wsi
->CreateImage(chain
->device
, &image_info
,
370 &chain
->alloc
, &image
->image
);
371 if (result
!= VK_SUCCESS
)
374 VkMemoryRequirements reqs
;
375 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
377 const struct wsi_memory_allocate_info memory_wsi_info
= {
378 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
380 .implicit_sync
= true,
382 const VkExportMemoryAllocateInfoKHR memory_export_info
= {
383 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
384 .pNext
= &memory_wsi_info
,
385 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
387 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
388 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
389 .pNext
= &memory_export_info
,
390 .image
= image
->image
,
391 .buffer
= VK_NULL_HANDLE
,
393 const VkMemoryAllocateInfo memory_info
= {
394 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
395 .pNext
= &memory_dedicated_info
,
396 .allocationSize
= reqs
.size
,
397 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
398 reqs
.memoryTypeBits
),
400 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
401 &chain
->alloc
, &image
->memory
);
402 if (result
!= VK_SUCCESS
)
405 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
407 if (result
!= VK_SUCCESS
)
410 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
411 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
413 .memory
= image
->memory
,
414 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
417 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
418 if (result
!= VK_SUCCESS
)
421 if (num_modifier_lists
> 0) {
422 image
->drm_modifier
= wsi
->image_get_modifier(image
->image
);
423 assert(image
->drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
425 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
426 if (modifier_props
[j
].modifier
== image
->drm_modifier
) {
427 image
->num_planes
= modifier_props
[j
].modifier_plane_count
;
432 for (uint32_t p
= 0; p
< image
->num_planes
; p
++) {
433 const VkImageSubresource image_subresource
= {
434 .aspectMask
= VK_IMAGE_ASPECT_PLANE_0_BIT_KHR
<< p
,
438 VkSubresourceLayout image_layout
;
439 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
440 &image_subresource
, &image_layout
);
441 image
->sizes
[p
] = image_layout
.size
;
442 image
->row_pitches
[p
] = image_layout
.rowPitch
;
443 image
->offsets
[p
] = image_layout
.offset
;
447 image
->fds
[p
] = dup(fd
);
448 if (image
->fds
[p
] == -1) {
449 for (uint32_t i
= 0; i
< p
; i
++)
450 close(image
->fds
[p
]);
457 const VkImageSubresource image_subresource
= {
458 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
462 VkSubresourceLayout image_layout
;
463 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
464 &image_subresource
, &image_layout
);
466 image
->drm_modifier
= DRM_FORMAT_MOD_INVALID
;
467 image
->num_planes
= 1;
468 image
->sizes
[0] = reqs
.size
;
469 image
->row_pitches
[0] = image_layout
.rowPitch
;
470 image
->offsets
[0] = 0;
474 vk_free(&chain
->alloc
, modifier_props
);
475 vk_free(&chain
->alloc
, image_modifiers
);
480 vk_free(&chain
->alloc
, modifier_props
);
481 vk_free(&chain
->alloc
, image_modifiers
);
482 wsi_destroy_image(chain
, image
);
487 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
490 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
491 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
493 struct wsi_image
*image
)
495 const struct wsi_device
*wsi
= chain
->wsi
;
498 memset(image
, 0, sizeof(*image
));
500 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
501 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
502 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
504 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
505 linear_size
= align_u32(linear_size
, 4096);
507 const VkExternalMemoryBufferCreateInfoKHR prime_buffer_external_info
= {
508 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR
,
510 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
512 const VkBufferCreateInfo prime_buffer_info
= {
513 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
514 .pNext
= &prime_buffer_external_info
,
516 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
517 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
519 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
520 &chain
->alloc
, &image
->prime
.buffer
);
521 if (result
!= VK_SUCCESS
)
524 VkMemoryRequirements reqs
;
525 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
526 assert(reqs
.size
<= linear_size
);
528 const struct wsi_memory_allocate_info memory_wsi_info
= {
529 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
531 .implicit_sync
= true,
533 const VkExportMemoryAllocateInfoKHR prime_memory_export_info
= {
534 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
535 .pNext
= &memory_wsi_info
,
536 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
538 const VkMemoryDedicatedAllocateInfoKHR prime_memory_dedicated_info
= {
539 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
540 .pNext
= &prime_memory_export_info
,
541 .image
= VK_NULL_HANDLE
,
542 .buffer
= image
->prime
.buffer
,
544 const VkMemoryAllocateInfo prime_memory_info
= {
545 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
546 .pNext
= &prime_memory_dedicated_info
,
547 .allocationSize
= linear_size
,
548 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
550 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
551 &chain
->alloc
, &image
->prime
.memory
);
552 if (result
!= VK_SUCCESS
)
555 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
556 image
->prime
.memory
, 0);
557 if (result
!= VK_SUCCESS
)
560 const VkImageCreateInfo image_info
= {
561 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
564 .imageType
= VK_IMAGE_TYPE_2D
,
565 .format
= pCreateInfo
->imageFormat
,
567 .width
= pCreateInfo
->imageExtent
.width
,
568 .height
= pCreateInfo
->imageExtent
.height
,
573 .samples
= VK_SAMPLE_COUNT_1_BIT
,
574 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
575 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
576 .sharingMode
= pCreateInfo
->imageSharingMode
,
577 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
578 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
579 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
581 result
= wsi
->CreateImage(chain
->device
, &image_info
,
582 &chain
->alloc
, &image
->image
);
583 if (result
!= VK_SUCCESS
)
586 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
588 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
589 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
591 .image
= image
->image
,
592 .buffer
= VK_NULL_HANDLE
,
594 const VkMemoryAllocateInfo memory_info
= {
595 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
596 .pNext
= &memory_dedicated_info
,
597 .allocationSize
= reqs
.size
,
598 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
599 reqs
.memoryTypeBits
),
601 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
602 &chain
->alloc
, &image
->memory
);
603 if (result
!= VK_SUCCESS
)
606 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
608 if (result
!= VK_SUCCESS
)
611 image
->prime
.blit_cmd_buffers
=
612 vk_zalloc(&chain
->alloc
,
613 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
614 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
615 if (!image
->prime
.blit_cmd_buffers
) {
616 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
620 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
621 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
622 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
624 .commandPool
= chain
->cmd_pools
[i
],
625 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
626 .commandBufferCount
= 1,
628 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
629 &image
->prime
.blit_cmd_buffers
[i
]);
630 if (result
!= VK_SUCCESS
)
633 const VkCommandBufferBeginInfo begin_info
= {
634 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
636 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
638 struct VkBufferImageCopy buffer_image_copy
= {
640 .bufferRowLength
= linear_stride
/ cpp
,
641 .bufferImageHeight
= 0,
642 .imageSubresource
= {
643 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
648 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
650 .width
= pCreateInfo
->imageExtent
.width
,
651 .height
= pCreateInfo
->imageExtent
.height
,
655 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
657 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
659 1, &buffer_image_copy
);
661 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
662 if (result
!= VK_SUCCESS
)
666 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
667 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
669 .memory
= image
->prime
.memory
,
670 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
673 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
674 if (result
!= VK_SUCCESS
)
677 image
->drm_modifier
= use_modifier
? DRM_FORMAT_MOD_LINEAR
: DRM_FORMAT_MOD_INVALID
;
678 image
->num_planes
= 1;
679 image
->sizes
[0] = linear_size
;
680 image
->row_pitches
[0] = linear_stride
;
681 image
->offsets
[0] = 0;
687 wsi_destroy_image(chain
, image
);
693 wsi_destroy_image(const struct wsi_swapchain
*chain
,
694 struct wsi_image
*image
)
696 const struct wsi_device
*wsi
= chain
->wsi
;
698 if (image
->prime
.blit_cmd_buffers
) {
699 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
700 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
701 1, &image
->prime
.blit_cmd_buffers
[i
]);
703 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
706 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
707 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
708 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
709 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
713 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
714 uint32_t queueFamilyIndex
,
715 VkSurfaceKHR _surface
,
716 VkBool32
* pSupported
)
718 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
719 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
721 return iface
->get_support(surface
, wsi_device
,
722 queueFamilyIndex
, pSupported
);
726 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
727 VkSurfaceKHR _surface
,
728 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
730 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
731 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
733 VkSurfaceCapabilities2KHR caps2
= {
734 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
737 VkResult result
= iface
->get_capabilities2(surface
, NULL
, &caps2
);
739 if (result
== VK_SUCCESS
)
740 *pSurfaceCapabilities
= caps2
.surfaceCapabilities
;
746 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
747 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
748 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
750 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
751 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
753 return iface
->get_capabilities2(surface
, pSurfaceInfo
->pNext
,
754 pSurfaceCapabilities
);
758 wsi_common_get_surface_capabilities2ext(
759 struct wsi_device
*wsi_device
,
760 VkSurfaceKHR _surface
,
761 VkSurfaceCapabilities2EXT
*pSurfaceCapabilities
)
763 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
764 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
766 assert(pSurfaceCapabilities
->sType
==
767 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT
);
769 struct wsi_surface_supported_counters counters
= {
770 .sType
= VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA
,
771 .pNext
= pSurfaceCapabilities
->pNext
,
772 .supported_surface_counters
= 0,
775 VkSurfaceCapabilities2KHR caps2
= {
776 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
780 VkResult result
= iface
->get_capabilities2(surface
, NULL
, &caps2
);
782 if (result
== VK_SUCCESS
) {
783 VkSurfaceCapabilities2EXT
*ext_caps
= pSurfaceCapabilities
;
784 VkSurfaceCapabilitiesKHR khr_caps
= caps2
.surfaceCapabilities
;
786 ext_caps
->minImageCount
= khr_caps
.minImageCount
;
787 ext_caps
->maxImageCount
= khr_caps
.maxImageCount
;
788 ext_caps
->currentExtent
= khr_caps
.currentExtent
;
789 ext_caps
->minImageExtent
= khr_caps
.minImageExtent
;
790 ext_caps
->maxImageExtent
= khr_caps
.maxImageExtent
;
791 ext_caps
->maxImageArrayLayers
= khr_caps
.maxImageArrayLayers
;
792 ext_caps
->supportedTransforms
= khr_caps
.supportedTransforms
;
793 ext_caps
->currentTransform
= khr_caps
.currentTransform
;
794 ext_caps
->supportedCompositeAlpha
= khr_caps
.supportedCompositeAlpha
;
795 ext_caps
->supportedUsageFlags
= khr_caps
.supportedUsageFlags
;
796 ext_caps
->supportedSurfaceCounters
= counters
.supported_surface_counters
;
803 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
804 VkSurfaceKHR _surface
,
805 uint32_t *pSurfaceFormatCount
,
806 VkSurfaceFormatKHR
*pSurfaceFormats
)
808 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
809 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
811 return iface
->get_formats(surface
, wsi_device
,
812 pSurfaceFormatCount
, pSurfaceFormats
);
816 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
817 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
818 uint32_t *pSurfaceFormatCount
,
819 VkSurfaceFormat2KHR
*pSurfaceFormats
)
821 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
822 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
824 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
825 pSurfaceFormatCount
, pSurfaceFormats
);
829 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
830 VkSurfaceKHR _surface
,
831 uint32_t *pPresentModeCount
,
832 VkPresentModeKHR
*pPresentModes
)
834 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
835 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
837 return iface
->get_present_modes(surface
, pPresentModeCount
,
842 wsi_common_get_present_rectangles(struct wsi_device
*wsi_device
,
843 VkSurfaceKHR _surface
,
844 uint32_t* pRectCount
,
847 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
848 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
850 return iface
->get_present_rectangles(surface
, wsi_device
,
855 wsi_common_create_swapchain(struct wsi_device
*wsi
,
857 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
858 const VkAllocationCallbacks
*pAllocator
,
859 VkSwapchainKHR
*pSwapchain
)
861 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
862 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
863 struct wsi_swapchain
*swapchain
;
865 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
,
866 pCreateInfo
, pAllocator
,
868 if (result
!= VK_SUCCESS
)
871 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
877 wsi_common_destroy_swapchain(VkDevice device
,
878 VkSwapchainKHR _swapchain
,
879 const VkAllocationCallbacks
*pAllocator
)
881 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
885 swapchain
->destroy(swapchain
, pAllocator
);
889 wsi_common_get_images(VkSwapchainKHR _swapchain
,
890 uint32_t *pSwapchainImageCount
,
891 VkImage
*pSwapchainImages
)
893 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
894 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
896 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
897 vk_outarray_append(&images
, image
) {
898 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
902 return vk_outarray_status(&images
);
906 wsi_common_acquire_next_image2(const struct wsi_device
*wsi
,
908 const VkAcquireNextImageInfoKHR
*pAcquireInfo
,
909 uint32_t *pImageIndex
)
911 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pAcquireInfo
->swapchain
);
913 return swapchain
->acquire_next_image(swapchain
, pAcquireInfo
, pImageIndex
);
917 wsi_common_queue_present(const struct wsi_device
*wsi
,
920 int queue_family_index
,
921 const VkPresentInfoKHR
*pPresentInfo
)
923 VkResult final_result
= VK_SUCCESS
;
925 const VkPresentRegionsKHR
*regions
=
926 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
928 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
929 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
932 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
933 const VkFenceCreateInfo fence_info
= {
934 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
938 result
= wsi
->CreateFence(device
, &fence_info
,
940 &swapchain
->fences
[0]);
941 if (result
!= VK_SUCCESS
)
944 wsi
->ResetFences(device
, 1, &swapchain
->fences
[0]);
947 VkSubmitInfo submit_info
= {
948 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
952 VkPipelineStageFlags
*stage_flags
= NULL
;
954 /* We only need/want to wait on semaphores once. After that, we're
955 * guaranteed ordering since it all happens on the same queue.
957 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
;
958 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
;
960 /* Set up the pWaitDstStageMasks */
961 stage_flags
= vk_alloc(&swapchain
->alloc
,
962 sizeof(VkPipelineStageFlags
) *
963 pPresentInfo
->waitSemaphoreCount
,
965 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
967 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
970 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
971 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
973 submit_info
.pWaitDstStageMask
= stage_flags
;
976 if (swapchain
->use_prime_blit
) {
977 /* If we are using prime blits, we need to perform the blit now. The
978 * command buffer is attached to the image.
980 struct wsi_image
*image
=
981 swapchain
->get_wsi_image(swapchain
, pPresentInfo
->pImageIndices
[i
]);
982 submit_info
.commandBufferCount
= 1;
983 submit_info
.pCommandBuffers
=
984 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
987 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[0]);
988 vk_free(&swapchain
->alloc
, stage_flags
);
989 if (result
!= VK_SUCCESS
)
992 const VkPresentRegionKHR
*region
= NULL
;
993 if (regions
&& regions
->pRegions
)
994 region
= ®ions
->pRegions
[i
];
996 result
= swapchain
->queue_present(swapchain
,
997 pPresentInfo
->pImageIndices
[i
],
999 if (result
!= VK_SUCCESS
)
1002 VkFence last
= swapchain
->fences
[2];
1003 swapchain
->fences
[2] = swapchain
->fences
[1];
1004 swapchain
->fences
[1] = swapchain
->fences
[0];
1005 swapchain
->fences
[0] = last
;
1007 if (last
!= VK_NULL_HANDLE
) {
1008 wsi
->WaitForFences(device
, 1, &last
, true, 1);
1012 if (pPresentInfo
->pResults
!= NULL
)
1013 pPresentInfo
->pResults
[i
] = result
;
1015 /* Let the final result be our first unsuccessful result */
1016 if (final_result
== VK_SUCCESS
)
1017 final_result
= result
;
1020 return final_result
;