f5f9a3fbeba51ce46f285eab6522772a7dec3c78
[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 #include "util/macros.h"
31
32 #define WSI_CB(x) .x = radv_##x
33 MAYBE_UNUSED static const struct wsi_callbacks wsi_cbs = {
34 WSI_CB(GetPhysicalDeviceFormatProperties),
35 };
36
37 VkResult
38 radv_init_wsi(struct radv_physical_device *physical_device)
39 {
40 VkResult result;
41
42 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
43
44 #ifdef VK_USE_PLATFORM_XCB_KHR
45 result = wsi_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
46 if (result != VK_SUCCESS)
47 return result;
48 #endif
49
50 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
51 result = wsi_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
52 radv_physical_device_to_handle(physical_device),
53 &wsi_cbs);
54 if (result != VK_SUCCESS) {
55 #ifdef VK_USE_PLATFORM_XCB_KHR
56 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
57 #endif
58 return result;
59 }
60 #endif
61
62 return VK_SUCCESS;
63 }
64
65 void
66 radv_finish_wsi(struct radv_physical_device *physical_device)
67 {
68 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
69 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
70 #endif
71 #ifdef VK_USE_PLATFORM_XCB_KHR
72 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
73 #endif
74 }
75
76 void radv_DestroySurfaceKHR(
77 VkInstance _instance,
78 VkSurfaceKHR _surface,
79 const VkAllocationCallbacks* pAllocator)
80 {
81 RADV_FROM_HANDLE(radv_instance, instance, _instance);
82 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
83
84 vk_free2(&instance->alloc, pAllocator, surface);
85 }
86
87 VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
88 VkPhysicalDevice physicalDevice,
89 uint32_t queueFamilyIndex,
90 VkSurfaceKHR _surface,
91 VkBool32* pSupported)
92 {
93 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
94 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
95 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
96
97 return iface->get_support(surface, &device->wsi_device,
98 &device->instance->alloc,
99 queueFamilyIndex, device->local_fd, true, pSupported);
100 }
101
102 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
103 VkPhysicalDevice physicalDevice,
104 VkSurfaceKHR _surface,
105 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
106 {
107 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
108 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
109 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
110
111 return iface->get_capabilities(surface, pSurfaceCapabilities);
112 }
113
114 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
115 VkPhysicalDevice physicalDevice,
116 VkSurfaceKHR _surface,
117 uint32_t* pSurfaceFormatCount,
118 VkSurfaceFormatKHR* pSurfaceFormats)
119 {
120 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
121 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
122 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
123
124 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
125 pSurfaceFormats);
126 }
127
128 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
129 VkPhysicalDevice physicalDevice,
130 VkSurfaceKHR _surface,
131 uint32_t* pPresentModeCount,
132 VkPresentModeKHR* pPresentModes)
133 {
134 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
135 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
136 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
137
138 return iface->get_present_modes(surface, pPresentModeCount,
139 pPresentModes);
140 }
141
142 static VkResult
143 radv_wsi_image_create(VkDevice device_h,
144 const VkSwapchainCreateInfoKHR *pCreateInfo,
145 const VkAllocationCallbacks* pAllocator,
146 bool needs_linear_copy,
147 bool linear,
148 struct wsi_image *wsi_image)
149 {
150 VkResult result = VK_SUCCESS;
151 struct radeon_surf *surface;
152 VkImage image_h;
153 struct radv_image *image;
154 int fd;
155 RADV_FROM_HANDLE(radv_device, device, device_h);
156
157 result = radv_image_create(device_h,
158 &(struct radv_image_create_info) {
159 .vk_info =
160 &(VkImageCreateInfo) {
161 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
162 .imageType = VK_IMAGE_TYPE_2D,
163 .format = pCreateInfo->imageFormat,
164 .extent = {
165 .width = pCreateInfo->imageExtent.width,
166 .height = pCreateInfo->imageExtent.height,
167 .depth = 1
168 },
169 .mipLevels = 1,
170 .arrayLayers = 1,
171 .samples = 1,
172 /* FIXME: Need a way to use X tiling to allow scanout */
173 .tiling = linear ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL,
174 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
175 .flags = 0,
176 },
177 .scanout = true},
178 NULL,
179 &image_h);
180 if (result != VK_SUCCESS)
181 return result;
182
183 image = radv_image_from_handle(image_h);
184
185 VkDeviceMemory memory_h;
186
187 const VkMemoryDedicatedAllocateInfoKHR ded_alloc = {
188 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
189 .pNext = NULL,
190 .buffer = VK_NULL_HANDLE,
191 .image = image_h
192 };
193
194 /* Find the first VRAM memory type, or GART for PRIME images. */
195 int memory_type_index = -1;
196 for (int i = 0; i < device->physical_device->memory_properties.memoryTypeCount; ++i) {
197 bool is_local = !!(device->physical_device->memory_properties.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
198 if ((linear && !is_local) || (!linear && is_local)) {
199 memory_type_index = i;
200 break;
201 }
202 }
203
204 /* fallback */
205 if (memory_type_index == -1)
206 memory_type_index = 0;
207
208 result = radv_alloc_memory(device_h,
209 &(VkMemoryAllocateInfo) {
210 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
211 .pNext = &ded_alloc,
212 .allocationSize = image->size,
213 .memoryTypeIndex = memory_type_index,
214 },
215 NULL /* XXX: pAllocator */,
216 RADV_MEM_IMPLICIT_SYNC,
217 &memory_h);
218 if (result != VK_SUCCESS)
219 goto fail_create_image;
220
221 radv_BindImageMemory(device_h, image_h, memory_h, 0);
222
223 /*
224 * return the fd for the image in the no copy mode,
225 * or the fd for the linear image if a copy is required.
226 */
227 if (!needs_linear_copy || (needs_linear_copy && linear)) {
228 RADV_FROM_HANDLE(radv_device_memory, memory, memory_h);
229 if (!radv_get_memory_fd(device, memory, &fd))
230 goto fail_alloc_memory;
231 wsi_image->fd = fd;
232 }
233
234 surface = &image->surface;
235
236 wsi_image->image = image_h;
237 wsi_image->memory = memory_h;
238 wsi_image->size = image->size;
239 wsi_image->offset = image->offset;
240 if (device->physical_device->rad_info.chip_class >= GFX9)
241 wsi_image->row_pitch =
242 surface->u.gfx9.surf_pitch * surface->bpe;
243 else
244 wsi_image->row_pitch =
245 surface->u.legacy.level[0].nblk_x * surface->bpe;
246
247 return VK_SUCCESS;
248 fail_alloc_memory:
249 radv_FreeMemory(device_h, memory_h, pAllocator);
250
251 fail_create_image:
252 radv_DestroyImage(device_h, image_h, pAllocator);
253
254 return result;
255 }
256
257 static void
258 radv_wsi_image_free(VkDevice device,
259 const VkAllocationCallbacks* pAllocator,
260 struct wsi_image *wsi_image)
261 {
262 radv_DestroyImage(device, wsi_image->image, pAllocator);
263
264 radv_FreeMemory(device, wsi_image->memory, pAllocator);
265 }
266
267 static const struct wsi_image_fns radv_wsi_image_fns = {
268 .create_wsi_image = radv_wsi_image_create,
269 .free_wsi_image = radv_wsi_image_free,
270 };
271
272 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
273 static void
274 radv_wsi_free_prime_command_buffers(struct radv_device *device,
275 struct wsi_swapchain *swapchain)
276 {
277 const int num_pools = NUM_PRIME_POOLS;
278 const int num_images = swapchain->image_count;
279 int i;
280 for (i = 0; i < num_pools; i++) {
281 radv_FreeCommandBuffers(radv_device_to_handle(device),
282 swapchain->cmd_pools[i],
283 swapchain->image_count,
284 &swapchain->cmd_buffers[i * num_images]);
285
286 radv_DestroyCommandPool(radv_device_to_handle(device),
287 swapchain->cmd_pools[i],
288 &swapchain->alloc);
289 }
290 }
291
292 static VkResult
293 radv_wsi_create_prime_command_buffers(struct radv_device *device,
294 const VkAllocationCallbacks *alloc,
295 struct wsi_swapchain *swapchain)
296 {
297 const int num_pools = NUM_PRIME_POOLS;
298 const int num_images = swapchain->image_count;
299 int num_cmd_buffers = num_images * num_pools; //TODO bump to MAX_QUEUE_FAMILIES
300 VkResult result;
301 int i, j;
302
303 swapchain->cmd_buffers = vk_alloc(alloc, (sizeof(VkCommandBuffer) * num_cmd_buffers), 8,
304 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
305 if (!swapchain->cmd_buffers)
306 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
307
308 memset(swapchain->cmd_buffers, 0, sizeof(VkCommandBuffer) * num_cmd_buffers);
309 memset(swapchain->cmd_pools, 0, sizeof(VkCommandPool) * num_pools);
310 for (i = 0; i < num_pools; i++) {
311 VkCommandPoolCreateInfo pool_create_info;
312
313 pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
314 pool_create_info.pNext = NULL;
315 pool_create_info.flags = 0;
316 pool_create_info.queueFamilyIndex = i;
317
318 result = radv_CreateCommandPool(radv_device_to_handle(device),
319 &pool_create_info, alloc,
320 &swapchain->cmd_pools[i]);
321 if (result != VK_SUCCESS)
322 goto fail;
323
324 VkCommandBufferAllocateInfo cmd_buffer_info;
325 cmd_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
326 cmd_buffer_info.pNext = NULL;
327 cmd_buffer_info.commandPool = swapchain->cmd_pools[i];
328 cmd_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
329 cmd_buffer_info.commandBufferCount = num_images;
330
331 result = radv_AllocateCommandBuffers(radv_device_to_handle(device),
332 &cmd_buffer_info,
333 &swapchain->cmd_buffers[i * num_images]);
334 if (result != VK_SUCCESS)
335 goto fail;
336 for (j = 0; j < num_images; j++) {
337 VkImage image, linear_image;
338 int idx = (i * num_images) + j;
339
340 swapchain->get_image_and_linear(swapchain, j, &image, &linear_image);
341 VkCommandBufferBeginInfo begin_info = {0};
342
343 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
344
345 radv_BeginCommandBuffer(swapchain->cmd_buffers[idx], &begin_info);
346
347 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx]),
348 radv_image_from_handle(image),
349 radv_image_from_handle(linear_image));
350
351 radv_EndCommandBuffer(swapchain->cmd_buffers[idx]);
352 }
353 }
354 return VK_SUCCESS;
355 fail:
356 radv_wsi_free_prime_command_buffers(device, swapchain);
357 return result;
358 }
359
360 VkResult radv_CreateSwapchainKHR(
361 VkDevice _device,
362 const VkSwapchainCreateInfoKHR* pCreateInfo,
363 const VkAllocationCallbacks* pAllocator,
364 VkSwapchainKHR* pSwapchain)
365 {
366 RADV_FROM_HANDLE(radv_device, device, _device);
367 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
368 struct wsi_interface *iface =
369 device->physical_device->wsi_device.wsi[surface->platform];
370 struct wsi_swapchain *swapchain;
371 const VkAllocationCallbacks *alloc;
372 if (pAllocator)
373 alloc = pAllocator;
374 else
375 alloc = &device->alloc;
376 VkResult result = iface->create_swapchain(surface, _device,
377 &device->physical_device->wsi_device,
378 device->physical_device->local_fd,
379 pCreateInfo,
380 alloc, &radv_wsi_image_fns,
381 &swapchain);
382 if (result != VK_SUCCESS)
383 return result;
384
385 if (pAllocator)
386 swapchain->alloc = *pAllocator;
387 else
388 swapchain->alloc = device->alloc;
389
390 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
391 swapchain->fences[i] = VK_NULL_HANDLE;
392
393 if (swapchain->needs_linear_copy) {
394 result = radv_wsi_create_prime_command_buffers(device, alloc,
395 swapchain);
396 if (result != VK_SUCCESS)
397 return result;
398 }
399
400 *pSwapchain = wsi_swapchain_to_handle(swapchain);
401
402 return VK_SUCCESS;
403 }
404
405 void radv_DestroySwapchainKHR(
406 VkDevice _device,
407 VkSwapchainKHR _swapchain,
408 const VkAllocationCallbacks* pAllocator)
409 {
410 RADV_FROM_HANDLE(radv_device, device, _device);
411 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
412 const VkAllocationCallbacks *alloc;
413
414 if (!_swapchain)
415 return;
416
417 if (pAllocator)
418 alloc = pAllocator;
419 else
420 alloc = &device->alloc;
421
422 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
423 if (swapchain->fences[i] != VK_NULL_HANDLE)
424 radv_DestroyFence(_device, swapchain->fences[i], pAllocator);
425 }
426
427 if (swapchain->needs_linear_copy)
428 radv_wsi_free_prime_command_buffers(device, swapchain);
429
430 swapchain->destroy(swapchain, alloc);
431 }
432
433 VkResult radv_GetSwapchainImagesKHR(
434 VkDevice device,
435 VkSwapchainKHR _swapchain,
436 uint32_t* pSwapchainImageCount,
437 VkImage* pSwapchainImages)
438 {
439 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
440
441 return swapchain->get_images(swapchain, pSwapchainImageCount,
442 pSwapchainImages);
443 }
444
445 VkResult radv_AcquireNextImageKHR(
446 VkDevice device,
447 VkSwapchainKHR _swapchain,
448 uint64_t timeout,
449 VkSemaphore semaphore,
450 VkFence _fence,
451 uint32_t* pImageIndex)
452 {
453 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
454 RADV_FROM_HANDLE(radv_fence, fence, _fence);
455
456 VkResult result = swapchain->acquire_next_image(swapchain, timeout, semaphore,
457 pImageIndex);
458
459 if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
460 fence->submitted = true;
461 fence->signalled = true;
462 }
463 return result;
464 }
465
466 VkResult radv_QueuePresentKHR(
467 VkQueue _queue,
468 const VkPresentInfoKHR* pPresentInfo)
469 {
470 RADV_FROM_HANDLE(radv_queue, queue, _queue);
471 VkResult result = VK_SUCCESS;
472 const VkPresentRegionsKHR *regions =
473 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
474
475 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
476 RADV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
477 struct radeon_winsys_cs *cs;
478 const VkPresentRegionKHR *region = NULL;
479 VkResult item_result;
480 struct radv_winsys_sem_info sem_info;
481
482 item_result = radv_alloc_sem_info(&sem_info,
483 pPresentInfo->waitSemaphoreCount,
484 pPresentInfo->pWaitSemaphores,
485 0,
486 NULL);
487 if (pPresentInfo->pResults != NULL)
488 pPresentInfo->pResults[i] = item_result;
489 result = result == VK_SUCCESS ? item_result : result;
490 if (item_result != VK_SUCCESS) {
491 radv_free_sem_info(&sem_info);
492 continue;
493 }
494
495 assert(radv_device_from_handle(swapchain->device) == queue->device);
496 if (swapchain->fences[0] == VK_NULL_HANDLE) {
497 item_result = radv_CreateFence(radv_device_to_handle(queue->device),
498 &(VkFenceCreateInfo) {
499 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
500 .flags = 0,
501 }, &swapchain->alloc, &swapchain->fences[0]);
502 if (pPresentInfo->pResults != NULL)
503 pPresentInfo->pResults[i] = item_result;
504 result = result == VK_SUCCESS ? item_result : result;
505 if (item_result != VK_SUCCESS) {
506 radv_free_sem_info(&sem_info);
507 continue;
508 }
509 } else {
510 radv_ResetFences(radv_device_to_handle(queue->device),
511 1, &swapchain->fences[0]);
512 }
513
514 if (swapchain->needs_linear_copy) {
515 int idx = (queue->queue_family_index * swapchain->image_count) + pPresentInfo->pImageIndices[i];
516 cs = radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx])->cs;
517 } else
518 cs = queue->device->empty_cs[queue->queue_family_index];
519 RADV_FROM_HANDLE(radv_fence, fence, swapchain->fences[0]);
520 struct radeon_winsys_fence *base_fence = fence->fence;
521 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
522
523 queue->device->ws->cs_submit(ctx, queue->queue_idx,
524 &cs,
525 1, NULL, NULL,
526 &sem_info,
527 false, base_fence);
528 fence->submitted = true;
529
530 if (regions && regions->pRegions)
531 region = &regions->pRegions[i];
532
533 item_result = swapchain->queue_present(swapchain,
534 pPresentInfo->pImageIndices[i],
535 region);
536 /* TODO: What if one of them returns OUT_OF_DATE? */
537 if (pPresentInfo->pResults != NULL)
538 pPresentInfo->pResults[i] = item_result;
539 result = result == VK_SUCCESS ? item_result : result;
540 if (item_result != VK_SUCCESS) {
541 radv_free_sem_info(&sem_info);
542 continue;
543 }
544
545 VkFence last = swapchain->fences[2];
546 swapchain->fences[2] = swapchain->fences[1];
547 swapchain->fences[1] = swapchain->fences[0];
548 swapchain->fences[0] = last;
549
550 if (last != VK_NULL_HANDLE) {
551 radv_WaitForFences(radv_device_to_handle(queue->device),
552 1, &last, true, 1);
553 }
554
555 radv_free_sem_info(&sem_info);
556 }
557
558 return VK_SUCCESS;
559 }