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"
34 wsi_device_init(struct wsi_device
*wsi
,
35 VkPhysicalDevice pdevice
,
36 WSI_FN_GetPhysicalDeviceProcAddr proc_addr
,
37 const VkAllocationCallbacks
*alloc
,
42 memset(wsi
, 0, sizeof(*wsi
));
44 wsi
->instance_alloc
= *alloc
;
45 wsi
->pdevice
= pdevice
;
47 #define WSI_GET_CB(func) \
48 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
49 WSI_GET_CB(GetPhysicalDeviceProperties2
);
50 WSI_GET_CB(GetPhysicalDeviceMemoryProperties
);
51 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties
);
54 wsi
->pci_bus_info
.sType
=
55 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
;
56 VkPhysicalDeviceProperties2 pdp2
= {
57 .sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
,
58 .pNext
= &wsi
->pci_bus_info
,
60 GetPhysicalDeviceProperties2(pdevice
, &pdp2
);
62 wsi
->maxImageDimension2D
= pdp2
.properties
.limits
.maxImageDimension2D
;
64 GetPhysicalDeviceMemoryProperties(pdevice
, &wsi
->memory_props
);
65 GetPhysicalDeviceQueueFamilyProperties(pdevice
, &wsi
->queue_family_count
, NULL
);
67 #define WSI_GET_CB(func) \
68 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
69 WSI_GET_CB(AllocateMemory
);
70 WSI_GET_CB(AllocateCommandBuffers
);
71 WSI_GET_CB(BindBufferMemory
);
72 WSI_GET_CB(BindImageMemory
);
73 WSI_GET_CB(BeginCommandBuffer
);
74 WSI_GET_CB(CmdCopyImageToBuffer
);
75 WSI_GET_CB(CreateBuffer
);
76 WSI_GET_CB(CreateCommandPool
);
77 WSI_GET_CB(CreateFence
);
78 WSI_GET_CB(CreateImage
);
79 WSI_GET_CB(DestroyBuffer
);
80 WSI_GET_CB(DestroyCommandPool
);
81 WSI_GET_CB(DestroyFence
);
82 WSI_GET_CB(DestroyImage
);
83 WSI_GET_CB(EndCommandBuffer
);
84 WSI_GET_CB(FreeMemory
);
85 WSI_GET_CB(FreeCommandBuffers
);
86 WSI_GET_CB(GetBufferMemoryRequirements
);
87 WSI_GET_CB(GetImageMemoryRequirements
);
88 WSI_GET_CB(GetImageSubresourceLayout
);
89 WSI_GET_CB(GetMemoryFdKHR
);
90 WSI_GET_CB(GetPhysicalDeviceFormatProperties
);
91 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR
);
92 WSI_GET_CB(ResetFences
);
93 WSI_GET_CB(QueueSubmit
);
94 WSI_GET_CB(WaitForFences
);
97 #ifdef VK_USE_PLATFORM_XCB_KHR
98 result
= wsi_x11_init_wsi(wsi
, alloc
);
99 if (result
!= VK_SUCCESS
)
103 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
104 result
= wsi_wl_init_wsi(wsi
, alloc
, pdevice
);
105 if (result
!= VK_SUCCESS
)
109 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
110 result
= wsi_display_init_wsi(wsi
, alloc
, display_fd
);
111 if (result
!= VK_SUCCESS
)
118 wsi_device_finish(wsi
, alloc
);
123 wsi_device_finish(struct wsi_device
*wsi
,
124 const VkAllocationCallbacks
*alloc
)
126 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
127 wsi_display_finish_wsi(wsi
, alloc
);
129 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
130 wsi_wl_finish_wsi(wsi
, alloc
);
132 #ifdef VK_USE_PLATFORM_XCB_KHR
133 wsi_x11_finish_wsi(wsi
, alloc
);
138 wsi_device_matches_drm_fd(const struct wsi_device
*wsi
, int drm_fd
)
140 drmDevicePtr fd_device
;
141 int ret
= drmGetDevice2(drm_fd
, 0, &fd_device
);
146 switch (fd_device
->bustype
) {
148 match
= wsi
->pci_bus_info
.pciDomain
== fd_device
->businfo
.pci
->domain
&&
149 wsi
->pci_bus_info
.pciBus
== fd_device
->businfo
.pci
->bus
&&
150 wsi
->pci_bus_info
.pciDevice
== fd_device
->businfo
.pci
->dev
&&
151 wsi
->pci_bus_info
.pciFunction
== fd_device
->businfo
.pci
->func
;
158 drmFreeDevice(&fd_device
);
164 wsi_swapchain_init(const struct wsi_device
*wsi
,
165 struct wsi_swapchain
*chain
,
167 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
168 const VkAllocationCallbacks
*pAllocator
)
172 memset(chain
, 0, sizeof(*chain
));
175 chain
->device
= device
;
176 chain
->alloc
= *pAllocator
;
177 chain
->use_prime_blit
= false;
180 vk_zalloc(pAllocator
, sizeof(VkCommandPool
) * wsi
->queue_family_count
, 8,
181 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
182 if (!chain
->cmd_pools
)
183 return VK_ERROR_OUT_OF_HOST_MEMORY
;
185 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
186 const VkCommandPoolCreateInfo cmd_pool_info
= {
187 .sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
,
190 .queueFamilyIndex
= i
,
192 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
193 &chain
->cmd_pools
[i
]);
194 if (result
!= VK_SUCCESS
)
201 wsi_swapchain_finish(chain
);
206 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
208 for (unsigned i
= 0; i
< ARRAY_SIZE(chain
->fences
); i
++)
209 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
211 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
212 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
215 vk_free(&chain
->alloc
, chain
->cmd_pools
);
219 select_memory_type(const struct wsi_device
*wsi
,
220 VkMemoryPropertyFlags props
,
223 for (uint32_t i
= 0; i
< wsi
->memory_props
.memoryTypeCount
; i
++) {
224 const VkMemoryType type
= wsi
->memory_props
.memoryTypes
[i
];
225 if ((type_bits
& (1 << i
)) && (type
.propertyFlags
& props
) == props
)
229 unreachable("No memory type found");
233 vk_format_size(VkFormat format
)
236 case VK_FORMAT_B8G8R8A8_UNORM
:
237 case VK_FORMAT_B8G8R8A8_SRGB
:
240 unreachable("Unknown WSI Format");
244 static inline uint32_t
245 align_u32(uint32_t v
, uint32_t a
)
247 assert(a
!= 0 && a
== (a
& -a
));
248 return (v
+ a
- 1) & ~(a
- 1);
252 wsi_create_native_image(const struct wsi_swapchain
*chain
,
253 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
254 uint32_t num_modifier_lists
,
255 const uint32_t *num_modifiers
,
256 const uint64_t *const *modifiers
,
257 struct wsi_image
*image
)
259 const struct wsi_device
*wsi
= chain
->wsi
;
262 memset(image
, 0, sizeof(*image
));
263 for (int i
= 0; i
< ARRAY_SIZE(image
->fds
); i
++)
266 struct wsi_image_create_info image_wsi_info
= {
267 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
271 uint32_t image_modifier_count
= 0, modifier_prop_count
= 0;
272 struct wsi_format_modifier_properties
*modifier_props
= NULL
;
273 uint64_t *image_modifiers
= NULL
;
274 if (num_modifier_lists
== 0) {
275 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
276 image_wsi_info
.scanout
= true;
278 /* The winsys can't request modifiers if we don't support them. */
279 assert(wsi
->supports_modifiers
);
280 struct wsi_format_modifier_properties_list modifier_props_list
= {
281 .sType
= VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA
,
284 VkFormatProperties2 format_props
= {
285 .sType
= VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2
,
286 .pNext
= &modifier_props_list
,
288 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
289 pCreateInfo
->imageFormat
,
291 assert(modifier_props_list
.modifier_count
> 0);
292 modifier_props
= vk_alloc(&chain
->alloc
,
293 sizeof(*modifier_props
) *
294 modifier_props_list
.modifier_count
,
296 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
297 if (!modifier_props
) {
298 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
302 modifier_props_list
.modifier_properties
= modifier_props
;
303 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
304 pCreateInfo
->imageFormat
,
306 modifier_prop_count
= modifier_props_list
.modifier_count
;
308 uint32_t max_modifier_count
= 0;
309 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++)
310 max_modifier_count
= MAX2(max_modifier_count
, num_modifiers
[l
]);
312 image_modifiers
= vk_alloc(&chain
->alloc
,
313 sizeof(*image_modifiers
) *
316 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
317 if (!image_modifiers
) {
318 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
322 image_modifier_count
= 0;
323 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++) {
324 /* Walk the modifier lists and construct a list of supported
327 for (uint32_t i
= 0; i
< num_modifiers
[l
]; i
++) {
328 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
329 if (modifier_props
[j
].modifier
== modifiers
[l
][i
])
330 image_modifiers
[image_modifier_count
++] = modifiers
[l
][i
];
334 /* We only want to take the modifiers from the first list */
335 if (image_modifier_count
> 0)
339 if (image_modifier_count
> 0) {
340 image_wsi_info
.modifier_count
= image_modifier_count
;
341 image_wsi_info
.modifiers
= image_modifiers
;
343 /* TODO: Add a proper error here */
344 assert(!"Failed to find a supported modifier! This should never "
345 "happen because LINEAR should always be available");
346 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
351 const VkImageCreateInfo image_info
= {
352 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
353 .pNext
= &image_wsi_info
,
355 .imageType
= VK_IMAGE_TYPE_2D
,
356 .format
= pCreateInfo
->imageFormat
,
358 .width
= pCreateInfo
->imageExtent
.width
,
359 .height
= pCreateInfo
->imageExtent
.height
,
364 .samples
= VK_SAMPLE_COUNT_1_BIT
,
365 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
366 .usage
= pCreateInfo
->imageUsage
,
367 .sharingMode
= pCreateInfo
->imageSharingMode
,
368 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
369 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
370 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
372 result
= wsi
->CreateImage(chain
->device
, &image_info
,
373 &chain
->alloc
, &image
->image
);
374 if (result
!= VK_SUCCESS
)
377 VkMemoryRequirements reqs
;
378 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
380 const struct wsi_memory_allocate_info memory_wsi_info
= {
381 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
383 .implicit_sync
= true,
385 const VkExportMemoryAllocateInfo memory_export_info
= {
386 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO
,
387 .pNext
= &memory_wsi_info
,
388 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
390 const VkMemoryDedicatedAllocateInfo memory_dedicated_info
= {
391 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
392 .pNext
= &memory_export_info
,
393 .image
= image
->image
,
394 .buffer
= VK_NULL_HANDLE
,
396 const VkMemoryAllocateInfo memory_info
= {
397 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
398 .pNext
= &memory_dedicated_info
,
399 .allocationSize
= reqs
.size
,
400 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
401 reqs
.memoryTypeBits
),
403 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
404 &chain
->alloc
, &image
->memory
);
405 if (result
!= VK_SUCCESS
)
408 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
410 if (result
!= VK_SUCCESS
)
413 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
414 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
416 .memory
= image
->memory
,
417 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
420 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
421 if (result
!= VK_SUCCESS
)
424 if (num_modifier_lists
> 0) {
425 image
->drm_modifier
= wsi
->image_get_modifier(image
->image
);
426 assert(image
->drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
428 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
429 if (modifier_props
[j
].modifier
== image
->drm_modifier
) {
430 image
->num_planes
= modifier_props
[j
].modifier_plane_count
;
435 for (uint32_t p
= 0; p
< image
->num_planes
; p
++) {
436 const VkImageSubresource image_subresource
= {
437 .aspectMask
= VK_IMAGE_ASPECT_PLANE_0_BIT
<< p
,
441 VkSubresourceLayout image_layout
;
442 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
443 &image_subresource
, &image_layout
);
444 image
->sizes
[p
] = image_layout
.size
;
445 image
->row_pitches
[p
] = image_layout
.rowPitch
;
446 image
->offsets
[p
] = image_layout
.offset
;
450 image
->fds
[p
] = dup(fd
);
451 if (image
->fds
[p
] == -1) {
452 for (uint32_t i
= 0; i
< p
; i
++)
453 close(image
->fds
[p
]);
460 const VkImageSubresource image_subresource
= {
461 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
465 VkSubresourceLayout image_layout
;
466 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
467 &image_subresource
, &image_layout
);
469 image
->drm_modifier
= DRM_FORMAT_MOD_INVALID
;
470 image
->num_planes
= 1;
471 image
->sizes
[0] = reqs
.size
;
472 image
->row_pitches
[0] = image_layout
.rowPitch
;
473 image
->offsets
[0] = 0;
477 vk_free(&chain
->alloc
, modifier_props
);
478 vk_free(&chain
->alloc
, image_modifiers
);
483 vk_free(&chain
->alloc
, modifier_props
);
484 vk_free(&chain
->alloc
, image_modifiers
);
485 wsi_destroy_image(chain
, image
);
490 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
493 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
494 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
496 struct wsi_image
*image
)
498 const struct wsi_device
*wsi
= chain
->wsi
;
501 memset(image
, 0, sizeof(*image
));
503 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
504 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
505 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
507 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
508 linear_size
= align_u32(linear_size
, 4096);
510 const VkExternalMemoryBufferCreateInfo prime_buffer_external_info
= {
511 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO
,
513 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
515 const VkBufferCreateInfo prime_buffer_info
= {
516 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
517 .pNext
= &prime_buffer_external_info
,
519 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
520 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
522 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
523 &chain
->alloc
, &image
->prime
.buffer
);
524 if (result
!= VK_SUCCESS
)
527 VkMemoryRequirements reqs
;
528 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
529 assert(reqs
.size
<= linear_size
);
531 const struct wsi_memory_allocate_info memory_wsi_info
= {
532 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
534 .implicit_sync
= true,
536 const VkExportMemoryAllocateInfo prime_memory_export_info
= {
537 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO
,
538 .pNext
= &memory_wsi_info
,
539 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
541 const VkMemoryDedicatedAllocateInfo prime_memory_dedicated_info
= {
542 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
543 .pNext
= &prime_memory_export_info
,
544 .image
= VK_NULL_HANDLE
,
545 .buffer
= image
->prime
.buffer
,
547 const VkMemoryAllocateInfo prime_memory_info
= {
548 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
549 .pNext
= &prime_memory_dedicated_info
,
550 .allocationSize
= linear_size
,
551 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
553 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
554 &chain
->alloc
, &image
->prime
.memory
);
555 if (result
!= VK_SUCCESS
)
558 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
559 image
->prime
.memory
, 0);
560 if (result
!= VK_SUCCESS
)
563 const VkImageCreateInfo image_info
= {
564 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
567 .imageType
= VK_IMAGE_TYPE_2D
,
568 .format
= pCreateInfo
->imageFormat
,
570 .width
= pCreateInfo
->imageExtent
.width
,
571 .height
= pCreateInfo
->imageExtent
.height
,
576 .samples
= VK_SAMPLE_COUNT_1_BIT
,
577 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
578 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
579 .sharingMode
= pCreateInfo
->imageSharingMode
,
580 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
581 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
582 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
584 result
= wsi
->CreateImage(chain
->device
, &image_info
,
585 &chain
->alloc
, &image
->image
);
586 if (result
!= VK_SUCCESS
)
589 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
591 const VkMemoryDedicatedAllocateInfo memory_dedicated_info
= {
592 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO
,
594 .image
= image
->image
,
595 .buffer
= VK_NULL_HANDLE
,
597 const VkMemoryAllocateInfo memory_info
= {
598 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
599 .pNext
= &memory_dedicated_info
,
600 .allocationSize
= reqs
.size
,
601 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
602 reqs
.memoryTypeBits
),
604 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
605 &chain
->alloc
, &image
->memory
);
606 if (result
!= VK_SUCCESS
)
609 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
611 if (result
!= VK_SUCCESS
)
614 image
->prime
.blit_cmd_buffers
=
615 vk_zalloc(&chain
->alloc
,
616 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
617 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
618 if (!image
->prime
.blit_cmd_buffers
) {
619 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
623 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
624 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
625 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
627 .commandPool
= chain
->cmd_pools
[i
],
628 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
629 .commandBufferCount
= 1,
631 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
632 &image
->prime
.blit_cmd_buffers
[i
]);
633 if (result
!= VK_SUCCESS
)
636 const VkCommandBufferBeginInfo begin_info
= {
637 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
639 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
641 struct VkBufferImageCopy buffer_image_copy
= {
643 .bufferRowLength
= linear_stride
/ cpp
,
644 .bufferImageHeight
= 0,
645 .imageSubresource
= {
646 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
651 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
653 .width
= pCreateInfo
->imageExtent
.width
,
654 .height
= pCreateInfo
->imageExtent
.height
,
658 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
660 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
662 1, &buffer_image_copy
);
664 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
665 if (result
!= VK_SUCCESS
)
669 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
670 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
672 .memory
= image
->prime
.memory
,
673 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
676 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
677 if (result
!= VK_SUCCESS
)
680 image
->drm_modifier
= use_modifier
? DRM_FORMAT_MOD_LINEAR
: DRM_FORMAT_MOD_INVALID
;
681 image
->num_planes
= 1;
682 image
->sizes
[0] = linear_size
;
683 image
->row_pitches
[0] = linear_stride
;
684 image
->offsets
[0] = 0;
690 wsi_destroy_image(chain
, image
);
696 wsi_destroy_image(const struct wsi_swapchain
*chain
,
697 struct wsi_image
*image
)
699 const struct wsi_device
*wsi
= chain
->wsi
;
701 if (image
->prime
.blit_cmd_buffers
) {
702 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
703 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
704 1, &image
->prime
.blit_cmd_buffers
[i
]);
706 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
709 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
710 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
711 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
712 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
716 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
717 uint32_t queueFamilyIndex
,
718 VkSurfaceKHR _surface
,
719 VkBool32
* pSupported
)
721 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
722 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
724 return iface
->get_support(surface
, wsi_device
,
725 queueFamilyIndex
, pSupported
);
729 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
730 VkSurfaceKHR _surface
,
731 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
733 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
734 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
736 VkSurfaceCapabilities2KHR caps2
= {
737 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
740 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
742 if (result
== VK_SUCCESS
)
743 *pSurfaceCapabilities
= caps2
.surfaceCapabilities
;
749 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
750 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
751 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
753 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
754 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
756 return iface
->get_capabilities2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
757 pSurfaceCapabilities
);
761 wsi_common_get_surface_capabilities2ext(
762 struct wsi_device
*wsi_device
,
763 VkSurfaceKHR _surface
,
764 VkSurfaceCapabilities2EXT
*pSurfaceCapabilities
)
766 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
767 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
769 assert(pSurfaceCapabilities
->sType
==
770 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT
);
772 struct wsi_surface_supported_counters counters
= {
773 .sType
= VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA
,
774 .pNext
= pSurfaceCapabilities
->pNext
,
775 .supported_surface_counters
= 0,
778 VkSurfaceCapabilities2KHR caps2
= {
779 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
783 VkResult result
= iface
->get_capabilities2(surface
, wsi_device
, NULL
, &caps2
);
785 if (result
== VK_SUCCESS
) {
786 VkSurfaceCapabilities2EXT
*ext_caps
= pSurfaceCapabilities
;
787 VkSurfaceCapabilitiesKHR khr_caps
= caps2
.surfaceCapabilities
;
789 ext_caps
->minImageCount
= khr_caps
.minImageCount
;
790 ext_caps
->maxImageCount
= khr_caps
.maxImageCount
;
791 ext_caps
->currentExtent
= khr_caps
.currentExtent
;
792 ext_caps
->minImageExtent
= khr_caps
.minImageExtent
;
793 ext_caps
->maxImageExtent
= khr_caps
.maxImageExtent
;
794 ext_caps
->maxImageArrayLayers
= khr_caps
.maxImageArrayLayers
;
795 ext_caps
->supportedTransforms
= khr_caps
.supportedTransforms
;
796 ext_caps
->currentTransform
= khr_caps
.currentTransform
;
797 ext_caps
->supportedCompositeAlpha
= khr_caps
.supportedCompositeAlpha
;
798 ext_caps
->supportedUsageFlags
= khr_caps
.supportedUsageFlags
;
799 ext_caps
->supportedSurfaceCounters
= counters
.supported_surface_counters
;
806 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
807 VkSurfaceKHR _surface
,
808 uint32_t *pSurfaceFormatCount
,
809 VkSurfaceFormatKHR
*pSurfaceFormats
)
811 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
812 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
814 return iface
->get_formats(surface
, wsi_device
,
815 pSurfaceFormatCount
, pSurfaceFormats
);
819 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
820 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
821 uint32_t *pSurfaceFormatCount
,
822 VkSurfaceFormat2KHR
*pSurfaceFormats
)
824 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
825 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
827 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
828 pSurfaceFormatCount
, pSurfaceFormats
);
832 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
833 VkSurfaceKHR _surface
,
834 uint32_t *pPresentModeCount
,
835 VkPresentModeKHR
*pPresentModes
)
837 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
838 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
840 return iface
->get_present_modes(surface
, pPresentModeCount
,
845 wsi_common_get_present_rectangles(struct wsi_device
*wsi_device
,
846 VkSurfaceKHR _surface
,
847 uint32_t* pRectCount
,
850 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
851 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
853 return iface
->get_present_rectangles(surface
, wsi_device
,
858 wsi_common_create_swapchain(struct wsi_device
*wsi
,
860 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
861 const VkAllocationCallbacks
*pAllocator
,
862 VkSwapchainKHR
*pSwapchain
)
864 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
865 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
866 struct wsi_swapchain
*swapchain
;
868 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
,
869 pCreateInfo
, pAllocator
,
871 if (result
!= VK_SUCCESS
)
874 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
880 wsi_common_destroy_swapchain(VkDevice device
,
881 VkSwapchainKHR _swapchain
,
882 const VkAllocationCallbacks
*pAllocator
)
884 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
888 swapchain
->destroy(swapchain
, pAllocator
);
892 wsi_common_get_images(VkSwapchainKHR _swapchain
,
893 uint32_t *pSwapchainImageCount
,
894 VkImage
*pSwapchainImages
)
896 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
897 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
899 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
900 vk_outarray_append(&images
, image
) {
901 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
905 return vk_outarray_status(&images
);
909 wsi_common_acquire_next_image2(const struct wsi_device
*wsi
,
911 const VkAcquireNextImageInfoKHR
*pAcquireInfo
,
912 uint32_t *pImageIndex
)
914 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pAcquireInfo
->swapchain
);
916 return swapchain
->acquire_next_image(swapchain
, pAcquireInfo
, pImageIndex
);
920 wsi_common_queue_present(const struct wsi_device
*wsi
,
923 int queue_family_index
,
924 const VkPresentInfoKHR
*pPresentInfo
)
926 VkResult final_result
= VK_SUCCESS
;
928 const VkPresentRegionsKHR
*regions
=
929 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
931 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
932 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
935 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
936 const VkFenceCreateInfo fence_info
= {
937 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
941 result
= wsi
->CreateFence(device
, &fence_info
,
943 &swapchain
->fences
[0]);
944 if (result
!= VK_SUCCESS
)
947 wsi
->ResetFences(device
, 1, &swapchain
->fences
[0]);
950 VkSubmitInfo submit_info
= {
951 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
955 VkPipelineStageFlags
*stage_flags
= NULL
;
957 /* We only need/want to wait on semaphores once. After that, we're
958 * guaranteed ordering since it all happens on the same queue.
960 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
;
961 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
;
963 /* Set up the pWaitDstStageMasks */
964 stage_flags
= vk_alloc(&swapchain
->alloc
,
965 sizeof(VkPipelineStageFlags
) *
966 pPresentInfo
->waitSemaphoreCount
,
968 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
970 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
973 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
974 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
976 submit_info
.pWaitDstStageMask
= stage_flags
;
979 if (swapchain
->use_prime_blit
) {
980 /* If we are using prime blits, we need to perform the blit now. The
981 * command buffer is attached to the image.
983 struct wsi_image
*image
=
984 swapchain
->get_wsi_image(swapchain
, pPresentInfo
->pImageIndices
[i
]);
985 submit_info
.commandBufferCount
= 1;
986 submit_info
.pCommandBuffers
=
987 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
990 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[0]);
991 vk_free(&swapchain
->alloc
, stage_flags
);
992 if (result
!= VK_SUCCESS
)
995 const VkPresentRegionKHR
*region
= NULL
;
996 if (regions
&& regions
->pRegions
)
997 region
= ®ions
->pRegions
[i
];
999 result
= swapchain
->queue_present(swapchain
,
1000 pPresentInfo
->pImageIndices
[i
],
1002 if (result
!= VK_SUCCESS
)
1005 VkFence last
= swapchain
->fences
[2];
1006 swapchain
->fences
[2] = swapchain
->fences
[1];
1007 swapchain
->fences
[1] = swapchain
->fences
[0];
1008 swapchain
->fences
[0] = last
;
1010 if (last
!= VK_NULL_HANDLE
) {
1011 wsi
->WaitForFences(device
, 1, &last
, true, 1);
1015 if (pPresentInfo
->pResults
!= NULL
)
1016 pPresentInfo
->pResults
[i
] = result
;
1018 /* Let the final result be our first unsuccessful result */
1019 if (final_result
== VK_SUCCESS
)
1020 final_result
= result
;
1023 return final_result
;
1027 wsi_common_get_current_time(void)
1029 struct timespec current
;
1030 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
1031 return current
.tv_nsec
+ current
.tv_sec
* 1000000000ull;