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  /// Get a type descriptor for given name.
61  /// Returns an empty shared pointer if no type with the given name is found.
62  static const TypeDesc* get(const string& name);
63 
64  /// Return the name of the type.
65  const string& getName() const { return _name; }
66 
67  /// Return the basetype for the type.
68  unsigned char getBaseType() const { return _basetype; }
69 
70  /// Returns the channel index for the supplied channel name.
71  /// Will return -1 on failure to find a matching index.
72  int getChannelIndex(char channel) const;
73 
74  /// Return the semantic for the type.
75  unsigned char getSemantic() const { return _semantic; }
76 
77  /// Return the number of elements the type is composed of.
78  /// Will return 1 for scalar types and a size greater than 1 for aggregate type.
79  /// For array types 0 is returned since the number of elements is undefined
80  /// until an array is instantiated.
81  size_t getSize() const { return _size; }
82 
83  /// Returns true if the type is editable by users.
84  /// Editable types are allowed to be published as shader uniforms
85  /// and hence must be presentable in a user interface.
86  bool isEditable() const { return _editable; }
87 
88  /// Return true if the type is a scalar type.
89  bool isScalar() const { return _size == 1; }
90 
91  /// Return true if the type is an aggregate type.
92  bool isAggregate() const { return _size > 1; }
93 
94  /// Return true if the type is an array type.
95  bool isArray() const { return _size == 0; }
96 
97  /// Return true if the type is an aggregate of 2 floats.
98  bool isFloat2() const { return _size == 2 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
99 
100  /// Return true if the type is an aggregate of 3 floats.
101  bool isFloat3() const { return _size == 3 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
102 
103  /// Return true if the type is an aggregate of 4 floats.
104  bool isFloat4() const { return _size == 4 && (_semantic == SEMANTIC_COLOR || _semantic == SEMANTIC_VECTOR); }
105 
106  /// Return true if the type represents a closure.
107  bool isClosure() const { return (_semantic == SEMANTIC_CLOSURE || _semantic == SEMANTIC_SHADER || _semantic == SEMANTIC_MATERIAL); }
108 
109  private:
110  TypeDesc(const string& name, unsigned char basetype, unsigned char semantic, size_t size,
111  bool editable, const ChannelMap& channelMapping);
112 
113  const string _name;
114  const unsigned char _basetype;
115  const unsigned char _semantic;
116  const size_t _size;
117  const bool _editable;
118  const ChannelMap _channelMapping;
119 };
120 
121 namespace Type
122 {
123 
124 /// Type descriptors for all standard types.
125 /// These are always registered by default.
126 ///
127 /// TODO: Add support for the standard array types.
128 ///
129 extern MX_GENSHADER_API const TypeDesc* NONE;
130 extern MX_GENSHADER_API const TypeDesc* BOOLEAN;
131 extern MX_GENSHADER_API const TypeDesc* INTEGER;
133 extern MX_GENSHADER_API const TypeDesc* FLOAT;
134 extern MX_GENSHADER_API const TypeDesc* FLOATARRAY;
135 extern MX_GENSHADER_API const TypeDesc* VECTOR2;
136 extern MX_GENSHADER_API const TypeDesc* VECTOR3;
137 extern MX_GENSHADER_API const TypeDesc* VECTOR4;
138 extern MX_GENSHADER_API const TypeDesc* COLOR3;
139 extern MX_GENSHADER_API const TypeDesc* COLOR4;
140 extern MX_GENSHADER_API const TypeDesc* MATRIX33;
141 extern MX_GENSHADER_API const TypeDesc* MATRIX44;
142 extern MX_GENSHADER_API const TypeDesc* STRING;
143 extern MX_GENSHADER_API const TypeDesc* FILENAME;
144 extern MX_GENSHADER_API const TypeDesc* BSDF;
145 extern MX_GENSHADER_API const TypeDesc* EDF;
146 extern MX_GENSHADER_API const TypeDesc* VDF;
151 extern MX_GENSHADER_API const TypeDesc* MATERIAL;
152 
153 } // namespace Type
154 
156 
157 #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:65
unsigned char getSemantic() const
Return the semantic for the type.
Definition: TypeDesc.h:75
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 isEditable() const
Definition: TypeDesc.h:86
unsigned char getBaseType() const
Return the basetype for the type.
Definition: TypeDesc.h:68
bool isClosure() const
Return true if the type represents a closure.
Definition: TypeDesc.h:107
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:98
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:92
size_t getSize() const
Definition: TypeDesc.h:81
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:101
MX_GENSHADER_API const TypeDesc * EDF
MX_GENSHADER_API const TypeDesc * FLOAT
Definition: ImfPixelType.h:26
bool isScalar() const
Return true if the type is a scalar type.
Definition: TypeDesc.h:89
MX_GENSHADER_API const TypeDesc * FILENAME
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
MX_GENSHADER_API const TypeDesc * INTEGER
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:95
bool isFloat4() const
Return true if the type is an aggregate of 4 floats.
Definition: TypeDesc.h:104