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 "radv_meta.h"
28 #include "wsi_common.h"
30 #include "util/macros.h"
32 #define WSI_CB(x) .x = radv_##x
33 MAYBE_UNUSED
static const struct wsi_callbacks wsi_cbs
= {
34 WSI_CB(GetPhysicalDeviceFormatProperties
),
37 static PFN_vkVoidFunction
38 radv_wsi_proc_addr(VkPhysicalDevice physicalDevice
, const char *pName
)
40 return radv_lookup_entrypoint(pName
);
44 radv_wsi_queue_get_family_index(VkQueue _queue
)
46 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
47 return queue
->queue_family_index
;
51 radv_init_wsi(struct radv_physical_device
*physical_device
)
55 wsi_device_init(&physical_device
->wsi_device
,
56 radv_physical_device_to_handle(physical_device
),
59 physical_device
->wsi_device
.queue_get_family_index
=
60 radv_wsi_queue_get_family_index
;
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
)
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
),
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
);
84 radv_finish_wsi(struct radv_physical_device
*physical_device
)
86 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
87 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
89 #ifdef VK_USE_PLATFORM_XCB_KHR
90 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
94 void radv_DestroySurfaceKHR(
96 VkSurfaceKHR _surface
,
97 const VkAllocationCallbacks
* pAllocator
)
99 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
100 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
102 vk_free2(&instance
->alloc
, pAllocator
, surface
);
105 VkResult
radv_GetPhysicalDeviceSurfaceSupportKHR(
106 VkPhysicalDevice physicalDevice
,
107 uint32_t queueFamilyIndex
,
108 VkSurfaceKHR _surface
,
109 VkBool32
* pSupported
)
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
];
115 return iface
->get_support(surface
, &device
->wsi_device
,
116 &device
->instance
->alloc
,
117 queueFamilyIndex
, device
->local_fd
, true, pSupported
);
120 VkResult
radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
121 VkPhysicalDevice physicalDevice
,
122 VkSurfaceKHR _surface
,
123 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
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
];
129 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
132 VkResult
radv_GetPhysicalDeviceSurfaceFormatsKHR(
133 VkPhysicalDevice physicalDevice
,
134 VkSurfaceKHR _surface
,
135 uint32_t* pSurfaceFormatCount
,
136 VkSurfaceFormatKHR
* pSurfaceFormats
)
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
];
142 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
146 VkResult
radv_GetPhysicalDeviceSurfacePresentModesKHR(
147 VkPhysicalDevice physicalDevice
,
148 VkSurfaceKHR _surface
,
149 uint32_t* pPresentModeCount
,
150 VkPresentModeKHR
* pPresentModes
)
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
];
156 return iface
->get_present_modes(surface
, pPresentModeCount
,
161 radv_wsi_image_create(VkDevice device_h
,
162 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
163 const VkAllocationCallbacks
* pAllocator
,
164 struct wsi_image
*wsi_image
)
166 VkResult result
= VK_SUCCESS
;
167 struct radeon_surf
*surface
;
169 struct radv_image
*image
;
171 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
173 result
= radv_image_create(device_h
,
174 &(struct radv_image_create_info
) {
176 &(VkImageCreateInfo
) {
177 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
178 .imageType
= VK_IMAGE_TYPE_2D
,
179 .format
= pCreateInfo
->imageFormat
,
181 .width
= pCreateInfo
->imageExtent
.width
,
182 .height
= pCreateInfo
->imageExtent
.height
,
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
,
196 if (result
!= VK_SUCCESS
)
199 image
= radv_image_from_handle(image_h
);
201 VkDeviceMemory memory_h
;
203 const VkMemoryDedicatedAllocateInfoKHR ded_alloc
= {
204 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
206 .buffer
= VK_NULL_HANDLE
,
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
);
215 memory_type_index
= i
;
221 if (memory_type_index
== -1)
222 memory_type_index
= 0;
224 result
= radv_alloc_memory(device_h
,
225 &(VkMemoryAllocateInfo
) {
226 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
228 .allocationSize
= image
->size
,
229 .memoryTypeIndex
= memory_type_index
,
231 NULL
/* XXX: pAllocator */,
232 RADV_MEM_IMPLICIT_SYNC
,
234 if (result
!= VK_SUCCESS
)
235 goto fail_create_image
;
237 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
239 RADV_FROM_HANDLE(radv_device_memory
, memory
, memory_h
);
240 if (!radv_get_memory_fd(device
, memory
, &fd
))
241 goto fail_alloc_memory
;
244 surface
= &image
->surface
;
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
;
254 wsi_image
->row_pitch
=
255 surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
259 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
262 radv_DestroyImage(device_h
, image_h
, pAllocator
);
268 radv_wsi_image_free(VkDevice device
,
269 const VkAllocationCallbacks
* pAllocator
,
270 struct wsi_image
*wsi_image
)
272 radv_DestroyImage(device
, wsi_image
->image
, pAllocator
);
274 radv_FreeMemory(device
, wsi_image
->memory
, pAllocator
);
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
,
282 VkResult
radv_CreateSwapchainKHR(
284 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
285 const VkAllocationCallbacks
* pAllocator
,
286 VkSwapchainKHR
* pSwapchain
)
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
;
297 alloc
= &device
->alloc
;
298 VkResult result
= iface
->create_swapchain(surface
, _device
,
299 &device
->physical_device
->wsi_device
,
300 device
->physical_device
->local_fd
,
302 alloc
, &radv_wsi_image_fns
,
304 if (result
!= VK_SUCCESS
)
308 swapchain
->alloc
= *pAllocator
;
310 swapchain
->alloc
= device
->alloc
;
312 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
313 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
315 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
320 void radv_DestroySwapchainKHR(
322 VkSwapchainKHR _swapchain
,
323 const VkAllocationCallbacks
* pAllocator
)
325 RADV_FROM_HANDLE(radv_device
, device
, _device
);
326 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
327 const VkAllocationCallbacks
*alloc
;
335 alloc
= &device
->alloc
;
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
);
342 swapchain
->destroy(swapchain
, alloc
);
345 VkResult
radv_GetSwapchainImagesKHR(
347 VkSwapchainKHR _swapchain
,
348 uint32_t* pSwapchainImageCount
,
349 VkImage
* pSwapchainImages
)
351 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
353 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
357 VkResult
radv_AcquireNextImageKHR(
359 VkSwapchainKHR _swapchain
,
361 VkSemaphore semaphore
,
363 uint32_t* pImageIndex
)
365 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
366 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
368 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
371 if (fence
&& (result
== VK_SUCCESS
|| result
== VK_SUBOPTIMAL_KHR
)) {
372 fence
->submitted
= true;
373 fence
->signalled
= true;
378 VkResult
radv_QueuePresentKHR(
380 const VkPresentInfoKHR
* pPresentInfo
)
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
);
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
;
394 item_result
= radv_alloc_sem_info(&sem_info
,
395 pPresentInfo
->waitSemaphoreCount
,
396 pPresentInfo
->pWaitSemaphores
,
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
);
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
,
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
);
422 radv_ResetFences(radv_device_to_handle(queue
->device
),
423 1, &swapchain
->fences
[0]);
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
;
431 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
436 fence
->submitted
= true;
438 if (regions
&& regions
->pRegions
)
439 region
= ®ions
->pRegions
[i
];
441 item_result
= swapchain
->queue_present(swapchain
,
443 pPresentInfo
->waitSemaphoreCount
,
444 pPresentInfo
->pWaitSemaphores
,
445 pPresentInfo
->pImageIndices
[i
],
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
);
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
;
461 if (last
!= VK_NULL_HANDLE
) {
462 radv_WaitForFences(radv_device_to_handle(queue
->device
),
466 radv_free_sem_info(&sem_info
);