From 566a6ee68d46a3a2568ff45123a7add90fa8a192 Mon Sep 17 00:00:00 2001 From: Normunds Rieksts Date: Thu, 24 Nov 2022 11:24:34 +0000 Subject: [PATCH] Extend TRY macro to print out error messages Extends the TRY macros exposed by the helper header to be able to print out error messages. Change-Id: I61c607376304ba744a95dd6782bb29653235096a Signed-off-by: Normunds Rieksts --- layer/layer.cpp | 36 +++++++-------- layer/swapchain_api.cpp | 46 ++++--------------- util/helpers.hpp | 25 ++++++---- wsi/external_memory.cpp | 28 ++++------- wsi/swapchain_base.cpp | 64 ++++++-------------------- wsi/wayland/surface_properties.cpp | 15 +++--- wsi/wayland/swapchain.cpp | 74 ++++++++++-------------------- wsi/wsi_factory.cpp | 32 +++++++------ 8 files changed, 117 insertions(+), 203 deletions(-) diff --git a/layer/layer.cpp b/layer/layer.cpp index 14e1dac..cc5634b 100644 --- a/layer/layer.cpp +++ b/layer/layer.cpp @@ -116,16 +116,16 @@ VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, con * This object and the extension_list object need to be in the global scope so they can be alive by the time * vkCreateInstance is called. */ - util::allocator allocator{VK_SYSTEM_ALLOCATION_SCOPE_COMMAND, pAllocator}; - util::vector modified_enabled_extensions{allocator}; - util::extension_list extensions{allocator}; + util::allocator allocator{ VK_SYSTEM_ALLOCATION_SCOPE_COMMAND, pAllocator }; + util::vector modified_enabled_extensions{ allocator }; + util::extension_list extensions{ allocator }; /* Find all the platforms that the layer can handle based on pCreateInfo->ppEnabledExtensionNames. */ auto layer_platforms_to_enable = wsi::find_enabled_layer_platforms(pCreateInfo); if (!layer_platforms_to_enable.empty()) { /* Create a list of extensions to enable, including the provided extensions and those required by the layer. */ - TRY(extensions.add(pCreateInfo->ppEnabledExtensionNames, pCreateInfo->enabledExtensionCount)); + TRY_LOG_CALL(extensions.add(pCreateInfo->ppEnabledExtensionNames, pCreateInfo->enabledExtensionCount)); if (!extensions.contains(VK_KHR_SURFACE_EXTENSION_NAME)) { @@ -135,15 +135,15 @@ VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, con /* The extensions listed below are those strictly required by the layer. Other extensions may be used by the * layer (such as calling their entrypoints), when they are enabled by the application. */ - std::array extra_extensions = { + std::array extra_extensions = { VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, VK_KHR_EXTERNAL_FENCE_CAPABILITIES_EXTENSION_NAME, VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME, /* The extension below is only needed for Wayland. For now, enable it also for headless. */ VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, }; - TRY(extensions.add(extra_extensions.data(), extra_extensions.size())); - TRY(extensions.get_extension_strings(modified_enabled_extensions)); + TRY_LOG_CALL(extensions.add(extra_extensions.data(), extra_extensions.size())); + TRY_LOG_CALL(extensions.get_extension_strings(modified_enabled_extensions)); modified_info.ppEnabledExtensionNames = modified_enabled_extensions.data(); modified_info.enabledExtensionCount = modified_enabled_extensions.size(); @@ -157,7 +157,7 @@ VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, con * Layers have to abide the rule that vkCreateInstance must not generate an error for unrecognized extension names. * Also, the loader filters the extension list to ensure that ICDs do not see extensions that they do not support. */ - TRY(fpCreateInstance(&modified_info, pAllocator, pInstance)); + TRY_LOG(fpCreateInstance(&modified_info, pAllocator, pInstance), "Failed to create the instance"); instance_dispatch_table table{}; VkResult result; @@ -241,23 +241,23 @@ VKAPI_ATTR VkResult create_device(VkPhysicalDevice physicalDevice, const VkDevic VkDeviceCreateInfo modified_info = *pCreateInfo; auto &inst_data = instance_private_data::get(physicalDevice); - util::allocator allocator{inst_data.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND, pAllocator}; - util::vector modified_enabled_extensions{allocator}; - util::extension_list enabled_extensions{allocator}; + util::allocator allocator{ inst_data.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND, pAllocator }; + util::vector modified_enabled_extensions{ allocator }; + util::extension_list enabled_extensions{ allocator }; - const util::wsi_platform_set& enabled_platforms = inst_data.get_enabled_platforms(); + const util::wsi_platform_set &enabled_platforms = inst_data.get_enabled_platforms(); if (!enabled_platforms.empty()) { - TRY(enabled_extensions.add(pCreateInfo->ppEnabledExtensionNames, pCreateInfo->enabledExtensionCount)); - TRY(wsi::add_extensions_required_by_layer(physicalDevice, enabled_platforms, enabled_extensions)); - TRY(enabled_extensions.get_extension_strings(modified_enabled_extensions)); + TRY_LOG_CALL(enabled_extensions.add(pCreateInfo->ppEnabledExtensionNames, pCreateInfo->enabledExtensionCount)); + TRY_LOG_CALL(wsi::add_extensions_required_by_layer(physicalDevice, enabled_platforms, enabled_extensions)); + TRY_LOG_CALL(enabled_extensions.get_extension_strings(modified_enabled_extensions)); modified_info.ppEnabledExtensionNames = modified_enabled_extensions.data(); modified_info.enabledExtensionCount = modified_enabled_extensions.size(); } /* Now call create device on the chain further down the list. */ - TRY(fpCreateDevice(physicalDevice, &modified_info, pAllocator, pDevice)); + TRY_LOG(fpCreateDevice(physicalDevice, &modified_info, pAllocator, pDevice), "Failed to create the device"); device_dispatch_table table{}; VkResult result = table.populate(*pDevice, fpGetDeviceProcAddr); @@ -273,7 +273,7 @@ VKAPI_ATTR VkResult create_device(VkPhysicalDevice physicalDevice, const VkDevic /* Following the spec: use the callbacks provided to vkCreateDevice() if not nullptr, otherwise use the callbacks * provided to the instance (if no allocator callbacks was provided to the instance, it will use default ones). */ - util::allocator device_allocator{inst_data.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE, pAllocator}; + util::allocator device_allocator{ inst_data.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE, pAllocator }; result = device_private_data::associate(*pDevice, inst_data, physicalDevice, table, loader_callback, device_allocator); if (result != VK_SUCCESS) @@ -303,7 +303,7 @@ VKAPI_ATTR VkResult create_device(VkPhysicalDevice physicalDevice, const VkDevic #if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN const auto *swapchain_compression_feature = - util::find_extension( + util::find_extension( VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT, pCreateInfo->pNext); if (swapchain_compression_feature != nullptr) { diff --git a/layer/swapchain_api.cpp b/layer/swapchain_api.cpp index 47cd6f1..e96ba71 100644 --- a/layer/swapchain_api.cpp +++ b/layer/swapchain_api.cpp @@ -61,20 +61,13 @@ wsi_layer_vkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR * return VK_ERROR_OUT_OF_HOST_MEMORY; } - VkResult result = sc->init(device, pSwapchainCreateInfo); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG(sc->init(device, pSwapchainCreateInfo), "Failed to initialise swapchain"); - result = device_data.add_layer_swapchain(reinterpret_cast(sc.get())); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG(device_data.add_layer_swapchain(reinterpret_cast(sc.get())), + "Failed to associate swapchain with the layer"); *pSwapchain = reinterpret_cast(sc.release()); - return result; + return VK_SUCCESS; } VWL_VKAPI_CALL(void) @@ -170,12 +163,7 @@ static VkResult submit_wait_request(VkQueue queue, const VkPresentInfoKHR &prese swapchain_semaphores.data(), }; - VkResult result = device_data.disp.QueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE); - if (result != VK_SUCCESS) - { - return result; - } - + TRY(device_data.disp.QueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE)); return VK_SUCCESS; } @@ -193,16 +181,10 @@ wsi_layer_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) } /* Avoid allocating on the heap when there is only one swapchain. */ - VkResult res = VK_SUCCESS; const VkPresentInfoKHR *present_info = pPresentInfo; if (pPresentInfo->swapchainCount > 1) { - res = submit_wait_request(queue, *pPresentInfo, device_data); - if (res != VK_SUCCESS) - { - return res; - } - + TRY_LOG_CALL(submit_wait_request(queue, *pPresentInfo, device_data)); present_info = nullptr; } @@ -214,8 +196,7 @@ wsi_layer_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) auto *sc = reinterpret_cast(swapc); assert(sc != nullptr); - res = sc->queue_present(queue, present_info, pPresentInfo->pImageIndices[i]); - + VkResult res = sc->queue_present(queue, present_info, pPresentInfo->pImageIndices[i]); if (pPresentInfo->pResults != nullptr) { pPresentInfo->pResults[i] = res; @@ -365,20 +346,13 @@ wsi_layer_vkBindImageMemory2(VkDevice device, uint32_t bindInfoCount, if (bind_sc_info == nullptr || bind_sc_info->swapchain == VK_NULL_HANDLE || !device_data.layer_owns_swapchain(bind_sc_info->swapchain)) { - VkResult result = device_data.disp.BindImageMemory2KHR(device, 1, &pBindInfos[i]); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(device_data.disp.BindImageMemory2KHR(device, 1, &pBindInfos[i])); } else { auto sc = reinterpret_cast(bind_sc_info->swapchain); - VkResult result = sc->bind_swapchain_image(device, &pBindInfos[i], bind_sc_info); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG(sc->bind_swapchain_image(device, &pBindInfos[i], bind_sc_info), + "Failed to bind an image to the swapchain"); } } return VK_SUCCESS; diff --git a/util/helpers.hpp b/util/helpers.hpp index a72591d..25add10 100644 --- a/util/helpers.hpp +++ b/util/helpers.hpp @@ -31,6 +31,7 @@ #pragma once #include +#include "log.hpp" /* * Conditional return statement. This allows functions to return early for @@ -56,16 +57,24 @@ * return VK_SUCCESS; * } */ -#define TRY(expression) \ - do \ - { \ - VkResult try_result = expression; \ - if (try_result != VK_SUCCESS) \ - { \ - return try_result; \ - } \ +#define TRY_HANDLER(expression, do_log, ...) \ + do \ + { \ + VkResult try_result = expression; \ + if (try_result != VK_SUCCESS) \ + { \ + if (do_log) \ + { \ + WSI_LOG_ERROR(__VA_ARGS__); \ + } \ + return try_result; \ + } \ } while (0) +#define TRY(expression) TRY_HANDLER(expression, false, "PLACEHOLDER") +#define TRY_LOG_CALL(expression) TRY_HANDLER(expression, true, #expression) +#define TRY_LOG(expression, ...) TRY_HANDLER(expression, true, __VA_ARGS__) + namespace util { template diff --git a/wsi/external_memory.cpp b/wsi/external_memory.cpp index 18fe4dd..ad1a606 100644 --- a/wsi/external_memory.cpp +++ b/wsi/external_memory.cpp @@ -100,13 +100,8 @@ VkResult external_memory::get_fd_mem_type_index(uint32_t index, uint32_t *mem_id VkMemoryFdPropertiesKHR mem_props = {}; mem_props.sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR; - VkResult result = - device_data.disp.GetMemoryFdPropertiesKHR(m_device, m_handle_type, m_buffer_fds[index], &mem_props); - if (result != VK_SUCCESS) - { - WSI_LOG_ERROR("Error querying Fd properties."); - return result; - } + TRY_LOG(device_data.disp.GetMemoryFdPropertiesKHR(m_device, m_handle_type, m_buffer_fds[index], &mem_props), + "Error querying file descriptor properties"); for (*mem_idx = 0; *mem_idx < VK_MAX_MEMORY_TYPES; (*mem_idx)++) { @@ -130,7 +125,7 @@ VkResult external_memory::import_plane_memories() auto it = std::find(std::begin(m_buffer_fds), std::end(m_buffer_fds), m_buffer_fds[plane]); if (std::distance(std::begin(m_buffer_fds), it) == plane) { - TRY(import_plane_memory(plane)); + TRY_LOG_CALL(import_plane_memory(plane)); } } return VK_SUCCESS; @@ -141,7 +136,7 @@ VkResult external_memory::import_plane_memories() VkResult external_memory::import_plane_memory(uint32_t index) { uint32_t mem_index = 0; - TRY(get_fd_mem_type_index(index, &mem_index)); + TRY_LOG_CALL(get_fd_mem_type_index(index, &mem_index)); const off_t fd_size = lseek(m_buffer_fds[index], 0, SEEK_END); if (fd_size < 0) @@ -162,14 +157,9 @@ VkResult external_memory::import_plane_memory(uint32_t index) alloc_info.memoryTypeIndex = mem_index; auto &device_data = layer::device_private_data::get(m_device); - VkResult result = - device_data.disp.AllocateMemory(m_device, &alloc_info, m_allocator.get_original_callbacks(), &m_memories[index]); - - if (result != VK_SUCCESS) - { - WSI_LOG_ERROR("Failed to import memory."); - return result; - } + TRY_LOG( + device_data.disp.AllocateMemory(m_device, &alloc_info, m_allocator.get_original_callbacks(), &m_memories[index]), + "Failed to import device memory"); return VK_SUCCESS; } @@ -213,8 +203,8 @@ VkResult external_memory::bind_swapchain_image_memory(const VkImage &image) VkResult external_memory::import_memory_and_bind_swapchain_image(const VkImage &image) { - TRY(import_plane_memories()); - TRY(bind_swapchain_image_memory(image)); + TRY_LOG_CALL(import_plane_memories()); + TRY_LOG_CALL(bind_swapchain_image_memory(image)); return VK_SUCCESS; } diff --git a/wsi/swapchain_base.cpp b/wsi/swapchain_base.cpp index 89f66e2..65e7de8 100644 --- a/wsi/swapchain_base.cpp +++ b/wsi/swapchain_base.cpp @@ -138,12 +138,7 @@ bool swapchain_base::has_descendant_started_presenting() VkResult swapchain_base::init_page_flip_thread() { /* Setup semaphore for signaling pageflip thread */ - VkResult result = m_page_flip_semaphore.init(0); - if (result != VK_SUCCESS) - { - return result; - } - + TRY_LOG_CALL(m_page_flip_semaphore.init(0)); m_thread_sem_defined = true; /* Launch page flipping thread */ @@ -232,19 +227,11 @@ VkResult swapchain_base::init(VkDevice device, const VkSwapchainCreateInfoKHR *s /* We have allocated images, we can call the platform init function if something needs to be done. */ bool use_presentation_thread = true; - VkResult result = init_platform(device, swapchain_create_info, use_presentation_thread); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(init_platform(device, swapchain_create_info, use_presentation_thread)); if (use_presentation_thread) { - result = init_page_flip_thread(); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(init_page_flip_thread()); } VkImageCreateInfo image_create_info = {}; @@ -265,7 +252,7 @@ VkResult swapchain_base::init(VkDevice device, const VkSwapchainCreateInfoKHR *s image_create_info.pQueueFamilyIndices = swapchain_create_info->pQueueFamilyIndices; image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - result = m_free_image_semaphore.init(m_swapchain_images.size()); + VkResult result = m_free_image_semaphore.init(m_swapchain_images.size()); if (result != VK_SUCCESS) { assert(result == VK_ERROR_OUT_OF_HOST_MEMORY); @@ -274,28 +261,17 @@ VkResult swapchain_base::init(VkDevice device, const VkSwapchainCreateInfoKHR *s for (auto &img : m_swapchain_images) { - result = create_and_bind_swapchain_image(image_create_info, img); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(create_and_bind_swapchain_image(image_create_info, img)); VkSemaphoreCreateInfo semaphore_info = {}; semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; - result = m_device_data.disp.CreateSemaphore(m_device, &semaphore_info, get_allocation_callbacks(), - &img.present_semaphore); - if (result != VK_SUCCESS) - { - return result; - } + + TRY_LOG_CALL(m_device_data.disp.CreateSemaphore(m_device, &semaphore_info, get_allocation_callbacks(), + &img.present_semaphore)); } m_device_data.disp.GetDeviceQueue(m_device, 0, 0, &m_queue); - result = m_device_data.SetDeviceLoaderData(m_device, m_queue); - if (VK_SUCCESS != result) - { - return result; - } + TRY_LOG_CALL(m_device_data.SetDeviceLoaderData(m_device, m_queue)); int res = sem_init(&m_start_present_semaphore, 0, 0); /* Only programming error can cause this to fail. */ @@ -401,12 +377,7 @@ VkResult swapchain_base::acquire_next_image(uint64_t timeout, VkSemaphore semaph { std::unique_lock acquire_lock(m_image_acquire_lock); - VkResult retval = wait_for_free_buffer(timeout); - if (retval != VK_SUCCESS) - { - return retval; - } - + TRY(wait_for_free_buffer(timeout)); if (error_has_occured()) { return get_error_state(); @@ -484,6 +455,7 @@ VkResult swapchain_base::acquire_next_image(uint64_t timeout, VkSemaphore semaph } /* Fallback for when importing fence/semaphore sync FDs is unsupported by the ICD. */ + VkResult retval = VK_SUCCESS; if (VK_NULL_HANDLE != semaphore || VK_NULL_HANDLE != fence) { VkSubmitInfo submit = { VK_STRUCTURE_TYPE_SUBMIT_INFO }; @@ -496,6 +468,7 @@ VkResult swapchain_base::acquire_next_image(uint64_t timeout, VkSemaphore semaph submit.commandBufferCount = 0; submit.pCommandBuffers = nullptr; + retval = m_device_data.disp.QueueSubmit(m_queue, 1, &submit, fence); assert(retval == VK_SUCCESS); } @@ -592,17 +565,8 @@ VkResult swapchain_base::queue_present(VkQueue queue, const VkPresentInfoKHR *pr sem_count = present_info->waitSemaphoreCount; } - VkResult result = image_set_present_payload(m_swapchain_images[image_index], queue, wait_semaphores, sem_count); - if (result != VK_SUCCESS) - { - return result; - } - - result = notify_presentation_engine(image_index); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(image_set_present_payload(m_swapchain_images[image_index], queue, wait_semaphores, sem_count)); + TRY(notify_presentation_engine(image_index)); return VK_SUCCESS; } diff --git a/wsi/wayland/surface_properties.cpp b/wsi/wayland/surface_properties.cpp index a729484..defc638 100644 --- a/wsi/wayland/surface_properties.cpp +++ b/wsi/wayland/surface_properties.cpp @@ -148,12 +148,12 @@ static VkResult surface_format_properties_map_init(VkPhysicalDevice phys_dev, su const VkFormat vk_format = util::drm::drm_to_vk_format(drm_format.fourcc); if (vk_format != VK_FORMAT_UNDEFINED && format_map.find(vk_format) == format_map.end()) { - TRY(surface_format_properties_map_add(phys_dev, format_map, vk_format, drm_format)); + TRY_LOG_CALL(surface_format_properties_map_add(phys_dev, format_map, vk_format, drm_format)); } const VkFormat srgb_vk_format = util::drm::drm_to_vk_srgb_format(drm_format.fourcc); if (srgb_vk_format != VK_FORMAT_UNDEFINED && format_map.find(srgb_vk_format) == format_map.end()) { - TRY(surface_format_properties_map_add(phys_dev, format_map, srgb_vk_format, drm_format)); + TRY_LOG_CALL(surface_format_properties_map_add(phys_dev, format_map, srgb_vk_format, drm_format)); } } @@ -173,7 +173,7 @@ static VkResult surface_format_properties_map_add_compression(VkPhysicalDevice p auto entry = format_map.find(vk_format); if (entry != format_map.end()) { - TRY(surface_format_properties_add_modifier_support(phys_dev, entry->second, drm_format, true)); + TRY_LOG_CALL(surface_format_properties_add_modifier_support(phys_dev, entry->second, drm_format, true)); } } const VkFormat srgb_vk_format = util::drm::drm_to_vk_srgb_format(drm_format.fourcc); @@ -182,7 +182,7 @@ static VkResult surface_format_properties_map_add_compression(VkPhysicalDevice p auto entry = format_map.find(srgb_vk_format); if (entry != format_map.end()) { - TRY(surface_format_properties_add_modifier_support(phys_dev, entry->second, drm_format, true)); + TRY_LOG_CALL(surface_format_properties_add_modifier_support(phys_dev, entry->second, drm_format, true)); } } } @@ -197,12 +197,13 @@ VkResult surface_properties::get_surface_formats(VkPhysicalDevice physical_devic assert(specific_surface); if (!supported_formats.size()) { - TRY(surface_format_properties_map_init(physical_device, supported_formats, specific_surface->get_formats())); + TRY_LOG_CALL( + surface_format_properties_map_init(physical_device, supported_formats, specific_surface->get_formats())); #if WSI_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN if (layer::instance_private_data::get(physical_device).has_image_compression_support(physical_device)) { - TRY(surface_format_properties_map_add_compression(physical_device, supported_formats, - specific_surface->get_formats())); + TRY_LOG_CALL(surface_format_properties_map_add_compression(physical_device, supported_formats, + specific_surface->get_formats())); } #endif } diff --git a/wsi/wayland/swapchain.cpp b/wsi/wayland/swapchain.cpp index 815eac1..19dc5e4 100644 --- a/wsi/wayland/swapchain.cpp +++ b/wsi/wayland/swapchain.cpp @@ -141,12 +141,10 @@ VkResult swapchain::get_surface_compatible_formats(const VkImageCreateInfo &info /* Query supported modifers. */ util::vector drm_format_props( util::allocator(m_allocator, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); - VkResult result = util::get_drm_format_properties(m_device_data.physical_device, info.format, drm_format_props); - if (result != VK_SUCCESS) - { - WSI_LOG_ERROR("Failed to get format properties."); - return result; - } + + TRY_LOG(util::get_drm_format_properties(m_device_data.physical_device, info.format, drm_format_props), + "Failed to get format properties"); + for (const auto &prop : drm_format_props) { bool is_supported = false; @@ -171,6 +169,8 @@ VkResult swapchain::get_surface_compatible_formats(const VkImageCreateInfo &info VkImageFormatProperties2KHR format_props = {}; format_props.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR; format_props.pNext = &external_props; + + VkResult result = VK_SUCCESS; { VkPhysicalDeviceExternalImageFormatInfoKHR external_info = {}; external_info.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO_KHR; @@ -300,7 +300,7 @@ static VkResult fill_image_create_info(VkImageCreateInfo &image_create_info, VkExternalMemoryImageCreateInfoKHR &external_info, wayland_image_data &image_data, uint64_t modifier) { - TRY(image_data.external_mem.fill_image_plane_layouts(image_plane_layouts)); + TRY_LOG_CALL(image_data.external_mem.fill_image_plane_layouts(image_plane_layouts)); if (image_data.external_mem.is_disjoint()) { @@ -324,20 +324,12 @@ VkResult swapchain::allocate_image(VkImageCreateInfo &image_create_info, wayland { return VK_ERROR_OUT_OF_HOST_MEMORY; } - VkResult result = allocate_wsialloc(m_image_create_info, image_data, importable_formats, &m_allocated_format); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(allocate_wsialloc(m_image_create_info, image_data, importable_formats, &m_allocated_format)); } else { util::vector exportable_modifiers(util::allocator(m_allocator, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); - VkResult result = get_surface_compatible_formats(image_create_info, importable_formats, exportable_modifiers); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(get_surface_compatible_formats(image_create_info, importable_formats, exportable_modifiers)); /* TODO: Handle exportable images which use ICD allocated memory in preference to an external allocator. */ if (importable_formats.empty()) @@ -347,27 +339,19 @@ VkResult swapchain::allocate_image(VkImageCreateInfo &image_create_info, wayland } wsialloc_format allocated_format = { 0 }; - result = allocate_wsialloc(image_create_info, image_data, importable_formats, &allocated_format); - if (result != VK_SUCCESS) - { - return result; - } + TRY_LOG_CALL(allocate_wsialloc(image_create_info, image_data, importable_formats, &allocated_format)); - TRY(fill_image_create_info(image_create_info, m_image_creation_parameters.m_image_layout, - m_image_creation_parameters.m_drm_mod_info, - m_image_creation_parameters.m_external_info, *image_data, allocated_format.modifier)); + TRY_LOG_CALL(fill_image_create_info( + image_create_info, m_image_creation_parameters.m_image_layout, m_image_creation_parameters.m_drm_mod_info, + m_image_creation_parameters.m_external_info, *image_data, allocated_format.modifier)); m_image_create_info = image_create_info; m_allocated_format = allocated_format; } - VkResult result = m_device_data.disp.CreateImage(m_device, &m_image_create_info, get_allocation_callbacks(), image); - if (result != VK_SUCCESS) - { - WSI_LOG_ERROR("Image creation failed."); - return result; - } - return result; + TRY_LOG(m_device_data.disp.CreateImage(m_device, &m_image_create_info, get_allocation_callbacks(), image), + "Image creation failed"); + return VK_SUCCESS; } VkResult swapchain::create_wl_buffer(const VkImageCreateInfo &image_create_info, swapchain_image &image, @@ -413,28 +397,16 @@ VkResult swapchain::create_and_bind_swapchain_image(VkImageCreateInfo image_crea std::unique_lock image_status_lock(m_image_status_mutex); image.data = image_data; image.status = swapchain_image::FREE; - VkResult result = allocate_image(image_create_info, image_data, &image.image); - image_status_lock.unlock(); - if (result != VK_SUCCESS) - { - WSI_LOG_ERROR("Failed to allocate image."); - return result; - } - result = create_wl_buffer(image_create_info, image, image_data); - if (result != VK_SUCCESS) - { - WSI_LOG_ERROR("Failed to create wl_buffer."); - return result; - } + TRY_LOG(allocate_image(image_create_info, image_data, &image.image), "Failed to allocate image"); + image_status_lock.unlock(); + + TRY_LOG(create_wl_buffer(image_create_info, image, image_data), "Failed to create wl_buffer"); image_data->external_mem.set_memory_handle_type(VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT); - result = image_data->external_mem.import_memory_and_bind_swapchain_image(image.image); - if (result != VK_SUCCESS) - { - WSI_LOG_ERROR("Failed to import memory and bind swapchain image."); - return result; - } + + TRY_LOG(image_data->external_mem.import_memory_and_bind_swapchain_image(image.image), + "Failed to import memory and bind swapchain image"); /* Initialize presentation fence. */ auto present_fence = sync_fd_fence_sync::create(m_device_data); diff --git a/wsi/wsi_factory.cpp b/wsi/wsi_factory.cpp index 90fb6a5..bd86f66 100644 --- a/wsi/wsi_factory.cpp +++ b/wsi/wsi_factory.cpp @@ -109,7 +109,7 @@ util::wsi_platform_set find_enabled_layer_platforms(const VkInstanceCreateInfo * { for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { - const char* ext_requested_by_user = pCreateInfo->ppEnabledExtensionNames[i]; + const char *ext_requested_by_user = pCreateInfo->ppEnabledExtensionNames[i]; if (strcmp(ext_requested_by_user, ext_provided_by_layer.extension.extensionName) == 0) { ret.add(ext_provided_by_layer.platform); @@ -123,17 +123,19 @@ static VkResult get_available_device_extensions(VkPhysicalDevice physical_device util::extension_list &available_extensions) { auto &instance_data = layer::instance_private_data::get(physical_device); - util::vector properties{available_extensions.get_allocator()}; + util::vector properties{ available_extensions.get_allocator() }; uint32_t count = 0; - TRY(instance_data.disp.EnumerateDeviceExtensionProperties(physical_device, nullptr, &count, nullptr)); + TRY_LOG(instance_data.disp.EnumerateDeviceExtensionProperties(physical_device, nullptr, &count, nullptr), + "Failed to enumurate properties of available physical device extensions"); if (!properties.try_resize(count)) { return VK_ERROR_OUT_OF_HOST_MEMORY; } - TRY(instance_data.disp.EnumerateDeviceExtensionProperties(physical_device, nullptr, &count, properties.data())); - TRY(available_extensions.add(properties.data(), count)); + TRY_LOG(instance_data.disp.EnumerateDeviceExtensionProperties(physical_device, nullptr, &count, properties.data()), + "Failed to enumurate properties of available physical device extensions"); + TRY_LOG_CALL(available_extensions.add(properties.data(), count)); return VK_SUCCESS; } @@ -141,15 +143,15 @@ static VkResult get_available_device_extensions(VkPhysicalDevice physical_device VkResult add_extensions_required_by_layer(VkPhysicalDevice phys_dev, const util::wsi_platform_set enabled_platforms, util::extension_list &extensions_to_enable) { - util::allocator allocator{extensions_to_enable.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND}; + util::allocator allocator{ extensions_to_enable.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND }; - util::extension_list available_device_extensions{allocator}; - TRY(get_available_device_extensions(phys_dev, available_device_extensions)); + util::extension_list available_device_extensions{ allocator }; + TRY_LOG(get_available_device_extensions(phys_dev, available_device_extensions), + "Failed to acquire available device extensions"); /* Add optional extensions independent of winsys. */ { - const char *optional_extensions[] = - { + const char *optional_extensions[] = { VK_KHR_EXTERNAL_FENCE_EXTENSION_NAME, VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME, @@ -161,7 +163,7 @@ VkResult add_extensions_required_by_layer(VkPhysicalDevice phys_dev, const util: { if (available_device_extensions.contains(extension)) { - TRY(extensions_to_enable.add(extension)); + TRY_LOG_CALL(extensions_to_enable.add(extension)); } } } @@ -174,14 +176,16 @@ VkResult add_extensions_required_by_layer(VkPhysicalDevice phys_dev, const util: continue; } - util::extension_list extensions_required_by_layer{allocator}; + util::extension_list extensions_required_by_layer{ allocator }; surface_properties *props = get_surface_properties(wsi_ext.platform); if (props == nullptr) { return VK_ERROR_INITIALIZATION_FAILED; } - TRY(props->get_required_device_extensions(extensions_required_by_layer)); + TRY_LOG(props->get_required_device_extensions(extensions_required_by_layer), + "Failed to acquire required device extensions"); + bool supported = available_device_extensions.contains(extensions_required_by_layer); if (!supported) { @@ -193,7 +197,7 @@ VkResult add_extensions_required_by_layer(VkPhysicalDevice phys_dev, const util: return VK_ERROR_INITIALIZATION_FAILED; } - TRY(extensions_to_enable.add(extensions_required_by_layer)); + TRY_LOG_CALL(extensions_to_enable.add(extensions_required_by_layer)); } return VK_SUCCESS;