From 6fe966c6d985bab4d97bdaa55e1e97ea9dd4aec9 Mon Sep 17 00:00:00 2001 From: asuessenbach Date: Wed, 21 Sep 2022 09:28:38 +0200 Subject: [PATCH] Fix inconsistency with return type of vk::raii::SwapchainKHR::getImages() + some minor cleanup --- .../05_InitSwapchain/05_InitSwapchain.cpp | 12 +- RAII_Samples/utils/utils.hpp | 6 +- VulkanHppGenerator.cpp | 182 ++++++++---------- VulkanHppGenerator.hpp | 13 +- vulkan/vulkan_raii.hpp | 16 +- 5 files changed, 103 insertions(+), 126 deletions(-) diff --git a/RAII_Samples/05_InitSwapchain/05_InitSwapchain.cpp b/RAII_Samples/05_InitSwapchain/05_InitSwapchain.cpp index bdde900..3d8ccf2 100644 --- a/RAII_Samples/05_InitSwapchain/05_InitSwapchain.cpp +++ b/RAII_Samples/05_InitSwapchain/05_InitSwapchain.cpp @@ -48,8 +48,8 @@ int main( int /*argc*/, char ** /*argv*/ ) // determine a queueFamilyIndex that suports present // first check if the graphicsQueueFamiliyIndex is good enough uint32_t presentQueueFamilyIndex = physicalDevice.getSurfaceSupportKHR( graphicsQueueFamilyIndex, *surface ) - ? graphicsQueueFamilyIndex - : vk::su::checked_cast( queueFamilyProperties.size() ); + ? graphicsQueueFamilyIndex + : vk::su::checked_cast( queueFamilyProperties.size() ); if ( presentQueueFamilyIndex == queueFamilyProperties.size() ) { // the graphicsQueueFamilyIndex doesn't support present -> look for an other family index that supports both @@ -109,8 +109,8 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::PresentModeKHR swapchainPresentMode = vk::PresentModeKHR::eFifo; vk::SurfaceTransformFlagBitsKHR preTransform = ( surfaceCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity ) - ? vk::SurfaceTransformFlagBitsKHR::eIdentity - : surfaceCapabilities.currentTransform; + ? vk::SurfaceTransformFlagBitsKHR::eIdentity + : surfaceCapabilities.currentTransform; vk::CompositeAlphaFlagBitsKHR compositeAlpha = ( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePreMultiplied ) ? vk::CompositeAlphaFlagBitsKHR::ePreMultiplied @@ -146,14 +146,14 @@ int main( int /*argc*/, char ** /*argv*/ ) } vk::raii::SwapchainKHR swapChain( device, swapChainCreateInfo ); - std::vector swapChainImages = swapChain.getImages(); + std::vector swapChainImages = swapChain.getImages(); std::vector imageViews; imageViews.reserve( swapChainImages.size() ); vk::ImageViewCreateInfo imageViewCreateInfo( {}, {}, vk::ImageViewType::e2D, format, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 } ); for ( auto image : swapChainImages ) { - imageViewCreateInfo.image = static_cast( image ); + imageViewCreateInfo.image = image; imageViews.push_back( { device, imageViewCreateInfo } ); } diff --git a/RAII_Samples/utils/utils.hpp b/RAII_Samples/utils/utils.hpp index e80d146..850252e 100644 --- a/RAII_Samples/utils/utils.hpp +++ b/RAII_Samples/utils/utils.hpp @@ -26,9 +26,9 @@ #endif #include "../../samples/utils/utils.hpp" -#include #include +#include namespace vk { @@ -398,14 +398,14 @@ namespace vk vk::ImageViewCreateInfo imageViewCreateInfo( {}, {}, vk::ImageViewType::e2D, colorFormat, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 } ); for ( auto image : images ) { - imageViewCreateInfo.image = static_cast( image ); + imageViewCreateInfo.image = image; imageViews.emplace_back( device, imageViewCreateInfo ); } } vk::Format colorFormat; vk::raii::SwapchainKHR swapChain = nullptr; - std::vector images; + std::vector images; std::vector imageViews; }; diff --git a/VulkanHppGenerator.cpp b/VulkanHppGenerator.cpp index b00627f..678d9f4 100644 --- a/VulkanHppGenerator.cpp +++ b/VulkanHppGenerator.cpp @@ -20,16 +20,18 @@ #include #include -void checkAttributes( int line, - std::map const & attributes, - std::map> const & required, - std::map> const & optional ); -void checkElements( int line, - std::vector const & elements, - std::map const & required, - std::set const & optional = {} ); -void checkForError( bool condition, int line, std::string const & message ); -void checkForWarning( bool condition, int line, std::string const & message ); +void checkAttributes( int line, + std::map const & attributes, + std::map> const & required, + std::map> const & optional ); +void checkElements( int line, + std::vector const & elements, + std::map const & required, + std::set const & optional = {} ); +void checkForError( bool condition, int line, std::string const & message ); +void checkForWarning( bool condition, int line, std::string const & message ); +template +std::vector findAll( InputIt first, InputIt last, UnaryPredicate p ); std::string findTag( std::set const & tags, std::string const & name, std::string const & postfix = "" ); std::string generateCArraySizes( std::vector const & sizes ); std::pair generateEnumSuffixes( std::string const & name, bool bitmask, std::set const & tags ); @@ -44,7 +46,6 @@ template std::vector getChildElements( ElementContainer const * element ); std::pair, std::string> readModifiers( tinyxml2::XMLNode const * node ); std::string readSnippet( std::string const & snippetFile ); -void replaceAll( std::string & str, std::string const & from, std::string const & to ); std::string replaceWithMap( std::string const & input, std::map replacements ); std::string startLowerCase( std::string const & input ); std::string startUpperCase( std::string const & input ); @@ -54,28 +55,11 @@ std::string stripPrefix( std::string const std::string toCamelCase( std::string const & value ); std::string toUpperCase( std::string const & name ); std::vector tokenize( std::string const & tokenString, std::string const & separator ); -template -std::string toString( StringContainer const & strings ); -std::string toString( tinyxml2::XMLError error ); -std::string trim( std::string const & input ); -std::string trimEnd( std::string const & input ); -std::string trimStars( std::string const & input ); -void writeToFile( std::string const & str, std::string const & fileName ); - -template -std::vector findAll( InputIt first, InputIt last, UnaryPredicate p ) -{ - std::vector result; - while ( first != last ) - { - if ( p( *first ) ) - { - result.push_back( first ); - } - ++first; - } - return result; -} +std::string toString( tinyxml2::XMLError error ); +std::string trim( std::string const & input ); +std::string trimEnd( std::string const & input ); +std::string trimStars( std::string const & input ); +void writeToFile( std::string const & str, std::string const & fileName ); const std::set altLens = { "2*VK_UUID_SIZE", "codeSize / 4", "(rasterizationSamples + 31) / 32", "(samples + 31) / 32" }; const std::set specialPointerTypes = { "Display", "IDirectFB", "wl_display", "xcb_connection_t", "_screen_window" }; @@ -102,15 +86,15 @@ VulkanHppGenerator::VulkanHppGenerator( tinyxml2::XMLDocument const & document ) { addMissingFlagBits( feature.second.requireData, feature.first ); } - for ( auto & ext : m_extensions ) + for ( auto & extension : m_extensions ) { - addMissingFlagBits( ext.second.requireData, ext.first ); + addMissingFlagBits( extension.second.requireData, extension.first ); } // determine the extensionsByNumber map for ( auto extensionIt = m_extensions.begin(); extensionIt != m_extensions.end(); ++extensionIt ) { - int number = atoi( extensionIt->second.number.c_str() ); + int number = stoi( extensionIt->second.number ); assert( m_extensionsByNumber.find( number ) == m_extensionsByNumber.end() ); m_extensionsByNumber[number] = extensionIt; } @@ -459,17 +443,8 @@ ${staticAssertions} #endif )"; - std::string staticAssertions; - for ( auto const & feature : m_features ) - { - staticAssertions += generateStaticAssertions( feature.second.requireData, feature.first ); - } - for ( auto const & extIt : m_extensionsByNumber ) - { - staticAssertions += generateStaticAssertions( extIt.second->second.requireData, extIt.second->first ); - } - - std::string str = replaceWithMap( vulkanHandlesHppTemplate, { { "licenseHeader", m_vulkanLicenseHeader }, { "staticAssertions", staticAssertions } } ); + std::string str = + replaceWithMap( vulkanHandlesHppTemplate, { { "licenseHeader", m_vulkanLicenseHeader }, { "staticAssertions", generateStaticAssertions() } } ); writeToFile( str, static_assertions_hpp ); } @@ -533,17 +508,15 @@ void VulkanHppGenerator::prepareRAIIHandles() { // filter out functions that are not usefull on this level of abstraction (like vkGetInstanceProcAddr) // and all the construction and destruction functions, as they are used differently - for ( auto & handle : m_handles ) + assert( m_handles.begin()->first.empty() ); + for ( auto handleIt = std::next( m_handles.begin() ); handleIt != m_handles.end(); ++handleIt ) { - if ( !handle.first.empty() ) + handleIt->second.destructorIt = determineRAIIHandleDestructor( handleIt->first ); + if ( handleIt->second.destructorIt != m_commands.end() ) { - handle.second.destructorIt = determineRAIIHandleDestructor( handle.first ); - if ( handle.second.destructorIt != m_commands.end() ) - { - m_RAIISpecialFunctions.insert( handle.second.destructorIt->first ); - } - handle.second.constructorIts = determineRAIIHandleConstructors( handle.first, handle.second.destructorIt ); + m_RAIISpecialFunctions.insert( handleIt->second.destructorIt->first ); } + handleIt->second.constructorIts = determineRAIIHandleConstructors( handleIt->first, handleIt->second.destructorIt ); } distributeSecondLevelCommands( m_RAIISpecialFunctions ); @@ -575,7 +548,7 @@ void VulkanHppGenerator::addCommand( std::string const & name, CommandData & com { handleIt = m_handles.find( "" ); } - checkForError( handleIt != m_handles.end(), commandData.xmlLine, "could not find a handle to hold command <" + name + ">" ); + assert( handleIt != m_handles.end() ); commandData.handle = handleIt->first; // add this command to the list of commands @@ -597,7 +570,7 @@ void VulkanHppGenerator::addMissingFlagBits( std::vector & requireD auto bitmaskIt = m_bitmasks.find( type ); if ( ( bitmaskIt != m_bitmasks.end() ) && bitmaskIt->second.requirements.empty() ) { - // generate the flagBits enum name out of the bitmask name + // generate the flagBits enum name out of the bitmask name: VkFooFlagsXXX -> VkFooFlagBitsXXX size_t pos = bitmaskIt->first.find( "Flags" ); assert( pos != std::string::npos ); std::string flagBits = bitmaskIt->first.substr( 0, pos + 4 ) + "Bit" + bitmaskIt->first.substr( pos + 4 ); @@ -609,7 +582,7 @@ void VulkanHppGenerator::addMissingFlagBits( std::vector & requireD bitmaskIt->second.requirements = flagBits; // some flagsBits are specified but never listed as required for any flags! - // so, even if this bitmask has not enum listed as required, it might still already exist in the enums list + // so, even if this bitmask has no enum listed as required, it might still already exist in the enums list if ( m_enums.find( flagBits ) == m_enums.end() ) { m_enums.insert( std::make_pair( flagBits, EnumData( 0, true ) ) ); @@ -648,6 +621,7 @@ std::string VulkanHppGenerator::addTitleAndProtection( std::string const & title bool VulkanHppGenerator::allVectorSizesSupported( std::vector const & params, std::map const & vectorParams ) const { + // check if all vector sizes are by value and their type is one of "uint32_t", "VkDeviceSize", or "VkSampleCountFlagBits" return std::find_if_not( vectorParams.begin(), vectorParams.end(), [¶ms]( auto const & vpi ) @@ -1440,8 +1414,7 @@ std::vector VulkanHppGenerator::determineConstPointerParams( std::vector std::vector VulkanHppGenerator::determineDataTypes( std::vector const & params, std::map const & vectorParams, std::vector const & returnParams, - std::set const & templatedParams, - bool raii ) const + std::set const & templatedParams ) const { std::vector dataTypes; for ( auto rp : returnParams ) @@ -1459,10 +1432,6 @@ std::vector VulkanHppGenerator::determineDataTypes( std::vector const & singularParams, - std::set const & templatedParams, - bool raiiHandleMemberFunction ) const + std::set const & templatedParams ) const { std::string argument; ParamData const & param = params[paramIndex]; @@ -2505,7 +2473,7 @@ std::string VulkanHppGenerator::generateCallArgumentEnhanced( std::vector const & singularParams, bool raiiHandleMemberFunction ) const +std::string VulkanHppGenerator::generateCallArgumentEnhancedNonConstPointer( ParamData const & param, + size_t paramIndex, + bool nonConstPointerAsNullptr, + std::set const & singularParams ) const { std::string argument; std::string name = startLowerCase( stripPrefix( param.name, "p" ) ); @@ -2624,7 +2594,7 @@ std::string VulkanHppGenerator::generateCallArgumentEnhancedNonConstPointer( // get the data of the array, which also covers no data -> no need to look at param.optional argument = name + ".data()"; } - if ( ( param.type.type.starts_with( "Vk" ) || ( param.type.type == "void" ) ) && ( !raiiHandleMemberFunction || !isHandleType( param.type.type ) ) ) + if ( param.type.type.starts_with( "Vk" ) || ( param.type.type == "void" ) ) { argument = "reinterpret_cast<" + param.type.compose( "" ) + ">( " + argument + " )"; } @@ -3053,7 +3023,7 @@ std::string VulkanHppGenerator::generateCommandEnhanced( std::string const & needsVectorSizeCheck( commandData.params, vectorParams, returnParams, singularParams ); bool enumerating = determineEnumeration( vectorParams, returnParams ); - std::vector dataTypes = determineDataTypes( commandData.params, vectorParams, returnParams, templatedParams, false ); + std::vector dataTypes = determineDataTypes( commandData.params, vectorParams, returnParams, templatedParams ); std::string dataType = combineDataTypes( vectorParams, returnParams, singular, enumerating, dataTypes, unique, false ); std::string argumentTemplates = generateArgumentTemplates( commandData.params, returnParams, vectorParams, templatedParams, chained, false ); @@ -6630,7 +6600,7 @@ std::string VulkanHppGenerator::generateRAIIHandleCommandEnhanced( std::mapfirst == "vkGetMemoryHostPointerPropertiesEXT" ) ? std::set() : determineVoidPointerParams( commandIt->second.params ); bool enumerating = determineEnumeration( vectorParams, returnParams ); - std::vector dataTypes = determineDataTypes( commandIt->second.params, vectorParams, returnParams, templatedParams, true ); + std::vector dataTypes = determineDataTypes( commandIt->second.params, vectorParams, returnParams, templatedParams ); std::string dataType = combineDataTypes( vectorParams, returnParams, singular, enumerating, dataTypes, false, true ); std::string argumentTemplates = generateArgumentTemplates( commandIt->second.params, returnParams, vectorParams, templatedParams, chained, true ); @@ -7543,7 +7513,7 @@ std::string else { assert( !param.optional ); - arguments += generateCallArgumentEnhanced( constructorIt->second.params, i, nonConstPointerAsNullptr, singularParams, {}, true ); + arguments += generateCallArgumentEnhanced( constructorIt->second.params, i, nonConstPointerAsNullptr, singularParams, {} ); } encounteredArgument = true; } @@ -8043,7 +8013,7 @@ std::string std::string initializationList = generateRAIIHandleConstructorInitializationList( handle, constructorIt, handle.second.destructorIt, false ); assert( !initializationList.empty() ); std::string failureCheck = generateFailureCheck( constructorIt->second.successCodes ); - replaceAll( failureCheck, "result", "m_constructorSuccessCode" ); + failureCheck = std::regex_replace( failureCheck, std::regex( "result" ), "m_constructorSuccessCode" ); const std::string singularConstructorTemplate = R"( @@ -8965,6 +8935,20 @@ std::string VulkanHppGenerator::generateSizeCheck( std::vectorsecond.requireData, extIt.second->first ); + } + return staticAssertions; +} + std::string VulkanHppGenerator::generateStaticAssertions( std::vector const & requireData, std::string const & title ) const { std::string str; @@ -13248,6 +13232,21 @@ void checkForWarning( bool condition, int line, std::string const & message ) } } +template +std::vector findAll( InputIt first, InputIt last, UnaryPredicate p ) +{ + std::vector result; + while ( first != last ) + { + if ( p( *first ) ) + { + result.push_back( first ); + } + ++first; + } + return result; +} + std::string findTag( std::set const & tags, std::string const & name, std::string const & postfix ) { auto tagIt = std::find_if( tags.begin(), tags.end(), [&name, &postfix]( std::string const & t ) { return name.ends_with( t + postfix ); } ); @@ -13441,16 +13440,6 @@ std::string readSnippet( std::string const & snippetFile ) return oss.str(); } -void replaceAll( std::string & str, std::string const & from, std::string const & to ) -{ - size_t pos = 0; - while ( ( pos = str.find( from, pos ) ) != std::string::npos ) - { - str.replace( pos, from.length(), to ); - pos += to.length(); // Handles case where 'to' is a substring of 'from' - } -} - std::string replaceWithMap( std::string const & input, std::map replacements ) { // This will match ${someVariable} and contain someVariable in match group 1 @@ -13504,12 +13493,14 @@ std::string replaceWithMap( std::string const & input, std::map( tolower( input[0] ) ) + input.substr( 1 ); + assert( !input.empty() ); + return static_cast( tolower( input[0] ) ) + input.substr( 1 ); } std::string startUpperCase( std::string const & input ) { - return input.empty() ? "" : static_cast( toupper( input[0] ) ) + input.substr( 1 ); + assert( !input.empty() ); + return static_cast( toupper( input[0] ) ) + input.substr( 1 ); } std::string stripPostfix( std::string const & value, std::string const & postfix ) @@ -13617,23 +13608,6 @@ std::vector tokenize( std::string const & tokenString, std::string return tokens; } -template -std::string toString( StringContainer const & strings ) -{ - std::string str; - bool encounteredMember = false; - for ( auto s : strings ) - { - if ( encounteredMember ) - { - str += ", "; - } - str += s; - encounteredMember = true; - } - return str; -} - std::string trim( std::string const & input ) { std::string result = input; diff --git a/VulkanHppGenerator.hpp b/VulkanHppGenerator.hpp index 2034d7f..42f6ef7 100644 --- a/VulkanHppGenerator.hpp +++ b/VulkanHppGenerator.hpp @@ -409,8 +409,7 @@ private: std::vector determineDataTypes( std::vector const & params, std::map const & vectorParams, std::vector const & returnParams, - std::set const & templatedParams, - bool raii ) const; + std::set const & templatedParams ) const; size_t determineDefaultStartIndex( std::vector const & params, std::set const & skippedParams ) const; bool determineEnumeration( std::map const & vectorParams, std::vector const & returnParams ) const; size_t determineInitialSkipCount( std::string const & command ) const; @@ -477,14 +476,15 @@ private: size_t paramIndex, bool nonConstPointerAsNullptr, std::set const & singularParams, - std::set const & templatedParams, - bool raiiHandleMemberFunction ) const; + std::set const & templatedParams ) const; std::string generateCallArgumentEnhancedConstPointer( ParamData const & param, size_t paramIndex, std::set const & singularParams, std::set const & templatedParams ) const; - std::string generateCallArgumentEnhancedNonConstPointer( - ParamData const & param, size_t paramIndex, bool nonConstPointerAsNullptr, std::set const & singularParams, bool raiiHandleMemberFunction ) const; + std::string generateCallArgumentEnhancedNonConstPointer( ParamData const & param, + size_t paramIndex, + bool nonConstPointerAsNullptr, + std::set const & singularParams ) const; std::string generateCallArgumentEnhancedValue( std::vector const & params, size_t paramIndex, std::set const & singularParams ) const; std::string generateCallSequence( std::string const & name, CommandData const & commandData, @@ -886,6 +886,7 @@ private: bool singular ) const; std::string generateSizeCheck( std::vector::const_iterator> const & arrayIts, std::string const & structName, bool mutualExclusiveLens ) const; + std::string generateStaticAssertions() const; std::string generateStaticAssertions( std::vector const & requireData, std::string const & title ) const; std::string generateStruct( std::pair const & structure, std::set & listedStructs ) const; std::string generateStructAssignmentOperators( std::pair const & structure ) const; diff --git a/vulkan/vulkan_raii.hpp b/vulkan/vulkan_raii.hpp index e39a043..2bf9aec 100644 --- a/vulkan/vulkan_raii.hpp +++ b/vulkan/vulkan_raii.hpp @@ -9794,7 +9794,7 @@ namespace VULKAN_HPP_NAMESPACE //=== VK_KHR_swapchain === - VULKAN_HPP_NODISCARD std::vector getImages() const; + VULKAN_HPP_NODISCARD std::vector getImages() const; VULKAN_HPP_NODISCARD std::pair acquireNextImage( uint64_t timeout, @@ -12390,13 +12390,13 @@ namespace VULKAN_HPP_NAMESPACE return VULKAN_HPP_RAII_NAMESPACE::SwapchainKHR( *this, createInfo, allocator ); } - VULKAN_HPP_NODISCARD VULKAN_HPP_INLINE std::vector SwapchainKHR::getImages() const + VULKAN_HPP_NODISCARD VULKAN_HPP_INLINE std::vector SwapchainKHR::getImages() const { VULKAN_HPP_ASSERT( getDispatcher()->vkGetSwapchainImagesKHR && "Function needs extension enabled!" ); - std::vector swapchainImages; - uint32_t swapchainImageCount; - VkResult result; + std::vector swapchainImages; + uint32_t swapchainImageCount; + VkResult result; do { result = getDispatcher()->vkGetSwapchainImagesKHR( @@ -12404,8 +12404,10 @@ namespace VULKAN_HPP_NAMESPACE if ( ( result == VK_SUCCESS ) && swapchainImageCount ) { swapchainImages.resize( swapchainImageCount ); - result = getDispatcher()->vkGetSwapchainImagesKHR( - static_cast( m_device ), static_cast( m_swapchain ), &swapchainImageCount, swapchainImages.data() ); + result = getDispatcher()->vkGetSwapchainImagesKHR( static_cast( m_device ), + static_cast( m_swapchain ), + &swapchainImageCount, + reinterpret_cast( swapchainImages.data() ) ); } } while ( result == VK_INCOMPLETE ); resultCheck( static_cast( result ), VULKAN_HPP_NAMESPACE_STRING "::SwapchainKHR::getImages" );