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
;
158 result
= radv_image_create(device_h
,
159 &(struct radv_image_create_info
) {
161 &(VkImageCreateInfo
) {
162 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
163 .imageType
= VK_IMAGE_TYPE_2D
,
164 .format
= pCreateInfo
->imageFormat
,
166 .width
= pCreateInfo
->imageExtent
.width
,
167 .height
= pCreateInfo
->imageExtent
.height
,
173 /* FIXME: Need a way to use X tiling to allow scanout */
174 .tiling
= linear
? VK_IMAGE_TILING_LINEAR
: VK_IMAGE_TILING_OPTIMAL
,
175 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
181 if (result
!= VK_SUCCESS
)
184 image
= radv_image_from_handle(image_h
);
186 VkDeviceMemory memory_h
;
188 const VkMemoryDedicatedAllocateInfoKHR ded_alloc
= {
189 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
191 .buffer
= VK_NULL_HANDLE
,
195 result
= radv_AllocateMemory(device_h
,
196 &(VkMemoryAllocateInfo
) {
197 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
199 .allocationSize
= image
->size
,
200 .memoryTypeIndex
= linear
? 1 : 0,
202 NULL
/* XXX: pAllocator */,
204 if (result
!= VK_SUCCESS
)
205 goto fail_create_image
;
207 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
210 * return the fd for the image in the no copy mode,
211 * or the fd for the linear image if a copy is required.
213 if (!needs_linear_copy
|| (needs_linear_copy
&& linear
)) {
214 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
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
;
227 *row_pitch
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
230 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
233 radv_DestroyImage(device_h
, image_h
, pAllocator
);
239 radv_wsi_image_free(VkDevice device
,
240 const VkAllocationCallbacks
* pAllocator
,
242 VkDeviceMemory memory_h
)
244 radv_DestroyImage(device
, image_h
, pAllocator
);
246 radv_FreeMemory(device
, memory_h
, pAllocator
);
249 static const struct wsi_image_fns radv_wsi_image_fns
= {
250 .create_wsi_image
= radv_wsi_image_create
,
251 .free_wsi_image
= radv_wsi_image_free
,
254 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
256 radv_wsi_free_prime_command_buffers(struct radv_device
*device
,
257 struct wsi_swapchain
*swapchain
)
259 const int num_pools
= NUM_PRIME_POOLS
;
260 const int num_images
= swapchain
->image_count
;
262 for (i
= 0; i
< num_pools
; i
++) {
263 radv_FreeCommandBuffers(radv_device_to_handle(device
),
264 swapchain
->cmd_pools
[i
],
265 swapchain
->image_count
,
266 &swapchain
->cmd_buffers
[i
* num_images
]);
268 radv_DestroyCommandPool(radv_device_to_handle(device
),
269 swapchain
->cmd_pools
[i
],
275 radv_wsi_create_prime_command_buffers(struct radv_device
*device
,
276 const VkAllocationCallbacks
*alloc
,
277 struct wsi_swapchain
*swapchain
)
279 const int num_pools
= NUM_PRIME_POOLS
;
280 const int num_images
= swapchain
->image_count
;
281 int num_cmd_buffers
= num_images
* num_pools
; //TODO bump to MAX_QUEUE_FAMILIES
285 swapchain
->cmd_buffers
= vk_alloc(alloc
, (sizeof(VkCommandBuffer
) * num_cmd_buffers
), 8,
286 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
287 if (!swapchain
->cmd_buffers
)
288 return VK_ERROR_OUT_OF_HOST_MEMORY
;
290 memset(swapchain
->cmd_buffers
, 0, sizeof(VkCommandBuffer
) * num_cmd_buffers
);
291 memset(swapchain
->cmd_pools
, 0, sizeof(VkCommandPool
) * num_pools
);
292 for (i
= 0; i
< num_pools
; i
++) {
293 VkCommandPoolCreateInfo pool_create_info
;
295 pool_create_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
296 pool_create_info
.pNext
= NULL
;
297 pool_create_info
.flags
= 0;
298 pool_create_info
.queueFamilyIndex
= i
;
300 result
= radv_CreateCommandPool(radv_device_to_handle(device
),
301 &pool_create_info
, alloc
,
302 &swapchain
->cmd_pools
[i
]);
303 if (result
!= VK_SUCCESS
)
306 VkCommandBufferAllocateInfo cmd_buffer_info
;
307 cmd_buffer_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
308 cmd_buffer_info
.pNext
= NULL
;
309 cmd_buffer_info
.commandPool
= swapchain
->cmd_pools
[i
];
310 cmd_buffer_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
311 cmd_buffer_info
.commandBufferCount
= num_images
;
313 result
= radv_AllocateCommandBuffers(radv_device_to_handle(device
),
315 &swapchain
->cmd_buffers
[i
* num_images
]);
316 if (result
!= VK_SUCCESS
)
318 for (j
= 0; j
< num_images
; j
++) {
319 VkImage image
, linear_image
;
320 int idx
= (i
* num_images
) + j
;
322 swapchain
->get_image_and_linear(swapchain
, j
, &image
, &linear_image
);
323 VkCommandBufferBeginInfo begin_info
= {0};
325 begin_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
;
327 radv_BeginCommandBuffer(swapchain
->cmd_buffers
[idx
], &begin_info
);
329 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
]),
330 radv_image_from_handle(image
),
331 radv_image_from_handle(linear_image
));
333 radv_EndCommandBuffer(swapchain
->cmd_buffers
[idx
]);
338 radv_wsi_free_prime_command_buffers(device
, swapchain
);
342 VkResult
radv_CreateSwapchainKHR(
344 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
345 const VkAllocationCallbacks
* pAllocator
,
346 VkSwapchainKHR
* pSwapchain
)
348 RADV_FROM_HANDLE(radv_device
, device
, _device
);
349 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
350 struct wsi_interface
*iface
=
351 device
->physical_device
->wsi_device
.wsi
[surface
->platform
];
352 struct wsi_swapchain
*swapchain
;
353 const VkAllocationCallbacks
*alloc
;
357 alloc
= &device
->alloc
;
358 VkResult result
= iface
->create_swapchain(surface
, _device
,
359 &device
->physical_device
->wsi_device
,
360 device
->physical_device
->local_fd
,
362 alloc
, &radv_wsi_image_fns
,
364 if (result
!= VK_SUCCESS
)
368 swapchain
->alloc
= *pAllocator
;
370 swapchain
->alloc
= device
->alloc
;
372 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
373 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
375 if (swapchain
->needs_linear_copy
) {
376 result
= radv_wsi_create_prime_command_buffers(device
, alloc
,
378 if (result
!= VK_SUCCESS
)
382 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
387 void radv_DestroySwapchainKHR(
389 VkSwapchainKHR _swapchain
,
390 const VkAllocationCallbacks
* pAllocator
)
392 RADV_FROM_HANDLE(radv_device
, device
, _device
);
393 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
394 const VkAllocationCallbacks
*alloc
;
402 alloc
= &device
->alloc
;
404 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
405 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
406 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
409 if (swapchain
->needs_linear_copy
)
410 radv_wsi_free_prime_command_buffers(device
, swapchain
);
412 swapchain
->destroy(swapchain
, alloc
);
415 VkResult
radv_GetSwapchainImagesKHR(
417 VkSwapchainKHR _swapchain
,
418 uint32_t* pSwapchainImageCount
,
419 VkImage
* pSwapchainImages
)
421 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
423 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
427 VkResult
radv_AcquireNextImageKHR(
429 VkSwapchainKHR _swapchain
,
431 VkSemaphore semaphore
,
433 uint32_t* pImageIndex
)
435 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
436 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
438 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
441 if (fence
&& (result
== VK_SUCCESS
|| result
== VK_SUBOPTIMAL_KHR
)) {
442 fence
->submitted
= true;
443 fence
->signalled
= true;
448 VkResult
radv_QueuePresentKHR(
450 const VkPresentInfoKHR
* pPresentInfo
)
452 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
453 VkResult result
= VK_SUCCESS
;
454 const VkPresentRegionsKHR
*regions
=
455 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
457 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
458 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
459 struct radeon_winsys_cs
*cs
;
460 const VkPresentRegionKHR
*region
= NULL
;
461 VkResult item_result
;
462 struct radv_winsys_sem_info sem_info
;
464 item_result
= radv_alloc_sem_info(&sem_info
,
465 pPresentInfo
->waitSemaphoreCount
,
466 pPresentInfo
->pWaitSemaphores
,
469 if (pPresentInfo
->pResults
!= NULL
)
470 pPresentInfo
->pResults
[i
] = item_result
;
471 result
= result
== VK_SUCCESS
? item_result
: result
;
472 if (item_result
!= VK_SUCCESS
) {
473 radv_free_sem_info(&sem_info
);
477 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
478 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
479 item_result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
480 &(VkFenceCreateInfo
) {
481 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
483 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
484 if (pPresentInfo
->pResults
!= NULL
)
485 pPresentInfo
->pResults
[i
] = item_result
;
486 result
= result
== VK_SUCCESS
? item_result
: result
;
487 if (item_result
!= VK_SUCCESS
) {
488 radv_free_sem_info(&sem_info
);
492 radv_ResetFences(radv_device_to_handle(queue
->device
),
493 1, &swapchain
->fences
[0]);
496 if (swapchain
->needs_linear_copy
) {
497 int idx
= (queue
->queue_family_index
* swapchain
->image_count
) + pPresentInfo
->pImageIndices
[i
];
498 cs
= radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
])->cs
;
500 cs
= queue
->device
->empty_cs
[queue
->queue_family_index
];
501 RADV_FROM_HANDLE(radv_fence
, fence
, swapchain
->fences
[0]);
502 struct radeon_winsys_fence
*base_fence
= fence
->fence
;
503 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
505 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
510 fence
->submitted
= true;
512 if (regions
&& regions
->pRegions
)
513 region
= ®ions
->pRegions
[i
];
515 item_result
= swapchain
->queue_present(swapchain
,
516 pPresentInfo
->pImageIndices
[i
],
518 /* TODO: What if one of them returns OUT_OF_DATE? */
519 if (pPresentInfo
->pResults
!= NULL
)
520 pPresentInfo
->pResults
[i
] = item_result
;
521 result
= result
== VK_SUCCESS
? item_result
: result
;
522 if (item_result
!= VK_SUCCESS
) {
523 radv_free_sem_info(&sem_info
);
527 VkFence last
= swapchain
->fences
[2];
528 swapchain
->fences
[2] = swapchain
->fences
[1];
529 swapchain
->fences
[1] = swapchain
->fences
[0];
530 swapchain
->fences
[0] = last
;
532 if (last
!= VK_NULL_HANDLE
) {
533 radv_WaitForFences(radv_device_to_handle(queue
->device
),
537 radv_free_sem_info(&sem_info
);