vulkan/wsi/radv: add initial prime support (v1.1)
[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
30 static const struct wsi_callbacks wsi_cbs = {
31 .get_phys_device_format_properties = radv_GetPhysicalDeviceFormatProperties,
32 };
33
34 VkResult
35 radv_init_wsi(struct radv_physical_device *physical_device)
36 {
37 VkResult result;
38
39 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
40
41 #ifdef VK_USE_PLATFORM_XCB_KHR
42 result = wsi_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
43 if (result != VK_SUCCESS)
44 return result;
45 #endif
46
47 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
48 result = wsi_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
49 radv_physical_device_to_handle(physical_device),
50 &wsi_cbs);
51 if (result != VK_SUCCESS) {
52 #ifdef VK_USE_PLATFORM_XCB_KHR
53 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
54 #endif
55 return result;
56 }
57 #endif
58
59 return VK_SUCCESS;
60 }
61
62 void
63 radv_finish_wsi(struct radv_physical_device *physical_device)
64 {
65 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
66 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
67 #endif
68 #ifdef VK_USE_PLATFORM_XCB_KHR
69 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
70 #endif
71 }
72
73 void radv_DestroySurfaceKHR(
74 VkInstance _instance,
75 VkSurfaceKHR _surface,
76 const VkAllocationCallbacks* pAllocator)
77 {
78 RADV_FROM_HANDLE(radv_instance, instance, _instance);
79 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
80
81 vk_free2(&instance->alloc, pAllocator, surface);
82 }
83
84 VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
85 VkPhysicalDevice physicalDevice,
86 uint32_t queueFamilyIndex,
87 VkSurfaceKHR _surface,
88 VkBool32* pSupported)
89 {
90 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
91 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
92 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
93
94 return iface->get_support(surface, &device->wsi_device,
95 &device->instance->alloc,
96 queueFamilyIndex, device->local_fd, true, pSupported);
97 }
98
99 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
100 VkPhysicalDevice physicalDevice,
101 VkSurfaceKHR _surface,
102 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
103 {
104 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
105 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
106 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
107
108 return iface->get_capabilities(surface, pSurfaceCapabilities);
109 }
110
111 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
112 VkPhysicalDevice physicalDevice,
113 VkSurfaceKHR _surface,
114 uint32_t* pSurfaceFormatCount,
115 VkSurfaceFormatKHR* pSurfaceFormats)
116 {
117 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
118 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
119 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
120
121 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
122 pSurfaceFormats);
123 }
124
125 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
126 VkPhysicalDevice physicalDevice,
127 VkSurfaceKHR _surface,
128 uint32_t* pPresentModeCount,
129 VkPresentModeKHR* pPresentModes)
130 {
131 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
132 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
133 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
134
135 return iface->get_present_modes(surface, pPresentModeCount,
136 pPresentModes);
137 }
138
139 static VkResult
140 radv_wsi_image_create(VkDevice device_h,
141 const VkSwapchainCreateInfoKHR *pCreateInfo,
142 const VkAllocationCallbacks* pAllocator,
143 bool needs_linear_copy,
144 bool linear,
145 VkImage *image_p,
146 VkDeviceMemory *memory_p,
147 uint32_t *size,
148 uint32_t *offset,
149 uint32_t *row_pitch, int *fd_p)
150 {
151 struct radv_device *device = radv_device_from_handle(device_h);
152 VkResult result = VK_SUCCESS;
153 struct radeon_surf *surface;
154 VkImage image_h;
155 struct radv_image *image;
156 bool bret;
157 int fd;
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 VkDeviceMemory memory_h;
187 struct radv_device_memory *memory;
188
189 result = radv_AllocateMemory(device_h,
190 &(VkMemoryAllocateInfo) {
191 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
192 .allocationSize = image->size,
193 .memoryTypeIndex = linear ? 1 : 0,
194 },
195 NULL /* XXX: pAllocator */,
196 &memory_h);
197 if (result != VK_SUCCESS)
198 goto fail_create_image;
199
200 memory = radv_device_memory_from_handle(memory_h);
201
202 radv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
203
204 /*
205 * return the fd for the image in the no copy mode,
206 * or the fd for the linear image if a copy is required.
207 */
208 if (!needs_linear_copy || (needs_linear_copy && linear)) {
209 bret = device->ws->buffer_get_fd(device->ws,
210 memory->bo, &fd);
211 if (bret == false)
212 goto fail_alloc_memory;
213 *fd_p = fd;
214 }
215
216 {
217 struct radeon_bo_metadata metadata;
218 radv_init_metadata(device, image, &metadata);
219 device->ws->buffer_set_metadata(memory->bo, &metadata);
220 }
221
222 surface = &image->surface;
223
224 *image_p = image_h;
225 *memory_p = memory_h;
226 *size = image->size;
227 *offset = image->offset;
228 *row_pitch = surface->level[0].pitch_bytes;
229 return VK_SUCCESS;
230 fail_alloc_memory:
231 radv_FreeMemory(device_h, memory_h, pAllocator);
232
233 fail_create_image:
234 radv_DestroyImage(device_h, image_h, pAllocator);
235
236 return result;
237 }
238
239 static void
240 radv_wsi_image_free(VkDevice device,
241 const VkAllocationCallbacks* pAllocator,
242 VkImage image_h,
243 VkDeviceMemory memory_h)
244 {
245 radv_DestroyImage(device, image_h, pAllocator);
246
247 radv_FreeMemory(device, memory_h, pAllocator);
248 }
249
250 static const struct wsi_image_fns radv_wsi_image_fns = {
251 .create_wsi_image = radv_wsi_image_create,
252 .free_wsi_image = radv_wsi_image_free,
253 };
254
255 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
256 static void
257 radv_wsi_free_prime_command_buffers(struct radv_device *device,
258 struct wsi_swapchain *swapchain)
259 {
260 const int num_pools = NUM_PRIME_POOLS;
261 const int num_images = swapchain->image_count;
262 int i;
263 for (i = 0; i < num_pools; i++) {
264 radv_FreeCommandBuffers(radv_device_to_handle(device),
265 swapchain->cmd_pools[i],
266 swapchain->image_count,
267 &swapchain->cmd_buffers[i * num_images]);
268
269 radv_DestroyCommandPool(radv_device_to_handle(device),
270 swapchain->cmd_pools[i],
271 &swapchain->alloc);
272 }
273 }
274
275 static VkResult
276 radv_wsi_create_prime_command_buffers(struct radv_device *device,
277 const VkAllocationCallbacks *alloc,
278 struct wsi_swapchain *swapchain)
279 {
280 const int num_pools = NUM_PRIME_POOLS;
281 const int num_images = swapchain->image_count;
282 int num_cmd_buffers = num_images * num_pools; //TODO bump to MAX_QUEUE_FAMILIES
283 VkResult result;
284 int i, j;
285
286 swapchain->cmd_buffers = vk_alloc(alloc, (sizeof(VkCommandBuffer) * num_cmd_buffers), 8,
287 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
288 if (!swapchain->cmd_buffers)
289 return VK_ERROR_OUT_OF_HOST_MEMORY;
290
291 memset(swapchain->cmd_buffers, 0, sizeof(VkCommandBuffer) * num_cmd_buffers);
292 memset(swapchain->cmd_pools, 0, sizeof(VkCommandPool) * num_pools);
293 for (i = 0; i < num_pools; i++) {
294 VkCommandPoolCreateInfo pool_create_info;
295
296 pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
297 pool_create_info.pNext = NULL;
298 pool_create_info.flags = 0;
299 pool_create_info.queueFamilyIndex = i;
300
301 result = radv_CreateCommandPool(radv_device_to_handle(device),
302 &pool_create_info, alloc,
303 &swapchain->cmd_pools[i]);
304 if (result != VK_SUCCESS)
305 goto fail;
306
307 VkCommandBufferAllocateInfo cmd_buffer_info;
308 cmd_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
309 cmd_buffer_info.pNext = NULL;
310 cmd_buffer_info.commandPool = swapchain->cmd_pools[i];
311 cmd_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
312 cmd_buffer_info.commandBufferCount = num_images;
313
314 result = radv_AllocateCommandBuffers(radv_device_to_handle(device),
315 &cmd_buffer_info,
316 &swapchain->cmd_buffers[i * num_images]);
317 if (result != VK_SUCCESS)
318 goto fail;
319 for (j = 0; j < num_images; j++) {
320 VkImage image, linear_image;
321 int idx = (i * num_images) + j;
322
323 swapchain->get_image_and_linear(swapchain, j, &image, &linear_image);
324 VkCommandBufferBeginInfo begin_info = {0};
325
326 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
327
328 radv_BeginCommandBuffer(swapchain->cmd_buffers[idx], &begin_info);
329
330 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx]),
331 radv_image_from_handle(image),
332 radv_image_from_handle(linear_image));
333
334 radv_EndCommandBuffer(swapchain->cmd_buffers[idx]);
335 }
336 }
337 return VK_SUCCESS;
338 fail:
339 radv_wsi_free_prime_command_buffers(device, swapchain);
340 return result;
341 }
342
343 VkResult radv_CreateSwapchainKHR(
344 VkDevice _device,
345 const VkSwapchainCreateInfoKHR* pCreateInfo,
346 const VkAllocationCallbacks* pAllocator,
347 VkSwapchainKHR* pSwapchain)
348 {
349 RADV_FROM_HANDLE(radv_device, device, _device);
350 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
351 struct wsi_interface *iface =
352 device->physical_device->wsi_device.wsi[surface->platform];
353 struct wsi_swapchain *swapchain;
354 const VkAllocationCallbacks *alloc;
355 if (pAllocator)
356 alloc = pAllocator;
357 else
358 alloc = &device->alloc;
359 VkResult result = iface->create_swapchain(surface, _device,
360 &device->physical_device->wsi_device,
361 device->physical_device->local_fd,
362 pCreateInfo,
363 alloc, &radv_wsi_image_fns,
364 &swapchain);
365 if (result != VK_SUCCESS)
366 return result;
367
368 if (pAllocator)
369 swapchain->alloc = *pAllocator;
370 else
371 swapchain->alloc = device->alloc;
372
373 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
374 swapchain->fences[i] = VK_NULL_HANDLE;
375
376 if (swapchain->needs_linear_copy) {
377 result = radv_wsi_create_prime_command_buffers(device, alloc,
378 swapchain);
379 if (result != VK_SUCCESS)
380 return result;
381 }
382
383 *pSwapchain = wsi_swapchain_to_handle(swapchain);
384
385 return VK_SUCCESS;
386 }
387
388 void radv_DestroySwapchainKHR(
389 VkDevice _device,
390 VkSwapchainKHR _swapchain,
391 const VkAllocationCallbacks* pAllocator)
392 {
393 RADV_FROM_HANDLE(radv_device, device, _device);
394 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
395 const VkAllocationCallbacks *alloc;
396
397 if (!_swapchain)
398 return;
399
400 if (pAllocator)
401 alloc = pAllocator;
402 else
403 alloc = &device->alloc;
404
405 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
406 if (swapchain->fences[i] != VK_NULL_HANDLE)
407 radv_DestroyFence(_device, swapchain->fences[i], pAllocator);
408 }
409
410 if (swapchain->needs_linear_copy)
411 radv_wsi_free_prime_command_buffers(device, swapchain);
412
413 swapchain->destroy(swapchain, alloc);
414 }
415
416 VkResult radv_GetSwapchainImagesKHR(
417 VkDevice device,
418 VkSwapchainKHR _swapchain,
419 uint32_t* pSwapchainImageCount,
420 VkImage* pSwapchainImages)
421 {
422 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
423
424 return swapchain->get_images(swapchain, pSwapchainImageCount,
425 pSwapchainImages);
426 }
427
428 VkResult radv_AcquireNextImageKHR(
429 VkDevice device,
430 VkSwapchainKHR _swapchain,
431 uint64_t timeout,
432 VkSemaphore semaphore,
433 VkFence _fence,
434 uint32_t* pImageIndex)
435 {
436 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
437 RADV_FROM_HANDLE(radv_fence, fence, _fence);
438
439 VkResult result = swapchain->acquire_next_image(swapchain, timeout, semaphore,
440 pImageIndex);
441
442 if (fence && result == VK_SUCCESS) {
443 fence->submitted = true;
444 fence->signalled = true;
445 }
446
447 return result;
448 }
449
450 VkResult radv_QueuePresentKHR(
451 VkQueue _queue,
452 const VkPresentInfoKHR* pPresentInfo)
453 {
454 RADV_FROM_HANDLE(radv_queue, queue, _queue);
455 VkResult result = VK_SUCCESS;
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 assert(radv_device_from_handle(swapchain->device) == queue->device);
461 if (swapchain->fences[0] == VK_NULL_HANDLE) {
462 result = radv_CreateFence(radv_device_to_handle(queue->device),
463 &(VkFenceCreateInfo) {
464 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
465 .flags = 0,
466 }, &swapchain->alloc, &swapchain->fences[0]);
467 if (result != VK_SUCCESS)
468 return result;
469 } else {
470 radv_ResetFences(radv_device_to_handle(queue->device),
471 1, &swapchain->fences[0]);
472 }
473
474 if (swapchain->needs_linear_copy) {
475 int idx = (queue->queue_family_index * swapchain->image_count) + pPresentInfo->pImageIndices[i];
476 cs = radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx])->cs;
477 } else
478 cs = queue->device->empty_cs[queue->queue_family_index];
479 RADV_FROM_HANDLE(radv_fence, fence, swapchain->fences[0]);
480 struct radeon_winsys_fence *base_fence = fence->fence;
481 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
482 queue->device->ws->cs_submit(ctx, queue->queue_idx,
483 &cs,
484 1, NULL, NULL,
485 (struct radeon_winsys_sem **)pPresentInfo->pWaitSemaphores,
486 pPresentInfo->waitSemaphoreCount, NULL, 0, false, base_fence);
487 fence->submitted = true;
488
489 result = swapchain->queue_present(swapchain,
490 pPresentInfo->pImageIndices[i]);
491 /* TODO: What if one of them returns OUT_OF_DATE? */
492 if (result != VK_SUCCESS)
493 return result;
494
495 VkFence last = swapchain->fences[2];
496 swapchain->fences[2] = swapchain->fences[1];
497 swapchain->fences[1] = swapchain->fences[0];
498 swapchain->fences[0] = last;
499
500 if (last != VK_NULL_HANDLE) {
501 radv_WaitForFences(radv_device_to_handle(queue->device),
502 1, &last, true, 1);
503 }
504
505 }
506
507 return VK_SUCCESS;
508 }