HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShaderGenerator Class Reference

#include <ShaderGenerator.h>

+ Inheritance diagram for ShaderGenerator:

Public Member Functions

virtual ~ShaderGenerator ()
 Destructor. More...
 
virtual const stringgetTarget () const
 Return the name of the target this generator is for. More...
 
virtual ShaderPtr generate (const string &, ElementPtr, GenContext &) const
 
virtual void emitScopeBegin (ShaderStage &stage, Syntax::Punctuation punc=Syntax::CURLY_BRACKETS) const
 Start a new scope using the given bracket type. More...
 
virtual void emitScopeEnd (ShaderStage &stage, bool semicolon=false, bool newline=true) const
 End the current scope. More...
 
virtual void emitLineBegin (ShaderStage &stage) const
 Start a new line. More...
 
virtual void emitLineEnd (ShaderStage &stage, bool semicolon=true) const
 End the current line. More...
 
virtual void emitLineBreak (ShaderStage &stage) const
 Add a line break. More...
 
virtual void emitString (const string &str, ShaderStage &stage) const
 Add a string. More...
 
virtual void emitLine (const string &str, ShaderStage &stage, bool semicolon=true) const
 Add a single line of code, optionally appending a semicolon. More...
 
virtual void emitComment (const string &str, ShaderStage &stage) const
 Add a single line code comment. More...
 
virtual void emitBlock (const string &str, const FilePath &sourceFilename, GenContext &context, ShaderStage &stage) const
 Add a block of code. More...
 
virtual void emitLibraryInclude (const FilePath &filename, GenContext &context, ShaderStage &stage) const
 
template<typename T >
void emitValue (const T &value, ShaderStage &stage) const
 Add a value. More...
 
virtual void emitFunctionDefinition (const ShaderNode &node, GenContext &context, ShaderStage &stage) const
 Add the function definition 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 emitFunctionCall (const ShaderNode &node, GenContext &context, ShaderStage &stage) const
 Add the function call for a single node. More...
 
virtual void emitFunctionCall (const ShaderNode &node, GenContext &context, ShaderStage &stage, bool checkScope) const
 
virtual void emitFunctionCalls (const ShaderGraph &graph, GenContext &context, ShaderStage &stage, uint32_t classification=0u) const
 
virtual void emitDependentFunctionCalls (const ShaderNode &node, GenContext &context, ShaderStage &stage, uint32_t classification=0u) const
 
virtual void emitFunctionBodyBegin (const ShaderNode &node, GenContext &context, ShaderStage &stage, Syntax::Punctuation punc=Syntax::CURLY_BRACKETS) const
 Emit code for starting a new function body. More...
 
virtual void emitFunctionBodyEnd (const ShaderNode &node, GenContext &context, ShaderStage &stage) const
 Emit code for ending a function body. More...
 
virtual void emitTypeDefinitions (GenContext &context, ShaderStage &stage) const
 Emit type definitions for all data types that needs it. More...
 
virtual void emitInput (const ShaderInput *input, GenContext &context, ShaderStage &stage) const
 
virtual void emitOutput (const ShaderOutput *output, bool includeType, bool assignValue, GenContext &context, ShaderStage &stage) const
 
virtual void emitVariableDeclarations (const VariableBlock &block, const string &qualifier, const string &separator, GenContext &context, ShaderStage &stage, bool assignValue=true) const
 
virtual void emitVariableDeclaration (const ShaderPort *variable, const string &qualifier, GenContext &context, ShaderStage &stage, bool assignValue=true) const
 
virtual void getClosureContexts (const ShaderNode &node, vector< ClosureContext * > &cct) const
 Return the closure contexts defined for the given node. 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. More...
 
void registerImplementation (const string &name, CreatorFunction< ShaderNodeImpl > creator)
 Register a shader node implementation for a given implementation element name. More...
 
void registerImplementation (const StringVec &nameVec, CreatorFunction< ShaderNodeImpl > creator)
 Register a shader node implementation for a given set of implementation element names. More...
 
bool implementationRegistered (const string &name) const
 Determine if a shader node implementation has been registered for a given implementation element name. More...
 
virtual ShaderNodeImplPtr getImplementation (const NodeDef &nodedef, GenContext &context) const
 Return a registered shader node implementation for the given nodedef. More...
 
void setColorManagementSystem (ColorManagementSystemPtr colorManagementSystem)
 Sets the color management system. More...
 
ColorManagementSystemPtr getColorManagementSystem () const
 Returns the color management system. More...
 
void setUnitSystem (UnitSystemPtr unitSystem)
 Sets the unit system. More...
 
UnitSystemPtr getUnitSystem () const
 Returns the unit system. More...
 
const StringMapgetTokenSubstitutions () const
 Return the map of token substitutions used by the generator. More...
 
virtual void registerShaderMetadata (const DocumentPtr &doc, GenContext &context) const
 

