The open source OpenXR runtime

c/main: Use vk_surface_info when creating images

+55 -74
+55 -74
src/xrt/compositor/main/comp_target_swapchain.c
··· 14 14 #include "util/u_pacing.h" 15 15 #include "util/u_pretty_print.h" 16 16 17 + #include "vk/vk_surface_info.h" 18 + 17 19 #include "main/comp_compositor.h" 18 20 #include "main/comp_target_swapchain.h" 19 21 ··· 187 189 } 188 190 189 191 static bool 190 - check_surface_present_mode(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkPresentModeKHR present_mode) 192 + check_surface_present_mode(struct comp_target_swapchain *cts, 193 + const struct vk_surface_info *info, 194 + VkPresentModeKHR present_mode) 191 195 { 192 - struct vk_bundle *vk = get_vk(cts); 193 - VkResult ret; 196 + for (uint32_t i = 0; i < info->present_mode_count; i++) { 197 + if (info->present_modes[i] == present_mode) { 198 + return true; 199 + } 200 + } 194 201 195 - uint32_t present_mode_count; 196 - VkPresentModeKHR *present_modes; 197 - ret = vk->vkGetPhysicalDeviceSurfacePresentModesKHR(vk->physical_device, surface, &present_mode_count, NULL); 202 + struct u_pp_sink_stack_only sink; 203 + u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink); 198 204 199 - if (present_mode_count != 0) { 200 - present_modes = U_TYPED_ARRAY_CALLOC(VkPresentModeKHR, present_mode_count); 201 - vk->vkGetPhysicalDeviceSurfacePresentModesKHR(vk->physical_device, surface, &present_mode_count, 202 - present_modes); 203 - } else { 204 - COMP_ERROR(cts->base.c, "Could not enumerate present modes. '%s'", vk_result_string(ret)); 205 - return false; 205 + u_pp(dg, "Present mode %s not supported, available:", vk_present_mode_string(present_mode)); 206 + for (uint32_t i = 0; i < info->present_mode_count; i++) { 207 + u_pp(dg, "\n\t%s", vk_present_mode_string(info->present_modes[i])); 206 208 } 207 209 208 - for (uint32_t i = 0; i < present_mode_count; i++) { 209 - if (present_modes[i] == present_mode) { 210 - free(present_modes); 211 - return true; 212 - } 213 - } 210 + COMP_ERROR(cts->base.c, "%s", sink.buffer); 214 211 215 - free(present_modes); 216 - COMP_ERROR(cts->base.c, "Requested present mode not supported.\n"); 217 212 return false; 218 213 } 219 214 220 215 static bool 221 - find_surface_format(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkSurfaceFormatKHR *format) 216 + find_surface_format(struct comp_target_swapchain *cts, const struct vk_surface_info *info, VkSurfaceFormatKHR *format) 222 217 { 223 - struct vk_bundle *vk = get_vk(cts); 224 - uint32_t format_count; 225 - VkSurfaceFormatKHR *formats = NULL; 226 - VkResult ret; 227 - 228 - ret = vk->vkGetPhysicalDeviceSurfaceFormatsKHR(vk->physical_device, surface, &format_count, NULL); 229 - 230 - if (format_count != 0) { 231 - formats = U_TYPED_ARRAY_CALLOC(VkSurfaceFormatKHR, format_count); 232 - vk->vkGetPhysicalDeviceSurfaceFormatsKHR(vk->physical_device, surface, &format_count, formats); 233 - } else { 234 - COMP_ERROR(cts->base.c, "Could not enumerate surface formats. '%s'", vk_result_string(ret)); 235 - return false; 236 - } 237 - 238 - { 239 - struct u_pp_sink_stack_only sink; 240 - u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink); 241 - 242 - u_pp(dg, "VkSurfaceKHR returned VkSurfaceFormatKHR:"); 243 - 244 - // Dump formats 245 - for (uint32_t i = 0; i < format_count; i++) { 246 - u_pp(dg, "\n\t%i [%s, %s]", i, vk_format_string(formats[i].format), 247 - vk_color_space_string(formats[i].colorSpace)); 248 - } 249 - 250 - COMP_DEBUG(cts->base.c, "%s", sink.buffer); 251 - } 252 - 253 218 VkSurfaceFormatKHR *formats_for_colorspace = NULL; 254 - formats_for_colorspace = U_TYPED_ARRAY_CALLOC(VkSurfaceFormatKHR, format_count); 219 + formats_for_colorspace = U_TYPED_ARRAY_CALLOC(VkSurfaceFormatKHR, info->format_count); 255 220 256 221 uint32_t format_for_colorspace_count = 0; 257 222 uint32_t pref_format_count = ARRAY_SIZE(preferred_color_formats); ··· 260 225 // from these in preference to others. 261 226 262 227 263 - for (uint32_t i = 0; i < format_count; i++) { 264 - if (formats[i].colorSpace == cts->preferred.color_space) { 265 - formats_for_colorspace[format_for_colorspace_count] = formats[i]; 228 + for (uint32_t i = 0; i < info->format_count; i++) { 229 + if (info->formats[i].colorSpace == cts->preferred.color_space) { 230 + formats_for_colorspace[format_for_colorspace_count] = info->formats[i]; 266 231 format_for_colorspace_count++; 267 232 } 268 233 } ··· 303 268 304 269 // we have nothing with the preferred colorspace? we can try to 305 270 // return a preferred format at least 306 - for (uint32_t i = 0; i < format_count; i++) { 271 + for (uint32_t i = 0; i < info->format_count; i++) { 307 272 for (uint32_t j = 0; j < pref_format_count; j++) { 308 - if (formats[i].format == preferred_color_formats[j]) { 273 + if (info->formats[i].format == preferred_color_formats[j]) { 309 274 *format = formats_for_colorspace[i]; 310 275 COMP_ERROR(cts->base.c, 311 276 "Returning known-wrong color space! Color shift may occur."); ··· 316 281 // if we are still here, we should just return the first format 317 282 // we have. we know its the wrong colorspace, and its not on our 318 283 // list of preferred formats, but its something. 319 - *format = formats[0]; 284 + *format = info->formats[0]; 320 285 COMP_ERROR(cts->base.c, 321 286 "Returning fallback format! cue up some Kenny Loggins, cos we're in the DANGER ZONE!"); 322 287 goto cleanup; ··· 327 292 328 293 cleanup: 329 294 free(formats_for_colorspace); 330 - free(formats); 331 295 332 296 COMP_DEBUG(cts->base.c, 333 297 "VkSurfaceFormatKHR" ··· 342 306 343 307 error: 344 308 free(formats_for_colorspace); 345 - free(formats); 346 309 return false; 347 310 } 348 311 ··· 695 658 &supported); // pSupported 696 659 if (ret != VK_SUCCESS) { 697 660 COMP_ERROR(ct->c, "vkGetPhysicalDeviceSurfaceSupportKHR: %s", vk_result_string(ret)); 661 + destroy_old(cts, old_swapchain_handle); 662 + return; 698 663 } else if (!supported) { 699 664 COMP_ERROR(ct->c, "vkGetPhysicalDeviceSurfaceSupportKHR: Surface not supported!"); 665 + destroy_old(cts, old_swapchain_handle); 666 + return; 700 667 } 701 668 702 - if (!check_surface_present_mode(cts, cts->surface.handle, cts->present_mode)) { 703 - // Free old. 669 + // Get information. 670 + struct vk_surface_info info = {0}; 671 + ret = vk_surface_info_fill_in(vk, &info, cts->surface.handle); 672 + if (ret != VK_SUCCESS) { 673 + VK_ERROR(vk, "vk_surface_info_fill_in: %s", vk_result_string(ret)); 704 674 destroy_old(cts, old_swapchain_handle); 705 675 return; 706 676 } 707 677 708 - // Find the correct format. 709 - if (!find_surface_format(cts, cts->surface.handle, &cts->surface.format)) { 678 + // Always print the first one. 679 + { 680 + static bool first = true; 681 + if (first) { 682 + vk_print_surface_info(vk, &info, U_LOGGING_INFO); 683 + first = false; 684 + } else { 685 + vk_print_surface_info(vk, &info, U_LOGGING_DEBUG); 686 + } 687 + } 688 + 689 + if (!check_surface_present_mode(cts, &info, cts->present_mode)) { 710 690 // Free old. 711 691 destroy_old(cts, old_swapchain_handle); 692 + vk_surface_info_destroy(&info); 712 693 return; 713 694 } 714 695 715 - // Get the caps first. 716 - VkSurfaceCapabilitiesKHR surface_caps; 717 - ret = vk->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk->physical_device, cts->surface.handle, &surface_caps); 718 - if (ret != VK_SUCCESS) { 719 - COMP_ERROR(ct->c, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR: %s", vk_result_string(ret)); 720 - 696 + // Find the correct format. 697 + if (!find_surface_format(cts, &info, &cts->surface.format)) { 721 698 // Free old. 722 699 destroy_old(cts, old_swapchain_handle); 700 + vk_surface_info_destroy(&info); 723 701 return; 724 702 } 703 + 704 + // Get the caps first. 705 + VkSurfaceCapabilitiesKHR surface_caps = info.caps; 706 + 707 + // Now we can free the info. 708 + vk_surface_info_destroy(&info); 725 709 726 710 // Get the extents of the swapchain. 727 711 VkExtent2D extent = select_extent(cts, surface_caps, preferred_width, preferred_height); ··· 734 718 extent.width = h2; 735 719 extent.height = w2; 736 720 } 737 - 738 - COMP_DEBUG(ct->c, "swapchain minImageCount %d maxImageCount %d", surface_caps.minImageCount, 739 - surface_caps.maxImageCount); 740 721 741 722 // Get the image count. 742 723 const uint32_t preferred_at_least_image_count = 3;