MaterialX 1.38.2
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
ShaderGenerator Class Referenceabstract

Base class for shader generators All third-party shader generators should derive from this class. More...

#include <ShaderGenerator.h>

Inheritance diagram for ShaderGenerator:
HwShaderGenerator MdlShaderGenerator OslShaderGenerator GlslShaderGenerator EsslShaderGenerator

Public Member Functions

virtual ~ShaderGenerator ()
 Destructor.
 
virtual const string & getTarget () const =0
 Return the name of the target this generator is for. More...
 
virtual ShaderPtr generate (const string &name, ElementPtr element, GenContext &context) const =0
 Generate a shader starting from the given element, translating the element and all dependencies upstream into shader code. More...
 
virtual void emitScopeBegin (ShaderStage &stage, Syntax::Punctuation punc=Syntax::CURLY_BRACKETS) const
 Start a new scope using the given bracket type.
 
virtual void emitScopeEnd (ShaderStage &stage, bool semicolon=false, bool newline=true) const
 End the current scope.
 
virtual void emitLineBegin (ShaderStage &stage) const
 Start a new line.
 
virtual void emitLineEnd (ShaderStage &stage, bool semicolon=true) const
 End the current line.
 
virtual void emitLineBreak (ShaderStage &stage) const
 Add a line break.
 
virtual void emitString (const string &str, ShaderStage &stage) const
 Add a string.
 
virtual void emitLine (const string &str, ShaderStage &stage, bool semicolon=true) const
 Add a single line of code, optionally appending a semicolon.
 
virtual void emitComment (const string &str, ShaderStage &stage) const
 Add a single line code comment.
 
virtual void emitBlock (const string &str, GenContext &context, ShaderStage &stage) const
 Add a block of code.
 
virtual void emitInclude (const string &file, GenContext &context, ShaderStage &stage) const
 Add the contents of an include file. More...
 
template<typename T >
void emitValue (const T &value, ShaderStage &stage) const
 Add a value.
 
virtual void emitFunctionDefinition (const ShaderNode &node, GenContext &context, ShaderStage &stage) const
 Add the function definition for a single node.
 
virtual void emitFunctionCall (const ShaderNode &node, GenContext &context, ShaderStage &stage, bool checkScope=true) const
 Add the function call for a single node. More...
 
virtual void emitFunctionDefinitions (const ShaderGraph &graph, GenContext &context, ShaderStage &stage) const
 Add all function definitions for a graph. More...
 
virtual void emitFunctionCalls (const ShaderGraph &graph, GenContext &context, ShaderStage &stage) const
 Add all function calls for a graph. More...
 
virtual void emitTypeDefinitions (GenContext &context, ShaderStage &stage) const
 Emit type definitions for all data types that needs it.
 
virtual void emitInput (const ShaderInput *input, GenContext &context, ShaderStage &stage) const
 Emit the connected variable name for an input, or constant value if the port is not connected.
 
virtual void emitOutput (const ShaderOutput *output, bool includeType, bool assignValue, GenContext &context, ShaderStage &stage) const
 Emit the output variable name for an output, optionally including it's type and default value assignment.
 
virtual void emitVariableDeclarations (const VariableBlock &block, const string &qualifier, const string &separator, GenContext &context, ShaderStage &stage, bool assignValue=true) const
 Emit definitions for all shader variables in a block. More...
 
virtual void emitVariableDeclaration (const ShaderPort *variable, const string &qualifier, GenContext &context, ShaderStage &stage, bool assignValue=true) const
 Emit definition of a single shader variable. More...
 
virtual string getUpstreamResult (const ShaderInput *input, GenContext &context) const
 Return the result of an upstream connection or value for an input. More...
 
const SyntaxgetSyntax () const
 Return the syntax object for the language used by the code generator.
 
void registerImplementation (const string &name, CreatorFunction< ShaderNodeImpl > creator)
 Register a shader node implementation for a given implementation element name.
 
bool implementationRegistered (const string &name) const
 Determine if a shader node implementation has been registered for a given implementation element name.
 
void setColorManagementSystem (ColorManagementSystemPtr colorManagementSystem)
 Sets the color management system.
 
ColorManagementSystemPtr getColorManagementSystem () const
 Returns the color management system.
 
void setUnitSystem (UnitSystemPtr unitSystem)
 Sets the unit system.
 
UnitSystemPtr getUnitSystem () const
 Returns the unit system.
 
ShaderNodeImplPtr getImplementation (const InterfaceElement &element, GenContext &context) const
 Return a registered shader node implementation given an implementation element. More...
 
const StringMapgetTokenSubstitutions () const
 Return the map of token substitutions used by the generator.
 
virtual void registerShaderMetadata (const DocumentPtr &doc, GenContext &context) const
 Register metadata that should be exported to the generated shaders. More...
 

Protected Member Functions

 ShaderGenerator (SyntaxPtr syntax)
 Protected constructor.
 
virtual ShaderStagePtr createStage (const string &name, Shader &shader) const
 Create a new stage in a shader.
 
virtual ShaderNodeImplPtr createSourceCodeImplementation (const Implementation &impl) const
 Create a source code implementation which is the implementation class to use for nodes that has no specific C++ implementation registered for it. More...
 
virtual ShaderNodeImplPtr createCompoundImplementation (const NodeGraph &impl) const
 Create a compound implementation which is the implementation class to use for nodes using a nodegraph as their implementation. More...
 
virtual void finalizeShaderGraph (ShaderGraph &graph)
 Method called on all created shader graphs. More...
 
