2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "radv_private.h"
27 #include "radv_meta.h"
28 #include "wsi_common.h"
31 static const struct wsi_callbacks wsi_cbs
= {
32 .get_phys_device_format_properties
= radv_GetPhysicalDeviceFormatProperties
,
36 radv_init_wsi(struct radv_physical_device
*physical_device
)
40 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
42 #ifdef VK_USE_PLATFORM_XCB_KHR
43 result
= wsi_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
44 if (result
!= VK_SUCCESS
)
48 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
49 result
= wsi_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
50 radv_physical_device_to_handle(physical_device
),
52 if (result
!= VK_SUCCESS
) {
53 #ifdef VK_USE_PLATFORM_XCB_KHR
54 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
64 radv_finish_wsi(struct radv_physical_device
*physical_device
)
66 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
67 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
69 #ifdef VK_USE_PLATFORM_XCB_KHR
70 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
74 void radv_DestroySurfaceKHR(
76 VkSurfaceKHR _surface
,
77 const VkAllocationCallbacks
* pAllocator
)
79 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
80 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
82 vk_free2(&instance
->alloc
, pAllocator
, surface
);
85 VkResult
radv_GetPhysicalDeviceSurfaceSupportKHR(
86 VkPhysicalDevice physicalDevice
,
87 uint32_t queueFamilyIndex
,
88 VkSurfaceKHR _surface
,
91 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
92 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
93 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
95 return iface
->get_support(surface
, &device
->wsi_device
,
96 &device
->instance
->alloc
,
97 queueFamilyIndex
, device
->local_fd
, true, pSupported
);
100 VkResult
radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
101 VkPhysicalDevice physicalDevice
,
102 VkSurfaceKHR _surface
,
103 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
105 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
106 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
107 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
109 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
112 VkResult
radv_GetPhysicalDeviceSurfaceFormatsKHR(
113 VkPhysicalDevice physicalDevice
,
114 VkSurfaceKHR _surface
,
115 uint32_t* pSurfaceFormatCount
,
116 VkSurfaceFormatKHR
* pSurfaceFormats
)
118 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
119 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
120 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
122 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
126 VkResult
radv_GetPhysicalDeviceSurfacePresentModesKHR(
127 VkPhysicalDevice physicalDevice
,
128 VkSurfaceKHR _surface
,
129 uint32_t* pPresentModeCount
,
130 VkPresentModeKHR
* pPresentModes
)
132 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
133 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
134 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
136 return iface
->get_present_modes(surface
, pPresentModeCount
,
141 radv_wsi_image_create(VkDevice device_h
,
142 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
143 const VkAllocationCallbacks
* pAllocator
,
144 bool needs_linear_copy
,
147 VkDeviceMemory
*memory_p
,
150 uint32_t *row_pitch
, int *fd_p
)
152 VkResult result
= VK_SUCCESS
;
153 struct radeon_surf
*surface
;
155 struct radv_image
*image
;
157 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
159 result
= radv_image_create(device_h
,
160 &(struct radv_image_create_info
) {
162 &(VkImageCreateInfo
) {
163 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
164 .imageType
= VK_IMAGE_TYPE_2D
,
165 .format
= pCreateInfo
->imageFormat
,
167 .width
= pCreateInfo
->imageExtent
.width
,
168 .height
= pCreateInfo
->imageExtent
.height
,
174 /* FIXME: Need a way to use X tiling to allow scanout */
175 .tiling
= linear
? VK_IMAGE_TILING_LINEAR
: VK_IMAGE_TILING_OPTIMAL
,
176 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
182 if (result
!= VK_SUCCESS
)
185 image
= radv_image_from_handle(image_h
);
187 VkDeviceMemory memory_h
;
189 const VkMemoryDedicatedAllocateInfoKHR ded_alloc
= {
190 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
192 .buffer
= VK_NULL_HANDLE
,
196 result
= radv_AllocateMemory(device_h
,
197 &(VkMemoryAllocateInfo
) {
198 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
200 .allocationSize
= image
->size
,
201 .memoryTypeIndex
= linear
? 1 : 0,
203 NULL
/* XXX: pAllocator */,
205 if (result
!= VK_SUCCESS
)
206 goto fail_create_image
;
208 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
211 * return the fd for the image in the no copy mode,
212 * or the fd for the linear image if a copy is required.
214 if (!needs_linear_copy
|| (needs_linear_copy
&& linear
)) {
215 RADV_FROM_HANDLE(radv_device_memory
, memory
, memory_h
);
216 if (!radv_get_memory_fd(device
, memory
, &fd
))
217 goto fail_alloc_memory
;
221 surface
= &image
->surface
;
224 *memory_p
= memory_h
;
226 *offset
= image
->offset
;
228 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
229 *row_pitch
= surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
231 *row_pitch
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
234 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
237 radv_DestroyImage(device_h
, image_h
, pAllocator
);
243 radv_wsi_image_free(VkDevice device
,
244 const VkAllocationCallbacks
* pAllocator
,
246 VkDeviceMemory memory_h
)
248 radv_DestroyImage(device
, image_h
, pAllocator
);
250 radv_FreeMemory(device
, memory_h
, pAllocator
);
253 static const struct wsi_image_fns radv_wsi_image_fns
= {
254 .create_wsi_image
= radv_wsi_image_create
,
255 .free_wsi_image
= radv_wsi_image_free
,
258 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
260 radv_wsi_free_prime_command_buffers(struct radv_device
*device
,
261 struct wsi_swapchain
*swapchain
)
263 const int num_pools
= NUM_PRIME_POOLS
;
264 const int num_images
= swapchain
->image_count
;
266 for (i
= 0; i
< num_pools
; i
++) {
267 radv_FreeCommandBuffers(radv_device_to_handle(device
),
268 swapchain
->cmd_pools
[i
],
269 swapchain
->image_count
,
270 &swapchain
->cmd_buffers
[i
* num_images
]);
272 radv_DestroyCommandPool(radv_device_to_handle(device
),
273 swapchain
->cmd_pools
[i
],
279 radv_wsi_create_prime_command_buffers(struct radv_device
*device
,
280 const VkAllocationCallbacks
*alloc
,
281 struct wsi_swapchain
*swapchain
)
283 const int num_pools
= NUM_PRIME_POOLS
;
284 const int num_images
= swapchain
->image_count
;
285 int num_cmd_buffers
= num_images
* num_pools
; //TODO bump to MAX_QUEUE_FAMILIES
289 swapchain
->cmd_buffers
= vk_alloc(alloc
, (sizeof(VkCommandBuffer
) * num_cmd_buffers
), 8,
290 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
291 if (!swapchain
->cmd_buffers
)
292 return VK_ERROR_OUT_OF_HOST_MEMORY
;
294 memset(swapchain
->cmd_buffers
, 0, sizeof(VkCommandBuffer
) * num_cmd_buffers
);
295 memset(swapchain
->cmd_pools
, 0, sizeof(VkCommandPool
) * num_pools
);
296 for (i
= 0; i
< num_pools
; i
++) {
297 VkCommandPoolCreateInfo pool_create_info
;
299 pool_create_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
300 pool_create_info
.pNext
= NULL
;
301 pool_create_info
.flags
= 0;
302 pool_create_info
.queueFamilyIndex
= i
;
304 result
= radv_CreateCommandPool(radv_device_to_handle(device
),
305 &pool_create_info
, alloc
,
306 &swapchain
->cmd_pools
[i
]);
307 if (result
!= VK_SUCCESS
)
310 VkCommandBufferAllocateInfo cmd_buffer_info
;
311 cmd_buffer_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
312 cmd_buffer_info
.pNext
= NULL
;
313 cmd_buffer_info
.commandPool
= swapchain
->cmd_pools
[i
];
314 cmd_buffer_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
315 cmd_buffer_info
.commandBufferCount
= num_images
;
317 result
= radv_AllocateCommandBuffers(radv_device_to_handle(device
),
319 &swapchain
->cmd_buffers
[i
* num_images
]);
320 if (result
!= VK_SUCCESS
)
322 for (j
= 0; j
< num_images
; j
++) {
323 VkImage image
, linear_image
;
324 int idx
= (i
* num_images
) + j
;
326 swapchain
->get_image_and_linear(swapchain
, j
, &image
, &linear_image
);
327 VkCommandBufferBeginInfo begin_info
= {0};
329 begin_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
;
331 radv_BeginCommandBuffer(swapchain
->cmd_buffers
[idx
], &begin_info
);
333 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
]),
334 radv_image_from_handle(image
),
335 radv_image_from_handle(linear_image
));
337 radv_EndCommandBuffer(swapchain
->cmd_buffers
[idx
]);
342 radv_wsi_free_prime_command_buffers(device
, swapchain
);
346 VkResult
radv_CreateSwapchainKHR(
348 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
349 const VkAllocationCallbacks
* pAllocator
,
350 VkSwapchainKHR
* pSwapchain
)
352 RADV_FROM_HANDLE(radv_device
, device
, _device
);
353 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
354 struct wsi_interface
*iface
=
355 device
->physical_device
->wsi_device
.wsi
[surface
->platform
];
356 struct wsi_swapchain
*swapchain
;
357 const VkAllocationCallbacks
*alloc
;
361 alloc
= &device
->alloc
;
362 VkResult result
= iface
->create_swapchain(surface
, _device
,
363 &device
->physical_device
->wsi_device
,
364 device
->physical_device
->local_fd
,
366 alloc
, &radv_wsi_image_fns
,
368 if (result
!= VK_SUCCESS
)
372 swapchain
->alloc
= *pAllocator
;
374 swapchain
->alloc
= device
->alloc
;
376 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
377 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
379 if (swapchain
->needs_linear_copy
) {
380 result
= radv_wsi_create_prime_command_buffers(device
, alloc
,
382 if (result
!= VK_SUCCESS
)
386 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
391 void radv_DestroySwapchainKHR(
393 VkSwapchainKHR _swapchain
,
394 const VkAllocationCallbacks
* pAllocator
)
396 RADV_FROM_HANDLE(radv_device
, device
, _device
);
397 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
398 const VkAllocationCallbacks
*alloc
;
406 alloc
= &device
->alloc
;
408 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
409 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
410 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
413 if (swapchain
->needs_linear_copy
)
414 radv_wsi_free_prime_command_buffers(device
, swapchain
);
416 swapchain
->destroy(swapchain
, alloc
);
419 VkResult
radv_GetSwapchainImagesKHR(
421 VkSwapchainKHR _swapchain
,
422 uint32_t* pSwapchainImageCount
,
423 VkImage
* pSwapchainImages
)
425 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
427 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
431 VkResult
radv_AcquireNextImageKHR(
433 VkSwapchainKHR _swapchain
,
435 VkSemaphore semaphore
,
437 uint32_t* pImageIndex
)
439 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
440 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
442 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
445 if (fence
&& (result
== VK_SUCCESS
|| result
== VK_SUBOPTIMAL_KHR
)) {
446 fence
->submitted
= true;
447 fence
->signalled
= true;
452 VkResult
radv_QueuePresentKHR(
454 const VkPresentInfoKHR
* pPresentInfo
)
456 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
457 VkResult result
= VK_SUCCESS
;
458 const VkPresentRegionsKHR
*regions
=
459 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
461 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
462 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
463 struct radeon_winsys_cs
*cs
;
464 const VkPresentRegionKHR
*region
= NULL
;
465 VkResult item_result
;
466 struct radv_winsys_sem_info sem_info
;
468 item_result
= radv_alloc_sem_info(&sem_info
,
469 pPresentInfo
->waitSemaphoreCount
,
470 pPresentInfo
->pWaitSemaphores
,
473 if (pPresentInfo
->pResults
!= NULL
)
474 pPresentInfo
->pResults
[i
] = item_result
;
475 result
= result
== VK_SUCCESS
? item_result
: result
;
476 if (item_result
!= VK_SUCCESS
) {
477 radv_free_sem_info(&sem_info
);
481 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
482 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
483 item_result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
484 &(VkFenceCreateInfo
) {
485 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
487 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
488 if (pPresentInfo
->pResults
!= NULL
)
489 pPresentInfo
->pResults
[i
] = item_result
;
490 result
= result
== VK_SUCCESS
? item_result
: result
;
491 if (item_result
!= VK_SUCCESS
) {
492 radv_free_sem_info(&sem_info
);
496 radv_ResetFences(radv_device_to_handle(queue
->device
),
497 1, &swapchain
->fences
[0]);
500 if (swapchain
->needs_linear_copy
) {
501 int idx
= (queue
->queue_family_index
* swapchain
->image_count
) + pPresentInfo
->pImageIndices
[i
];
502 cs
= radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
])->cs
;
504 cs
= queue
->device
->empty_cs
[queue
->queue_family_index
];
505 RADV_FROM_HANDLE(radv_fence
, fence
, swapchain
->fences
[0]);
506 struct radeon_winsys_fence
*base_fence
= fence
->fence
;
507 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
509 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
514 fence
->submitted
= true;
516 if (regions
&& regions
->pRegions
)
517 region
= ®ions
->pRegions
[i
];
519 item_result
= swapchain
->queue_present(swapchain
,
520 pPresentInfo
->pImageIndices
[i
],
522 /* TODO: What if one of them returns OUT_OF_DATE? */
523 if (pPresentInfo
->pResults
!= NULL
)
524 pPresentInfo
->pResults
[i
] = item_result
;
525 result
= result
== VK_SUCCESS
? item_result
: result
;
526 if (item_result
!= VK_SUCCESS
) {
527 radv_free_sem_info(&sem_info
);
531 VkFence last
= swapchain
->fences
[2];
532 swapchain
->fences
[2] = swapchain
->fences
[1];
533 swapchain
->fences
[1] = swapchain
->fences
[0];
534 swapchain
->fences
[0] = last
;
536 if (last
!= VK_NULL_HANDLE
) {
537 radv_WaitForFences(radv_device_to_handle(queue
->device
),
541 radv_free_sem_info(&sem_info
);