radv: initial support for shared semaphores (v2)
[mesa.git] / src / amd / vulkan / radv_wsi.c
1 /*
2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * IN THE SOFTWARE.
24 */
25
26 #include "radv_private.h"
27 #include "radv_meta.h"
28 #include "wsi_common.h"
29 #include "vk_util.h"
30
31 static const struct wsi_callbacks wsi_cbs = {
32 .get_phys_device_format_properties = radv_GetPhysicalDeviceFormatProperties,
33 };
34
35 VkResult
36 radv_init_wsi(struct radv_physical_device *physical_device)
37 {
38 VkResult result;
39
40 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
41
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)
45 return result;
46 #endif
47
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),
51 &wsi_cbs);
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);
55 #endif
56 return result;
57 }
58 #endif
59
60 return VK_SUCCESS;
61 }
62
63 void
64 radv_finish_wsi(struct radv_physical_device *physical_device)
65 {
66 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
67 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
68 #endif
69 #ifdef VK_USE_PLATFORM_XCB_KHR
70 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
71 #endif
72 }
73
74 void radv_DestroySurfaceKHR(
75 VkInstance _instance,
76 VkSurfaceKHR _surface,
77 const VkAllocationCallbacks* pAllocator)
78 {
79 RADV_FROM_HANDLE(radv_instance, instance, _instance);
80 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
81
82 vk_free2(&instance->alloc, pAllocator, surface);
83 }
84
85 VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
86 VkPhysicalDevice physicalDevice,
87 uint32_t queueFamilyIndex,
88 VkSurfaceKHR _surface,
89 VkBool32* pSupported)
90 {
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];
94
95 return iface->get_support(surface, &device->wsi_device,
96 &device->instance->alloc,
97 queueFamilyIndex, device->local_fd, true, pSupported);
98 }
99
100 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
101 VkPhysicalDevice physicalDevice,
102 VkSurfaceKHR _surface,
103 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
104 {
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];
108
109 return iface->get_capabilities(surface, pSurfaceCapabilities);
110 }
111
112 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
113 VkPhysicalDevice physicalDevice,
114 VkSurfaceKHR _surface,
115 uint32_t* pSurfaceFormatCount,
116 VkSurfaceFormatKHR* pSurfaceFormats)
117 {
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];
121
122 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
123 pSurfaceFormats);
124 }
125
126 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
127 VkPhysicalDevice physicalDevice,
128 VkSurfaceKHR _surface,
129 uint32_t* pPresentModeCount,
130 VkPresentModeKHR* pPresentModes)
131 {
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];
135
136 return iface->get_present_modes(surface, pPresentModeCount,
137 pPresentModes);
138 }
139
140 static VkResult
141 radv_wsi_image_create(VkDevice device_h,
142 const VkSwapchainCreateInfoKHR *pCreateInfo,
143 const VkAllocationCallbacks* pAllocator,
144 bool needs_linear_copy,
145 bool linear,
146 VkImage *image_p,
147 VkDeviceMemory *memory_p,
148 uint32_t *size,
149 uint32_t *offset,
150 uint32_t *row_pitch, int *fd_p)
151 {
152 VkResult result = VK_SUCCESS;
153 struct radeon_surf *surface;
154 VkImage image_h;
155 struct radv_image *image;
156 int fd;
157
158 result = radv_image_create(device_h,
159 &(struct radv_image_create_info) {
160 .vk_info =
161 &(VkImageCreateInfo) {
162 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
163 .imageType = VK_IMAGE_TYPE_2D,
164 .format = pCreateInfo->imageFormat,
165 .extent = {
166 .width = pCreateInfo->imageExtent.width,
167 .height = pCreateInfo->imageExtent.height,
168 .depth = 1
169 },
170 .mipLevels = 1,
171 .arrayLayers = 1,
172 .samples = 1,
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,
176 .flags = 0,
177 },
178 .scanout = true},
179 NULL,
180 &image_h);
181 if (result != VK_SUCCESS)
182 return result;
183
184 image = radv_image_from_handle(image_h);
185
186 VkDeviceMemory memory_h;
187
188 const VkMemoryDedicatedAllocateInfoKHR ded_alloc = {
189 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
190 .pNext = NULL,
191 .buffer = VK_NULL_HANDLE,
192 .image = image_h
193 };
194
195 result = radv_AllocateMemory(device_h,
196 &(VkMemoryAllocateInfo) {
197 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
198 .pNext = &ded_alloc,
199 .allocationSize = image->size,
200 .memoryTypeIndex = linear ? 1 : 0,
201 },
202 NULL /* XXX: pAllocator */,
203 &memory_h);
204 if (result != VK_SUCCESS)
205 goto fail_create_image;
206
207 radv_BindImageMemory(device_h, image_h, memory_h, 0);
208
209 /*
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.
212 */
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;
218 *fd_p = fd;
219 }
220
221 surface = &image->surface;
222
223 *image_p = image_h;
224 *memory_p = memory_h;
225 *size = image->size;
226 *offset = image->offset;
227 *row_pitch = surface->u.legacy.level[0].nblk_x * surface->bpe;
228 return VK_SUCCESS;
229 fail_alloc_memory:
230 radv_FreeMemory(device_h, memory_h, pAllocator);
231
232 fail_create_image:
233 radv_DestroyImage(device_h, image_h, pAllocator);
234
235 return result;
236 }
237
238 static void
239 radv_wsi_image_free(VkDevice device,
240 const VkAllocationCallbacks* pAllocator,
241 VkImage image_h,
242 VkDeviceMemory memory_h)
243 {
244 radv_DestroyImage(device, image_h, pAllocator);
245
246 radv_FreeMemory(device, memory_h, pAllocator);
247 }
248
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,
252 };
253
254 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
255 static void
256 radv_wsi_free_prime_command_buffers(struct radv_device *device,
257 struct wsi_swapchain *swapchain)
258 {
259 const int num_pools = NUM_PRIME_POOLS;
260 const int num_images = swapchain->image_count;
261 int i;
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]);
267
268 radv_DestroyCommandPool(radv_device_to_handle(device),
269 swapchain->cmd_pools[i],
270 &swapchain->alloc);
271 }
272 }
273
274 static VkResult
275 radv_wsi_create_prime_command_buffers(struct radv_device *device,
276 const VkAllocationCallbacks *alloc,
277 struct wsi_swapchain *swapchain)
278 {
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
282 VkResult result;
283 int i, j;
284
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;
289
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;
294
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;
299
300 result = radv_CreateCommandPool(radv_device_to_handle(device),
301 &pool_create_info, alloc,
302 &swapchain->cmd_pools[i]);
303 if (result != VK_SUCCESS)
304 goto fail;
305
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;
312
313 result = radv_AllocateCommandBuffers(radv_device_to_handle(device),
314 &cmd_buffer_info,
315 &swapchain->cmd_buffers[i * num_images]);
316 if (result != VK_SUCCESS)
317 goto fail;
318 for (j = 0; j < num_images; j++) {
319 VkImage image, linear_image;
320 int idx = (i * num_images) + j;
321
322 swapchain->get_image_and_linear(swapchain, j, &image, &linear_image);
323 VkCommandBufferBeginInfo begin_info = {0};
324
325 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
326
327 radv_BeginCommandBuffer(swapchain->cmd_buffers[idx], &begin_info);
328
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));
332
333 radv_EndCommandBuffer(swapchain->cmd_buffers[idx]);
334 }
335 }
336 return VK_SUCCESS;
337 fail:
338 radv_wsi_free_prime_command_buffers(device, swapchain);
339 return result;
340 }
341
342 VkResult radv_CreateSwapchainKHR(
343 VkDevice _device,
344 const VkSwapchainCreateInfoKHR* pCreateInfo,
345 const VkAllocationCallbacks* pAllocator,
346 VkSwapchainKHR* pSwapchain)
347 {
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;
354 if (pAllocator)
355 alloc = pAllocator;
356 else
357 alloc = &device->alloc;
358 VkResult result = iface->create_swapchain(surface, _device,
359 &device->physical_device->wsi_device,
360 device->physical_device->local_fd,
361 pCreateInfo,
362 alloc, &radv_wsi_image_fns,
363 &swapchain);
364 if (result != VK_SUCCESS)
365 return result;
366
367 if (pAllocator)
368 swapchain->alloc = *pAllocator;
369 else
370 swapchain->alloc = device->alloc;
371
372 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
373 swapchain->fences[i] = VK_NULL_HANDLE;
374
375 if (swapchain->needs_linear_copy) {
376 result = radv_wsi_create_prime_command_buffers(device, alloc,
377 swapchain);
378 if (result != VK_SUCCESS)
379 return result;
380 }
381
382 *pSwapchain = wsi_swapchain_to_handle(swapchain);
383
384 return VK_SUCCESS;
385 }
386
387 void radv_DestroySwapchainKHR(
388 VkDevice _device,
389 VkSwapchainKHR _swapchain,
390 const VkAllocationCallbacks* pAllocator)
391 {
392 RADV_FROM_HANDLE(radv_device, device, _device);
393 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
394 const VkAllocationCallbacks *alloc;
395
396 if (!_swapchain)
397 return;
398
399 if (pAllocator)
400 alloc = pAllocator;
401 else
402 alloc = &device->alloc;
403
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);
407 }
408
409 if (swapchain->needs_linear_copy)
410 radv_wsi_free_prime_command_buffers(device, swapchain);
411
412 swapchain->destroy(swapchain, alloc);
413 }
414
415 VkResult radv_GetSwapchainImagesKHR(
416 VkDevice device,
417 VkSwapchainKHR _swapchain,
418 uint32_t* pSwapchainImageCount,
419 VkImage* pSwapchainImages)
420 {
421 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
422
423 return swapchain->get_images(swapchain, pSwapchainImageCount,
424 pSwapchainImages);
425 }
426
427 VkResult radv_AcquireNextImageKHR(
428 VkDevice device,
429 VkSwapchainKHR _swapchain,
430 uint64_t timeout,
431 VkSemaphore semaphore,
432 VkFence _fence,
433 uint32_t* pImageIndex)
434 {
435 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
436 RADV_FROM_HANDLE(radv_fence, fence, _fence);
437
438 VkResult result = swapchain->acquire_next_image(swapchain, timeout, semaphore,
439 pImageIndex);
440
441 if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
442 fence->submitted = true;
443 fence->signalled = true;
444 }
445 return result;
446 }
447
448 VkResult radv_QueuePresentKHR(
449 VkQueue _queue,
450 const VkPresentInfoKHR* pPresentInfo)
451 {
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);
456
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;
463
464 item_result = radv_alloc_sem_info(&sem_info,
465 pPresentInfo->waitSemaphoreCount,
466 pPresentInfo->pWaitSemaphores,
467 0,
468 NULL);
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);
474 continue;
475 }
476
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,
482 .flags = 0,
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);
489 continue;
490 }
491 } else {
492 radv_ResetFences(radv_device_to_handle(queue->device),
493 1, &swapchain->fences[0]);
494 }
495
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;
499 } else
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;
504
505 queue->device->ws->cs_submit(ctx, queue->queue_idx,
506 &cs,
507 1, NULL, NULL,
508 &sem_info,
509 false, base_fence);
510 fence->submitted = true;
511
512 if (regions && regions->pRegions)
513 region = &regions->pRegions[i];
514
515 item_result = swapchain->queue_present(swapchain,
516 pPresentInfo->pImageIndices[i],
517 region);
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);
524 continue;
525 }
526
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;
531
532 if (last != VK_NULL_HANDLE) {
533 radv_WaitForFences(radv_device_to_handle(queue->device),
534 1, &last, true, 1);
535 }
536
537 radv_free_sem_info(&sem_info);
538 }
539
540 return VK_SUCCESS;
541 }