Protected Member Functions

 ShaderGenerator (SyntaxPtr syntax)
 Protected constructor. More...
 
virtual ShaderStagePtr createStage (const string &name, Shader &shader) const
 Create a new stage in a shader. More...
 
void setFunctionName (const string &functionName, ShaderStage &stage) const
 Set function name for a stage. More...
 
void replaceTokens (const StringMap &substitutions, ShaderStage &stage) const
 Replace tokens with identifiers according to the given substitutions map. More...
 
void createVariables (ShaderGraphPtr graph, GenContext &context, Shader &shader) const
 

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
 

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.

Definition at line 30 of file ShaderGenerator.h.

Constructor & Destructor Documentation

virtual ShaderGenerator::~ShaderGenerator ( )
inlinevirtual

Destructor.

Definition at line 34 of file ShaderGenerator.h.

ShaderGenerator::ShaderGenerator ( SyntaxPtr  syntax)
protected

Protected constructor.

Member Function Documentation

virtual ShaderStagePtr ShaderGenerator::createStage ( const string name,
Shader shader 
) const
protectedvirtual

Create a new stage in a shader.

void ShaderGenerator::createVariables ( ShaderGraphPtr  graph,
GenContext context,
Shader shader 
) const
protected

Create shader variables (e.g. uniforms, inputs and outputs) for nodes that require input data from the application.

virtual void ShaderGenerator::emitBlock ( const string str,
const FilePath sourceFilename,
GenContext context,
ShaderStage stage 
) const
virtual

Add a block of code.

virtual void ShaderGenerator::emitComment ( const string str,
ShaderStage stage 
) const
virtual

Add a single line code comment.

virtual void ShaderGenerator::emitDependentFunctionCalls ( const ShaderNode node,
GenContext context,
ShaderStage stage,
uint32_t  classification = 0u 
) const
virtual

Add function calls for nodes connected directly upstream from the given node. If a classification mask is given only functions for nodes matching this classification will be emitted.

virtual void ShaderGenerator::emitFunctionBodyBegin ( const ShaderNode node,
GenContext context,
ShaderStage stage,
Syntax::Punctuation  punc = Syntax::CURLY_BRACKETS 
) const
virtual

Emit code for starting a new function body.

Reimplemented in OslShaderGenerator.

virtual void ShaderGenerator::emitFunctionBodyEnd ( const ShaderNode node,
GenContext context,
ShaderStage stage 
) const
virtual

Emit code for ending a function body.

virtual void ShaderGenerator::emitFunctionCall ( const ShaderNode node,
GenContext context,
ShaderStage stage 
) const
virtual

Add the function call for a single node.

Reimplemented in HwShaderGenerator.

virtual void ShaderGenerator::emitFunctionCall ( const ShaderNode node,
GenContext context,
ShaderStage stage,
bool  checkScope 
) const
virtual
virtual void ShaderGenerator::emitFunctionCalls ( const ShaderGraph graph,
GenContext context,
ShaderStage stage,
uint32_t  classification = 0u 
) const
virtual

Add all function calls for a graph. If a classification mask is given only functions for nodes matching this classification will be emitted.

Reimplemented in OslShaderGenerator.

virtual void ShaderGenerator::emitFunctionDefinition ( const ShaderNode node,
GenContext context,
ShaderStage stage 
) const
virtual

Add the function definition for a single node.

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

Add all function definitions for a graph.

virtual void ShaderGenerator::emitInput ( const ShaderInput input,
GenContext context,
ShaderStage stage 
) const
virtual

Emit the connected variable name for an input, or constant value if the port is not connected

virtual void ShaderGenerator::emitLibraryInclude ( const FilePath filename,
GenContext context,
ShaderStage stage 
) const
virtual

Add the contents of a standard library include file if not already present. The library file prefix of the given context, if any, will be prepended to the given filename.

virtual void ShaderGenerator::emitLine ( const string str,
ShaderStage stage,
bool  semicolon = true 
) const
virtual

Add a single line of code, optionally appending a semicolon.

virtual void ShaderGenerator::emitLineBegin ( ShaderStage stage) const
virtual

Start a new line.

virtual void ShaderGenerator::emitLineBreak ( ShaderStage stage) const
virtual

Add a line break.

virtual void ShaderGenerator::emitLineEnd ( ShaderStage stage,
bool  semicolon = true 
) const
virtual

End the current line.

virtual void ShaderGenerator::emitOutput ( const ShaderOutput output,
bool  includeType,
bool  assignValue,
GenContext context,
ShaderStage stage 
) const
virtual

Emit the output variable name for an output, optionally including it's type and default value assignment.

virtual void ShaderGenerator::emitScopeBegin ( ShaderStage stage,
Syntax::Punctuation  punc = Syntax::CURLY_BRACKETS 
) const
virtual

Start a new scope using the given bracket type.

