Skip to content

Commit 6bc56a5

Browse files
committed
Use union for MSLFormatInfo
1 parent 3a1cac2 commit 6bc56a5

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

spirv_msl.hpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,20 +368,44 @@ enum class MSLFormatPacking : uint16_t
368368
};
369369

370370
/// Describes the details of an MSLFormat necessary for easily generating vertex loading code
371-
struct MSLFormatInfo
371+
union MSLFormatInfo
372372
{
373-
uint16_t log2_align : 2; ///< log2(minimum msl alignment)
374-
uint16_t num_elems : 3;
375-
MSLFormatPacking packing : 3;
376-
uint16_t is_float : 1;
377-
uint16_t is_signed : 1;
378-
uint16_t is_normalized : 1;
379-
uint16_t is_packed : 1; ///< Is packed in MSL
380-
uint16_t vk_packed : 1; ///< Is packed in Vulkan but not MSL
381-
uint16_t is_bgr : 1;
382-
uint16_t is_srgb : 1;
383-
uint16_t _pad : 1;
384-
373+
struct
374+
{
375+
uint16_t log2_align : 2; ///< log2(minimum msl alignment)
376+
uint16_t num_elems : 3;
377+
MSLFormatPacking packing : 3;
378+
uint16_t is_float : 1;
379+
uint16_t is_signed : 1;
380+
uint16_t is_normalized : 1;
381+
uint16_t is_packed : 1; ///< Is packed in MSL
382+
uint16_t vk_packed : 1; ///< Is packed in Vulkan but not MSL
383+
uint16_t is_bgr : 1;
384+
uint16_t is_srgb : 1;
385+
uint16_t _pad : 1;
386+
};
387+
uint16_t bits;
388+
389+
constexpr MSLFormatInfo(uint16_t log2_align, uint16_t num_elems, MSLFormatPacking packing, bool is_float,
390+
bool is_signed, bool is_normalized, bool is_packed, bool vk_packed, bool is_bgr,
391+
bool is_srgb)
392+
: log2_align(log2_align)
393+
, num_elems(num_elems)
394+
, packing(packing)
395+
, is_float(is_float)
396+
, is_signed(is_signed)
397+
, is_normalized(is_normalized)
398+
, is_packed(is_packed)
399+
, vk_packed(vk_packed)
400+
, is_bgr(is_bgr)
401+
, is_srgb(is_srgb)
402+
, _pad(0)
403+
{
404+
}
405+
constexpr MSLFormatInfo()
406+
: bits(0)
407+
{
408+
}
385409
constexpr uint32_t align() const
386410
{
387411
return 1 << log2_align;
@@ -400,8 +424,8 @@ struct MSLFormatInfo
400424
}
401425
uint16_t raw_value() const
402426
{
403-
static_assert(sizeof(MSLFormatInfo) == sizeof(uint16_t), "Size check");
404-
return *reinterpret_cast<const uint16_t *>(this);
427+
static_assert(sizeof(MSLFormatInfo) == sizeof(bits), "Size check");
428+
return bits;
405429
}
406430
bool operator==(const MSLFormatInfo &other) const
407431
{

spirv_msl_vertex_loader.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format)
155155
};
156156
// clang-format off
157157
#define FORMAT(log2_align, elems, packing, type, packed, order, vk_packed) \
158-
MSLFormatInfo{ \
158+
MSLFormatInfo( \
159159
/* log2_align = */ log2_align, \
160160
/* num_elems = */ elems, \
161161
/* packing = */ MSLFormatPacking::packing, \
@@ -165,9 +165,8 @@ const MSLFormatInfo &CompilerMSL::get_format_info(MSLFormat format)
165165
/* is_packed = */ packed, \
166166
/* vk_packed = */ vk_packed, \
167167
/* is_bgr = */ (PixelOrder::order == PixelOrder::BGR), \
168-
/* is_srgb = */ (Type::type == Type::SRGB), \
169-
/* _pad = */ 0, \
170-
}
168+
/* is_srgb = */ (Type::type == Type::SRGB) \
169+
)
171170
#define SIMPLE_FORMAT_VK_PACKED(log2_align, elems, type, order) FORMAT(log2_align, elems, EvenAHigh, type, false, order, true)
172171
#define SIMPLE_FORMAT(log2_align, elems, type, order) FORMAT(log2_align, elems, EvenAHigh, type, false, order, false)
173172
#define PACKED_FORMAT(log2_align, elems, packing, type, order) FORMAT(log2_align, elems, packing, type, true, order, false)

0 commit comments

Comments
 (0)