Class Glsl100ShaderGenerator

java.lang.Object
com.jme3.shader.ShaderGenerator
com.jme3.shader.Glsl100ShaderGenerator
Direct Known Subclasses:
Glsl150ShaderGenerator

public class Glsl100ShaderGenerator extends ShaderGenerator
This shader Generator can generate Vertex and Fragment shaders from shadernodes for GLSL 1.0
  • Field Details

  • Constructor Details

    • Glsl100ShaderGenerator

      public Glsl100ShaderGenerator(AssetManager assetManager)
      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 class ShaderGenerator
      Parameters:
      source - the source StringBuilder to append generated code.
      info - the ShaderGenerationInfo.
      type - the shader type the uniforms have to be generated for.
    • generateUniforms

      protected void generateUniforms(StringBuilder source, List<ShaderNodeVariable> uniforms)
      declare a list of uniforms
      Parameters:
      source - the source to append to
      uniforms - the list of uniforms
    • generateAttributes

      protected void generateAttributes(StringBuilder source, ShaderGenerationInfo info)
      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 class ShaderGenerator
      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 class ShaderGenerator
      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 class ShaderGenerator
      Parameters:
      source - the StringBuilder to append generated code.
      shaderNode - the shaderNode.
      nodeSource - the declaration part of the node
      info - 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 class ShaderGenerator
      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 class ShaderGenerator
      Parameters:
      source - the StringBuilder to append generated code.
      info - the ShaderGenerationInfo.
      type - the shader type the section has to be generated for.
    • appendOutput

      protected void appendOutput(StringBuilder source, String globalOutputName, ShaderNodeVariable var)
      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 class ShaderGenerator
      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 use
      var - the variable to declare
      appendNameSpace - true to append the nameSpace + "_"
    • declareVariable

      protected void declareVariable(StringBuilder source, ShaderNodeVariable var)
      declares a variable, embed in a conditional block if needed. the namespace is appended with "_"
      Parameters:
      source - the StringBuilder to use
      var - the variable to declare
    • declareVariable

      protected void declareVariable(StringBuilder source, ShaderNodeVariable var, String value)
      declares a variable, embed in a conditional block if needed. the namespace is appended with "_"
      Parameters:
      source - the StringBuilder to use
      var - the variable to declare
      value - 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 use
      var - the variable to declare
      appendNameSpace - 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 use
      var - the variable to declare
      value - the initialization value to assign the variable
      appendNameSpace - true to append the nameSpace + "_"
      modifier - the modifier of the variable (attribute, varying, in , out,...)
    • startCondition

      protected void startCondition(String condition, StringBuilder source)
      Starts a conditional block
      Parameters:
      condition - the block condition
      source - the StringBuilder to use
    • endCondition

      protected void endCondition(String condition, StringBuilder source)
      Ends a conditional block
      Parameters:
      condition - the block condition
      source - the StringBuilder to use
    • map

      protected 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.
      Parameters:
      mapping - the VariableMapping to append
      source - the StringBuilder to use
      declare - true to declare the variable, false if already declared
    • replaceVariableName

      protected String replaceVariableName(String nodeSource, ShaderNodeVariable var)
      replaces a variable name in a shaderNode source code by prefixing it with its nameSpace and "_" if needed.
      Parameters:
      nodeSource - the source to modify
      var - the variable to replace
      Returns:
      the modified source
    • isVarying

      protected boolean isVarying(ShaderGenerationInfo info, ShaderNodeVariable v)
      Finds if a variable is a varying
      Parameters:
      info - the ShaderGenerationInfo
      v - the variable
      Returns:
      true is the given variable is a varying
    • comment

      protected void comment(StringBuilder source, ShaderNode shaderNode, String comment)
      Appends a comment to the generated code
      Parameters:
      source - the StringBuilder to use
      shaderNode - the shader node being processed (to append its name)
      comment - the comment to append
    • getAppendableNameSpace

      protected String getAppendableNameSpace(ShaderNodeVariable var)
      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

      protected String updateDefinesName(String nodeSource, ShaderNode shaderNode)
      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 code
      shaderNode - the ShaderNode being processed
      Returns:
      the modified shaderNode source.
    • replace

      protected String replace(String nodeSource, ShaderNodeVariable var, String newName)
      replaced a variable name in a source code with the given name
      Parameters:
      nodeSource - the source to use
      var - the variable
      newName - the new name of the variable
      Returns:
      the modified source code
    • isWorldOrMaterialParam

      protected boolean isWorldOrMaterialParam(ShaderNodeVariable var)
      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

      protected String getLanguageAndVersion(Shader.ShaderType type)
      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 class ShaderGenerator
      Parameters:
      type - the shader type for which the version should be returned.
      Returns:
      the shaderLanguage and version.
    • appendIndent

      protected void appendIndent(StringBuilder source)
      appends indentation.
      Parameters:
      source - the builder to append to
    • declareAttribute

      protected void declareAttribute(StringBuilder source, ShaderNodeVariable var)
      Declares an attribute
      Parameters:
      source - the StringBuilder to use
      var - the variable to declare as an attribute
    • declareVarying

      protected void declareVarying(StringBuilder source, ShaderNodeVariable var, boolean input)
      Declares a varying
      Parameters:
      source - the StringBuilder to use
      var - the variable to declare as a varying
      input - 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

      protected void fixInPositionType(ShaderNodeVariable var)
      makes sure inPosition attribute is of type vec3 or vec4
      Parameters:
      var - the inPosition attribute
    • declareGlobalPosition

      protected void declareGlobalPosition(ShaderGenerationInfo info, StringBuilder source)
      declare and assign the global position in the vertex shader.
      Parameters:
      info - the shader generation info
      source - the shader source being generated