MaterialX 1.38.9
Loading...
Searching...
No Matches
Syntax Class Referenceabstract

Base class for syntax objects used by shader generators to emit code with correct syntax for each language. More...

#include <Syntax.h>

Inheritance diagram for Syntax:
GlslSyntax MdlSyntax OslSyntax EsslSyntax VkSyntax

Public Types

enum  Punctuation { PARENTHESES , CURLY_BRACKETS , SQUARE_BRACKETS , DOUBLE_SQUARE_BRACKETS }
 Punctuation types.
 

Public Member Functions

void registerTypeSyntax (const TypeDesc *type, TypeSyntaxPtr syntax)
 Register syntax handling for a data type. More...
 
void registerReservedWords (const StringSet &names)
 Register names that are reserved words not to be used by a code generator when naming variables and functions. More...
 
void registerInvalidTokens (const StringMap &tokens)
 Register a set string replacements for disallowed tokens for a code generator when naming variables and functions. More...
 
const StringSetgetReservedWords () const
 Returns a set of names that are reserved words for this language syntax.
 
const StringMapgetInvalidTokens () const
 Returns a mapping from disallowed tokens to replacement strings for this language syntax.
 
const TypeSyntaxgetTypeSyntax (const TypeDesc *type) const
 Returns the type syntax object for a named type. More...
 
const vector< TypeSyntaxPtr > & getTypeSyntaxes () const
 Returns an array of all registered type syntax objects.
 
const TypeDescgetTypeDescription (const TypeSyntaxPtr &typeSyntax) const
 Returns a type description given a type syntax. More...
 
const string & getTypeName (const TypeDesc *type) const
 Returns the name syntax of the given type.
 
virtual string getOutputTypeName (const TypeDesc *type) const
 Returns the type name in an output context.
 
const string & getTypeAlias (const TypeDesc *type) const
 Returns a type alias for the given data type. More...
 
const string & getTypeDefinition (const TypeDesc *type) const
 Returns a custom type definition if needed for the given data type. More...
 
const string & getDefaultValue (const TypeDesc *type, bool uniform=false) const
 Returns the default value string for the given type.
 
virtual string getValue (const TypeDesc *type, const Value &value, bool uniform=false) const
 Returns the value string for a given type and value object.
 
virtual string getValue (const ShaderPort *port, bool uniform=false) const
 Returns the value string for a given shader port object.
 
virtual string getSwizzledVariable (const string &srcName, const TypeDesc *srcType, const string &channels, const TypeDesc *dstType) const
 Get syntax for a swizzled variable. More...
 
virtual ValuePtr getSwizzledValue (ValuePtr value, const TypeDesc *srcType, const string &channels, const TypeDesc *dstType) const
 Get swizzled value.
 
virtual const string & getInputQualifier () const
 Returns a type qualifier to be used when declaring types for input variables. More...
 
virtual const string & getOutputQualifier () const
 Returns a type qualifier to be used when declaring types for output variables. More...
 
virtual const string & getConstantQualifier () const =0
 Get the qualifier used when declaring constant variables. More...
 
virtual const string & getUniformQualifier () const
 Get the qualifier used when declaring uniform variables. More...
 
virtual const string & getNewline () const
 Return the characters used for a newline.
 
virtual const string & getIndentation () const
 Return the characters used for a single indentation level.
 
virtual const string & getStringQuote () const
 Return the characters used to begin/end a string definition.
 
virtual const string & getIncludeStatement () const
 Return the string pattern used for a file include statement.
 
virtual const string & getSingleLineComment () const
 Return the characters used for single line comment.
 
virtual const string & getBeginMultiLineComment () const
 Return the characters used to begin a multi line comments block.
 
virtual const string & getEndMultiLineComment () const
 Return the characters used to end a multi line comments block.
 
virtual const string & getSourceFileExtension () const =0
 Return the file extension used for source code files in this language. More...
 
virtual string getArrayTypeSuffix (const TypeDesc *, const Value &) const
 Return the array suffix to use for declaring an array type. More...
 
virtual string getArrayVariableSuffix (const TypeDesc *type, const Value &value) const
 Return the array suffix to use for declaring an array variable. More...
 
virtual bool typeSupported (const TypeDesc *type) const
 Query if given type is suppored in the syntax. More...
 
virtual void makeValidName (string &name) const
 Modify the given name string to remove any invalid characters or tokens. More...
 
virtual void makeIdentifier (string &name, IdentifierMap &identifiers) const
 Make sure the given name is a unique identifier, updating it if needed to make it unique.
 
virtual string getVariableName (const string &name, const TypeDesc *type, IdentifierMap &identifiers) const
 Create a unique identifier for the given variable name and type. More...
 
virtual bool remapEnumeration (const string &value, const TypeDesc *type, const string &enumNames, std::pair< const TypeDesc *, ValuePtr > &result) const
 Given an input specification attempt to remap this to an enumeration which is accepted by the shader generator. More...
 

Static Public Attributes

static const string NEWLINE = "\n"
 Constants with commonly used strings.
 
static const string SEMICOLON = ";"
 
static const string COMMA = ","
 

Protected Member Functions

 Syntax ()
 Protected constructor.
 

Protected Attributes

vector< TypeSyntaxPtr_typeSyntaxes
 
std::unordered_map< const TypeDesc *, size_t > _typeSyntaxByType
 
StringSet _reservedWords
 
StringMap _invalidTokens
 

Static Protected Attributes

static const string INDENTATION = " "
 
static const string STRING_QUOTE = "\""
 
static const string INCLUDE_STATEMENT = "#include"
 
static const string SINGLE_LINE_COMMENT = "// "
 
static const string BEGIN_MULTI_LINE_COMMENT = "/* "
 
static const string END_MULTI_LINE_COMMENT = " */"
 
static const std::unordered_map< char, size_t > CHANNELS_MAPPING
 

Detailed Description

Base class for syntax objects used by shader generators to emit code with correct syntax for each language.

Member Function Documentation

◆ getArrayTypeSuffix()

virtual string getArrayTypeSuffix ( const TypeDesc ,
const Value  
) const
inlinevirtual

Return the array suffix to use for declaring an array type.

Reimplemented in MdlSyntax.

◆ getArrayVariableSuffix()

string getArrayVariableSuffix ( const TypeDesc type,
const Value value 
) const
virtual

Return the array suffix to use for declaring an array variable.

Reimplemented in MdlSyntax.

◆ getConstantQualifier()

virtual const string & getConstantQualifier ( ) const
pure virtual

Get the qualifier used when declaring constant variables.

Derived classes must define this method.

Implemented in GlslSyntax, MdlSyntax, and OslSyntax.

◆ getInputQualifier()

virtual const string & getInputQualifier ( ) const
inlinevirtual

Returns a type qualifier to be used when declaring types for input variables.

Default implementation returns empty string and derived syntax classes should override this method.

Reimplemented in GlslSyntax, and VkSyntax.

◆ getOutputQualifier()

virtual const string & getOutputQualifier ( ) const
inlinevirtual

Returns a type qualifier to be used when declaring types for output variables.

Default implementation returns empty string and derived syntax classes should override this method.

Reimplemented in GlslSyntax, and OslSyntax.

◆ getSourceFileExtension()

virtual const string & getSourceFileExtension ( ) const
pure virtual

Return the file extension used for source code files in this language.

Implemented in GlslSyntax, MdlSyntax, and OslSyntax.

◆ getSwizzledVariable()

string getSwizzledVariable ( const string &  srcName,
const TypeDesc srcType,
const string &  channels,
const TypeDesc dstType 
) const
virtual

Get syntax for a swizzled variable.

Reimplemented in MdlSyntax.

◆ getTypeAlias()

const string & getTypeAlias ( const TypeDesc type) const

Returns a type alias for the given data type.

If not used returns an empty string.

◆ getTypeDefinition()

const string & getTypeDefinition ( const TypeDesc type) const

Returns a custom type definition if needed for the given data type.

If not used returns an empty string.

◆ getTypeDescription()

const TypeDesc * getTypeDescription ( const TypeSyntaxPtr typeSyntax) const

Returns a type description given a type syntax.

Throws an exception if the type syntax has not been registered

◆ getTypeSyntax()

const TypeSyntax & getTypeSyntax ( const TypeDesc type) const

Returns the type syntax object for a named type.

Throws an exception if a type syntax is not defined for the given type.

◆ getUniformQualifier()

virtual const string & getUniformQualifier ( ) const
inlinevirtual

Get the qualifier used when declaring uniform variables.

Default implementation returns empty string and derived syntax classes should override this method.

Reimplemented in GlslSyntax, and MdlSyntax.

◆ getVariableName()

string getVariableName ( const string &  name,
const TypeDesc type,
IdentifierMap identifiers 
) const
virtual

Create a unique identifier for the given variable name and type.

The method is used for naming variables (inputs and outputs) in generated code. Derived classes can override this method to have a custom naming strategy. Default implementation adds a number suffix, or increases an existing number suffix, on the name string if there is a name collision.

◆ makeValidName()

void makeValidName ( string &  name) const
virtual

Modify the given name string to remove any invalid characters or tokens.

Reimplemented in MdlSyntax.

◆ registerInvalidTokens()

void registerInvalidTokens ( const StringMap tokens)

Register a set string replacements for disallowed tokens for a code generator when naming variables and functions.

Multiple calls will add to the internal set of tokens.

◆ registerReservedWords()

void registerReservedWords ( const StringSet names)

Register names that are reserved words not to be used by a code generator when naming variables and functions.

Keywords, types, built-in functions etc. should be added to this set. Multiple calls will add to the internal set of names.

◆ registerTypeSyntax()

void registerTypeSyntax ( const TypeDesc type,
TypeSyntaxPtr  syntax 
)

Register syntax handling for a data type.

Required to be set for all supported data types.

◆ remapEnumeration()

bool remapEnumeration ( const string &  value,
const TypeDesc type,
const string &  enumNames,
std::pair< const TypeDesc *, ValuePtr > &  result 
) const
virtual

Given an input specification attempt to remap this to an enumeration which is accepted by the shader generator.

The enumeration may be converted to a different type than the input.

Parameters
valueThe value string to remap.
typeThe type of the value to remap,
enumNamesType enumeration names
resultEnumeration type and value (returned).
Returns
Return true if the remapping was successful.

Reimplemented in GlslSyntax, and MdlSyntax.

◆ typeSupported()

bool typeSupported ( const TypeDesc type) const
virtual

Query if given type is suppored in the syntax.

By default all types are assumed to be supported.

Reimplemented in GlslSyntax.

Member Data Documentation

◆ CHANNELS_MAPPING

const std::unordered_map< char, size_t > CHANNELS_MAPPING
staticprotected
Initial value:
=
{
{ 'r', 0 }, { 'x', 0 },
{ 'g', 1 }, { 'y', 1 },
{ 'b', 2 }, { 'z', 2 },
{ 'a', 3 }, { 'w', 3 }
}

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