From 4f05d5add18048c2fbd1837c0563446c11177e8c Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 26 Oct 2012 09:24:40 +0200 Subject: ShaderCode: Add defaultShaderCustomization(..) to prelude shader source w/ GLSL version and default precision (if GLES) - Used by GearsES2/RedSquare/PointDemo (Made GLSL version proof) --- .../com/jogamp/opengl/util/glsl/ShaderCode.java | 29 ++++++++++++++++++++++ .../com/jogamp/opengl/util/glsl/ShaderUtil.java | 10 +++++--- .../test/junit/jogl/demos/es1/GearsObjectES1.java | 7 +++--- .../opengl/test/junit/jogl/demos/es2/GearsES2.java | 8 +++--- .../test/junit/jogl/demos/es2/GearsObjectES2.java | 7 +++--- .../test/junit/jogl/demos/es2/PointsDemoES2.java | 17 ++----------- .../test/junit/jogl/demos/es2/RedSquareES2.java | 6 +++-- .../jogl/demos/es2/newt/TestGearsES2NEWT.java | 13 +++++++++- .../junit/jogl/demos/es2/shader/PointsShader.fp | 17 +++++++++---- .../junit/jogl/demos/es2/shader/PointsShader.vp | 5 ++++ .../junit/jogl/demos/es2/shader/RedSquareShader.fp | 17 ++++--------- .../junit/jogl/demos/es2/shader/RedSquareShader.vp | 15 +++-------- .../jogl/demos/es2/shader/RedSquareShader2.fp | 20 ++++----------- .../test/junit/jogl/demos/es2/shader/gears.fp | 10 +++++--- .../test/junit/jogl/demos/es2/shader/gears.vp | 7 +++--- 15 files changed, 104 insertions(+), 84 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. + *

+ * Note: The shader source to be edit must be created using a mutable StringBuilder. + *

