-
Notifications
You must be signed in to change notification settings - Fork 10
/
Constant.h
188 lines (163 loc) · 4.83 KB
/
Constant.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#pragma once
#include "Type.h"
#include "Operand.h" // for appendLiteralsToContainer
#include "Vector.h"
namespace spvgentwo
{
class Constant
{
public:
Constant(IAllocator* _pAllocator = nullptr);
Constant(const Constant& _other);
Constant(Constant&& _other) noexcept;
~Constant();
Constant& operator=(const Constant& _other);
Constant& operator=(Constant&& _other) noexcept;
constexpr spv::Op getOperation() const { return m_Operation; }
void setOperation(const spv::Op _op) { m_Operation = _op; }
constexpr const Type& getType() const { return m_Type; }
constexpr Type& getType() { return m_Type; }
template <class T>
Type& setType();
constexpr const Vector<unsigned int>& getData() const { return m_literalData; }
constexpr Vector<unsigned int>& getData() { return m_literalData; }
template <class T>
void addData(const T& _data);
constexpr const List<Constant>& getComponents() const { return m_Components; }
constexpr List<Constant>& getComponents() { return m_Components; }
template <class T>
Constant& make(const T& _value, const bool _spec = false);
// adds a new constituent constant
Constant& Component();
void reset();
// get top-level data (would be empty for structs)
template <class T>
const T* getDataAs(bool _checkType = true) const;
private:
spv::Op m_Operation = spv::Op::OpConstantNull;
Type m_Type;
List<Constant> m_Components;
Vector<unsigned int> m_literalData;
};
template<class T>
inline Type& Constant::setType()
{
return m_Type.make<T>();
}
template<class T>
inline void Constant::addData(const T& _data)
{
m_literalData.reserve(m_literalData.size() + wordCount<T>());
appendLiteralsToContainer(m_literalData, _data);
}
template <class T>
Constant& Constant::make(const T& _value, const bool _spec)
{
if constexpr (stdrep::is_same_v<T, bool>)
{
m_Operation = _value ? (_spec ? spv::Op::OpSpecConstantTrue : spv::Op::OpConstantTrue) : (_spec ? spv::Op::OpSpecConstantFalse : spv::Op::OpConstantFalse);
m_Type.make<bool>();
addData(_value);
}
else if constexpr (traits::is_primitive_type_v<traits::remove_cvref_t<T>>)
{
m_Operation = _spec ? spv::Op::OpSpecConstant : spv::Op::OpConstant;
m_Type.make<T>();
addData(_value);
}
else if constexpr (is_const_null_v<T>)
{
m_Type.make<typename T::const_null_type>();
m_Operation = spv::Op::OpConstantNull; // no literal data
}
else if constexpr (is_const_array_v<T>)
{
m_Operation = _spec ? spv::Op::OpSpecConstantComposite : spv::Op::OpConstantComposite;
m_Type.make<typename T::const_array_type>();
for (unsigned int i = 0u; i < T::Elements; ++i)
{
Component().make(_value.data[i]);
}
}
else if constexpr (is_const_vector_v<T>)
{
m_Operation = _spec ? spv::Op::OpSpecConstantComposite : spv::Op::OpConstantComposite;
m_Type.make<typename T::const_vector_type>();
for (unsigned int i = 0u; i < T::Elements; ++i)
{
Component().make(_value.data[i]);
}
}
else if constexpr (is_const_matrix_v<T>)
{
m_Operation = _spec ? spv::Op::OpSpecConstantComposite : spv::Op::OpConstantComposite;
m_Type.make<typename T::const_matrix_type>();
for (unsigned int i = 0u; i < T::Columns; ++i)
{
Component().make(_value.data[i]);
}
}
else if constexpr(is_const_sampler_v<T>)
{
m_Operation = spv::Op::OpConstantSampler;
m_Type.make<typename T::const_sampler_type>();
addData(_value.addressMode);
addData(_value.coordMode);
addData(_value.filterMode);
}
else
{
// required for clang which can't deduce that this code is unreachable for supported type instantiation
constexpr bool match =
stdrep::is_same_v<T, bool> ||
traits::is_primitive_type_v<traits::remove_cvref_t<T>> ||
is_const_null_v<T> ||
is_const_array_v<T> ||
is_const_vector_v<T> ||
is_const_matrix_v<T> ||
is_const_sampler_v<T>;
static_assert(match, "Could not match type");
}
return *this;
}
template<class T>
inline const T* Constant::getDataAs(bool _checkType) const
{
if (sizeof(T) <= m_literalData.size() * sizeof(unsigned int))
{
if (_checkType)
{
Type t(m_literalData.getAllocator());
if (t.make<T>() != m_Type)
{
return nullptr;
}
}
return reinterpret_cast<const T*>(m_literalData.data());
}
return nullptr;
}
template <>
struct Hasher<Constant>
{
constexpr Hash64 operator()(const Constant& _const, Hash64 _seed) const
{
FNV1aHasher hasher(_seed);
return operator()(_const, hasher);
}
constexpr Hash64 operator()(const Constant& _const, FNV1aHasher& _hasher) const
{
_hasher << _const.getOperation();
Hasher<Type>()(_const.getType(), _hasher);
for (const auto& l : _const.getData())
{
_hasher << l;
}
for (const Constant& component : _const.getComponents())
{
operator()(component, _hasher);
}
return _hasher;
}
};
} // !spvgentwo