diff options
author | Jason Ekstrand <[email protected]> | 2017-11-15 22:30:20 -0800 |
---|---|---|
committer | Jason Ekstrand <[email protected]> | 2017-12-04 10:04:19 -0800 |
commit | e12688f365258f6f719983528a2af9b6ca4e25c5 (patch) | |
tree | 2db39e32fdfb6cbbe4f1aa27cb94ab7cfd71bc14 /src/vulkan/wsi | |
parent | d50937f137201e1d90ce18bbea1383af7a7ebd5d (diff) |
vulkan/wsi: Do image creation in common code
This uses the mock extension created in a previous commit to tell the
driver that the image it's just been asked to create is, in fact, a
window system image with whatever assumptions that implies. There was a
lot of redundant code between the two drivers to do basically exactly
the same thing.
Reviewed-by: Dave Airlie <[email protected]>
Reviewed-by: Chad Versace <[email protected]>
Diffstat (limited to 'src/vulkan/wsi')
-rw-r--r-- | src/vulkan/wsi/wsi_common.c | 117 | ||||
-rw-r--r-- | src/vulkan/wsi/wsi_common.h | 28 | ||||
-rw-r--r-- | src/vulkan/wsi/wsi_common_private.h | 25 | ||||
-rw-r--r-- | src/vulkan/wsi/wsi_common_wayland.c | 13 | ||||
-rw-r--r-- | src/vulkan/wsi/wsi_common_x11.c | 20 |
5 files changed, 144 insertions, 59 deletions
diff --git a/src/vulkan/wsi/wsi_common.c b/src/vulkan/wsi/wsi_common.c index 7cb5d894e77..b86bb90cec6 100644 --- a/src/vulkan/wsi/wsi_common.c +++ b/src/vulkan/wsi/wsi_common.c @@ -59,6 +59,7 @@ wsi_device_init(struct wsi_device *wsi, WSI_GET_CB(FreeCommandBuffers); WSI_GET_CB(GetBufferMemoryRequirements); WSI_GET_CB(GetImageMemoryRequirements); + WSI_GET_CB(GetImageSubresourceLayout); WSI_GET_CB(GetMemoryFdKHR); WSI_GET_CB(QueueSubmit); #undef WSI_GET_CB @@ -147,6 +148,116 @@ align_u32(uint32_t v, uint32_t a) return (v + a - 1) & ~(a - 1); } +VkResult +wsi_create_native_image(const struct wsi_swapchain *chain, + const VkSwapchainCreateInfoKHR *pCreateInfo, + struct wsi_image *image) +{ + const struct wsi_device *wsi = chain->wsi; + VkResult result; + + memset(image, 0, sizeof(*image)); + + const struct wsi_image_create_info image_wsi_info = { + .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA, + .pNext = NULL, + .scanout = true, + }; + const VkImageCreateInfo image_info = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .pNext = &image_wsi_info, + .flags = 0, + .imageType = VK_IMAGE_TYPE_2D, + .format = pCreateInfo->imageFormat, + .extent = { + .width = pCreateInfo->imageExtent.width, + .height = pCreateInfo->imageExtent.height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = pCreateInfo->imageUsage, + .sharingMode = pCreateInfo->imageSharingMode, + .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount, + .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, + }; + result = wsi->CreateImage(chain->device, &image_info, + &chain->alloc, &image->image); + if (result != VK_SUCCESS) + goto fail; + + VkMemoryRequirements reqs; + wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs); + + VkSubresourceLayout image_layout; + const VkImageSubresource image_subresource = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .mipLevel = 0, + .arrayLayer = 0, + }; + wsi->GetImageSubresourceLayout(chain->device, image->image, + &image_subresource, &image_layout); + + const struct wsi_memory_allocate_info memory_wsi_info = { + .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA, + .pNext = NULL, + .implicit_sync = true, + }; + const VkExportMemoryAllocateInfoKHR memory_export_info = { + .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR, + .pNext = &memory_wsi_info, + .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, + }; + const VkMemoryDedicatedAllocateInfoKHR memory_dedicated_info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR, + .pNext = &memory_export_info, + .image = image->image, + .buffer = VK_NULL_HANDLE, + }; + const VkMemoryAllocateInfo memory_info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .pNext = &memory_dedicated_info, + .allocationSize = reqs.size, + .memoryTypeIndex = select_memory_type(wsi, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + reqs.memoryTypeBits), + }; + result = wsi->AllocateMemory(chain->device, &memory_info, + &chain->alloc, &image->memory); + if (result != VK_SUCCESS) + goto fail; + + result = wsi->BindImageMemory(chain->device, image->image, + image->memory, 0); + if (result != VK_SUCCESS) + goto fail; + + const VkMemoryGetFdInfoKHR memory_get_fd_info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, + .pNext = NULL, + .memory = image->memory, + .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, + }; + int fd; + result = wsi->GetMemoryFdKHR(chain->device, &memory_get_fd_info, &fd); + if (result != VK_SUCCESS) + goto fail; + + image->size = reqs.size; + image->row_pitch = image_layout.rowPitch; + image->offset = 0; + image->fd = fd; + + return VK_SUCCESS; + +fail: + wsi_destroy_image(chain, image); + + return result; +} + #define WSI_PRIME_LINEAR_STRIDE_ALIGN 256 VkResult @@ -342,14 +453,14 @@ wsi_create_prime_image(const struct wsi_swapchain *chain, return VK_SUCCESS; fail: - wsi_destroy_prime_image(chain, image); + wsi_destroy_image(chain, image); return result; } void -wsi_destroy_prime_image(const struct wsi_swapchain *chain, - struct wsi_image *image) +wsi_destroy_image(const struct wsi_swapchain *chain, + struct wsi_image *image) { const struct wsi_device *wsi = chain->wsi; diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h index d77ae446b08..a5e7e655fc8 100644 --- a/src/vulkan/wsi/wsi_common.h +++ b/src/vulkan/wsi/wsi_common.h @@ -48,39 +48,13 @@ struct wsi_memory_allocate_info { bool implicit_sync; }; -struct wsi_image { - VkImage image; - VkDeviceMemory memory; - - struct { - VkBuffer buffer; - VkDeviceMemory memory; - VkCommandBuffer *blit_cmd_buffers; - } prime; - - uint32_t size; - uint32_t offset; - uint32_t row_pitch; - int fd; -}; - struct wsi_device; -struct wsi_image_fns { - VkResult (*create_wsi_image)(VkDevice device_h, - const VkSwapchainCreateInfoKHR *pCreateInfo, - const VkAllocationCallbacks *pAllocator, - struct wsi_image *image_p); - void (*free_wsi_image)(VkDevice device, - const VkAllocationCallbacks *pAllocator, - struct wsi_image *image); -}; struct wsi_swapchain { const struct wsi_device *wsi; VkDevice device; VkAllocationCallbacks alloc; - const struct wsi_image_fns *image_fns; VkFence fences[3]; VkPresentModeKHR present_mode; uint32_t image_count; @@ -134,7 +108,6 @@ struct wsi_interface { int local_fd, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, - const struct wsi_image_fns *image_fns, struct wsi_swapchain **swapchain); }; @@ -164,6 +137,7 @@ struct wsi_device { WSI_CB(FreeCommandBuffers); WSI_CB(GetBufferMemoryRequirements); WSI_CB(GetImageMemoryRequirements); + WSI_CB(GetImageSubresourceLayout); WSI_CB(GetMemoryFdKHR); WSI_CB(QueueSubmit); #undef WSI_CB diff --git a/src/vulkan/wsi/wsi_common_private.h b/src/vulkan/wsi/wsi_common_private.h index 3cef6dd4394..ff8ca2a4b20 100644 --- a/src/vulkan/wsi/wsi_common_private.h +++ b/src/vulkan/wsi/wsi_common_private.h @@ -25,6 +25,22 @@ #include "wsi_common.h" +struct wsi_image { + VkImage image; + VkDeviceMemory memory; + + struct { + VkBuffer buffer; + VkDeviceMemory memory; + VkCommandBuffer *blit_cmd_buffers; + } prime; + + uint32_t size; + uint32_t offset; + uint32_t row_pitch; + int fd; +}; + VkResult wsi_swapchain_init(const struct wsi_device *wsi, struct wsi_swapchain *chain, @@ -35,13 +51,18 @@ wsi_swapchain_init(const struct wsi_device *wsi, void wsi_swapchain_finish(struct wsi_swapchain *chain); VkResult +wsi_create_native_image(const struct wsi_swapchain *chain, + const VkSwapchainCreateInfoKHR *pCreateInfo, + struct wsi_image *image); + +VkResult wsi_create_prime_image(const struct wsi_swapchain *chain, const VkSwapchainCreateInfoKHR *pCreateInfo, struct wsi_image *image); void -wsi_destroy_prime_image(const struct wsi_swapchain *chain, - struct wsi_image *image); +wsi_destroy_image(const struct wsi_swapchain *chain, + struct wsi_image *image); VkResult wsi_prime_image_blit_to_linear(const struct wsi_swapchain *chain, diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c index c24afcd184a..22c01b2963a 100644 --- a/src/vulkan/wsi/wsi_common_wayland.c +++ b/src/vulkan/wsi/wsi_common_wayland.c @@ -728,13 +728,9 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain, const VkSwapchainCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks* pAllocator) { - VkDevice vk_device = chain->base.device; VkResult result; - result = chain->base.image_fns->create_wsi_image(vk_device, - pCreateInfo, - pAllocator, - &image->base); + result = wsi_create_native_image(&chain->base, pCreateInfo, &image->base); if (result != VK_SUCCESS) return result; @@ -756,7 +752,7 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain, return VK_SUCCESS; fail_image: - chain->base.image_fns->free_wsi_image(vk_device, pAllocator, &image->base); + wsi_destroy_image(&chain->base, &image->base); return result; } @@ -770,8 +766,7 @@ wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain, for (uint32_t i = 0; i < chain->base.image_count; i++) { if (chain->images[i].buffer) { wl_buffer_destroy(chain->images[i].buffer); - chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator, - &chain->images[i].base); + wsi_destroy_image(&chain->base, &chain->images[i].base); } } @@ -799,7 +794,6 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface, int local_fd, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, - const struct wsi_image_fns *image_fns, struct wsi_swapchain **swapchain_out) { VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface; @@ -841,7 +835,6 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface, chain->base.get_images = wsi_wl_swapchain_get_images; chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image; chain->base.queue_present = wsi_wl_swapchain_queue_present; - chain->base.image_fns = image_fns; chain->base.present_mode = pCreateInfo->presentMode; chain->base.image_count = num_images; chain->extent = pCreateInfo->imageExtent; diff --git a/src/vulkan/wsi/wsi_common_x11.c b/src/vulkan/wsi/wsi_common_x11.c index fd60cfa95b0..68cb0355612 100644 --- a/src/vulkan/wsi/wsi_common_x11.c +++ b/src/vulkan/wsi/wsi_common_x11.c @@ -969,10 +969,7 @@ x11_image_init(VkDevice device_h, struct x11_swapchain *chain, if (chain->use_prime_blit) { result = wsi_create_prime_image(&chain->base, pCreateInfo, &image->base); } else { - result = chain->base.image_fns->create_wsi_image(device_h, - pCreateInfo, - pAllocator, - &image->base); + result = wsi_create_native_image(&chain->base, pCreateInfo, &image->base); } if (result != VK_SUCCESS) return result; @@ -1019,11 +1016,7 @@ fail_pixmap: cookie = xcb_free_pixmap(chain->conn, image->pixmap); xcb_discard_reply(chain->conn, cookie.sequence); - if (chain->use_prime_blit) { - wsi_destroy_prime_image(&chain->base, &image->base); - } else { - chain->base.image_fns->free_wsi_image(device_h, pAllocator, &image->base); - } + wsi_destroy_image(&chain->base, &image->base); return result; } @@ -1042,12 +1035,7 @@ x11_image_finish(struct x11_swapchain *chain, cookie = xcb_free_pixmap(chain->conn, image->pixmap); xcb_discard_reply(chain->conn, cookie.sequence); - if (chain->use_prime_blit) { - wsi_destroy_prime_image(&chain->base, &image->base); - } else { - chain->base.image_fns->free_wsi_image(chain->base.device, - pAllocator, &image->base); - } + wsi_destroy_image(&chain->base, &image->base); } static VkResult @@ -1089,7 +1077,6 @@ x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface, int local_fd, const VkSwapchainCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks* pAllocator, - const struct wsi_image_fns *image_fns, struct wsi_swapchain **swapchain_out) { struct x11_swapchain *chain; @@ -1125,7 +1112,6 @@ x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface, chain->base.get_images = x11_get_images; chain->base.acquire_next_image = x11_acquire_next_image; chain->base.queue_present = x11_queue_present; - chain->base.image_fns = image_fns; chain->base.present_mode = pCreateInfo->presentMode; chain->base.image_count = num_images; chain->conn = conn; |