Refactor commands returning a StructureChain and a Result.

This commit is contained in:
asuessenbach 2020-10-15 09:01:04 +02:00
parent 5ecb57bdc5
commit 74498b2b9e
3 changed files with 187 additions and 113 deletions

View File

@ -57,9 +57,8 @@ std::string createEnumValueName( std::string const & name,
std::string createSuccessCode( std::string const & code, std::set<std::string> const & tags );
std::string determineCommandName( std::string const & vulkanCommandName, std::string const & firstArgumentType );
std::set<size_t> determineSkippedParams( size_t returnParamIndex, std::map<size_t, size_t> const & vectorParamIndices );
bool determineStructureChaining( std::string const & structType,
std::set<std::string> const & extendedStructs );
std::string extractTag( int line, std::string const & name, std::set<std::string> const & tags );
bool determineStructureChaining( std::string const & structType, std::set<std::string> const & extendedStructs );
std::string extractTag( int line, std::string const & name, std::set<std::string> const & tags );
std::string findTag( std::set<std::string> const & tags, std::string const & name, std::string const & postfix = "" );
std::pair<bool, std::string> generateFunctionBodyStandardReturn( std::string const & returnType );
std::map<std::string, std::string> getAttributes( tinyxml2::XMLElement const * element );
@ -392,8 +391,7 @@ std::set<size_t> determineSkippedParams( size_t returnParamIndex, std::map<size_
return skippedParams;
}
bool determineStructureChaining( std::string const & structType,
std::set<std::string> const & extendedStructs )
bool determineStructureChaining( std::string const & structType, std::set<std::string> const & extendedStructs )
{
return ( extendedStructs.find( structType ) != extendedStructs.end() );
}
@ -806,19 +804,9 @@ void VulkanHppGenerator::appendArgumentPlainType( std::string & str, ParamData c
assert( paramData.type.postfix.back() == '*' );
// it's a pointer
std::string parameterName = startLowerCase( stripPrefix( paramData.name, "p" ) );
if ( paramData.type.prefix.find( "const" ) != std::string::npos )
{
assert( paramData.type.type != "char" );
// it's const pointer -> just use the name
assert( !paramData.optional );
str += paramData.name;
}
else
{
// it's a non-const pointer, and char is the only type that occurs -> use the address of the parameter
assert( paramData.type.type.find( "char" ) == std::string::npos );
str += "&" + parameterName;
}
// it's a non-const pointer, and char is the only type that occurs -> use the address of the parameter
assert( paramData.type.type.find( "char" ) == std::string::npos );
str += "&" + parameterName;
}
else
{
@ -1241,11 +1229,16 @@ void VulkanHppGenerator::appendCommand( std::string & str,
// the return parameter is not a vector
if ( isStructureChainAnchor( commandData.params[nonConstPointerParamIndices[0]].type.type ) )
{
if ( commandData.returnType == "void" )
if ( commandData.returnType == "VkResult" )
{
appendCommandGetChain( str, name, commandData, nonConstPointerParamIndices[0], definition );
appendedFunction = true;
appendCommandGetChainResult( str, name, commandData, nonConstPointerParamIndices[0], definition );
}
else
{
assert( commandData.returnType == "void" );
appendCommandGetChainVoid( str, name, commandData, nonConstPointerParamIndices[0], definition );
}
appendedFunction = true;
}
else
{
@ -1307,8 +1300,8 @@ void VulkanHppGenerator::appendCommand( std::string & str,
assert( ( vectorParamIndexIt->second == *nonConstPointerParamIndices.begin() ) &&
( vectorParamIndexIt->first == *std::next( nonConstPointerParamIndices.begin() ) ) );
if ( ( commandData.returnType == "void" ) &&
!determineStructureChaining(
commandData.params[vectorParamIndexIt->first].type.type, m_extendedStructs ) )
!determineStructureChaining( commandData.params[vectorParamIndexIt->first].type.type,
m_extendedStructs ) )
{
assert( commandData.params[vectorParamIndexIt->first].type.type != "void" );
appendCommandEnumerateVoid( str, name, commandData, *vectorParamIndexIt, definition );
@ -1585,11 +1578,40 @@ ${commandEnhancedWithAllocators}
{ "newlineOnDefinition", definition ? "\n" : "" } } ) );
}
void VulkanHppGenerator::appendCommandGetChain( std::string & str,
std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const
void VulkanHppGenerator::appendCommandGetChainResult( std::string & str,
std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const
{
std::string const functionTemplate = R"(
${enter}${commandStandard}${newlineOnDefinition}
#ifndef VULKAN_HPP_DISABLE_ENHANCED_MODE
${commandEnhanced}${newlineOnDefinition}
${commandEnhancedChained}
#endif /*VULKAN_HPP_DISABLE_ENHANCED_MODE*/
${leave})";
std::string enter, leave;
std::tie( enter, leave ) = generateProtection( commandData.feature, commandData.extensions );
str += replaceWithMap(
functionTemplate,
std::map<std::string, std::string>(
{ { "commandEnhanced", constructCommandResultGetValue( name, commandData, nonConstPointerIndex, definition ) },
{ "commandEnhancedChained",
constructCommandResultGetChain( name, commandData, nonConstPointerIndex, definition ) },
{ "commandStandard", constructCommandStandard( name, commandData, definition ) },
{ "enter", enter },
{ "leave", leave },
{ "newlineOnDefinition", definition ? "\n" : "" } } ) );
}
void VulkanHppGenerator::appendCommandGetChainVoid( std::string & str,
std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const
{
std::string const functionTemplate = R"(
${enter}${commandStandard}${newlineOnDefinition}
@ -2597,9 +2619,7 @@ std::string VulkanHppGenerator::appendFunctionBodyEnhancedLocalReturnVariable(
std::string const & indentation,
CommandData const & commandData,
size_t returnParamIndex,
std::map<size_t, size_t> const & vectorParamIndices,
std::string const & enhancedReturnType,
bool isStructureChain,
bool withAllocator ) const
{
std::string pureReturnType = stripPrefix( commandData.params[returnParamIndex].type.type, "Vk" );
@ -2610,17 +2630,7 @@ std::string VulkanHppGenerator::appendFunctionBodyEnhancedLocalReturnVariable(
// the returned parameter is somehow enhanced by us
str += indentation + " ";
// in non-singular case, use the enhanced type for the return variable (like vector<...>)
if ( isStructureChain && vectorParamIndices.empty() )
{
// For StructureChains use the template parameters
str += "StructureChain<X, Y, Z...> structureChain;\n" + indentation + " " + enhancedReturnType + "& " +
returnName + " = structureChain.template get<" + enhancedReturnType + ">()";
returnName = "structureChain";
}
else
{
str += enhancedReturnType + " " + returnName;
}
str += enhancedReturnType + " " + returnName;
if ( withAllocator )
{
@ -2916,7 +2926,7 @@ void VulkanHppGenerator::appendFunctionHeaderArgumentEnhancedPointer( std::strin
// for non-char-pointer, change to reference
assert( param.type.postfix == "*" );
str += param.type.prefix + ( param.type.prefix.empty() ? "" : " " ) + stripPrefix( param.type.type, "Vk" ) + " & " +
strippedParameterName;
strippedParameterName;
}
void VulkanHppGenerator::appendFunctionHeaderArgumentEnhancedVector( std::string & str,
@ -3635,28 +3645,54 @@ std::string VulkanHppGenerator::constructArgumentListEnhanced( std::vector<Param
argumentList += "const " + stripPrefix( params[i].type.type, "Vk" ) + " & " +
stripPluralS( startLowerCase( stripPrefix( params[i].name, "p" ) ) );
}
else if ( params[i].type.isConstPointer() && !params[i].len.empty() )
else if ( params[i].type.isConstPointer() )
{
assert( params[i].arraySizes.empty() );
if ( params[i].len == "null-terminated" )
if ( params[i].len.empty() )
{
assert( params[i].type.type == "char" );
argumentList += "const std::string & ";
assert( !params[i].type.prefix.empty() && ( params[i].type.postfix == "*" ) );
assert( params[i].arraySizes.empty() );
if ( params[i].type.type == "void" )
{
argumentList += params[i].type.compose() + " " + params[i].name;
}
else if ( params[i].optional )
{
argumentList +=
"Optional<const " + stripPrefix( params[i].type.type, "Vk" ) + "> " +
startLowerCase(
stripPrefix( params[i].name, "p" ) +
( ( definition || withAllocators ) ? "" : " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT" ) );
hasDefaultAssignment = true;
}
else
{
argumentList += params[i].type.prefix + " " + stripPrefix( params[i].type.type, "Vk" ) + " & " +
startLowerCase( stripPrefix( params[i].name, "p" ) );
}
}
else
{
std::string type = params[i].type.compose();
assert( endsWith( type, "*" ) );
type.pop_back();
size_t pos = type.find( "void" );
if ( pos != std::string::npos )
assert( params[i].arraySizes.empty() );
if ( params[i].len == "null-terminated" )
{
type.replace( pos, 4, "T" );
assert( params[i].type.type == "char" );
argumentList += "const std::string & ";
}
else
{
std::string type = params[i].type.compose();
assert( endsWith( type, "*" ) );
type.pop_back();
size_t pos = type.find( "void" );
if ( pos != std::string::npos )
{
type.replace( pos, 4, "T" );
}
argumentList += "ArrayProxy<" + type + "> const & ";
argumentList += "ArrayProxy<" + type + "> const & ";
}
argumentList += startLowerCase( stripPrefix( params[i].name, "p" ) );
}
argumentList += startLowerCase( stripPrefix( params[i].name, "p" ) );
}
else if ( params[i].type.isNonConstPointer() )
{
@ -3667,30 +3703,8 @@ std::string VulkanHppGenerator::constructArgumentListEnhanced( std::vector<Param
}
else if ( beginsWith( params[i].type.type, "Vk" ) )
{
if ( params[i].type.isConstPointer() )
{
assert( params[i].arraySizes.empty() );
if ( params[i].optional )
{
assert( params[i].type.isConstPointer() );
argumentList +=
"Optional<const " + stripPrefix( params[i].type.type, "Vk" ) + "> " +
startLowerCase(
stripPrefix( params[i].name, "p" ) +
( ( definition || withAllocators ) ? "" : " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT" ) );
hasDefaultAssignment = true;
}
else
{
argumentList += "const " + stripPrefix( params[i].type.type, "Vk" ) + " & " +
startLowerCase( stripPrefix( params[i].name, "p" ) );
}
}
else
{
argumentList +=
params[i].type.compose() + " " + params[i].name + constructCArraySizes( params[i].arraySizes );
}
assert( !params[i].type.isConstPointer() );
argumentList += params[i].type.compose() + " " + params[i].name + constructCArraySizes( params[i].arraySizes );
}
else
{
@ -4269,6 +4283,64 @@ std::string VulkanHppGenerator::constructCommandResultEnumerateTwoVectorsDepreca
return str;
}
std::string VulkanHppGenerator::constructCommandResultGetChain( std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const
{
assert( !commandData.handle.empty() && ( commandData.returnType == "VkResult" ) &&
( commandData.successCodes.size() == 1 ) && !commandData.errorCodes.empty() );
std::string str;
std::set<size_t> skippedParams{ 0, nonConstPointerIndex };
std::string argumentList =
constructArgumentListEnhanced( commandData.params, skippedParams, INVALID_INDEX, definition, false );
std::string commandName = determineCommandName( name, commandData.params[0].type.type );
std::string nodiscard = constructNoDiscardEnhanced( commandData );
assert( beginsWith( commandData.params[nonConstPointerIndex].type.type, "Vk" ) );
std::string returnType =
"VULKAN_HPP_NAMESPACE::" + stripPrefix( commandData.params[nonConstPointerIndex].type.type, "Vk" );
if ( definition )
{
std::string const functionTemplate =
R"( template <typename X, typename Y, typename... Z, typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE typename ResultValueType<StructureChain<X, Y, Z...>>::type ${className}::${commandName}( ${argumentList} ) const
{
StructureChain<X, Y, Z...> structureChain;
${returnType} & ${returnVariable} = structureChain.template get<${returnType}>();
Result result = static_cast<Result>( d.${vkCommand}( ${callArguments} ) );
return createResultValue( result, structureChain, VULKAN_HPP_NAMESPACE_STRING"::${className}::${commandName}" );
})";
str = replaceWithMap(
functionTemplate,
std::map<std::string, std::string>(
{ { "argumentList", argumentList },
{ "callArguments",
constructCallArgumentsGetValue( commandData.handle, commandData.params, nonConstPointerIndex ) },
{ "className", stripPrefix( commandData.handle, "Vk" ) },
{ "commandName", commandName },
{ "returnVariable", startLowerCase( stripPrefix( commandData.params[nonConstPointerIndex].name, "p" ) ) },
{ "returnType", returnType },
{ "vkCommand", name } } ) );
}
else
{
std::string const functionTemplate =
R"( template <typename X, typename Y, typename... Z, typename Dispatch = VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS typename ResultValueType<StructureChain<X, Y, Z...>>::type ${commandName}( ${argumentList} ) const;)";
str = replaceWithMap(
functionTemplate,
std::map<std::string, std::string>( { { "argumentList", argumentList }, { "commandName", commandName } } ) );
}
return str;
}
std::string VulkanHppGenerator::constructCommandResultGetHandleUnique( std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
@ -5119,12 +5191,7 @@ std::string VulkanHppGenerator::constructCommandVoidGetChain( std::string const
std::string str;
std::set<size_t> skippedParams{ nonConstPointerIndex };
if ( !commandData.handle.empty() )
{
assert( commandData.params[0].type.type == commandData.handle );
skippedParams.insert( 0 );
}
std::set<size_t> skippedParams{ 0, nonConstPointerIndex };
std::string argumentList =
constructArgumentListEnhanced( commandData.params, skippedParams, INVALID_INDEX, definition, false );
@ -5138,7 +5205,7 @@ std::string VulkanHppGenerator::constructCommandVoidGetChain( std::string const
{
std::string const functionTemplate =
R"( template <typename X, typename Y, typename... Z, typename Dispatch>
VULKAN_HPP_NODISCARD VULKAN_HPP_INLINE StructureChain<X, Y, Z...> ${className}${commandName}( ${argumentList} ) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD VULKAN_HPP_INLINE StructureChain<X, Y, Z...> ${className}::${commandName}( ${argumentList} ) const VULKAN_HPP_NOEXCEPT
{
StructureChain<X, Y, Z...> structureChain;
${returnType} & ${returnVariable} = structureChain.template get<${returnType}>();
@ -5146,19 +5213,13 @@ std::string VulkanHppGenerator::constructCommandVoidGetChain( std::string const
return structureChain;
})";
std::string className = stripPrefix( commandData.handle, "Vk" );
if ( !className.empty() )
{
className += "::";
}
str = replaceWithMap(
functionTemplate,
std::map<std::string, std::string>(
{ { "argumentList", argumentList },
{ "callArguments",
constructCallArgumentsGetValue( commandData.handle, commandData.params, nonConstPointerIndex ) },
{ "className", className },
{ "className", stripPrefix( commandData.handle, "Vk" ) },
{ "commandName", commandName },
{ "returnVariable", startLowerCase( stripPrefix( commandData.params[nonConstPointerIndex].name, "p" ) ) },
{ "returnType", returnType },
@ -5263,8 +5324,8 @@ std::string VulkanHppGenerator::constructCommandVoid( std::string const &
std::string commandName = determineCommandName( name, commandData.params[0].type.type );
std::string typenameT = ( ( vectorParamIndices.size() == 1 ) &&
( commandData.params[vectorParamIndices.begin()->first].type.type == "void" ) )
? "typename T, "
: "";
? "typename T, "
: "";
std::pair<bool, std::map<size_t, std::vector<size_t>>> vectorSizeCheck = needsVectorSizeCheck( vectorParamIndices );
std::string noexceptString = vectorSizeCheck.first ? "VULKAN_HPP_NOEXCEPT_WHEN_NO_EXCEPTIONS" : "VULKAN_HPP_NOEXCEPT";
@ -5489,9 +5550,7 @@ std::string VulkanHppGenerator::constructFunctionBodyEnhanced( std::string const
indentation,
commandData,
returnParamIndex,
vectorParamIndices,
enhancedReturnType,
isStructureChain,
withAllocator );
}

View File

@ -323,11 +323,16 @@ private:
CommandData const & commandData,
std::pair<size_t, size_t> const & vectorParamIndex,
bool definition ) const;
void appendCommandGetChain( std::string & str,
std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const;
void appendCommandGetChainResult( std::string & str,
std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const;
void appendCommandGetChainVoid( std::string & str,
std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const;
void appendCommandGetHandle( std::string & str,
std::string const & name,
CommandData const & commandData,
@ -414,9 +419,7 @@ private:
std::string const & indentation,
CommandData const & commandData,
size_t returnParamIndex,
std::map<size_t, size_t> const & vectorParamIndices,
std::string const & enhancedReturnType,
bool isStructureChain,
bool withAllocator ) const;
void appendFunctionBodyEnhancedMultiVectorSizeCheck( std::string & str,
std::string const & indentation,
@ -562,6 +565,10 @@ private:
std::map<size_t, size_t> const & vectorParamIndices,
bool definition,
bool withAllocators ) const;
std::string constructCommandResultGetChain( std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
bool definition ) const;
std::string constructCommandResultGetHandleUnique( std::string const & name,
CommandData const & commandData,
size_t nonConstPointerIndex,
@ -614,15 +621,15 @@ private:
bool definition ) const;
std::string
constructCommandStandard( std::string const & name, CommandData const & commandData, bool definition ) const;
std::string constructCommandType( std::string const & name,
CommandData const & commandData,
bool definition,
std::map<size_t, size_t> const & vectorParamIndices ) const;
std::string constructCommandVoid( std::string const & name,
CommandData const & commandData,
bool definition,
std::map<size_t, size_t> const & vectorParamIndices ) const;
std::string constructCommandVoidEnumerate( std::string const & name,
std::string constructCommandType( std::string const & name,
CommandData const & commandData,
bool definition,
std::map<size_t, size_t> const & vectorParamIndices ) const;
std::string constructCommandVoid( std::string const & name,
CommandData const & commandData,
bool definition,
std::map<size_t, size_t> const & vectorParamIndices ) const;
std::string constructCommandVoidEnumerate( std::string const & name,
CommandData const & commandData,
std::pair<size_t, size_t> const & vectorParamIndex,
bool definition,

View File

@ -97422,6 +97422,7 @@ namespace VULKAN_HPP_NAMESPACE
return static_cast<Result>( d.vkGetAndroidHardwareBufferPropertiesANDROID(
m_device, buffer, reinterpret_cast<VkAndroidHardwareBufferPropertiesANDROID *>( pProperties ) ) );
}
# ifndef VULKAN_HPP_DISABLE_ENHANCED_MODE
template <typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE
@ -97434,6 +97435,7 @@ namespace VULKAN_HPP_NAMESPACE
return createResultValue(
result, properties, VULKAN_HPP_NAMESPACE_STRING "::Device::getAndroidHardwareBufferPropertiesANDROID" );
}
template <typename X, typename Y, typename... Z, typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE typename ResultValueType<StructureChain<X, Y, Z...>>::type
Device::getAndroidHardwareBufferPropertiesANDROID( const struct AHardwareBuffer & buffer, Dispatch const & d ) const
@ -103899,6 +103901,7 @@ namespace VULKAN_HPP_NAMESPACE
reinterpret_cast<const VkPhysicalDeviceImageFormatInfo2 *>( pImageFormatInfo ),
reinterpret_cast<VkImageFormatProperties2 *>( pImageFormatProperties ) ) );
}
#ifndef VULKAN_HPP_DISABLE_ENHANCED_MODE
template <typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE
@ -103914,6 +103917,7 @@ namespace VULKAN_HPP_NAMESPACE
return createResultValue(
result, imageFormatProperties, VULKAN_HPP_NAMESPACE_STRING "::PhysicalDevice::getImageFormatProperties2" );
}
template <typename X, typename Y, typename... Z, typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE typename ResultValueType<StructureChain<X, Y, Z...>>::type
PhysicalDevice::getImageFormatProperties2( const PhysicalDeviceImageFormatInfo2 & imageFormatInfo,
@ -103942,6 +103946,7 @@ namespace VULKAN_HPP_NAMESPACE
reinterpret_cast<const VkPhysicalDeviceImageFormatInfo2 *>( pImageFormatInfo ),
reinterpret_cast<VkImageFormatProperties2 *>( pImageFormatProperties ) ) );
}
#ifndef VULKAN_HPP_DISABLE_ENHANCED_MODE
template <typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE
@ -103957,6 +103962,7 @@ namespace VULKAN_HPP_NAMESPACE
return createResultValue(
result, imageFormatProperties, VULKAN_HPP_NAMESPACE_STRING "::PhysicalDevice::getImageFormatProperties2KHR" );
}
template <typename X, typename Y, typename... Z, typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE typename ResultValueType<StructureChain<X, Y, Z...>>::type
PhysicalDevice::getImageFormatProperties2KHR( const PhysicalDeviceImageFormatInfo2 & imageFormatInfo,
@ -104858,6 +104864,7 @@ namespace VULKAN_HPP_NAMESPACE
reinterpret_cast<const VkPhysicalDeviceSurfaceInfo2KHR *>( pSurfaceInfo ),
reinterpret_cast<VkSurfaceCapabilities2KHR *>( pSurfaceCapabilities ) ) );
}
#ifndef VULKAN_HPP_DISABLE_ENHANCED_MODE
template <typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE
@ -104873,6 +104880,7 @@ namespace VULKAN_HPP_NAMESPACE
return createResultValue(
result, surfaceCapabilities, VULKAN_HPP_NAMESPACE_STRING "::PhysicalDevice::getSurfaceCapabilities2KHR" );
}
template <typename X, typename Y, typename... Z, typename Dispatch>
VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS VULKAN_HPP_INLINE typename ResultValueType<StructureChain<X, Y, Z...>>::type
PhysicalDevice::getSurfaceCapabilities2KHR( const PhysicalDeviceSurfaceInfo2KHR & surfaceInfo,