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 static const struct wsi_callbacks wsi_cbs
= {
31 .get_phys_device_format_properties
= radv_GetPhysicalDeviceFormatProperties
,
35 radv_init_wsi(struct radv_physical_device
*physical_device
)
39 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
41 #ifdef VK_USE_PLATFORM_XCB_KHR
42 result
= wsi_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
43 if (result
!= VK_SUCCESS
)
47 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
48 result
= wsi_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
49 radv_physical_device_to_handle(physical_device
),
51 if (result
!= VK_SUCCESS
) {
52 #ifdef VK_USE_PLATFORM_XCB_KHR
53 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
63 radv_finish_wsi(struct radv_physical_device
*physical_device
)
65 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
66 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
68 #ifdef VK_USE_PLATFORM_XCB_KHR
69 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
73 void radv_DestroySurfaceKHR(
75 VkSurfaceKHR _surface
,
76 const VkAllocationCallbacks
* pAllocator
)
78 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
79 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
81 vk_free2(&instance
->alloc
, pAllocator
, surface
);
84 VkResult
radv_GetPhysicalDeviceSurfaceSupportKHR(
85 VkPhysicalDevice physicalDevice
,
86 uint32_t queueFamilyIndex
,
87 VkSurfaceKHR _surface
,
90 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
91 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
92 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
94 return iface
->get_support(surface
, &device
->wsi_device
,
95 &device
->instance
->alloc
,
96 queueFamilyIndex
, device
->local_fd
, true, pSupported
);
99 VkResult
radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
100 VkPhysicalDevice physicalDevice
,
101 VkSurfaceKHR _surface
,
102 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
104 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
105 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
106 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
108 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
111 VkResult
radv_GetPhysicalDeviceSurfaceFormatsKHR(
112 VkPhysicalDevice physicalDevice
,
113 VkSurfaceKHR _surface
,
114 uint32_t* pSurfaceFormatCount
,
115 VkSurfaceFormatKHR
* pSurfaceFormats
)
117 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
118 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
119 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
121 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
125 VkResult
radv_GetPhysicalDeviceSurfacePresentModesKHR(
126 VkPhysicalDevice physicalDevice
,
127 VkSurfaceKHR _surface
,
128 uint32_t* pPresentModeCount
,
129 VkPresentModeKHR
* pPresentModes
)
131 RADV_FROM_HANDLE(radv_physical_device
, device
, physicalDevice
);
132 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
133 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
135 return iface
->get_present_modes(surface
, pPresentModeCount
,
140 radv_wsi_image_create(VkDevice device_h
,
141 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
142 const VkAllocationCallbacks
* pAllocator
,
143 bool needs_linear_copy
,
146 VkDeviceMemory
*memory_p
,
149 uint32_t *row_pitch
, int *fd_p
)
151 VkResult result
= VK_SUCCESS
;
152 struct radeon_surf
*surface
;
154 struct radv_image
*image
;
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 VkDedicatedAllocationMemoryAllocateInfoNV ded_alloc
= {
188 .sType
= VK_STRUCTURE_TYPE_DEDICATED_ALLOCATION_MEMORY_ALLOCATE_INFO_NV
,
190 .buffer
= VK_NULL_HANDLE
,
194 result
= radv_AllocateMemory(device_h
,
195 &(VkMemoryAllocateInfo
) {
196 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
198 .allocationSize
= image
->size
,
199 .memoryTypeIndex
= linear
? 1 : 0,
201 NULL
/* XXX: pAllocator */,
203 if (result
!= VK_SUCCESS
)
204 goto fail_create_image
;
206 radv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
209 * return the fd for the image in the no copy mode,
210 * or the fd for the linear image if a copy is required.
212 if (!needs_linear_copy
|| (needs_linear_copy
&& linear
)) {
213 RADV_FROM_HANDLE(radv_device
, device
, device_h
);
214 RADV_FROM_HANDLE(radv_device_memory
, memory
, memory_h
);
215 if (!radv_get_memory_fd(device
, memory
, &fd
))
216 goto fail_alloc_memory
;
220 surface
= &image
->surface
;
223 *memory_p
= memory_h
;
225 *offset
= image
->offset
;
226 *row_pitch
= surface
->level
[0].pitch_bytes
;
229 radv_FreeMemory(device_h
, memory_h
, pAllocator
);
232 radv_DestroyImage(device_h
, image_h
, pAllocator
);
238 radv_wsi_image_free(VkDevice device
,
239 const VkAllocationCallbacks
* pAllocator
,
241 VkDeviceMemory memory_h
)
243 radv_DestroyImage(device
, image_h
, pAllocator
);
245 radv_FreeMemory(device
, memory_h
, pAllocator
);
248 static const struct wsi_image_fns radv_wsi_image_fns
= {
249 .create_wsi_image
= radv_wsi_image_create
,
250 .free_wsi_image
= radv_wsi_image_free
,
253 #define NUM_PRIME_POOLS RADV_QUEUE_TRANSFER
255 radv_wsi_free_prime_command_buffers(struct radv_device
*device
,
256 struct wsi_swapchain
*swapchain
)
258 const int num_pools
= NUM_PRIME_POOLS
;
259 const int num_images
= swapchain
->image_count
;
261 for (i
= 0; i
< num_pools
; i
++) {
262 radv_FreeCommandBuffers(radv_device_to_handle(device
),
263 swapchain
->cmd_pools
[i
],
264 swapchain
->image_count
,
265 &swapchain
->cmd_buffers
[i
* num_images
]);
267 radv_DestroyCommandPool(radv_device_to_handle(device
),
268 swapchain
->cmd_pools
[i
],
274 radv_wsi_create_prime_command_buffers(struct radv_device
*device
,
275 const VkAllocationCallbacks
*alloc
,
276 struct wsi_swapchain
*swapchain
)
278 const int num_pools
= NUM_PRIME_POOLS
;
279 const int num_images
= swapchain
->image_count
;
280 int num_cmd_buffers
= num_images
* num_pools
; //TODO bump to MAX_QUEUE_FAMILIES
284 swapchain
->cmd_buffers
= vk_alloc(alloc
, (sizeof(VkCommandBuffer
) * num_cmd_buffers
), 8,
285 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
286 if (!swapchain
->cmd_buffers
)
287 return VK_ERROR_OUT_OF_HOST_MEMORY
;
289 memset(swapchain
->cmd_buffers
, 0, sizeof(VkCommandBuffer
) * num_cmd_buffers
);
290 memset(swapchain
->cmd_pools
, 0, sizeof(VkCommandPool
) * num_pools
);
291 for (i
= 0; i
< num_pools
; i
++) {
292 VkCommandPoolCreateInfo pool_create_info
;
294 pool_create_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
295 pool_create_info
.pNext
= NULL
;
296 pool_create_info
.flags
= 0;
297 pool_create_info
.queueFamilyIndex
= i
;
299 result
= radv_CreateCommandPool(radv_device_to_handle(device
),
300 &pool_create_info
, alloc
,
301 &swapchain
->cmd_pools
[i
]);
302 if (result
!= VK_SUCCESS
)
305 VkCommandBufferAllocateInfo cmd_buffer_info
;
306 cmd_buffer_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
307 cmd_buffer_info
.pNext
= NULL
;
308 cmd_buffer_info
.commandPool
= swapchain
->cmd_pools
[i
];
309 cmd_buffer_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
310 cmd_buffer_info
.commandBufferCount
= num_images
;
312 result
= radv_AllocateCommandBuffers(radv_device_to_handle(device
),
314 &swapchain
->cmd_buffers
[i
* num_images
]);
315 if (result
!= VK_SUCCESS
)
317 for (j
= 0; j
< num_images
; j
++) {
318 VkImage image
, linear_image
;
319 int idx
= (i
* num_images
) + j
;
321 swapchain
->get_image_and_linear(swapchain
, j
, &image
, &linear_image
);
322 VkCommandBufferBeginInfo begin_info
= {0};
324 begin_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
;
326 radv_BeginCommandBuffer(swapchain
->cmd_buffers
[idx
], &begin_info
);
328 radv_blit_to_prime_linear(radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
]),
329 radv_image_from_handle(image
),
330 radv_image_from_handle(linear_image
));
332 radv_EndCommandBuffer(swapchain
->cmd_buffers
[idx
]);
337 radv_wsi_free_prime_command_buffers(device
, swapchain
);
341 VkResult
radv_CreateSwapchainKHR(
343 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
344 const VkAllocationCallbacks
* pAllocator
,
345 VkSwapchainKHR
* pSwapchain
)
347 RADV_FROM_HANDLE(radv_device
, device
, _device
);
348 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
349 struct wsi_interface
*iface
=
350 device
->physical_device
->wsi_device
.wsi
[surface
->platform
];
351 struct wsi_swapchain
*swapchain
;
352 const VkAllocationCallbacks
*alloc
;
356 alloc
= &device
->alloc
;
357 VkResult result
= iface
->create_swapchain(surface
, _device
,
358 &device
->physical_device
->wsi_device
,
359 device
->physical_device
->local_fd
,
361 alloc
, &radv_wsi_image_fns
,
363 if (result
!= VK_SUCCESS
)
367 swapchain
->alloc
= *pAllocator
;
369 swapchain
->alloc
= device
->alloc
;
371 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
372 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
374 if (swapchain
->needs_linear_copy
) {
375 result
= radv_wsi_create_prime_command_buffers(device
, alloc
,
377 if (result
!= VK_SUCCESS
)
381 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
386 void radv_DestroySwapchainKHR(
388 VkSwapchainKHR _swapchain
,
389 const VkAllocationCallbacks
* pAllocator
)
391 RADV_FROM_HANDLE(radv_device
, device
, _device
);
392 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
393 const VkAllocationCallbacks
*alloc
;
401 alloc
= &device
->alloc
;
403 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
404 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
405 radv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
408 if (swapchain
->needs_linear_copy
)
409 radv_wsi_free_prime_command_buffers(device
, swapchain
);
411 swapchain
->destroy(swapchain
, alloc
);
414 VkResult
radv_GetSwapchainImagesKHR(
416 VkSwapchainKHR _swapchain
,
417 uint32_t* pSwapchainImageCount
,
418 VkImage
* pSwapchainImages
)
420 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
422 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
426 VkResult
radv_AcquireNextImageKHR(
428 VkSwapchainKHR _swapchain
,
430 VkSemaphore semaphore
,
432 uint32_t* pImageIndex
)
434 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
435 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
437 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
440 if (fence
&& result
== VK_SUCCESS
) {
441 fence
->submitted
= true;
442 fence
->signalled
= true;
448 VkResult
radv_QueuePresentKHR(
450 const VkPresentInfoKHR
* pPresentInfo
)
452 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
453 VkResult result
= VK_SUCCESS
;
455 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
456 RADV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
457 struct radeon_winsys_cs
*cs
;
458 assert(radv_device_from_handle(swapchain
->device
) == queue
->device
);
459 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
460 result
= radv_CreateFence(radv_device_to_handle(queue
->device
),
461 &(VkFenceCreateInfo
) {
462 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
464 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
465 if (result
!= VK_SUCCESS
)
468 radv_ResetFences(radv_device_to_handle(queue
->device
),
469 1, &swapchain
->fences
[0]);
472 if (swapchain
->needs_linear_copy
) {
473 int idx
= (queue
->queue_family_index
* swapchain
->image_count
) + pPresentInfo
->pImageIndices
[i
];
474 cs
= radv_cmd_buffer_from_handle(swapchain
->cmd_buffers
[idx
])->cs
;
476 cs
= queue
->device
->empty_cs
[queue
->queue_family_index
];
477 RADV_FROM_HANDLE(radv_fence
, fence
, swapchain
->fences
[0]);
478 struct radeon_winsys_fence
*base_fence
= fence
->fence
;
479 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
480 queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
483 (struct radeon_winsys_sem
**)pPresentInfo
->pWaitSemaphores
,
484 pPresentInfo
->waitSemaphoreCount
, NULL
, 0, false, base_fence
);
485 fence
->submitted
= true;
487 result
= swapchain
->queue_present(swapchain
,
488 pPresentInfo
->pImageIndices
[i
],
490 /* TODO: What if one of them returns OUT_OF_DATE? */
491 if (result
!= VK_SUCCESS
)
494 VkFence last
= swapchain
->fences
[2];
495 swapchain
->fences
[2] = swapchain
->fences
[1];
496 swapchain
->fences
[1] = swapchain
->fences
[0];
497 swapchain
->fences
[0] = last
;
499 if (last
!= VK_NULL_HANDLE
) {
500 radv_WaitForFences(radv_device_to_handle(queue
->device
),