6#ifndef MATERIALX_SHADERSTAGE_H
7#define MATERIALX_SHADERSTAGE_H
27#define DEFINE_SHADER_STAGE(stage, name) if (stage.getName() == name)
30#define BEGIN_SHADER_STAGE(stage, name) \
31 if (stage.getName() == name) \
33#define END_SHADER_STAGE(stage, name) }
35MATERIALX_NAMESPACE_BEGIN
46extern MX_GENSHADER_API
const string PIXEL;
70 const string&
getName()
const {
return _name; }
73 void setName(
const string& name) { _name = name; }
79 void setInstance(
const string& instance) { _instance = instance; }
82 bool empty()
const {
return _variableOrder.empty(); }
85 size_t size()
const {
return _variableOrder.size(); }
131 std::unordered_map<string, ShaderPortPtr> _variableMap;
132 vector<ShaderPort*> _variableOrder;
141 using FunctionCallId = std::pair<const ShaderNode*, int>;
145 std::set<FunctionCallId> functions;
155 const string&
getName()
const {
return _name; }
226 return _sourceDependencies;
233 void endScope(
bool semicolon =
false,
bool newline =
true);
248 void addLine(
const string& str,
bool semicolon =
true);
266 template <
typename T>
289 _functionName = functionName;
297 string _functionName;
306 vector<Scope> _scopes;
315 std::set<size_t> _definedFunctions;
345 return uniforms.
add(type, name);
360 bool shouldWiden =
false)
363 return inputs.
add(type, name, {}, shouldWiden);
369 bool shouldWiden =
false)
371 return addStageInput(block, *type, name, stage, shouldWiden);
379 bool shouldWiden =
false)
382 return outputs.
add(type, name, {}, shouldWiden);
388 bool shouldWiden =
false)
395 const string& instance,
409 bool shouldWiden =
false)
419 bool shouldWiden =
false)
424MATERIALX_NAMESPACE_END
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
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