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"
32 wsi_device_init(struct wsi_device
*wsi
,
33 VkPhysicalDevice pdevice
,
34 WSI_FN_GetPhysicalDeviceProcAddr proc_addr
,
35 const VkAllocationCallbacks
*alloc
,
40 memset(wsi
, 0, sizeof(*wsi
));
42 wsi
->instance_alloc
= *alloc
;
43 wsi
->pdevice
= pdevice
;
45 #define WSI_GET_CB(func) \
46 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
47 WSI_GET_CB(GetPhysicalDeviceMemoryProperties
);
48 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties
);
51 GetPhysicalDeviceMemoryProperties(pdevice
, &wsi
->memory_props
);
52 GetPhysicalDeviceQueueFamilyProperties(pdevice
, &wsi
->queue_family_count
, NULL
);
54 #define WSI_GET_CB(func) \
55 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
56 WSI_GET_CB(AllocateMemory
);
57 WSI_GET_CB(AllocateCommandBuffers
);
58 WSI_GET_CB(BindBufferMemory
);
59 WSI_GET_CB(BindImageMemory
);
60 WSI_GET_CB(BeginCommandBuffer
);
61 WSI_GET_CB(CmdCopyImageToBuffer
);
62 WSI_GET_CB(CreateBuffer
);
63 WSI_GET_CB(CreateCommandPool
);
64 WSI_GET_CB(CreateFence
);
65 WSI_GET_CB(CreateImage
);
66 WSI_GET_CB(DestroyBuffer
);
67 WSI_GET_CB(DestroyCommandPool
);
68 WSI_GET_CB(DestroyFence
);
69 WSI_GET_CB(DestroyImage
);
70 WSI_GET_CB(EndCommandBuffer
);
71 WSI_GET_CB(FreeMemory
);
72 WSI_GET_CB(FreeCommandBuffers
);
73 WSI_GET_CB(GetBufferMemoryRequirements
);
74 WSI_GET_CB(GetImageMemoryRequirements
);
75 WSI_GET_CB(GetImageSubresourceLayout
);
76 WSI_GET_CB(GetMemoryFdKHR
);
77 WSI_GET_CB(GetPhysicalDeviceFormatProperties
);
78 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR
);
79 WSI_GET_CB(ResetFences
);
80 WSI_GET_CB(QueueSubmit
);
81 WSI_GET_CB(WaitForFences
);
84 #ifdef VK_USE_PLATFORM_XCB_KHR
85 result
= wsi_x11_init_wsi(wsi
, alloc
);
86 if (result
!= VK_SUCCESS
)
90 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
91 result
= wsi_wl_init_wsi(wsi
, alloc
, pdevice
);
92 if (result
!= VK_SUCCESS
)
96 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
97 result
= wsi_display_init_wsi(wsi
, alloc
, display_fd
);
98 if (result
!= VK_SUCCESS
)
105 wsi_device_finish(wsi
, alloc
);
110 wsi_device_finish(struct wsi_device
*wsi
,
111 const VkAllocationCallbacks
*alloc
)
113 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
114 wsi_display_finish_wsi(wsi
, alloc
);
116 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
117 wsi_wl_finish_wsi(wsi
, alloc
);
119 #ifdef VK_USE_PLATFORM_XCB_KHR
120 wsi_x11_finish_wsi(wsi
, alloc
);
125 wsi_swapchain_init(const struct wsi_device
*wsi
,
126 struct wsi_swapchain
*chain
,
128 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
129 const VkAllocationCallbacks
*pAllocator
)
133 memset(chain
, 0, sizeof(*chain
));
136 chain
->device
= device
;
137 chain
->alloc
= *pAllocator
;
138 chain
->use_prime_blit
= false;
141 vk_zalloc(pAllocator
, sizeof(VkCommandPool
) * wsi
->queue_family_count
, 8,
142 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
143 if (!chain
->cmd_pools
)
144 return VK_ERROR_OUT_OF_HOST_MEMORY
;
146 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
147 const VkCommandPoolCreateInfo cmd_pool_info
= {
148 .sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
,
151 .queueFamilyIndex
= i
,
153 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
154 &chain
->cmd_pools
[i
]);
155 if (result
!= VK_SUCCESS
)
162 wsi_swapchain_finish(chain
);
167 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
169 for (unsigned i
= 0; i
< ARRAY_SIZE(chain
->fences
); i
++)
170 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
172 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
173 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
176 vk_free(&chain
->alloc
, chain
->cmd_pools
);
180 select_memory_type(const struct wsi_device
*wsi
,
181 VkMemoryPropertyFlags props
,
184 for (uint32_t i
= 0; i
< wsi
->memory_props
.memoryTypeCount
; i
++) {
185 const VkMemoryType type
= wsi
->memory_props
.memoryTypes
[i
];
186 if ((type_bits
& (1 << i
)) && (type
.propertyFlags
& props
) == props
)
190 unreachable("No memory type found");
194 vk_format_size(VkFormat format
)
197 case VK_FORMAT_B8G8R8A8_UNORM
:
198 case VK_FORMAT_B8G8R8A8_SRGB
:
201 unreachable("Unknown WSI Format");
205 static inline uint32_t
206 align_u32(uint32_t v
, uint32_t a
)
208 assert(a
!= 0 && a
== (a
& -a
));
209 return (v
+ a
- 1) & ~(a
- 1);
213 wsi_create_native_image(const struct wsi_swapchain
*chain
,
214 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
215 uint32_t num_modifier_lists
,
216 const uint32_t *num_modifiers
,
217 const uint64_t *const *modifiers
,
218 struct wsi_image
*image
)
220 const struct wsi_device
*wsi
= chain
->wsi
;
223 memset(image
, 0, sizeof(*image
));
224 for (int i
= 0; i
< ARRAY_SIZE(image
->fds
); i
++)
227 struct wsi_image_create_info image_wsi_info
= {
228 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
232 uint32_t image_modifier_count
= 0, modifier_prop_count
= 0;
233 struct wsi_format_modifier_properties
*modifier_props
= NULL
;
234 uint64_t *image_modifiers
= NULL
;
235 if (num_modifier_lists
== 0) {
236 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
237 image_wsi_info
.scanout
= true;
239 /* The winsys can't request modifiers if we don't support them. */
240 assert(wsi
->supports_modifiers
);
241 struct wsi_format_modifier_properties_list modifier_props_list
= {
242 .sType
= VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA
,
245 VkFormatProperties2KHR format_props
= {
246 .sType
= VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2_KHR
,
247 .pNext
= &modifier_props_list
,
249 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
250 pCreateInfo
->imageFormat
,
252 assert(modifier_props_list
.modifier_count
> 0);
253 modifier_props
= vk_alloc(&chain
->alloc
,
254 sizeof(*modifier_props
) *
255 modifier_props_list
.modifier_count
,
257 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
258 if (!modifier_props
) {
259 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
263 modifier_props_list
.modifier_properties
= modifier_props
;
264 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
265 pCreateInfo
->imageFormat
,
267 modifier_prop_count
= modifier_props_list
.modifier_count
;
269 uint32_t max_modifier_count
= 0;
270 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++)
271 max_modifier_count
= MAX2(max_modifier_count
, num_modifiers
[l
]);
273 image_modifiers
= vk_alloc(&chain
->alloc
,
274 sizeof(*image_modifiers
) *
277 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
278 if (!image_modifiers
) {
279 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
283 image_modifier_count
= 0;
284 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++) {
285 /* Walk the modifier lists and construct a list of supported
288 for (uint32_t i
= 0; i
< num_modifiers
[l
]; i
++) {
289 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
290 if (modifier_props
[j
].modifier
== modifiers
[l
][i
])
291 image_modifiers
[image_modifier_count
++] = modifiers
[l
][i
];
295 /* We only want to take the modifiers from the first list */
296 if (image_modifier_count
> 0)
300 if (image_modifier_count
> 0) {
301 image_wsi_info
.modifier_count
= image_modifier_count
;
302 image_wsi_info
.modifiers
= image_modifiers
;
304 /* TODO: Add a proper error here */
305 assert(!"Failed to find a supported modifier! This should never "
306 "happen because LINEAR should always be available");
307 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
312 const VkImageCreateInfo image_info
= {
313 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
314 .pNext
= &image_wsi_info
,
316 .imageType
= VK_IMAGE_TYPE_2D
,
317 .format
= pCreateInfo
->imageFormat
,
319 .width
= pCreateInfo
->imageExtent
.width
,
320 .height
= pCreateInfo
->imageExtent
.height
,
325 .samples
= VK_SAMPLE_COUNT_1_BIT
,
326 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
327 .usage
= pCreateInfo
->imageUsage
,
328 .sharingMode
= pCreateInfo
->imageSharingMode
,
329 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
330 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
331 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
333 result
= wsi
->CreateImage(chain
->device
, &image_info
,
334 &chain
->alloc
, &image
->image
);
335 if (result
!= VK_SUCCESS
)
338 VkMemoryRequirements reqs
;
339 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
341 const struct wsi_memory_allocate_info memory_wsi_info
= {
342 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
344 .implicit_sync
= true,
346 const VkExportMemoryAllocateInfoKHR memory_export_info
= {
347 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
348 .pNext
= &memory_wsi_info
,
349 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
351 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
352 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
353 .pNext
= &memory_export_info
,
354 .image
= image
->image
,
355 .buffer
= VK_NULL_HANDLE
,
357 const VkMemoryAllocateInfo memory_info
= {
358 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
359 .pNext
= &memory_dedicated_info
,
360 .allocationSize
= reqs
.size
,
361 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
362 reqs
.memoryTypeBits
),
364 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
365 &chain
->alloc
, &image
->memory
);
366 if (result
!= VK_SUCCESS
)
369 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
371 if (result
!= VK_SUCCESS
)
374 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
375 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
377 .memory
= image
->memory
,
378 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
381 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
382 if (result
!= VK_SUCCESS
)
385 if (num_modifier_lists
> 0) {
386 image
->drm_modifier
= wsi
->image_get_modifier(image
->image
);
387 assert(image
->drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
389 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
390 if (modifier_props
[j
].modifier
== image
->drm_modifier
) {
391 image
->num_planes
= modifier_props
[j
].modifier_plane_count
;
396 for (uint32_t p
= 0; p
< image
->num_planes
; p
++) {
397 const VkImageSubresource image_subresource
= {
398 .aspectMask
= VK_IMAGE_ASPECT_PLANE_0_BIT_KHR
<< p
,
402 VkSubresourceLayout image_layout
;
403 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
404 &image_subresource
, &image_layout
);
405 image
->sizes
[p
] = image_layout
.size
;
406 image
->row_pitches
[p
] = image_layout
.rowPitch
;
407 image
->offsets
[p
] = image_layout
.offset
;
411 image
->fds
[p
] = dup(fd
);
412 if (image
->fds
[p
] == -1) {
413 for (uint32_t i
= 0; i
< p
; i
++)
414 close(image
->fds
[p
]);
421 const VkImageSubresource image_subresource
= {
422 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
426 VkSubresourceLayout image_layout
;
427 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
428 &image_subresource
, &image_layout
);
430 image
->drm_modifier
= DRM_FORMAT_MOD_INVALID
;
431 image
->num_planes
= 1;
432 image
->sizes
[0] = reqs
.size
;
433 image
->row_pitches
[0] = image_layout
.rowPitch
;
434 image
->offsets
[0] = 0;
438 vk_free(&chain
->alloc
, modifier_props
);
439 vk_free(&chain
->alloc
, image_modifiers
);
444 vk_free(&chain
->alloc
, modifier_props
);
445 vk_free(&chain
->alloc
, image_modifiers
);
446 wsi_destroy_image(chain
, image
);
451 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
454 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
455 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
457 struct wsi_image
*image
)
459 const struct wsi_device
*wsi
= chain
->wsi
;
462 memset(image
, 0, sizeof(*image
));
464 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
465 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
466 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
468 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
469 linear_size
= align_u32(linear_size
, 4096);
471 const VkExternalMemoryBufferCreateInfoKHR prime_buffer_external_info
= {
472 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR
,
474 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
476 const VkBufferCreateInfo prime_buffer_info
= {
477 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
478 .pNext
= &prime_buffer_external_info
,
480 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
481 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
483 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
484 &chain
->alloc
, &image
->prime
.buffer
);
485 if (result
!= VK_SUCCESS
)
488 VkMemoryRequirements reqs
;
489 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
490 assert(reqs
.size
<= linear_size
);
492 const struct wsi_memory_allocate_info memory_wsi_info
= {
493 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
495 .implicit_sync
= true,
497 const VkExportMemoryAllocateInfoKHR prime_memory_export_info
= {
498 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
499 .pNext
= &memory_wsi_info
,
500 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
502 const VkMemoryDedicatedAllocateInfoKHR prime_memory_dedicated_info
= {
503 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
504 .pNext
= &prime_memory_export_info
,
505 .image
= VK_NULL_HANDLE
,
506 .buffer
= image
->prime
.buffer
,
508 const VkMemoryAllocateInfo prime_memory_info
= {
509 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
510 .pNext
= &prime_memory_dedicated_info
,
511 .allocationSize
= linear_size
,
512 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
514 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
515 &chain
->alloc
, &image
->prime
.memory
);
516 if (result
!= VK_SUCCESS
)
519 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
520 image
->prime
.memory
, 0);
521 if (result
!= VK_SUCCESS
)
524 const VkImageCreateInfo image_info
= {
525 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
528 .imageType
= VK_IMAGE_TYPE_2D
,
529 .format
= pCreateInfo
->imageFormat
,
531 .width
= pCreateInfo
->imageExtent
.width
,
532 .height
= pCreateInfo
->imageExtent
.height
,
537 .samples
= VK_SAMPLE_COUNT_1_BIT
,
538 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
539 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
540 .sharingMode
= pCreateInfo
->imageSharingMode
,
541 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
542 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
543 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
545 result
= wsi
->CreateImage(chain
->device
, &image_info
,
546 &chain
->alloc
, &image
->image
);
547 if (result
!= VK_SUCCESS
)
550 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
552 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
553 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
555 .image
= image
->image
,
556 .buffer
= VK_NULL_HANDLE
,
558 const VkMemoryAllocateInfo memory_info
= {
559 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
560 .pNext
= &memory_dedicated_info
,
561 .allocationSize
= reqs
.size
,
562 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
563 reqs
.memoryTypeBits
),
565 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
566 &chain
->alloc
, &image
->memory
);
567 if (result
!= VK_SUCCESS
)
570 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
572 if (result
!= VK_SUCCESS
)
575 image
->prime
.blit_cmd_buffers
=
576 vk_zalloc(&chain
->alloc
,
577 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
578 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
579 if (!image
->prime
.blit_cmd_buffers
) {
580 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
584 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
585 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
586 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
588 .commandPool
= chain
->cmd_pools
[i
],
589 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
590 .commandBufferCount
= 1,
592 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
593 &image
->prime
.blit_cmd_buffers
[i
]);
594 if (result
!= VK_SUCCESS
)
597 const VkCommandBufferBeginInfo begin_info
= {
598 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
600 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
602 struct VkBufferImageCopy buffer_image_copy
= {
604 .bufferRowLength
= linear_stride
/ cpp
,
605 .bufferImageHeight
= 0,
606 .imageSubresource
= {
607 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
612 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
614 .width
= pCreateInfo
->imageExtent
.width
,
615 .height
= pCreateInfo
->imageExtent
.height
,
619 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
621 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
623 1, &buffer_image_copy
);
625 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
626 if (result
!= VK_SUCCESS
)
630 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
631 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
633 .memory
= image
->prime
.memory
,
634 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
637 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
638 if (result
!= VK_SUCCESS
)
641 image
->drm_modifier
= use_modifier
? DRM_FORMAT_MOD_LINEAR
: DRM_FORMAT_MOD_INVALID
;
642 image
->num_planes
= 1;
643 image
->sizes
[0] = linear_size
;
644 image
->row_pitches
[0] = linear_stride
;
645 image
->offsets
[0] = 0;
651 wsi_destroy_image(chain
, image
);
657 wsi_destroy_image(const struct wsi_swapchain
*chain
,
658 struct wsi_image
*image
)
660 const struct wsi_device
*wsi
= chain
->wsi
;
662 if (image
->prime
.blit_cmd_buffers
) {
663 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
664 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
665 1, &image
->prime
.blit_cmd_buffers
[i
]);
667 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
670 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
671 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
672 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
673 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
677 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
679 uint32_t queueFamilyIndex
,
680 VkSurfaceKHR _surface
,
681 VkBool32
* pSupported
)
683 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
684 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
686 return iface
->get_support(surface
, wsi_device
,
687 queueFamilyIndex
, local_fd
, pSupported
);
691 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
692 VkSurfaceKHR _surface
,
693 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
695 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
696 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
698 VkSurfaceCapabilities2KHR caps2
= {
699 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
702 VkResult result
= iface
->get_capabilities2(surface
, NULL
, &caps2
);
704 if (result
== VK_SUCCESS
)
705 *pSurfaceCapabilities
= caps2
.surfaceCapabilities
;
711 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
712 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
713 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
715 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
716 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
718 return iface
->get_capabilities2(surface
, pSurfaceInfo
->pNext
,
719 pSurfaceCapabilities
);
723 wsi_common_get_surface_capabilities2ext(
724 struct wsi_device
*wsi_device
,
725 VkSurfaceKHR _surface
,
726 VkSurfaceCapabilities2EXT
*pSurfaceCapabilities
)
728 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
729 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
731 assert(pSurfaceCapabilities
->sType
==
732 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT
);
734 struct wsi_surface_supported_counters counters
= {
735 .sType
= VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA
,
736 .pNext
= pSurfaceCapabilities
->pNext
,
737 .supported_surface_counters
= 0,
740 VkSurfaceCapabilities2KHR caps2
= {
741 .sType
= VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
,
745 VkResult result
= iface
->get_capabilities2(surface
, NULL
, &caps2
);
747 if (result
== VK_SUCCESS
) {
748 VkSurfaceCapabilities2EXT
*ext_caps
= pSurfaceCapabilities
;
749 VkSurfaceCapabilitiesKHR khr_caps
= caps2
.surfaceCapabilities
;
751 ext_caps
->minImageCount
= khr_caps
.minImageCount
;
752 ext_caps
->maxImageCount
= khr_caps
.maxImageCount
;
753 ext_caps
->currentExtent
= khr_caps
.currentExtent
;
754 ext_caps
->minImageExtent
= khr_caps
.minImageExtent
;
755 ext_caps
->maxImageExtent
= khr_caps
.maxImageExtent
;
756 ext_caps
->maxImageArrayLayers
= khr_caps
.maxImageArrayLayers
;
757 ext_caps
->supportedTransforms
= khr_caps
.supportedTransforms
;
758 ext_caps
->currentTransform
= khr_caps
.currentTransform
;
759 ext_caps
->supportedCompositeAlpha
= khr_caps
.supportedCompositeAlpha
;
760 ext_caps
->supportedUsageFlags
= khr_caps
.supportedUsageFlags
;
761 ext_caps
->supportedSurfaceCounters
= counters
.supported_surface_counters
;
768 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
769 VkSurfaceKHR _surface
,
770 uint32_t *pSurfaceFormatCount
,
771 VkSurfaceFormatKHR
*pSurfaceFormats
)
773 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
774 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
776 return iface
->get_formats(surface
, wsi_device
,
777 pSurfaceFormatCount
, pSurfaceFormats
);
781 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
782 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
783 uint32_t *pSurfaceFormatCount
,
784 VkSurfaceFormat2KHR
*pSurfaceFormats
)
786 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
787 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
789 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
790 pSurfaceFormatCount
, pSurfaceFormats
);
794 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
795 VkSurfaceKHR _surface
,
796 uint32_t *pPresentModeCount
,
797 VkPresentModeKHR
*pPresentModes
)
799 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
800 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
802 return iface
->get_present_modes(surface
, pPresentModeCount
,
807 wsi_common_get_present_rectangles(struct wsi_device
*wsi_device
,
809 VkSurfaceKHR _surface
,
810 uint32_t* pRectCount
,
813 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
814 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
816 return iface
->get_present_rectangles(surface
, wsi_device
, local_fd
,
821 wsi_common_create_swapchain(struct wsi_device
*wsi
,
824 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
825 const VkAllocationCallbacks
*pAllocator
,
826 VkSwapchainKHR
*pSwapchain
)
828 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
829 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
830 struct wsi_swapchain
*swapchain
;
832 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
, fd
,
833 pCreateInfo
, pAllocator
,
835 if (result
!= VK_SUCCESS
)
838 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
844 wsi_common_destroy_swapchain(VkDevice device
,
845 VkSwapchainKHR _swapchain
,
846 const VkAllocationCallbacks
*pAllocator
)
848 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
852 swapchain
->destroy(swapchain
, pAllocator
);
856 wsi_common_get_images(VkSwapchainKHR _swapchain
,
857 uint32_t *pSwapchainImageCount
,
858 VkImage
*pSwapchainImages
)
860 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
861 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
863 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
864 vk_outarray_append(&images
, image
) {
865 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
869 return vk_outarray_status(&images
);
873 wsi_common_acquire_next_image2(const struct wsi_device
*wsi
,
875 const VkAcquireNextImageInfoKHR
*pAcquireInfo
,
876 uint32_t *pImageIndex
)
878 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pAcquireInfo
->swapchain
);
880 return swapchain
->acquire_next_image(swapchain
, pAcquireInfo
, pImageIndex
);
884 wsi_common_queue_present(const struct wsi_device
*wsi
,
887 int queue_family_index
,
888 const VkPresentInfoKHR
*pPresentInfo
)
890 VkResult final_result
= VK_SUCCESS
;
892 const VkPresentRegionsKHR
*regions
=
893 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
895 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
896 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
899 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
900 const VkFenceCreateInfo fence_info
= {
901 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
905 result
= wsi
->CreateFence(device
, &fence_info
,
907 &swapchain
->fences
[0]);
908 if (result
!= VK_SUCCESS
)
911 wsi
->ResetFences(device
, 1, &swapchain
->fences
[0]);
914 VkSubmitInfo submit_info
= {
915 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
919 VkPipelineStageFlags
*stage_flags
= NULL
;
921 /* We only need/want to wait on semaphores once. After that, we're
922 * guaranteed ordering since it all happens on the same queue.
924 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
,
925 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
,
927 /* Set up the pWaitDstStageMasks */
928 stage_flags
= vk_alloc(&swapchain
->alloc
,
929 sizeof(VkPipelineStageFlags
) *
930 pPresentInfo
->waitSemaphoreCount
,
932 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
934 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
937 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
938 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
940 submit_info
.pWaitDstStageMask
= stage_flags
;
943 if (swapchain
->use_prime_blit
) {
944 /* If we are using prime blits, we need to perform the blit now. The
945 * command buffer is attached to the image.
947 struct wsi_image
*image
=
948 swapchain
->get_wsi_image(swapchain
, pPresentInfo
->pImageIndices
[i
]);
949 submit_info
.commandBufferCount
= 1;
950 submit_info
.pCommandBuffers
=
951 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
954 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[0]);
955 vk_free(&swapchain
->alloc
, stage_flags
);
956 if (result
!= VK_SUCCESS
)
959 const VkPresentRegionKHR
*region
= NULL
;
960 if (regions
&& regions
->pRegions
)
961 region
= ®ions
->pRegions
[i
];
963 result
= swapchain
->queue_present(swapchain
,
964 pPresentInfo
->pImageIndices
[i
],
966 if (result
!= VK_SUCCESS
)
969 VkFence last
= swapchain
->fences
[2];
970 swapchain
->fences
[2] = swapchain
->fences
[1];
971 swapchain
->fences
[1] = swapchain
->fences
[0];
972 swapchain
->fences
[0] = last
;
974 if (last
!= VK_NULL_HANDLE
) {
975 wsi
->WaitForFences(device
, 1, &last
, true, 1);
979 if (pPresentInfo
->pResults
!= NULL
)
980 pPresentInfo
->pResults
[i
] = result
;
982 /* Let the final result be our first unsuccessful result */
983 if (final_result
== VK_SUCCESS
)
984 final_result
= result
;