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
)
39 memset(wsi
, 0, sizeof(*wsi
));
41 wsi
->pdevice
= pdevice
;
43 #define WSI_GET_CB(func) \
44 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
45 WSI_GET_CB(GetPhysicalDeviceMemoryProperties
);
46 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties
);
49 GetPhysicalDeviceMemoryProperties(pdevice
, &wsi
->memory_props
);
50 GetPhysicalDeviceQueueFamilyProperties(pdevice
, &wsi
->queue_family_count
, NULL
);
52 #define WSI_GET_CB(func) \
53 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
54 WSI_GET_CB(AllocateMemory
);
55 WSI_GET_CB(AllocateCommandBuffers
);
56 WSI_GET_CB(BindBufferMemory
);
57 WSI_GET_CB(BindImageMemory
);
58 WSI_GET_CB(BeginCommandBuffer
);
59 WSI_GET_CB(CmdCopyImageToBuffer
);
60 WSI_GET_CB(CreateBuffer
);
61 WSI_GET_CB(CreateCommandPool
);
62 WSI_GET_CB(CreateFence
);
63 WSI_GET_CB(CreateImage
);
64 WSI_GET_CB(DestroyBuffer
);
65 WSI_GET_CB(DestroyCommandPool
);
66 WSI_GET_CB(DestroyFence
);
67 WSI_GET_CB(DestroyImage
);
68 WSI_GET_CB(EndCommandBuffer
);
69 WSI_GET_CB(FreeMemory
);
70 WSI_GET_CB(FreeCommandBuffers
);
71 WSI_GET_CB(GetBufferMemoryRequirements
);
72 WSI_GET_CB(GetImageMemoryRequirements
);
73 WSI_GET_CB(GetImageSubresourceLayout
);
74 WSI_GET_CB(GetMemoryFdKHR
);
75 WSI_GET_CB(GetPhysicalDeviceFormatProperties
);
76 WSI_GET_CB(GetPhysicalDeviceFormatProperties2KHR
);
77 WSI_GET_CB(ResetFences
);
78 WSI_GET_CB(QueueSubmit
);
79 WSI_GET_CB(WaitForFences
);
82 #ifdef VK_USE_PLATFORM_XCB_KHR
83 result
= wsi_x11_init_wsi(wsi
, alloc
);
84 if (result
!= VK_SUCCESS
)
88 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
89 result
= wsi_wl_init_wsi(wsi
, alloc
, pdevice
);
90 if (result
!= VK_SUCCESS
)
97 wsi_device_finish(wsi
, alloc
);
102 wsi_device_finish(struct wsi_device
*wsi
,
103 const VkAllocationCallbacks
*alloc
)
105 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
106 wsi_wl_finish_wsi(wsi
, alloc
);
108 #ifdef VK_USE_PLATFORM_XCB_KHR
109 wsi_x11_finish_wsi(wsi
, alloc
);
114 wsi_swapchain_init(const struct wsi_device
*wsi
,
115 struct wsi_swapchain
*chain
,
117 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
118 const VkAllocationCallbacks
*pAllocator
)
122 memset(chain
, 0, sizeof(*chain
));
125 chain
->device
= device
;
126 chain
->alloc
= *pAllocator
;
127 chain
->use_prime_blit
= false;
130 vk_zalloc(pAllocator
, sizeof(VkCommandPool
) * wsi
->queue_family_count
, 8,
131 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
132 if (!chain
->cmd_pools
)
133 return VK_ERROR_OUT_OF_HOST_MEMORY
;
135 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
136 const VkCommandPoolCreateInfo cmd_pool_info
= {
137 .sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
,
140 .queueFamilyIndex
= i
,
142 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
143 &chain
->cmd_pools
[i
]);
144 if (result
!= VK_SUCCESS
)
151 wsi_swapchain_finish(chain
);
156 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
158 for (unsigned i
= 0; i
< ARRAY_SIZE(chain
->fences
); i
++)
159 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
161 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
162 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
165 vk_free(&chain
->alloc
, chain
->cmd_pools
);
169 select_memory_type(const struct wsi_device
*wsi
,
170 VkMemoryPropertyFlags props
,
173 for (uint32_t i
= 0; i
< wsi
->memory_props
.memoryTypeCount
; i
++) {
174 const VkMemoryType type
= wsi
->memory_props
.memoryTypes
[i
];
175 if ((type_bits
& (1 << i
)) && (type
.propertyFlags
& props
) == props
)
179 unreachable("No memory type found");
183 vk_format_size(VkFormat format
)
186 case VK_FORMAT_B8G8R8A8_UNORM
:
187 case VK_FORMAT_B8G8R8A8_SRGB
:
190 unreachable("Unknown WSI Format");
194 static inline uint32_t
195 align_u32(uint32_t v
, uint32_t a
)
197 assert(a
!= 0 && a
== (a
& -a
));
198 return (v
+ a
- 1) & ~(a
- 1);
202 wsi_create_native_image(const struct wsi_swapchain
*chain
,
203 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
204 uint32_t num_modifier_lists
,
205 const uint32_t *num_modifiers
,
206 const uint64_t *const *modifiers
,
207 struct wsi_image
*image
)
209 const struct wsi_device
*wsi
= chain
->wsi
;
212 memset(image
, 0, sizeof(*image
));
213 for (int i
= 0; i
< ARRAY_SIZE(image
->fds
); i
++)
216 struct wsi_image_create_info image_wsi_info
= {
217 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
221 uint32_t image_modifier_count
= 0, modifier_prop_count
= 0;
222 struct wsi_format_modifier_properties
*modifier_props
= NULL
;
223 uint64_t *image_modifiers
= NULL
;
224 if (num_modifier_lists
== 0) {
225 /* If we don't have modifiers, fall back to the legacy "scanout" flag */
226 image_wsi_info
.scanout
= true;
228 /* The winsys can't request modifiers if we don't support them. */
229 assert(wsi
->supports_modifiers
);
230 struct wsi_format_modifier_properties_list modifier_props_list
= {
231 .sType
= VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA
,
234 VkFormatProperties2KHR format_props
= {
235 .sType
= VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2_KHR
,
236 .pNext
= &modifier_props_list
,
238 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
239 pCreateInfo
->imageFormat
,
241 assert(modifier_props_list
.modifier_count
> 0);
242 modifier_props
= vk_alloc(&chain
->alloc
,
243 sizeof(*modifier_props
) *
244 modifier_props_list
.modifier_count
,
246 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
247 if (!modifier_props
) {
248 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
252 modifier_props_list
.modifier_properties
= modifier_props
;
253 wsi
->GetPhysicalDeviceFormatProperties2KHR(wsi
->pdevice
,
254 pCreateInfo
->imageFormat
,
256 modifier_prop_count
= modifier_props_list
.modifier_count
;
258 uint32_t max_modifier_count
= 0;
259 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++)
260 max_modifier_count
= MAX2(max_modifier_count
, num_modifiers
[l
]);
262 image_modifiers
= vk_alloc(&chain
->alloc
,
263 sizeof(*image_modifiers
) *
266 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
267 if (!image_modifiers
) {
268 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
272 image_modifier_count
= 0;
273 for (uint32_t l
= 0; l
< num_modifier_lists
; l
++) {
274 /* Walk the modifier lists and construct a list of supported
277 for (uint32_t i
= 0; i
< num_modifiers
[l
]; i
++) {
278 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
279 if (modifier_props
[j
].modifier
== modifiers
[l
][i
])
280 image_modifiers
[image_modifier_count
++] = modifiers
[l
][i
];
284 /* We only want to take the modifiers from the first list */
285 if (image_modifier_count
> 0)
289 if (image_modifier_count
> 0) {
290 image_wsi_info
.modifier_count
= image_modifier_count
;
291 image_wsi_info
.modifiers
= image_modifiers
;
293 /* TODO: Add a proper error here */
294 assert(!"Failed to find a supported modifier! This should never "
295 "happen because LINEAR should always be available");
296 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
301 const VkImageCreateInfo image_info
= {
302 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
303 .pNext
= &image_wsi_info
,
305 .imageType
= VK_IMAGE_TYPE_2D
,
306 .format
= pCreateInfo
->imageFormat
,
308 .width
= pCreateInfo
->imageExtent
.width
,
309 .height
= pCreateInfo
->imageExtent
.height
,
314 .samples
= VK_SAMPLE_COUNT_1_BIT
,
315 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
316 .usage
= pCreateInfo
->imageUsage
,
317 .sharingMode
= pCreateInfo
->imageSharingMode
,
318 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
319 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
320 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
322 result
= wsi
->CreateImage(chain
->device
, &image_info
,
323 &chain
->alloc
, &image
->image
);
324 if (result
!= VK_SUCCESS
)
327 VkMemoryRequirements reqs
;
328 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
330 const struct wsi_memory_allocate_info memory_wsi_info
= {
331 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
333 .implicit_sync
= true,
335 const VkExportMemoryAllocateInfoKHR memory_export_info
= {
336 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
337 .pNext
= &memory_wsi_info
,
338 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
340 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
341 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
342 .pNext
= &memory_export_info
,
343 .image
= image
->image
,
344 .buffer
= VK_NULL_HANDLE
,
346 const VkMemoryAllocateInfo memory_info
= {
347 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
348 .pNext
= &memory_dedicated_info
,
349 .allocationSize
= reqs
.size
,
350 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
351 reqs
.memoryTypeBits
),
353 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
354 &chain
->alloc
, &image
->memory
);
355 if (result
!= VK_SUCCESS
)
358 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
360 if (result
!= VK_SUCCESS
)
363 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
364 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
366 .memory
= image
->memory
,
367 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
370 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
371 if (result
!= VK_SUCCESS
)
374 if (num_modifier_lists
> 0) {
375 image
->drm_modifier
= wsi
->image_get_modifier(image
->image
);
376 assert(image
->drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
378 for (uint32_t j
= 0; j
< modifier_prop_count
; j
++) {
379 if (modifier_props
[j
].modifier
== image
->drm_modifier
) {
380 image
->num_planes
= modifier_props
[j
].modifier_plane_count
;
385 for (uint32_t p
= 0; p
< image
->num_planes
; p
++) {
386 const VkImageSubresource image_subresource
= {
387 .aspectMask
= VK_IMAGE_ASPECT_PLANE_0_BIT_KHR
<< p
,
391 VkSubresourceLayout image_layout
;
392 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
393 &image_subresource
, &image_layout
);
394 image
->sizes
[p
] = image_layout
.size
;
395 image
->row_pitches
[p
] = image_layout
.rowPitch
;
396 image
->offsets
[p
] = image_layout
.offset
;
400 image
->fds
[p
] = dup(fd
);
401 if (image
->fds
[p
] == -1) {
402 for (uint32_t i
= 0; i
< p
; i
++)
403 close(image
->fds
[p
]);
410 const VkImageSubresource image_subresource
= {
411 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
415 VkSubresourceLayout image_layout
;
416 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
417 &image_subresource
, &image_layout
);
419 image
->drm_modifier
= DRM_FORMAT_MOD_INVALID
;
420 image
->num_planes
= 1;
421 image
->sizes
[0] = reqs
.size
;
422 image
->row_pitches
[0] = image_layout
.rowPitch
;
423 image
->offsets
[0] = 0;
427 vk_free(&chain
->alloc
, modifier_props
);
428 vk_free(&chain
->alloc
, image_modifiers
);
433 vk_free(&chain
->alloc
, modifier_props
);
434 vk_free(&chain
->alloc
, image_modifiers
);
435 wsi_destroy_image(chain
, image
);
440 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
443 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
444 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
446 struct wsi_image
*image
)
448 const struct wsi_device
*wsi
= chain
->wsi
;
451 memset(image
, 0, sizeof(*image
));
453 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
454 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
455 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
457 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
458 linear_size
= align_u32(linear_size
, 4096);
460 const VkExternalMemoryBufferCreateInfoKHR prime_buffer_external_info
= {
461 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR
,
463 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
465 const VkBufferCreateInfo prime_buffer_info
= {
466 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
467 .pNext
= &prime_buffer_external_info
,
469 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
470 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
472 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
473 &chain
->alloc
, &image
->prime
.buffer
);
474 if (result
!= VK_SUCCESS
)
477 VkMemoryRequirements reqs
;
478 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
479 assert(reqs
.size
<= linear_size
);
481 const struct wsi_memory_allocate_info memory_wsi_info
= {
482 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
484 .implicit_sync
= true,
486 const VkExportMemoryAllocateInfoKHR prime_memory_export_info
= {
487 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
488 .pNext
= &memory_wsi_info
,
489 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
491 const VkMemoryDedicatedAllocateInfoKHR prime_memory_dedicated_info
= {
492 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
493 .pNext
= &prime_memory_export_info
,
494 .image
= VK_NULL_HANDLE
,
495 .buffer
= image
->prime
.buffer
,
497 const VkMemoryAllocateInfo prime_memory_info
= {
498 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
499 .pNext
= &prime_memory_dedicated_info
,
500 .allocationSize
= linear_size
,
501 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
503 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
504 &chain
->alloc
, &image
->prime
.memory
);
505 if (result
!= VK_SUCCESS
)
508 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
509 image
->prime
.memory
, 0);
510 if (result
!= VK_SUCCESS
)
513 const VkImageCreateInfo image_info
= {
514 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
517 .imageType
= VK_IMAGE_TYPE_2D
,
518 .format
= pCreateInfo
->imageFormat
,
520 .width
= pCreateInfo
->imageExtent
.width
,
521 .height
= pCreateInfo
->imageExtent
.height
,
526 .samples
= VK_SAMPLE_COUNT_1_BIT
,
527 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
528 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
529 .sharingMode
= pCreateInfo
->imageSharingMode
,
530 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
531 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
532 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
534 result
= wsi
->CreateImage(chain
->device
, &image_info
,
535 &chain
->alloc
, &image
->image
);
536 if (result
!= VK_SUCCESS
)
539 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
541 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
542 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
544 .image
= image
->image
,
545 .buffer
= VK_NULL_HANDLE
,
547 const VkMemoryAllocateInfo memory_info
= {
548 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
549 .pNext
= &memory_dedicated_info
,
550 .allocationSize
= reqs
.size
,
551 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
552 reqs
.memoryTypeBits
),
554 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
555 &chain
->alloc
, &image
->memory
);
556 if (result
!= VK_SUCCESS
)
559 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
561 if (result
!= VK_SUCCESS
)
564 image
->prime
.blit_cmd_buffers
=
565 vk_zalloc(&chain
->alloc
,
566 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
567 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
568 if (!image
->prime
.blit_cmd_buffers
) {
569 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
573 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
574 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
575 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
577 .commandPool
= chain
->cmd_pools
[i
],
578 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
579 .commandBufferCount
= 1,
581 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
582 &image
->prime
.blit_cmd_buffers
[i
]);
583 if (result
!= VK_SUCCESS
)
586 const VkCommandBufferBeginInfo begin_info
= {
587 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
589 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
591 struct VkBufferImageCopy buffer_image_copy
= {
593 .bufferRowLength
= linear_stride
/ cpp
,
594 .bufferImageHeight
= 0,
595 .imageSubresource
= {
596 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
601 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
603 .width
= pCreateInfo
->imageExtent
.width
,
604 .height
= pCreateInfo
->imageExtent
.height
,
608 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
610 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
612 1, &buffer_image_copy
);
614 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
615 if (result
!= VK_SUCCESS
)
619 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
620 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
622 .memory
= image
->prime
.memory
,
623 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
626 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
627 if (result
!= VK_SUCCESS
)
630 image
->drm_modifier
= use_modifier
? DRM_FORMAT_MOD_LINEAR
: DRM_FORMAT_MOD_INVALID
;
631 image
->num_planes
= 1;
632 image
->sizes
[0] = linear_size
;
633 image
->row_pitches
[0] = linear_stride
;
634 image
->offsets
[0] = 0;
640 wsi_destroy_image(chain
, image
);
646 wsi_destroy_image(const struct wsi_swapchain
*chain
,
647 struct wsi_image
*image
)
649 const struct wsi_device
*wsi
= chain
->wsi
;
651 if (image
->prime
.blit_cmd_buffers
) {
652 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
653 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
654 1, &image
->prime
.blit_cmd_buffers
[i
]);
656 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
659 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
660 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
661 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
662 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
666 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
668 uint32_t queueFamilyIndex
,
669 VkSurfaceKHR _surface
,
670 const VkAllocationCallbacks
*alloc
,
671 VkBool32
* pSupported
)
673 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
674 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
676 return iface
->get_support(surface
, wsi_device
, alloc
,
677 queueFamilyIndex
, local_fd
, pSupported
);
681 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
682 VkSurfaceKHR _surface
,
683 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
685 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
686 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
688 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
692 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
693 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
694 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
696 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
697 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
699 return iface
->get_capabilities2(surface
, pSurfaceInfo
->pNext
,
700 pSurfaceCapabilities
);
704 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
705 VkSurfaceKHR _surface
,
706 uint32_t *pSurfaceFormatCount
,
707 VkSurfaceFormatKHR
*pSurfaceFormats
)
709 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
710 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
712 return iface
->get_formats(surface
, wsi_device
,
713 pSurfaceFormatCount
, pSurfaceFormats
);
717 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
718 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
719 uint32_t *pSurfaceFormatCount
,
720 VkSurfaceFormat2KHR
*pSurfaceFormats
)
722 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
723 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
725 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
726 pSurfaceFormatCount
, pSurfaceFormats
);
730 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
731 VkSurfaceKHR _surface
,
732 uint32_t *pPresentModeCount
,
733 VkPresentModeKHR
*pPresentModes
)
735 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
736 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
738 return iface
->get_present_modes(surface
, pPresentModeCount
,
743 wsi_common_create_swapchain(struct wsi_device
*wsi
,
746 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
747 const VkAllocationCallbacks
*pAllocator
,
748 VkSwapchainKHR
*pSwapchain
)
750 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
751 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
752 struct wsi_swapchain
*swapchain
;
754 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
, fd
,
755 pCreateInfo
, pAllocator
,
757 if (result
!= VK_SUCCESS
)
760 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
766 wsi_common_destroy_swapchain(VkDevice device
,
767 VkSwapchainKHR _swapchain
,
768 const VkAllocationCallbacks
*pAllocator
)
770 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
774 swapchain
->destroy(swapchain
, pAllocator
);
778 wsi_common_get_images(VkSwapchainKHR _swapchain
,
779 uint32_t *pSwapchainImageCount
,
780 VkImage
*pSwapchainImages
)
782 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
783 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
785 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
786 vk_outarray_append(&images
, image
) {
787 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
791 return vk_outarray_status(&images
);
795 wsi_common_acquire_next_image(const struct wsi_device
*wsi
,
797 VkSwapchainKHR _swapchain
,
799 VkSemaphore semaphore
,
800 uint32_t *pImageIndex
)
802 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
804 return swapchain
->acquire_next_image(swapchain
, timeout
,
805 semaphore
, pImageIndex
);
809 wsi_common_queue_present(const struct wsi_device
*wsi
,
812 int queue_family_index
,
813 const VkPresentInfoKHR
*pPresentInfo
)
815 VkResult final_result
= VK_SUCCESS
;
817 const VkPresentRegionsKHR
*regions
=
818 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
820 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
821 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
824 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
825 const VkFenceCreateInfo fence_info
= {
826 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
830 result
= wsi
->CreateFence(device
, &fence_info
,
832 &swapchain
->fences
[0]);
833 if (result
!= VK_SUCCESS
)
836 wsi
->ResetFences(device
, 1, &swapchain
->fences
[0]);
839 VkSubmitInfo submit_info
= {
840 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
844 VkPipelineStageFlags
*stage_flags
= NULL
;
846 /* We only need/want to wait on semaphores once. After that, we're
847 * guaranteed ordering since it all happens on the same queue.
849 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
,
850 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
,
852 /* Set up the pWaitDstStageMasks */
853 stage_flags
= vk_alloc(&swapchain
->alloc
,
854 sizeof(VkPipelineStageFlags
) *
855 pPresentInfo
->waitSemaphoreCount
,
857 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
859 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
862 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
863 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
865 submit_info
.pWaitDstStageMask
= stage_flags
;
868 if (swapchain
->use_prime_blit
) {
869 /* If we are using prime blits, we need to perform the blit now. The
870 * command buffer is attached to the image.
872 struct wsi_image
*image
=
873 swapchain
->get_wsi_image(swapchain
, pPresentInfo
->pImageIndices
[i
]);
874 submit_info
.commandBufferCount
= 1;
875 submit_info
.pCommandBuffers
=
876 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
879 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[0]);
880 vk_free(&swapchain
->alloc
, stage_flags
);
881 if (result
!= VK_SUCCESS
)
884 const VkPresentRegionKHR
*region
= NULL
;
885 if (regions
&& regions
->pRegions
)
886 region
= ®ions
->pRegions
[i
];
888 result
= swapchain
->queue_present(swapchain
,
889 pPresentInfo
->pImageIndices
[i
],
891 if (result
!= VK_SUCCESS
)
894 VkFence last
= swapchain
->fences
[2];
895 swapchain
->fences
[2] = swapchain
->fences
[1];
896 swapchain
->fences
[1] = swapchain
->fences
[0];
897 swapchain
->fences
[0] = last
;
899 if (last
!= VK_NULL_HANDLE
) {
900 wsi
->WaitForFences(device
, 1, &last
, true, 1);
904 if (pPresentInfo
->pResults
!= NULL
)
905 pPresentInfo
->pResults
[i
] = result
;
907 /* Let the final result be our first unsuccessful result */
908 if (final_result
== VK_SUCCESS
)
909 final_result
= result
;