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