Top Description Fields Constructors Methods
com.jogamp.opengl.util.glsl

public Class ShaderCode

extends Object
Class Inheritance
Imports
java.io.BufferedInputStream, .BufferedReader, .FileNotFoundException, .IOException, .InputStreamReader, .PrintStream, .StringReader, java.net.URISyntaxException, .URLConnection, java.nio.Buffer, .ByteBuffer, .IntBuffer, java.util.Arrays, .Iterator, .Set, com.jogamp.opengl.GL, .GL2ES2, .GL3, .GL3ES3, .GL4, .GLES2, .GLContext, .GLException, jogamp.opengl.Debug, com.jogamp.common.net.Uri, com.jogamp.common.nio.Buffers, com.jogamp.common.util.IOUtil, .VersionNumber

Convenient shader code class to use and instantiate vertex or fragment programs.

A documented example of how to use this code is available here and here.

Support for GL4#GL_TESS_CONTROL_SHADER and GL4#GL_TESS_EVALUATION_SHADER was added since 2.2.1.

Field Summary

Modifier and TypeField and Description
public static final boolean
public static final String
DISABLE

Behavior for GLSL extension directive, see createExtensionDirective(String, String), value .

public static final String
ENABLE

Behavior for GLSL extension directive, see createExtensionDirective(String, String), value .

public static final String
es2_default_precision_fp

Default precision of ES2 for fragment-shader: precision mediump float; precision mediump int; /*precision lowp sampler2D;*/ /*precision lowp samplerCube;*/

public static final String
es2_default_precision_vp

Default precision of ES2 for vertex-shader: precision highp float; precision highp int; /*precision lowp sampler2D;*/ /*precision lowp samplerCube;*/

public static final String
es3_default_precision_fp

Default precision of ES3 for fragment-shader: precision highp float; precision highp int; /*precision lowp sampler2D;*/ /*precision lowp samplerCube;*/ , same as for vertex-shader, i.e es3_default_precision_vp, due to ES 3.x requirements of using same precision for uniforms!

public static final String
es3_default_precision_vp

Default precision of ES3 for vertex-shader: precision highp float; precision highp int; /*precision lowp sampler2D;*/ /*precision lowp samplerCube;*/

public static final String
gl3_default_precision_fp

Default precision of GLSL ≥ 1.30 as required until < 1.50 for fragment-shader: precision highp float; precision mediump int; /*precision mediump sampler2D;*/ .

public static final String
gl3_default_precision_vp_gp

Default precision of GLSL ≥ 1.30 as required until < 1.50 for vertex-shader or geometry-shader: precision highp float; precision highp int; .

protected int
protected static int
public static final String
REQUIRE

Behavior for GLSL extension directive, see createExtensionDirective(String, String), value .

protected IntBuffer
protected Buffer
protected int
protected CharSequence[][]
protected int
public static final String
SUB_PATH_NVIDIA

Unique relative path for binary shader resources for NVIDIA:

public static final String
SUFFIX_COMPUTE_BINARY

Unique resource suffix for GL3ES3#GL_COMPUTE_SHADER in binary:

public static final String
SUFFIX_COMPUTE_SOURCE

Unique resource suffix for GL3ES3#GL_COMPUTE_SHADER in source code:

public static final String
SUFFIX_FRAGMENT_BINARY

Unique resource suffix for GL2ES2#GL_FRAGMENT_SHADER in binary:

public static final String
SUFFIX_FRAGMENT_SOURCE

Unique resource suffix for GL2ES2#GL_FRAGMENT_SHADER in source code:

public static final String
SUFFIX_GEOMETRY_BINARY

Unique resource suffix for GL3#GL_GEOMETRY_SHADER in binary:

public static final String
SUFFIX_GEOMETRY_SOURCE

Unique resource suffix for GL3#GL_GEOMETRY_SHADER in source code:

public static final String
SUFFIX_TESS_CONTROL_BINARY

Unique resource suffix for GL4#GL_TESS_CONTROL_SHADER in binary:

public static final String
SUFFIX_TESS_CONTROL_SOURCE

Unique resource suffix for GL4#GL_TESS_CONTROL_SHADER in source code:

public static final String
SUFFIX_TESS_EVALUATION_BINARY

Unique resource suffix for GL4#GL_TESS_EVALUATION_SHADER in binary:

public static final String
SUFFIX_TESS_EVALUATION_SOURCE

