6#ifndef MATERIALX_TYPEDESC_H
7#define MATERIALX_TYPEDESC_H
18MATERIALX_NAMESPACE_BEGIN
22using TypeDescVec = vector<TypeDesc>;
23using TypeDescMap = std::unordered_map<string, TypeDesc>;
24using StructMemberDescVec = vector<StructMemberDesc>;
25using StructMemberDescVecPtr = shared_ptr<const StructMemberDescVec>;
71 DataBlock(
const string& name,
const StructMemberDescVecPtr members =
nullptr) noexcept : _name(name), _members(members) {}
73 const string& getName()
const {
return _name; }
74 StructMemberDescVecPtr getStructMembers()
const {
return _members; }
78 const StructMemberDescVecPtr _members;
81 using DataBlockPtr = std::shared_ptr<DataBlock>;
86 _basetype(BASETYPE_NONE),
87 _semantic(SEMANTIC_NONE),
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)),
132 bool isFloat2()
const {
return _size == 2 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
135 bool isFloat3()
const {
return _size == 3 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
138 bool isFloat4()
const {
return _size == 4 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
141 bool isClosure()
const {
return (_semantic == SEMANTIC_CLOSURE || _semantic == SEMANTIC_SHADER || _semantic == SEMANTIC_MATERIAL); }
144 bool isStruct()
const {
return _basetype == BASETYPE_STRUCT; }
153 return _id == rhs._id;
159 return _id != rhs._id;
165 return _id < rhs._id;
171 size_t operator()(
const TypeDesc& t)
const
177 static const string NONE_TYPE_NAME;
185 constexpr uint32_t constexpr_hash(std::string_view str, uint32_t n = 0, uint32_t h = 2166136261)
187 return n == uint32_t(str.size()) ? h : constexpr_hash(str, n + 1, (h * 16777619) ^ (str[n]));
194 const DataBlock* _data;
199class StructMemberDesc
202 StructMemberDesc(
TypeDesc type,
const string& name,
const string& defaultValueStr) :
205 _defaultValueStr(defaultValueStr)
209 TypeDesc getType()
const {
return _type; }
210 const string& getName()
const {
return _name; }
211 const string& getDefaultValueStr()
const {
return _defaultValueStr; }
216 const string _defaultValueStr;
219using TypeSystemPtr = shared_ptr<class TypeSystem>;
223class MX_GENSHADER_API TypeSystem
233 void registerType(
const string& name, uint8_t basetype, uint8_t semantic, uint16_t size, StructMemberDescVecPtr members =
nullptr);
251 TypeDescMap _typesByName;
252 vector<TypeDesc::DataBlockPtr> _dataBlocks;
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());
270TYPEDESC_DEFINE_TYPE(INTEGERARRAY,
"integerarray", TypeDesc::BASETYPE_INTEGER, TypeDesc::SEMANTIC_NONE, 0)
271TYPEDESC_DEFINE_TYPE(FLOATARRAY,
"floatarray", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_NONE, 0)
277TYPEDESC_DEFINE_TYPE(MATRIX33,
"matrix33", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 9)
278TYPEDESC_DEFINE_TYPE(MATRIX44,
"matrix44", TypeDesc::BASETYPE_FLOAT, TypeDesc::SEMANTIC_MATRIX, 16)
280TYPEDESC_DEFINE_TYPE(FILENAME,
"filename", TypeDesc::BASETYPE_STRING, TypeDesc::SEMANTIC_FILENAME, 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)
292MATERIALX_NAMESPACE_END
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
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