From b1aedcf1d5ca2cfd4241299681fe0c4cadc029d4 Mon Sep 17 00:00:00 2001 From: Zhongwang Zhang Date: Mon, 6 Nov 2023 16:26:25 +0800 Subject: [PATCH] st/oxr: Enable VK_EXT_debug_utils extension for client side on the platform that support it --- src/xrt/state_trackers/oxr/oxr_objects.h | 1 + .../state_trackers/oxr/oxr_session_gfx_vk.c | 2 +- src/xrt/state_trackers/oxr/oxr_vulkan.c | 103 ++++++++++++++++-- 3 files changed, 93 insertions(+), 13 deletions(-) diff --git a/src/xrt/state_trackers/oxr/oxr_objects.h b/src/xrt/state_trackers/oxr/oxr_objects.h index 8ed6c7780..095408998 100644 --- a/src/xrt/state_trackers/oxr/oxr_objects.h +++ b/src/xrt/state_trackers/oxr/oxr_objects.h @@ -1293,6 +1293,7 @@ struct oxr_system bool external_fence_fd_enabled; bool external_semaphore_fd_enabled; bool timeline_semaphore_enabled; + bool debug_utils_enabled; } vk; #endif diff --git a/src/xrt/state_trackers/oxr/oxr_session_gfx_vk.c b/src/xrt/state_trackers/oxr/oxr_session_gfx_vk.c index a9b2e6c8e..e35ea1d11 100644 --- a/src/xrt/state_trackers/oxr/oxr_session_gfx_vk.c +++ b/src/xrt/state_trackers/oxr/oxr_session_gfx_vk.c @@ -119,7 +119,7 @@ oxr_session_populate_vk(struct oxr_logger *log, external_fence_fd_enabled, // external_semaphore_fd_enabled, // timeline_semaphore_enabled, // - false, // + sess->sys->vk.debug_utils_enabled, // false, // next->queueFamilyIndex, // next->queueIndex); // diff --git a/src/xrt/state_trackers/oxr/oxr_vulkan.c b/src/xrt/state_trackers/oxr/oxr_vulkan.c index 82d2e1344..9f709b70b 100644 --- a/src/xrt/state_trackers/oxr/oxr_vulkan.c +++ b/src/xrt/state_trackers/oxr/oxr_vulkan.c @@ -110,6 +110,12 @@ static const char *required_vk_instance_extensions[] = { VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, // }; +static const char *optional_vk_instance_extensions[] = { +#if defined(VK_EXT_debug_utils) + VK_EXT_DEBUG_UTILS_EXTENSION_NAME, +#endif +}; + // The device extensions do vary by platform, but in a very regular way. // This should match the list in comp_compositor, except it shouldn't include // VK_KHR_SWAPCHAIN_EXTENSION_NAME @@ -166,6 +172,57 @@ static const char *optional_device_extensions[] = { #endif }; +static bool +vk_check_extension(VkExtensionProperties *props, uint32_t prop_count, const char *ext) +{ + for (uint32_t i = 0; i < prop_count; i++) { + if (strcmp(props[i].extensionName, ext) == 0) { + return true; + } + } + + return false; +} + +static XrResult +vk_get_instance_ext_props(struct oxr_logger *log, + VkInstance instance, + PFN_vkGetInstanceProcAddr GetInstanceProcAddr, + VkExtensionProperties **out_props, + uint32_t *out_prop_count) +{ + PFN_vkEnumerateInstanceExtensionProperties EnumerateInstanceExtensionProperties = + (PFN_vkEnumerateInstanceExtensionProperties)vkGetInstanceProcAddr(NULL, + "vkEnumerateInstanceExtensionProperties"); + + if (!EnumerateInstanceExtensionProperties) { + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, + "Failed to get EnumerateInstanceExtensionProperties fp"); + } + + uint32_t prop_count = 0; + VkResult res = EnumerateInstanceExtensionProperties(NULL, &prop_count, NULL); + if (res != VK_SUCCESS) { + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, + "Failed to enumerate instance extension properties count (%d)", res); + } + + + VkExtensionProperties *props = U_TYPED_ARRAY_CALLOC(VkExtensionProperties, prop_count); + + res = EnumerateInstanceExtensionProperties(NULL, &prop_count, props); + if (res != VK_SUCCESS) { + free(props); + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, + "Failed to enumerate instance extension properties (%d)", res); + } + + *out_props = props; + *out_prop_count = prop_count; + + return XR_SUCCESS; +} + XrResult oxr_vk_create_vulkan_instance(struct oxr_logger *log, struct oxr_system *sys, @@ -183,9 +240,37 @@ oxr_vk_create_vulkan_instance(struct oxr_logger *log, return XR_SUCCESS; } + VkExtensionProperties *props = NULL; + uint32_t prop_count = 0; + XrResult res = vk_get_instance_ext_props(log, sys->vulkan_enable2_instance, createInfo->pfnGetInstanceProcAddr, + &props, &prop_count); + if (res != XR_SUCCESS) { + return res; + } + struct u_string_list *instance_ext_list = u_string_list_create_from_array( required_vk_instance_extensions, ARRAY_SIZE(required_vk_instance_extensions)); +#if defined(VK_EXT_debug_utils) + bool debug_utils_enabled = false; +#endif + + for (uint32_t i = 0; i < ARRAY_SIZE(optional_vk_instance_extensions); i++) { + + if (optional_vk_instance_extensions[i] == NULL || + !vk_check_extension(props, prop_count, optional_vk_instance_extensions[i])) { + continue; + } + + u_string_list_append_unique(instance_ext_list, optional_vk_instance_extensions[i]); + +#if defined(VK_EXT_debug_utils) + if (strcmp(optional_vk_instance_extensions[i], VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) { + debug_utils_enabled = true; + } +#endif + } + for (uint32_t i = 0; i < createInfo->vulkanCreateInfo->enabledExtensionCount; i++) { u_string_list_append_unique(instance_ext_list, createInfo->vulkanCreateInfo->ppEnabledExtensionNames[i]); @@ -213,6 +298,12 @@ oxr_vk_create_vulkan_instance(struct oxr_logger *log, oxr_log_slog(log, &slog); } +#if defined(VK_EXT_debug_utils) + if (*vulkanResult == VK_SUCCESS) { + sys->vk.debug_utils_enabled = debug_utils_enabled; + } +#endif + u_string_list_destroy(&instance_ext_list); return XR_SUCCESS; @@ -258,18 +349,6 @@ vk_get_device_ext_props(struct oxr_logger *log, return XR_SUCCESS; } -static bool -vk_check_extension(VkExtensionProperties *props, uint32_t prop_count, const char *ext) -{ - for (uint32_t i = 0; i < prop_count; i++) { - if (strcmp(props[i].extensionName, ext) == 0) { - return true; - } - } - - return false; -} - static XrResult vk_get_device_features(struct oxr_logger *log, VkInstance instance,