Unique resource suffix for GL4#GL_TESS_EVALUATION_SHADER in source code:

public static final String
SUFFIX_VERTEX_BINARY

Unique resource suffix for GL2ES2#GL_VERTEX_SHADER in binary:

public static final String
SUFFIX_VERTEX_SOURCE

Unique resource suffix for GL2ES2#GL_VERTEX_SHADER in source code:

protected boolean
public static final String
WARN

Behavior for GLSL extension directive, see createExtensionDirective(String, String), value .

Constructor Summary

AccessConstructor and Description
public
ShaderCode(final int type, final int
number of shaders
count
,
final CharSequence[][]
CharSequence array containing the shader sources, organized as source[count][strings-per-shader]. May be either an immutable String - or mutable StringBuilder array.
source
)

public
ShaderCode(final int type, final int
number of shaders
count
,
final int binFormat, final Buffer
binary buffer containing the shader binaries,
binary
)

Method Summary

Modifier and TypeMethod and Description
public final int

Returns:

the index after the inserted data, maybe 0 if nothing has be inserted.
addDefaultShaderPrecision
(final GL2ES2
a GL context, which must have been made current once
gl
,
int
position within this mutable shader source.
pos
)

Adds default precision to source code at given position if required, i.e. es2_default_precision_vp, es2_default_precision_fp, gl3_default_precision_vp_gp, gl3_default_precision_fp or none, depending on the GLSL version being used.

public final int

Returns:

the index after the inserted data, maybe 0 if nothing has be inserted.
addGLSLVersion
(final GL2ES2
a GL context, which must have been made current once
gl
)

Add GLSL version at the head of this shader source code.

public boolean
compile(final GL2ES2 gl)

public boolean
compile(final GL2ES2 gl, final PrintStream verboseOut)

public static ShaderCode
create(final GL2ES2
current GL object to determine whether a shader compiler is available. If null, no validation is performed.
gl
,
final int type, final int
number of shaders
count
,
final Class<?>
class used to help resolving the source location
context
,
final String[]
array of source locations, organized as sourceFiles[count] -> shaderSources[count][1]
sourceFiles
,
final boolean
if true method returns a mutable StringBuilder instance which can be edited later on at the costs of a String conversion when passing to GL2ES2#glShaderSource(int, int, String[], IntBuffer). If false method returns an immutable String instance, which can be passed to GL2ES2#glShaderSource(int, int, String[], IntBuffer) at no additional costs.
mutableStringBuilder
)

Creates a complete ShaderCode object while reading all shader source of sourceFiles, which location is resolved using the context class, see readShaderSource(Class, String).

public static ShaderCode
create(final GL2ES2
current GL object to determine whether a shader compiler is available. If null, no validation is performed.
gl
,
final int type, final int
number of shaders
count
,
final Uri[]
array of Uri source locations, organized as sourceFiles[count] -> shaderSources[count][1]
sourceLocations
,
final boolean
if true method returns a mutable StringBuilder instance which can be edited later on at the costs of a String conversion when passing to GL2ES2#glShaderSource(int, int, String[], IntBuffer). If false method returns an immutable String instance, which can be passed to GL2ES2#glShaderSource(int, int, String[], IntBuffer) at no additional costs.
mutableStringBuilder
)

Creates a complete ShaderCode object while reading all shader sources from Uri sourceLocations via readShaderSource(Uri, boolean).

public static ShaderCode
create(final int type, final int
number of shaders
count
,
final Class<?>
class used to help resolving the source location
context
,
int
a valid native binary format as they can be queried by ShaderUtil#getShaderBinaryFormats(GL).
binFormat
,
final String
array of source locations, organized as sourceFiles[count]
binaryFile
)

Creates a complete ShaderCode object while reading the shader binary of binaryFile, which location is resolved using the context class, see readShaderBinary(Class, String).

public static ShaderCode
create(final int type, final int
number of shaders
count
,
int
a valid native binary format as they can be queried by ShaderUtil#getShaderBinaryFormats(GL).
binFormat
,
final Uri
Uri binary location
binLocation
)

Creates a complete ShaderCode object while reading the shader binary from Uri binLocations via readShaderBinary(Uri).

