Skip to content

Commit

Permalink
Vertex Loader C++11 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
etang-cw committed Oct 2, 2023
1 parent c3bf805 commit 2606371
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spirv_msl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7579,7 +7579,7 @@ void CompilerMSL::prepare_shader_vertex_loader()
}

auto &entry_func = get<SPIRFunction>(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<SPIRVariable>(stage_in_var_id).basetype), " ", get_name(stage_in_var_id), " = spvLoadVertex(", load, ");");
}, SPIRFunction::FixupInPriority::VertexLoad);
}
Expand Down
91 changes: 88 additions & 3 deletions spirv_msl_vertex_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ struct ConstexprLookupTable
static constexpr std::size_t size() { return Size; }
};

template <typename First, typename Second>
struct ConstexprPair
{
First first;
Second second;
};

#if __cplusplus >= 201300L
template <uint32_t Highest, typename OutType, uint32_t InSize, typename IndexType>
constexpr static ConstexprLookupTable<OutType, Highest + 1> gen_lookup_table(const std::pair<IndexType, OutType> (&entry_list)[InSize])
constexpr static ConstexprLookupTable<OutType, Highest + 1> gen_lookup_table(
const ConstexprPair<IndexType, OutType> (&entry_list)[InSize])
{
#define CXPR_ASSERT_REL(x) ((x) ? static_cast<void>(0) : abort())
bool written[Highest + 1] = {};
Expand All @@ -50,6 +59,82 @@ constexpr static ConstexprLookupTable<OutType, Highest + 1> gen_lookup_table(con
#undef CXPR_ASSERT_REL
return output;
}
#else
namespace detail
{

// From https://stackoverflow.com/a/61447603
template<class T, T... Ints>
struct integer_sequence {};

template<std::size_t... Ints>
using index_sequence = integer_sequence<std::size_t, Ints...>;

template<typename Firsts, typename Last>
struct index_sequence_eights_append;

template<std::size_t... N, std::size_t... M>
struct index_sequence_eights_append<index_sequence<N...>, index_sequence<M...>> {
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<std::size_t N>
struct make_index_sequence_helper {
using type = typename index_sequence_eights_append<typename make_index_sequence_helper<N / 8u>::type, typename make_index_sequence_helper<N - 7u * (N / 8u)>::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<std::size_t N>
using make_index_sequence = typename make_index_sequence_helper<N>::type;

template <typename OutType, uint32_t InSize, typename IndexType, uint32_t Idx>
struct get_matching_element
{
static constexpr OutType search(const ConstexprPair<IndexType, OutType> (&entry_list)[InSize], IndexType needle)
{
return entry_list[Idx].first == needle ?
entry_list[Idx].second :
get_matching_element<OutType, InSize, IndexType, Idx + 1>::search(entry_list, needle);
}
};

template <typename OutType, uint32_t InSize, typename IndexType>
struct get_matching_element<OutType, InSize, IndexType, InSize>
{
static constexpr OutType search(const ConstexprPair<IndexType, OutType> (&)[InSize], IndexType)
{
return {};
}
};

template <typename OutType, uint32_t InSize, typename IndexType, std::size_t... I>
constexpr static ConstexprLookupTable<OutType, sizeof...(I)> gen_lookup_table(
const ConstexprPair<IndexType, OutType> (&entry_list)[InSize], index_sequence<I...>)
{
return { get_matching_element<OutType, InSize, IndexType, 0>::search(entry_list, static_cast<IndexType>(I))... };
}

} // namespace detail

template <uint32_t Highest, typename OutType, uint32_t InSize, typename IndexType>
constexpr static ConstexprLookupTable<OutType, Highest + 1> gen_lookup_table(
const ConstexprPair<IndexType, OutType> (&entry_list)[InSize])
{
return detail::gen_lookup_table(entry_list, detail::make_index_sequence<Highest + 1>{});
}
#endif

const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format)
{
Expand Down Expand Up @@ -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<MSLFormat, MSLFormatInfo> s_vertex_info_entries[] = {
static constexpr ConstexprPair<MSLFormat, MSLFormatInfo> s_vertex_info_entries[] = {
{ MSL_FORMAT_UNDEFINED, {} },
// Packed Formats
// (VkFormat uses big endian names, which makes the bit ordering extra fun)
Expand Down Expand Up @@ -221,7 +306,7 @@ const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format)

static constexpr auto s_vertex_info = gen_lookup_table<MSL_FORMAT_E5B9G9R9_UFLOAT_PACK32>(s_vertex_info_entries);

static constexpr std::pair<MSLFormat, MSLFormatInfo> s_vertex_info_high[] = {
static constexpr ConstexprPair<MSLFormat, MSLFormatInfo> 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) },
Expand Down

0 comments on commit 2606371

Please sign in to comment.