MaterialX 1.39.1
Loading...
Searching...
No Matches
ShaderStage.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_SHADERSTAGE_H
7#define MATERIALX_SHADERSTAGE_H
8
11
13
17
19
20#include <MaterialXCore/Node.h>
21
22#include <sstream>
23
24// Restrict a scoped block of statements to a specific shader stage, as
25// is required for multi-stage shading languages. Statements within
26// the block will only be emitted when processing the given stage.
27#define DEFINE_SHADER_STAGE(stage, name) if (stage.getName() == name)
28
29// These macros are deprecated, and should be replaced with DEFINE_SHADER_STAGE.
30#define BEGIN_SHADER_STAGE(stage, name) \
31 if (stage.getName() == name) \
32 {
33#define END_SHADER_STAGE(stage, name) }
34
35MATERIALX_NAMESPACE_BEGIN
36
37namespace Stage
38{
39
46extern MX_GENSHADER_API const string PIXEL;
47
48} // namespace Stage
49
50class VariableBlock;
52using VariableBlockPtr = std::shared_ptr<VariableBlock>;
54using VariableBlockMap = std::unordered_map<string, VariableBlockPtr>;
56using ShaderPortPredicate = std::function<bool(ShaderPort*)>;
57
60class MX_GENSHADER_API VariableBlock
61{
62 public:
63 VariableBlock(const string& name, const string& instance) :
64 _name(name),
65 _instance(instance)
66 {
67 }
68
70 const string& getName() const { return _name; }
71
73 void setName(const string& name) { _name = name; }
74
76 const string& getInstance() const { return _instance; }
77
79 void setInstance(const string& instance) { _instance = instance; }
80
82 bool empty() const { return _variableOrder.empty(); }
83
85 size_t size() const { return _variableOrder.size(); }
86
88 ShaderPort* operator[](size_t index) { return _variableOrder[index]; }
89
91 const ShaderPort* operator[](size_t index) const { return _variableOrder[index]; }
92
94 const vector<ShaderPort*>& getVariableOrder() const { return _variableOrder; }
95
98 ShaderPort* operator[](const string& name);
99
102 const ShaderPort* operator[](const string& name) const;
103
106 ShaderPort* find(const string& name);
107
110 const ShaderPort* find(const string& name) const;
111
114
123 ShaderPort* add(TypeDesc type, const string& name, ValuePtr value = nullptr, bool shouldWiden = false);
124
126 void add(ShaderPortPtr port);
127
128 private:
129 string _name;
130 string _instance;
131 std::unordered_map<string, ShaderPortPtr> _variableMap;
132 vector<ShaderPort*> _variableOrder;
133};
134
138class MX_GENSHADER_API ShaderStage
139{
140 public:
141 using FunctionCallId = std::pair<const ShaderNode*, int>;
142 struct Scope
143 {
144 Syntax::Punctuation punctuation;
145 std::set<FunctionCallId> functions;
147 punctuation(p) { }
148 };
149
150 public:
152 ShaderStage(const string& name, ConstSyntaxPtr syntax);
153
155 const string& getName() const { return _name; }
156
158 const string& getFunctionName() const { return _functionName; }
159
161 void setSourceCode(const string& code) { _code = code; }
162
164 const string& getSourceCode() const { return _code; }
165
167 VariableBlockPtr createUniformBlock(const string& name, const string& instance = EMPTY_STRING);
168
170 VariableBlockPtr createInputBlock(const string& name, const string& instance = EMPTY_STRING);
171
173 VariableBlockPtr createOutputBlock(const string& name, const string& instance = EMPTY_STRING);
174
176 VariableBlock& getUniformBlock(const string& name);
177
179 const VariableBlock& getUniformBlock(const string& name) const;
180
182 VariableBlock& getInputBlock(const string& name);
183
185 const VariableBlock& getInputBlock(const string& name) const;
186
188 VariableBlock& getOutputBlock(const string& name);
189
191 const VariableBlock& getOutputBlock(const string& name) const;
192
195
198
201 {
202 return _uniforms;
203 }
204
207 {
208 return _inputs;
209 }
210
213 {
214 return _outputs;
215 }
216
218 const StringSet& getIncludes() const
219 {
220 return _includes;
221 }
222
225 {
226 return _sourceDependencies;
227 }
228
230 void beginScope(Syntax::Punctuation punc = Syntax::CURLY_BRACKETS);
231
233 void endScope(bool semicolon = false, bool newline = true);
234
236 void beginLine();
237
239 void endLine(bool semicolon = true);
240
242 void newLine();
243
245 void addString(const string& str);
246
248 void addLine(const string& str, bool semicolon = true);
249
251 void addComment(const string& str);
252
254 void addBlock(const string& str, const FilePath& sourceFilename, GenContext& context);
255
257 void addInclude(const FilePath& includeFilename, const FilePath& sourceFilename, GenContext& context);
258
260 bool hasSourceDependency(const FilePath& file);
261
263 void addSourceDependency(const FilePath& file);
264
266 template <typename T>
267 void addValue(const T& value)
268 {
269 StringStream str;
270 str << value;
271 _code += str.str();
272 }
273
275 void addFunctionDefinition(const ShaderNode& node, GenContext& context);
276
281 void addFunctionCall(const ShaderNode& node, GenContext& context, bool emitCode = true);
282
284 bool isEmitted(const ShaderNode& node, GenContext& context) const;
285
287 void setFunctionName(const string& functionName)
288 {
289 _functionName = functionName;
290 }
291
292 private:
294 const string _name;
295
297 string _functionName;
298
300 ConstSyntaxPtr _syntax;
301
303 int _indentations;
304
306 vector<Scope> _scopes;
307
309 StringSet _includes;
310
312 StringSet _sourceDependencies;
313
315 std::set<size_t> _definedFunctions;
316
318 VariableBlock _constants;
319
321 VariableBlockMap _uniforms;
322
324 VariableBlockMap _inputs;
325
327 VariableBlockMap _outputs;
328
330 string _code;
331
332 friend class ShaderGenerator;
333};
334
336using ShaderStagePtr = std::shared_ptr<ShaderStage>;
337
339inline ShaderPort* addStageUniform(const string& block,
340 TypeDesc type,
341 const string& name,
342 ShaderStage& stage)
343{
344 VariableBlock& uniforms = stage.getUniformBlock(block);
345 return uniforms.add(type, name);
346}
347[[deprecated]] inline ShaderPort* addStageUniform(const string& block,
348 const TypeDesc* type,
349 const string& name,
350 ShaderStage& stage)
351{
352 return addStageUniform(block, *type, name, stage);
353}
354
356inline ShaderPort* addStageInput(const string& block,
357 TypeDesc type,
358 const string& name,
359 ShaderStage& stage,
360 bool shouldWiden = false)
361{
362 VariableBlock& inputs = stage.getInputBlock(block);
363 return inputs.add(type, name, {}, shouldWiden);
364}
365[[deprecated]] inline ShaderPort* addStageInput(const string& block,
366 const TypeDesc* type,
367 const string& name,
368 ShaderStage& stage,
369 bool shouldWiden = false)
370{
371 return addStageInput(block, *type, name, stage, shouldWiden);
372}
373
375inline ShaderPort* addStageOutput(const string& block,
376 TypeDesc type,
377 const string& name,
378 ShaderStage& stage,
379 bool shouldWiden = false)
380{
381 VariableBlock& outputs = stage.getOutputBlock(block);
382 return outputs.add(type, name, {}, shouldWiden);
383}
384[[deprecated]] inline ShaderPort* addStageOutput(const string& block,
385 const TypeDesc* type,
386 const string& name,
387 ShaderStage& stage,
388 bool shouldWiden = false)
389{
390 return addStageOutput(block, *type, name, stage, shouldWiden);
391}
392
394inline void addStageConnectorBlock(const string& block,
395 const string& instance,
396 ShaderStage& from,
397 ShaderStage& to)
398{
399 from.createOutputBlock(block, instance);
400 to.createInputBlock(block, instance);
401}
402
404inline void addStageConnector(const string& block,
405 TypeDesc type,
406 const string& name,
407 ShaderStage& from,
408 ShaderStage& to,
409 bool shouldWiden = false)
410{
411 addStageOutput(block, type, name, from, shouldWiden);
412 addStageInput(block, type, name, to, shouldWiden);
413}
414[[deprecated]] inline void addStageConnector(const string& block,
415 const TypeDesc* type,
416 const string& name,
417 ShaderStage& from,
418 ShaderStage& to,
419 bool shouldWiden = false)
420{
421 addStageConnector(block, *type, name, from, to, shouldWiden);
422}
423
424MATERIALX_NAMESPACE_END
425
426#endif
Cross-platform support for file and search paths.
Shader generation options class.
std::set< string > StringSet
A set of strings.
Definition: Library.h:59
Macros for declaring imported and exported symbols.
shared_ptr< ShaderStage > ShaderStagePtr
Shared pointer to a ShaderStage.
Definition: Library.h:36
std::stringstream StringStream
A string stream.
Definition: Library.h:31
Node element subclasses.
Shader graph class.
shared_ptr< class ShaderPort > ShaderPortPtr
Shared pointer to a ShaderPort.
Definition: ShaderNode.h:29
std::shared_ptr< VariableBlock > VariableBlockPtr
Shared pointer to a VariableBlock.
Definition: ShaderStage.h:52
void addStageConnectorBlock(const string &block, const string &instance, ShaderStage &from, ShaderStage &to)
Utility function for adding a connector block between stages.
Definition: ShaderStage.h:394
ShaderPort * addStageOutput(const string &block, TypeDesc type, const string &name, ShaderStage &stage, bool shouldWiden=false)
Utility function for adding a new shader port to an output block.
Definition: ShaderStage.h:375
ShaderPort * addStageInput(const string &block, TypeDesc type, const string &name, ShaderStage &stage, bool shouldWiden=false)
Utility function for adding a new shader port to an input block.
Definition: ShaderStage.h:356
ShaderPort * addStageUniform(const string &block, TypeDesc type, const string &name, ShaderStage &stage)
Utility function for adding a new shader port to a uniform block.
Definition: ShaderStage.h:339
void addStageConnector(const string &block, TypeDesc type, const string &name, ShaderStage &from, ShaderStage &to, bool shouldWiden=false)
Utility function for adding a variable to a stage connector block.
Definition: ShaderStage.h:404
MX_GENSHADER_API const string PIXEL
Identifier for pixel stage.
std::unordered_map< string, VariableBlockPtr > VariableBlockMap
Shared pointer to a map between string identifiers and VariableBlocks.
Definition: ShaderStage.h:54
std::function< bool(ShaderPort *)> ShaderPortPredicate
A standard function predicate taking an ShaderPort pointer and returning a boolean.
Definition: ShaderStage.h:56
Base class for syntax handling for shader generators.
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
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:27
A context class for shader generation.
Definition: GenContext.h:31
Base class for shader generators All third-party shader generators should derive from this class.
Definition: ShaderGenerator.h:31
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:320
An input or output port on a ShaderNode.
Definition: ShaderNode.h:123
A shader stage, containing the state and resulting source code for the stage.
Definition: ShaderStage.h:139
const VariableBlockMap & getUniformBlocks() const
Return a map of all uniform blocks.
Definition: ShaderStage.h:200
void setFunctionName(const string &functionName)
Set stage function name.
Definition: ShaderStage.h:287
void beginLine()
Start a new line.
void addValue(const T &value)
Add a value.
Definition: ShaderStage.h:267
void addBlock(const string &str, const FilePath &sourceFilename, GenContext &context)
Add a block of code.
const VariableBlock & getConstantBlock() const
Return the constant variable block.
void newLine()
Add a newline character.
void addInclude(const FilePath &includeFilename, const FilePath &sourceFilename, GenContext &context)
Add the contents of an include file if not already present.
void addLine(const string &str, bool semicolon=true)
Add a single line of code, optionally appending a semicolon.
VariableBlock & getOutputBlock(const string &name)
Return the output variable block with given name.
const string & getName() const
Return the stage name.
Definition: ShaderStage.h:155
void addString(const string &str)
Add a string.
bool isEmitted(const ShaderNode &node, GenContext &context) const
Return true if the function for the given node has been emitted in the current scope.
VariableBlockPtr createUniformBlock(const string &name, const string &instance=EMPTY_STRING)
Create a new uniform variable block.
ShaderStage(const string &name, ConstSyntaxPtr syntax)
Contructor.
bool hasSourceDependency(const FilePath &file)
Return true if this stage depends on the given source file.
void setSourceCode(const string &code)
Set the stage source code.
Definition: ShaderStage.h:161
void addFunctionCall(const ShaderNode &node, GenContext &context, bool emitCode=true)
Add the function call for the given node.
void addSourceDependency(const FilePath &file)
Mark the given source file as a dependency of this stage.
const VariableBlockMap & getInputBlocks() const
Return a map of all input blocks.
Definition: ShaderStage.h:206
VariableBlockPtr createOutputBlock(const string &name, const string &instance=EMPTY_STRING)
Create a new output variable block.
void endScope(bool semicolon=false, bool newline=true)
End the current scope.
void addFunctionDefinition(const ShaderNode &node, GenContext &context)
Add the function definition for a node's implementation.
void beginScope(Syntax::Punctuation punc=Syntax::CURLY_BRACKETS)
Start a new scope using the given bracket type.
const string & getFunctionName() const
Return the stage function name.
Definition: ShaderStage.h:158
const StringSet & getSourceDependencies() const
Return a set of all source dependencies.
Definition: ShaderStage.h:224
VariableBlockPtr createInputBlock(const string &name, const string &instance=EMPTY_STRING)
Create a new input variable block.
void addComment(const string &str)
Add a single line code comment.
VariableBlock & getUniformBlock(const string &name)
Return the uniform variable block with given name.
void endLine(bool semicolon=true)
End the current line.
const VariableBlock & getUniformBlock(const string &name) const
Return the uniform variable block with given name.
const string & getSourceCode() const
Return the stage source code.
Definition: ShaderStage.h:164
const VariableBlock & getOutputBlock(const string &name) const
Return the output variable block with given name.
const VariableBlockMap & getOutputBlocks() const
Return a map of all output blocks.
Definition: ShaderStage.h:212
const StringSet & getIncludes() const
Return a set of all include files.
Definition: ShaderStage.h:218
const VariableBlock & getInputBlock(const string &name) const
Return the input variable block with given name.
VariableBlock & getInputBlock(const string &name)
Return the input variable block with given name.
VariableBlock & getConstantBlock()
Return the constant variable block.
Punctuation
Punctuation types.
Definition: Syntax.h:45
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:35
A block of variables in a shader stage.
Definition: ShaderStage.h:61
const ShaderPort * operator[](const string &name) const
Return a variable by name.
size_t size() const
Return the number of variables in this block.
Definition: ShaderStage.h:85
const string & getName() const
Get the name of this block.
Definition: ShaderStage.h:70
void setName(const string &name)
Set the name of this block.
Definition: ShaderStage.h:73
ShaderPort * operator[](size_t index)
Return a variable by index.
Definition: ShaderStage.h:88
bool empty() const
Return true if the block has no variables.
Definition: ShaderStage.h:82
void add(ShaderPortPtr port)
Add an existing shader port to this block.
ShaderPort * add(TypeDesc type, const string &name, ValuePtr value=nullptr, bool shouldWiden=false)
Add a new shader port to this block.
ShaderPort * operator[](const string &name)
Return a variable by name.
const string & getInstance() const
Get the instance name of this block.
Definition: ShaderStage.h:76
ShaderPort * find(const string &name)
Return a variable by name.
const ShaderPort * find(const string &name) const
Return a variable by name.
const ShaderPort * operator[](size_t index) const
Return a variable by index.
Definition: ShaderStage.h:91
ShaderPort * find(const ShaderPortPredicate &predicate)
Find a port based on a predicate.
const vector< ShaderPort * > & getVariableOrder() const
Return a const reference to our variable order vector.
Definition: ShaderStage.h:94
void setInstance(const string &instance)
Set the instance name of this block.
Definition: ShaderStage.h:79
Definition: ShaderStage.h:143