Skip to content

Commit

Permalink
Use union for MSLFormatInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
etang-cw committed Nov 16, 2023
1 parent 3a1cac2 commit 5fad5d1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
54 changes: 39 additions & 15 deletions spirv_msl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,44 @@ enum class MSLFormatPacking : uint16_t
};

/// Describes the details of an MSLFormat necessary for easily generating vertex loading code
struct MSLFormatInfo
union MSLFormatInfo
{
uint16_t log2_align : 2; ///< log2(minimum msl alignment)
uint16_t num_elems : 3;
MSLFormatPacking packing : 3;
uint16_t is_float : 1;
uint16_t is_signed : 1;
uint16_t is_normalized : 1;
uint16_t is_packed : 1; ///< Is packed in MSL
uint16_t vk_packed : 1; ///< Is packed in Vulkan but not MSL
uint16_t is_bgr : 1;
uint16_t is_srgb : 1;
uint16_t _pad : 1;

struct
{
uint16_t log2_align : 2; ///< log2(minimum msl alignment)
uint16_t num_elems : 3;
MSLFormatPacking packing : 3;
uint16_t is_float : 1;
uint16_t is_signed : 1;
uint16_t is_normalized : 1;
uint16_t is_packed : 1; ///< Is packed in MSL
uint16_t vk_packed : 1; ///< Is packed in Vulkan but not MSL
uint16_t is_bgr : 1;
uint16_t is_srgb : 1;
uint16_t _pad : 1;
};
uint16_t bits;

constexpr MSLFormatInfo(uint16_t log2_align_, uint16_t num_elems_, MSLFormatPacking packing_, bool is_float_,
bool is_signed_, bool is_normalized_, bool is_packed_, bool vk_packed_, bool is_bgr_,
bool is_srgb_)
: log2_align(log2_align_)
, num_elems(num_elems_)
, packing(packing_)
, is_float(is_float_)
, is_signed(is_signed_)
, is_normalized(is_normalized_)
, is_packed(is_packed_)
, vk_packed(vk_packed_)
, is_bgr(is_bgr_)
, is_srgb(is_srgb_)
, _pad(0)
{
}
constexpr MSLFormatInfo()
: bits(0)
{
}
constexpr uint32_t align() const
{
return 1 << log2_align;
Expand All @@ -400,8 +424,8 @@ struct MSLFormatInfo
}
uint16_t raw_value() const
{
static_assert(sizeof(MSLFormatInfo) == sizeof(uint16_t), "Size check");
return *reinterpret_cast<const uint16_t *>(this);
static_assert(sizeof(MSLFormatInfo) == sizeof(bits), "Size check");
return bits;
}
bool operator==(const MSLFormatInfo &other) const
{
Expand Down
7 changes: 3 additions & 4 deletions spirv_msl_vertex_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format)
};
// clang-format off
#define FORMAT(log2_align, elems, packing, type, packed, order, vk_packed) \
MSLFormatInfo{ \
MSLFormatInfo( \
/* log2_align = */ log2_align, \
/* num_elems = */ elems, \
/* packing = */ MSLFormatPacking::packing, \
Expand All @@ -165,9 +165,8 @@ const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format)
/* is_packed = */ packed, \
/* vk_packed = */ vk_packed, \
/* is_bgr = */ (PixelOrder::order == PixelOrder::BGR), \
/* is_srgb = */ (Type::type == Type::SRGB), \
/* _pad = */ 0, \
}
/* is_srgb = */ (Type::type == Type::SRGB) \
)
#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)
Expand Down

0 comments on commit 5fad5d1

Please sign in to comment.