Skip to content

Commit

Permalink
Split vulkan.hpp into multiple sub-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
asuessenbach committed Jun 8, 2021
1 parent 6bc8a07 commit 89a1946
Show file tree
Hide file tree
Showing 7 changed files with 115,792 additions and 114,669 deletions.
15 changes: 14 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,22 @@ if (NOT DEFINED VulkanHeaders_INCLUDE_DIR)
endif()
file(TO_NATIVE_PATH ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan.hpp vulkan_hpp)
string(REPLACE "\\" "\\\\" vulkan_hpp ${vulkan_hpp})
file(TO_NATIVE_PATH ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan_enums.hpp vulkan_enums_hpp)
string(REPLACE "\\" "\\\\" vulkan_enums_hpp ${vulkan_enums_hpp})
file(TO_NATIVE_PATH ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan_funcs.hpp vulkan_funcs_hpp)
string(REPLACE "\\" "\\\\" vulkan_funcs_hpp ${vulkan_funcs_hpp})
file(TO_NATIVE_PATH ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan_handles.hpp vulkan_handles_hpp)
string(REPLACE "\\" "\\\\" vulkan_handles_hpp ${vulkan_handles_hpp})
file(TO_NATIVE_PATH ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan_structs.hpp vulkan_structs_hpp)
string(REPLACE "\\" "\\\\" vulkan_structs_hpp ${vulkan_structs_hpp})
file(TO_NATIVE_PATH ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan_raii.hpp vulkan_raii_hpp)
string(REPLACE "\\" "\\\\" vulkan_raii_hpp ${vulkan_raii_hpp})
add_definitions(-DVULKAN_HPP_FILE="${vulkan_hpp}" -DVULKAN_RAII_HPP_FILE="${vulkan_raii_hpp}")
add_definitions(-DVULKAN_HPP_FILE="${vulkan_hpp}"
-DVULKAN_ENUMS_HPP_FILE="${vulkan_enums_hpp}"
-DVULKAN_FUNCS_HPP_FILE="${vulkan_funcs_hpp}"
-DVULKAN_HANDLES_HPP_FILE="${vulkan_handles_hpp}"
-DVULKAN_STRUCTS_HPP_FILE="${vulkan_structs_hpp}"
-DVULKAN_RAII_HPP_FILE="${vulkan_raii_hpp}")
include_directories(${VulkanHeaders_INCLUDE_DIR})

set(HEADERS
Expand Down
193 changes: 137 additions & 56 deletions VulkanHppGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ std::string trim( std::string const & input );
std::string trimEnd( std::string const & input );
std::string trimStars( std::string const & input );
void warn( bool condition, int line, std::string const & message );
void writeToFile( std::string const & str, std::string const & fileName );

const std::set<std::string> ignoreLens = { "null-terminated",
R"(latexmath:[\lceil{\mathit{rasterizationSamples} \over 32}\rceil])",
Expand Down Expand Up @@ -701,6 +702,24 @@ void warn( bool condition, int line, std::string const & message )
}
}

void writeToFile( std::string const & str, std::string const & fileName )
{
std::ofstream ofs( fileName );
assert( !ofs.fail() );
ofs << str;
ofs.close();

#if defined( CLANG_FORMAT_EXECUTABLE )
std::cout << "VulkanHppGenerator: Formatting " << fileName << " ..." << std::endl;
std::string commandString = "\"" CLANG_FORMAT_EXECUTABLE "\" -i --style=file " + fileName;
int ret = std::system( commandString.c_str() );
if ( ret != 0 )
{
std::cout << "VulkanHppGenerator: failed to format file " << fileName << " with error <" << ret << ">\n";
}
#endif
}

