anv: Don't place scratch buffers above the 32-bit boundary
[mesa.git] / src / intel / vulkan / anv_wsi.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_private.h"
25 #include "wsi_common.h"
26 #include "vk_format_info.h"
27 #include "util/vk_util.h"
28
29 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
30 static const struct wsi_callbacks wsi_cbs = {
31 .get_phys_device_format_properties = anv_GetPhysicalDeviceFormatProperties,
32 };
33 #endif
34
35 VkResult
36 anv_init_wsi(struct anv_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 anv_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 anv_finish_wsi(struct anv_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 anv_DestroySurfaceKHR(
75 VkInstance _instance,
76 VkSurfaceKHR _surface,
77 const VkAllocationCallbacks* pAllocator)
78 {
79 ANV_FROM_HANDLE(anv_instance, instance, _instance);
80 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
81
82 if (!surface)
83 return;
84
85 vk_free2(&instance->alloc, pAllocator, surface);
86 }
87
88 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
89 VkPhysicalDevice physicalDevice,
90 uint32_t queueFamilyIndex,
91 VkSurfaceKHR _surface,
92 VkBool32* pSupported)
93 {
94 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
95 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
96 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
97
98 return iface->get_support(surface, &device->wsi_device,
99 &device->instance->alloc,
100 queueFamilyIndex, device->local_fd, false, pSupported);
101 }
102
103 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
104 VkPhysicalDevice physicalDevice,
105 VkSurfaceKHR _surface,
106 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
107 {
108 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
109 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
110 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
111
112 return iface->get_capabilities(surface, pSurfaceCapabilities);
113 }
114
115 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
116 VkPhysicalDevice physicalDevice,
117 VkSurfaceKHR _surface,
118 uint32_t* pSurfaceFormatCount,
119 VkSurfaceFormatKHR* pSurfaceFormats)
120 {
121 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
122 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
123 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
124
125 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
126 pSurfaceFormats);
127 }
128
129 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
130 VkPhysicalDevice physicalDevice,
131 VkSurfaceKHR _surface,
132 uint32_t* pPresentModeCount,
133 VkPresentModeKHR* pPresentModes)
134 {
135 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
136 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
137 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
138
139 return iface->get_present_modes(surface, pPresentModeCount,
140 pPresentModes);
141 }
142
143
144 static VkResult
145 x11_anv_wsi_image_create(VkDevice device_h,
146 const VkSwapchainCreateInfoKHR *pCreateInfo,
147 const VkAllocationCallbacks* pAllocator,
148 bool different_gpu,
149 bool linear,
150 VkImage *image_p,
151 VkDeviceMemory *memory_p,
152 uint32_t *size,
153 uint32_t *offset,
154 uint32_t *row_pitch, int *fd_p)
155 {
156 struct anv_device *device = anv_device_from_handle(device_h);
157 VkImage image_h;
158 struct anv_image *image;
159
160 VkResult result;
161 result = anv_image_create(anv_device_to_handle(device),
162 &(struct anv_image_create_info) {
163 .isl_tiling_flags = ISL_TILING_X_BIT,
164 .stride = 0,
165 .vk_info =
166 &(VkImageCreateInfo) {
167 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
168 .imageType = VK_IMAGE_TYPE_2D,
169 .format = pCreateInfo->imageFormat,
170 .extent = {
171 .width = pCreateInfo->imageExtent.width,
172 .height = pCreateInfo->imageExtent.height,
173 .depth = 1
174 },
175 .mipLevels = 1,
176 .arrayLayers = 1,
177 .samples = 1,
178 /* FIXME: Need a way to use X tiling to allow scanout */
179 .tiling = VK_IMAGE_TILING_OPTIMAL,
180 .usage = (pCreateInfo->imageUsage |
181 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
182 .flags = 0,
183 }},
184 NULL,
185 &image_h);
186 if (result != VK_SUCCESS)
187 return result;
188
189 image = anv_image_from_handle(image_h);
190 assert(vk_format_is_color(image->vk_format));
191
192 VkDeviceMemory memory_h;
193 struct anv_device_memory *memory;
194 result = anv_AllocateMemory(anv_device_to_handle(device),
195 &(VkMemoryAllocateInfo) {
196 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
197 .allocationSize = image->size,
198 .memoryTypeIndex = 0,
199 },
200 NULL /* XXX: pAllocator */,
201 &memory_h);
202 if (result != VK_SUCCESS)
203 goto fail_create_image;
204
205 memory = anv_device_memory_from_handle(memory_h);
206
207 /* We need to set the WRITE flag on window system buffers so that GEM will
208 * know we're writing to them and synchronize uses on other rings (eg if
209 * the display server uses the blitter ring).
210 */
211 memory->bo.flags |= EXEC_OBJECT_WRITE;
212
213 anv_BindImageMemory(device_h, image_h, memory_h, 0);
214
215 struct anv_surface *surface = &image->color_surface;
216 assert(surface->isl.tiling == ISL_TILING_X);
217
218 *row_pitch = surface->isl.row_pitch;
219 int ret = anv_gem_set_tiling(device, memory->bo.gem_handle,
220 surface->isl.row_pitch, I915_TILING_X);
221 if (ret) {
222 /* FINISHME: Choose a better error. */
223 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
224 "set_tiling failed: %m");
225 goto fail_alloc_memory;
226 }
227
228 int fd = anv_gem_handle_to_fd(device, memory->bo.gem_handle);
229 if (fd == -1) {
230 /* FINISHME: Choose a better error. */
231 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
232 "handle_to_fd failed: %m");
233 goto fail_alloc_memory;
234 }
235
236 *image_p = image_h;
237 *memory_p = memory_h;
238 *fd_p = fd;
239 *size = image->size;
240 *offset = image->offset;
241 return VK_SUCCESS;
242 fail_alloc_memory:
243 anv_FreeMemory(device_h, memory_h, pAllocator);
244
245 fail_create_image:
246 anv_DestroyImage(device_h, image_h, pAllocator);
247 return result;
248 }
249
250 static void
251 x11_anv_wsi_image_free(VkDevice device,
252 const VkAllocationCallbacks* pAllocator,
253 VkImage image_h,
254 VkDeviceMemory memory_h)
255 {
256 anv_DestroyImage(device, image_h, pAllocator);
257
258 anv_FreeMemory(device, memory_h, pAllocator);
259 }
260
261 static const struct wsi_image_fns anv_wsi_image_fns = {
262 .create_wsi_image = x11_anv_wsi_image_create,
263 .free_wsi_image = x11_anv_wsi_image_free,
264 };
265
266 VkResult anv_CreateSwapchainKHR(
267 VkDevice _device,
268 const VkSwapchainCreateInfoKHR* pCreateInfo,
269 const VkAllocationCallbacks* pAllocator,
270 VkSwapchainKHR* pSwapchain)
271 {
272 ANV_FROM_HANDLE(anv_device, device, _device);
273 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
274 struct wsi_interface *iface =
275 device->instance->physicalDevice.wsi_device.wsi[surface->platform];
276 struct wsi_swapchain *swapchain;
277 const VkAllocationCallbacks *alloc;
278
279 if (pAllocator)
280 alloc = pAllocator;
281 else
282 alloc = &device->alloc;
283 VkResult result = iface->create_swapchain(surface, _device,
284 &device->instance->physicalDevice.wsi_device,
285 device->instance->physicalDevice.local_fd,
286 pCreateInfo,
287 alloc, &anv_wsi_image_fns,
288 &swapchain);
289 if (result != VK_SUCCESS)
290 return result;
291
292 swapchain->alloc = *alloc;
293
294 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
295 swapchain->fences[i] = VK_NULL_HANDLE;
296
297 *pSwapchain = wsi_swapchain_to_handle(swapchain);
298
299 return VK_SUCCESS;
300 }
301
302 void anv_DestroySwapchainKHR(
303 VkDevice _device,
304 VkSwapchainKHR _swapchain,
305 const VkAllocationCallbacks* pAllocator)
306 {
307 ANV_FROM_HANDLE(anv_device, device, _device);
308 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
309 const VkAllocationCallbacks *alloc;
310
311 if (!swapchain)
312 return;
313
314 if (pAllocator)
315 alloc = pAllocator;
316 else
317 alloc = &device->alloc;
318 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
319 if (swapchain->fences[i] != VK_NULL_HANDLE)
320 anv_DestroyFence(_device, swapchain->fences[i], pAllocator);
321 }
322
323 swapchain->destroy(swapchain, alloc);
324 }
325
326 VkResult anv_GetSwapchainImagesKHR(
327 VkDevice device,
328 VkSwapchainKHR _swapchain,
329 uint32_t* pSwapchainImageCount,
330 VkImage* pSwapchainImages)
331 {
332 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
333
334 return swapchain->get_images(swapchain, pSwapchainImageCount,
335 pSwapchainImages);
336 }
337
338 VkResult anv_AcquireNextImageKHR(
339 VkDevice device,
340 VkSwapchainKHR _swapchain,
341 uint64_t timeout,
342 VkSemaphore semaphore,
343 VkFence _fence,
344 uint32_t* pImageIndex)
345 {
346 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
347 ANV_FROM_HANDLE(anv_fence, fence, _fence);
348
349 VkResult result = swapchain->acquire_next_image(swapchain, timeout,
350 semaphore, pImageIndex);
351
352 /* Thanks to implicit sync, the image is ready immediately. */
353 if (fence)
354 fence->state = ANV_FENCE_STATE_SIGNALED;
355
356 return result;
357 }
358
359 VkResult anv_QueuePresentKHR(
360 VkQueue _queue,
361 const VkPresentInfoKHR* pPresentInfo)
362 {
363 ANV_FROM_HANDLE(anv_queue, queue, _queue);
364 VkResult result = VK_SUCCESS;
365
366 const VkPresentRegionsKHR *regions =
367 vk_find_struct_const(pPresentInfo->pNext, PRESENT_REGIONS_KHR);
368
369 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
370 ANV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
371 VkResult item_result;
372
373 const VkPresentRegionKHR *region = NULL;
374 if (regions && regions->pRegions)
375 region = &regions->pRegions[i];
376
377 assert(anv_device_from_handle(swapchain->device) == queue->device);
378
379 if (swapchain->fences[0] == VK_NULL_HANDLE) {
380 item_result = anv_CreateFence(anv_device_to_handle(queue->device),
381 &(VkFenceCreateInfo) {
382 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
383 .flags = 0,
384 }, &swapchain->alloc, &swapchain->fences[0]);
385 if (pPresentInfo->pResults != NULL)
386 pPresentInfo->pResults[i] = item_result;
387 result = result == VK_SUCCESS ? item_result : result;
388 if (item_result != VK_SUCCESS)
389 continue;
390 } else {
391 anv_ResetFences(anv_device_to_handle(queue->device),
392 1, &swapchain->fences[0]);
393 }
394
395 anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
396
397 item_result = swapchain->queue_present(swapchain,
398 pPresentInfo->pImageIndices[i],
399 region);
400 /* TODO: What if one of them returns OUT_OF_DATE? */
401 if (pPresentInfo->pResults != NULL)
402 pPresentInfo->pResults[i] = item_result;
403 result = result == VK_SUCCESS ? item_result : result;
404 if (item_result != VK_SUCCESS)
405 continue;
406
407 VkFence last = swapchain->fences[2];
408 swapchain->fences[2] = swapchain->fences[1];
409 swapchain->fences[1] = swapchain->fences[0];
410 swapchain->fences[0] = last;
411
412 if (last != VK_NULL_HANDLE) {
413 anv_WaitForFences(anv_device_to_handle(queue->device),
414 1, &last, true, 1);
415 }
416 }
417
418 return VK_SUCCESS;
419 }