radv/gfx9: allocate events from uncached VA space
[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 RADV_FROM_HANDLE(radv_device, device, device_h);
158
159 result = radv_image_create(device_h,
160 &(struct radv_image_create_info) {
161 .vk_info =
162 &(VkImageCreateInfo) {
163 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
164 .imageType = VK_IMAGE_TYPE_2D,
165 .format = pCreateInfo->imageFormat,
166 .extent = {
167 .width = pCreateInfo->imageExtent.width,
168 .height = pCreateInfo->imageExtent.height,
169 .depth = 1
170 },
171 .mipLevels = 1,
172 .arrayLayers = 1,
173 .samples = 1,
174 /* FIXME: Need a way to use X tiling to allow scanout */
175 .tiling = linear ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL,
176 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
177 .flags = 0,
178 },
179 .scanout = true},
180 NULL,
181 &image_h);
182 if (result != VK_SUCCESS)
183 return result;
184
185 image = radv_image_from_handle(image_h);
186
187 VkDeviceMemory memory_h;
188
189 const VkMemoryDedicatedAllocateInfoKHR ded_alloc = {
190 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
191 .pNext = NULL,
192 .buffer = VK_NULL_HANDLE,
193 .image = image_h
194 };
195
196 result = radv_AllocateMemory(device_h,
197 &(VkMemoryAllocateInfo) {
198 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
199 .pNext = &ded_alloc,
200 .allocationSize = image->size,
201 .memoryTypeIndex = linear ? 1 : 0,
202 },
203 NULL /* XXX: pAllocator */,
204 &memory_h);
205 if (result != VK_SUCCESS)
206 goto fail_create_image;
207
208 radv_BindImageMemory(device_h, image_h, memory_h, 0);
209
210 /*
211 * return the fd for the image in the no copy mode,
212 * or the fd for the linear image if a copy is required.
213 */
214 if (!needs_linear_copy || (needs_linear_copy && linear)) {
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
228 if (device->physical_device->rad_info.chip_class >= GFX9)
229 *row_pitch = surface->u.gfx9.surf_pitch * surface->bpe;
230 else
231 *row_pitch = surface->u.legacy.level[0].nblk_x * surface->bpe;
232 return VK_SUCCESS;
233 fail_alloc_memory:
234 radv_FreeMemory(device_h, memory_h, pAllocator);
235
236 fail_create_image:
237 radv_DestroyImage(device_h, image_h, pAllocator);
238
239 return result;
240 }
241
242 static void
243 radv_wsi_image_free(VkDevice device,
244 const VkAllocationCallbacks* pAllocator,
245 VkImage image_h,
246 VkDeviceMemory memory_h)
247 {
248 radv_DestroyImage(device, image_h, pAllocator);
249
250 radv_FreeMemory(device, memory_h, pAllocator);
251 }
252
253 static const struct wsi_image_fns radv_wsi_image_fns = {
254 .create_wsi_image = radv_wsi_image_create,
255 .free_wsi_image = radv_wsi_image_free,
256 };
257
258 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
259 static void
260 radv_wsi_free_prime_command_buffers(struct radv_device *device,
261 struct wsi_swapchain *swapchain)
262 {
263 const int num_pools = NUM_PRIME_POOLS;
264 const int num_images = swapchain->image_count;
265 int i;
266 for (i = 0; i < num_pools; i++) {
267 radv_FreeCommandBuffers(radv_device_to_handle(device),
268 swapchain->cmd_pools[i],
269 swapchain->image_count,
270 &swapchain->cmd_buffers[i * num_images]);
271
272 radv_DestroyCommandPool(radv_device_to_handle(device),
273 swapchain->cmd_pools[i],
274 &swapchain->alloc);
275 }
276 }
277
278 static VkResult
279 radv_wsi_create_prime_command_buffers(struct radv_device *device,
280 const VkAllocationCallbacks *alloc,
281 struct wsi_swapchain *swapchain)
282 {
283 const int num_pools = NUM_PRIME_POOLS;
284 const int num_images = swapchain->image_count;
285 int num_cmd_buffers = num_images * num_pools; //TODO bump to MAX_QUEUE_FAMILIES
286 VkResult result;
287 int i, j;
288
289 swapchain->cmd_buffers = vk_alloc(alloc, (sizeof(VkCommandBuffer) * num_cmd_buffers), 8,
290 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
291 if (!swapchain->cmd_buffers)
292 return VK_ERROR_OUT_OF_HOST_MEMORY;
293
294 memset(swapchain->cmd_buffers, 0, sizeof(VkCommandBuffer) * num_cmd_buffers);
295 memset(swapchain->cmd_pools, 0, sizeof(VkCommandPool) * num_pools);
296 for (i = 0; i < num_pools; i++) {
297 VkCommandPoolCreateInfo pool_create_info;
298
299 pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
300 pool_create_info.pNext = NULL;
301 pool_create_info.flags = 0;
302 pool_create_info.queueFamilyIndex = i;
303
304 result = radv_CreateCommandPool(radv_device_to_handle(device),
305 &pool_create_info, alloc,
306 &swapchain->cmd_pools[i]);
307 if (result != VK_SUCCESS)
308 goto fail;
309
310 VkCommandBufferAllocateInfo cmd_buffer_info;
311 cmd_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
312 cmd_buffer_info.pNext = NULL;
313 cmd_buffer_info.commandPool = swapchain->cmd_pools[i];
314 cmd_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
315 cmd_buffer_info.commandBufferCount = num_images;
316
317 result = radv_AllocateCommandBuffers(radv_device_to_handle(device),
318 &cmd_buffer_info,
319 &swapchain->cmd_buffers[i * num_images]);
320 if (result != VK_SUCCESS)
321 goto fail;
322 for (j = 0; j < num_images; j++) {
323 VkImage image, linear_image;
324 int idx = (i * num_images) + j;
325
326 swapchain->get_image_and_linear(swapchain, j, &image, &linear_image);
327 VkCommandBufferBeginInfo begin_info = {0};
328
329 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
330
331 radv_BeginCommandBuffer(swapchain->cmd_buffers[idx], &begin_info);
332
333 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx]),
334 radv_image_from_handle(image),
335 radv_image_from_handle(linear_image));
336
337 radv_EndCommandBuffer(swapchain->cmd_buffers[idx]);
338 }
339 }
340 return VK_SUCCESS;
341 fail:
342 radv_wsi_free_prime_command_buffers(device, swapchain);
343 return result;
344 }
345
346 VkResult radv_CreateSwapchainKHR(
347 VkDevice _device,
348 const VkSwapchainCreateInfoKHR* pCreateInfo,
349 const VkAllocationCallbacks* pAllocator,
350 VkSwapchainKHR* pSwapchain)
351 {
352 RADV_FROM_HANDLE(radv_device, device, _device);
353 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
354 struct wsi_interface *iface =
355 device->physical_device->wsi_device.wsi[surface->platform];
356 struct wsi_swapchain *swapchain;
357 const VkAllocationCallbacks *alloc;
358 if (pAllocator)
359 alloc = pAllocator;
360 else
361 alloc = &device->alloc;
362 VkResult result = iface->create_swapchain(surface, _device,
363 &device->physical_device->wsi_device,
364 device->physical_device->local_fd,
365 pCreateInfo,
366 alloc, &radv_wsi_image_fns,
367 &swapchain);
368 if (result != VK_SUCCESS)
369 return result;
370
371 if (pAllocator)
372 swapchain->alloc = *pAllocator;
373 else
374 swapchain->alloc = device->alloc;
375
376 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
377 swapchain->fences[i] = VK_NULL_HANDLE;
378
379 if (swapchain->needs_linear_copy) {
380 result = radv_wsi_create_prime_command_buffers(device, alloc,
381 swapchain);
382 if (result != VK_SUCCESS)
383 return result;
384 }
385
386 *pSwapchain = wsi_swapchain_to_handle(swapchain);
387
388 return VK_SUCCESS;
389 }
390
391 void radv_DestroySwapchainKHR(
392 VkDevice _device,
393 VkSwapchainKHR _swapchain,
394 const VkAllocationCallbacks* pAllocator)
395 {
396 RADV_FROM_HANDLE(radv_device, device, _device);
397 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
398 const VkAllocationCallbacks *alloc;
399
400 if (!_swapchain)
401 return;
402
403 if (pAllocator)
404 alloc = pAllocator;
405 else
406 alloc = &device->alloc;
407
408 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
409 if (swapchain->fences[i] != VK_NULL_HANDLE)
410 radv_DestroyFence(_device, swapchain->fences[i], pAllocator);
411 }
412
413 if (swapchain->needs_linear_copy)
414 radv_wsi_free_prime_command_buffers(device, swapchain);
415
416 swapchain->destroy(swapchain, alloc);
417 }
418
419 VkResult radv_GetSwapchainImagesKHR(
420 VkDevice device,
421 VkSwapchainKHR _swapchain,
422 uint32_t* pSwapchainImageCount,
423 VkImage* pSwapchainImages)
424 {
425 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
426
427 return swapchain->get_images(swapchain, pSwapchainImageCount,
428 pSwapchainImages);
429 }
430
431 VkResult radv_AcquireNextImageKHR(
432 VkDevice device,
433 VkSwapchainKHR _swapchain,
434 uint64_t timeout,
435 VkSemaphore semaphore,
436 VkFence _fence,
437 uint32_t* pImageIndex)
438 {
439 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
440 RADV_FROM_HANDLE(radv_fence, fence, _fence);
441
442 VkResult result = swapchain->acquire_next_image(swapchain, timeout, semaphore,
443 pImageIndex);
444
445 if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
446 fence->submitted = true;
447 fence->signalled = true;
448 }
449 return result;
450 }
451
452 VkResult radv_QueuePresentKHR(
453 VkQueue _queue,
454 const VkPresentInfoKHR* pPresentInfo)
455 {
456 RADV_FROM_HANDLE(radv_queue, queue, _queue);
457 VkResult result = VK_SUCCESS;
458 const VkPresentRegionsKHR *regions =
459 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
460
461 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
462 RADV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
463 struct radeon_winsys_cs *cs;
464 const VkPresentRegionKHR *region = NULL;
465 VkResult item_result;
466 struct radv_winsys_sem_info sem_info;
467
468 item_result = radv_alloc_sem_info(&sem_info,
469 pPresentInfo->waitSemaphoreCount,
470 pPresentInfo->pWaitSemaphores,
471 0,
472 NULL);
473 if (pPresentInfo->pResults != NULL)
474 pPresentInfo->pResults[i] = item_result;
475 result = result == VK_SUCCESS ? item_result : result;
476 if (item_result != VK_SUCCESS) {
477 radv_free_sem_info(&sem_info);
478 continue;
479 }
480
481 assert(radv_device_from_handle(swapchain->device) == queue->device);
482 if (swapchain->fences[0] == VK_NULL_HANDLE) {
483 item_result = radv_CreateFence(radv_device_to_handle(queue->device),
484 &(VkFenceCreateInfo) {
485 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
486 .flags = 0,
487 }, &swapchain->alloc, &swapchain->fences[0]);
488 if (pPresentInfo->pResults != NULL)
489 pPresentInfo->pResults[i] = item_result;
490 result = result == VK_SUCCESS ? item_result : result;
491 if (item_result != VK_SUCCESS) {
492 radv_free_sem_info(&sem_info);
493 continue;
494 }
495 } else {
496 radv_ResetFences(radv_device_to_handle(queue->device),
497 1, &swapchain->fences[0]);
498 }
499
500 if (swapchain->needs_linear_copy) {
501 int idx = (queue->queue_family_index * swapchain->image_count) + pPresentInfo->pImageIndices[i];
502 cs = radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx])->cs;
503 } else
504 cs = queue->device->empty_cs[queue->queue_family_index];
505 RADV_FROM_HANDLE(radv_fence, fence, swapchain->fences[0]);
506 struct radeon_winsys_fence *base_fence = fence->fence;
507 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
508
509 queue->device->ws->cs_submit(ctx, queue->queue_idx,
510 &cs,
511 1, NULL, NULL,
512 &sem_info,
513 false, base_fence);
514 fence->submitted = true;
515
516 if (regions && regions->pRegions)
517 region = &regions->pRegions[i];
518
519 item_result = swapchain->queue_present(swapchain,
520 pPresentInfo->pImageIndices[i],
521 region);
522 /* TODO: What if one of them returns OUT_OF_DATE? */
523 if (pPresentInfo->pResults != NULL)
524 pPresentInfo->pResults[i] = item_result;
525 result = result == VK_SUCCESS ? item_result : result;
526 if (item_result != VK_SUCCESS) {
527 radv_free_sem_info(&sem_info);
528 continue;
529 }
530
531 VkFence last = swapchain->fences[2];
532 swapchain->fences[2] = swapchain->fences[1];
533 swapchain->fences[1] = swapchain->fences[0];
534 swapchain->fences[0] = last;
535
536 if (last != VK_NULL_HANDLE) {
537 radv_WaitForFences(radv_device_to_handle(queue->device),
538 1, &last, true, 1);
539 }
540
541 radv_free_sem_info(&sem_info);
542 }
543
544 return VK_SUCCESS;
545 }