The open source OpenXR runtime

a/vk: Add vk_surface_info helpers

+286
+2
src/xrt/auxiliary/vk/CMakeLists.txt
··· 19 19 vk_image_readback_to_xf_pool.h 20 20 vk_print.c 21 21 vk_state_creators.c 22 + vk_surface_info.c 23 + vk_surface_info.h 22 24 vk_sync_objects.c 23 25 vk_time.c 24 26 )
+214
src/xrt/auxiliary/vk/vk_surface_info.c
··· 1 + // Copyright 2019-2023, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Helper for getting information from a VkSurfaceKHR. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @ingroup aux_vk 8 + */ 9 + 10 + #include "util/u_pretty_print.h" 11 + #include "vk_surface_info.h" 12 + 13 + 14 + /* 15 + * 16 + * Helpers. 17 + * 18 + */ 19 + 20 + #define P(...) u_pp(dg, __VA_ARGS__) 21 + #define PNT(...) u_pp(dg, "\n\t" __VA_ARGS__) 22 + #define PNTT(...) u_pp(dg, "\n\t\t" __VA_ARGS__) 23 + 24 + XRT_CHECK_RESULT static VkResult 25 + surface_info_get_present_modes(struct vk_bundle *vk, struct vk_surface_info *info, VkSurfaceKHR surface) 26 + { 27 + VkResult ret; 28 + 29 + assert(info->present_modes == NULL); 30 + assert(info->present_mode_count == 0); 31 + 32 + ret = vk->vkGetPhysicalDeviceSurfacePresentModesKHR( // 33 + vk->physical_device, // 34 + surface, // 35 + &info->present_mode_count, // 36 + NULL); // 37 + if (ret != VK_SUCCESS) { 38 + VK_ERROR(vk, "vkGetPhysicalDeviceSurfacePresentModesKHR: %s", vk_result_string(ret)); 39 + return ret; 40 + } 41 + // Nothing to do. 42 + if (info->present_mode_count == 0) { 43 + return VK_SUCCESS; 44 + } 45 + 46 + info->present_modes = U_TYPED_ARRAY_CALLOC(VkPresentModeKHR, info->present_mode_count); 47 + ret = vk->vkGetPhysicalDeviceSurfacePresentModesKHR( // 48 + vk->physical_device, // 49 + surface, // 50 + &info->present_mode_count, // 51 + info->present_modes); // 52 + if (ret == VK_SUCCESS) { 53 + return ret; 54 + } 55 + 56 + free(info->present_modes); 57 + info->present_mode_count = 0; 58 + info->present_modes = NULL; 59 + 60 + VK_ERROR(vk, "vkGetPhysicalDeviceSurfacePresentModesKHR: %s", vk_result_string(ret)); 61 + 62 + return ret; 63 + } 64 + 65 + XRT_CHECK_RESULT static VkResult 66 + surface_info_get_surface_formats(struct vk_bundle *vk, struct vk_surface_info *info, VkSurfaceKHR surface) 67 + { 68 + VkResult ret; 69 + 70 + assert(info->formats == NULL); 71 + assert(info->format_count == 0); 72 + 73 + ret = vk->vkGetPhysicalDeviceSurfaceFormatsKHR( // 74 + vk->physical_device, // 75 + surface, // 76 + &info->format_count, // 77 + NULL); // 78 + if (ret != VK_SUCCESS) { 79 + VK_ERROR(vk, "vkGetPhysicalDeviceSurfaceFormatsKHR: %s", vk_result_string(ret)); 80 + return ret; 81 + } 82 + // Nothing to do. 83 + if (info->format_count == 0) { 84 + return VK_SUCCESS; 85 + } 86 + 87 + info->formats = U_TYPED_ARRAY_CALLOC(VkSurfaceFormatKHR, info->format_count); 88 + ret = vk->vkGetPhysicalDeviceSurfaceFormatsKHR( // 89 + vk->physical_device, // 90 + surface, // 91 + &info->format_count, // 92 + info->formats); // 93 + if (ret == VK_SUCCESS) { 94 + return ret; 95 + } 96 + 97 + free(info->formats); 98 + info->format_count = 0; 99 + info->formats = NULL; 100 + 101 + VK_ERROR(vk, "vkGetPhysicalDeviceSurfaceFormatsKHR: %s", vk_result_string(ret)); 102 + 103 + return ret; 104 + } 105 + 106 + 107 + /* 108 + * 109 + * 'Exported' functions. 110 + * 111 + */ 112 + 113 + void 114 + vk_surface_info_destroy(struct vk_surface_info *info) 115 + { 116 + if (info->present_modes != NULL) { 117 + free(info->present_modes); 118 + info->present_mode_count = 0; 119 + info->present_modes = NULL; 120 + } 121 + 122 + if (info->formats != NULL) { 123 + free(info->formats); 124 + info->format_count = 0; 125 + info->formats = NULL; 126 + } 127 + 128 + U_ZERO(info); 129 + } 130 + 131 + XRT_CHECK_RESULT VkResult 132 + vk_surface_info_fill_in(struct vk_bundle *vk, struct vk_surface_info *info, VkSurfaceKHR surface) 133 + { 134 + VkResult ret; 135 + 136 + ret = surface_info_get_present_modes(vk, info, surface); 137 + if (ret != VK_SUCCESS) { 138 + VK_ERROR(vk, "surface_info_get_present_modes: %s", vk_result_string(ret)); 139 + goto error; 140 + } 141 + 142 + ret = surface_info_get_surface_formats(vk, info, surface); 143 + if (ret != VK_SUCCESS) { 144 + VK_ERROR(vk, "surface_info_get_surface_formats: %s", vk_result_string(ret)); 145 + goto error; 146 + } 147 + 148 + ret = vk->vkGetPhysicalDeviceSurfaceCapabilitiesKHR( // 149 + vk->physical_device, // 150 + surface, // 151 + &info->caps); // 152 + if (ret != VK_SUCCESS) { 153 + VK_ERROR(vk, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR: %s", vk_result_string(ret)); 154 + goto error; 155 + } 156 + 157 + #ifdef VK_EXT_display_surface_counter 158 + if (vk->has_EXT_display_control) { 159 + info->caps2.sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT; 160 + ret = vk->vkGetPhysicalDeviceSurfaceCapabilities2EXT( // 161 + vk->physical_device, // 162 + surface, // 163 + &info->caps2); // 164 + if (ret != VK_SUCCESS) { 165 + VK_ERROR(vk, "vkGetPhysicalDeviceSurfaceCapabilities2EXT: %s", vk_result_string(ret)); 166 + goto error; 167 + } 168 + } 169 + #endif 170 + 171 + return VK_SUCCESS; 172 + 173 + error: 174 + vk_surface_info_destroy(info); 175 + 176 + return ret; 177 + } 178 + 179 + void 180 + vk_print_surface_info(struct vk_bundle *vk, struct vk_surface_info *info, enum u_logging_level log_level) 181 + { 182 + if (vk->log_level > log_level) { 183 + return; 184 + } 185 + 186 + struct u_pp_sink_stack_only sink; 187 + u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink); 188 + 189 + P("VkSurfaceKHR info:"); 190 + PNT("caps.minImageCount: %u", info->caps.minImageCount); 191 + PNT("caps.maxImageCount: %u", info->caps.maxImageCount); 192 + PNT("caps.currentExtent: %ux%u", info->caps.currentExtent.width, info->caps.currentExtent.height); 193 + PNT("caps.minImageExtent: %ux%u", info->caps.minImageExtent.width, info->caps.minImageExtent.height); 194 + PNT("caps.maxImageExtent: %ux%u", info->caps.maxImageExtent.width, info->caps.maxImageExtent.height); 195 + PNT("caps.maxImageArrayLayers: %u", info->caps.maxImageArrayLayers); 196 + // PNT("caps.supportedTransforms") 197 + // PNT("caps.currentTransform") 198 + // PNT("caps.supportedCompositeAlpha") 199 + // PNT("caps.supportedUsageFlags") 200 + 201 + PNT("present_modes(%u):", info->present_mode_count); 202 + for (uint32_t i = 0; i < info->present_mode_count; i++) { 203 + PNTT("%s", vk_present_mode_string(info->present_modes[i])); 204 + } 205 + 206 + PNT("formats(%u):", info->format_count); 207 + for (uint32_t i = 0; i < info->format_count; i++) { 208 + VkSurfaceFormatKHR *f = &info->formats[i]; 209 + PNTT("[format = %s, colorSpace = %s]", vk_format_string(f->format), 210 + vk_color_space_string(f->colorSpace)); 211 + } 212 + 213 + U_LOG_IFL(log_level, vk->log_level, "%s", sink.buffer); 214 + }
+70
src/xrt/auxiliary/vk/vk_surface_info.h
··· 1 + // Copyright 2019-2023, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Helper for getting information from a VkSurfaceKHR. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @ingroup aux_vk 8 + */ 9 + 10 + #pragma once 11 + 12 + #include "vk/vk_helpers.h" 13 + 14 + 15 + #ifdef __cplusplus 16 + extern "C" { 17 + #endif 18 + 19 + 20 + /* 21 + * 22 + * Struct(s). 23 + * 24 + */ 25 + 26 + struct vk_surface_info 27 + { 28 + VkPresentModeKHR *present_modes; 29 + VkSurfaceFormatKHR *formats; 30 + 31 + uint32_t present_mode_count; 32 + uint32_t format_count; 33 + 34 + VkSurfaceCapabilitiesKHR caps; 35 + 36 + #ifdef VK_EXT_display_surface_counter 37 + VkSurfaceCapabilities2EXT caps2; 38 + #endif 39 + }; 40 + 41 + 42 + /* 43 + * 44 + * Functions. 45 + * 46 + */ 47 + 48 + /*! 49 + * Free all lists allocated by @ref vk_surface_info. 50 + */ 51 + void 52 + vk_surface_info_destroy(struct vk_surface_info *info); 53 + 54 + /*! 55 + * Fill in the given @ref vk_surface_info, will allocate lists. 56 + */ 57 + XRT_CHECK_RESULT VkResult 58 + vk_surface_info_fill_in(struct vk_bundle *vk, struct vk_surface_info *info, VkSurfaceKHR surface); 59 + 60 + /*! 61 + * Print out the gathered information about the 62 + * surface given to @ref vk_surface_info_fill_in. 63 + */ 64 + void 65 + vk_print_surface_info(struct vk_bundle *vk, struct vk_surface_info *info, enum u_logging_level log_level); 66 + 67 + 68 + #ifdef __cplusplus 69 + } 70 + #endif