From 260637155fe4ae387bf38c958046f5334f232905 Mon Sep 17 00:00:00 2001 From: Evan Tang Date: Mon, 2 Oct 2023 11:45:40 -0500 Subject: [PATCH] Vertex Loader C++11 Support --- CMakeLists.txt | 2 - Makefile | 2 +- spirv_msl.cpp | 2 +- spirv_msl_vertex_loader.cpp | 91 +++++++++++++++++++++++++++++++++++-- 4 files changed, 90 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51704bc6a..b3a0b144b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -288,7 +288,6 @@ if (SPIRV_CROSS_STATIC) if (SPIRV_CROSS_ENABLE_MSL) spirv_cross_add_library(spirv-cross-msl spirv_cross_msl STATIC ${spirv-cross-msl-sources}) - target_compile_features(spirv-cross-msl PRIVATE cxx_std_14) if (SPIRV_CROSS_ENABLE_GLSL) target_link_libraries(spirv-cross-msl PRIVATE spirv-cross-glsl) else() @@ -377,7 +376,6 @@ if (SPIRV_CROSS_SHARED) if (SPIRV_CROSS_ENABLE_MSL) if (SPIRV_CROSS_ENABLE_GLSL) target_sources(spirv-cross-c-shared PRIVATE ${spirv-cross-msl-sources}) - target_compile_features(spirv-cross-c-shared PRIVATE cxx_std_14) else() message(FATAL_ERROR "Must enable GLSL support to enable MSL support.") endif() diff --git a/Makefile b/Makefile index 07167e16e..b44eb5e8c 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ STATIC_LIB := lib$(TARGET).a DEPS := $(OBJECTS:.o=.d) $(CLI_OBJECTS:.o=.d) -CXXFLAGS += -std=c++14 -Wall -Wextra -Wshadow -Wno-deprecated-declarations +CXXFLAGS += -std=c++11 -Wall -Wextra -Wshadow -Wno-deprecated-declarations ifeq ($(DEBUG), 1) CXXFLAGS += -O0 -g diff --git a/spirv_msl.cpp b/spirv_msl.cpp index 0c9ae0c43..83d70c8e8 100644 --- a/spirv_msl.cpp +++ b/spirv_msl.cpp @@ -7579,7 +7579,7 @@ void CompilerMSL::prepare_shader_vertex_loader() } auto &entry_func = get(ir.default_entry_point); - entry_func.add_fixup_hook_in([this, load = std::move(load)]{ + entry_func.add_fixup_hook_in([this, load]{ statement(get_name(this->get(stage_in_var_id).basetype), " ", get_name(stage_in_var_id), " = spvLoadVertex(", load, ");"); }, SPIRFunction::FixupInPriority::VertexLoad); } diff --git a/spirv_msl_vertex_loader.cpp b/spirv_msl_vertex_loader.cpp index 71b7a0d8b..f9599c730 100644 --- a/spirv_msl_vertex_loader.cpp +++ b/spirv_msl_vertex_loader.cpp @@ -33,8 +33,17 @@ struct ConstexprLookupTable static constexpr std::size_t size() { return Size; } }; +template +struct ConstexprPair +{ + First first; + Second second; +}; + +#if __cplusplus >= 201300L template -constexpr static ConstexprLookupTable gen_lookup_table(const std::pair (&entry_list)[InSize]) +constexpr static ConstexprLookupTable gen_lookup_table( + const ConstexprPair (&entry_list)[InSize]) { #define CXPR_ASSERT_REL(x) ((x) ? static_cast(0) : abort()) bool written[Highest + 1] = {}; @@ -50,6 +59,82 @@ constexpr static ConstexprLookupTable gen_lookup_table(con #undef CXPR_ASSERT_REL return output; } +#else +namespace detail +{ + +// From https://stackoverflow.com/a/61447603 +template +struct integer_sequence {}; + +template +using index_sequence = integer_sequence; + +template +struct index_sequence_eights_append; + +template +struct index_sequence_eights_append, index_sequence> { + using type = index_sequence< + N..., (sizeof...(N) + N)..., (2u * sizeof...(N) + N)..., (3u * sizeof...(N) + N)..., + (4u * sizeof...(N) + N)..., (5u * sizeof...(N) + N)..., (6u * sizeof...(N) + N)..., + (7u * sizeof...(N) + M)... + >; +}; + +template +struct make_index_sequence_helper { + using type = typename index_sequence_eights_append::type, typename make_index_sequence_helper::type>::type; +}; + +template<> struct make_index_sequence_helper<0> { using type = index_sequence<>; }; +template<> struct make_index_sequence_helper<1> { using type = index_sequence<0>; }; +template<> struct make_index_sequence_helper<2> { using type = index_sequence<0, 1>; }; +template<> struct make_index_sequence_helper<3> { using type = index_sequence<0, 1, 2>; }; +template<> struct make_index_sequence_helper<4> { using type = index_sequence<0, 1, 2, 3>; }; +template<> struct make_index_sequence_helper<5> { using type = index_sequence<0, 1, 2, 3, 4>; }; +template<> struct make_index_sequence_helper<6> { using type = index_sequence<0, 1, 2, 3, 4, 5>; }; +template<> struct make_index_sequence_helper<7> { using type = index_sequence<0, 1, 2, 3, 4, 5, 6>; }; + +template +using make_index_sequence = typename make_index_sequence_helper::type; + +template +struct get_matching_element +{ + static constexpr OutType search(const ConstexprPair (&entry_list)[InSize], IndexType needle) + { + return entry_list[Idx].first == needle ? + entry_list[Idx].second : + get_matching_element::search(entry_list, needle); + } +}; + +template +struct get_matching_element +{ + static constexpr OutType search(const ConstexprPair (&)[InSize], IndexType) + { + return {}; + } +}; + +template +constexpr static ConstexprLookupTable gen_lookup_table( + const ConstexprPair (&entry_list)[InSize], index_sequence) +{ + return { get_matching_element::search(entry_list, static_cast(I))... }; +} + +} // namespace detail + +template +constexpr static ConstexprLookupTable gen_lookup_table( + const ConstexprPair (&entry_list)[InSize]) +{ + return detail::gen_lookup_table(entry_list, detail::make_index_sequence{}); +} +#endif const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format) { @@ -86,7 +171,7 @@ const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format) #define SIMPLE_FORMAT_VK_PACKED(log2_align, elems, type, order) FORMAT(log2_align, elems, EvenAHigh, type, false, order, true) #define SIMPLE_FORMAT(log2_align, elems, type, order) FORMAT(log2_align, elems, EvenAHigh, type, false, order, false) #define PACKED_FORMAT(log2_align, elems, packing, type, order) FORMAT(log2_align, elems, packing, type, true, order, false) - static constexpr std::pair s_vertex_info_entries[] = { + static constexpr ConstexprPair s_vertex_info_entries[] = { { MSL_FORMAT_UNDEFINED, {} }, // Packed Formats // (VkFormat uses big endian names, which makes the bit ordering extra fun) @@ -221,7 +306,7 @@ const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format) static constexpr auto s_vertex_info = gen_lookup_table(s_vertex_info_entries); - static constexpr std::pair s_vertex_info_high[] = { + static constexpr ConstexprPair s_vertex_info_high[] = { { MSL_FORMAT_A4R4G4B4_UNORM_PACK16, PACKED_FORMAT(1, 4, EvenAHigh, UNorm, BGR) }, { MSL_FORMAT_A4B4G4R4_UNORM_PACK16, PACKED_FORMAT(1, 4, EvenAHigh, UNorm, RGB) }, { MSL_FORMAT_A1B5G5R5_UNORM_PACK16_KHR, PACKED_FORMAT(1, 4, AlphaHigh, UNorm, RGB) },