The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

a/vk: Improve format list handling

This fixes scope error on struct and combines the format list on Android.

+57 -24
+57 -24
src/xrt/auxiliary/vk/vk_image_allocator.c
··· 22 22 #ifdef XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER 23 23 #include "android/android_ahardwarebuffer_allocator.h" 24 24 #endif 25 + 26 + 27 + /* 28 + * 29 + * Struct. 30 + * 31 + */ 32 + 33 + /* 34 + * Small helper to merge the format list the app can provide with any needed 35 + * workaround format list, like the one needed on Android for SRGB swapchains. 36 + */ 37 + struct format_list_helper 38 + { 39 + // Make it slightly larger so extra formats can always fit. 40 + VkFormat formats[XRT_MAX_SWAPCHAIN_CREATE_INFO_FORMAT_LIST_COUNT + 2]; 41 + 42 + // Counts the total amount of formats. 43 + uint32_t format_count; 44 + }; 45 + 46 + 25 47 /* 26 48 * 27 49 * Helper functions. ··· 40 62 #else 41 63 #error "need port" 42 64 #endif 65 + } 66 + 67 + static void 68 + add_format_non_dup(struct format_list_helper *flh, VkFormat format) 69 + { 70 + for (uint32_t i = 0; i < flh->format_count; i++) { 71 + if (flh->formats[i] == format) { 72 + return; 73 + } 74 + } 75 + 76 + if (flh->format_count >= ARRAY_SIZE(flh->formats)) { 77 + U_LOG_E("Too many formats!"); 78 + return; 79 + } 80 + 81 + flh->formats[flh->format_count++] = format; 43 82 } 44 83 45 84 static VkResult ··· 124 163 }; 125 164 CHAIN(external_memory_image_create_info); 126 165 166 + // Format list helper needed for the below. 167 + struct format_list_helper flh = XRT_STRUCT_INIT; 168 + 169 + // If the format list is used add them here. 170 + for (uint32_t i = 0; i < info->format_count; i++) { 171 + add_format_non_dup(&flh, info->formats[i]); 172 + } 173 + 174 + 127 175 #if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER) 128 176 VkExternalFormatANDROID format_android = { 129 177 .sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID, ··· 131 179 }; 132 180 CHAIN(format_android); 133 181 134 - #ifdef VK_KHR_image_format_list 135 - VkImageFormatListCreateInfoKHR image_format_list_create_info = { 136 - .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR, 137 - .pNext = NULL, 138 - .viewFormatCount = 2, 139 - .pViewFormats = 140 - (VkFormat[2]){ 141 - VK_FORMAT_R8G8B8A8_UNORM, 142 - VK_FORMAT_R8G8B8A8_SRGB, 143 - }, 144 - }; 145 - #endif 146 182 // Android can't allocate native sRGB. 147 183 // Use UNORM and correct gamma later. 148 184 if (image_format == VK_FORMAT_R8G8B8A8_SRGB) { ··· 156 192 assert(a_buffer_format_props.format != VK_FORMAT_UNDEFINED); // Make sure there is a Vulkan format. 157 193 assert(format_android.externalFormat == 0); 158 194 159 - #ifdef VK_KHR_image_format_list 160 - if (vk->has_KHR_image_format_list) { 161 - CHAIN(image_format_list_create_info); 162 - } 163 - #endif 195 + add_format_non_dup(&flh, VK_FORMAT_R8G8B8A8_UNORM); 196 + add_format_non_dup(&flh, VK_FORMAT_R8G8B8A8_SRGB); 164 197 } 165 198 #endif 166 199 167 200 #ifdef VK_KHR_image_format_list 168 - if (vk->has_KHR_image_format_list && info->format_count != 0) { 169 - VkImageFormatListCreateInfoKHR image_format_list_create_info = { 170 - .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR, 171 - .viewFormatCount = info->format_count, 172 - .pViewFormats = info->formats, 173 - }; 201 + VkImageFormatListCreateInfoKHR image_format_list_create_info = { 202 + .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR, 203 + .viewFormatCount = flh.format_count, 204 + .pViewFormats = flh.formats, 205 + }; 174 206 207 + if (vk->has_KHR_image_format_list && flh.format_count != 0) { 175 208 CHAIN(image_format_list_create_info); 176 209 } 177 210 #else 178 - if (info->format_count != 0) { 211 + if (flh.format_count != 0) { 179 212 VK_WARN(vk, "VK_KHR_image_format_list not supported"); 180 213 } 181 214 #endif