From 3427b0039b2c97f152b7279b2b333ec66873e64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=BC=C3=9Fenbach?= Date: Wed, 12 Apr 2023 11:48:59 +0200 Subject: [PATCH] Introduce overload of getExtensionDepends to get availability and dependencies by vulkan version. (#1559) --- VulkanHppGenerator.cpp | 61 ++++++++++++++++++- VulkanHppGenerator.hpp | 1 + .../ExtensionInspection.cpp | 17 +++++- vulkan/vulkan_extension_inspection.hpp | 29 +++++++++ vulkan/vulkansc_extension_inspection.hpp | 5 +- 5 files changed, 109 insertions(+), 4 deletions(-) diff --git a/VulkanHppGenerator.cpp b/VulkanHppGenerator.cpp index 15f9847..7d740b0 100644 --- a/VulkanHppGenerator.cpp +++ b/VulkanHppGenerator.cpp @@ -141,6 +141,7 @@ namespace VULKAN_HPP_NAMESPACE std::set const & getInstanceExtensions(); std::map const & getDeprecatedExtensions(); std::map> const & getExtensionDepends( std::string const & extension ); + ${getExtensionDependsByVersionDeclaration} std::map const & getObsoletedExtensions(); std::map const & getPromotedExtensions(); VULKAN_HPP_CONSTEXPR_20 std::string getExtensionDeprecatedBy( std::string const & extension ); @@ -182,6 +183,8 @@ namespace VULKAN_HPP_NAMESPACE return ( depIt != dependencies.end() ) ? depIt->second : noDependencies; } + ${getExtensionDependsByVersionDefinition} + VULKAN_HPP_INLINE std::map const & getObsoletedExtensions() { static std::map obsoletedExtensions = { ${obsoletedExtensions} }; @@ -255,6 +258,8 @@ namespace VULKAN_HPP_NAMESPACE []( ExtensionData const & extension ) { return extension.deprecatedBy; } ) }, { "deprecatedTest", generateExtensionReplacedTest( []( ExtensionData const & extension ) { return extension.isDeprecated; } ) }, { "extensionDependencies", generateExtensionDependencies() }, + { "getExtensionDependsByVersionDeclaration", generateExtensionDependsByVersion( false ) }, + { "getExtensionDependsByVersionDefinition", generateExtensionDependsByVersion( true ) }, { "instanceExtensions", generateExtensionsList( "instance" ) }, { "instanceTest", generateExtensionTypeTest( "instance" ) }, { "licenseHeader", m_vulkanLicenseHeader }, @@ -5676,7 +5681,7 @@ std::string VulkanHppGenerator::generateEnumValueName( std::string const & enumN std::string VulkanHppGenerator::generateExtensionDependencies() const { std::string extensionDependencies, previousEnter, previousLeave; - for (auto const& extension : m_extensions) + for ( auto const & extension : m_extensions ) { if ( !extension.depends.empty() ) { @@ -5715,6 +5720,60 @@ std::string VulkanHppGenerator::generateExtensionDependencies() const return extensionDependencies; } +std::string VulkanHppGenerator::generateExtensionDependsByVersion( bool definition ) const +{ + if (m_api != "vulkan") + { + return ""; + } + + if ( definition ) + { + const std::string generateExtensionDependsTemplate = R"( VULKAN_HPP_INLINE std::pair const &> getExtensionDepends( std::string const & version, std::string const & extension ) + { +#if !defined( NDEBUG ) + static std::set versions = { ${versions} }; + assert( versions.find( version ) != versions.end() ); +#endif + static std::vector noDependencies; + + std::map> const & dependencies = getExtensionDepends( extension ); + if ( dependencies.empty() ) + { + return { true, noDependencies }; + } + auto depIt = dependencies.lower_bound( version ); + if ( ( depIt == dependencies.end() ) || ( depIt->first != version ) ) + { + depIt = std::prev( depIt ); + } + if ( depIt == dependencies.end() ) + { + return { false, noDependencies }; + } + else + { + return { true, depIt->second }; + } + } +)"; + + std::string versions; + for (auto const& feature : m_features) + { + versions += "\"" + feature.name + "\", "; + } + assert( versions.ends_with( ", " ) ); + versions = versions.substr( 0, versions.length() - 2 ); + + return replaceWithMap( generateExtensionDependsTemplate, { { "versions", versions } } ); + } + else + { + return "std::pair const &> getExtensionDepends( std::string const & version, std::string const & extension );"; + } +} + template std::string VulkanHppGenerator::generateExtensionReplacedBy( Predicate p, Extraction e ) const { diff --git a/VulkanHppGenerator.hpp b/VulkanHppGenerator.hpp index 5339c92..2df56e6 100644 --- a/VulkanHppGenerator.hpp +++ b/VulkanHppGenerator.hpp @@ -694,6 +694,7 @@ private: std::pair generateEnumSuffixes( std::string const & name, bool bitmask ) const; std::string generateEnumValueName( std::string const & enumName, std::string const & valueName, bool bitmask ) const; std::string generateExtensionDependencies() const; + std::string generateExtensionDependsByVersion( bool definition ) const; template std::string generateExtensionReplacedBy( Predicate p, Extraction e ) const; template diff --git a/tests/ExtensionInspection/ExtensionInspection.cpp b/tests/ExtensionInspection/ExtensionInspection.cpp index c977b20..91aa1d4 100644 --- a/tests/ExtensionInspection/ExtensionInspection.cpp +++ b/tests/ExtensionInspection/ExtensionInspection.cpp @@ -51,7 +51,22 @@ int main( int /*argc*/, char ** /*argv*/ ) std::map> depends = vk::getExtensionDepends( VK_KHR_SWAPCHAIN_EXTENSION_NAME ); assert( ( depends.size() == 1 ) && ( depends.begin()->first == "VK_VERSION_1_0" ) && ( depends.begin()->second.size() == 1 ) && - ( depends.begin()->second[0] == VK_KHR_DISPLAY_EXTENSION_NAME ) ); + ( depends.begin()->second[0] == VK_KHR_SURFACE_EXTENSION_NAME ) ); + + auto [available0, deps0] = vk::getExtensionDepends( "VK_VERSION_1_0", VK_KHR_SURFACE_EXTENSION_NAME ); + assert( available0 && deps0.empty() ); + + auto [available1, deps1] = vk::getExtensionDepends( "VK_VERSION_1_0", VK_KHR_SWAPCHAIN_EXTENSION_NAME ); + assert( available1 && ( deps1.size() == 1 ) && ( deps1[0] == VK_KHR_SURFACE_EXTENSION_NAME ) ); + + auto [available2, deps2] = vk::getExtensionDepends( "VK_VERSION_1_1", VK_KHR_SWAPCHAIN_EXTENSION_NAME ); + assert( available2 && ( deps2.size() == 1 ) && ( deps2[0] == VK_KHR_SURFACE_EXTENSION_NAME ) ); + + auto [available3, deps3] = vk::getExtensionDepends( "VK_VERSION_1_1", VK_EXT_SHADER_TILE_IMAGE_EXTENSION_NAME ); + assert( !available3 ); + + auto [available4, deps4] = vk::getExtensionDepends( "VK_VERSION_1_3", VK_EXT_SHADER_TILE_IMAGE_EXTENSION_NAME ); + assert( available4 && deps4.empty() ); std::map const & deprecatedExtensions = vk::getDeprecatedExtensions(); auto deprecatedIt = deprecatedExtensions.find( VK_EXT_DEBUG_REPORT_EXTENSION_NAME ); diff --git a/vulkan/vulkan_extension_inspection.hpp b/vulkan/vulkan_extension_inspection.hpp index c5c5515..e28b428 100644 --- a/vulkan/vulkan_extension_inspection.hpp +++ b/vulkan/vulkan_extension_inspection.hpp @@ -22,6 +22,7 @@ namespace VULKAN_HPP_NAMESPACE std::set const & getInstanceExtensions(); std::map const & getDeprecatedExtensions(); std::map> const & getExtensionDepends( std::string const & extension ); + std::pair const &> getExtensionDepends( std::string const & version, std::string const & extension ); std::map const & getObsoletedExtensions(); std::map const & getPromotedExtensions(); VULKAN_HPP_CONSTEXPR_20 std::string getExtensionDeprecatedBy( std::string const & extension ); @@ -802,6 +803,34 @@ namespace VULKAN_HPP_NAMESPACE return ( depIt != dependencies.end() ) ? depIt->second : noDependencies; } + VULKAN_HPP_INLINE std::pair const &> getExtensionDepends( std::string const & version, std::string const & extension ) + { +#if !defined( NDEBUG ) + static std::set versions = { "VK_VERSION_1_0", "VK_VERSION_1_1", "VK_VERSION_1_2", "VK_VERSION_1_3" }; + assert( versions.find( version ) != versions.end() ); +#endif + static std::vector noDependencies; + + std::map> const & dependencies = getExtensionDepends( extension ); + if ( dependencies.empty() ) + { + return { true, noDependencies }; + } + auto depIt = dependencies.lower_bound( version ); + if ( ( depIt == dependencies.end() ) || ( depIt->first != version ) ) + { + depIt = std::prev( depIt ); + } + if ( depIt == dependencies.end() ) + { + return { false, noDependencies }; + } + else + { + return { true, depIt->second }; + } + } + VULKAN_HPP_INLINE std::map const & getObsoletedExtensions() { static std::map obsoletedExtensions = { { "VK_AMD_negative_viewport_height", "VK_KHR_maintenance1" } }; diff --git a/vulkan/vulkansc_extension_inspection.hpp b/vulkan/vulkansc_extension_inspection.hpp index f744415..905c889 100644 --- a/vulkan/vulkansc_extension_inspection.hpp +++ b/vulkan/vulkansc_extension_inspection.hpp @@ -22,8 +22,9 @@ namespace VULKAN_HPP_NAMESPACE std::set const & getInstanceExtensions(); std::map const & getDeprecatedExtensions(); std::map> const & getExtensionDepends( std::string const & extension ); - std::map const & getObsoletedExtensions(); - std::map const & getPromotedExtensions(); + + std::map const & getObsoletedExtensions(); + std::map const & getPromotedExtensions(); VULKAN_HPP_CONSTEXPR_20 std::string getExtensionDeprecatedBy( std::string const & extension ); VULKAN_HPP_CONSTEXPR_20 std::string getExtensionObsoletedBy( std::string const & extension ); VULKAN_HPP_CONSTEXPR_20 std::string getExtensionPromotedTo( std::string const & extension );