public static ShaderCode
create(final GL2ES2
current GL object to determine whether a shader compiler is available (if source is used), or to determine the shader binary format (if binary is used).
gl
,
final int type, final int
number of shaders
count
,
final Class<?>
class used to help resolving the source and binary location
context
,
final String
relative root path for srcBasenames optional
srcRoot
,
final String[]
basenames w/o path or suffix relative to srcRoot for the shader's source code
srcBasenames
,
final String
optional custom suffix for shader's source file, if null getFileSuffix(boolean, int) is being used.
srcSuffixOpt
,
final String
relative root path for binBasenames
binRoot
,
final String
basename w/o path or suffix relative to binRoot for the shader's binary code
binBasename
,
final String
optional custom suffix for shader's binary file, if null getFileSuffix(boolean, int) is being used.
binSuffixOpt
,
final boolean
if true method returns a mutable StringBuilder instance which can be edited later on at the costs of a String conversion when passing to GL2ES2#glShaderSource(int, int, String[], IntBuffer). If false method returns an immutable String instance, which can be passed to GL2ES2#glShaderSource(int, int, String[], IntBuffer) at no additional costs.
mutableStringBuilder
)

Convenient creation method for instantiating a complete ShaderCode object either from source code using create(GL2ES2, int, int, Class, String[]), or from a binary code using create(int, int, Class, int, String), whatever is available first.

public static ShaderCode
create(final GL2ES2
current GL object to determine whether a shader compiler is available (if source is used), or to determine the shader binary format (if binary is used).
gl
,
final int type, final int
number of shaders
count
,
final Class<?>
class used to help resolving the source and binary location
context
,
final String
relative root path for srcBasenames optional
srcRoot
,
final String[]
basenames w/o path or suffix relative to srcRoot for the shader's source code
srcBasenames
,
final String
relative root path for binBasenames
binRoot
,
final String
basename w/o path or suffix relative to binRoot for the shader's binary code
binBasename
,
final boolean
if true method returns a mutable StringBuilder instance which can be edited later on at the costs of a String conversion when passing to GL2ES2#glShaderSource(int, int, String[], IntBuffer). If false method returns an immutable String instance, which can be passed to GL2ES2#glShaderSource(int, int, String[], IntBuffer) at no additional costs.
mutableStringBuilder
)

Simplified variation of create(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean).

public static ShaderCode
create(final GL2ES2
current GL object to determine whether a shader compiler is available (if source is used), or to determine the shader binary format (if binary is used).
gl
,
final int type, final Class<?>
class used to help resolving the source and binary location
context
,
final String
relative root path for basename optional
srcRoot
,
final String
relative root path for basename
binRoot
,
final String
basename w/o path or suffix relative to srcRoot and binRoot for the shader's source and binary code.
basename
,
final String
optional custom suffix for shader's source file, if null getFileSuffix(boolean, int) is being used.
srcSuffixOpt
,
final String
optional custom suffix for shader's binary file, if null getFileSuffix(boolean, int) is being used.
binSuffixOpt
,
final boolean
if true method returns a mutable StringBuilder instance which can be edited later on at the costs of a String conversion when passing to GL2ES2#glShaderSource(int, int, String[], IntBuffer). If false method returns an immutable String instance, which can be passed to GL2ES2#glShaderSource(int, int, String[], IntBuffer) at no additional costs.
mutableStringBuilder
)

Simplified variation of create(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean).

public static ShaderCode
create(final GL2ES2
current GL object to determine whether a shader compiler is available (if source is used), or to determine the shader binary format (if binary is used).
gl
,
final int type, final Class<?>
class used to help resolving the source and binary location
context
,
final String
relative root path for basename optional
srcRoot
,
final String
relative root path for basename
binRoot
,
final String basename, final boolean
mutableStringBuilder
)

Simplified variation of create(GL2ES2, int, Class, String, String, String, String, String, boolean).

public static String

Returns:

the complete extension directive
createExtensionDirective
(final String extensionName, final String
shall be either REQUIRE, ENABLE, DISABLE or WARN
behavior
)

Creates a GLSL extension directive.

public final int

Returns:

the index after the inserted data, maybe 0 if nothing has be inserted.
defaultShaderCustomization
(final GL2ES2
a GL context, which must have been made current once
gl
,
final boolean
if true GLContext#getGLSLVersionString() is preluded, otherwise not.
preludeVersion
,
final boolean
if true default precision source code line(s) are added, i.e. es2_default_precision_vp, es2_default_precision_fp, gl3_default_precision_vp_gp, gl3_default_precision_fp or none, depending on the GLSL version being used.
addDefaultPrecision
)

