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,8 +57,7 @@ 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 );
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 );
@ -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,20 +804,10 @@ 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;
}
}
else
{
// it's a plain parameter -> just use its name
@ -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,7 +1578,36 @@ ${commandEnhancedWithAllocators}
{ "newlineOnDefinition", definition ? "\n" : "" } } ) );
}
void VulkanHppGenerator::appendCommandGetChain( std::string & str,
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,
@ -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;
}
if ( withAllocator )
{
@ -3635,7 +3645,32 @@ 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() )
{
if ( params[i].len.empty() )
{
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
{
assert( params[i].arraySizes.empty() );
if ( params[i].len == "null-terminated" )
@ -3658,6 +3693,7 @@ std::string VulkanHppGenerator::constructArgumentListEnhanced( std::vector<Param
}
argumentList += startLowerCase( stripPrefix( params[i].name, "p" ) );
}
}
else if ( params[i].type.isNonConstPointer() )
{
assert( params[i].type.type ==
@ -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 },
@ -5489,9 +5550,7 @@ std::string VulkanHppGenerator::constructFunctionBodyEnhanced( std::string const
indentation,
commandData,
returnParamIndex,
vectorParamIndices,
enhancedReturnType,
isStructureChain,
withAllocator );
}

View File

@ -323,7 +323,12 @@ private:
CommandData const & commandData,
std::pair<size_t, size_t> const & vectorParamIndex,
bool definition ) const;
void appendCommandGetChain( std::string & str,
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,
@ -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,

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,