+ * @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 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); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es1/GearsObjectES1.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es1/GearsObjectES1.java index 1208dad61..8276c6bb9 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es1/GearsObjectES1.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es1/GearsObjectES1.java @@ -47,10 +47,9 @@ public class GearsObjectES1 extends GearsObject { } @Override - public void addInterleavedVertexAndNormalArrays(GLArrayDataServer array, - int components) { - array.addFixedSubArray(GLPointerFunc.GL_VERTEX_ARRAY, 3, GL.GL_ARRAY_BUFFER); - array.addFixedSubArray(GLPointerFunc.GL_NORMAL_ARRAY, 3, GL.GL_ARRAY_BUFFER); + public void addInterleavedVertexAndNormalArrays(GLArrayDataServer array, int components) { + array.addFixedSubArray(GLPointerFunc.GL_VERTEX_ARRAY, components, GL.GL_ARRAY_BUFFER); + array.addFixedSubArray(GLPointerFunc.GL_NORMAL_ARRAY, components, GL.GL_ARRAY_BUFFER); } private void draw(GL2ES1 gl, GLArrayDataServer array, int mode) { diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java index 47feb60e2..c4864b521 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java @@ -120,7 +120,7 @@ public class GearsES2 implements GLEventListener { System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR)); System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER)); System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION)); - System.err.println("GL GLSL: "+gl.hasGLSL()+", has-compiler: "+gl.isFunctionAvailable("glCompileShader")+", version "+(gl.hasGLSL() ? gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION) : "none")); + System.err.println("GL GLSL: "+gl.hasGLSL()+", has-compiler: "+gl.isFunctionAvailable("glCompileShader")+", version "+(gl.hasGLSL() ? gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION) : "none")+", "+gl.getContext().getGLSLVersionNumber()); System.err.println("GL FBO: basic "+ gl.hasBasicFBOSupport()+", full "+gl.hasFullFBOSupport()); System.err.println("GL Profile: "+gl.getGLProfile()); System.err.println("GL:" + gl + ", " + gl.getContext().getGLVersion()); @@ -130,9 +130,11 @@ public class GearsES2 implements GLEventListener { st = new ShaderState(); // st.setVerbose(true); final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", - "shader/bin", "gears", false); + "shader/bin", "gears", true); final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", - "shader/bin", "gears", false); + "shader/bin", "gears", true); + vp0.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_vp); + fp0.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_fp); final ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsObjectES2.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsObjectES2.java index bb526e350..e8b97e287 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsObjectES2.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsObjectES2.java @@ -69,10 +69,9 @@ public class GearsObjectES2 extends GearsObject { } @Override - public void addInterleavedVertexAndNormalArrays(GLArrayDataServer array, - int components) { - array.addGLSLSubArray("vertices", 3, GL.GL_ARRAY_BUFFER); - array.addGLSLSubArray("normals", 3, GL.GL_ARRAY_BUFFER); + public void addInterleavedVertexAndNormalArrays(GLArrayDataServer array, int components) { + array.addGLSLSubArray("vertices", components, GL.GL_ARRAY_BUFFER); + array.addGLSLSubArray("normals", components, GL.GL_ARRAY_BUFFER); } private void draw(GL2ES2 gl, GLArrayDataServer array, int mode) { diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/PointsDemoES2.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/PointsDemoES2.java index 27457e23c..1e0f9596f 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/PointsDemoES2.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/PointsDemoES2.java @@ -58,20 +58,6 @@ public class PointsDemoES2 extends PointsDemo { /** ( pointSize, pointSmooth, attn. pointMinSize, attn. pointMaxSize ) , ( attenuation coefficients 1f 0f 0f, attenuation fade theshold 1f ) */ private final FloatBuffer pointParams = Buffers.newDirectFloatBuffer(new float[] { 1.0f, 0.0f, 0.0f, 4096.0f, 1.0f, 0.0f, 0.0f, 1.0f }); - static final String es2_prelude_vp = "#version 100\n\nprecision highp float;\nprecision highp int;\n"; - static final String es2_prelude_fp = "#version 100\n\nprecision mediump float;\nprecision mediump int;\n/*precision lowp sampler2D;*/\n"; - static final String gl2_prelude = "#version 120\n"; // GL 2.1 (Nvidia driver claims it's required to use gl_Points, OSX claim's it for gl_PointCoord -> driver bug - both were introduced w/ 1.10) - - private void customizeShader(GL2ES2 gl, ShaderCode vp, ShaderCode fp) { - if(gl.isGLES2()) { - vp.insertShaderSource(0, 0, es2_prelude_vp); - fp.insertShaderSource(0, 0, es2_prelude_fp); - } else { - vp.insertShaderSource(0, 0, gl2_prelude); - fp.insertShaderSource(0, 0, gl2_prelude); - } - } - public PointsDemoES2(int swapInterval) { this.swapInterval = swapInterval; } @@ -108,7 +94,8 @@ public class PointsDemoES2 extends PointsDemo { "shader/bin", "PointsShader", true); final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", "shader/bin", "PointsShader", true); - customizeShader(gl, vp0, fp0); + vp0.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_vp); + fp0.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_fp); final ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java index fea1d752e..a78ae17a9 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java @@ -86,9 +86,11 @@ public class RedSquareES2 implements GLEventListener { st = new ShaderState(); st.setVerbose(true); final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", - "shader/bin", "RedSquareShader", false); + "shader/bin", "RedSquareShader", true); final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", - "shader/bin", "RedSquareShader", false); + "shader/bin", "RedSquareShader", true); + vp0.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_vp); + fp0.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_fp); final ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java index 1620985c1..d2c00f980 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/newt/TestGearsES2NEWT.java @@ -88,6 +88,7 @@ public class TestGearsES2NEWT extends UITestCase { static int loops = 1; static boolean loop_shutdown = false; static boolean forceES2 = false; + static boolean forceGL3 = false; @BeforeClass public static void initClass() { @@ -256,7 +257,15 @@ public class TestGearsES2NEWT extends UITestCase { public void test01GL2ES2() throws InterruptedException { for(int i=1; i<=loops; i++) { System.err.println("Loop "+i+"/"+loops); - GLCapabilities caps = new GLCapabilities(forceES2 ? GLProfile.get(GLProfile.GLES2) : GLProfile.getGL2ES2()); + final GLProfile glp; + if(forceGL3) { + glp = GLProfile.get(GLProfile.GL3); + } else if(forceES2) { + glp = GLProfile.get(GLProfile.GLES2); + } else { + glp = GLProfile.getGL2ES2(); + } + GLCapabilities caps = new GLCapabilities( glp ); caps.setBackgroundOpaque(opaque); if(-1 < forceAlpha) { caps.setAlphaBits(forceAlpha); @@ -294,6 +303,8 @@ public class TestGearsES2NEWT extends UITestCase { swapInterval = MiscUtils.atoi(args[i], swapInterval); } else if(args[i].equals("-es2")) { forceES2 = true; + } else if(args[i].equals("-gl3")) { + forceGL3 = true; } else if(args[i].equals("-wait")) { waitForKey = true; } else if(args[i].equals("-mouseInvisible")) { diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.fp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.fp index 3e3be10b3..02efa1522 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.fp +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.fp @@ -1,4 +1,11 @@ +#if __VERSION__ >= 140 + #define varying in + out vec4 mgl_FragColor; +#else + #define mgl_FragColor gl_FragColor +#endif + // [0].rgba: 0, smooth, attnMinSz, attnMaxSz // [1].rgba: attnCoeff(3), attnFadeTs uniform vec4 mgl_PointParams[2]; @@ -28,12 +35,12 @@ void main (void) #endif #ifndef TEST - gl_FragColor.a *= r1; + mgl_FragColor.a *= r1; #else - gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); - gl_FragColor.r = r1 < 0.0 ? 1.0 : 0.0; - gl_FragColor.g = r > 1.0 ? 1.0 : 0.0; - gl_FragColor.b = r > border ? 1.0 : 0.0; + mgl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); + mgl_FragColor.r = r1 < 0.0 ? 1.0 : 0.0; + mgl_FragColor.g = r > 1.0 ? 1.0 : 0.0; + mgl_FragColor.b = r > border ? 1.0 : 0.0; #endif } } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.vp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.vp index 5043a652b..873b1f834 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.vp +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/PointsShader.vp @@ -1,4 +1,9 @@ +#if __VERSION__ >= 140 + #define attribute in + #define varying out +#endif + uniform vec4 mgl_ColorStatic; uniform mat4 mgl_PMVMatrix[4]; // P, Mv, Mvi and Mvit (transpose(inverse(ModelView)) == normalMatrix) diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.fp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.fp index d3cfecd94..7cf059bae 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.fp +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.fp @@ -1,23 +1,16 @@ // Copyright 2010 JogAmp Community. All rights reserved. -/** - * AMD complains: #version must occur before any other statement in the program -#ifdef GL_ES - #version 100 +#if __VERSION__ >= 140 + #define varying in + out vec4 mgl_FragColor; #else - #version 110 -#endif - */ - -#ifdef GL_ES - precision mediump float; - precision mediump int; + #define mgl_FragColor gl_FragColor #endif varying vec4 frontColor; void main (void) { - gl_FragColor = frontColor; + mgl_FragColor = frontColor; } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.vp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.vp index bfd44c8f4..2b19f3251 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.vp +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader.vp @@ -1,17 +1,8 @@ // Copyright 2010 JogAmp Community. All rights reserved. -/** - * AMD complains: #version must occur before any other statement in the program -#ifdef GL_ES - #version 100 -#else - #version 110 -#endif - */ - -#ifdef GL_ES - precision mediump float; - precision mediump int; +#if __VERSION__ >= 140 + #define attribute in + #define varying out #endif uniform mat4 mgl_PMVMatrix[2]; diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader2.fp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader2.fp index 01e4b0964..e0b92db3b 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader2.fp +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/RedSquareShader2.fp @@ -1,26 +1,16 @@ // Copyright 2010 JogAmp Community. All rights reserved. -/** - * AMD complains: #version must occur before any other statement in the program -#ifdef GL_ES - #version 100 +#if __VERSION__ >= 140 + #define varying in + out vec4 mgl_FragColor; #else - #version 110 -#endif - */ - -#ifdef GL_ES - #define MEDIUMP mediump - #define HIGHP highp -#else - #define MEDIUMP - #define HIGHP + #define mgl_FragColor gl_FragColor #endif varying HIGHP vec4 frontColor; void main (void) { - gl_FragColor = vec4(0.0, frontColor.g, frontColor.b, 1.0); + mgl_FragColor = vec4(0.0, frontColor.g, frontColor.b, 1.0); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.fp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.fp index f41addad8..b8e9e0eee 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.fp +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.fp @@ -1,9 +1,11 @@ // Copyright (C) 2011 JogAmp Community. All rights reserved. // Details see GearsES2.java -#ifdef GL_ES - precision mediump float; - precision mediump int; +#if __VERSION__ >= 140 + #define varying in + out vec4 mgl_FragColor; +#else + #define mgl_FragColor gl_FragColor #endif uniform vec4 color; @@ -42,5 +44,5 @@ void main() specular += color * pow(NdotHV, matShininess) * attenuation * matSpecular; } - gl_FragColor = ambient + diffuse + specular ; + mgl_FragColor = ambient + diffuse + specular ; } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.vp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.vp index b2d77082b..e3a84b257 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.vp +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/shader/gears.vp @@ -1,10 +1,11 @@ // Copyright (C) 2011 JogAmp Community. All rights reserved. // Details see GearsES2.java -#ifdef GL_ES - precision mediump float; - precision mediump int; +#if __VERSION__ >= 140 + #define attribute in + #define varying out #endif + uniform mat4 pmvMatrix[4]; // P, Mv, Mvi and Mvit uniform vec3 lightPos; -- cgit v1.2.3