MaterialX 1.39.1
Loading...
Searching...
No Matches
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
11
13
17
18MATERIALX_NAMESPACE_BEGIN
19
20class ShaderGenerator;
21class Shader;
22
32class MX_GENSHADER_API Shader
33{
34 public:
36 Shader(const string& name, ShaderGraphPtr graph);
37
39 virtual ~Shader() { }
40
42 const string& getName() const { return _name; }
43
45 size_t numStages() const { return _stages.size(); }
46
48 ShaderStage& getStage(size_t index);
49
51 const ShaderStage& getStage(size_t index) const;
52
54 bool hasStage(const string& name);
55
57 ShaderStage& getStage(const string& name);
58
60 const ShaderStage& getStage(const string& name) const;
61
63 bool hasAttribute(const string& attrib) const
64 {
65 return _attributeMap.count(attrib) != 0;
66 }
67
70 ValuePtr getAttribute(const string& attrib) const
71 {
72 auto it = _attributeMap.find(attrib);
73 return it != _attributeMap.end() ? it->second : nullptr;
74 }
75
77 void setAttribute(const string& attrib, ValuePtr value)
78 {
79 _attributeMap[attrib] = value;
80 }
81
83 void setAttribute(const string& attrib)
84 {
85 _attributeMap[attrib] = Value::createValue<bool>(true);
86 }
87
89 const ShaderGraph& getGraph() const { return *_graph; }
90
92 ShaderGraph& getGraph() { return *_graph; }
93
95 bool hasClassification(unsigned int c) const { return _graph->hasClassification(c); }
96
98 void setSourceCode(const string& code, const string& stage = Stage::PIXEL) { getStage(stage).setSourceCode(code); }
99
101 const string& getSourceCode(const string& stage = Stage::PIXEL) const { return getStage(stage).getSourceCode(); }
102
103 protected:
105 ShaderStagePtr createStage(const string& name, ConstSyntaxPtr syntax);
106
107 string _name;
108 ShaderGraphPtr _graph;
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
116MATERIALX_NAMESPACE_END
117
118#endif
Shader generation options class.
Macros for declaring imported and exported symbols.
shared_ptr< ShaderStage > ShaderStagePtr
Shared pointer to a ShaderStage.
Definition: Library.h:36
Shader graph class.
shared_ptr< class ShaderGraph > ShaderGraphPtr
A shared pointer to a shader graph.
Definition: ShaderGraph.h:39
Class related to holding information for shader stages.
shared_ptr< const Syntax > ConstSyntaxPtr
Shared pointer to a constant Syntax.
Definition: Syntax.h:29
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
Base class for shader generators All third-party shader generators should derive from this class.
Definition: ShaderGenerator.h:31
Class representing a graph (DAG) for shader generation.
Definition: ShaderGraph.h:44
Class containing all data needed during shader generation.
Definition: Shader.h:33
void setAttribute(const string &attrib)
Set a flag attribute on the shader.
Definition: Shader.h:83
const ShaderGraph & getGraph() const
Return the shader graph.
Definition: Shader.h:89
const ShaderStage & getStage(size_t index) const
Return a stage by index.
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 hasStage(const string &name)
Return if stage exists.
size_t numStages() const
Return the number of shader stages for this shader.
Definition: Shader.h:45
bool hasClassification(unsigned int c) const
Return true if this shader matches the given classification.
Definition: Shader.h:95
const string & getName() const
Return the shader name.
Definition: Shader.h:42
ShaderStagePtr createStage(const string &name, ConstSyntaxPtr syntax)
Create a new stage in the shader.
virtual ~Shader()
Destructor.
Definition: Shader.h:39
ShaderStage & getStage(const string &name)
Return a stage by name.
void setAttribute(const string &attrib, ValuePtr value)
Set a value attribute on the shader.
Definition: Shader.h:77
const string & getSourceCode(const string &stage=Stage::PIXEL) const
Return the shader source code for a given shader stage.
Definition: Shader.h:101
const ShaderStage & getStage(const string &name) const
Return a stage by name.
ShaderStage & getStage(size_t index)
Return a stage by index.
Shader(const string &name, ShaderGraphPtr graph)
Constructor.
ShaderGraph & getGraph()
Return the shader graph.
Definition: Shader.h:92
ValuePtr getAttribute(const string &attrib) const
Return the value for a named attribute, or nullptr if no such attribute is found.
Definition: Shader.h:70
bool hasAttribute(const string &attrib) const
Return true if the shader has a given named attribute.
Definition: Shader.h:63
A shader stage, containing the state and resulting source code for the stage.
Definition: ShaderStage.h:139