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
),
38 radv_init_wsi(struct radv_physical_device
*physical_device
)
42 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
44 #ifdef VK_USE_PLATFORM_XCB_KHR
45 result
= wsi_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
46 if (result
!= VK_SUCCESS
)
50 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
51 result
= wsi_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
52 radv_physical_device_to_handle(physical_device
),
54 if (result
!= VK_SUCCESS
) {
55 #ifdef VK_USE_PLATFORM_XCB_KHR
56 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
66 radv_finish_wsi(struct radv_physical_device
*physical_device
)
68 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
69 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
71 #ifdef VK_USE_PLATFORM_XCB_KHR
72 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
76 void radv_DestroySurfaceKHR(
78 VkSurfaceKHR _surface
,
79 const VkAllocationCallbacks
* pAllocator
)
81 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
82 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
84 vk_free2(&instance
->alloc
, pAllocator
, surface
);
87 VkResult
radv_GetPhysicalDeviceSurfaceSupportKHR(
88 VkPhysicalDevice physicalDevice
,
89 uint32_t queueFamilyIndex
,
90 VkSurfaceKHR _surface
,
93 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
94 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
95 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
97 return iface
->get_support(surface
, &device
->wsi_device
,
98 &device
->instance
->alloc
,
99 queueFamilyIndex
, device
->local_fd
, true, pSupported
);
102 VkResult
radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
103 VkPhysicalDevice physicalDevice
,
104 VkSurfaceKHR _surface
,
105 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
107 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
108 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
109 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
111 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
114 VkResult
radv_GetPhysicalDeviceSurfaceFormatsKHR(
115 VkPhysicalDevice physicalDevice
,
116 VkSurfaceKHR _surface
,
117 uint32_t* pSurfaceFormatCount
,
118 VkSurfaceFormatKHR
* pSurfaceFormats
)
120 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
121 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
122 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
124 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
128 VkResult
radv_GetPhysicalDeviceSurfacePresentModesKHR(
129 VkPhysicalDevice physicalDevice
,
130 VkSurfaceKHR _surface
,
131 uint32_t* pPresentModeCount
,
132 VkPresentModeKHR
* pPresentModes
)
134 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
135 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
136 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
138 return iface
->get_present_modes(surface
, pPresentModeCount
,
143 radv_wsi_image_create(VkDevice device_h
,
144 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
145 const VkAllocationCallbacks
* pAllocator
,
146 bool needs_linear_copy
,
148 struct wsi_image
*wsi_image
)
150 VkResult result
= VK_SUCCESS
;
151 struct radeon_surf
*surface
;
153 struct radv_image
*image
;
155 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
157 result
= radv_image_create(device_h
,
158 &(struct radv_image_create_info
) {
160 &(VkImageCreateInfo
) {
161 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
162 .imageType
= VK_IMAGE_TYPE_2D
,
163 .format
= pCreateInfo
->imageFormat
,
165 .width
= pCreateInfo
->imageExtent
.width
,
166 .height
= pCreateInfo
->imageExtent
.height
,
172 /* FIXME: Need a way to use X tiling to allow scanout */
173 .tiling
= linear
? VK_IMAGE_TILING_LINEAR
: VK_IMAGE_TILING_OPTIMAL
,
174 .usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
180 if (result
!= VK_SUCCESS
)
183 image
= radv_image_from_handle(image_h
);
185 VkDeviceMemory memory_h
;
187 const VkMemoryDedicatedAllocateInfoKHR ded_alloc
= {
188 .sType
= VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
,
190 .buffer
= VK_NULL_HANDLE
,
194 /* Find the first VRAM memory type, or GART for PRIME images. */
195 int memory_type_index
= -1;
196 for (int i
= 0; i
< device
->physical_device
->memory_properties
.memoryTypeCount
; ++i
) {
197 bool is_local
= !!(device
->physical_device
->memory_properties
.memoryTypes
[i
].propertyFlags
& VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
);
198 if ((linear
&& !is_local
) || (!linear
&& is_local
)) {
199 memory_type_index
= i
;
205 if (memory_type_index
== -1)
206 memory_type_index
= 0;
208 result
= radv_alloc_memory(device_h
,
209 &(VkMemoryAllocateInfo
) {
210 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
212 .allocationSize
= image
->size
,
213 .memoryTypeIndex
= memory_type_index
,
215 NULL
/* XXX: pAllocator */,
216 RADV_MEM_IMPLICIT_SYNC
,
218 if (result
!= VK_SUCCESS
)
219 goto fail_create_image
;
221 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
224 * return the fd for the image in the no copy mode,
225 * or the fd for the linear image if a copy is required.
227 if (!needs_linear_copy
|| (needs_linear_copy
&& linear
)) {
228 RADV_FROM_HANDLE(radv_device_memory
, memory
, memory_h
);
229 if (!radv_get_memory_fd(device
, memory
, &fd
))
230 goto fail_alloc_memory
;
234 surface
= &image
->surface
;
236 wsi_image
->image
= image_h
;
237 wsi_image
->memory
= memory_h
;
238 wsi_image
->size
= image
->size
;
239 wsi_image
->offset
= image
->offset
;
240 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
241 wsi_image
->row_pitch
=
242 surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
244 wsi_image
->row_pitch
=
245 surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
249 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
252 radv_DestroyImage(device_h
, image_h
, pAllocator
);
258 radv_wsi_image_free(VkDevice device
,
259 const VkAllocationCallbacks
* pAllocator
,
260 struct wsi_image
*wsi_image
)
262 radv_DestroyImage(device
, wsi_image
->image
, pAllocator
);
264 radv_FreeMemory(device
, wsi_image
->memory
, pAllocator
);
267 static const struct wsi_image_fns radv_wsi_image_fns
= {
268 .create_wsi_image
= radv_wsi_image_create
,
269 .free_wsi_image
= radv_wsi_image_free
,
272 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
274 radv_wsi_free_prime_command_buffers(struct radv_device
*device
,
275 struct wsi_swapchain
*swapchain
)
277 const int num_pools
= NUM_PRIME_POOLS
;
278 const int num_images
= swapchain
->image_count
;
280 for (i
= 0; i
< num_pools
; i
++) {
281 radv_FreeCommandBuffers(radv_device_to_handle(device
),
282 swapchain
->cmd_pools
[i
],
283 swapchain
->image_count
,
284 &swapchain
->cmd_buffers
[i
* num_images
]);
286 radv_DestroyCommandPool(radv_device_to_handle(device
),
287 swapchain
->cmd_pools
[i
],
293 radv_wsi_create_prime_command_buffers(struct radv_device
*device
,
294 const VkAllocationCallbacks
*alloc
,
295 struct wsi_swapchain
*swapchain
)
297 const int num_pools
= NUM_PRIME_POOLS
;
298 const int num_images
= swapchain
->image_count
;
299 int num_cmd_buffers
= num_images
* num_pools
; //TODO bump to MAX_QUEUE_FAMILIES
303 swapchain
->cmd_buffers
= vk_alloc(alloc
, (sizeof(VkCommandBuffer
) * num_cmd_buffers
), 8,
304 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
305 if (!swapchain
->cmd_buffers
)
306 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
308 memset(swapchain
->cmd_buffers
, 0, sizeof(VkCommandBuffer
) * num_cmd_buffers
);
309 memset(swapchain
->cmd_pools
, 0, sizeof(VkCommandPool
) * num_pools
);
310 for (i
= 0; i
< num_pools
; i
++) {
311 VkCommandPoolCreateInfo pool_create_info
;
313 pool_create_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
314 pool_create_info
.pNext
= NULL
;
315 pool_create_info
.flags
= 0;
316 pool_create_info
.queueFamilyIndex
= i
;
318 result
= radv_CreateCommandPool(radv_device_to_handle(device
),
319 &pool_create_info
, alloc
,
320 &swapchain
->cmd_pools
[i
]);
321 if (result
!= VK_SUCCESS
)
324 VkCommandBufferAllocateInfo cmd_buffer_info
;
325 cmd_buffer_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
326 cmd_buffer_info
.pNext
= NULL
;
327 cmd_buffer_info
.commandPool
= swapchain
->cmd_pools
[i
];
328 cmd_buffer_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
329 cmd_buffer_info
.commandBufferCount
= num_images
;
331 result
= radv_AllocateCommandBuffers(radv_device_to_handle(device
),
333 &swapchain
->cmd_buffers
[i
* num_images
]);
334 if (result
!= VK_SUCCESS
)
336 for (j
= 0; j
< num_images
; j
++) {
337 VkImage image
, linear_image
;
338 int idx
= (i
* num_images
) + j
;
340 swapchain
->get_image_and_linear(swapchain
, j
, &image
, &linear_image
);
341 VkCommandBufferBeginInfo begin_info
= {0};
343 begin_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
;
345 radv_BeginCommandBuffer(swapchain
->cmd_buffers
[idx
], &begin_info
);
347 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
]),
348 radv_image_from_handle(image
),
349 radv_image_from_handle(linear_image
));
351 radv_EndCommandBuffer(swapchain
->cmd_buffers
[idx
]);
356 radv_wsi_free_prime_command_buffers(device
, swapchain
);
360 VkResult
radv_CreateSwapchainKHR(
362 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
363 const VkAllocationCallbacks
* pAllocator
,
364 VkSwapchainKHR
* pSwapchain
)
366 RADV_FROM_HANDLE(radv_device
, device
, _device
);
367 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
368 struct wsi_interface
*iface
=
369 device
->physical_device
->wsi_device
.wsi
[surface
->platform
];
370 struct wsi_swapchain
*swapchain
;
371 const VkAllocationCallbacks
*alloc
;
375 alloc
= &device
->alloc
;
376 VkResult result
= iface
->create_swapchain(surface
, _device
,
377 &device
->physical_device
->wsi_device
,
378 device
->physical_device
->local_fd
,
380 alloc
, &radv_wsi_image_fns
,
382 if (result
!= VK_SUCCESS
)
386 swapchain
->alloc
= *pAllocator
;
388 swapchain
->alloc
= device
->alloc
;
390 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
391 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
393 if (swapchain
->needs_linear_copy
) {
394 result
= radv_wsi_create_prime_command_buffers(device
, alloc
,
396 if (result
!= VK_SUCCESS
)
400 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
405 void radv_DestroySwapchainKHR(
407 VkSwapchainKHR _swapchain
,
408 const VkAllocationCallbacks
* pAllocator
)
410 RADV_FROM_HANDLE(radv_device
, device
, _device
);
411 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
412 const VkAllocationCallbacks
*alloc
;
420 alloc
= &device
->alloc
;
422 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
423 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
424 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
427 if (swapchain
->needs_linear_copy
)
428 radv_wsi_free_prime_command_buffers(device
, swapchain
);
430 swapchain
->destroy(swapchain
, alloc
);
433 VkResult
radv_GetSwapchainImagesKHR(
435 VkSwapchainKHR _swapchain
,
436 uint32_t* pSwapchainImageCount
,
437 VkImage
* pSwapchainImages
)
439 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
441 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
445 VkResult
radv_AcquireNextImageKHR(
447 VkSwapchainKHR _swapchain
,
449 VkSemaphore semaphore
,
451 uint32_t* pImageIndex
)
453 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
454 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
456 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
459 if (fence
&& (result
== VK_SUCCESS
|| result
== VK_SUBOPTIMAL_KHR
)) {
460 fence
->submitted
= true;
461 fence
->signalled
= true;
466 VkResult
radv_QueuePresentKHR(
468 const VkPresentInfoKHR
* pPresentInfo
)
470 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
471 VkResult result
= VK_SUCCESS
;
472 const VkPresentRegionsKHR
*regions
=
473 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
475 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
476 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
477 struct radeon_winsys_cs
*cs
;
478 const VkPresentRegionKHR
*region
= NULL
;
479 VkResult item_result
;
480 struct radv_winsys_sem_info sem_info
;
482 item_result
= radv_alloc_sem_info(&sem_info
,
483 pPresentInfo
->waitSemaphoreCount
,
484 pPresentInfo
->pWaitSemaphores
,
487 if (pPresentInfo
->pResults
!= NULL
)
488 pPresentInfo
->pResults
[i
] = item_result
;
489 result
= result
== VK_SUCCESS
? item_result
: result
;
490 if (item_result
!= VK_SUCCESS
) {
491 radv_free_sem_info(&sem_info
);
495 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
496 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
497 item_result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
498 &(VkFenceCreateInfo
) {
499 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
501 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
502 if (pPresentInfo
->pResults
!= NULL
)
503 pPresentInfo
->pResults
[i
] = item_result
;
504 result
= result
== VK_SUCCESS
? item_result
: result
;
505 if (item_result
!= VK_SUCCESS
) {
506 radv_free_sem_info(&sem_info
);
510 radv_ResetFences(radv_device_to_handle(queue
->device
),
511 1, &swapchain
->fences
[0]);
514 if (swapchain
->needs_linear_copy
) {
515 int idx
= (queue
->queue_family_index
* swapchain
->image_count
) + pPresentInfo
->pImageIndices
[i
];
516 cs
= radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
])->cs
;
518 cs
= queue
->device
->empty_cs
[queue
->queue_family_index
];
519 RADV_FROM_HANDLE(radv_fence
, fence
, swapchain
->fences
[0]);
520 struct radeon_winsys_fence
*base_fence
= fence
->fence
;
521 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
523 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
528 fence
->submitted
= true;
530 if (regions
&& regions
->pRegions
)
531 region
= ®ions
->pRegions
[i
];
533 item_result
= swapchain
->queue_present(swapchain
,
534 pPresentInfo
->pImageIndices
[i
],
536 /* TODO: What if one of them returns OUT_OF_DATE? */
537 if (pPresentInfo
->pResults
!= NULL
)
538 pPresentInfo
->pResults
[i
] = item_result
;
539 result
= result
== VK_SUCCESS
? item_result
: result
;
540 if (item_result
!= VK_SUCCESS
) {
541 radv_free_sem_info(&sem_info
);
545 VkFence last
= swapchain
->fences
[2];
546 swapchain
->fences
[2] = swapchain
->fences
[1];
547 swapchain
->fences
[1] = swapchain
->fences
[0];
548 swapchain
->fences
[0] = last
;
550 if (last
!= VK_NULL_HANDLE
) {
551 radv_WaitForFences(radv_device_to_handle(queue
->device
),
555 radv_free_sem_info(&sem_info
);