vulkan/wsi: Plumb present regions through the common code
[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 VkResult result = VK_SUCCESS;
152 struct radeon_surf *surface;
153 VkImage image_h;
154 struct radv_image *image;
155 int fd;
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 VkDedicatedAllocationMemoryAllocateInfoNV ded_alloc = {
188 .sType = VK_STRUCTURE_TYPE_DEDICATED_ALLOCATION_MEMORY_ALLOCATE_INFO_NV,
189 .pNext = NULL,
190 .buffer = VK_NULL_HANDLE,
191 .image = image_h
192 };
193
194 result = radv_AllocateMemory(device_h,
195 &(VkMemoryAllocateInfo) {
196 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
197 .pNext = &ded_alloc,
198 .allocationSize = image->size,
199 .memoryTypeIndex = linear ? 1 : 0,
200 },
201 NULL /* XXX: pAllocator */,
202 &memory_h);
203 if (result != VK_SUCCESS)
204 goto fail_create_image;
205
206 radv_BindImageMemory(device_h, image_h, memory_h, 0);
207
208 /*
209 * return the fd for the image in the no copy mode,
210 * or the fd for the linear image if a copy is required.
211 */
212 if (!needs_linear_copy || (needs_linear_copy && linear)) {
213 RADV_FROM_HANDLE(radv_device, device, device_h);
214 RADV_FROM_HANDLE(radv_device_memory, memory, memory_h);
215 if (!radv_get_memory_fd(device, memory, &fd))
216 goto fail_alloc_memory;
217 *fd_p = fd;
218 }
219
220 surface = &image->surface;
221
222 *image_p = image_h;
223 *memory_p = memory_h;
224 *size = image->size;
225 *offset = image->offset;
226 *row_pitch = surface->level[0].pitch_bytes;
227 return VK_SUCCESS;
228 fail_alloc_memory:
229 radv_FreeMemory(device_h, memory_h, pAllocator);
230
231 fail_create_image:
232 radv_DestroyImage(device_h, image_h, pAllocator);
233
234 return result;
235 }
236
237 static void
238 radv_wsi_image_free(VkDevice device,
239 const VkAllocationCallbacks* pAllocator,
240 VkImage image_h,
241 VkDeviceMemory memory_h)
242 {
243 radv_DestroyImage(device, image_h, pAllocator);
244
245 radv_FreeMemory(device, memory_h, pAllocator);
246 }
247
248 static const struct wsi_image_fns radv_wsi_image_fns = {
249 .create_wsi_image = radv_wsi_image_create,
250 .free_wsi_image = radv_wsi_image_free,
251 };
252
253 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
254 static void
255 radv_wsi_free_prime_command_buffers(struct radv_device *device,
256 struct wsi_swapchain *swapchain)
257 {
258 const int num_pools = NUM_PRIME_POOLS;
259 const int num_images = swapchain->image_count;
260 int i;
261 for (i = 0; i < num_pools; i++) {
262 radv_FreeCommandBuffers(radv_device_to_handle(device),
263 swapchain->cmd_pools[i],
264 swapchain->image_count,
265 &swapchain->cmd_buffers[i * num_images]);
266
267 radv_DestroyCommandPool(radv_device_to_handle(device),
268 swapchain->cmd_pools[i],
269 &swapchain->alloc);
270 }
271 }
272
273 static VkResult
274 radv_wsi_create_prime_command_buffers(struct radv_device *device,
275 const VkAllocationCallbacks *alloc,
276 struct wsi_swapchain *swapchain)
277 {
278 const int num_pools = NUM_PRIME_POOLS;
279 const int num_images = swapchain->image_count;
280 int num_cmd_buffers = num_images * num_pools; //TODO bump to MAX_QUEUE_FAMILIES
281 VkResult result;
282 int i, j;
283
284 swapchain->cmd_buffers = vk_alloc(alloc, (sizeof(VkCommandBuffer) * num_cmd_buffers), 8,
285 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
286 if (!swapchain->cmd_buffers)
287 return VK_ERROR_OUT_OF_HOST_MEMORY;
288
289 memset(swapchain->cmd_buffers, 0, sizeof(VkCommandBuffer) * num_cmd_buffers);
290 memset(swapchain->cmd_pools, 0, sizeof(VkCommandPool) * num_pools);
291 for (i = 0; i < num_pools; i++) {
292 VkCommandPoolCreateInfo pool_create_info;
293
294 pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
295 pool_create_info.pNext = NULL;
296 pool_create_info.flags = 0;
297 pool_create_info.queueFamilyIndex = i;
298
299 result = radv_CreateCommandPool(radv_device_to_handle(device),
300 &pool_create_info, alloc,
301 &swapchain->cmd_pools[i]);
302 if (result != VK_SUCCESS)
303 goto fail;
304
305 VkCommandBufferAllocateInfo cmd_buffer_info;
306 cmd_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
307 cmd_buffer_info.pNext = NULL;
308 cmd_buffer_info.commandPool = swapchain->cmd_pools[i];
309 cmd_buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
310 cmd_buffer_info.commandBufferCount = num_images;
311
312 result = radv_AllocateCommandBuffers(radv_device_to_handle(device),
313 &cmd_buffer_info,
314 &swapchain->cmd_buffers[i * num_images]);
315 if (result != VK_SUCCESS)
316 goto fail;
317 for (j = 0; j < num_images; j++) {
318 VkImage image, linear_image;
319 int idx = (i * num_images) + j;
320
321 swapchain->get_image_and_linear(swapchain, j, &image, &linear_image);
322 VkCommandBufferBeginInfo begin_info = {0};
323
324 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
325
326 radv_BeginCommandBuffer(swapchain->cmd_buffers[idx], &begin_info);
327
328 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx]),
329 radv_image_from_handle(image),
330 radv_image_from_handle(linear_image));
331
332 radv_EndCommandBuffer(swapchain->cmd_buffers[idx]);
333 }
334 }
335 return VK_SUCCESS;
336 fail:
337 radv_wsi_free_prime_command_buffers(device, swapchain);
338 return result;
339 }
340
341 VkResult radv_CreateSwapchainKHR(
342 VkDevice _device,
343 const VkSwapchainCreateInfoKHR* pCreateInfo,
344 const VkAllocationCallbacks* pAllocator,
345 VkSwapchainKHR* pSwapchain)
346 {
347 RADV_FROM_HANDLE(radv_device, device, _device);
348 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
349 struct wsi_interface *iface =
350 device->physical_device->wsi_device.wsi[surface->platform];
351 struct wsi_swapchain *swapchain;
352 const VkAllocationCallbacks *alloc;
353 if (pAllocator)
354 alloc = pAllocator;
355 else
356 alloc = &device->alloc;
357 VkResult result = iface->create_swapchain(surface, _device,
358 &device->physical_device->wsi_device,
359 device->physical_device->local_fd,
360 pCreateInfo,
361 alloc, &radv_wsi_image_fns,
362 &swapchain);
363 if (result != VK_SUCCESS)
364 return result;
365
366 if (pAllocator)
367 swapchain->alloc = *pAllocator;
368 else
369 swapchain->alloc = device->alloc;
370
371 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
372 swapchain->fences[i] = VK_NULL_HANDLE;
373
374 if (swapchain->needs_linear_copy) {
375 result = radv_wsi_create_prime_command_buffers(device, alloc,
376 swapchain);
377 if (result != VK_SUCCESS)
378 return result;
379 }
380
381 *pSwapchain = wsi_swapchain_to_handle(swapchain);
382
383 return VK_SUCCESS;
384 }
385
386 void radv_DestroySwapchainKHR(
387 VkDevice _device,
388 VkSwapchainKHR _swapchain,
389 const VkAllocationCallbacks* pAllocator)
390 {
391 RADV_FROM_HANDLE(radv_device, device, _device);
392 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
393 const VkAllocationCallbacks *alloc;
394
395 if (!_swapchain)
396 return;
397
398 if (pAllocator)
399 alloc = pAllocator;
400 else
401 alloc = &device->alloc;
402
403 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
404 if (swapchain->fences[i] != VK_NULL_HANDLE)
405 radv_DestroyFence(_device, swapchain->fences[i], pAllocator);
406 }
407
408 if (swapchain->needs_linear_copy)
409 radv_wsi_free_prime_command_buffers(device, swapchain);
410
411 swapchain->destroy(swapchain, alloc);
412 }
413
414 VkResult radv_GetSwapchainImagesKHR(
415 VkDevice device,
416 VkSwapchainKHR _swapchain,
417 uint32_t* pSwapchainImageCount,
418 VkImage* pSwapchainImages)
419 {
420 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
421
422 return swapchain->get_images(swapchain, pSwapchainImageCount,
423 pSwapchainImages);
424 }
425
426 VkResult radv_AcquireNextImageKHR(
427 VkDevice device,
428 VkSwapchainKHR _swapchain,
429 uint64_t timeout,
430 VkSemaphore semaphore,
431 VkFence _fence,
432 uint32_t* pImageIndex)
433 {
434 RADV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
435 RADV_FROM_HANDLE(radv_fence, fence, _fence);
436
437 VkResult result = swapchain->acquire_next_image(swapchain, timeout, semaphore,
438 pImageIndex);
439
440 if (fence && result == VK_SUCCESS) {
441 fence->submitted = true;
442 fence->signalled = true;
443 }
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
455 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
456 RADV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
457 struct radeon_winsys_cs *cs;
458 assert(radv_device_from_handle(swapchain->device) == queue->device);
459 if (swapchain->fences[0] == VK_NULL_HANDLE) {
460 result = radv_CreateFence(radv_device_to_handle(queue->device),
461 &(VkFenceCreateInfo) {
462 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
463 .flags = 0,
464 }, &swapchain->alloc, &swapchain->fences[0]);
465 if (result != VK_SUCCESS)
466 return result;
467 } else {
468 radv_ResetFences(radv_device_to_handle(queue->device),
469 1, &swapchain->fences[0]);
470 }
471
472 if (swapchain->needs_linear_copy) {
473 int idx = (queue->queue_family_index * swapchain->image_count) + pPresentInfo->pImageIndices[i];
474 cs = radv_cmd_buffer_from_handle(swapchain->cmd_buffers[idx])->cs;
475 } else
476 cs = queue->device->empty_cs[queue->queue_family_index];
477 RADV_FROM_HANDLE(radv_fence, fence, swapchain->fences[0]);
478 struct radeon_winsys_fence *base_fence = fence->fence;
479 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
480 queue->device->ws->cs_submit(ctx, queue->queue_idx,
481 &cs,
482 1, NULL, NULL,
483 (struct radeon_winsys_sem **)pPresentInfo->pWaitSemaphores,
484 pPresentInfo->waitSemaphoreCount, NULL, 0, false, base_fence);
485 fence->submitted = true;
486
487 result = swapchain->queue_present(swapchain,
488 pPresentInfo->pImageIndices[i],
489 NULL);
490 /* TODO: What if one of them returns OUT_OF_DATE? */
491 if (result != VK_SUCCESS)
492 return result;
493
494 VkFence last = swapchain->fences[2];
495 swapchain->fences[2] = swapchain->fences[1];
496 swapchain->fences[1] = swapchain->fences[0];
497 swapchain->fences[0] = last;
498
499 if (last != VK_NULL_HANDLE) {
500 radv_WaitForFences(radv_device_to_handle(queue->device),
501 1, &last, true, 1);
502 }
503
504 }
505
506 return VK_SUCCESS;
507 }