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_init_wsi(struct radv_physical_device
*physical_device
)
48 wsi_device_init(&physical_device
->wsi_device
,
49 radv_physical_device_to_handle(physical_device
),
52 #ifdef VK_USE_PLATFORM_XCB_KHR
53 result
= wsi_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
54 if (result
!= VK_SUCCESS
)
58 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
59 result
= wsi_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
60 radv_physical_device_to_handle(physical_device
),
62 if (result
!= VK_SUCCESS
) {
63 #ifdef VK_USE_PLATFORM_XCB_KHR
64 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
74 radv_finish_wsi(struct radv_physical_device
*physical_device
)
76 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
77 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
79 #ifdef VK_USE_PLATFORM_XCB_KHR
80 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
84 void radv_DestroySurfaceKHR(
86 VkSurfaceKHR _surface
,
87 const VkAllocationCallbacks
* pAllocator
)
89 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
90 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
92 vk_free2(&instance
->alloc
, pAllocator
, surface
);
95 VkResult
radv_GetPhysicalDeviceSurfaceSupportKHR(
96 VkPhysicalDevice physicalDevice
,
97 uint32_t queueFamilyIndex
,
98 VkSurfaceKHR _surface
,
101 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
102 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
103 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
105 return iface
->get_support(surface
, &device
->wsi_device
,
106 &device
->instance
->alloc
,
107 queueFamilyIndex
, device
->local_fd
, true, pSupported
);
110 VkResult
radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
111 VkPhysicalDevice physicalDevice
,
112 VkSurfaceKHR _surface
,
113 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
115 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
116 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
117 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
119 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
122 VkResult
radv_GetPhysicalDeviceSurfaceFormatsKHR(
123 VkPhysicalDevice physicalDevice
,
124 VkSurfaceKHR _surface
,
125 uint32_t* pSurfaceFormatCount
,
126 VkSurfaceFormatKHR
* pSurfaceFormats
)
128 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
129 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
130 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
132 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
136 VkResult
radv_GetPhysicalDeviceSurfacePresentModesKHR(
137 VkPhysicalDevice physicalDevice
,
138 VkSurfaceKHR _surface
,
139 uint32_t* pPresentModeCount
,
140 VkPresentModeKHR
* pPresentModes
)
142 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
143 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
144 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
146 return iface
->get_present_modes(surface
, pPresentModeCount
,
151 radv_wsi_image_create(VkDevice device_h
,
152 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
153 const VkAllocationCallbacks
* pAllocator
,
154 bool needs_linear_copy
,
156 struct wsi_image
*wsi_image
)
158 VkResult result
= VK_SUCCESS
;
159 struct radeon_surf
*surface
;
161 struct radv_image
*image
;
163 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
165 result
= radv_image_create(device_h
,
166 &(struct radv_image_create_info
) {
168 &(VkImageCreateInfo
) {
169 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
170 .imageType
= VK_IMAGE_TYPE_2D
,
171 .format
= pCreateInfo
->imageFormat
,
173 .width
= pCreateInfo
->imageExtent
.width
,
174 .height
= pCreateInfo
->imageExtent
.height
,
180 /* FIXME: Need a way to use X tiling to allow scanout */
181 .tiling
= linear
? VK_IMAGE_TILING_LINEAR
: VK_IMAGE_TILING_OPTIMAL
,
182 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
188 if (result
!= VK_SUCCESS
)
191 image
= radv_image_from_handle(image_h
);
193 VkDeviceMemory memory_h
;
195 const VkMemoryDedicatedAllocateInfoKHR ded_alloc
= {
196 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
198 .buffer
= VK_NULL_HANDLE
,
202 /* Find the first VRAM memory type, or GART for PRIME images. */
203 int memory_type_index
= -1;
204 for (int i
= 0; i
< device
->physical_device
->memory_properties
.memoryTypeCount
; ++i
) {
205 bool is_local
= !!(device
->physical_device
->memory_properties
.memoryTypes
[i
].propertyFlags
& VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
);
206 if ((linear
&& !is_local
) || (!linear
&& is_local
)) {
207 memory_type_index
= i
;
213 if (memory_type_index
== -1)
214 memory_type_index
= 0;
216 result
= radv_alloc_memory(device_h
,
217 &(VkMemoryAllocateInfo
) {
218 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
220 .allocationSize
= image
->size
,
221 .memoryTypeIndex
= memory_type_index
,
223 NULL
/* XXX: pAllocator */,
224 RADV_MEM_IMPLICIT_SYNC
,
226 if (result
!= VK_SUCCESS
)
227 goto fail_create_image
;
229 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
232 * return the fd for the image in the no copy mode,
233 * or the fd for the linear image if a copy is required.
235 if (!needs_linear_copy
|| (needs_linear_copy
&& linear
)) {
236 RADV_FROM_HANDLE(radv_device_memory
, memory
, memory_h
);
237 if (!radv_get_memory_fd(device
, memory
, &fd
))
238 goto fail_alloc_memory
;
242 surface
= &image
->surface
;
244 wsi_image
->image
= image_h
;
245 wsi_image
->memory
= memory_h
;
246 wsi_image
->size
= image
->size
;
247 wsi_image
->offset
= image
->offset
;
248 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
249 wsi_image
->row_pitch
=
250 surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
252 wsi_image
->row_pitch
=
253 surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
257 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
260 radv_DestroyImage(device_h
, image_h
, pAllocator
);
266 radv_wsi_image_free(VkDevice device
,
267 const VkAllocationCallbacks
* pAllocator
,
268 struct wsi_image
*wsi_image
)
270 radv_DestroyImage(device
, wsi_image
->image
, pAllocator
);
272 radv_FreeMemory(device
, wsi_image
->memory
, pAllocator
);
275 static const struct wsi_image_fns radv_wsi_image_fns
= {
276 .create_wsi_image
= radv_wsi_image_create
,
277 .free_wsi_image
= radv_wsi_image_free
,
280 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
282 radv_wsi_free_prime_command_buffers(struct radv_device
*device
,
283 struct wsi_swapchain
*swapchain
)
285 const int num_pools
= NUM_PRIME_POOLS
;
286 const int num_images
= swapchain
->image_count
;
288 for (i
= 0; i
< num_pools
; i
++) {
289 radv_FreeCommandBuffers(radv_device_to_handle(device
),
290 swapchain
->cmd_pools
[i
],
291 swapchain
->image_count
,
292 &swapchain
->cmd_buffers
[i
* num_images
]);
294 radv_DestroyCommandPool(radv_device_to_handle(device
),
295 swapchain
->cmd_pools
[i
],
301 radv_wsi_create_prime_command_buffers(struct radv_device
*device
,
302 const VkAllocationCallbacks
*alloc
,
303 struct wsi_swapchain
*swapchain
)
305 const int num_pools
= NUM_PRIME_POOLS
;
306 const int num_images
= swapchain
->image_count
;
307 int num_cmd_buffers
= num_images
* num_pools
; //TODO bump to MAX_QUEUE_FAMILIES
311 swapchain
->cmd_buffers
= vk_alloc(alloc
, (sizeof(VkCommandBuffer
) * num_cmd_buffers
), 8,
312 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
313 if (!swapchain
->cmd_buffers
)
314 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
316 memset(swapchain
->cmd_buffers
, 0, sizeof(VkCommandBuffer
) * num_cmd_buffers
);
317 memset(swapchain
->cmd_pools
, 0, sizeof(VkCommandPool
) * num_pools
);
318 for (i
= 0; i
< num_pools
; i
++) {
319 VkCommandPoolCreateInfo pool_create_info
;
321 pool_create_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
322 pool_create_info
.pNext
= NULL
;
323 pool_create_info
.flags
= 0;
324 pool_create_info
.queueFamilyIndex
= i
;
326 result
= radv_CreateCommandPool(radv_device_to_handle(device
),
327 &pool_create_info
, alloc
,
328 &swapchain
->cmd_pools
[i
]);
329 if (result
!= VK_SUCCESS
)
332 VkCommandBufferAllocateInfo cmd_buffer_info
;
333 cmd_buffer_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
334 cmd_buffer_info
.pNext
= NULL
;
335 cmd_buffer_info
.commandPool
= swapchain
->cmd_pools
[i
];
336 cmd_buffer_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
337 cmd_buffer_info
.commandBufferCount
= num_images
;
339 result
= radv_AllocateCommandBuffers(radv_device_to_handle(device
),
341 &swapchain
->cmd_buffers
[i
* num_images
]);
342 if (result
!= VK_SUCCESS
)
344 for (j
= 0; j
< num_images
; j
++) {
345 VkImage image
, linear_image
;
346 int idx
= (i
* num_images
) + j
;
348 swapchain
->get_image_and_linear(swapchain
, j
, &image
, &linear_image
);
349 VkCommandBufferBeginInfo begin_info
= {0};
351 begin_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
;
353 radv_BeginCommandBuffer(swapchain
->cmd_buffers
[idx
], &begin_info
);
355 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
]),
356 radv_image_from_handle(image
),
357 radv_image_from_handle(linear_image
));
359 radv_EndCommandBuffer(swapchain
->cmd_buffers
[idx
]);
364 radv_wsi_free_prime_command_buffers(device
, swapchain
);
368 VkResult
radv_CreateSwapchainKHR(
370 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
371 const VkAllocationCallbacks
* pAllocator
,
372 VkSwapchainKHR
* pSwapchain
)
374 RADV_FROM_HANDLE(radv_device
, device
, _device
);
375 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
376 struct wsi_interface
*iface
=
377 device
->physical_device
->wsi_device
.wsi
[surface
->platform
];
378 struct wsi_swapchain
*swapchain
;
379 const VkAllocationCallbacks
*alloc
;
383 alloc
= &device
->alloc
;
384 VkResult result
= iface
->create_swapchain(surface
, _device
,
385 &device
->physical_device
->wsi_device
,
386 device
->physical_device
->local_fd
,
388 alloc
, &radv_wsi_image_fns
,
390 if (result
!= VK_SUCCESS
)
394 swapchain
->alloc
= *pAllocator
;
396 swapchain
->alloc
= device
->alloc
;
398 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
399 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
401 if (swapchain
->needs_linear_copy
) {
402 result
= radv_wsi_create_prime_command_buffers(device
, alloc
,
404 if (result
!= VK_SUCCESS
)
408 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
413 void radv_DestroySwapchainKHR(
415 VkSwapchainKHR _swapchain
,
416 const VkAllocationCallbacks
* pAllocator
)
418 RADV_FROM_HANDLE(radv_device
, device
, _device
);
419 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
420 const VkAllocationCallbacks
*alloc
;
428 alloc
= &device
->alloc
;
430 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
431 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
432 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
435 if (swapchain
->needs_linear_copy
)
436 radv_wsi_free_prime_command_buffers(device
, swapchain
);
438 swapchain
->destroy(swapchain
, alloc
);
441 VkResult
radv_GetSwapchainImagesKHR(
443 VkSwapchainKHR _swapchain
,
444 uint32_t* pSwapchainImageCount
,
445 VkImage
* pSwapchainImages
)
447 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
449 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
453 VkResult
radv_AcquireNextImageKHR(
455 VkSwapchainKHR _swapchain
,
457 VkSemaphore semaphore
,
459 uint32_t* pImageIndex
)
461 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
462 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
464 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
467 if (fence
&& (result
== VK_SUCCESS
|| result
== VK_SUBOPTIMAL_KHR
)) {
468 fence
->submitted
= true;
469 fence
->signalled
= true;
474 VkResult
radv_QueuePresentKHR(
476 const VkPresentInfoKHR
* pPresentInfo
)
478 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
479 VkResult result
= VK_SUCCESS
;
480 const VkPresentRegionsKHR
*regions
=
481 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
483 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
484 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
485 struct radeon_winsys_cs
*cs
;
486 const VkPresentRegionKHR
*region
= NULL
;
487 VkResult item_result
;
488 struct radv_winsys_sem_info sem_info
;
490 item_result
= radv_alloc_sem_info(&sem_info
,
491 pPresentInfo
->waitSemaphoreCount
,
492 pPresentInfo
->pWaitSemaphores
,
495 if (pPresentInfo
->pResults
!= NULL
)
496 pPresentInfo
->pResults
[i
] = item_result
;
497 result
= result
== VK_SUCCESS
? item_result
: result
;
498 if (item_result
!= VK_SUCCESS
) {
499 radv_free_sem_info(&sem_info
);
503 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
504 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
505 item_result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
506 &(VkFenceCreateInfo
) {
507 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
509 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
510 if (pPresentInfo
->pResults
!= NULL
)
511 pPresentInfo
->pResults
[i
] = item_result
;
512 result
= result
== VK_SUCCESS
? item_result
: result
;
513 if (item_result
!= VK_SUCCESS
) {
514 radv_free_sem_info(&sem_info
);
518 radv_ResetFences(radv_device_to_handle(queue
->device
),
519 1, &swapchain
->fences
[0]);
522 if (swapchain
->needs_linear_copy
) {
523 int idx
= (queue
->queue_family_index
* swapchain
->image_count
) + pPresentInfo
->pImageIndices
[i
];
524 cs
= radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
])->cs
;
526 cs
= queue
->device
->empty_cs
[queue
->queue_family_index
];
527 RADV_FROM_HANDLE(radv_fence
, fence
, swapchain
->fences
[0]);
528 struct radeon_winsys_fence
*base_fence
= fence
->fence
;
529 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
531 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
536 fence
->submitted
= true;
538 if (regions
&& regions
->pRegions
)
539 region
= ®ions
->pRegions
[i
];
541 item_result
= swapchain
->queue_present(swapchain
,
542 pPresentInfo
->pImageIndices
[i
],
544 /* TODO: What if one of them returns OUT_OF_DATE? */
545 if (pPresentInfo
->pResults
!= NULL
)
546 pPresentInfo
->pResults
[i
] = item_result
;
547 result
= result
== VK_SUCCESS
? item_result
: result
;
548 if (item_result
!= VK_SUCCESS
) {
549 radv_free_sem_info(&sem_info
);
553 VkFence last
= swapchain
->fences
[2];
554 swapchain
->fences
[2] = swapchain
->fences
[1];
555 swapchain
->fences
[1] = swapchain
->fences
[0];
556 swapchain
->fences
[0] = last
;
558 if (last
!= VK_NULL_HANDLE
) {
559 radv_WaitForFences(radv_device_to_handle(queue
->device
),
563 radv_free_sem_info(&sem_info
);