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_alloc_memory(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 */,
205 RADV_MEM_IMPLICIT_SYNC
,
207 if (result
!= VK_SUCCESS
)
208 goto fail_create_image
;
210 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
213 * return the fd for the image in the no copy mode,
214 * or the fd for the linear image if a copy is required.
216 if (!needs_linear_copy
|| (needs_linear_copy
&& linear
)) {
217 RADV_FROM_HANDLE(radv_device_memory
, memory
, memory_h
);
218 if (!radv_get_memory_fd(device
, memory
, &fd
))
219 goto fail_alloc_memory
;
223 surface
= &image
->surface
;
226 *memory_p
= memory_h
;
228 *offset
= image
->offset
;
230 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
231 *row_pitch
= surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
233 *row_pitch
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
236 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
239 radv_DestroyImage(device_h
, image_h
, pAllocator
);
245 radv_wsi_image_free(VkDevice device
,
246 const VkAllocationCallbacks
* pAllocator
,
248 VkDeviceMemory memory_h
)
250 radv_DestroyImage(device
, image_h
, pAllocator
);
252 radv_FreeMemory(device
, memory_h
, pAllocator
);
255 static const struct wsi_image_fns radv_wsi_image_fns
= {
256 .create_wsi_image
= radv_wsi_image_create
,
257 .free_wsi_image
= radv_wsi_image_free
,
260 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
262 radv_wsi_free_prime_command_buffers(struct radv_device
*device
,
263 struct wsi_swapchain
*swapchain
)
265 const int num_pools
= NUM_PRIME_POOLS
;
266 const int num_images
= swapchain
->image_count
;
268 for (i
= 0; i
< num_pools
; i
++) {
269 radv_FreeCommandBuffers(radv_device_to_handle(device
),
270 swapchain
->cmd_pools
[i
],
271 swapchain
->image_count
,
272 &swapchain
->cmd_buffers
[i
* num_images
]);
274 radv_DestroyCommandPool(radv_device_to_handle(device
),
275 swapchain
->cmd_pools
[i
],
281 radv_wsi_create_prime_command_buffers(struct radv_device
*device
,
282 const VkAllocationCallbacks
*alloc
,
283 struct wsi_swapchain
*swapchain
)
285 const int num_pools
= NUM_PRIME_POOLS
;
286 const int num_images
= swapchain
->image_count
;
287 int num_cmd_buffers
= num_images
* num_pools
; //TODO bump to MAX_QUEUE_FAMILIES
291 swapchain
->cmd_buffers
= vk_alloc(alloc
, (sizeof(VkCommandBuffer
) * num_cmd_buffers
), 8,
292 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
293 if (!swapchain
->cmd_buffers
)
294 return VK_ERROR_OUT_OF_HOST_MEMORY
;
296 memset(swapchain
->cmd_buffers
, 0, sizeof(VkCommandBuffer
) * num_cmd_buffers
);
297 memset(swapchain
->cmd_pools
, 0, sizeof(VkCommandPool
) * num_pools
);
298 for (i
= 0; i
< num_pools
; i
++) {
299 VkCommandPoolCreateInfo pool_create_info
;
301 pool_create_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
302 pool_create_info
.pNext
= NULL
;
303 pool_create_info
.flags
= 0;
304 pool_create_info
.queueFamilyIndex
= i
;
306 result
= radv_CreateCommandPool(radv_device_to_handle(device
),
307 &pool_create_info
, alloc
,
308 &swapchain
->cmd_pools
[i
]);
309 if (result
!= VK_SUCCESS
)
312 VkCommandBufferAllocateInfo cmd_buffer_info
;
313 cmd_buffer_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
314 cmd_buffer_info
.pNext
= NULL
;
315 cmd_buffer_info
.commandPool
= swapchain
->cmd_pools
[i
];
316 cmd_buffer_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
317 cmd_buffer_info
.commandBufferCount
= num_images
;
319 result
= radv_AllocateCommandBuffers(radv_device_to_handle(device
),
321 &swapchain
->cmd_buffers
[i
* num_images
]);
322 if (result
!= VK_SUCCESS
)
324 for (j
= 0; j
< num_images
; j
++) {
325 VkImage image
, linear_image
;
326 int idx
= (i
* num_images
) + j
;
328 swapchain
->get_image_and_linear(swapchain
, j
, &image
, &linear_image
);
329 VkCommandBufferBeginInfo begin_info
= {0};
331 begin_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
;
333 radv_BeginCommandBuffer(swapchain
->cmd_buffers
[idx
], &begin_info
);
335 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
]),
336 radv_image_from_handle(image
),
337 radv_image_from_handle(linear_image
));
339 radv_EndCommandBuffer(swapchain
->cmd_buffers
[idx
]);
344 radv_wsi_free_prime_command_buffers(device
, swapchain
);
348 VkResult
radv_CreateSwapchainKHR(
350 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
351 const VkAllocationCallbacks
* pAllocator
,
352 VkSwapchainKHR
* pSwapchain
)
354 RADV_FROM_HANDLE(radv_device
, device
, _device
);
355 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
356 struct wsi_interface
*iface
=
357 device
->physical_device
->wsi_device
.wsi
[surface
->platform
];
358 struct wsi_swapchain
*swapchain
;
359 const VkAllocationCallbacks
*alloc
;
363 alloc
= &device
->alloc
;
364 VkResult result
= iface
->create_swapchain(surface
, _device
,
365 &device
->physical_device
->wsi_device
,
366 device
->physical_device
->local_fd
,
368 alloc
, &radv_wsi_image_fns
,
370 if (result
!= VK_SUCCESS
)
374 swapchain
->alloc
= *pAllocator
;
376 swapchain
->alloc
= device
->alloc
;
378 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
379 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
381 if (swapchain
->needs_linear_copy
) {
382 result
= radv_wsi_create_prime_command_buffers(device
, alloc
,
384 if (result
!= VK_SUCCESS
)
388 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
393 void radv_DestroySwapchainKHR(
395 VkSwapchainKHR _swapchain
,
396 const VkAllocationCallbacks
* pAllocator
)
398 RADV_FROM_HANDLE(radv_device
, device
, _device
);
399 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
400 const VkAllocationCallbacks
*alloc
;
408 alloc
= &device
->alloc
;
410 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
411 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
412 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
415 if (swapchain
->needs_linear_copy
)
416 radv_wsi_free_prime_command_buffers(device
, swapchain
);
418 swapchain
->destroy(swapchain
, alloc
);
421 VkResult
radv_GetSwapchainImagesKHR(
423 VkSwapchainKHR _swapchain
,
424 uint32_t* pSwapchainImageCount
,
425 VkImage
* pSwapchainImages
)
427 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
429 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
433 VkResult
radv_AcquireNextImageKHR(
435 VkSwapchainKHR _swapchain
,
437 VkSemaphore semaphore
,
439 uint32_t* pImageIndex
)
441 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
442 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
444 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
447 if (fence
&& (result
== VK_SUCCESS
|| result
== VK_SUBOPTIMAL_KHR
)) {
448 fence
->submitted
= true;
449 fence
->signalled
= true;
454 VkResult
radv_QueuePresentKHR(
456 const VkPresentInfoKHR
* pPresentInfo
)
458 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
459 VkResult result
= VK_SUCCESS
;
460 const VkPresentRegionsKHR
*regions
=
461 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
463 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
464 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
465 struct radeon_winsys_cs
*cs
;
466 const VkPresentRegionKHR
*region
= NULL
;
467 VkResult item_result
;
468 struct radv_winsys_sem_info sem_info
;
470 item_result
= radv_alloc_sem_info(&sem_info
,
471 pPresentInfo
->waitSemaphoreCount
,
472 pPresentInfo
->pWaitSemaphores
,
475 if (pPresentInfo
->pResults
!= NULL
)
476 pPresentInfo
->pResults
[i
] = item_result
;
477 result
= result
== VK_SUCCESS
? item_result
: result
;
478 if (item_result
!= VK_SUCCESS
) {
479 radv_free_sem_info(&sem_info
);
483 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
484 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
485 item_result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
486 &(VkFenceCreateInfo
) {
487 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
489 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
490 if (pPresentInfo
->pResults
!= NULL
)
491 pPresentInfo
->pResults
[i
] = item_result
;
492 result
= result
== VK_SUCCESS
? item_result
: result
;
493 if (item_result
!= VK_SUCCESS
) {
494 radv_free_sem_info(&sem_info
);
498 radv_ResetFences(radv_device_to_handle(queue
->device
),
499 1, &swapchain
->fences
[0]);
502 if (swapchain
->needs_linear_copy
) {
503 int idx
= (queue
->queue_family_index
* swapchain
->image_count
) + pPresentInfo
->pImageIndices
[i
];
504 cs
= radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
])->cs
;
506 cs
= queue
->device
->empty_cs
[queue
->queue_family_index
];
507 RADV_FROM_HANDLE(radv_fence
, fence
, swapchain
->fences
[0]);
508 struct radeon_winsys_fence
*base_fence
= fence
->fence
;
509 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
511 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
516 fence
->submitted
= true;
518 if (regions
&& regions
->pRegions
)
519 region
= ®ions
->pRegions
[i
];
521 item_result
= swapchain
->queue_present(swapchain
,
522 pPresentInfo
->pImageIndices
[i
],
524 /* TODO: What if one of them returns OUT_OF_DATE? */
525 if (pPresentInfo
->pResults
!= NULL
)
526 pPresentInfo
->pResults
[i
] = item_result
;
527 result
= result
== VK_SUCCESS
? item_result
: result
;
528 if (item_result
!= VK_SUCCESS
) {
529 radv_free_sem_info(&sem_info
);
533 VkFence last
= swapchain
->fences
[2];
534 swapchain
->fences
[2] = swapchain
->fences
[1];
535 swapchain
->fences
[1] = swapchain
->fences
[0];
536 swapchain
->fences
[0] = last
;
538 if (last
!= VK_NULL_HANDLE
) {
539 radv_WaitForFences(radv_device_to_handle(queue
->device
),
543 radv_free_sem_info(&sem_info
);