2 * Copyright © 2015 Intel Corporation
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:
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
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
25 #include "vk_format_info.h"
27 anv_init_wsi(struct anv_physical_device
*physical_device
)
31 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
33 #ifdef VK_USE_PLATFORM_XCB_KHR
34 result
= anv_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
35 if (result
!= VK_SUCCESS
)
39 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
40 result
= anv_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
41 anv_physical_device_to_handle(physical_device
));
42 if (result
!= VK_SUCCESS
) {
43 #ifdef VK_USE_PLATFORM_XCB_KHR
44 anv_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
54 anv_finish_wsi(struct anv_physical_device
*physical_device
)
56 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
57 anv_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
59 #ifdef VK_USE_PLATFORM_XCB_KHR
60 anv_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
64 void anv_DestroySurfaceKHR(
66 VkSurfaceKHR _surface
,
67 const VkAllocationCallbacks
* pAllocator
)
69 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
70 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
72 vk_free2(&instance
->alloc
, pAllocator
, surface
);
75 VkResult
anv_GetPhysicalDeviceSurfaceSupportKHR(
76 VkPhysicalDevice physicalDevice
,
77 uint32_t queueFamilyIndex
,
78 VkSurfaceKHR _surface
,
81 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
82 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
83 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
85 return iface
->get_support(surface
, &device
->wsi_device
,
86 &device
->instance
->alloc
,
87 queueFamilyIndex
, pSupported
);
90 VkResult
anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
91 VkPhysicalDevice physicalDevice
,
92 VkSurfaceKHR _surface
,
93 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
95 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
96 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
97 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
99 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
102 VkResult
anv_GetPhysicalDeviceSurfaceFormatsKHR(
103 VkPhysicalDevice physicalDevice
,
104 VkSurfaceKHR _surface
,
105 uint32_t* pSurfaceFormatCount
,
106 VkSurfaceFormatKHR
* pSurfaceFormats
)
108 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
109 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
110 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
112 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
116 VkResult
anv_GetPhysicalDeviceSurfacePresentModesKHR(
117 VkPhysicalDevice physicalDevice
,
118 VkSurfaceKHR _surface
,
119 uint32_t* pPresentModeCount
,
120 VkPresentModeKHR
* pPresentModes
)
122 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
123 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
124 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
126 return iface
->get_present_modes(surface
, pPresentModeCount
,
132 x11_anv_wsi_image_create(VkDevice device_h
,
133 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
134 const VkAllocationCallbacks
* pAllocator
,
136 VkDeviceMemory
*memory_p
,
139 uint32_t *row_pitch
, int *fd_p
)
141 struct anv_device
*device
= anv_device_from_handle(device_h
);
143 struct anv_image
*image
;
146 result
= anv_image_create(anv_device_to_handle(device
),
147 &(struct anv_image_create_info
) {
148 .isl_tiling_flags
= ISL_TILING_X_BIT
,
151 &(VkImageCreateInfo
) {
152 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
153 .imageType
= VK_IMAGE_TYPE_2D
,
154 .format
= pCreateInfo
->imageFormat
,
156 .width
= pCreateInfo
->imageExtent
.width
,
157 .height
= pCreateInfo
->imageExtent
.height
,
163 /* FIXME: Need a way to use X tiling to allow scanout */
164 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
165 .usage
= (pCreateInfo
->imageUsage
|
166 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
),
171 if (result
!= VK_SUCCESS
)
174 image
= anv_image_from_handle(image_h
);
175 assert(vk_format_is_color(image
->vk_format
));
177 VkDeviceMemory memory_h
;
178 struct anv_device_memory
*memory
;
179 result
= anv_AllocateMemory(anv_device_to_handle(device
),
180 &(VkMemoryAllocateInfo
) {
181 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
182 .allocationSize
= image
->size
,
183 .memoryTypeIndex
= 0,
185 NULL
/* XXX: pAllocator */,
187 if (result
!= VK_SUCCESS
)
188 goto fail_create_image
;
190 memory
= anv_device_memory_from_handle(memory_h
);
191 memory
->bo
.is_winsys_bo
= true;
193 anv_BindImageMemory(VK_NULL_HANDLE
, image_h
, memory_h
, 0);
195 struct anv_surface
*surface
= &image
->color_surface
;
196 assert(surface
->isl
.tiling
== ISL_TILING_X
);
198 *row_pitch
= surface
->isl
.row_pitch
;
199 int ret
= anv_gem_set_tiling(device
, memory
->bo
.gem_handle
,
200 surface
->isl
.row_pitch
, I915_TILING_X
);
202 /* FINISHME: Choose a better error. */
203 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
204 "set_tiling failed: %m");
205 goto fail_alloc_memory
;
208 int fd
= anv_gem_handle_to_fd(device
, memory
->bo
.gem_handle
);
210 /* FINISHME: Choose a better error. */
211 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
212 "handle_to_fd failed: %m");
213 goto fail_alloc_memory
;
217 *memory_p
= memory_h
;
220 *offset
= image
->offset
;
223 anv_FreeMemory(device_h
, memory_h
, pAllocator
);
226 anv_DestroyImage(device_h
, image_h
, pAllocator
);
231 x11_anv_wsi_image_free(VkDevice device
,
232 const VkAllocationCallbacks
* pAllocator
,
234 VkDeviceMemory memory_h
)
236 anv_DestroyImage(device
, image_h
, pAllocator
);
238 anv_FreeMemory(device
, memory_h
, pAllocator
);
241 static const struct anv_wsi_image_fns anv_wsi_image_fns
= {
242 .create_wsi_image
= x11_anv_wsi_image_create
,
243 .free_wsi_image
= x11_anv_wsi_image_free
,
246 VkResult
anv_CreateSwapchainKHR(
248 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
249 const VkAllocationCallbacks
* pAllocator
,
250 VkSwapchainKHR
* pSwapchain
)
252 ANV_FROM_HANDLE(anv_device
, device
, _device
);
253 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
254 struct anv_wsi_interface
*iface
=
255 device
->instance
->physicalDevice
.wsi_device
.wsi
[surface
->platform
];
256 struct anv_swapchain
*swapchain
;
257 const VkAllocationCallbacks
*alloc
;
262 alloc
= &device
->alloc
;
263 VkResult result
= iface
->create_swapchain(surface
, _device
,
264 &device
->instance
->physicalDevice
.wsi_device
,
266 alloc
, &anv_wsi_image_fns
,
268 if (result
!= VK_SUCCESS
)
271 swapchain
->alloc
= *alloc
;
273 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
274 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
276 *pSwapchain
= anv_swapchain_to_handle(swapchain
);
281 void anv_DestroySwapchainKHR(
283 VkSwapchainKHR _swapchain
,
284 const VkAllocationCallbacks
* pAllocator
)
286 ANV_FROM_HANDLE(anv_device
, device
, _device
);
287 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, _swapchain
);
288 const VkAllocationCallbacks
*alloc
;
293 alloc
= &device
->alloc
;
294 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
295 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
296 anv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
299 swapchain
->destroy(swapchain
, alloc
);
302 VkResult
anv_GetSwapchainImagesKHR(
304 VkSwapchainKHR _swapchain
,
305 uint32_t* pSwapchainImageCount
,
306 VkImage
* pSwapchainImages
)
308 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, _swapchain
);
310 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
314 VkResult
anv_AcquireNextImageKHR(
316 VkSwapchainKHR _swapchain
,
318 VkSemaphore semaphore
,
320 uint32_t* pImageIndex
)
322 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, _swapchain
);
324 return swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
328 VkResult
anv_QueuePresentKHR(
330 const VkPresentInfoKHR
* pPresentInfo
)
332 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
335 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
336 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
338 assert(anv_device_from_handle(swapchain
->device
) == queue
->device
);
340 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
341 result
= anv_CreateFence(anv_device_to_handle(queue
->device
),
342 &(VkFenceCreateInfo
) {
343 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
345 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
346 if (result
!= VK_SUCCESS
)
349 anv_ResetFences(anv_device_to_handle(queue
->device
),
350 1, &swapchain
->fences
[0]);
353 anv_QueueSubmit(_queue
, 0, NULL
, swapchain
->fences
[0]);
355 result
= swapchain
->queue_present(swapchain
,
356 pPresentInfo
->pImageIndices
[i
]);
357 /* TODO: What if one of them returns OUT_OF_DATE? */
358 if (result
!= VK_SUCCESS
)
361 VkFence last
= swapchain
->fences
[2];
362 swapchain
->fences
[2] = swapchain
->fences
[1];
363 swapchain
->fences
[1] = swapchain
->fences
[0];
364 swapchain
->fences
[0] = last
;
366 if (last
!= VK_NULL_HANDLE
) {
367 anv_WaitForFences(anv_device_to_handle(queue
->device
),