Default customization of this shader source code.

public final int

Returns:

the index after the inserted data, maybe 0 if nothing has be inserted.
defaultShaderCustomization
(final GL2ES2
a GL context, which must have been made current once
gl
,
final boolean
if true GLContext#getGLSLVersionString() is preluded, otherwise not.
preludeVersion
,
final String
optional default precision source code line(s) preluded if not null and if GL#isGLES(). You may use es2_default_precision_fp for fragment shader and es2_default_precision_vp for vertex shader.
esDefaultPrecision
)

Default customization of this shader source code.

public void
destroy(final GL2ES2 gl)

public void
public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public static String
getBinarySubPath(final int binFormat)

Returns a unique relative path for binary shader resources as follows:

public static String
private static synchronized int
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public int
id()

returns the uniq shader id as an integer

public int

Returns:

index after the inserted data
insertShaderSource
(final int
the shader index to be used.
shaderIdx
,
final String
search string
tag
,
final int
start search tag begininig with this index
fromIndex
,
final CharSequence
the text to be inserted. Shall end with an EOL '\n' character.
data
)

Adds data after the line containing tag.

public int

Returns:

index after the inserted data
insertShaderSource
(final int
the shader index to be used.
shaderIdx
,
int
in shader source segments of shader shaderIdx, -1 will append data
position
,
final CharSequence
the text to be inserted. Shall end with an EOL '\n' character
data
)

Adds data at position in shader source for shader shaderIdx.

public int

Returns:

index after the inserted code.
insertShaderSource
(final int
the shader index to be used.
shaderIdx
,
final int
in shader source segments of shader shaderIdx, -1 will append data
position
,
final Class<?>
class used to help resolve the source location
context
,
final String
location of shader source
path
)

Adds shader source located in path, either relative to the context class or absolute as-is at position in shader source for shader shaderIdx.

public boolean
public static ByteBuffer
readShaderBinary(final Class<?>
class used to help resolve the source location
context
,
final String
location of shader binary
path
)

Reads shader binary located in path, either relative to the context class or absolute as-is.

public static ByteBuffer
readShaderBinary(final Uri
Uri location of shader binary
binLocation
)

Reads shader binary located from Uri#absolute Uri binLocation.

private static int
readShaderSource(final Class<?> context, final URLConnection conn, final StringBuilder result, int lineno)

public static void
readShaderSource(final Class<?>
class used to help resolve the source location, may be null
context
,
final URLConnection
the URLConnection of the shader source
conn
,
final StringBuilder
StringBuilder sink for the resulting shader source code
result
)

Reads shader source located in conn.

public static CharSequence
readShaderSource(final Class<?>
class used to help resolve the source location
context
,
final String
location of shader source
path
,
final boolean
if true method returns a mutable StringBuilder instance which can be edited later on at the costs of a String conversion when passing to GL2ES2#glShaderSource(int, int, String[], IntBuffer). If false method returns an immutable String instance, which can be passed to GL2ES2#glShaderSource(int, int, String[], IntBuffer) at no additional costs.
mutableStringBuilder
)

Reads shader source located in path, either relative to the context class or absolute as-is.

public static CharSequence
readShaderSource(final Uri
Uri location of shader source
sourceLocation
,
final boolean
if true method returns a mutable StringBuilder instance which can be edited later on at the costs of a String conversion when passing to GL2ES2#glShaderSource(int, int, String[], IntBuffer). If false method returns an immutable String instance, which can be passed to GL2ES2#glShaderSource(int, int, String[], IntBuffer) at no additional costs.
mutableStringBuilder
)

Reads shader source located from Uri#absolute Uri sourceLocation.

public int

Returns:

the number of replacements
replaceInShaderSource
(final String
the to be replace string
oldName
,
final String
the replacement string
newName
)

Replaces oldName with newName in all shader sources.

public static final boolean
requiresDefaultPrecision(final GL2ES2 gl)

Returns true, if GLSL version requires default precision, i.e. ES2 or GLSL [1.30 ..

public static final boolean
requiresGL3DefaultPrecision(final GL2ES2 gl)

Returns true, if GL3 GLSL version requires default precision, i.e. GLSL [1.30 ..

public IntBuffer
public Buffer
public int
public CharSequence[][]
public int
public String
public static String
shaderTypeStr(final int type)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait