HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Shader.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_SHADER_H
7 #define MATERIALX_SHADER_H
8 
9 /// @file
10 /// Shader instance class created during shader generation
11 
13 
17 
19 
20 class ShaderGenerator;
21 class Shader;
22 
23 /// @class Shader
24 /// Class containing all data needed during shader generation.
25 /// After generation is completed it will contain the resulting source code
26 /// emitted by shader generators.
27 ///
28 /// The class contains a default implementation using a single shader stage.
29 /// Derived shaders can override this, as well as overriding all methods
30 /// that add code to the shader.
31 ///
33 {
34  public:
35  /// Constructor
36  Shader(const string& name, ShaderGraphPtr graph);
37 
38  /// Destructor
39  virtual ~Shader() { }
40 
41  /// Return the shader name
42  const string& getName() const { return _name; }
43 
44  /// Return the number of shader stages for this shader.
45  size_t numStages() const { return _stages.size(); }
46 
47  /// Return a stage by index.
48  ShaderStage& getStage(size_t index);
49 
50  /// Return a stage by index.
51  const ShaderStage& getStage(size_t index) const;
52 
53  /// Return if stage exists.
54  bool hasStage(const string& name);
55 
56  /// Return a stage by name.
57  ShaderStage& getStage(const string& name);
58 
59  /// Return a stage by name.
60  const ShaderStage& getStage(const string& name) const;
61 
62  /// Return true if the shader has a given named attribute.
63  bool hasAttribute(const string& attrib) const
64  {
65  return _attributeMap.count(attrib) != 0;
66  }
67 
68  /// Return the value for a named attribute,
69  /// or nullptr if no such attribute is found.
70  ValuePtr getAttribute(const string& attrib) const
71  {
72  auto it = _attributeMap.find(attrib);
73  return it != _attributeMap.end() ? it->second : nullptr;
74  }
75 
76  /// Set a value attribute on the shader.
77  void setAttribute(const string& attrib, ValuePtr value)
78  {
79  _attributeMap[attrib] = value;
80  }
81 
82  /// Set a flag attribute on the shader.
83  void setAttribute(const string& attrib)
84  {
85  _attributeMap[attrib] = Value::createValue<bool>(true);
86  }
87 
88  /// Return the shader graph.
89  const ShaderGraph& getGraph() const { return *_graph; }
90 
91  /// Return the shader graph.
92  ShaderGraph& getGraph() { return *_graph; }
93 
94  /// Return true if this shader matches the given classification.
95  bool hasClassification(unsigned int c) const { return _graph->hasClassification(c); }
96 
97  /// Set the shader source code for a given shader stage.
98  void setSourceCode(const string& code, const string& stage = Stage::PIXEL) { getStage(stage).setSourceCode(code); }
99 
100  /// Return the shader source code for a given shader stage.
101  const string& getSourceCode(const string& stage = Stage::PIXEL) const { return getStage(stage).getSourceCode(); }
102 
103  protected:
104  /// Create a new stage in the shader.
105  ShaderStagePtr createStage(const string& name, ConstSyntaxPtr syntax);
106 
107  string _name;
109  std::unordered_map<string, ShaderStagePtr> _stagesMap;
110  vector<ShaderStage*> _stages;
111  std::unordered_map<string, ValuePtr> _attributeMap;
112 
113  friend class ShaderGenerator;
114 };
115 
117 
118 #endif
bool hasClassification(unsigned int c) const
Return true if this shader matches the given classification.
Definition: Shader.h:95
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
ValuePtr getAttribute(const string &attrib) const
Definition: Shader.h:70
GLsizei const GLfloat * value
Definition: glcorearb.h:824
ShaderGraphPtr _graph
Definition: Shader.h:108
string _name
Definition: Shader.h:107
shared_ptr< const Syntax > ConstSyntaxPtr
Shared pointer to a constant Syntax.
Definition: Syntax.h:28
#define MX_GENSHADER_API
Definition: Export.h:18
void setSourceCode(const string &code, const string &stage=Stage::PIXEL)
Set the shader source code for a given shader stage.
Definition: Shader.h:98
bool hasAttribute(const string &attrib) const
Return true if the shader has a given named attribute.
Definition: Shader.h:63
size_t numStages() const
Return the number of shader stages for this shader.
Definition: Shader.h:45
std::unordered_map< string, ShaderStagePtr > _stagesMap
Definition: Shader.h:109
vector< ShaderStage * > _stages
Definition: Shader.h:110
ShaderGraph & getGraph()
Return the shader graph.
Definition: Shader.h:92
std::unordered_map< string, ValuePtr > _attributeMap
Definition: Shader.h:111
virtual ~Shader()
Destructor.
Definition: Shader.h:39
GLuint const GLchar * name
Definition: glcorearb.h:786
shared_ptr< class ShaderGraph > ShaderGraphPtr
A shared pointer to a shader graph.
Definition: ShaderGraph.h:39
MX_GENSHADER_API const string PIXEL
Definition: Shader.h:32
void setAttribute(const string &attrib)
Set a flag attribute on the shader.
Definition: Shader.h:83
shared_ptr< ShaderStage > ShaderStagePtr
Shared pointer to a ShaderStage.
Definition: Library.h:36
GLuint index
Definition: glcorearb.h:786
void setAttribute(const string &attrib, ValuePtr value)
Set a value attribute on the shader.
Definition: Shader.h:77
Definition: core.h:1131
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
const string & getName() const
Return the shader name.
Definition: Shader.h:42
const ShaderGraph & getGraph() const
Return the shader graph.
Definition: Shader.h:89
const string & getSourceCode(const string &stage=Stage::PIXEL) const
Return the shader source code for a given shader stage.
Definition: Shader.h:101
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29