MaterialX 1.39.2
Loading...
Searching...
No Matches
TypeDesc.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_TYPEDESC_H
7#define MATERIALX_TYPEDESC_H
8
11
13#include <MaterialXCore/Value.h>
14
15#include <string_view>
16
17MATERIALX_NAMESPACE_BEGIN
18
35class MX_GENSHADER_API TypeDesc
36{
37 public:
38 enum BaseType
39 {
40 BASETYPE_NONE,
41 BASETYPE_BOOLEAN,
42 BASETYPE_INTEGER,
43 BASETYPE_FLOAT,
44 BASETYPE_STRING,
45 BASETYPE_STRUCT,
46 BASETYPE_LAST
47 };
48
49 enum Semantic
50 {
51 SEMANTIC_NONE,
52 SEMANTIC_COLOR,
53 SEMANTIC_VECTOR,
54 SEMANTIC_MATRIX,
55 SEMANTIC_FILENAME,
56 SEMANTIC_CLOSURE,
57 SEMANTIC_SHADER,
58 SEMANTIC_MATERIAL,
59 SEMANTIC_ENUM,
60 SEMANTIC_LAST
61 };
62
64 constexpr TypeDesc() noexcept :
65 _id(0),
66 _basetype(BASETYPE_NONE),
67 _semantic(SEMANTIC_NONE),
68 _size(0),
69 _structIndex(0)
70 {
71 }
72
74 constexpr TypeDesc(std::string_view name, uint8_t basetype, uint8_t semantic = SEMANTIC_NONE, uint16_t size = 1, uint16_t structIndex = 0) noexcept :
75 _id(constexpr_hash(name)), // Note: We only store the hash to keep the class size minimal.
76 _basetype(basetype),
77 _semantic(semantic),
78 _size(size),
79 _structIndex(structIndex)
80 {
81 }
82
85 uint32_t typeId() const { return _id; }
86
88 const string& getName() const;
89
91 unsigned char getBaseType() const { return _basetype; }
92
94 unsigned char getSemantic() const { return _semantic; }
95
100 size_t getSize() const { return _size; }
101
103 bool isScalar() const { return _size == 1; }
104
106 bool isAggregate() const { return _size > 1; }
107
109 bool isArray() const { return _size == 0; }
110
112 bool isFloat2() const { return _size == 2 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
113
115 bool isFloat3() const { return _size == 3 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
116
118 bool isFloat4() const { return _size == 4 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
119
121 bool isClosure() const { return (_semantic == SEMANTIC_CLOSURE || _semantic == SEMANTIC_SHADER || _semantic == SEMANTIC_MATERIAL); }
122
124 bool isStruct() const { return _basetype == BASETYPE_STRUCT; }
125
127 uint16_t getStructIndex() const { return _structIndex; }
128
130 bool operator==(TypeDesc rhs) const
131 {
132 return _id == rhs._id;
133 }
134
136 bool operator!=(TypeDesc rhs) const
137 {
138 return _id != rhs._id;
139 }
140
142 bool operator<(TypeDesc rhs) const
143 {
144 return _id < rhs._id;
145 }
146
148 struct Hasher
149 {
150 size_t operator()(const TypeDesc& t) const
151 {
152 return t._id;
153 }
154 };
155
158 static TypeDesc get(const string& name);
159
161 static void remove(const string& name);
162
163 static const string NONE_TYPE_NAME;
164
166 ValuePtr createValueFromStrings(const string& value) const;
167
168 private:
171 constexpr uint32_t constexpr_hash(std::string_view str, uint32_t n = 0, uint32_t h = 2166136261)
172 {
173 return n == uint32_t(str.size()) ? h : constexpr_hash(str, n + 1, (h * 16777619) ^ (str[n]));
174 }
175
176 uint32_t _id;
177 uint8_t _basetype;
178 uint8_t _semantic;
179 uint16_t _size;
180 uint16_t _structIndex;
181};
182
185class MX_GENSHADER_API TypeDescRegistry
186{
187 public:
188 TypeDescRegistry(TypeDesc type, const string& name);
189};
190
192#define TYPEDESC_DEFINE_TYPE(T, name, basetype, semantic, size) \
193 static constexpr TypeDesc T(name, basetype, semantic, size);
194
197#define TYPEDESC_REGISTER_TYPE(T, name) \
198 TypeDescRegistry register_##T(T, name);
199
200namespace Type
201{
202
203//
205//
206TYPEDESC_DEFINE_TYPE(NONE, "none", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_NONE, 1)
207TYPEDESC_DEFINE_TYPE(BOOLEAN, "boolean", TypeDesc::BASETYPE_BOOLEAN, TypeDesc::SEMANTIC_NONE, 1)
208TYPEDESC_DEFINE_TYPE(INTEGER, "integer", TypeDesc::BASETYPE_INTEGER, TypeDesc::SEMANTIC_NONE, 1)
209TYPEDESC_DEFINE_TYPE(FLOAT, "float", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_NONE, 1)
210TYPEDESC_DEFINE_TYPE(INTEGERARRAY, "integerarray", TypeDesc::BASETYPE_INTEGER, TypeDesc::SEMANTIC_NONE, 0)
211TYPEDESC_DEFINE_TYPE(FLOATARRAY, "floatarray", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_NONE, 0)
212TYPEDESC_DEFINE_TYPE(VECTOR2, "vector2", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 2)
213TYPEDESC_DEFINE_TYPE(VECTOR3, "vector3", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 3)
214TYPEDESC_DEFINE_TYPE(VECTOR4, "vector4", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 4)
215TYPEDESC_DEFINE_TYPE(COLOR3, "color3", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_COLOR, 3)
216TYPEDESC_DEFINE_TYPE(COLOR4, "color4", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_COLOR, 4)
217TYPEDESC_DEFINE_TYPE(MATRIX33, "matrix33", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 9)
218TYPEDESC_DEFINE_TYPE(MATRIX44, "matrix44", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 16)
219TYPEDESC_DEFINE_TYPE(STRING, "string", TypeDesc::BASETYPE_STRING, TypeDesc::SEMANTIC_NONE, 1)
220TYPEDESC_DEFINE_TYPE(FILENAME, "filename", TypeDesc::BASETYPE_STRING, TypeDesc::SEMANTIC_FILENAME, 1)
221TYPEDESC_DEFINE_TYPE(BSDF, "BSDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
222TYPEDESC_DEFINE_TYPE(EDF, "EDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
223TYPEDESC_DEFINE_TYPE(VDF, "VDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
224TYPEDESC_DEFINE_TYPE(SURFACESHADER, "surfaceshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
225TYPEDESC_DEFINE_TYPE(VOLUMESHADER, "volumeshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
226TYPEDESC_DEFINE_TYPE(DISPLACEMENTSHADER, "displacementshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
227TYPEDESC_DEFINE_TYPE(LIGHTSHADER, "lightshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
228TYPEDESC_DEFINE_TYPE(MATERIAL, "material", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_MATERIAL, 1)
229
230} // namespace Type
231
232
241class MX_GENSHADER_API StructTypeDesc
242{
243 public:
244 struct StructMemberTypeDesc
245 {
246 StructMemberTypeDesc(string name, TypeDesc typeDesc, string defaultValueStr) :
247 _name(name), _typeDesc(typeDesc), _defaultValueStr(defaultValueStr)
248 {
249 }
250 string _name;
251 TypeDesc _typeDesc;
252 string _defaultValueStr;
253 };
254
256 StructTypeDesc() noexcept{}
257
258 void addMember(const string& name, TypeDesc type, const string& defaultValueStr);
259 void setTypeDesc(TypeDesc typedesc) { _typedesc = typedesc; }
260
262 static StructTypeDesc& get(unsigned int index);
263 static vector<string> getStructTypeNames();
264 static uint16_t emplace_back(StructTypeDesc structTypeDesc);
265 static void clear();
266
267 TypeDesc typeDesc() const { return _typedesc; }
268
269 const string& getName() const;
270
271 const vector<StructMemberTypeDesc>& getMembers() const;
272
273 private:
274 TypeDesc _typedesc;
275 vector<StructMemberTypeDesc> _members;
276};
277
280class MX_GENSHADER_API StructTypeDescRegistry
281{
282 public:
283 StructTypeDescRegistry();
284};
285
286MATERIALX_NAMESPACE_END
287
288#endif
Macros for declaring imported and exported symbols.
#define TYPEDESC_DEFINE_TYPE(T, name, basetype, semantic, size)
Macro to define global type descriptions for commonly used types.
Definition TypeDesc.h:192
Generic value classes.
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition Value.h:30
StructTypeDesc() noexcept
Empty constructor.
Definition TypeDesc.h:256
static StructTypeDesc & get(unsigned int index)
Return a type description by index.
A type descriptor for MaterialX data types.
Definition TypeDesc.h:36
constexpr TypeDesc(std::string_view name, uint8_t basetype, uint8_t semantic=SEMANTIC_NONE, uint16_t size=1, uint16_t structIndex=0) noexcept
Constructor.
Definition TypeDesc.h:74
constexpr TypeDesc() noexcept
Empty constructor.
Definition TypeDesc.h:64
bool operator!=(TypeDesc rhs) const
Inequality operator.
Definition TypeDesc.h:136
bool operator==(TypeDesc rhs) const
Equality operator.
Definition TypeDesc.h:130
const string & getName() const
Return the name of the type.
bool isClosure() const
Return true if the type represents a closure.
Definition TypeDesc.h:121
static void remove(const string &name)
Remove a type description by name, if it exists.
bool isStruct() const
Return true if the type represents a struct.
Definition TypeDesc.h:124
bool operator<(TypeDesc rhs) const
Less-than operator.
Definition TypeDesc.h:142
bool isScalar() const
Return true if the type is a scalar type.
Definition TypeDesc.h:103
size_t getSize() const
Return the number of elements the type is composed of.
Definition TypeDesc.h:100
bool isFloat3() const
Return true if the type is an aggregate of 3 floats.
Definition TypeDesc.h:115
unsigned char getSemantic() const
Return the semantic for the type.
Definition TypeDesc.h:94
ValuePtr createValueFromStrings(const string &value) const
Create a Value from a string for a given typeDesc.
bool isAggregate() const
Return true if the type is an aggregate type.
Definition TypeDesc.h:106
static TypeDesc get(const string &name)
Return a type description by name.
bool isFloat2() const
Return true if the type is an aggregate of 2 floats.
Definition TypeDesc.h:112
uint16_t getStructIndex() const
Return the index for the struct member information in StructTypeDesc, the result is invalid if isStru...
Definition TypeDesc.h:127
bool isArray() const
Return true if the type is an array type.
Definition TypeDesc.h:109
uint32_t typeId() const
Return the unique id assigned to this type.
Definition TypeDesc.h:85
bool isFloat4() const
Return true if the type is an aggregate of 4 floats.
Definition TypeDesc.h:118
unsigned char getBaseType() const
Return the basetype for the type.
Definition TypeDesc.h:91
Hash operator.
Definition TypeDesc.h:149