virtual void ShaderGenerator::emitScopeEnd ( ShaderStage stage,
bool  semicolon = false,
bool  newline = true 
) const
virtual

End the current scope.

virtual void ShaderGenerator::emitString ( const string str,
ShaderStage stage 
) const
virtual

Add a string.

virtual void ShaderGenerator::emitTypeDefinitions ( GenContext context,
ShaderStage stage 
) const
virtual

Emit type definitions for all data types that needs it.

template<typename T >
void ShaderGenerator::emitValue ( const T value,
ShaderStage stage 
) const
inline

Add a value.

Definition at line 83 of file ShaderGenerator.h.

virtual void ShaderGenerator::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 MslShaderGenerator, and GlslShaderGenerator.

virtual void ShaderGenerator::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.
virtual ShaderPtr ShaderGenerator::generate ( const string ,
ElementPtr  ,
GenContext  
) const
inlinevirtual

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

Reimplemented in MdlShaderGenerator, OslShaderGenerator, MslShaderGenerator, and GlslShaderGenerator.

Definition at line 44 of file ShaderGenerator.h.

virtual void ShaderGenerator::getClosureContexts ( const ShaderNode node,
vector< ClosureContext * > &  cct 
) const
virtual

Return the closure contexts defined for the given node.

Reimplemented in HwShaderGenerator.

ColorManagementSystemPtr ShaderGenerator::getColorManagementSystem ( ) const
inline

Returns the color management system.

Definition at line 171 of file ShaderGenerator.h.

virtual ShaderNodeImplPtr ShaderGenerator::getImplementation ( const NodeDef nodedef,
GenContext context 
) const
virtual

Return a registered shader node implementation for the given nodedef.

Reimplemented in MdlShaderGenerator, MslShaderGenerator, and GlslShaderGenerator.

const Syntax& ShaderGenerator::getSyntax ( ) const
inline

Return the syntax object for the language used by the code generator.

Definition at line 150 of file ShaderGenerator.h.

virtual const string& ShaderGenerator::getTarget ( ) const
inlinevirtual

Return the name of the target this generator is for.

Reimplemented in MdlShaderGenerator, MslShaderGenerator, GlslShaderGenerator, OslShaderGenerator, VkShaderGenerator, and EsslShaderGenerator.

Definition at line 37 of file ShaderGenerator.h.

const StringMap& ShaderGenerator::getTokenSubstitutions ( ) const
inline

Return the map of token substitutions used by the generator.

Definition at line 189 of file ShaderGenerator.h.

UnitSystemPtr ShaderGenerator::getUnitSystem ( ) const
inline

Returns the unit system.

Definition at line 183 of file ShaderGenerator.h.

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

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

Reimplemented in MdlShaderGenerator.

bool ShaderGenerator::implementationRegistered ( const string name) const

Determine if a shader node implementation has been registered for a given implementation element name.

void ShaderGenerator::registerImplementation ( const string name,
CreatorFunction< ShaderNodeImpl creator 
)

Register a shader node implementation for a given implementation element name.

void ShaderGenerator::registerImplementation ( const StringVec nameVec,
CreatorFunction< ShaderNodeImpl creator 
)

Register a shader node implementation for a given set of implementation element names.

virtual void ShaderGenerator::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.

void ShaderGenerator::replaceTokens ( const StringMap substitutions,
ShaderStage stage 
) const
protected

Replace tokens with identifiers according to the given substitutions map.

void ShaderGenerator::setColorManagementSystem ( ColorManagementSystemPtr  colorManagementSystem)
inline

Sets the color management system.

Definition at line 165 of file ShaderGenerator.h.

void ShaderGenerator::setFunctionName ( const string functionName,
ShaderStage stage 
) const
inlineprotected

Set function name for a stage.

Definition at line 213 of file ShaderGenerator.h.

void ShaderGenerator::setUnitSystem ( UnitSystemPtr  unitSystem)
inline

Sets the unit system.

Definition at line 177 of file ShaderGenerator.h.

Member Data Documentation

ColorManagementSystemPtr ShaderGenerator::_colorManagementSystem
protected

Definition at line 230 of file ShaderGenerator.h.

Factory<ShaderNodeImpl> ShaderGenerator::_implFactory
protected

Definition at line 229 of file ShaderGenerator.h.

SyntaxPtr ShaderGenerator::_syntax
protected

Definition at line 228 of file ShaderGenerator.h.

StringMap ShaderGenerator::_tokenSubstitutions
mutableprotected

Definition at line 232 of file ShaderGenerator.h.

UnitSystemPtr ShaderGenerator::_unitSystem
protected

Definition at line 231 of file ShaderGenerator.h.

friend ShaderGenerator::ShaderGraph
protected

Definition at line 234 of file ShaderGenerator.h.

const string ShaderGenerator::T_FILE_TRANSFORM_UV
staticprotected

Definition at line 226 of file ShaderGenerator.h.


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