diff --git a/include/precomp.h b/include/precomp.h index 5c8e3bc..369270b 100644 --- a/include/precomp.h +++ b/include/precomp.h @@ -2,7 +2,10 @@ #include #include #include +#include #include #include #include #include + +#include "utilities.h" diff --git a/include/utilities.h b/include/utilities.h new file mode 100644 index 0000000..d002903 --- /dev/null +++ b/include/utilities.h @@ -0,0 +1,5 @@ +#pragma once + +namespace veng { +bool streq(gsl::czstring left, gsl::czstring right); +} diff --git a/include/vulkan/graphics.h b/include/vulkan/graphics.h index 08bc416..e93c4bc 100644 --- a/include/vulkan/graphics.h +++ b/include/vulkan/graphics.h @@ -1,5 +1,6 @@ #pragma once #include + #include "glfw/glfw_window.h" namespace veng { @@ -12,7 +13,9 @@ class Graphics final { void InitializeVulkan(); void CreateInstance(); - gsl::span GetSuggestedExtentions(); + static gsl::span GetSuggestedExtentions(); + static std::vector GetSupprotedInstanceExtensions(); + static bool AreAllExtensionsSupported(gsl::span extensions); VkInstance instance_ = nullptr; gsl::not_null window_; diff --git a/src/utilities.cpp b/src/utilities.cpp new file mode 100644 index 0000000..6991d68 --- /dev/null +++ b/src/utilities.cpp @@ -0,0 +1,9 @@ +#include "utilities.h" + +#include "precomp.h" + +namespace veng { +bool streq(gsl::czstring left, gsl::czstring right) { + return std::strcmp(left, right) == 0; +} +} // namespace veng diff --git a/src/vulkan/graphics.cpp b/src/vulkan/graphics.cpp index 0f23e0e..4912f64 100644 --- a/src/vulkan/graphics.cpp +++ b/src/vulkan/graphics.cpp @@ -18,6 +18,7 @@ void Graphics::InitializeVulkan() { CreateInstance(); } void Graphics::CreateInstance() { gsl::span suggested_extentions = GetSuggestedExtentions(); + if (!AreAllExtensionsSupported(suggested_extentions)) std::exit(EXIT_FAILURE); VkApplicationInfo app_info = {}; app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; @@ -48,4 +49,36 @@ gsl::span Graphics::GetSuggestedExtentions() { return {glfw_extentions, glfw_extention_count}; } +std::vector Graphics::GetSupprotedInstanceExtensions() { + VkExtensionProperties buffer[32]; + std::uint32_t count; + vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr); + + if (count == 0) return {}; + + std::vector properties(count); + vkEnumerateInstanceExtensionProperties(nullptr, &count, properties.data()); + return properties; +} + +bool ExtentionMatchesName(gsl::czstring name, + const VkExtensionProperties& properties) { + return streq(properties.extensionName, name); +} + +bool IsExtensionSupported(gsl::span extensions, + gsl::czstring name) { + return std::any_of(extensions.begin(), extensions.end(), + std::bind_front(ExtentionMatchesName, name)); +} + +bool Graphics::AreAllExtensionsSupported(gsl::span extensions) { + std::vector supported_extensions = + GetSupprotedInstanceExtensions(); + + return std::all_of( + extensions.begin(), extensions.end(), + std::bind_front(IsExtensionSupported, supported_extensions)); +} + } // namespace veng