// Copyright(c) 2015-2019, NVIDIA CORPORATION. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include #include #include class VulkanHppGenerator { public: VulkanHppGenerator(); void checkCorrectness(); std::string const& getTypesafeCheck() const; std::string const& getVersion() const; std::string const& getVulkanLicenseHeader() const; void readCommands(tinyxml2::XMLElement const* element); void readComment(tinyxml2::XMLElement const* element); void readEnums(tinyxml2::XMLElement const* element); void readExtensions(tinyxml2::XMLElement const* element); void readFeature(tinyxml2::XMLElement const* element); void readPlatforms(tinyxml2::XMLElement const* element); void readTags(tinyxml2::XMLElement const* element); void readTypes(tinyxml2::XMLElement const* element); void writeBaseTypes(std::ostream & os) const; void writeBitmasks(std::ostream & os) const; void writeDispatchLoaderDynamic(std::ostream &os); // use vkGet*ProcAddress to get function pointers void writeDispatchLoaderStatic(std::ostream &os); // use exported symbols from loader void writeDispatchLoaderDefault(std::ostream &os); // typedef to DispatchLoaderStatic or undefined type, based on VK_NO_PROTOTYPES void writeEnums(std::ostream & os) const; void writeForwardDeclarations(std::ostream & os) const; void writeHandles(std::ostream & os) const; void writeHandlesCommandDefintions(std::ostream & os) const; void writeResultExceptions(std::ostream & os) const; void writeStructs(std::ostream & os) const; void writeStructureChainValidation(std::ostream & os); void writeThrowExceptions(std::ostream& os) const; private: struct BitmaskData { BitmaskData(std::string const& r) : requirement(r) {} std::string requirement; // original vulkan name: VK*FlagBits std::string platform; std::string alias; // original vulkan name }; struct TypeData { std::string compose() const; bool operator==(TypeData const& rhs) const { return (prefix == rhs.prefix) && (type == rhs.type) && (postfix == rhs.postfix); } std::string prefix; std::string type; std::string postfix; }; struct ParamData { ParamData() : optional(false) {} TypeData type; std::string name; std::string arraySize; std::string len; bool optional; }; struct CommandData { CommandData() : isAlias(false) {} std::vector params; std::string platform; std::string returnType; std::vector successCodes; bool isAlias; }; struct EnumValueData { EnumValueData(std::string const& vulkan, std::string const& vk, bool singleBit_) : vulkanValue(vulkan) , vkValue(vk) , singleBit(singleBit_) {} std::string vulkanValue; std::string vkValue; bool singleBit; }; struct EnumData { void addEnumValue(std::string const& valueName, bool bitmask, bool bitpos, std::string const& prefix, std::string const& postfix, std::string const& tag); std::vector> aliases; // pairs of vulkan enum value and corresponding vk::-namespace enum value std::string platform; std::vector values; }; struct HandleData { std::string alias; std::map commands; std::string deleteCommand; std::string deletePool; std::set childrenHandles; }; struct MemberData { TypeData type; std::string name; std::string arraySize; std::string values; }; struct StructureData { StructureData() : returnedOnly(false) {} bool returnedOnly; bool isUnion; std::vector members; std::string platform; std::vector structExtends; std::vector aliases; std::string subStruct; }; private: bool containsUnion(std::string const& type) const; std::string defaultValue(std::string const& type) const; std::string determineEnhancedReturnType(CommandData const& commandData, size_t returnParamIndex, std::map const& vectorParamIndices, bool twoStep, bool isStructureChain) const; size_t determineReturnParamIndex(CommandData const& commandData, std::map const& vectorParamIndices, bool twoStep) const; std::string determineSubStruct(std::pair const& structure) const; size_t determineTemplateParamIndex(std::vector const& params, std::map const& vectorParamIndices) const; std::map determineVectorParamIndices(std::vector const& params) const; std::string generateCall(std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool twoStep, bool firstCall, bool singular) const; bool isTwoStepAlgorithm(std::vector const& params) const; void linkCommandToHandle(std::string const& name, CommandData const& commandData); void readBaseType(tinyxml2::XMLElement const* element, std::map const& attributes); void readBitmask(tinyxml2::XMLElement const* element, std::map const& attributes); void readBitmaskAlias(int lineNum, std::string const& alias, std::map const& attributes, std::vector const& children); void readCommand(tinyxml2::XMLElement const* element); void readCommandAlias(int lineNum, std::string const& alias, std::map const& attributes, std::vector const& children); ParamData readCommandParam(tinyxml2::XMLElement const* element); std::string readCommandProto(tinyxml2::XMLElement const* element, std::string & returnType); std::vector readCommandSuccessCodes(std::map const& attributes); void readDefine(tinyxml2::XMLElement const* element, std::map const& attributes); void readEnum(tinyxml2::XMLElement const* element, EnumData & enumData, bool bitmask, std::string const& prefix, std::string const& postfix); void readExtension(tinyxml2::XMLElement const* element); void readExtensionDisabled(std::vector const& children); void readExtensionDisabledRequire(tinyxml2::XMLElement const* element); void readExtensionRequire(tinyxml2::XMLElement const* element, std::string const& platform, std::string const& tag); void readExtensionRequireCommand(tinyxml2::XMLElement const* element, std::string const& platform); void readExtensionRequireType(tinyxml2::XMLElement const* element, std::string const& platform); void readFeatureRequire(tinyxml2::XMLElement const* element); void readFuncpointer(tinyxml2::XMLElement const* element, std::map const& attributes); void readHandle(tinyxml2::XMLElement const* element, std::map const& attributes); void readPlatform(tinyxml2::XMLElement const* element); void readRequireEnum(tinyxml2::XMLElement const* element, std::string const& tag); void readStruct(tinyxml2::XMLElement const* element, bool isUnion, std::map const& attributes); void readStructAlias(int lineNum, std::string const& name, std::string const& alias, std::map const& attributes); MemberData readStructMember(tinyxml2::XMLElement const* element); std::vector readStructMembers(std::vector const& children); void readTag(tinyxml2::XMLElement const* element); void readType(tinyxml2::XMLElement const* element); void registerDeleter(std::string const& name, std::pair const& commandData); void unlinkCommandFromHandle(std::string const& name); void writeArgumentPlainType(std::ostream & os, ParamData const& paramData) const; void writeArguments(std::ostream & os, CommandData const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool twoStep, bool firstCall, bool singular, size_t from, size_t to) const; void writeArgumentVector(std::ostream & os, size_t paramIndex, ParamData const& paramData, size_t returnParamIndex, size_t templateParamIndex, bool twoStep, bool firstCall, bool singular) const; void writeArgumentVulkanType(std::ostream & os, ParamData const& paramData) const; void writeBitmask(std::ostream & os, std::string const& bitmaskName, std::string const& bitmaskAlias, std::string const& enumName, std::vector const& enumValues) const; void writeBitmaskToStringFunction(std::ostream & os, std::string const& flagsName, std::string const& enumName, std::vector const& enumValues) const; void writeCommand(std::ostream &os, std::string const& indentation, std::string const& name, std::pair const& commandData, bool definition) const; void writeEnum(std::ostream & os, std::pair const& enumData) const; void writeEnumToString(std::ostream & os, std::pair const& enumData) const; void writeFunction(std::ostream & os, std::string const& indentation, std::string const& name, std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool definition, bool enhanced, bool singular, bool unique, bool isStructureChain, bool withAllocator) const; void writeFunctionBodyEnhanced(std::ostream & os, std::string const& indentation, std::string const& commandName, std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool singular, bool unique, bool isStructureChain, bool withAllocator) const; std::string writeFunctionBodyEnhancedLocalReturnVariable(std::ostream & os, std::string const& indentation, CommandData const& commandData, size_t returnParamIndex, std::map const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool singular, bool isStructureChain, bool withAllocator) const; void writeFunctionBodyEnhancedLocalReturnVariableVectorSize(std::ostream & os, std::vector const& params, std::pair const& vectorParamIndex, size_t returnParamIndex, std::map const& vectorParamIndices, bool withAllocator) const; void writeFunctionBodyEnhancedMultiVectorSizeCheck(std::ostream & os, std::string const& indentation, std::string const& commandName, std::pair const& commandData, size_t returnParamIndex, std::map const& vectorParamIndices) const; void writeFunctionBodyEnhancedReturnResultValue(std::ostream & os, std::string const& indentation, std::string const& returnName, std::string const& commandName, std::pair const& commandData, size_t returnParamIndex, bool twoStep, bool singular, bool unique) const; void writeFunctionBodyEnhancedSingleStep(std::ostream & os, std::string const& indentation, std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool singular) const; void writeFunctionBodyEnhancedTwoStep(std::ostream & os, std::string const& indentation, std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool singular, std::string const& returnName) const; void writeFunctionBodyEnhancedVectorOfUniqueHandles(std::ostream & os, std::string const& indentation, std::string const& commandName, std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool twoStep, bool singular, bool withAllocator) const; void writeFunctionBodyStandard(std::ostream & os, std::string const& indentation, std::pair const& commandData) const; std::string writeFunctionBodyStandardArgument(TypeData const& typeData, std::string const& name) const; bool writeFunctionHeaderArgumentEnhanced(std::ostream & os, ParamData const& param, size_t paramIndex, std::map const& vectorParamIndices, bool skip, bool argEncountered, bool isTemplateParam, bool isLastArgument, bool singular, bool withDefaults, bool withAllocator) const; void writeFunctionHeaderArgumentEnhancedPointer(std::ostream & os, ParamData const& param, std::string const& strippedParameterName, bool withDefaults, bool withAllocator) const; void writeFunctionHeaderArgumentEnhancedSimple(std::ostream & os, ParamData const& param, bool lastArgument, bool withDefaults, bool withAllocator) const; void writeFunctionHeaderArgumentEnhancedVector(std::ostream & os, ParamData const& param, std::string const& strippedParameterName, bool hasSizeParam, bool isTemplateParam, bool singular, bool withDefaults, bool withAllocator) const; void writeFunctionHeaderArguments(std::ostream & os, std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool enhanced, bool singular, bool withDefaults, bool withAllocator) const; void writeFunctionHeaderArgumentsEnhanced(std::ostream & os, std::pair const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map const& vectorParamIndices, bool singular, bool withDefaults, bool withAllocator) const; void writeFunctionHeaderArgumentsStandard(std::ostream & os, std::pair const& commandData, bool withDefaults) const; bool writeFunctionHeaderArgumentStandard(std::ostream & os, ParamData const& param, bool argEncountered, bool isLastArgument, bool withDefaults) const; void writeFunctionHeaderReturnType(std::ostream & os, CommandData const& commandData, size_t returnParamIndex, std::map const& vectorParamIndices, std::string const& enhancedReturnType, bool enhanced, bool singular, bool unique, bool isStructureChain) const; void writeFunctionHeaderTemplate(std::ostream & os, std::string const& indentation, size_t returnParamIndex, size_t templateParamIndex, std::string const& enhancedReturnType, bool enhanced, bool singular, bool unique, bool withDefault, bool isStructureChain) const; void writeHandle(std::ostream & os, std::pair const& handle, std::set & listedHandles) const; void writePlatformEnter(std::ostream & os, std::string const& platform) const; void writePlatformLeave(std::ostream & os, std::string const& platform) const; void writeStruct(std::ostream & os, std::pair const& structure, std::set & listedStructures) const; void writeStructCompareOperators(std::ostream & os, std::pair const& structure) const; void writeStructConstructor(std::ostream & os, std::pair const& structData) const; std::string writeStructConstructorArgument(bool & listedArgument, std::string const& indentation, MemberData const& memberData) const; void writeStructMembers(std::ostream & os, StructureData const& structData) const; void writeStructSetter(std::ostream & os, std::string const& structureName, MemberData const& memberData) const; void writeStructure(std::ostream & os, std::pair const& structure) const; void writeUnion(std::ostream & os, std::pair const& structure) const; void writeUniqueTypes(std::ostream &os, std::string const& parentType, std::set const& childrenTypes) const; #if !defined(NDEBUG) void readRequires(tinyxml2::XMLElement const* element, std::map const& attributes); #endif private: std::map m_baseTypes; std::map m_bitmaskBits; std::map m_bitmasks; std::map m_commandToHandle; std::map m_enums; std::set m_extendedStructs; // structs which are referenced by the structextends tag std::map m_handles; std::map m_platforms; std::map m_structureAliases; std::map m_structures; std::set m_tags; std::string m_typesafeCheck; std::string m_version; std::string m_vulkanLicenseHeader; #if !defined(NDEBUG) std::set m_defaultZeroTypes; std::set m_defines; // just used for verfication in readExtensionType #endif }; const size_t INVALID_INDEX = (size_t)~0;