From c45d8e11083a1c6a0fff9d4cf64350c878bea1b6 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 19 Jul 2015 01:19:31 +0200 Subject: ShaderCode: Add 'create' variations w/ custom shader file suffixes as well as w/ Uri usage --- .../com/jogamp/opengl/util/glsl/ShaderCode.java | 322 +++++++++++++++++++-- 1 file changed, 303 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java index 6052c3b49..e1db3f7a3 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java @@ -189,7 +189,7 @@ public class ShaderCode { * {@link GL4#GL_TESS_CONTROL_SHADER} or {@link GL4#GL_TESS_EVALUATION_SHADER}. * @param count number of shaders * @param context class used to help resolving the source location - * @param sourceFiles array of source locations, organized as sourceFiles[count] + * @param sourceFiles array of source locations, organized as sourceFiles[count] -> shaderSources[count][1] * @param mutableStringBuilder if true method returns a mutable StringBuilder instance * which can be edited later on at the costs of a String conversion when passing to * {@link GL2ES2#glShaderSource(int, int, String[], IntBuffer)}. @@ -200,7 +200,8 @@ public class ShaderCode { * @throws IllegalArgumentException if count and sourceFiles.length do not match * @see #readShaderSource(Class, String) */ - public static ShaderCode create(final GL2ES2 gl, final int type, final int count, final Class context, final String[] sourceFiles, final boolean mutableStringBuilder) { + public static ShaderCode create(final GL2ES2 gl, final int type, final int count, final Class context, + final String[] sourceFiles, final boolean mutableStringBuilder) { if(null != gl && !ShaderUtil.isShaderCompilerAvailable(gl)) { return null; } @@ -226,6 +227,53 @@ public class ShaderCode { return new ShaderCode(type, count, shaderSources); } + /** + * Creates a complete {@link ShaderCode} object while reading all shader sources from {@link Uri} sourceLocations + * via {@link #readShaderSource(Uri, boolean)}. + * + * @param gl current GL object to determine whether a shader compiler is available. If null, no validation is performed. + * @param type either {@link GL2ES2#GL_VERTEX_SHADER}, {@link GL2ES2#GL_FRAGMENT_SHADER}, {@link GL3#GL_GEOMETRY_SHADER}, + * {@link GL4#GL_TESS_CONTROL_SHADER} or {@link GL4#GL_TESS_EVALUATION_SHADER}. + * @param count number of shaders + * @param sourceLocations array of {@link Uri} source locations, organized as sourceFiles[count] -> shaderSources[count][1] + * @param mutableStringBuilder if true method returns a mutable StringBuilder instance + * which can be edited later on at the costs of a String conversion when passing to + * {@link GL2ES2#glShaderSource(int, int, String[], IntBuffer)}. + * If false method returns an immutable String instance, + * which can be passed to {@link GL2ES2#glShaderSource(int, int, String[], IntBuffer)} + * at no additional costs. + * + * @throws IllegalArgumentException if count and sourceFiles.length do not match + * @see #readShaderSource(Uri, boolean) + * @since 2.3.2 + */ + public static ShaderCode create(final GL2ES2 gl, final int type, final int count, + final Uri[] sourceLocations, final boolean mutableStringBuilder) { + if(null != gl && !ShaderUtil.isShaderCompilerAvailable(gl)) { + return null; + } + + CharSequence[][] shaderSources = null; + if(null!=sourceLocations) { + // sourceFiles.length and count is validated in ctor + shaderSources = new CharSequence[sourceLocations.length][1]; + for(int i=0; ibinaryFile, * which location is resolved using the context class, see {@link #readShaderBinary(Class, String)}. @@ -258,6 +306,37 @@ public class ShaderCode { return new ShaderCode(type, count, binFormat, shaderBinary); } + /** + * Creates a complete {@link ShaderCode} object while reading the shader binary from {@link Uri} binLocations + * via {@link #readShaderBinary(Uri)}. + * @param type either {@link GL2ES2#GL_VERTEX_SHADER}, {@link GL2ES2#GL_FRAGMENT_SHADER}, {@link GL3#GL_GEOMETRY_SHADER}, + * {@link GL4#GL_TESS_CONTROL_SHADER} or {@link GL4#GL_TESS_EVALUATION_SHADER}. + * @param count number of shaders + * @param binFormat a valid native binary format as they can be queried by {@link ShaderUtil#getShaderBinaryFormats(GL)}. + * @param binLocations {@link Uri} binary location + * + * @see #readShaderBinary(Uri) + * @see ShaderUtil#getShaderBinaryFormats(GL) + * @since 2.3.2 + */ + public static ShaderCode create(final int type, final int count, int binFormat, final Uri binLocation) { + ByteBuffer shaderBinary = null; + if(null!=binLocation && 0<=binFormat) { + try { + shaderBinary = readShaderBinary(binLocation); + } catch (final IOException ioe) { + throw new RuntimeException("readShaderBinary("+binLocation+") error: ", ioe); + } + if(null == shaderBinary) { + binFormat = -1; + } + } + if(null==shaderBinary) { + return null; + } + return new ShaderCode(type, count, binFormat, shaderBinary); + } + /** * Returns a unique suffix for shader resources as follows: *