VulkanHppGenerator::VulkanHppGenerator( tinyxml2::XMLDocument const & document )
{
m_handles.insert( std::make_pair(
Expand Down Expand Up @@ -3131,7 +3150,7 @@ void VulkanHppGenerator::appendHandle( std::string & str, std::pair<std::string,
assert( commandIt != m_commands.end() );
for ( auto const & parameter : commandIt->second.params )
{
if ( handleData.first != parameter.type.type ) // the commands use this handleData type !
if ( isHandleType( parameter.type.type ) && ( handleData.first != parameter.type.type ) ) // the commands use this handleData type !
{
appendType( str, parameter.type.type );
}
Expand Down Expand Up @@ -3405,6 +3424,17 @@ void VulkanHppGenerator::appendHandle( std::string & str, std::pair<std::string,
void VulkanHppGenerator::appendHandles( std::string & str )
{
// Note reordering structs or handles by features and extensions is not possible!
// first, forward declare all the structs!
for ( auto const & structure : m_structures )
{
std::string enter, leave;
std::tie( enter, leave ) = generateProtection( structure.first, !structure.second.aliases.empty() );
str += enter + ( structure.second.isUnion ? " union " : " struct " ) + stripPrefix( structure.first, "Vk" ) + ";\n" + leave;
for ( auto const & alias : structure.second.aliases )
{
str += " using " + stripPrefix( alias, "Vk" ) + " = " + stripPrefix( structure.first, "Vk" ) + ";\n";
}
}
for ( auto const & handle : m_handles )
{
if ( m_listedTypes.find( handle.first ) == m_listedTypes.end() )
Expand Down Expand Up @@ -16261,17 +16291,6 @@ extern "C" __declspec( dllimport ) FARPROC __stdcall GetProcAddress( HINSTANCE h
# include <compare>
#endif

)";

static const std::string is_error_code_enum = R"(
#ifndef VULKAN_HPP_NO_EXCEPTIONS
namespace std
{
template <>
struct is_error_code_enum<VULKAN_HPP_NAMESPACE::Result> : public true_type
{};
}
#endif
)";

static const std::string structResultValue = R"(
Expand Down Expand Up @@ -16585,10 +16604,79 @@ namespace std
std::cout << "VulkanHppGenerator: Parsing " << filename << std::endl;
VulkanHppGenerator generator( doc );

std::cout << "VulkanHppGenerator: Generating " << VULKAN_HPP_FILE << std::endl;
std::string str;
static const size_t estimatedLength = 4 * 1024 * 1024;
str.reserve( estimatedLength );
std::cout << "VulkanHppGenerator: Generating" << VULKAN_ENUMS_HPP_FILE << " ..." << std::endl;
std::string str;
str = generator.getVulkanLicenseHeader();
str += +R"(
#ifndef VULKAN_ENUMS_HPP
# define VULKAN_ENUMS_HPP

namespace VULKAN_HPP_NAMESPACE
{
)";
str += typeTraits;
generator.appendEnums( str );
generator.appendIndexTypeTraits( str );
generator.appendBitmasks( str );
str += R"(
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
writeToFile( str, VULKAN_ENUMS_HPP_FILE );

std::cout << "VulkanHppGenerator: Generating " << VULKAN_HANDLES_HPP_FILE << " ..." << std::endl;
str.clear();
str = generator.getVulkanLicenseHeader();
str += +R"(
#ifndef VULKAN_HANDLES_HPP
# define VULKAN_HANDLES_HPP

namespace VULKAN_HPP_NAMESPACE
{
)";
generator.appendHandles( str );
str += R"(
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
writeToFile( str, VULKAN_HANDLES_HPP_FILE );

std::cout << "VulkanHppGenerator: Generating " << VULKAN_STRUCTS_HPP_FILE << " ..." << std::endl;
str.clear();
str = generator.getVulkanLicenseHeader();
str += +R"(
#ifndef VULKAN_STRUCTS_HPP
# define VULKAN_STRUCTS_HPP

namespace VULKAN_HPP_NAMESPACE
{
)";
generator.appendStructs( str );
str += R"(
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
writeToFile( str, VULKAN_STRUCTS_HPP_FILE );

std::cout << "VulkanHppGenerator: Generating " << VULKAN_FUNCS_HPP_FILE << " ..." << std::endl;
str.clear();
str = generator.getVulkanLicenseHeader();
str += +R"(
#ifndef VULKAN_FUNCS_HPP
# define VULKAN_FUNCS_HPP

namespace VULKAN_HPP_NAMESPACE
{
)";
generator.appendHandlesCommandDefinitions( str );
str += R"(
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
writeToFile( str, VULKAN_FUNCS_HPP_FILE );

std::cout << "VulkanHppGenerator: Generating " << VULKAN_HPP_FILE << " ..." << std::endl;
str.clear();
str += generator.getVulkanLicenseHeader() + includes + "\n";
str += "static_assert( VK_HEADER_VERSION == " + generator.getVersion() +
" , \"Wrong VK_HEADER_VERSION!\" );\n"
Expand All @@ -16608,18 +16696,38 @@ namespace std
generator.appendDispatchLoaderDefault( str );
str += classObjectDestroy + classObjectFree + classObjectRelease + classPoolFree + "\n";
generator.appendBaseTypes( str );
str += typeTraits;
generator.appendEnums( str );
generator.appendIndexTypeTraits( str );
generator.appendBitmasks( str );
str += "} // namespace VULKAN_HPP_NAMESPACE\n" + is_error_code_enum + "\n" + "namespace VULKAN_HPP_NAMESPACE\n" +
"{\n" + "#ifndef VULKAN_HPP_NO_EXCEPTIONS" + exceptions;
str += R"(} // namespace VULKAN_HPP_NAMESPACE

#include <vulkan/vulkan_enums.hpp>

#ifndef VULKAN_HPP_NO_EXCEPTIONS
namespace std
{
template <>
struct is_error_code_enum<VULKAN_HPP_NAMESPACE::Result> : public true_type
{};
}
#endif

namespace VULKAN_HPP_NAMESPACE
{
#ifndef VULKAN_HPP_NO_EXCEPTIONS
)";
str += exceptions;
generator.appendResultExceptions( str );
generator.appendThrowExceptions( str );
str += "#endif\n" + structResultValue;
generator.appendStructs( str );
generator.appendHandles( str );
generator.appendHandlesCommandDefinitions( str );
str += R"(} // namespace VULKAN_HPP_NAMESPACE

// clang-format off
#include <vulkan/vulkan_handles.hpp>
#include <vulkan/vulkan_structs.hpp>
#include <vulkan/vulkan_funcs.hpp>
// clang-format on

namespace VULKAN_HPP_NAMESPACE
{
)";
generator.appendStructureChainValidation( str );
generator.appendDispatchLoaderDynamic( str );
str +=
Expand All @@ -16632,24 +16740,9 @@ namespace std
"} // namespace std\n"
"#endif\n";

std::ofstream ofs( VULKAN_HPP_FILE );
assert( !ofs.fail() );
ofs << str;
ofs.close();
writeToFile( str, VULKAN_HPP_FILE );

#if defined( CLANG_FORMAT_EXECUTABLE )
int ret = std::system( "\"" CLANG_FORMAT_EXECUTABLE "\" --version" );
assert( ret == 0 );
std::cout << "VulkanHppGenerator: Formatting " << VULKAN_HPP_FILE << " using clang-format..." << std::endl;
ret = std::system( "\"" CLANG_FORMAT_EXECUTABLE "\" -i --style=file " VULKAN_HPP_FILE );
if ( ret != 0 )
{
std::cout << "VulkanHppGenerator: failed to format file " << VULKAN_HPP_FILE << " with error <" << ret << ">\n";
return -1;
}
#endif

std::cout << "VulkanHppGenerator: Generating " << VULKAN_RAII_HPP_FILE << std::endl;
std::cout << "VulkanHppGenerator: Generating " << VULKAN_RAII_HPP_FILE << " ..." << std::endl;
str.clear();
str = generator.getVulkanLicenseHeader() + R"(
#ifndef VULKAN_RAII_HPP
Expand Down Expand Up @@ -16693,21 +16786,9 @@ namespace VULKAN_HPP_NAMESPACE
#endif
)";

ofs.open( VULKAN_RAII_HPP_FILE );
assert( !ofs.fail() );
ofs << str;
ofs.close();
writeToFile( str, VULKAN_RAII_HPP_FILE );

#if defined( CLANG_FORMAT_EXECUTABLE )
std::cout << "VulkanHppGenerator: Formatting " << VULKAN_RAII_HPP_FILE << " using clang-format..." << std::endl;
ret = std::system( "\"" CLANG_FORMAT_EXECUTABLE "\" -i --style=file " VULKAN_RAII_HPP_FILE );
if ( ret != 0 )
{
std::cout << "VulkanHppGenerator: failed to format file " << VULKAN_RAII_HPP_FILE << " with error <" << ret
<< ">\n";
return -1;
}
#else
#if !defined( CLANG_FORMAT_EXECUTABLE )
std::cout
<< "VulkanHppGenerator: could not find clang-format. The generated files will not be formatted accordingly.\n";
#endif
Expand Down
Loading

0 comments on commit 89a1946

Please sign in to comment.