HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// Type descriptor for a MaterialX data type.
11 
13 
15 
16 using ChannelMap = std::unordered_map<char, int>;
17 
18 /// @class TypeDesc
19 /// A type descriptor for MaterialX data types.
20 /// All types need to have a type descriptor registered in order for shader generators
21 /// to know about the type. A unique type descriptor pointer is the identifier used for
22 /// types, and can be used for type comparisons as well as getting more information
23 /// about the type. All standard library data types are registered by default and their
24 /// type descriptors can be accessed from the Type namespace, e.g. MaterialX::Type::FLOAT.
25 /// If custom types are used they must be registered by calling TypeDesc::registerType().
26 /// Descriptors for registered types can be retreived using TypeDesc::get(), see below.
28 {
29  public:
30  enum BaseType
31  {
38  BASETYPE_LAST
39  };
40 
41  enum Semantic
42  {
52  SEMANTIC_LAST
53  };
54 
55  /// Register a type descriptor for a MaterialX data type.
56  /// Throws an exception if a type with the same name is already registered.
57  static const TypeDesc* registerType(const string& name, unsigned char basetype, unsigned char semantic = SEMANTIC_NONE,
58  size_t size = 1, bool editable = true, const ChannelMap& channelMapping = ChannelMap());
59 
60  /// Equality operator overload
61  bool operator==(const TypeDesc& rhs) const;
62 
63  /// Inequality operator overload
64  bool operator!=(const TypeDesc& rhs) const;
65 
66  /// Get a type descriptor for given name.
67  /// Returns an empty shared pointer if no type with the given name is found.
68  static const TypeDesc* get(const string& name);
69 
70  /// Return the name of the type.
71  const string& getName() const { return _name; }
72 
73  /// Return the basetype for the type.
74  unsigned char getBaseType() const { return _basetype; }
75 
76  /// Returns the channel index for the supplied channel name.
77  /// Will return -1 on failure to find a matching index.
78  int getChannelIndex(char channel) const;
79 
80  /// Return the semantic for the type.
81  unsigned char getSemantic() const { return _semantic; }
82 
83  /// Return the number of elements the type is composed of.
84  /// Will return 1 for scalar types and a size greater than 1 for aggregate type.
85  /// For array types 0 is returned since the number of elements is undefined
86  /// until an array is instantiated.
87  size_t getSize() const { return _size; }
88 
89  /// Returns true if the type is editable by users.
90  /// Editable types are allowed to be published as shader uniforms
91  /// and hence must be presentable in a user interface.
92  bool isEditable() const { return _editable; }
93 
94  /// Return true if the type is a scalar type.
95  bool isScalar() const { return _size == 1; }
96 
97  /// Return true if the type is an aggregate type.
98  bool isAggregate() const { return _size > 1; }
99 
100  /// Return true if the type is an array type.
101  bool isArray() const { return _size == 0; }
102 
103  /// Return true if the type is an aggregate of 2 floats.
104  bool isFloat2() const { return _size == 2 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
105 
106  /// Return true if the type is an aggregate of 3 floats.
107  bool isFloat3() const { return _size == 3 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
108 
109  /// Return true if the type is an aggregate of 4 floats.
110  bool isFloat4() const { return _size == 4 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
111 
112  /// Return true if the type represents a closure.
113  bool isClosure() const { return (_semantic == SEMANTIC_CLOSURE || _semantic == SEMANTIC_SHADER || _semantic == SEMANTIC_MATERIAL); }
114 
115  private:
116  TypeDesc(const string& name, unsigned char basetype, unsigned char semantic, size_t size,
117  bool editable, const ChannelMap& channelMapping);
118 
119  const string _name;
120  const unsigned char _basetype;
121  const unsigned char _semantic;
122  const size_t _size;
123  const bool _editable;
124  const ChannelMap _channelMapping;
125 };
126 
127 namespace Type
128 {
129 
130 /// Type descriptors for all standard types.
131 /// These are always registered by default.
132 ///
133 /// TODO: Add support for the standard array types.
134 ///
135 extern MX_GENSHADER_API const TypeDesc* NONE;
136 extern MX_GENSHADER_API const TypeDesc* BOOLEAN;
137 extern MX_GENSHADER_API const TypeDesc* INTEGER;
139 extern MX_GENSHADER_API const TypeDesc* FLOAT;
140 extern MX_GENSHADER_API const TypeDesc* FLOATARRAY;
141 extern MX_GENSHADER_API const TypeDesc* VECTOR2;
142 extern MX_GENSHADER_API const TypeDesc* VECTOR3;
143 extern MX_GENSHADER_API const TypeDesc* VECTOR4;
144 extern MX_GENSHADER_API const TypeDesc* COLOR3;
145 extern MX_GENSHADER_API const TypeDesc* COLOR4;
146 extern MX_GENSHADER_API const TypeDesc* MATRIX33;
147 extern MX_GENSHADER_API const TypeDesc* MATRIX44;
148 extern MX_GENSHADER_API const TypeDesc* STRING;
149 extern MX_GENSHADER_API const TypeDesc* FILENAME;
150 extern MX_GENSHADER_API const TypeDesc* BSDF;
151 extern MX_GENSHADER_API const TypeDesc* EDF;
152 extern MX_GENSHADER_API const TypeDesc* VDF;
157 extern MX_GENSHADER_API const TypeDesc* MATERIAL;
158 
159 } // namespace Type
160 
162 
163 #endif
MX_GENSHADER_API const TypeDesc * FLOATARRAY
MX_GENSHADER_API const TypeDesc * MATRIX44
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
MX_GENSHADER_API const TypeDesc * SURFACESHADER
const string & getName() const
Return the name of the type.
Definition: TypeDesc.h:71
unsigned char getSemantic() const
Return the semantic for the type.
Definition: TypeDesc.h:81
MX_GENSHADER_API const TypeDesc * BSDF
MX_GENSHADER_API const TypeDesc * VOLUMESHADER
MX_GENSHADER_API const TypeDesc * VECTOR3
#define MX_GENSHADER_API
Definition: Export.h:18
MX_GENSHADER_API const TypeDesc * MATERIAL
MX_GENSHADER_API const TypeDesc * COLOR4
MX_GENSHADER_API const TypeDesc * MATRIX33
MX_GENSHADER_API const TypeDesc * COLOR3
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
bool isEditable() const
Definition: TypeDesc.h:92
unsigned char getBaseType() const
Return the basetype for the type.
Definition: TypeDesc.h:74
bool isClosure() const
Return true if the type represents a closure.
Definition: TypeDesc.h:113
MX_GENSHADER_API const TypeDesc * VDF
MX_GENSHADER_API const TypeDesc * LIGHTSHADER
bool isFloat2() const
Return true if the type is an aggregate of 2 floats.
Definition: TypeDesc.h:104
GLuint const GLchar * name
Definition: glcorearb.h:786
MX_GENSHADER_API const TypeDesc * STRING
MX_GENSHADER_API const TypeDesc * VECTOR2
MX_GENSHADER_API const TypeDesc * BOOLEAN
MX_GENSHADER_API const TypeDesc * NONE
bool isAggregate() const
Return true if the type is an aggregate type.
Definition: TypeDesc.h:98
size_t getSize() const
Definition: TypeDesc.h:87
MX_GENSHADER_API const TypeDesc * INTEGERARRAY
GLsizeiptr size
Definition: glcorearb.h:664
MX_GENSHADER_API const TypeDesc * DISPLACEMENTSHADER
MX_GENSHADER_API const TypeDesc * VECTOR4
bool isFloat3() const
Return true if the type is an aggregate of 3 floats.
Definition: TypeDesc.h:107
MX_GENSHADER_API const TypeDesc * EDF
MX_GENSHADER_API const TypeDesc * FLOAT
Definition: ImfPixelType.h:24
bool isScalar() const
Return true if the type is a scalar type.
Definition: TypeDesc.h:95
MX_GENSHADER_API const TypeDesc * FILENAME
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
MX_GENSHADER_API const TypeDesc * INTEGER
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
std::unordered_map< char, int > ChannelMap
Definition: TypeDesc.h:16
bool isArray() const
Return true if the type is an array type.
Definition: TypeDesc.h:101
bool isFloat4() const
Return true if the type is an aggregate of 4 floats.
Definition: TypeDesc.h:110