void setFunctionName (const string &functionName, ShaderStage &stage) const
 Set function name for a stage.
 
void replaceTokens (const StringMap &substitutions, ShaderStage &stage) const
 Replace tokens with identifiers according to the given substitutions map.
 

Protected Attributes

SyntaxPtr _syntax
 
Factory< ShaderNodeImpl_implFactory
 
ColorManagementSystemPtr _colorManagementSystem
 
UnitSystemPtr _unitSystem
 
StringMap _tokenSubstitutions
 
friend ShaderGraph
 

Static Protected Attributes

static const string T_FILE_TRANSFORM_UV = "$fileTransformUv"
 

Detailed Description

Base class for shader generators All third-party shader generators should derive from this class.

Derived classes should use DECLARE_SHADER_GENERATOR / DEFINE_SHADER_GENERATOR in their declaration / definition, and register with the Registry class.

Member Function Documentation

◆ createCompoundImplementation()

ShaderNodeImplPtr createCompoundImplementation ( const NodeGraph impl) const
protectedvirtual

Create a compound implementation which is the implementation class to use for nodes using a nodegraph as their implementation.

Derived classes can override this to use custom compound implementations.

Reimplemented in GlslShaderGenerator, MdlShaderGenerator, and HwShaderGenerator.

◆ createSourceCodeImplementation()

ShaderNodeImplPtr createSourceCodeImplementation ( const Implementation impl) const
protectedvirtual

Create a source code implementation which is the implementation class to use for nodes that has no specific C++ implementation registered for it.

Derived classes can override this to use custom source code implementations.

Reimplemented in MdlShaderGenerator, and HwShaderGenerator.

◆ emitFunctionCall()

void emitFunctionCall ( const ShaderNode node,
GenContext context,
ShaderStage stage,
bool  checkScope = true 
) const
virtual

Add the function call for a single node.

Reimplemented in HwShaderGenerator.

◆ emitFunctionCalls()

void emitFunctionCalls ( const ShaderGraph graph,
GenContext context,
ShaderStage stage 
) const
virtual

Add all function calls for a graph.

Reimplemented in GlslShaderGenerator, and OslShaderGenerator.

◆ emitFunctionDefinitions()

void emitFunctionDefinitions ( const ShaderGraph graph,
GenContext context,
ShaderStage stage 
) const
virtual

Add all function definitions for a graph.

Reimplemented in GlslShaderGenerator.

◆ emitInclude()

void emitInclude ( const string &  file,
GenContext context,
ShaderStage stage 
) const
virtual

Add the contents of an include file.

Making sure it is only included once for the shader stage.

◆ emitVariableDeclaration()

void emitVariableDeclaration ( const ShaderPort variable,
const string &  qualifier,
GenContext context,
ShaderStage stage,
bool  assignValue = true 
) const
virtual

Emit definition of a single shader variable.

Parameters
variableShader port representing the variable.
qualifierOptional qualifier to add before the variable declaration.
contextContext for generation.
stageThe stage to emit code into.
assignValueIf true the variable is initialized with its value.

Reimplemented in GlslShaderGenerator.

◆ emitVariableDeclarations()

void emitVariableDeclarations ( const VariableBlock block,
const string &  qualifier,
const string &  separator,
GenContext context,
ShaderStage stage,
bool  assignValue = true 
) const
virtual

Emit definitions for all shader variables in a block.

Parameters
blockBlock to emit.
qualifierOptional qualifier to add before the variable declaration.
separatorSeparator to use between the declarations.
contextContext for generation.
stageThe stage to emit code into.
assignValueIf true the variables are initialized with their value.

◆ finalizeShaderGraph()

void finalizeShaderGraph ( ShaderGraph graph)
protectedvirtual

Method called on all created shader graphs.

By default it does nothing, but shader generators can override this to perform custom edits on the graph before shader generation starts.

Reimplemented in MdlShaderGenerator.

◆ generate()

virtual ShaderPtr generate ( const string &  name,
ElementPtr  element,
GenContext context 
) const
pure virtual

Generate a shader starting from the given element, translating the element and all dependencies upstream into shader code.

Implemented in GlslShaderGenerator, MdlShaderGenerator, and OslShaderGenerator.

◆ getImplementation()

ShaderNodeImplPtr getImplementation ( const InterfaceElement element,
GenContext context 
) const

Return a registered shader node implementation given an implementation element.

The element must be an Implementation or a NodeGraph acting as implementation. If no registered implementation is found a 'default' implementation instance will be returned, as defined by the createDefaultImplementation method.

◆ getTarget()

virtual const string & getTarget ( ) const
pure virtual

Return the name of the target this generator is for.

Implemented in EsslShaderGenerator, GlslShaderGenerator, MdlShaderGenerator, and OslShaderGenerator.

◆ getUpstreamResult()

string getUpstreamResult ( const ShaderInput input,
GenContext context 
) const
virtual

Return the result of an upstream connection or value for an input.

Reimplemented in MdlShaderGenerator.

◆ registerShaderMetadata()

void registerShaderMetadata ( const DocumentPtr doc,
GenContext context 
) const
virtual

Register metadata that should be exported to the generated shaders.

Supported metadata includes standard UI attributes like "uiname", "uifolder", "uimin", "uimax", etc. But it is also extendable by defining custom attributes using AttributeDefs. Any AttributeDef in the given document with exportable="true" will be exported as shader metadata when found on nodes during shader generation. Derived shader generators may override this method to change the registration. Applications must explicitly call this method before shader generation to enable export of metadata.

Reimplemented in OslShaderGenerator.


The documentation for this class was generated from the following files: