Package com.jme3.shader
Class ShaderGenerator
java.lang.Object
com.jme3.shader.ShaderGenerator
- Direct Known Subclasses:
Glsl100ShaderGenerator
This class is the base for a shader generator using the ShaderNodes system,
it contains basis mechanism of generation, but no actual generation code.
This class is abstract, any Shader generator must extend it.
-
Field Summary
Modifier and TypeFieldDescriptionprotected AssetManager
the asset managerprotected int
indentation value for generationstatic final String
static final String
static final String
static final String
protected TechniqueDef
the technique def to use for the shader generation -
Constructor Summary
ModifierConstructorDescriptionprotected
ShaderGenerator
(AssetManager assetManager) Build a shaderGenerator -
Method Summary
Modifier and TypeMethodDescriptionprotected void
appendNodeDeclarationAndMain
(String loadedSource, StringBuilder sourceDeclaration, StringBuilder source, ShaderNode shaderNode, ShaderGenerationInfo info, String shaderPath) Appends declaration and main part of a node to the shader declaration and main part.protected String
buildShader
(List<ShaderNode> shaderNodes, ShaderGenerationInfo info, Shader.ShaderType type) This method is responsible for the shader generation.protected int
findShaderIndexFromVersion
(ShaderNode shaderNode, Shader.ShaderType type) Returns the shader-path index according to the version of the generator.protected abstract void
generateAttributes
(StringBuilder source, ShaderGenerationInfo info) generates the attributes declaration for the vertex shader.protected void
generateDeclarationAndMainBody
(List<ShaderNode> shaderNodes, StringBuilder sourceDeclaration, StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) iterates through shader nodes to load them and generate the shader declaration part and main body extracted from the shader nodes, for the given shader typeprotected abstract void
generateDeclarativeSection
(StringBuilder source, ShaderNode shaderNode, String nodeDeclarationSource, ShaderGenerationInfo info) Appends the given shaderNode declarative part to the shader declarative part.protected abstract void
generateEndOfMainSection
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) Generates the end of the shader main section.protected abstract void
generateNodeMainSection
(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info) Appends the given shaderNode main part to the shader declarative part.generateShader
(String definesSourceCode) Generate vertex and fragment shaders for the given techniqueprotected abstract void
generateStartOfMainSection
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the start of the shader main section.protected abstract void
generateUniforms
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the uniforms declaration for a shader of the given type.protected abstract void
generateVaryings
(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the varyings for the given shader type shader.protected abstract String
returns the language + version of the shader should be something like "GLSL100" for glsl 1.0 "GLSL150" for glsl 1.5.void
initialize
(TechniqueDef techniqueDef)
-
Field Details
-
NAME_SPACE_GLOBAL
- See Also:
-
NAME_SPACE_VERTEX_ATTRIBUTE
- See Also:
-
NAME_SPACE_MAT_PARAM
- See Also:
-
NAME_SPACE_WORLD_PARAM
- See Also:
-
assetManager
the asset manager -
indent
protected int indentindentation value for generation -
techniqueDef
the technique def to use for the shader generation
-
-
Constructor Details
-
ShaderGenerator
Build a shaderGenerator- Parameters:
assetManager
- for loading assets (alias created)
-
-
Method Details
-
initialize
-
generateShader
Generate vertex and fragment shaders for the given technique- Parameters:
definesSourceCode
- (may be null)- Returns:
- a Shader program
-
buildShader
protected String buildShader(List<ShaderNode> shaderNodes, ShaderGenerationInfo info, Shader.ShaderType type) This method is responsible for the shader generation.- Parameters:
shaderNodes
- the list of shader nodesinfo
- the ShaderGenerationInfo filled during the Technique loadingtype
- the type of shader to generate- Returns:
- the code of the generated vertex shader
-
generateDeclarationAndMainBody
protected void generateDeclarationAndMainBody(List<ShaderNode> shaderNodes, StringBuilder sourceDeclaration, StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) iterates through shader nodes to load them and generate the shader declaration part and main body extracted from the shader nodes, for the given shader type- Parameters:
shaderNodes
- the list of shader nodessourceDeclaration
- the declaration part StringBuilder of the shader to generatesource
- the main part StringBuilder of the shader to generateinfo
- the ShaderGenerationInfotype
- the Shader type
-
appendNodeDeclarationAndMain
protected void appendNodeDeclarationAndMain(String loadedSource, StringBuilder sourceDeclaration, StringBuilder source, ShaderNode shaderNode, ShaderGenerationInfo info, String shaderPath) Appends declaration and main part of a node to the shader declaration and main part. the loadedSource is split by "void main(){" to split declaration from main part of the node source code.The trailing "}" is removed from the main part. Each part is then respectively passed to generateDeclarativeSection and generateNodeMainSection.- Parameters:
loadedSource
- the actual source code loaded for this node.shaderPath
- path to the shader filesourceDeclaration
- the Shader declaration part string builder.source
- the Shader main part StringBuilder.shaderNode
- the shader node.info
- the ShaderGenerationInfo.- See Also:
-
getLanguageAndVersion
returns the language + version of the shader should be something like "GLSL100" for glsl 1.0 "GLSL150" for glsl 1.5.- Parameters:
type
- the shader type for which the version should be returned.- Returns:
- the shaderLanguage and version.
-
generateUniforms
protected abstract void generateUniforms(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) generates the uniforms declaration for a shader of the given type.- Parameters:
source
- the source StringBuilder to append generated code.info
- the ShaderGenerationInfo.type
- the shader type the uniforms have to be generated for.
-
generateAttributes
generates the attributes declaration for the vertex shader. There is no Shader type passed here as attributes are only used in vertex shaders- Parameters:
source
- the source StringBuilder to append generated code.info
- the ShaderGenerationInfo.
-
generateVaryings
protected abstract void generateVaryings(StringBuilder source, ShaderGenerationInfo info, Shader.ShaderType type) 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.- 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 abstract void generateDeclarativeSection(StringBuilder source, ShaderNode shaderNode, String nodeDeclarationSource, 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.- Parameters:
nodeDeclarationSource
- the declaration part of the nodesource
- the StringBuilder to append generated code.shaderNode
- the shaderNode.info
- the ShaderGenerationInfo.- See Also:
-
generateStartOfMainSection
protected abstract 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- Parameters:
source
- the StringBuilder to append generated code.info
- the ShaderGenerationInfo.type
- the shader type the section has to be generated for.
-
generateEndOfMainSection
protected abstract 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.- Parameters:
source
- the StringBuilder to append generated code.info
- the ShaderGenerationInfo.type
- the shader type the section has to be generated for.
-
generateNodeMainSection
protected abstract 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.- 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:
-
findShaderIndexFromVersion
protected int findShaderIndexFromVersion(ShaderNode shaderNode, Shader.ShaderType type) throws NumberFormatException Returns the shader-path index according to the version of the generator. This allows selecting the highest version of the shader that the generator can handle.- Parameters:
shaderNode
- the shaderNode being processedtype
- the shaderType- Returns:
- the index of the shader path in ShaderNodeDefinition shadersPath list
- Throws:
NumberFormatException
- for an invalid version
-