2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
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:
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
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
26 #include "radv_private.h"
27 #include "wsi_common.h"
29 static const struct wsi_callbacks wsi_cbs
= {
30 .get_phys_device_format_properties
= radv_GetPhysicalDeviceFormatProperties
,
34 radv_init_wsi(struct radv_physical_device
*physical_device
)
38 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
40 #ifdef VK_USE_PLATFORM_XCB_KHR
41 result
= wsi_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
42 if (result
!= VK_SUCCESS
)
46 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
47 result
= wsi_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
48 radv_physical_device_to_handle(physical_device
),
50 if (result
!= VK_SUCCESS
) {
51 #ifdef VK_USE_PLATFORM_XCB_KHR
52 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
62 radv_finish_wsi(struct radv_physical_device
*physical_device
)
64 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
65 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
67 #ifdef VK_USE_PLATFORM_XCB_KHR
68 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
72 void radv_DestroySurfaceKHR(
74 VkSurfaceKHR _surface
,
75 const VkAllocationCallbacks
* pAllocator
)
77 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
78 RADV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
80 vk_free2(&instance
->alloc
, pAllocator
, surface
);
83 VkResult
radv_GetPhysicalDeviceSurfaceSupportKHR(
84 VkPhysicalDevice physicalDevice
,
85 uint32_t queueFamilyIndex
,
86 VkSurfaceKHR _surface
,
89 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
90 RADV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
91 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
93 return iface
->get_support(surface
, &device
->wsi_device
,
94 &device
->instance
->alloc
,
95 queueFamilyIndex
, pSupported
);
98 VkResult
radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
99 VkPhysicalDevice physicalDevice
,
100 VkSurfaceKHR _surface
,
101 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
103 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
104 RADV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
105 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
107 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
110 VkResult
radv_GetPhysicalDeviceSurfaceFormatsKHR(
111 VkPhysicalDevice physicalDevice
,
112 VkSurfaceKHR _surface
,
113 uint32_t* pSurfaceFormatCount
,
114 VkSurfaceFormatKHR
* pSurfaceFormats
)
116 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
117 RADV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
118 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
120 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
124 VkResult
radv_GetPhysicalDeviceSurfacePresentModesKHR(
125 VkPhysicalDevice physicalDevice
,
126 VkSurfaceKHR _surface
,
127 uint32_t* pPresentModeCount
,
128 VkPresentModeKHR
* pPresentModes
)
130 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
131 RADV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
132 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
134 return iface
->get_present_modes(surface
, pPresentModeCount
,
139 radv_wsi_image_create(VkDevice device_h
,
140 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
141 const VkAllocationCallbacks
* pAllocator
,
143 VkDeviceMemory
*memory_p
,
146 uint32_t *row_pitch
, int *fd_p
)
148 struct radv_device
*device
= radv_device_from_handle(device_h
);
149 VkResult result
= VK_SUCCESS
;
150 struct radeon_surf
*surface
;
152 struct radv_image
*image
;
156 result
= radv_image_create(device_h
,
157 &(struct radv_image_create_info
) {
159 &(VkImageCreateInfo
) {
160 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
161 .imageType
= VK_IMAGE_TYPE_2D
,
162 .format
= pCreateInfo
->imageFormat
,
164 .width
= pCreateInfo
->imageExtent
.width
,
165 .height
= pCreateInfo
->imageExtent
.height
,
171 /* FIXME: Need a way to use X tiling to allow scanout */
172 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
173 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
179 if (result
!= VK_SUCCESS
)
182 image
= radv_image_from_handle(image_h
);
184 VkDeviceMemory memory_h
;
185 struct radv_device_memory
*memory
;
186 result
= radv_AllocateMemory(device_h
,
187 &(VkMemoryAllocateInfo
) {
188 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
189 .allocationSize
= image
->size
,
190 .memoryTypeIndex
= 0,
192 NULL
/* XXX: pAllocator */,
194 if (result
!= VK_SUCCESS
)
195 goto fail_create_image
;
197 memory
= radv_device_memory_from_handle(memory_h
);
199 radv_BindImageMemory(VK_NULL_HANDLE
, image_h
, memory_h
, 0);
201 bret
= device
->ws
->buffer_get_fd(device
->ws
,
204 goto fail_alloc_memory
;
207 struct radeon_bo_metadata metadata
;
208 radv_init_metadata(device
, image
, &metadata
);
209 device
->ws
->buffer_set_metadata(memory
->bo
, &metadata
);
211 surface
= &image
->surface
;
214 *memory_p
= memory_h
;
217 *offset
= image
->offset
;
218 *row_pitch
= surface
->level
[0].pitch_bytes
;
221 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
224 radv_DestroyImage(device_h
, image_h
, pAllocator
);
230 radv_wsi_image_free(VkDevice device
,
231 const VkAllocationCallbacks
* pAllocator
,
233 VkDeviceMemory memory_h
)
235 radv_DestroyImage(device
, image_h
, pAllocator
);
237 radv_FreeMemory(device
, memory_h
, pAllocator
);
240 static const struct wsi_image_fns radv_wsi_image_fns
= {
241 .create_wsi_image
= radv_wsi_image_create
,
242 .free_wsi_image
= radv_wsi_image_free
,
245 VkResult
radv_CreateSwapchainKHR(
247 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
248 const VkAllocationCallbacks
* pAllocator
,
249 VkSwapchainKHR
* pSwapchain
)
251 RADV_FROM_HANDLE(radv_device
, device
, _device
);
252 RADV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
253 struct wsi_interface
*iface
=
254 device
->instance
->physicalDevice
.wsi_device
.wsi
[surface
->platform
];
255 struct wsi_swapchain
*swapchain
;
256 const VkAllocationCallbacks
*alloc
;
260 alloc
= &device
->alloc
;
261 VkResult result
= iface
->create_swapchain(surface
, _device
,
262 &device
->instance
->physicalDevice
.wsi_device
,
264 alloc
, &radv_wsi_image_fns
,
266 if (result
!= VK_SUCCESS
)
270 swapchain
->alloc
= *pAllocator
;
272 swapchain
->alloc
= device
->alloc
;
274 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
275 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
277 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
282 void radv_DestroySwapchainKHR(
284 VkSwapchainKHR _swapchain
,
285 const VkAllocationCallbacks
* pAllocator
)
287 RADV_FROM_HANDLE(radv_device
, device
, _device
);
288 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
289 const VkAllocationCallbacks
*alloc
;
294 alloc
= &device
->alloc
;
296 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
297 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
298 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
301 swapchain
->destroy(swapchain
, alloc
);
304 VkResult
radv_GetSwapchainImagesKHR(
306 VkSwapchainKHR _swapchain
,
307 uint32_t* pSwapchainImageCount
,
308 VkImage
* pSwapchainImages
)
310 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
312 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
316 VkResult
radv_AcquireNextImageKHR(
318 VkSwapchainKHR _swapchain
,
320 VkSemaphore semaphore
,
322 uint32_t* pImageIndex
)
324 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
325 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
327 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
330 if (fence
&& result
== VK_SUCCESS
) {
331 fence
->submitted
= true;
332 fence
->signalled
= true;
338 VkResult
radv_QueuePresentKHR(
340 const VkPresentInfoKHR
* pPresentInfo
)
342 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
343 VkResult result
= VK_SUCCESS
;
345 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
346 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
348 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
349 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
350 result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
351 &(VkFenceCreateInfo
) {
352 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
354 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
355 if (result
!= VK_SUCCESS
)
358 radv_ResetFences(radv_device_to_handle(queue
->device
),
359 1, &swapchain
->fences
[0]);
362 radv_QueueSubmit(_queue
, 0, NULL
, swapchain
->fences
[0]);
364 result
= swapchain
->queue_present(swapchain
,
365 pPresentInfo
->pImageIndices
[i
]);
366 /* TODO: What if one of them returns OUT_OF_DATE? */
367 if (result
!= VK_SUCCESS
)
370 VkFence last
= swapchain
->fences
[2];
371 swapchain
->fences
[2] = swapchain
->fences
[1];
372 swapchain
->fences
[1] = swapchain
->fences
[0];
373 swapchain
->fences
[0] = last
;
375 if (last
!= VK_NULL_HANDLE
) {
376 radv_WaitForFences(radv_device_to_handle(queue
->device
),