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
) {
91 #ifdef VK_USE_PLATFORM_XCB_KHR
92 wsi_x11_finish_wsi(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
,
445 struct wsi_image
*image
)
447 const struct wsi_device
*wsi
= chain
->wsi
;
450 memset(image
, 0, sizeof(*image
));
452 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
453 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
454 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
456 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
457 linear_size
= align_u32(linear_size
, 4096);
459 const VkExternalMemoryBufferCreateInfoKHR prime_buffer_external_info
= {
460 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR
,
462 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
464 const VkBufferCreateInfo prime_buffer_info
= {
465 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
466 .pNext
= &prime_buffer_external_info
,
468 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
469 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
471 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
472 &chain
->alloc
, &image
->prime
.buffer
);
473 if (result
!= VK_SUCCESS
)
476 VkMemoryRequirements reqs
;
477 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
478 assert(reqs
.size
<= linear_size
);
480 const struct wsi_memory_allocate_info memory_wsi_info
= {
481 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
483 .implicit_sync
= true,
485 const VkExportMemoryAllocateInfoKHR prime_memory_export_info
= {
486 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
487 .pNext
= &memory_wsi_info
,
488 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
490 const VkMemoryDedicatedAllocateInfoKHR prime_memory_dedicated_info
= {
491 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
492 .pNext
= &prime_memory_export_info
,
493 .image
= VK_NULL_HANDLE
,
494 .buffer
= image
->prime
.buffer
,
496 const VkMemoryAllocateInfo prime_memory_info
= {
497 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
498 .pNext
= &prime_memory_dedicated_info
,
499 .allocationSize
= linear_size
,
500 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
502 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
503 &chain
->alloc
, &image
->prime
.memory
);
504 if (result
!= VK_SUCCESS
)
507 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
508 image
->prime
.memory
, 0);
509 if (result
!= VK_SUCCESS
)
512 const VkImageCreateInfo image_info
= {
513 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
516 .imageType
= VK_IMAGE_TYPE_2D
,
517 .format
= pCreateInfo
->imageFormat
,
519 .width
= pCreateInfo
->imageExtent
.width
,
520 .height
= pCreateInfo
->imageExtent
.height
,
525 .samples
= VK_SAMPLE_COUNT_1_BIT
,
526 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
527 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
528 .sharingMode
= pCreateInfo
->imageSharingMode
,
529 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
530 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
531 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
533 result
= wsi
->CreateImage(chain
->device
, &image_info
,
534 &chain
->alloc
, &image
->image
);
535 if (result
!= VK_SUCCESS
)
538 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
540 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
541 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
543 .image
= image
->image
,
544 .buffer
= VK_NULL_HANDLE
,
546 const VkMemoryAllocateInfo memory_info
= {
547 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
548 .pNext
= &memory_dedicated_info
,
549 .allocationSize
= reqs
.size
,
550 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
551 reqs
.memoryTypeBits
),
553 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
554 &chain
->alloc
, &image
->memory
);
555 if (result
!= VK_SUCCESS
)
558 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
560 if (result
!= VK_SUCCESS
)
563 image
->prime
.blit_cmd_buffers
=
564 vk_zalloc(&chain
->alloc
,
565 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
566 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
567 if (!image
->prime
.blit_cmd_buffers
) {
568 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
572 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
573 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
574 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
576 .commandPool
= chain
->cmd_pools
[i
],
577 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
578 .commandBufferCount
= 1,
580 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
581 &image
->prime
.blit_cmd_buffers
[i
]);
582 if (result
!= VK_SUCCESS
)
585 const VkCommandBufferBeginInfo begin_info
= {
586 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
588 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
590 struct VkBufferImageCopy buffer_image_copy
= {
592 .bufferRowLength
= linear_stride
/ cpp
,
593 .bufferImageHeight
= 0,
594 .imageSubresource
= {
595 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
600 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
602 .width
= pCreateInfo
->imageExtent
.width
,
603 .height
= pCreateInfo
->imageExtent
.height
,
607 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
609 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
611 1, &buffer_image_copy
);
613 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
614 if (result
!= VK_SUCCESS
)
618 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
619 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
621 .memory
= image
->prime
.memory
,
622 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
625 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
626 if (result
!= VK_SUCCESS
)
629 image
->drm_modifier
= DRM_FORMAT_MOD_LINEAR
;
630 image
->num_planes
= 1;
631 image
->sizes
[0] = linear_size
;
632 image
->row_pitches
[0] = linear_stride
;
633 image
->offsets
[0] = 0;
639 wsi_destroy_image(chain
, image
);
645 wsi_destroy_image(const struct wsi_swapchain
*chain
,
646 struct wsi_image
*image
)
648 const struct wsi_device
*wsi
= chain
->wsi
;
650 if (image
->prime
.blit_cmd_buffers
) {
651 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
652 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
653 1, &image
->prime
.blit_cmd_buffers
[i
]);
655 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
658 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
659 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
660 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
661 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
665 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
667 uint32_t queueFamilyIndex
,
668 VkSurfaceKHR _surface
,
669 const VkAllocationCallbacks
*alloc
,
670 VkBool32
* pSupported
)
672 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
673 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
675 return iface
->get_support(surface
, wsi_device
, alloc
,
676 queueFamilyIndex
, local_fd
, pSupported
);
680 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
681 VkSurfaceKHR _surface
,
682 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
684 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
685 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
687 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
691 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
692 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
693 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
695 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
696 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
698 return iface
->get_capabilities2(surface
, pSurfaceInfo
->pNext
,
699 pSurfaceCapabilities
);
703 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
704 VkSurfaceKHR _surface
,
705 uint32_t *pSurfaceFormatCount
,
706 VkSurfaceFormatKHR
*pSurfaceFormats
)
708 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
709 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
711 return iface
->get_formats(surface
, wsi_device
,
712 pSurfaceFormatCount
, pSurfaceFormats
);
716 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
717 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
718 uint32_t *pSurfaceFormatCount
,
719 VkSurfaceFormat2KHR
*pSurfaceFormats
)
721 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
722 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
724 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
725 pSurfaceFormatCount
, pSurfaceFormats
);
729 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
730 VkSurfaceKHR _surface
,
731 uint32_t *pPresentModeCount
,
732 VkPresentModeKHR
*pPresentModes
)
734 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
735 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
737 return iface
->get_present_modes(surface
, pPresentModeCount
,
742 wsi_common_create_swapchain(struct wsi_device
*wsi
,
745 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
746 const VkAllocationCallbacks
*pAllocator
,
747 VkSwapchainKHR
*pSwapchain
)
749 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
750 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
751 struct wsi_swapchain
*swapchain
;
753 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
, fd
,
754 pCreateInfo
, pAllocator
,
756 if (result
!= VK_SUCCESS
)
759 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
765 wsi_common_destroy_swapchain(VkDevice device
,
766 VkSwapchainKHR _swapchain
,
767 const VkAllocationCallbacks
*pAllocator
)
769 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
773 swapchain
->destroy(swapchain
, pAllocator
);
777 wsi_common_get_images(VkSwapchainKHR _swapchain
,
778 uint32_t *pSwapchainImageCount
,
779 VkImage
*pSwapchainImages
)
781 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
782 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
784 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
785 vk_outarray_append(&images
, image
) {
786 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
790 return vk_outarray_status(&images
);
794 wsi_common_acquire_next_image(const struct wsi_device
*wsi
,
796 VkSwapchainKHR _swapchain
,
798 VkSemaphore semaphore
,
799 uint32_t *pImageIndex
)
801 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
803 return swapchain
->acquire_next_image(swapchain
, timeout
,
804 semaphore
, pImageIndex
);
808 wsi_common_queue_present(const struct wsi_device
*wsi
,
811 int queue_family_index
,
812 const VkPresentInfoKHR
*pPresentInfo
)
814 VkResult final_result
= VK_SUCCESS
;
816 const VkPresentRegionsKHR
*regions
=
817 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
819 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
820 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
823 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
824 const VkFenceCreateInfo fence_info
= {
825 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
829 result
= wsi
->CreateFence(device
, &fence_info
,
831 &swapchain
->fences
[0]);
832 if (result
!= VK_SUCCESS
)
835 wsi
->ResetFences(device
, 1, &swapchain
->fences
[0]);
838 VkSubmitInfo submit_info
= {
839 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
843 VkPipelineStageFlags
*stage_flags
= NULL
;
845 /* We only need/want to wait on semaphores once. After that, we're
846 * guaranteed ordering since it all happens on the same queue.
848 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
,
849 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
,
851 /* Set up the pWaitDstStageMasks */
852 stage_flags
= vk_alloc(&swapchain
->alloc
,
853 sizeof(VkPipelineStageFlags
) *
854 pPresentInfo
->waitSemaphoreCount
,
856 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
858 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
861 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
862 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
864 submit_info
.pWaitDstStageMask
= stage_flags
;
867 if (swapchain
->use_prime_blit
) {
868 /* If we are using prime blits, we need to perform the blit now. The
869 * command buffer is attached to the image.
871 struct wsi_image
*image
=
872 swapchain
->get_wsi_image(swapchain
, pPresentInfo
->pImageIndices
[i
]);
873 submit_info
.commandBufferCount
= 1;
874 submit_info
.pCommandBuffers
=
875 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
878 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[0]);
879 vk_free(&swapchain
->alloc
, stage_flags
);
880 if (result
!= VK_SUCCESS
)
883 const VkPresentRegionKHR
*region
= NULL
;
884 if (regions
&& regions
->pRegions
)
885 region
= ®ions
->pRegions
[i
];
887 result
= swapchain
->queue_present(swapchain
,
888 pPresentInfo
->pImageIndices
[i
],
890 if (result
!= VK_SUCCESS
)
893 VkFence last
= swapchain
->fences
[2];
894 swapchain
->fences
[2] = swapchain
->fences
[1];
895 swapchain
->fences
[1] = swapchain
->fences
[0];
896 swapchain
->fences
[0] = last
;
898 if (last
!= VK_NULL_HANDLE
) {
899 wsi
->WaitForFences(device
, 1, &last
, true, 1);
903 if (pPresentInfo
->pResults
!= NULL
)
904 pPresentInfo
->pResults
[i
] = result
;
906 /* Let the final result be our first unsuccessful result */
907 if (final_result
== VK_SUCCESS
)
908 final_result
= result
;