Skip to content

Commit

Permalink
Refactor detection of extension support in api sample swapchain_recre…
Browse files Browse the repository at this point in the history
  • Loading branch information
asuessenbach authored Jun 3, 2024
1 parent e453310 commit ada3895
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 57 deletions.
73 changes: 19 additions & 54 deletions samples/api/swapchain_recreation/swapchain_recreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,51 +40,6 @@ void SwapchainRecreation::get_queue()
}
}

void SwapchainRecreation::check_for_maintenance1()
{
const char *use_maintenance1 = std::getenv("USE_MAINTENANCE1");
has_maintenance1 = use_maintenance1 == nullptr || strcmp(use_maintenance1, "no") != 0;

if (!has_maintenance1)
{
LOGI("Disabling usage of VK_EXT_surface_maintenance1 due to USE_MAINTENANCE1=no");
return;
}

VkResult result = volkInitialize();
if (result)
{
throw vkb::VulkanException(result, "Failed to initialize volk.");
}

// Check to see if VK_EXT_surface_maintenance1 is supported in the first place.
// Assume that VK_EXT_swapchain_maintenance1 is also supported in that case.
uint32_t instance_extension_count;
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr));

std::vector<VkExtensionProperties> available_instance_extensions(instance_extension_count);
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, available_instance_extensions.data()));

const bool has_ext = std::find_if(available_instance_extensions.begin(),
available_instance_extensions.end(),
[](auto const &ep) {
return strcmp(ep.extensionName, VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME) == 0;
}) != available_instance_extensions.end();

if (has_ext)
{
// It is indeed supported.
add_instance_extension(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME);
add_instance_extension(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
add_device_extension(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
return;
}

// Extension is not supported by the driver or loader.
LOGI("Skipping unsupported VK_EXT_surface_maintenance1");
has_maintenance1 = false;
}

void SwapchainRecreation::query_surface_format()
{
surface_format = vkb::select_surface_format(get_gpu_handle(), get_surface());
Expand Down Expand Up @@ -937,6 +892,19 @@ VkDevice SwapchainRecreation::get_device_handle()

SwapchainRecreation::SwapchainRecreation()
{
const char *use_maintenance1 = std::getenv("USE_MAINTENANCE1");

if ((use_maintenance1 == nullptr) || (strcmp(use_maintenance1, "no") != 0))
{
// Request sample-specific extensions as optional
add_instance_extension(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME, true);
add_instance_extension(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME, true);
add_device_extension(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME, true);
}
else
{
LOGI("Disabling usage of VK_EXT_surface_maintenance1 due to USE_MAINTENANCE1=no");
}
}

SwapchainRecreation::~SwapchainRecreation()
Expand Down Expand Up @@ -1005,16 +973,13 @@ SwapchainRecreation::~SwapchainRecreation()
}
}

bool SwapchainRecreation::prepare(const vkb::ApplicationOptions &options)
std::unique_ptr<vkb::Device> SwapchainRecreation::create_device(vkb::PhysicalDevice &gpu)
{
// Add the relevant instance extensions before creating the instance in
// VulkanSample::prepare.
check_for_maintenance1();
std::unique_ptr<vkb::Device> device = vkb::VulkanSample<vkb::BindingType::C>::create_device(gpu);

if (!VulkanSample::prepare(options))
{
return false;
}
has_maintenance1 = get_instance().is_enabled(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME) &&
get_instance().is_enabled(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME) &&
device->is_enabled(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);

LOGI("------------------------------------");
LOGI("USAGE:");
Expand All @@ -1028,7 +993,7 @@ bool SwapchainRecreation::prepare(const vkb::ApplicationOptions &options)
}
LOGI("------------------------------------");

return true;
return device;
}

void SwapchainRecreation::create_render_context()
Expand Down
5 changes: 2 additions & 3 deletions samples/api/swapchain_recreation/swapchain_recreation.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class SwapchainRecreation : public vkb::VulkanSample<vkb::BindingType::C>

void prepare_render_context() override;

bool prepare(const vkb::ApplicationOptions &options) override;

void update(float delta_time) override;

bool resize(uint32_t width, uint32_t height) override;
Expand Down Expand Up @@ -170,8 +168,9 @@ class SwapchainRecreation : public vkb::VulkanSample<vkb::BindingType::C>
// User toggles.
bool recreate_swapchain_on_present_mode_change = false;

std::unique_ptr<vkb::Device> create_device(vkb::PhysicalDevice &gpu) override;

void get_queue();
void check_for_maintenance1();
void query_surface_format();
void query_present_modes();
void query_compatible_present_modes(VkPresentModeKHR present_mode);
Expand Down

0 comments on commit ada3895

Please sign in to comment.