MaterialX 1.39.1
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
14#include <string_view>
15
16MATERIALX_NAMESPACE_BEGIN
17
34class MX_GENSHADER_API TypeDesc
35{
36 public:
37 enum BaseType
38 {
39 BASETYPE_NONE,
40 BASETYPE_BOOLEAN,
41 BASETYPE_INTEGER,
42 BASETYPE_FLOAT,
43 BASETYPE_STRING,
44 BASETYPE_STRUCT,
45 BASETYPE_LAST
46 };
47
48 enum Semantic
49 {
50 SEMANTIC_NONE,
51 SEMANTIC_COLOR,
52 SEMANTIC_VECTOR,
53 SEMANTIC_MATRIX,
54 SEMANTIC_FILENAME,
55 SEMANTIC_CLOSURE,
56 SEMANTIC_SHADER,
57 SEMANTIC_MATERIAL,
58 SEMANTIC_ENUM,
59 SEMANTIC_LAST
60 };
61
63 constexpr TypeDesc() noexcept :
64 _id(0), _basetype(BASETYPE_NONE), _semantic(SEMANTIC_NONE), _size(0) { }
65
67 constexpr TypeDesc(std::string_view name, uint8_t basetype, uint8_t semantic = SEMANTIC_NONE, uint16_t size = 1) noexcept :
68 _id(constexpr_hash(name)), // Note: We only store the hash to keep the class size minimal.
69 _basetype(basetype),
70 _semantic(semantic),
71 _size(size)
72 {
73 }
74
77 uint32_t typeId() const { return _id; }
78
80 const string& getName() const;
81
83 unsigned char getBaseType() const { return _basetype; }
84
86 unsigned char getSemantic() const { return _semantic; }
87
92 size_t getSize() const { return _size; }
93
95 bool isScalar() const { return _size == 1; }
96
98 bool isAggregate() const { return _size > 1; }
99
101 bool isArray() const { return _size == 0; }
102
104 bool isFloat2() const { return _size == 2 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
105
107 bool isFloat3() const { return _size == 3 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
108
110 bool isFloat4() const { return _size == 4 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
111
113 bool isClosure() const { return (_semantic == SEMANTIC_CLOSURE || _semantic == SEMANTIC_SHADER || _semantic == SEMANTIC_MATERIAL); }
114
116 bool operator==(TypeDesc rhs) const
117 {
118 return _id == rhs._id;
119 }
120
122 bool operator!=(TypeDesc rhs) const
123 {
124 return _id != rhs._id;
125 }
126
128 bool operator<(TypeDesc rhs) const
129 {
130 return _id < rhs._id;
131 }
132
134 struct Hasher
135 {
136 size_t operator()(const TypeDesc& t) const
137 {
138 return t._id;
139 }
140 };
141
144 static TypeDesc get(const string& name);
145
146 static const string NONE_TYPE_NAME;
147
148 private:
151 constexpr uint32_t constexpr_hash(std::string_view str, uint32_t n = 0, uint32_t h = 2166136261)
152 {
153 return n == uint32_t(str.size()) ? h : constexpr_hash(str, n + 1, (h * 16777619) ^ (str[n]));
154 }
155
156 uint32_t _id;
157 uint8_t _basetype;
158 uint8_t _semantic;
159 uint16_t _size;
160};
161
164class MX_GENSHADER_API TypeDescRegistry
165{
166 public:
167 TypeDescRegistry(TypeDesc type, const string& name);
168};
169
171#define TYPEDESC_DEFINE_TYPE(T, name, basetype, semantic, size) \
172 static constexpr TypeDesc T(name, basetype, semantic, size);
173
176#define TYPEDESC_REGISTER_TYPE(T, name) \
177 TypeDescRegistry register_##T(T, name);
178
179namespace Type
180{
181
182//
184//
185TYPEDESC_DEFINE_TYPE(NONE, "none", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_NONE, 1)
186TYPEDESC_DEFINE_TYPE(BOOLEAN, "boolean", TypeDesc::BASETYPE_BOOLEAN, TypeDesc::SEMANTIC_NONE, 1)
187TYPEDESC_DEFINE_TYPE(INTEGER, "integer", TypeDesc::BASETYPE_INTEGER, TypeDesc::SEMANTIC_NONE, 1)
188TYPEDESC_DEFINE_TYPE(FLOAT, "float", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_NONE, 1)
189TYPEDESC_DEFINE_TYPE(INTEGERARRAY, "integerarray", TypeDesc::BASETYPE_INTEGER, TypeDesc::SEMANTIC_NONE, 0)
190TYPEDESC_DEFINE_TYPE(FLOATARRAY, "floatarray", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_NONE, 0)
191TYPEDESC_DEFINE_TYPE(VECTOR2, "vector2", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 2)
192TYPEDESC_DEFINE_TYPE(VECTOR3, "vector3", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 3)
193TYPEDESC_DEFINE_TYPE(VECTOR4, "vector4", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_VECTOR, 4)
194TYPEDESC_DEFINE_TYPE(COLOR3, "color3", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_COLOR, 3)
195TYPEDESC_DEFINE_TYPE(COLOR4, "color4", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_COLOR, 4)
196TYPEDESC_DEFINE_TYPE(MATRIX33, "matrix33", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 9)
197TYPEDESC_DEFINE_TYPE(MATRIX44, "matrix44", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 16)
198TYPEDESC_DEFINE_TYPE(STRING, "string", TypeDesc::BASETYPE_STRING, TypeDesc::SEMANTIC_NONE, 1)
199TYPEDESC_DEFINE_TYPE(FILENAME, "filename", TypeDesc::BASETYPE_STRING, TypeDesc::SEMANTIC_FILENAME, 1)
200TYPEDESC_DEFINE_TYPE(BSDF, "BSDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
201TYPEDESC_DEFINE_TYPE(EDF, "EDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
202TYPEDESC_DEFINE_TYPE(VDF, "VDF", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_CLOSURE, 1)
203TYPEDESC_DEFINE_TYPE(SURFACESHADER, "surfaceshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
204TYPEDESC_DEFINE_TYPE(VOLUMESHADER, "volumeshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
205TYPEDESC_DEFINE_TYPE(DISPLACEMENTSHADER, "displacementshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
206TYPEDESC_DEFINE_TYPE(LIGHTSHADER, "lightshader", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_SHADER, 1)
207TYPEDESC_DEFINE_TYPE(MATERIAL, "material", TypeDesc::BASETYPE_NONE, TypeDesc::SEMANTIC_MATERIAL, 1)
208
209} // namespace Type
210
211MATERIALX_NAMESPACE_END
212
213#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:171
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:35
constexpr TypeDesc() noexcept
Empty constructor.
Definition: TypeDesc.h:63
bool operator!=(TypeDesc rhs) const
Inequality operator.
Definition: TypeDesc.h:122
bool operator==(TypeDesc rhs) const
Equality operator.
Definition: TypeDesc.h:116
const string & getName() const
Return the name of the type.
bool isClosure() const
Return true if the type represents a closure.
Definition: TypeDesc.h:113
bool operator<(TypeDesc rhs) const
Less-than operator.
Definition: TypeDesc.h:128
bool isScalar() const
Return true if the type is a scalar type.
Definition: TypeDesc.h:95
size_t getSize() const
Return the number of elements the type is composed of.
Definition: TypeDesc.h:92
bool isFloat3() const
Return true if the type is an aggregate of 3 floats.
Definition: TypeDesc.h:107
unsigned char getSemantic() const
Return the semantic for the type.
Definition: TypeDesc.h:86
constexpr TypeDesc(std::string_view name, uint8_t basetype, uint8_t semantic=SEMANTIC_NONE, uint16_t size=1) noexcept
Constructor.
Definition: TypeDesc.h:67
bool isAggregate() const
Return true if the type is an aggregate type.
Definition: TypeDesc.h:98
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:104
bool isArray() const
Return true if the type is an array type.
Definition: TypeDesc.h:101
uint32_t typeId() const
Return the unique id assigned to this type.
Definition: TypeDesc.h:77
bool isFloat4() const
Return true if the type is an aggregate of 4 floats.
Definition: TypeDesc.h:110
unsigned char getBaseType() const
Return the basetype for the type.
Definition: TypeDesc.h:83
Helper class for type registration.
Definition: TypeDesc.h:165
Hash operator.
Definition: TypeDesc.h:135