diff options
author | Sven Gothel <[email protected]> | 2012-10-26 09:24:40 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-10-26 09:24:40 +0200 |
commit | 4f05d5add18048c2fbd1837c0563446c11177e8c (patch) | |
tree | 8b7d919e59a60554c95ab56e9427d700d42df531 /src/jogl/classes/com/jogamp/opengl | |
parent | 0f198907181396c5aab545f621098ed2f210003a (diff) |
ShaderCode: Add defaultShaderCustomization(..) to prelude shader source w/ GLSL version and default precision (if GLES) - Used by GearsES2/RedSquare/PointDemo (Made GLSL version proof)
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl')
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java | 29 | ||||
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderUtil.java | 10 |
2 files changed, 35 insertions, 4 deletions
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 f6b686d7e..378167c2c 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java @@ -46,6 +46,7 @@ import java.util.Set; import javax.media.opengl.GL; import javax.media.opengl.GL2ES2; import javax.media.opengl.GLES2; +import javax.media.opengl.GLContext; import javax.media.opengl.GLException; import jogamp.opengl.Debug; @@ -806,6 +807,34 @@ public class ShaderCode { } } + /** {@value #es2_default_precision_vp} */ + public static final String es2_default_precision_vp = "\nprecision highp float;\nprecision highp int;\n"; + /** {@value #es2_default_precision_fp} */ + public static final String es2_default_precision_fp = "\nprecision mediump float;\nprecision mediump int;\n/*precision lowp sampler2D;*/\n"; + + /** + * Default customization of this shader source code. + * <p> + * Note: The shader source to be edit must be created using a mutable StringBuilder. + * </p> + * @param gl a GL context, which must have been made current once + * @param preludeVersion if true {@link GLContext#getGLSLVersionString()} is preluded, otherwise not. + * @param es2DefaultPrecision optional default precision source code line(s) preluded if not null and if {@link GL#isGLES()}. + * You may use {@link #es2_default_precision_fp} for fragment shader and {@link #es2_default_precision_vp} for vertex shader. + * @return the index after the inserted data, maybe 0 if nothing has be inserted. + */ + public final int defaultShaderCustomization(GL2ES2 gl, boolean preludeVersion, String es2DefaultPrecision) { + int pos = 0; + if(preludeVersion) { + final String glslVersion_prelude = gl.getContext().getGLSLVersionString(); + pos = insertShaderSource(0, pos, glslVersion_prelude); + } + if( gl.isGLES() && null != es2DefaultPrecision ) { + pos = insertShaderSource(0, pos, es2DefaultPrecision); + } + return pos; + } + //---------------------------------------------------------------------- // Internals only below this point // diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderUtil.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderUtil.java index 8c0addb78..5afc5e38c 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderUtil.java @@ -162,7 +162,8 @@ public class ShaderUtil { try { final int[] param = new int[1]; gl.glGetIntegerv(GL2ES2.GL_NUM_SHADER_BINARY_FORMATS, param, 0); - int numFormats = param[0]; + final int err = gl.glGetError(); + final int numFormats = GL.GL_NO_ERROR == err ? param[0] : 0; if(numFormats>0) { int[] formats = new int[numFormats]; gl.glGetIntegerv(GL2ES2.GL_SHADER_BINARY_FORMATS, formats, 0); @@ -170,7 +171,7 @@ public class ShaderUtil { info.shaderBinaryFormats.add(new Integer(formats[i])); } } - } catch (GLException gle) { gle.printStackTrace(); } + } catch (GLException gle) { System.err.println("Catched Exception: "+gle.getMessage()); gle.printStackTrace(); } } } return info.shaderBinaryFormats; @@ -186,7 +187,8 @@ public class ShaderUtil { try { final byte[] param = new byte[1]; gl.glGetBooleanv(GL2ES2.GL_SHADER_COMPILER, param, 0); - boolean v = param[0]!=(byte)0x00; + final int err = gl.glGetError(); + boolean v = GL.GL_NO_ERROR == err && param[0]!=(byte)0x00; if(!v) { final Set<Integer> bfs = getShaderBinaryFormats(gl); if(bfs.size()==0) { @@ -196,7 +198,7 @@ public class ShaderUtil { } info.shaderCompilerAvailable = new Boolean(v); queryOK = true; - } catch (GLException gle) { gle.printStackTrace(); } + } catch (GLException gle) { System.err.println("Catched Exception: "+gle.getMessage()); gle.printStackTrace(); } if(!queryOK) { info.shaderCompilerAvailable = new Boolean(true); } |