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"
30 #include "util/macros.h"
32 MAYBE_UNUSED
static const struct wsi_callbacks wsi_cbs
= {
33 .get_phys_device_format_properties
= radv_GetPhysicalDeviceFormatProperties
,
37 radv_init_wsi(struct radv_physical_device
*physical_device
)
41 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
43 #ifdef VK_USE_PLATFORM_XCB_KHR
44 result
= wsi_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
45 if (result
!= VK_SUCCESS
)
49 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
50 result
= wsi_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
51 radv_physical_device_to_handle(physical_device
),
53 if (result
!= VK_SUCCESS
) {
54 #ifdef VK_USE_PLATFORM_XCB_KHR
55 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
65 radv_finish_wsi(struct radv_physical_device
*physical_device
)
67 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
68 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
70 #ifdef VK_USE_PLATFORM_XCB_KHR
71 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
75 void radv_DestroySurfaceKHR(
77 VkSurfaceKHR _surface
,
78 const VkAllocationCallbacks
* pAllocator
)
80 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
81 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
83 vk_free2(&instance
->alloc
, pAllocator
, surface
);
86 VkResult
radv_GetPhysicalDeviceSurfaceSupportKHR(
87 VkPhysicalDevice physicalDevice
,
88 uint32_t queueFamilyIndex
,
89 VkSurfaceKHR _surface
,
92 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
93 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
94 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
96 return iface
->get_support(surface
, &device
->wsi_device
,
97 &device
->instance
->alloc
,
98 queueFamilyIndex
, device
->local_fd
, true, pSupported
);
101 VkResult
radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
102 VkPhysicalDevice physicalDevice
,
103 VkSurfaceKHR _surface
,
104 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
106 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
107 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
108 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
110 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
113 VkResult
radv_GetPhysicalDeviceSurfaceFormatsKHR(
114 VkPhysicalDevice physicalDevice
,
115 VkSurfaceKHR _surface
,
116 uint32_t* pSurfaceFormatCount
,
117 VkSurfaceFormatKHR
* pSurfaceFormats
)
119 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
120 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
121 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
123 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
127 VkResult
radv_GetPhysicalDeviceSurfacePresentModesKHR(
128 VkPhysicalDevice physicalDevice
,
129 VkSurfaceKHR _surface
,
130 uint32_t* pPresentModeCount
,
131 VkPresentModeKHR
* pPresentModes
)
133 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
134 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
135 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
137 return iface
->get_present_modes(surface
, pPresentModeCount
,
142 radv_wsi_image_create(VkDevice device_h
,
143 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
144 const VkAllocationCallbacks
* pAllocator
,
145 bool needs_linear_copy
,
148 VkDeviceMemory
*memory_p
,
151 uint32_t *row_pitch
, int *fd_p
)
153 VkResult result
= VK_SUCCESS
;
154 struct radeon_surf
*surface
;
156 struct radv_image
*image
;
158 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
160 result
= radv_image_create(device_h
,
161 &(struct radv_image_create_info
) {
163 &(VkImageCreateInfo
) {
164 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
165 .imageType
= VK_IMAGE_TYPE_2D
,
166 .format
= pCreateInfo
->imageFormat
,
168 .width
= pCreateInfo
->imageExtent
.width
,
169 .height
= pCreateInfo
->imageExtent
.height
,
175 /* FIXME: Need a way to use X tiling to allow scanout */
176 .tiling
= linear
? VK_IMAGE_TILING_LINEAR
: VK_IMAGE_TILING_OPTIMAL
,
177 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
183 if (result
!= VK_SUCCESS
)
186 image
= radv_image_from_handle(image_h
);
188 VkDeviceMemory memory_h
;
190 const VkMemoryDedicatedAllocateInfoKHR ded_alloc
= {
191 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
193 .buffer
= VK_NULL_HANDLE
,
197 result
= radv_AllocateMemory(device_h
,
198 &(VkMemoryAllocateInfo
) {
199 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
201 .allocationSize
= image
->size
,
202 .memoryTypeIndex
= linear
? 1 : 0,
204 NULL
/* XXX: pAllocator */,
206 if (result
!= VK_SUCCESS
)
207 goto fail_create_image
;
209 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
212 * return the fd for the image in the no copy mode,
213 * or the fd for the linear image if a copy is required.
215 if (!needs_linear_copy
|| (needs_linear_copy
&& linear
)) {
216 RADV_FROM_HANDLE(radv_device_memory
, memory
, memory_h
);
217 if (!radv_get_memory_fd(device
, memory
, &fd
))
218 goto fail_alloc_memory
;
222 surface
= &image
->surface
;
225 *memory_p
= memory_h
;
227 *offset
= image
->offset
;
229 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
230 *row_pitch
= surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
232 *row_pitch
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
235 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
238 radv_DestroyImage(device_h
, image_h
, pAllocator
);
244 radv_wsi_image_free(VkDevice device
,
245 const VkAllocationCallbacks
* pAllocator
,
247 VkDeviceMemory memory_h
)
249 radv_DestroyImage(device
, image_h
, pAllocator
);
251 radv_FreeMemory(device
, memory_h
, pAllocator
);
254 static const struct wsi_image_fns radv_wsi_image_fns
= {
255 .create_wsi_image
= radv_wsi_image_create
,
256 .free_wsi_image
= radv_wsi_image_free
,
259 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
261 radv_wsi_free_prime_command_buffers(struct radv_device
*device
,
262 struct wsi_swapchain
*swapchain
)
264 const int num_pools
= NUM_PRIME_POOLS
;
265 const int num_images
= swapchain
->image_count
;
267 for (i
= 0; i
< num_pools
; i
++) {
268 radv_FreeCommandBuffers(radv_device_to_handle(device
),
269 swapchain
->cmd_pools
[i
],
270 swapchain
->image_count
,
271 &swapchain
->cmd_buffers
[i
* num_images
]);
273 radv_DestroyCommandPool(radv_device_to_handle(device
),
274 swapchain
->cmd_pools
[i
],
280 radv_wsi_create_prime_command_buffers(struct radv_device
*device
,
281 const VkAllocationCallbacks
*alloc
,
282 struct wsi_swapchain
*swapchain
)
284 const int num_pools
= NUM_PRIME_POOLS
;
285 const int num_images
= swapchain
->image_count
;
286 int num_cmd_buffers
= num_images
* num_pools
; //TODO bump to MAX_QUEUE_FAMILIES
290 swapchain
->cmd_buffers
= vk_alloc(alloc
, (sizeof(VkCommandBuffer
) * num_cmd_buffers
), 8,
291 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
292 if (!swapchain
->cmd_buffers
)
293 return VK_ERROR_OUT_OF_HOST_MEMORY
;
295 memset(swapchain
->cmd_buffers
, 0, sizeof(VkCommandBuffer
) * num_cmd_buffers
);
296 memset(swapchain
->cmd_pools
, 0, sizeof(VkCommandPool
) * num_pools
);
297 for (i
= 0; i
< num_pools
; i
++) {
298 VkCommandPoolCreateInfo pool_create_info
;
300 pool_create_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
301 pool_create_info
.pNext
= NULL
;
302 pool_create_info
.flags
= 0;
303 pool_create_info
.queueFamilyIndex
= i
;
305 result
= radv_CreateCommandPool(radv_device_to_handle(device
),
306 &pool_create_info
, alloc
,
307 &swapchain
->cmd_pools
[i
]);
308 if (result
!= VK_SUCCESS
)
311 VkCommandBufferAllocateInfo cmd_buffer_info
;
312 cmd_buffer_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
313 cmd_buffer_info
.pNext
= NULL
;
314 cmd_buffer_info
.commandPool
= swapchain
->cmd_pools
[i
];
315 cmd_buffer_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
316 cmd_buffer_info
.commandBufferCount
= num_images
;
318 result
= radv_AllocateCommandBuffers(radv_device_to_handle(device
),
320 &swapchain
->cmd_buffers
[i
* num_images
]);
321 if (result
!= VK_SUCCESS
)
323 for (j
= 0; j
< num_images
; j
++) {
324 VkImage image
, linear_image
;
325 int idx
= (i
* num_images
) + j
;
327 swapchain
->get_image_and_linear(swapchain
, j
, &image
, &linear_image
);
328 VkCommandBufferBeginInfo begin_info
= {0};
330 begin_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
;
332 radv_BeginCommandBuffer(swapchain
->cmd_buffers
[idx
], &begin_info
);
334 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
]),
335 radv_image_from_handle(image
),
336 radv_image_from_handle(linear_image
));
338 radv_EndCommandBuffer(swapchain
->cmd_buffers
[idx
]);
343 radv_wsi_free_prime_command_buffers(device
, swapchain
);
347 VkResult
radv_CreateSwapchainKHR(
349 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
350 const VkAllocationCallbacks
* pAllocator
,
351 VkSwapchainKHR
* pSwapchain
)
353 RADV_FROM_HANDLE(radv_device
, device
, _device
);
354 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
355 struct wsi_interface
*iface
=
356 device
->physical_device
->wsi_device
.wsi
[surface
->platform
];
357 struct wsi_swapchain
*swapchain
;
358 const VkAllocationCallbacks
*alloc
;
362 alloc
= &device
->alloc
;
363 VkResult result
= iface
->create_swapchain(surface
, _device
,
364 &device
->physical_device
->wsi_device
,
365 device
->physical_device
->local_fd
,
367 alloc
, &radv_wsi_image_fns
,
369 if (result
!= VK_SUCCESS
)
373 swapchain
->alloc
= *pAllocator
;
375 swapchain
->alloc
= device
->alloc
;
377 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
378 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
380 if (swapchain
->needs_linear_copy
) {
381 result
= radv_wsi_create_prime_command_buffers(device
, alloc
,
383 if (result
!= VK_SUCCESS
)
387 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
392 void radv_DestroySwapchainKHR(
394 VkSwapchainKHR _swapchain
,
395 const VkAllocationCallbacks
* pAllocator
)
397 RADV_FROM_HANDLE(radv_device
, device
, _device
);
398 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
399 const VkAllocationCallbacks
*alloc
;
407 alloc
= &device
->alloc
;
409 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
410 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
411 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
414 if (swapchain
->needs_linear_copy
)
415 radv_wsi_free_prime_command_buffers(device
, swapchain
);
417 swapchain
->destroy(swapchain
, alloc
);
420 VkResult
radv_GetSwapchainImagesKHR(
422 VkSwapchainKHR _swapchain
,
423 uint32_t* pSwapchainImageCount
,
424 VkImage
* pSwapchainImages
)
426 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
428 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
432 VkResult
radv_AcquireNextImageKHR(
434 VkSwapchainKHR _swapchain
,
436 VkSemaphore semaphore
,
438 uint32_t* pImageIndex
)
440 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
441 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
443 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
446 if (fence
&& (result
== VK_SUCCESS
|| result
== VK_SUBOPTIMAL_KHR
)) {
447 fence
->submitted
= true;
448 fence
->signalled
= true;
453 VkResult
radv_QueuePresentKHR(
455 const VkPresentInfoKHR
* pPresentInfo
)
457 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
458 VkResult result
= VK_SUCCESS
;
459 const VkPresentRegionsKHR
*regions
=
460 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
462 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
463 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
464 struct radeon_winsys_cs
*cs
;
465 const VkPresentRegionKHR
*region
= NULL
;
466 VkResult item_result
;
467 struct radv_winsys_sem_info sem_info
;
469 item_result
= radv_alloc_sem_info(&sem_info
,
470 pPresentInfo
->waitSemaphoreCount
,
471 pPresentInfo
->pWaitSemaphores
,
474 if (pPresentInfo
->pResults
!= NULL
)
475 pPresentInfo
->pResults
[i
] = item_result
;
476 result
= result
== VK_SUCCESS
? item_result
: result
;
477 if (item_result
!= VK_SUCCESS
) {
478 radv_free_sem_info(&sem_info
);
482 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
483 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
484 item_result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
485 &(VkFenceCreateInfo
) {
486 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
488 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
489 if (pPresentInfo
->pResults
!= NULL
)
490 pPresentInfo
->pResults
[i
] = item_result
;
491 result
= result
== VK_SUCCESS
? item_result
: result
;
492 if (item_result
!= VK_SUCCESS
) {
493 radv_free_sem_info(&sem_info
);
497 radv_ResetFences(radv_device_to_handle(queue
->device
),
498 1, &swapchain
->fences
[0]);
501 if (swapchain
->needs_linear_copy
) {
502 int idx
= (queue
->queue_family_index
* swapchain
->image_count
) + pPresentInfo
->pImageIndices
[i
];
503 cs
= radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
])->cs
;
505 cs
= queue
->device
->empty_cs
[queue
->queue_family_index
];
506 RADV_FROM_HANDLE(radv_fence
, fence
, swapchain
->fences
[0]);
507 struct radeon_winsys_fence
*base_fence
= fence
->fence
;
508 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
510 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
515 fence
->submitted
= true;
517 if (regions
&& regions
->pRegions
)
518 region
= ®ions
->pRegions
[i
];
520 item_result
= swapchain
->queue_present(swapchain
,
521 pPresentInfo
->pImageIndices
[i
],
523 /* TODO: What if one of them returns OUT_OF_DATE? */
524 if (pPresentInfo
->pResults
!= NULL
)
525 pPresentInfo
->pResults
[i
] = item_result
;
526 result
= result
== VK_SUCCESS
? item_result
: result
;
527 if (item_result
!= VK_SUCCESS
) {
528 radv_free_sem_info(&sem_info
);
532 VkFence last
= swapchain
->fences
[2];
533 swapchain
->fences
[2] = swapchain
->fences
[1];
534 swapchain
->fences
[1] = swapchain
->fences
[0];
535 swapchain
->fences
[0] = last
;
537 if (last
!= VK_NULL_HANDLE
) {
538 radv_WaitForFences(radv_device_to_handle(queue
->device
),
542 radv_free_sem_info(&sem_info
);