MaterialX 1.39.3
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>
15
16#include <string_view>
17
18MATERIALX_NAMESPACE_BEGIN
19
20class TypeDesc;
22using TypeDescVec = vector<TypeDesc>;
23using TypeDescMap = std::unordered_map<string, TypeDesc>;
24using StructMemberDescVec = vector<StructMemberDesc>;
25using StructMemberDescVecPtr = shared_ptr<const StructMemberDescVec>;
26
39class MX_GENSHADER_API TypeDesc
40{
41 public:
42 enum BaseType
43 {
44 BASETYPE_NONE,
45 BASETYPE_BOOLEAN,
46 BASETYPE_INTEGER,
47 BASETYPE_FLOAT,
48 BASETYPE_STRING,
49 BASETYPE_STRUCT,
50 BASETYPE_LAST
51 };
52
53 enum Semantic
54 {
55 SEMANTIC_NONE,
56 SEMANTIC_COLOR,
57 SEMANTIC_VECTOR,
58 SEMANTIC_MATRIX,
59 SEMANTIC_FILENAME,
60 SEMANTIC_CLOSURE,
61 SEMANTIC_SHADER,
62 SEMANTIC_MATERIAL,
63 SEMANTIC_ENUM,
64 SEMANTIC_LAST
65 };
66
68 class DataBlock
69 {
70 public:
71 DataBlock(const string& name, const StructMemberDescVecPtr members = nullptr) noexcept : _name(name), _members(members) {}
72
73 const string& getName() const { return _name; }
74 StructMemberDescVecPtr getStructMembers() const { return _members; }
75
76 private:
77 const string _name;
78 const StructMemberDescVecPtr _members;
79 };
80
81 using DataBlockPtr = std::shared_ptr<DataBlock>;
82
84 constexpr TypeDesc() noexcept :
85 _id(0),
86 _basetype(BASETYPE_NONE),
87 _semantic(SEMANTIC_NONE),
88 _size(0),
89 _data(nullptr)
90 {
91 }
92
94 constexpr TypeDesc(std::string_view name, uint8_t basetype, uint8_t semantic, uint16_t size, const DataBlock* data) noexcept :
95 _id(constexpr_hash(name)), // Note: We only store the hash to keep the class size minimal.
96 _basetype(basetype),
97 _semantic(semantic),
98 _size(size),
99 _data(data)
100 {
101 }
102
105 uint32_t typeId() const { return _id; }
106
108 const string& getName() const;
109
111 unsigned char getBaseType() const { return _basetype; }
112
114 unsigned char getSemantic() const { return _semantic; }
115
120 size_t getSize() const { return _size; }
121
123 bool isScalar() const { return _size == 1; }
124
126 bool isAggregate() const { return _size > 1; }
127
129 bool isArray() const { return _size == 0; }
130
132 bool isFloat2() const { return _size == 2 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
133
135 bool isFloat3() const { return _size == 3 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
136
138 bool isFloat4() const { return _size == 4 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
139
141 bool isClosure() const { return (_semantic == SEMANTIC_CLOSURE || _semantic == SEMANTIC_SHADER || _semantic == SEMANTIC_MATERIAL); }
142
144 bool isStruct() const { return _basetype == BASETYPE_STRUCT; }
145
148 StructMemberDescVecPtr getStructMembers() const;
149
151 bool operator==(TypeDesc rhs) const
152 {
153 return _id == rhs._id;
154 }
155
157 bool operator!=(TypeDesc rhs) const
158 {
159 return _id != rhs._id;
160 }
161
163 bool operator<(TypeDesc rhs) const
164 {
165 return _id < rhs._id;
166 }
167
169 struct Hasher
170 {
171 size_t operator()(const TypeDesc& t) const
172 {
173 return t._id;
174 }
175 };
176
177 static const string NONE_TYPE_NAME;
178
180 ValuePtr createValueFromStrings(const string& value) const;
181
182 private:
185 constexpr uint32_t constexpr_hash(std::string_view str, uint32_t n = 0, uint32_t h = 2166136261)
186 {
187 return n == uint32_t(str.size()) ? h : constexpr_hash(str, n + 1, (h * 16777619) ^ (str[n]));
188 }
189
190 uint32_t _id;
191 uint8_t _basetype;
192 uint8_t _semantic;
193 uint16_t _size;
194 const DataBlock* _data;
195};
196
199class StructMemberDesc
200{
201public:
202 StructMemberDesc(TypeDesc type, const string& name, const string& defaultValueStr) :
203 _type(type),
204 _name(name),
205 _defaultValueStr(defaultValueStr)
206 {
207 }
208
209 TypeDesc getType() const { return _type; }
210 const string& getName() const { return _name; }
211 const string& getDefaultValueStr() const { return _defaultValueStr; }
212
213private:
214 const TypeDesc _type;
215 const string _name;
216 const string _defaultValueStr;
217};
218
219using TypeSystemPtr = shared_ptr<class TypeSystem>;
220
223class MX_GENSHADER_API TypeSystem
224{
225public:
227 static TypeSystemPtr create();
228
231
233 void registerType(const string& name, uint8_t basetype, uint8_t semantic, uint16_t size, StructMemberDescVecPtr members = nullptr);
234
237 TypeDesc getType(const string& name) const;
238
240 const TypeDescVec& getTypes() const
241 {
242 return _types;
243 }
244
245protected:
246 // Protected constructor
247 TypeSystem();
248
249private:
250 TypeDescVec _types;
251 TypeDescMap _typesByName;
252 vector<TypeDesc::DataBlockPtr> _dataBlocks;
253};
254
256#define TYPEDESC_DEFINE_TYPE(T, name, basetype, semantic, size) \
257 inline const TypeDesc::DataBlock* T##_data() { static const TypeDesc::DataBlock _data(name); return &_data; } \
258 static const TypeDesc T(name, basetype, semantic, size, T##_data());
259
260namespace Type
261{
262
263//
265//
266TYPEDESC_DEFINE_TYPE(NONE, "none", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_NONE, 1)
267TYPEDESC_DEFINE_TYPE(BOOLEAN, "boolean", TypeDesc::BASETYPE_BOOLEAN, TypeDesc::SEMANTIC_NONE, 1)
268TYPEDESC_DEFINE_TYPE(INTEGER, "integer", TypeDesc::BASETYPE_INTEGER, TypeDesc::SEMANTIC_NONE, 1)
269TYPEDESC_DEFINE_TYPE(FLOAT, "float", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_NONE, 1)
270TYPEDESC_DEFINE_TYPE(INTEGERARRAY, "integerarray", TypeDesc::BASETYPE_INTEGER, TypeDesc::SEMANTIC_NONE, 0)
271TYPEDESC_DEFINE_TYPE(FLOATARRAY, "floatarray", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_NONE, 0)
272TYPEDESC_DEFINE_TYPE(VECTOR2, "vector2", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 2)
273TYPEDESC_DEFINE_TYPE(VECTOR3, "vector3", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 3)
274TYPEDESC_DEFINE_TYPE(VECTOR4, "vector4", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 4)
275TYPEDESC_DEFINE_TYPE(COLOR3, "color3", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_COLOR, 3)
276TYPEDESC_DEFINE_TYPE(COLOR4, "color4", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_COLOR, 4)
277TYPEDESC_DEFINE_TYPE(MATRIX33, "matrix33", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 9)
278TYPEDESC_DEFINE_TYPE(MATRIX44, "matrix44", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 16)
279TYPEDESC_DEFINE_TYPE(STRING, "string", TypeDesc::BASETYPE_STRING, TypeDesc::SEMANTIC_NONE, 1)
280TYPEDESC_DEFINE_TYPE(FILENAME, "filename", TypeDesc::BASETYPE_STRING, TypeDesc::SEMANTIC_FILENAME, 1)
281TYPEDESC_DEFINE_TYPE(BSDF, "BSDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
282TYPEDESC_DEFINE_TYPE(EDF, "EDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
283TYPEDESC_DEFINE_TYPE(VDF, "VDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
284TYPEDESC_DEFINE_TYPE(SURFACESHADER, "surfaceshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
285TYPEDESC_DEFINE_TYPE(VOLUMESHADER, "volumeshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
286TYPEDESC_DEFINE_TYPE(DISPLACEMENTSHADER, "displacementshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
287TYPEDESC_DEFINE_TYPE(LIGHTSHADER, "lightshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
288TYPEDESC_DEFINE_TYPE(MATERIAL, "material", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_MATERIAL, 1)
289
290} // namespace Type
291
292MATERIALX_NAMESPACE_END
293
294#endif
The top-level Document class.
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:256
Generic value classes.
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition Value.h:30
Type descriptor for member of a struct type.
Definition TypeDesc.h:200
Data block holding large data needed by the type description.
Definition TypeDesc.h:69
A type descriptor for MaterialX data types.
Definition TypeDesc.h:40
constexpr TypeDesc(std::string_view name, uint8_t basetype, uint8_t semantic, uint16_t size, const DataBlock *data) noexcept
Constructor.
Definition TypeDesc.h:94
constexpr TypeDesc() noexcept
Empty constructor.
Definition TypeDesc.h:84
bool operator!=(TypeDesc rhs) const
Inequality operator.
Definition TypeDesc.h:157
bool operator==(TypeDesc rhs) const
Equality operator.
Definition TypeDesc.h:151
const string & getName() const
Return the name of the type.
bool isClosure() const
Return true if the type represents a closure.
Definition TypeDesc.h:141
bool isStruct() const
Return true if the type represents a struct.
Definition TypeDesc.h:144
bool operator<(TypeDesc rhs) const
Less-than operator.
Definition TypeDesc.h:163
bool isScalar() const
Return true if the type is a scalar type.
Definition TypeDesc.h:123
size_t getSize() const
Return the number of elements the type is composed of.
Definition TypeDesc.h:120
bool isFloat3() const
Return true if the type is an aggregate of 3 floats.
Definition TypeDesc.h:135
unsigned char getSemantic() const
Return the semantic for the type.
Definition TypeDesc.h:114
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:126
StructMemberDescVecPtr getStructMembers() const
Return a pointer to the struct member description.
bool isFloat2() const
Return true if the type is an aggregate of 2 floats.
Definition TypeDesc.h:132
bool isArray() const
Return true if the type is an array type.
Definition TypeDesc.h:129
uint32_t typeId() const
Return the unique id assigned to this type.
Definition TypeDesc.h:105
bool isFloat4() const
Return true if the type is an aggregate of 4 floats.
Definition TypeDesc.h:138
unsigned char getBaseType() const
Return the basetype for the type.
Definition TypeDesc.h:111
Class handling registration, storage and query of type descriptions.
Definition TypeDesc.h:224
void registerType(TypeDesc type)
Register an existing type decription.
const TypeDescVec & getTypes() const
Return all registered type descriptions.
Definition TypeDesc.h:240
void registerType(const string &name, uint8_t basetype, uint8_t semantic, uint16_t size, StructMemberDescVecPtr members=nullptr)
Create and register a new type description.
TypeDesc getType(const string &name) const
Return a type description by name.
static TypeSystemPtr create()
Create a new type system.
Hash operator.
Definition TypeDesc.h:170