c/main: Use vk_surface_info when creating images

This commit is contained in:
Jakob Bornecrantz 2023-04-27 01:28:23 +01:00
parent 622e09bc19
commit 2a17212d7f

View file

@ -14,6 +14,8 @@
#include "util/u_pacing.h"
#include "util/u_pretty_print.h"
#include "vk/vk_surface_info.h"
#include "main/comp_compositor.h"
#include "main/comp_target_swapchain.h"
@ -187,71 +189,34 @@ select_image_count(struct comp_target_swapchain *cts,
}
static bool
check_surface_present_mode(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkPresentModeKHR present_mode)
check_surface_present_mode(struct comp_target_swapchain *cts,
const struct vk_surface_info *info,
VkPresentModeKHR present_mode)
{
struct vk_bundle *vk = get_vk(cts);
VkResult ret;
uint32_t present_mode_count;
VkPresentModeKHR *present_modes;
ret = vk->vkGetPhysicalDeviceSurfacePresentModesKHR(vk->physical_device, surface, &present_mode_count, NULL);
if (present_mode_count != 0) {
present_modes = U_TYPED_ARRAY_CALLOC(VkPresentModeKHR, present_mode_count);
vk->vkGetPhysicalDeviceSurfacePresentModesKHR(vk->physical_device, surface, &present_mode_count,
present_modes);
} else {
COMP_ERROR(cts->base.c, "Could not enumerate present modes. '%s'", vk_result_string(ret));
return false;
}
for (uint32_t i = 0; i < present_mode_count; i++) {
if (present_modes[i] == present_mode) {
free(present_modes);
for (uint32_t i = 0; i < info->present_mode_count; i++) {
if (info->present_modes[i] == present_mode) {
return true;
}
}
free(present_modes);
COMP_ERROR(cts->base.c, "Requested present mode not supported.\n");
struct u_pp_sink_stack_only sink;
u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink);
u_pp(dg, "Present mode %s not supported, available:", vk_present_mode_string(present_mode));
for (uint32_t i = 0; i < info->present_mode_count; i++) {
u_pp(dg, "\n\t%s", vk_present_mode_string(info->present_modes[i]));
}
COMP_ERROR(cts->base.c, "%s", sink.buffer);
return false;
}
static bool
find_surface_format(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkSurfaceFormatKHR *format)
find_surface_format(struct comp_target_swapchain *cts, const struct vk_surface_info *info, VkSurfaceFormatKHR *format)
{
struct vk_bundle *vk = get_vk(cts);
uint32_t format_count;
VkSurfaceFormatKHR *formats = NULL;
VkResult ret;
ret = vk->vkGetPhysicalDeviceSurfaceFormatsKHR(vk->physical_device, surface, &format_count, NULL);
if (format_count != 0) {
formats = U_TYPED_ARRAY_CALLOC(VkSurfaceFormatKHR, format_count);
vk->vkGetPhysicalDeviceSurfaceFormatsKHR(vk->physical_device, surface, &format_count, formats);
} else {
COMP_ERROR(cts->base.c, "Could not enumerate surface formats. '%s'", vk_result_string(ret));
return false;
}
{
struct u_pp_sink_stack_only sink;
u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink);
u_pp(dg, "VkSurfaceKHR returned VkSurfaceFormatKHR:");
// Dump formats
for (uint32_t i = 0; i < format_count; i++) {
u_pp(dg, "\n\t%i [%s, %s]", i, vk_format_string(formats[i].format),
vk_color_space_string(formats[i].colorSpace));
}
COMP_DEBUG(cts->base.c, "%s", sink.buffer);
}
VkSurfaceFormatKHR *formats_for_colorspace = NULL;
formats_for_colorspace = U_TYPED_ARRAY_CALLOC(VkSurfaceFormatKHR, format_count);
formats_for_colorspace = U_TYPED_ARRAY_CALLOC(VkSurfaceFormatKHR, info->format_count);
uint32_t format_for_colorspace_count = 0;
uint32_t pref_format_count = ARRAY_SIZE(preferred_color_formats);
@ -260,9 +225,9 @@ find_surface_format(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkS
// from these in preference to others.
for (uint32_t i = 0; i < format_count; i++) {
if (formats[i].colorSpace == cts->preferred.color_space) {
formats_for_colorspace[format_for_colorspace_count] = formats[i];
for (uint32_t i = 0; i < info->format_count; i++) {
if (info->formats[i].colorSpace == cts->preferred.color_space) {
formats_for_colorspace[format_for_colorspace_count] = info->formats[i];
format_for_colorspace_count++;
}
}
@ -303,9 +268,9 @@ find_surface_format(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkS
// we have nothing with the preferred colorspace? we can try to
// return a preferred format at least
for (uint32_t i = 0; i < format_count; i++) {
for (uint32_t i = 0; i < info->format_count; i++) {
for (uint32_t j = 0; j < pref_format_count; j++) {
if (formats[i].format == preferred_color_formats[j]) {
if (info->formats[i].format == preferred_color_formats[j]) {
*format = formats_for_colorspace[i];
COMP_ERROR(cts->base.c,
"Returning known-wrong color space! Color shift may occur.");
@ -316,7 +281,7 @@ find_surface_format(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkS
// if we are still here, we should just return the first format
// we have. we know its the wrong colorspace, and its not on our
// list of preferred formats, but its something.
*format = formats[0];
*format = info->formats[0];
COMP_ERROR(cts->base.c,
"Returning fallback format! cue up some Kenny Loggins, cos we're in the DANGER ZONE!");
goto cleanup;
@ -327,7 +292,6 @@ find_surface_format(struct comp_target_swapchain *cts, VkSurfaceKHR surface, VkS
cleanup:
free(formats_for_colorspace);
free(formats);
COMP_DEBUG(cts->base.c,
"VkSurfaceFormatKHR"
@ -342,7 +306,6 @@ cleanup:
error:
free(formats_for_colorspace);
free(formats);
return false;
}
@ -695,33 +658,54 @@ comp_target_swapchain_create_images(struct comp_target *ct,
&supported); // pSupported
if (ret != VK_SUCCESS) {
COMP_ERROR(ct->c, "vkGetPhysicalDeviceSurfaceSupportKHR: %s", vk_result_string(ret));
destroy_old(cts, old_swapchain_handle);
return;
} else if (!supported) {
COMP_ERROR(ct->c, "vkGetPhysicalDeviceSurfaceSupportKHR: Surface not supported!");
destroy_old(cts, old_swapchain_handle);
return;
}
if (!check_surface_present_mode(cts, cts->surface.handle, cts->present_mode)) {
// Get information.
struct vk_surface_info info = {0};
ret = vk_surface_info_fill_in(vk, &info, cts->surface.handle);
if (ret != VK_SUCCESS) {
VK_ERROR(vk, "vk_surface_info_fill_in: %s", vk_result_string(ret));
destroy_old(cts, old_swapchain_handle);
return;
}
// Always print the first one.
{
static bool first = true;
if (first) {
vk_print_surface_info(vk, &info, U_LOGGING_INFO);
first = false;
} else {
vk_print_surface_info(vk, &info, U_LOGGING_DEBUG);
}
}
if (!check_surface_present_mode(cts, &info, cts->present_mode)) {
// Free old.
destroy_old(cts, old_swapchain_handle);
vk_surface_info_destroy(&info);
return;
}
// Find the correct format.
if (!find_surface_format(cts, cts->surface.handle, &cts->surface.format)) {
if (!find_surface_format(cts, &info, &cts->surface.format)) {
// Free old.
destroy_old(cts, old_swapchain_handle);
vk_surface_info_destroy(&info);
return;
}
// Get the caps first.
VkSurfaceCapabilitiesKHR surface_caps;
ret = vk->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk->physical_device, cts->surface.handle, &surface_caps);
if (ret != VK_SUCCESS) {
COMP_ERROR(ct->c, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR: %s", vk_result_string(ret));
VkSurfaceCapabilitiesKHR surface_caps = info.caps;
// Free old.
destroy_old(cts, old_swapchain_handle);
return;
}
// Now we can free the info.
vk_surface_info_destroy(&info);
// Get the extents of the swapchain.
VkExtent2D extent = select_extent(cts, surface_caps, preferred_width, preferred_height);
@ -735,9 +719,6 @@ comp_target_swapchain_create_images(struct comp_target *ct,
extent.height = w2;
}
COMP_DEBUG(ct->c, "swapchain minImageCount %d maxImageCount %d", surface_caps.minImageCount,
surface_caps.maxImageCount);
// Get the image count.
const uint32_t preferred_at_least_image_count = 3;
uint32_t image_count = select_image_count(cts, surface_caps, preferred_at_least_image_count);