Class ShaderCode
- java.lang.Object
-
- com.jogamp.opengl.util.glsl.ShaderCode
-
public final class ShaderCode extends Object
Convenient shader code class to use and instantiate vertex or fragment programs.A documented example of how to use this code is available
hereandhere.Support for
GL3ES3.GL_TESS_CONTROL_SHADERandGL3ES3.GL_TESS_EVALUATION_SHADERwas added since 2.2.1.
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEBUG_CODEstatic StringDISABLEBehavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "disable".static StringENABLEBehavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "enable".static Stringes2_default_precision_fpDefault precision ofES2forfragment-shader: "\nprecision mediump float;\nprecision mediump int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n"static Stringes2_default_precision_vpDefault precision ofES2forvertex-shader: "\nprecision highp float;\nprecision highp int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n"static Stringes3_default_precision_fpDefault precision ofES3forfragment-shader: "\nprecision highp float;\nprecision highp int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n", same as forvertex-shader, i.ees3_default_precision_vp, due to ES 3.x requirements of using same precision for uniforms!static Stringes3_default_precision_vpDefault precision ofES3forvertex-shader: "\nprecision highp float;\nprecision highp int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n"static Stringgl3_default_precision_fpDefault precision of GLSL ≥ 1.30 as required until < 1.50 forfragment-shader: "\nprecision highp float;\nprecision mediump int;\n/*precision mediump sampler2D;*/\n".static Stringgl3_default_precision_vp_gpDefault precision of GLSL ≥ 1.30 as required until < 1.50 forvertex-shaderorgeometry-shader: "\nprecision highp float;\nprecision highp int;\n".static StringREQUIREBehavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "require".static StringSUB_PATH_NVIDIAUnique relative path for binary shader resources forNVIDIA:"nvidia"static StringSUFFIX_COMPUTE_BINARYUnique resource suffix forGL3ES3.GL_COMPUTE_SHADERin binary:"bcp"static StringSUFFIX_COMPUTE_SOURCEUnique resource suffix forGL3ES3.GL_COMPUTE_SHADERin source code:"cp"static StringSUFFIX_FRAGMENT_BINARYUnique resource suffix forGL2ES2.GL_FRAGMENT_SHADERin binary:"bfp"static StringSUFFIX_FRAGMENT_SOURCEUnique resource suffix forGL2ES2.GL_FRAGMENT_SHADERin source code:"fp"static StringSUFFIX_GEOMETRY_BINARYUnique resource suffix forGL3ES3.GL_GEOMETRY_SHADERin binary:"bgp"static StringSUFFIX_GEOMETRY_SOURCEUnique resource suffix forGL3ES3.GL_GEOMETRY_SHADERin source code:"gp"static StringSUFFIX_TESS_CONTROL_BINARYUnique resource suffix forGL3ES3.GL_TESS_CONTROL_SHADERin binary:"btcp"static StringSUFFIX_TESS_CONTROL_SOURCEUnique resource suffix forGL3ES3.GL_TESS_CONTROL_SHADERin source code:"tcp"static StringSUFFIX_TESS_EVALUATION_BINARYUnique resource suffix forGL3ES3.GL_TESS_EVALUATION_SHADERin binary:"btep"static StringSUFFIX_TESS_EVALUATION_SOURCEUnique resource suffix forGL3ES3.GL_TESS_EVALUATION_SHADERin source code:"tep"static StringSUFFIX_VERTEX_BINARYUnique resource suffix forGL2ES2.GL_VERTEX_SHADERin binary:"bvp"static StringSUFFIX_VERTEX_SOURCEUnique resource suffix forGL2ES2.GL_VERTEX_SHADERin source code:"vp"static StringWARNBehavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "warn".
-
Constructor Summary
Constructors Constructor Description ShaderCode(int type, int count, int binFormat, Buffer binary)ShaderCode(int type, int count, CharSequence[][] source)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddDefaultShaderPrecision(GL2ES2 gl, int pos)Adds default precision to source code at given position if required, i.e.intaddGLSLVersion(GL2ES2 gl)Add GLSL version at the head of this shader source code.booleancompile(GL2ES2 gl)booleancompile(GL2ES2 gl, PrintStream verboseOut)static ShaderCodecreate(int type, int count, int binFormat, Uri binLocation)Creates a completeShaderCodeobject while reading the shader binary fromUribinLocationsviareadShaderBinary(Uri).static ShaderCodecreate(int type, int count, Class<?> context, int binFormat, String binaryFile)Creates a completeShaderCodeobject while reading the shader binary ofbinaryFile, which location is resolved using thecontextclass, seereadShaderBinary(Class, String).static ShaderCodecreate(GL2ES2 gl, int type, int count, Uri[] sourceLocations, boolean mutableStringBuilder)Creates a completeShaderCodeobject while reading all shader sources fromUrisourceLocationsviareadShaderSource(Uri, boolean).static ShaderCodecreate(GL2ES2 gl, int type, int count, Class<?> context, String[] sourceFiles, boolean mutableStringBuilder)Creates a completeShaderCodeobject while reading all shader source ofsourceFiles, which location is resolved using thecontextclass, see#readShaderSource(Class, String).static ShaderCodecreate(GL2ES2 gl, int type, int count, Class<?> context, String srcRoot, String[] srcBasenames, String binRoot, String binBasename, boolean mutableStringBuilder)Simplified variation ofcreate(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean).static ShaderCodecreate(GL2ES2 gl, int type, int count, Class<?> context, String srcRoot, String[] srcBasenames, String srcSuffixOpt, String binRoot, String binBasename, String binSuffixOpt, boolean mutableStringBuilder)Convenient creation method for instantiating a completeShaderCodeobject either from source code using#create(GL2ES2, int, int, Class, String[]), or from a binary code usingcreate(int, int, Class, int, String), whatever is available first.static ShaderCodecreate(GL2ES2 gl, int type, Class<?> context, String srcRoot, String binRoot, String basename, boolean mutableStringBuilder)Simplified variation ofcreate(GL2ES2, int, Class, String, String, String, String, String, boolean).static ShaderCodecreate(GL2ES2 gl, int type, Class<?> context, String srcRoot, String binRoot, String basename, String srcSuffixOpt, String binSuffixOpt, boolean mutableStringBuilder)Simplified variation ofcreate(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean).static StringcreateExtensionDirective(String extensionName, String behavior)Creates a GLSL extension directive.intdefaultShaderCustomization(GL2ES2 gl, boolean preludeVersion, boolean addDefaultPrecision)Default customization of this shader source code.intdefaultShaderCustomization(GL2ES2 gl, boolean preludeVersion, String esDefaultPrecision)Default customization of this shader source code.voiddestroy(GL2ES2 gl)voiddumpSource(PrintStream out)booleanequals(Object obj)static StringgetBinarySubPath(int binFormat)Returns a unique relative path for binary shader resources as follows:NVIDIA:SUB_PATH_NVIDIAstatic StringgetFileSuffix(boolean binary, int type)Returns a unique suffix for shader resources as follows: Sourcevertex:SUFFIX_VERTEX_SOURCEfragment:SUFFIX_FRAGMENT_SOURCEgeometry:SUFFIX_GEOMETRY_SOURCEtess-ctrl:SUFFIX_TESS_CONTROL_SOURCEtess-eval:SUFFIX_TESS_EVALUATION_SOURCEGL3ES3.GL_COMPUTE_SHADER:SUFFIX_COMPUTE_SOURCEBinaryvertex:SUFFIX_VERTEX_BINARYfragment:SUFFIX_FRAGMENT_BINARYgeometry:SUFFIX_GEOMETRY_BINARYtess-ctrl:SUFFIX_TESS_CONTROL_BINARYtess-eval:SUFFIX_TESS_EVALUATION_BINARYGL3ES3.GL_COMPUTE_SHADER:SUFFIX_COMPUTE_BINARYinthashCode()intid()returns the uniq shader id as an integerintinsertShaderSource(int shaderIdx, int position, CharSequence data)Addsdataatpositionin shader source for shadershaderIdx.intinsertShaderSource(int shaderIdx, int position, Class<?> context, String path)Adds shader source located inpath, either relative to thecontextclass or absolute as-is atpositionin shader source for shadershaderIdx.intinsertShaderSource(int shaderIdx, String tag, int fromIndex, CharSequence data)Addsdataafter the line containingtag.booleanisValid()static ByteBufferreadShaderBinary(Uri binLocation)static ByteBufferreadShaderBinary(Class<?> context, String path)Reads shader binary located inpath, either relative to thecontextclass or absolute as-is.static CharSequencereadShaderSource(Uri sourceLocation, boolean mutableStringBuilder)static CharSequencereadShaderSource(Class<?> context, String path, boolean mutableStringBuilder)Reads shader source located inpath, either relative to thecontextclass or absolute as-is.static voidreadShaderSource(Class<?> context, URLConnection conn, StringBuilder result)Reads shader source located inconn.intreplaceInShaderSource(String oldName, String newName)ReplacesoldNamewithnewNamein all shader sources.static booleanrequiresDefaultPrecision(GL2ES2 gl)Returns true, if GLSL version requires default precision, i.e.static booleanrequiresGL3DefaultPrecision(GL2ES2 gl)Returns true, if GL3 GLSL version requires default precision, i.e.IntBuffershader()BuffershaderBinary()intshaderBinaryFormat()CharSequence[][]shaderSource()intshaderType()StringshaderTypeStr()static StringshaderTypeStr(int type)StringtoString()
-
-
-
Field Detail
-
DEBUG_CODE
public static final boolean DEBUG_CODE
-
SUFFIX_VERTEX_SOURCE
public static final String SUFFIX_VERTEX_SOURCE
Unique resource suffix forGL2ES2.GL_VERTEX_SHADERin source code:"vp"- See Also:
- Constant Field Values
-
SUFFIX_VERTEX_BINARY
public static final String SUFFIX_VERTEX_BINARY
Unique resource suffix forGL2ES2.GL_VERTEX_SHADERin binary:"bvp"- See Also:
- Constant Field Values
-
SUFFIX_GEOMETRY_SOURCE
public static final String SUFFIX_GEOMETRY_SOURCE
Unique resource suffix forGL3ES3.GL_GEOMETRY_SHADERin source code:"gp"- See Also:
- Constant Field Values
-
SUFFIX_GEOMETRY_BINARY
public static final String SUFFIX_GEOMETRY_BINARY
Unique resource suffix forGL3ES3.GL_GEOMETRY_SHADERin binary:"bgp"- See Also:
- Constant Field Values
-
SUFFIX_COMPUTE_SOURCE
public static final String SUFFIX_COMPUTE_SOURCE
Unique resource suffix forGL3ES3.GL_COMPUTE_SHADERin source code:"cp"- Since:
- 2.3.2
- See Also:
- Constant Field Values
-
SUFFIX_COMPUTE_BINARY
public static final String SUFFIX_COMPUTE_BINARY
Unique resource suffix forGL3ES3.GL_COMPUTE_SHADERin binary:"bcp"- Since:
- 2.3.2
- See Also:
- Constant Field Values
-
SUFFIX_TESS_CONTROL_SOURCE
public static final String SUFFIX_TESS_CONTROL_SOURCE
Unique resource suffix forGL3ES3.GL_TESS_CONTROL_SHADERin source code:"tcp"- Since:
- 2.2.1
- See Also:
- Constant Field Values
-
SUFFIX_TESS_CONTROL_BINARY
public static final String SUFFIX_TESS_CONTROL_BINARY
Unique resource suffix forGL3ES3.GL_TESS_CONTROL_SHADERin binary:"btcp"- Since:
- 2.2.1
- See Also:
- Constant Field Values
-
SUFFIX_TESS_EVALUATION_SOURCE
public static final String SUFFIX_TESS_EVALUATION_SOURCE
Unique resource suffix forGL3ES3.GL_TESS_EVALUATION_SHADERin source code:"tep"- Since:
- 2.2.1
- See Also:
- Constant Field Values
-
SUFFIX_TESS_EVALUATION_BINARY
public static final String SUFFIX_TESS_EVALUATION_BINARY
Unique resource suffix forGL3ES3.GL_TESS_EVALUATION_SHADERin binary:"btep"- Since:
- 2.2.1
- See Also:
- Constant Field Values
-
SUFFIX_FRAGMENT_SOURCE
public static final String SUFFIX_FRAGMENT_SOURCE
Unique resource suffix forGL2ES2.GL_FRAGMENT_SHADERin source code:"fp"- See Also:
- Constant Field Values
-
SUFFIX_FRAGMENT_BINARY
public static final String SUFFIX_FRAGMENT_BINARY
Unique resource suffix forGL2ES2.GL_FRAGMENT_SHADERin binary:"bfp"- See Also:
- Constant Field Values
-
SUB_PATH_NVIDIA
public static final String SUB_PATH_NVIDIA
Unique relative path for binary shader resources forNVIDIA:"nvidia"- See Also:
- Constant Field Values
-
es2_default_precision_vp
public static final String es2_default_precision_vp
Default precision ofES2forvertex-shader: "\nprecision highp float;\nprecision highp int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n"- See Also:
- Constant Field Values
-
es2_default_precision_fp
public static final String es2_default_precision_fp
Default precision ofES2forfragment-shader: "\nprecision mediump float;\nprecision mediump int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n"- See Also:
- Constant Field Values
-
es3_default_precision_vp
public static final String es3_default_precision_vp
Default precision ofES3forvertex-shader: "\nprecision highp float;\nprecision highp int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n"- See Also:
- Constant Field Values
-
es3_default_precision_fp
public static final String es3_default_precision_fp
Default precision ofES3forfragment-shader: "\nprecision highp float;\nprecision highp int;\n/*precision lowp sampler2D;*/\n/*precision lowp samplerCube;*/\n", same as forvertex-shader, i.ees3_default_precision_vp, due to ES 3.x requirements of using same precision for uniforms!- See Also:
- Constant Field Values
-
gl3_default_precision_vp_gp
public static final String gl3_default_precision_vp_gp
Default precision of GLSL ≥ 1.30 as required until < 1.50 forvertex-shaderorgeometry-shader: "\nprecision highp float;\nprecision highp int;\n". See GLSL Spec 1.30-1.50 Section 4.5.3.- See Also:
- Constant Field Values
-
gl3_default_precision_fp
public static final String gl3_default_precision_fp
Default precision of GLSL ≥ 1.30 as required until < 1.50 forfragment-shader: "\nprecision highp float;\nprecision mediump int;\n/*precision mediump sampler2D;*/\n". See GLSL Spec 1.30-1.50 Section 4.5.3.- See Also:
- Constant Field Values
-
REQUIRE
public static final String REQUIRE
Behavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "require".- See Also:
- Constant Field Values
-
ENABLE
public static final String ENABLE
Behavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "enable".- See Also:
- Constant Field Values
-
DISABLE
public static final String DISABLE
Behavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "disable".- See Also:
- Constant Field Values
-
WARN
public static final String WARN
Behavior for GLSL extension directive, seecreateExtensionDirective(String, String), value "warn".- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ShaderCode
public ShaderCode(int type, int count, CharSequence[][] source)- Parameters:
type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shaderssource- CharSequence array containing the shader sources, organized assource[count][strings-per-shader]. May be either an immutableString- or mutableStringBuilderarray.- Throws:
IllegalArgumentException- ifcountandsource.lengthdo not match
-
ShaderCode
public ShaderCode(int type, int count, int binFormat, Buffer binary)- Parameters:
type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shadersbinary- binary buffer containing the shader binaries,
-
-
Method Detail
-
create
public static ShaderCode create(GL2ES2 gl, int type, int count, Class<?> context, String[] sourceFiles, boolean mutableStringBuilder)
Creates a completeShaderCodeobject while reading all shader source ofsourceFiles, which location is resolved using thecontextclass, see#readShaderSource(Class, String).- Parameters:
gl- current GL object to determine whether a shader compiler is available. If null, no validation is performed.type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shaderscontext- class used to help resolving the source locationsourceFiles- array of source locations, organized assourceFiles[count]->shaderSources[count][1]mutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IllegalArgumentException- ifcountandsourceFiles.lengthdo not match- See Also:
#readShaderSource(Class, String)
-
create
public static ShaderCode create(GL2ES2 gl, int type, int count, Uri[] sourceLocations, boolean mutableStringBuilder)
Creates a completeShaderCodeobject while reading all shader sources fromUrisourceLocationsviareadShaderSource(Uri, boolean).- Parameters:
gl- current GL object to determine whether a shader compiler is available. If null, no validation is performed.type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shaderssourceLocations- array ofUrisource locations, organized assourceFiles[count]->shaderSources[count][1]mutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IllegalArgumentException- ifcountandsourceFiles.lengthdo not match- Since:
- 2.3.2
- See Also:
readShaderSource(Uri, boolean)
-
create
public static ShaderCode create(int type, int count, Class<?> context, int binFormat, String binaryFile)
Creates a completeShaderCodeobject while reading the shader binary ofbinaryFile, which location is resolved using thecontextclass, seereadShaderBinary(Class, String).- Parameters:
type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shaderscontext- class used to help resolving the source locationbinFormat- a valid native binary format as they can be queried byShaderUtil.getShaderBinaryFormats(GL).sourceFiles- array of source locations, organized assourceFiles[count]- See Also:
readShaderBinary(Class, String),ShaderUtil.getShaderBinaryFormats(GL)
-
create
public static ShaderCode create(int type, int count, int binFormat, Uri binLocation)
Creates a completeShaderCodeobject while reading the shader binary fromUribinLocationsviareadShaderBinary(Uri).- Parameters:
type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shadersbinFormat- a valid native binary format as they can be queried byShaderUtil.getShaderBinaryFormats(GL).binLocations-Uribinary location- Since:
- 2.3.2
- See Also:
readShaderBinary(Uri),ShaderUtil.getShaderBinaryFormats(GL)
-
getFileSuffix
public static String getFileSuffix(boolean binary, int type)
Returns a unique suffix for shader resources as follows:- Source
- Binary
- Parameters:
binary- true for a binary resource, false for a source resourcetype- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.- Throws:
GLException- iftypeis not supported- See Also:
create(GL2ES2, int, Class, String, String, String, boolean)
-
getBinarySubPath
public static String getBinarySubPath(int binFormat)
Returns a unique relative path for binary shader resources as follows:- Throws:
GLException- ifbinFormatis not supported- See Also:
create(GL2ES2, int, Class, String, String, String, boolean)
-
create
public static ShaderCode create(GL2ES2 gl, int type, int count, Class<?> context, String srcRoot, String[] srcBasenames, String srcSuffixOpt, String binRoot, String binBasename, String binSuffixOpt, boolean mutableStringBuilder)
Convenient creation method for instantiating a completeShaderCodeobject either from source code using#create(GL2ES2, int, int, Class, String[]), or from a binary code usingcreate(int, int, Class, int, String), whatever is available first.The source and binary location names are expected w/o suffixes which are resolved and appended using the given
srcSuffixOptandbinSuffixOptif notnull, otherwisegetFileSuffix(boolean, int)determines the suffixes.Additionally, the binary resource is expected within a subfolder of
Example:binRootwhich reflects the vendor specific binary format, seegetBinarySubPath(int). AllShaderUtil.getShaderBinaryFormats(GL)are being iterated using the binary subfolder, the first existing resource is being used.Your std JVM layout (plain or within a JAR): org/test/glsl/MyShaderTest.class org/test/glsl/shader/vertex.vp org/test/glsl/shader/fragment.fp org/test/glsl/shader/bin/nvidia/vertex.bvp org/test/glsl/shader/bin/nvidia/fragment.bfp Your Android APK layout: classes.dex assets/org/test/glsl/shader/vertex.vp assets/org/test/glsl/shader/fragment.fp assets/org/test/glsl/shader/bin/nvidia/vertex.bvp assets/org/test/glsl/shader/bin/nvidia/fragment.bfp ... Your invocation in org/test/glsl/MyShaderTest.java: ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, 1, this.getClass(), "shader", new String[] { "vertex" }, null, "shader/bin", "vertex", null, true); ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, 1, this.getClass(), "shader", new String[] { "vertex" }, null, "shader/bin", "fragment", null, true); ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); st.attachShaderProgram(gl, sp0, true);A simplified entry point iscreate(GL2ES2, int, Class, String, String, String, boolean).The location is finally being resolved using the
contextclass, seereadShaderBinary(Class, String).- Parameters:
gl- current GL object to determine whether a shader compiler is available (ifsourceis used), or to determine the shader binary format (ifbinaryis used).type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shaderscontext- class used to help resolving the source and binary locationsrcRoot- relative root path forsrcBasenamesoptionalsrcBasenames- basenames w/o path or suffix relative tosrcRootfor the shader's source codesrcSuffixOpt- optional custom suffix for shader's source file, ifnullgetFileSuffix(boolean, int)is being used.binRoot- relative root path forbinBasenamesbinBasename- basename w/o path or suffix relative tobinRootfor the shader's binary codebinSuffixOpt- optional custom suffix for shader's binary file, ifnullgetFileSuffix(boolean, int)is being used.mutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IllegalArgumentException- ifcountandsrcBasenames.lengthdo not match- Since:
- 2.3.2
- See Also:
#create(GL2ES2, int, int, Class, String[]),create(int, int, Class, int, String),#readShaderSource(Class, String),getFileSuffix(boolean, int),ShaderUtil.getShaderBinaryFormats(GL),getBinarySubPath(int)
-
create
public static ShaderCode create(GL2ES2 gl, int type, int count, Class<?> context, String srcRoot, String[] srcBasenames, String binRoot, String binBasename, boolean mutableStringBuilder)
Simplified variation ofcreate(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean).Convenient creation method for instantiating a complete
ShaderCodeobject either from source code using#create(GL2ES2, int, int, Class, String[]), or from a binary code usingcreate(int, int, Class, int, String), whatever is available first.The source and binary location names are expected w/o suffixes which are resolved and appended using
getFileSuffix(boolean, int).Additionally, the binary resource is expected within a subfolder of
Example:binRootwhich reflects the vendor specific binary format, seegetBinarySubPath(int). AllShaderUtil.getShaderBinaryFormats(GL)are being iterated using the binary subfolder, the first existing resource is being used.Your std JVM layout (plain or within a JAR): org/test/glsl/MyShaderTest.class org/test/glsl/shader/vertex.vp org/test/glsl/shader/fragment.fp org/test/glsl/shader/bin/nvidia/vertex.bvp org/test/glsl/shader/bin/nvidia/fragment.bfp Your Android APK layout: classes.dex assets/org/test/glsl/shader/vertex.vp assets/org/test/glsl/shader/fragment.fp assets/org/test/glsl/shader/bin/nvidia/vertex.bvp assets/org/test/glsl/shader/bin/nvidia/fragment.bfp ... Your invocation in org/test/glsl/MyShaderTest.java: ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, 1, this.getClass(), "shader", new String[] { "vertex" }, "shader/bin", "vertex", true); ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, 1, this.getClass(), "shader", new String[] { "vertex" }, "shader/bin", "fragment", true); ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); st.attachShaderProgram(gl, sp0, true);A simplified entry point iscreate(GL2ES2, int, Class, String, String, String, boolean).The location is finally being resolved using the
contextclass, seereadShaderBinary(Class, String).- Parameters:
gl- current GL object to determine whether a shader compiler is available (ifsourceis used), or to determine the shader binary format (ifbinaryis used).type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.count- number of shaderscontext- class used to help resolving the source and binary locationsrcRoot- relative root path forsrcBasenamesoptionalsrcBasenames- basenames w/o path or suffix relative tosrcRootfor the shader's source codebinRoot- relative root path forbinBasenamesbinBasename- basename w/o path or suffix relative tobinRootfor the shader's binary codemutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IllegalArgumentException- ifcountandsrcBasenames.lengthdo not match- See Also:
create(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean),#readShaderSource(Class, String),getFileSuffix(boolean, int),ShaderUtil.getShaderBinaryFormats(GL),getBinarySubPath(int)
-
create
public static ShaderCode create(GL2ES2 gl, int type, Class<?> context, String srcRoot, String binRoot, String basename, String srcSuffixOpt, String binSuffixOpt, boolean mutableStringBuilder)
Simplified variation ofcreate(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean).Example:
Your std JVM layout (plain or within a JAR): org/test/glsl/MyShaderTest.class org/test/glsl/shader/vertex.vp org/test/glsl/shader/fragment.fp org/test/glsl/shader/bin/nvidia/vertex.bvp org/test/glsl/shader/bin/nvidia/fragment.bfp Your Android APK layout: classes.dex assets/org/test/glsl/shader/vertex.vp assets/org/test/glsl/shader/fragment.fp assets/org/test/glsl/shader/bin/nvidia/vertex.bvp assets/org/test/glsl/shader/bin/nvidia/fragment.bfp ... Your invocation in org/test/glsl/MyShaderTest.java: ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", "shader/bin", "vertex", null, null, true); ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", "shader/bin", "fragment", null, null, true); ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); st.attachShaderProgram(gl, sp0, true);- Parameters:
gl- current GL object to determine whether a shader compiler is available (ifsourceis used), or to determine the shader binary format (ifbinaryis used).type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.context- class used to help resolving the source and binary locationsrcRoot- relative root path forbasenameoptionalbinRoot- relative root path forbasenamebasename- basename w/o path or suffix relative tosrcRootandbinRootfor the shader's source and binary code.srcSuffixOpt- optional custom suffix for shader's source file, ifnullgetFileSuffix(boolean, int)is being used.binSuffixOpt- optional custom suffix for shader's binary file, ifnullgetFileSuffix(boolean, int)is being used.mutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IllegalArgumentException- ifcountis not 1- Since:
- 2.3.2
- See Also:
create(GL2ES2, int, int, Class, String, String[], String, String, String, String, boolean)
-
create
public static ShaderCode create(GL2ES2 gl, int type, Class<?> context, String srcRoot, String binRoot, String basename, boolean mutableStringBuilder)
Simplified variation ofcreate(GL2ES2, int, Class, String, String, String, String, String, boolean).Example:
Your std JVM layout (plain or within a JAR): org/test/glsl/MyShaderTest.class org/test/glsl/shader/vertex.vp org/test/glsl/shader/fragment.fp org/test/glsl/shader/bin/nvidia/vertex.bvp org/test/glsl/shader/bin/nvidia/fragment.bfp Your Android APK layout: classes.dex assets/org/test/glsl/shader/vertex.vp assets/org/test/glsl/shader/fragment.fp assets/org/test/glsl/shader/bin/nvidia/vertex.bvp assets/org/test/glsl/shader/bin/nvidia/fragment.bfp ... Your invocation in org/test/glsl/MyShaderTest.java: ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", "shader/bin", "vertex", true); ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", "shader/bin", "fragment", true); ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); st.attachShaderProgram(gl, sp0, true);- Parameters:
gl- current GL object to determine whether a shader compiler is available (ifsourceis used), or to determine the shader binary format (ifbinaryis used).type- eitherGL2ES2.GL_VERTEX_SHADER,GL2ES2.GL_FRAGMENT_SHADER,GL3ES3.GL_GEOMETRY_SHADER,GL3ES3.GL_TESS_CONTROL_SHADER,GL3ES3.GL_TESS_EVALUATION_SHADERorGL3ES3.GL_COMPUTE_SHADER.context- class used to help resolving the source and binary locationsrcRoot- relative root path forbasenameoptionalbinRoot- relative root path forbasenamemutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.basenames- basename w/o path or suffix relative tosrcRootandbinRootfor the shader's source and binary code.mutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IllegalArgumentException- ifcountis not 1
-
id
public int id()
returns the uniq shader id as an integer
-
shaderType
public int shaderType()
-
shaderTypeStr
public String shaderTypeStr()
-
shaderTypeStr
public static String shaderTypeStr(int type)
-
shaderBinaryFormat
public int shaderBinaryFormat()
-
shaderBinary
public Buffer shaderBinary()
-
shaderSource
public CharSequence[][] shaderSource()
-
isValid
public boolean isValid()
-
shader
public IntBuffer shader()
-
compile
public boolean compile(GL2ES2 gl)
-
compile
public boolean compile(GL2ES2 gl, PrintStream verboseOut)
-
destroy
public void destroy(GL2ES2 gl)
-
dumpSource
public void dumpSource(PrintStream out)
-
insertShaderSource
public int insertShaderSource(int shaderIdx, String tag, int fromIndex, CharSequence data)Addsdataafter the line containingtag.Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
shaderIdx- the shader index to be used.tag- search stringfromIndex- start searchtagbegininig with this indexdata- the text to be inserted. Shall end with an EOL '\n' character.- Returns:
- index after the inserted
data - Throws:
IllegalStateException- if the shader source's CharSequence is immutable, i.e. not of typeStringBuilder
-
replaceInShaderSource
public int replaceInShaderSource(String oldName, String newName)
ReplacesoldNamewithnewNamein all shader sources.In case
oldNameandnewNameare equal, no action is performed.Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
oldName- the to be replace stringnewName- the replacement string- Returns:
- the number of replacements
- Throws:
IllegalStateException- if the shader source's CharSequence is immutable, i.e. not of typeStringBuilder
-
insertShaderSource
public int insertShaderSource(int shaderIdx, int position, CharSequence data)Addsdataatpositionin shader source for shadershaderIdx.Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
shaderIdx- the shader index to be used.position- in shader source segments of shadershaderIdx, -1 will append datadata- the text to be inserted. Shall end with an EOL '\n' character- Returns:
- index after the inserted
data - Throws:
IllegalStateException- if the shader source's CharSequence is immutable, i.e. not of typeStringBuilder
-
insertShaderSource
public int insertShaderSource(int shaderIdx, int position, Class<?> context, String path) throws IOExceptionAdds shader source located inpath, either relative to thecontextclass or absolute as-is atpositionin shader source for shadershaderIdx.Final location lookup is performed via
ClassLoader.getResource(String)andClassLoader.getSystemResource(String), seeIOUtil#getResource(Class, String).Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
shaderIdx- the shader index to be used.position- in shader source segments of shadershaderIdx, -1 will append datacontext- class used to help resolve the source locationpath- location of shader source- Returns:
- index after the inserted code.
- Throws:
IOExceptionIllegalStateException- if the shader source's CharSequence is immutable, i.e. not of typeStringBuilder- See Also:
IOUtil#getResource(Class, String)
-
readShaderSource
public static void readShaderSource(Class<?> context, URLConnection conn, StringBuilder result) throws IOException
Reads shader source located inconn.- Parameters:
context- class used to help resolve the source location, may benullconn- theURLConnectionof the shader sourceresult-StringBuildersink for the resulting shader source code- Throws:
IOException
-
readShaderSource
public static CharSequence readShaderSource(Class<?> context, String path, boolean mutableStringBuilder) throws IOException
Reads shader source located inpath, either relative to thecontextclass or absolute as-is.Final location lookup is performed via
ClassLoader.getResource(String)andClassLoader.getSystemResource(String), seeIOUtil#getResource(Class, String).- Parameters:
context- class used to help resolve the source locationpath- location of shader sourcemutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IOException- See Also:
IOUtil#getResource(Class, String)
-
readShaderSource
public static CharSequence readShaderSource(Uri sourceLocation, boolean mutableStringBuilder) throws IOException
- Parameters:
sourceLocation-Urilocation of shader sourcemutableStringBuilder- iftruemethod returns a mutableStringBuilderinstance which can be edited later on at the costs of a String conversion when passing toGL2ES2.glShaderSource(int, int, String[], IntBuffer). Iffalsemethod returns an immutableStringinstance, which can be passed toGL2ES2.glShaderSource(int, int, String[], IntBuffer)at no additional costs.- Throws:
IOException- Since:
- 2.3.2
-
readShaderBinary
public static ByteBuffer readShaderBinary(Class<?> context, String path) throws IOException
Reads shader binary located inpath, either relative to thecontextclass or absolute as-is.Final location lookup is perfomed via
ClassLoader.getResource(String)andClassLoader.getSystemResource(String), seeIOUtil#getResource(Class, String).- Parameters:
context- class used to help resolve the source locationpath- location of shader binary- Throws:
IOException- See Also:
IOUtil#getResource(Class, String)
-
readShaderBinary
public static ByteBuffer readShaderBinary(Uri binLocation) throws IOException
- Parameters:
binLocation-Urilocation of shader binary- Throws:
IOException- Since:
- 2.3.2
-
createExtensionDirective
public static String createExtensionDirective(String extensionName, String behavior)
-
addGLSLVersion
public final int addGLSLVersion(GL2ES2 gl)
Add GLSL version at the head of this shader source code.Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
gl- a GL context, which must have been made current once- Returns:
- the index after the inserted data, maybe 0 if nothing has be inserted.
-
addDefaultShaderPrecision
public final int addDefaultShaderPrecision(GL2ES2 gl, int 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_fpor none, depending on theGLSL versionbeing used.Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
gl- a GL context, which must have been made current oncepos- position within this mutable shader source.- Returns:
- the index after the inserted data, maybe 0 if nothing has be inserted.
-
requiresDefaultPrecision
public static final boolean requiresDefaultPrecision(GL2ES2 gl)
Returns true, if GLSL version requires default precision, i.e. ES2 or GLSL [1.30 .. 1.50[.
-
requiresGL3DefaultPrecision
public static final boolean requiresGL3DefaultPrecision(GL2ES2 gl)
Returns true, if GL3 GLSL version requires default precision, i.e. GLSL [1.30 .. 1.50[.
-
defaultShaderCustomization
public final int defaultShaderCustomization(GL2ES2 gl, boolean preludeVersion, boolean addDefaultPrecision)
Default customization of this shader source code.Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
gl- a GL context, which must have been made current oncepreludeVersion- if trueGLContext.getGLSLVersionString()is preluded, otherwise not.addDefaultPrecision- iftruedefault 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_fpor none, depending on theGLSL versionbeing used.- Returns:
- the index after the inserted data, maybe 0 if nothing has be inserted.
- See Also:
addGLSLVersion(GL2ES2),addDefaultShaderPrecision(GL2ES2, int)
-
defaultShaderCustomization
public final int defaultShaderCustomization(GL2ES2 gl, boolean preludeVersion, String esDefaultPrecision)
Default customization of this shader source code.Note: The shader source to be edit must be created using a mutable StringBuilder.
- Parameters:
gl- a GL context, which must have been made current oncepreludeVersion- if trueGLContext.getGLSLVersionString()is preluded, otherwise not.esDefaultPrecision- optional default precision source code line(s) preluded if not null and ifGLBase.isGLES(). You may usees2_default_precision_fpfor fragment shader andes2_default_precision_vpfor vertex shader.- Returns:
- the index after the inserted data, maybe 0 if nothing has be inserted.
- See Also:
addGLSLVersion(GL2ES2),addDefaultShaderPrecision(GL2ES2, int)
-
-