Package com.jme3.shader
Class Glsl100ShaderGenerator
java.lang.Object
com.jme3.shader.ShaderGenerator
com.jme3.shader.Glsl100ShaderGenerator
- Direct Known Subclasses:
Glsl150ShaderGenerator
This shader Generator can generate Vertex and Fragment shaders from
shadernodes for GLSL 1.0
-
Field Summary
Fields inherited from class com.jme3.shader.ShaderGenerator
assetManager, indent, NAME_SPACE_GLOBAL, NAME_SPACE_MAT_PARAM, NAME_SPACE_VERTEX_ATTRIBUTE, NAME_SPACE_WORLD_PARAM, techniqueDef
-
Constructor Summary
ConstructorDescriptionGlsl100ShaderGenerator
(AssetManager assetManager) creates a Glsl100ShaderGenerator -
Method Summary
Modifier and TypeMethodDescriptionprotected void
appendIndent
(StringBuilder source) appends indentation.protected void
appendOutput
(StringBuilder source, String globalOutputName, ShaderNodeVariable var) Appends an output assignment to a shader globalOutputName = nameSpace_varName;protected void
comment
(StringBuilder source, ShaderNode shaderNode, String comment) Appends a comment to the generated codeprotected void
declareAttribute
(StringBuilder source, ShaderNodeVariable var) Declares an attributeprotected void
declareGlobalPosition
(ShaderGenerationInfo info, StringBuilder source) declare and assign the global position in the vertex shader.protected void
declareVariable
(StringBuilder source, ShaderNodeVariable var) declares a variable, embed in a conditional block if needed.protected void
declareVariable
(StringBuilder source, ShaderNodeVariable var, boolean appendNameSpace) declares a variable, embed in a conditional block if neededprotected void
declareVariable
(StringBuilder source, ShaderNodeVariable var, boolean appendNameSpace, String modifier) declares a variable, embed in a conditional block if needed.protected void
declareVariable
(StringBuilder source, ShaderNodeVariable var, String value) declares a variable, embed in a conditional block if needed.protected void
declareVariable
(StringBuilder source, ShaderNodeVariable var, String value, boolean appendNameSpace, String modifier) declares a variable, embed in a conditional block if needed.protected void
declareVarying
(StringBuilder source, ShaderNodeVariable var, boolean input) Declares a varyingprotected void
endCondition
(String condition, StringBuilder source) Ends a conditional blockprotected void
makes sure inPosition attribute is of type vec3 or vec4protected void
generateAttributes
(StringBuilder source, ShaderGenerationInfo info) generates the attributes declaration for the vertex shader.protected void
generateDeclarativeSection
(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info) Appends the given shaderNode declarative part to the shader declarative part.protected void
generateEndOfMainSection
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) Generates the end of the shader main section.protected void
generateNodeMainSection
(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info) Appends the given shaderNode main part to the shader declarative part.protected void
generateStartOfMainSection
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the start of the shader main section.protected void
generateUniforms
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the uniforms declaration for a shader of the given type.protected void
generateUniforms
(StringBuilder source, List<ShaderNodeVariable> uniforms) declare a list of uniformsprotected void
generateVaryings
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the varyings for the given shader type shader.protected String
returns the name space to append for a variable.protected String
returns the language + version of the shader should be something like "GLSL100" for glsl 1.0 "GLSL150" for glsl 1.5.protected void
indent()
increase indentation with a check so that indentation is never over 10protected boolean
Finds if a variable is a varyingprotected boolean
Finds if a variable is a world or a material parameterprotected void
map
(VariableMapping mapping, StringBuilder source, boolean declare) Appends a mapping to the source, embed in a conditional block if needed, with variables nameSpaces and swizzle.protected String
replace
(String nodeSource, ShaderNodeVariable var, String newName) replaced a variable name in a source code with the given nameprotected String
replaceVariableName
(String nodeSource, ShaderNodeVariable var) replaces a variable name in a shaderNode source code by prefixing it with its nameSpace and "_" if needed.protected void
startCondition
(String condition, StringBuilder source) Starts a conditional blockprotected void
unIndent()
Decrease indentation with a check so the indent is never negative.protected String
updateDefinesName
(String nodeSource, ShaderNode shaderNode) transforms defines name is the shader node code.Methods inherited from class com.jme3.shader.ShaderGenerator
appendNodeDeclarationAndMain, buildShader, findShaderIndexFromVersion, generateDeclarationAndMainBody, generateShader, initialize
-
Field Details
-
inPosTmp
-
-
Constructor Details
-
Glsl100ShaderGenerator
creates a Glsl100ShaderGenerator- Parameters:
assetManager
- the assetManager
-
-
Method Details
-
generateUniforms
protected void generateUniforms(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) Description copied from class:ShaderGenerator
generates the uniforms declaration for a shader of the given type.- Specified by:
generateUniforms
in classShaderGenerator
- Parameters:
source
- the source StringBuilder to append generated code.info
- the ShaderGenerationInfo.type
- the shader type the uniforms have to be generated for.
-
generateUniforms
declare a list of uniforms- Parameters:
source
- the source to append touniforms
- the list of uniforms
-
generateAttributes
generates the attributes declaration for the vertex shader. There is no Shader type passed here as attributes are only used in vertex shaders attributes are all declared, inPosition is declared even if it's not in the list and its condition is nulled.- Specified by:
generateAttributes
in classShaderGenerator
- Parameters:
source
- the source StringBuilder to append generated code.info
- the ShaderGenerationInfo.
-
generateVaryings
protected void generateVaryings(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) Description copied from class:ShaderGenerator
generates the varyings for the given shader type shader. Note that varyings are deprecated in glsl 1.3, but this method will still be called to generate all non-global inputs and output of the shaders.- Specified by:
generateVaryings
in classShaderGenerator
- Parameters:
source
- the source StringBuilder to append generated code.info
- the ShaderGenerationInfo.type
- the shader type the varyings have to be generated for.
-
generateDeclarativeSection
protected void generateDeclarativeSection(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info) Appends the given shaderNode declarative part to the shader declarative part. If needed the shader type can be determined by fetching the shaderNode's definition type. if the declaration contains no code nothing is done, else it's appended- Specified by:
generateDeclarativeSection
in classShaderGenerator
- Parameters:
source
- the StringBuilder to append generated code.shaderNode
- the shaderNode.nodeSource
- the declaration part of the nodeinfo
- the ShaderGenerationInfo.- See Also:
-
generateStartOfMainSection
protected void generateStartOfMainSection(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the start of the shader main section. this method is responsible of appending the "void main(){" in the shader and declaring all global outputs of the shader Shader outputs are declared and initialized inside the main section- Specified by:
generateStartOfMainSection
in classShaderGenerator
- Parameters:
source
- the StringBuilder to append generated code.info
- the ShaderGenerationInfo.type
- the shader type the section has to be generated for.
-
generateEndOfMainSection
protected void generateEndOfMainSection(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) Generates the end of the shader main section. This method is responsible for appending the last "}" in the shader and mapping all global outputs of the shader. outputs are assigned to built in glsl output. then the main section is closed This code accounts for multi render target and correctly output to gl_FragData if several output are declared for the fragment shader- Specified by:
generateEndOfMainSection
in classShaderGenerator
- Parameters:
source
- the StringBuilder to append generated code.info
- the ShaderGenerationInfo.type
- the shader type the section has to be generated for.
-
appendOutput
Appends an output assignment to a shader globalOutputName = nameSpace_varName;- Parameters:
source
- the source StringBuilder to append the code.globalOutputName
- the name of the global output (can be gl_Position or gl_FragColor etc...).var
- the variable to assign to the output.
-
generateNodeMainSection
protected void generateNodeMainSection(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info) Appends the given shaderNode main part to the shader declarative part. If needed the shader type can be determined by fetching the shaderNode's definition type. This method does things in the following order: 1. declaring and mapping input
variables : variable replaced with MatParams or WorldParams that are Samplers are not declared and are replaced by the param actual name in the code. For others variables, the name space is appended with a "_" before the variable name in the code to avoid names collision between shaderNodes.
2. declaring output variables :
variables are declared if they were not already declared as input (inputs can also be outputs) or if they are not declared as varyings. The variable name is also prefixed with the s=name space and "_" in the shaderNode code
3. append of the actual ShaderNode code
4. mapping outputs to global output if needed
All of this is embedded in a #if conditional statement if necessary.- Specified by:
generateNodeMainSection
in classShaderGenerator
- Parameters:
source
- the StringBuilder to append generated code.shaderNode
- the shaderNode.nodeSource
- the declaration part of the loaded shaderNode source.info
- the ShaderGenerationInfo.- See Also:
-
declareVariable
protected void declareVariable(StringBuilder source, ShaderNodeVariable var, boolean appendNameSpace) declares a variable, embed in a conditional block if needed- Parameters:
source
- the StringBuilder to usevar
- the variable to declareappendNameSpace
- true to append the nameSpace + "_"
-
declareVariable
declares a variable, embed in a conditional block if needed. the namespace is appended with "_"- Parameters:
source
- the StringBuilder to usevar
- the variable to declare
-
declareVariable
declares a variable, embed in a conditional block if needed. the namespace is appended with "_"- Parameters:
source
- the StringBuilder to usevar
- the variable to declarevalue
- the initialization value to assign the variable
-
declareVariable
protected void declareVariable(StringBuilder source, ShaderNodeVariable var, boolean appendNameSpace, String modifier) declares a variable, embed in a conditional block if needed.- Parameters:
source
- the StringBuilder to usevar
- the variable to declareappendNameSpace
- true to append the nameSpace + "_"modifier
- the modifier of the variable (attribute, varying, in , out,...)
-
declareVariable
protected void declareVariable(StringBuilder source, ShaderNodeVariable var, String value, boolean appendNameSpace, String modifier) declares a variable, embed in a conditional block if needed.- Parameters:
source
- the StringBuilder to usevar
- the variable to declarevalue
- the initialization value to assign the variableappendNameSpace
- true to append the nameSpace + "_"modifier
- the modifier of the variable (attribute, varying, in , out,...)
-
startCondition
Starts a conditional block- Parameters:
condition
- the block conditionsource
- the StringBuilder to use
-
endCondition
Ends a conditional block- Parameters:
condition
- the block conditionsource
- the StringBuilder to use
-
map
Appends a mapping to the source, embed in a conditional block if needed, with variables nameSpaces and swizzle.- Parameters:
mapping
- the VariableMapping to appendsource
- the StringBuilder to usedeclare
- true to declare the variable, false if already declared
-
replaceVariableName
replaces a variable name in a shaderNode source code by prefixing it with its nameSpace and "_" if needed.- Parameters:
nodeSource
- the source to modifyvar
- the variable to replace- Returns:
- the modified source
-
isVarying
Finds if a variable is a varying- Parameters:
info
- the ShaderGenerationInfov
- the variable- Returns:
- true is the given variable is a varying
-
comment
Appends a comment to the generated code- Parameters:
source
- the StringBuilder to useshaderNode
- the shader node being processed (to append its name)comment
- the comment to append
-
getAppendableNameSpace
returns the name space to append for a variable. Attributes, WorldParam and MatParam names space must not be appended- Parameters:
var
- the variable- Returns:
- the namespace to append for this variable
-
updateDefinesName
transforms defines name is the shader node code. One can use a #if defined(inputVariableName) in a shaderNode code. This method is responsible for changing the variable name with the appropriate defined based on the mapping condition of this variable. Complex condition syntax are handled.- Parameters:
nodeSource
- the shaderNode source codeshaderNode
- the ShaderNode being processed- Returns:
- the modified shaderNode source.
-
replace
replaced a variable name in a source code with the given name- Parameters:
nodeSource
- the source to usevar
- the variablenewName
- the new name of the variable- Returns:
- the modified source code
-
isWorldOrMaterialParam
Finds if a variable is a world or a material parameter- Parameters:
var
- the variable- Returns:
- true if the variable is a Word or material parameter
-
getLanguageAndVersion
Description copied from class:ShaderGenerator
returns the language + version of the shader should be something like "GLSL100" for glsl 1.0 "GLSL150" for glsl 1.5.- Specified by:
getLanguageAndVersion
in classShaderGenerator
- Parameters:
type
- the shader type for which the version should be returned.- Returns:
- the shaderLanguage and version.
-
appendIndent
appends indentation.- Parameters:
source
- the builder to append to
-
declareAttribute
Declares an attribute- Parameters:
source
- the StringBuilder to usevar
- the variable to declare as an attribute
-
declareVarying
Declares a varying- Parameters:
source
- the StringBuilder to usevar
- the variable to declare as a varyinginput
- a boolean set to true if the varying is an input. This in not used in this implementation, but can be used in overriding implementations.
-
unIndent
protected void unIndent()Decrease indentation with a check so the indent is never negative. -
indent
protected void indent()increase indentation with a check so that indentation is never over 10 -
fixInPositionType
makes sure inPosition attribute is of type vec3 or vec4- Parameters:
var
- the inPosition attribute
-
declareGlobalPosition
declare and assign the global position in the vertex shader.- Parameters:
info
- the shader generation infosource
- the shader source being generated
-