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 "util/macros.h"
29 wsi_device_init(struct wsi_device
*wsi
,
30 VkPhysicalDevice pdevice
,
31 WSI_FN_GetPhysicalDeviceProcAddr proc_addr
)
33 memset(wsi
, 0, sizeof(*wsi
));
35 #define WSI_GET_CB(func) \
36 PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
37 WSI_GET_CB(GetPhysicalDeviceMemoryProperties
);
38 WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties
);
41 GetPhysicalDeviceMemoryProperties(pdevice
, &wsi
->memory_props
);
42 GetPhysicalDeviceQueueFamilyProperties(pdevice
, &wsi
->queue_family_count
, NULL
);
44 #define WSI_GET_CB(func) \
45 wsi->func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
46 WSI_GET_CB(AllocateMemory
);
47 WSI_GET_CB(AllocateCommandBuffers
);
48 WSI_GET_CB(BindBufferMemory
);
49 WSI_GET_CB(BindImageMemory
);
50 WSI_GET_CB(BeginCommandBuffer
);
51 WSI_GET_CB(CmdCopyImageToBuffer
);
52 WSI_GET_CB(CreateBuffer
);
53 WSI_GET_CB(CreateCommandPool
);
54 WSI_GET_CB(CreateFence
);
55 WSI_GET_CB(CreateImage
);
56 WSI_GET_CB(DestroyBuffer
);
57 WSI_GET_CB(DestroyCommandPool
);
58 WSI_GET_CB(DestroyFence
);
59 WSI_GET_CB(DestroyImage
);
60 WSI_GET_CB(EndCommandBuffer
);
61 WSI_GET_CB(FreeMemory
);
62 WSI_GET_CB(FreeCommandBuffers
);
63 WSI_GET_CB(GetBufferMemoryRequirements
);
64 WSI_GET_CB(GetImageMemoryRequirements
);
65 WSI_GET_CB(GetImageSubresourceLayout
);
66 WSI_GET_CB(GetMemoryFdKHR
);
67 WSI_GET_CB(GetPhysicalDeviceFormatProperties
);
68 WSI_GET_CB(ResetFences
);
69 WSI_GET_CB(QueueSubmit
);
70 WSI_GET_CB(WaitForFences
);
75 wsi_swapchain_init(const struct wsi_device
*wsi
,
76 struct wsi_swapchain
*chain
,
78 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
79 const VkAllocationCallbacks
*pAllocator
)
83 memset(chain
, 0, sizeof(*chain
));
86 chain
->device
= device
;
87 chain
->alloc
= *pAllocator
;
88 chain
->use_prime_blit
= false;
91 vk_zalloc(pAllocator
, sizeof(VkCommandPool
) * wsi
->queue_family_count
, 8,
92 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
93 if (!chain
->cmd_pools
)
94 return VK_ERROR_OUT_OF_HOST_MEMORY
;
96 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
97 const VkCommandPoolCreateInfo cmd_pool_info
= {
98 .sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
,
101 .queueFamilyIndex
= i
,
103 result
= wsi
->CreateCommandPool(device
, &cmd_pool_info
, &chain
->alloc
,
104 &chain
->cmd_pools
[i
]);
105 if (result
!= VK_SUCCESS
)
112 wsi_swapchain_finish(chain
);
117 wsi_swapchain_finish(struct wsi_swapchain
*chain
)
119 for (unsigned i
= 0; i
< ARRAY_SIZE(chain
->fences
); i
++)
120 chain
->wsi
->DestroyFence(chain
->device
, chain
->fences
[i
], &chain
->alloc
);
122 for (uint32_t i
= 0; i
< chain
->wsi
->queue_family_count
; i
++) {
123 chain
->wsi
->DestroyCommandPool(chain
->device
, chain
->cmd_pools
[i
],
129 select_memory_type(const struct wsi_device
*wsi
,
130 VkMemoryPropertyFlags props
,
133 for (uint32_t i
= 0; i
< wsi
->memory_props
.memoryTypeCount
; i
++) {
134 const VkMemoryType type
= wsi
->memory_props
.memoryTypes
[i
];
135 if ((type_bits
& (1 << i
)) && (type
.propertyFlags
& props
) == props
)
139 unreachable("No memory type found");
143 vk_format_size(VkFormat format
)
146 case VK_FORMAT_B8G8R8A8_UNORM
:
147 case VK_FORMAT_B8G8R8A8_SRGB
:
150 unreachable("Unknown WSI Format");
154 static inline uint32_t
155 align_u32(uint32_t v
, uint32_t a
)
157 assert(a
!= 0 && a
== (a
& -a
));
158 return (v
+ a
- 1) & ~(a
- 1);
162 wsi_create_native_image(const struct wsi_swapchain
*chain
,
163 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
164 struct wsi_image
*image
)
166 const struct wsi_device
*wsi
= chain
->wsi
;
169 memset(image
, 0, sizeof(*image
));
171 const struct wsi_image_create_info image_wsi_info
= {
172 .sType
= VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA
,
176 const VkImageCreateInfo image_info
= {
177 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
178 .pNext
= &image_wsi_info
,
180 .imageType
= VK_IMAGE_TYPE_2D
,
181 .format
= pCreateInfo
->imageFormat
,
183 .width
= pCreateInfo
->imageExtent
.width
,
184 .height
= pCreateInfo
->imageExtent
.height
,
189 .samples
= VK_SAMPLE_COUNT_1_BIT
,
190 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
191 .usage
= pCreateInfo
->imageUsage
,
192 .sharingMode
= pCreateInfo
->imageSharingMode
,
193 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
194 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
195 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
197 result
= wsi
->CreateImage(chain
->device
, &image_info
,
198 &chain
->alloc
, &image
->image
);
199 if (result
!= VK_SUCCESS
)
202 VkMemoryRequirements reqs
;
203 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
205 VkSubresourceLayout image_layout
;
206 const VkImageSubresource image_subresource
= {
207 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
211 wsi
->GetImageSubresourceLayout(chain
->device
, image
->image
,
212 &image_subresource
, &image_layout
);
214 const struct wsi_memory_allocate_info memory_wsi_info
= {
215 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
217 .implicit_sync
= true,
219 const VkExportMemoryAllocateInfoKHR memory_export_info
= {
220 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
221 .pNext
= &memory_wsi_info
,
222 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
224 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
225 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
226 .pNext
= &memory_export_info
,
227 .image
= image
->image
,
228 .buffer
= VK_NULL_HANDLE
,
230 const VkMemoryAllocateInfo memory_info
= {
231 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
232 .pNext
= &memory_dedicated_info
,
233 .allocationSize
= reqs
.size
,
234 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
235 reqs
.memoryTypeBits
),
237 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
238 &chain
->alloc
, &image
->memory
);
239 if (result
!= VK_SUCCESS
)
242 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
244 if (result
!= VK_SUCCESS
)
247 const VkMemoryGetFdInfoKHR memory_get_fd_info
= {
248 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
250 .memory
= image
->memory
,
251 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
254 result
= wsi
->GetMemoryFdKHR(chain
->device
, &memory_get_fd_info
, &fd
);
255 if (result
!= VK_SUCCESS
)
258 image
->size
= reqs
.size
;
259 image
->row_pitch
= image_layout
.rowPitch
;
266 wsi_destroy_image(chain
, image
);
271 #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
274 wsi_create_prime_image(const struct wsi_swapchain
*chain
,
275 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
276 struct wsi_image
*image
)
278 const struct wsi_device
*wsi
= chain
->wsi
;
281 memset(image
, 0, sizeof(*image
));
283 const uint32_t cpp
= vk_format_size(pCreateInfo
->imageFormat
);
284 const uint32_t linear_stride
= align_u32(pCreateInfo
->imageExtent
.width
* cpp
,
285 WSI_PRIME_LINEAR_STRIDE_ALIGN
);
287 uint32_t linear_size
= linear_stride
* pCreateInfo
->imageExtent
.height
;
288 linear_size
= align_u32(linear_size
, 4096);
290 const VkExternalMemoryBufferCreateInfoKHR prime_buffer_external_info
= {
291 .sType
= VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR
,
293 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
295 const VkBufferCreateInfo prime_buffer_info
= {
296 .sType
= VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
,
297 .pNext
= &prime_buffer_external_info
,
299 .usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
,
300 .sharingMode
= VK_SHARING_MODE_EXCLUSIVE
,
302 result
= wsi
->CreateBuffer(chain
->device
, &prime_buffer_info
,
303 &chain
->alloc
, &image
->prime
.buffer
);
304 if (result
!= VK_SUCCESS
)
307 VkMemoryRequirements reqs
;
308 wsi
->GetBufferMemoryRequirements(chain
->device
, image
->prime
.buffer
, &reqs
);
309 assert(reqs
.size
<= linear_size
);
311 const struct wsi_memory_allocate_info memory_wsi_info
= {
312 .sType
= VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA
,
314 .implicit_sync
= true,
316 const VkExportMemoryAllocateInfoKHR prime_memory_export_info
= {
317 .sType
= VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR
,
318 .pNext
= &memory_wsi_info
,
319 .handleTypes
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
321 const VkMemoryDedicatedAllocateInfoKHR prime_memory_dedicated_info
= {
322 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
323 .pNext
= &prime_memory_export_info
,
324 .image
= VK_NULL_HANDLE
,
325 .buffer
= image
->prime
.buffer
,
327 const VkMemoryAllocateInfo prime_memory_info
= {
328 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
329 .pNext
= &prime_memory_dedicated_info
,
330 .allocationSize
= linear_size
,
331 .memoryTypeIndex
= select_memory_type(wsi
, 0, reqs
.memoryTypeBits
),
333 result
= wsi
->AllocateMemory(chain
->device
, &prime_memory_info
,
334 &chain
->alloc
, &image
->prime
.memory
);
335 if (result
!= VK_SUCCESS
)
338 result
= wsi
->BindBufferMemory(chain
->device
, image
->prime
.buffer
,
339 image
->prime
.memory
, 0);
340 if (result
!= VK_SUCCESS
)
343 const VkImageCreateInfo image_info
= {
344 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
347 .imageType
= VK_IMAGE_TYPE_2D
,
348 .format
= pCreateInfo
->imageFormat
,
350 .width
= pCreateInfo
->imageExtent
.width
,
351 .height
= pCreateInfo
->imageExtent
.height
,
356 .samples
= VK_SAMPLE_COUNT_1_BIT
,
357 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
358 .usage
= pCreateInfo
->imageUsage
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
,
359 .sharingMode
= pCreateInfo
->imageSharingMode
,
360 .queueFamilyIndexCount
= pCreateInfo
->queueFamilyIndexCount
,
361 .pQueueFamilyIndices
= pCreateInfo
->pQueueFamilyIndices
,
362 .initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
,
364 result
= wsi
->CreateImage(chain
->device
, &image_info
,
365 &chain
->alloc
, &image
->image
);
366 if (result
!= VK_SUCCESS
)
369 wsi
->GetImageMemoryRequirements(chain
->device
, image
->image
, &reqs
);
371 const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info
= {
372 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
374 .image
= image
->image
,
375 .buffer
= VK_NULL_HANDLE
,
377 const VkMemoryAllocateInfo memory_info
= {
378 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
379 .pNext
= &memory_dedicated_info
,
380 .allocationSize
= reqs
.size
,
381 .memoryTypeIndex
= select_memory_type(wsi
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
382 reqs
.memoryTypeBits
),
384 result
= wsi
->AllocateMemory(chain
->device
, &memory_info
,
385 &chain
->alloc
, &image
->memory
);
386 if (result
!= VK_SUCCESS
)
389 result
= wsi
->BindImageMemory(chain
->device
, image
->image
,
391 if (result
!= VK_SUCCESS
)
394 image
->prime
.blit_cmd_buffers
=
395 vk_zalloc(&chain
->alloc
,
396 sizeof(VkCommandBuffer
) * wsi
->queue_family_count
, 8,
397 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
398 if (!image
->prime
.blit_cmd_buffers
)
401 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
402 const VkCommandBufferAllocateInfo cmd_buffer_info
= {
403 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
,
405 .commandPool
= chain
->cmd_pools
[i
],
406 .level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
,
407 .commandBufferCount
= 1,
409 result
= wsi
->AllocateCommandBuffers(chain
->device
, &cmd_buffer_info
,
410 &image
->prime
.blit_cmd_buffers
[i
]);
411 if (result
!= VK_SUCCESS
)
414 const VkCommandBufferBeginInfo begin_info
= {
415 .sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
,
417 wsi
->BeginCommandBuffer(image
->prime
.blit_cmd_buffers
[i
], &begin_info
);
419 struct VkBufferImageCopy buffer_image_copy
= {
421 .bufferRowLength
= linear_stride
/ cpp
,
422 .bufferImageHeight
= 0,
423 .imageSubresource
= {
424 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
429 .imageOffset
= { .x
= 0, .y
= 0, .z
= 0 },
431 .width
= pCreateInfo
->imageExtent
.width
,
432 .height
= pCreateInfo
->imageExtent
.height
,
436 wsi
->CmdCopyImageToBuffer(image
->prime
.blit_cmd_buffers
[i
],
438 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
,
440 1, &buffer_image_copy
);
442 result
= wsi
->EndCommandBuffer(image
->prime
.blit_cmd_buffers
[i
]);
443 if (result
!= VK_SUCCESS
)
447 const VkMemoryGetFdInfoKHR linear_memory_get_fd_info
= {
448 .sType
= VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
,
450 .memory
= image
->prime
.memory
,
451 .handleType
= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
,
454 result
= wsi
->GetMemoryFdKHR(chain
->device
, &linear_memory_get_fd_info
, &fd
);
455 if (result
!= VK_SUCCESS
)
458 image
->size
= linear_size
;
459 image
->row_pitch
= linear_stride
;
466 wsi_destroy_image(chain
, image
);
472 wsi_destroy_image(const struct wsi_swapchain
*chain
,
473 struct wsi_image
*image
)
475 const struct wsi_device
*wsi
= chain
->wsi
;
477 if (image
->prime
.blit_cmd_buffers
) {
478 for (uint32_t i
= 0; i
< wsi
->queue_family_count
; i
++) {
479 wsi
->FreeCommandBuffers(chain
->device
, chain
->cmd_pools
[i
],
480 1, &image
->prime
.blit_cmd_buffers
[i
]);
482 vk_free(&chain
->alloc
, image
->prime
.blit_cmd_buffers
);
485 wsi
->FreeMemory(chain
->device
, image
->memory
, &chain
->alloc
);
486 wsi
->DestroyImage(chain
->device
, image
->image
, &chain
->alloc
);
487 wsi
->FreeMemory(chain
->device
, image
->prime
.memory
, &chain
->alloc
);
488 wsi
->DestroyBuffer(chain
->device
, image
->prime
.buffer
, &chain
->alloc
);
492 wsi_common_get_surface_support(struct wsi_device
*wsi_device
,
494 uint32_t queueFamilyIndex
,
495 VkSurfaceKHR _surface
,
496 const VkAllocationCallbacks
*alloc
,
497 VkBool32
* pSupported
)
499 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
500 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
502 return iface
->get_support(surface
, wsi_device
, alloc
,
503 queueFamilyIndex
, local_fd
, pSupported
);
507 wsi_common_get_surface_capabilities(struct wsi_device
*wsi_device
,
508 VkSurfaceKHR _surface
,
509 VkSurfaceCapabilitiesKHR
*pSurfaceCapabilities
)
511 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
512 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
514 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
518 wsi_common_get_surface_capabilities2(struct wsi_device
*wsi_device
,
519 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
520 VkSurfaceCapabilities2KHR
*pSurfaceCapabilities
)
522 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
523 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
525 return iface
->get_capabilities2(surface
, pSurfaceInfo
->pNext
,
526 pSurfaceCapabilities
);
530 wsi_common_get_surface_formats(struct wsi_device
*wsi_device
,
531 VkSurfaceKHR _surface
,
532 uint32_t *pSurfaceFormatCount
,
533 VkSurfaceFormatKHR
*pSurfaceFormats
)
535 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
536 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
538 return iface
->get_formats(surface
, wsi_device
,
539 pSurfaceFormatCount
, pSurfaceFormats
);
543 wsi_common_get_surface_formats2(struct wsi_device
*wsi_device
,
544 const VkPhysicalDeviceSurfaceInfo2KHR
*pSurfaceInfo
,
545 uint32_t *pSurfaceFormatCount
,
546 VkSurfaceFormat2KHR
*pSurfaceFormats
)
548 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
549 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
551 return iface
->get_formats2(surface
, wsi_device
, pSurfaceInfo
->pNext
,
552 pSurfaceFormatCount
, pSurfaceFormats
);
556 wsi_common_get_surface_present_modes(struct wsi_device
*wsi_device
,
557 VkSurfaceKHR _surface
,
558 uint32_t *pPresentModeCount
,
559 VkPresentModeKHR
*pPresentModes
)
561 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
562 struct wsi_interface
*iface
= wsi_device
->wsi
[surface
->platform
];
564 return iface
->get_present_modes(surface
, pPresentModeCount
,
569 wsi_common_create_swapchain(struct wsi_device
*wsi
,
572 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
573 const VkAllocationCallbacks
*pAllocator
,
574 VkSwapchainKHR
*pSwapchain
)
576 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
577 struct wsi_interface
*iface
= wsi
->wsi
[surface
->platform
];
578 struct wsi_swapchain
*swapchain
;
580 VkResult result
= iface
->create_swapchain(surface
, device
, wsi
, fd
,
581 pCreateInfo
, pAllocator
,
583 if (result
!= VK_SUCCESS
)
586 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
592 wsi_common_destroy_swapchain(VkDevice device
,
593 VkSwapchainKHR _swapchain
,
594 const VkAllocationCallbacks
*pAllocator
)
596 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
600 swapchain
->destroy(swapchain
, pAllocator
);
604 wsi_common_get_images(VkSwapchainKHR _swapchain
,
605 uint32_t *pSwapchainImageCount
,
606 VkImage
*pSwapchainImages
)
608 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
609 VK_OUTARRAY_MAKE(images
, pSwapchainImages
, pSwapchainImageCount
);
611 for (uint32_t i
= 0; i
< swapchain
->image_count
; i
++) {
612 vk_outarray_append(&images
, image
) {
613 *image
= swapchain
->get_wsi_image(swapchain
, i
)->image
;
617 return vk_outarray_status(&images
);
621 wsi_common_acquire_next_image(const struct wsi_device
*wsi
,
623 VkSwapchainKHR _swapchain
,
625 VkSemaphore semaphore
,
626 uint32_t *pImageIndex
)
628 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
630 return swapchain
->acquire_next_image(swapchain
, timeout
,
631 semaphore
, pImageIndex
);
635 wsi_common_queue_present(const struct wsi_device
*wsi
,
638 int queue_family_index
,
639 const VkPresentInfoKHR
*pPresentInfo
)
641 VkResult final_result
= VK_SUCCESS
;
643 const VkPresentRegionsKHR
*regions
=
644 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
646 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
647 WSI_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
650 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
651 const VkFenceCreateInfo fence_info
= {
652 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
656 result
= wsi
->CreateFence(device
, &fence_info
,
658 &swapchain
->fences
[0]);
659 if (result
!= VK_SUCCESS
)
662 wsi
->ResetFences(device
, 1, &swapchain
->fences
[0]);
665 VkSubmitInfo submit_info
= {
666 .sType
= VK_STRUCTURE_TYPE_SUBMIT_INFO
,
670 VkPipelineStageFlags
*stage_flags
= NULL
;
672 /* We only need/want to wait on semaphores once. After that, we're
673 * guaranteed ordering since it all happens on the same queue.
675 submit_info
.waitSemaphoreCount
= pPresentInfo
->waitSemaphoreCount
,
676 submit_info
.pWaitSemaphores
= pPresentInfo
->pWaitSemaphores
,
678 /* Set up the pWaitDstStageMasks */
679 stage_flags
= vk_alloc(&swapchain
->alloc
,
680 sizeof(VkPipelineStageFlags
) *
681 pPresentInfo
->waitSemaphoreCount
,
683 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
685 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
688 for (uint32_t s
= 0; s
< pPresentInfo
->waitSemaphoreCount
; s
++)
689 stage_flags
[s
] = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
;
691 submit_info
.pWaitDstStageMask
= stage_flags
;
694 if (swapchain
->use_prime_blit
) {
695 /* If we are using prime blits, we need to perform the blit now. The
696 * command buffer is attached to the image.
698 struct wsi_image
*image
=
699 swapchain
->get_wsi_image(swapchain
, pPresentInfo
->pImageIndices
[i
]);
700 submit_info
.commandBufferCount
= 1;
701 submit_info
.pCommandBuffers
=
702 &image
->prime
.blit_cmd_buffers
[queue_family_index
];
705 result
= wsi
->QueueSubmit(queue
, 1, &submit_info
, swapchain
->fences
[0]);
706 vk_free(&swapchain
->alloc
, stage_flags
);
707 if (result
!= VK_SUCCESS
)
710 const VkPresentRegionKHR
*region
= NULL
;
711 if (regions
&& regions
->pRegions
)
712 region
= ®ions
->pRegions
[i
];
714 result
= swapchain
->queue_present(swapchain
,
715 pPresentInfo
->pImageIndices
[i
],
717 if (result
!= VK_SUCCESS
)
720 VkFence last
= swapchain
->fences
[2];
721 swapchain
->fences
[2] = swapchain
->fences
[1];
722 swapchain
->fences
[1] = swapchain
->fences
[0];
723 swapchain
->fences
[0] = last
;
725 if (last
!= VK_NULL_HANDLE
) {
726 wsi
->WaitForFences(device
, 1, &last
, true, 1);
730 if (pPresentInfo
->pResults
!= NULL
)
731 pPresentInfo
->pResults
[i
] = result
;
733 /* Let the final result be our first unsuccessful result */
734 if (final_result
== VK_SUCCESS
)
735 final_result
= result
;