From ce8e9c7fb1ce9d2747a6014a86e759809a4c5c34 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 8 Oct 2014 21:14:35 +0200 Subject: TestTessellationShader01GL4NEWT: Add robustness, i.e. case where tessellation failed to compile/link (on unsupported platforms) --- .../jogl/demos/gl4/TessellationShader01aGL4.java | 191 ----------------- .../TessellationShader01aGLSL440CoreHardcoded.java | 225 +++++++++++++++++++++ .../jogl/demos/gl4/TessellationShader01bGL4.java | 26 ++- .../gl4/newt/TestTessellationShader01GL4NEWT.java | 4 +- 4 files changed, 246 insertions(+), 200 deletions(-) delete mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGL4.java create mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGLSL440CoreHardcoded.java (limited to 'src') diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGL4.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGL4.java deleted file mode 100644 index a5807a096..000000000 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGL4.java +++ /dev/null @@ -1,191 +0,0 @@ -/** - * Copyright 2014 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ -package com.jogamp.opengl.test.junit.jogl.demos.gl4; - -import java.nio.FloatBuffer; - -import javax.media.opengl.GL; -import javax.media.opengl.GL2ES2; -import javax.media.opengl.GL2ES3; -import javax.media.opengl.GL2GL3; -import javax.media.opengl.GL4; -import javax.media.opengl.GLAutoDrawable; -import javax.media.opengl.GLEventListener; - -import com.jogamp.opengl.util.glsl.ShaderCode; -import com.jogamp.opengl.util.glsl.ShaderProgram; - -/** - * JOGL Tessellation ShaderCode GL4 test case. - *

- * Demonstrates tessellation-control and -evaluation shaders. - *

- * - * @author Raymond L. Rivera, 2014 - * @author Sven Gothel - */ -public class TessellationShader01aGL4 implements GLEventListener { - private static final double ANIMATION_RATE = 950.0; - - private ShaderProgram program; - private final int[] vertexArray = new int[1]; - private FloatBuffer vertexOffset; - private FloatBuffer backgroundColor; - - - @Override - public void init(final GLAutoDrawable auto) { - final double theta = System.currentTimeMillis() / ANIMATION_RATE; - vertexOffset = FloatBuffer.allocate(4); - vertexOffset.put(0, (float)(Math.sin(theta) * 0.5f)); - vertexOffset.put(1, (float)(Math.cos(theta) * 0.6f)); - vertexOffset.put(2, 0.0f); - vertexOffset.put(3, 0.0f); - - backgroundColor = FloatBuffer.allocate(4); - backgroundColor.put(0, 0.25f); - backgroundColor.put(1, 0.25f); - backgroundColor.put(2, 0.25f); - backgroundColor.put(3, 1.0f); - - final GL4 gl = auto.getGL().getGL4(); - program = createProgram(auto); - gl.glGenVertexArrays(vertexArray.length, vertexArray, 0); - gl.glBindVertexArray(vertexArray[0]); - gl.glPatchParameteri(GL4.GL_PATCH_VERTICES, 3); - gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_LINE); - } - - @Override - public void display(final GLAutoDrawable auto) { - final GL4 gl = auto.getGL().getGL4(); - final double value = System.currentTimeMillis() / ANIMATION_RATE; - gl.glClearBufferfv(GL2ES3.GL_COLOR, 0, backgroundColor); - gl.glUseProgram(program.program()); - vertexOffset.put(0, (float)(Math.sin(value) * 0.5f)); - vertexOffset.put(1, (float)(Math.cos(value) * 0.6f)); - gl.glVertexAttrib4fv(0, vertexOffset); - gl.glDrawArrays(GL4.GL_PATCHES, 0, 3); - } - - @Override - public void dispose(final GLAutoDrawable auto) { - final GL4 gl = auto.getGL().getGL4(); - gl.glDeleteVertexArrays(vertexArray.length, vertexArray, 0); - program.destroy(gl); - } - - @Override - public void reshape(final GLAutoDrawable auto, final int x, final int y, final int width, final int height) { - // final GL4 gl = auto.getGL().getGL4(); - } - - private ShaderProgram createProgram(final GLAutoDrawable auto) { - final GL4 gl = auto.getGL().getGL4(); - final String vertexSource = - "#version 440 core \n" + - " \n" + - "layout (location = 0) in vec4 offset; \n" + - " \n" + - "void main(void) \n" + - "{ \n" + - " const vec4 vertices[3] = vec4[3] ( \n" + - " vec4( 0.25, 0.25, 0.5, 1.0), \n" + - " vec4(-0.25, -0.25, 0.5, 1.0), \n" + - " vec4( 0.25, -0.25, 0.5, 1.0)); \n" + - " gl_Position = vertices[gl_VertexID] + offset; \n" + - "} \n"; - final String tessCtrlSource = - "#version 440 core \n" + - "layout (vertices = 3) out; \n" + - " \n" + - "void main(void) \n" + - "{ \n" + - " if (gl_InvocationID == 0) \n" + - " { \n" + - " gl_TessLevelInner[0] = 5.0; \n" + - " gl_TessLevelOuter[0] = 5.0; \n" + - " gl_TessLevelOuter[1] = 5.0; \n" + - " gl_TessLevelOuter[2] = 5.0; \n" + - " } \n" + - " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n" + - "} \n"; - final String tessEvalSource = - "#version 440 core \n" + - " \n" + - "layout (triangles, equal_spacing, cw) in; \n" + - " \n" + - "void main(void) \n" + - "{ \n" + - " gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + \n" + - " (gl_TessCoord.y * gl_in[1].gl_Position) + \n" + - " (gl_TessCoord.z * gl_in[2].gl_Position); \n" + - "} \n"; - final String fragmentSource = - "#version 440 core \n" + - " \n" + - "out vec4 color; \n" + - " \n" + - "void main(void) \n" + - "{ \n" + - " color = vec4(1.0, 1.0, 1.0, 1.0); \n" + - "} \n"; - - final ShaderCode vertexShader = createShader(gl, GL2ES2.GL_VERTEX_SHADER, vertexSource); - final ShaderCode tessCtrlShader = createShader(gl, GL4.GL_TESS_CONTROL_SHADER, tessCtrlSource); - final ShaderCode tessEvalShader = createShader(gl, GL4.GL_TESS_EVALUATION_SHADER, tessEvalSource); - final ShaderCode fragmentShader = createShader(gl, GL2ES2.GL_FRAGMENT_SHADER, fragmentSource); - - final ShaderProgram program = new ShaderProgram(); - - program.init(gl); - program.add(vertexShader); - program.add(tessCtrlShader); - program.add(tessEvalShader); - program.add(fragmentShader); - - program.link(gl, System.err); - if(!program.validateProgram(gl, System.out)) - System.err.println("[error] Program linking failed."); - - return program; - } - - private ShaderCode createShader(final GL4 gl, final int shaderType, final String source) { - final String[][] sources = new String[1][1]; - sources[0] = new String[]{ source }; - final ShaderCode shader = new ShaderCode(shaderType, sources.length, sources); - - final boolean compiled = shader.compile(gl, System.err); - if (!compiled) - System.err.println("[error] Shader compilation failed."); - - return shader; - } - -} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGLSL440CoreHardcoded.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGLSL440CoreHardcoded.java new file mode 100644 index 000000000..223eb1a14 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGLSL440CoreHardcoded.java @@ -0,0 +1,225 @@ +/** + * Copyright 2014 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.opengl.test.junit.jogl.demos.gl4; + +import java.nio.FloatBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GL2ES3; +import javax.media.opengl.GL2GL3; +import javax.media.opengl.GL4; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; + +import com.jogamp.opengl.util.glsl.ShaderCode; +import com.jogamp.opengl.util.glsl.ShaderProgram; + +/** + * JOGL Tessellation ShaderCode GL4 test case. + *

+ * Demonstrates tessellation-control and -evaluation shaders. + *

+ * + * @author Raymond L. Rivera, 2014 + * @author Sven Gothel + */ +public class TessellationShader01aGLSL440CoreHardcoded implements GLEventListener { + private static final double ANIMATION_RATE = 950.0; + + private ShaderProgram program; + private final int[] vertexArray = new int[1]; + private FloatBuffer vertexOffset; + private FloatBuffer backgroundColor; + + + @Override + public void init(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + program = createProgram(auto); + if( null == program ) { + return; + } + + final double theta = System.currentTimeMillis() / ANIMATION_RATE; + vertexOffset = FloatBuffer.allocate(4); + vertexOffset.put(0, (float)(Math.sin(theta) * 0.5f)); + vertexOffset.put(1, (float)(Math.cos(theta) * 0.6f)); + vertexOffset.put(2, 0.0f); + vertexOffset.put(3, 0.0f); + + backgroundColor = FloatBuffer.allocate(4); + backgroundColor.put(0, 0.25f); + backgroundColor.put(1, 0.25f); + backgroundColor.put(2, 0.25f); + backgroundColor.put(3, 1.0f); + + gl.glGenVertexArrays(vertexArray.length, vertexArray, 0); + gl.glBindVertexArray(vertexArray[0]); + gl.glPatchParameteri(GL4.GL_PATCH_VERTICES, 3); + gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_LINE); + } + + @Override + public void display(final GLAutoDrawable auto) { + if( null == program ) { + return; + } + final GL4 gl = auto.getGL().getGL4(); + final double value = System.currentTimeMillis() / ANIMATION_RATE; + gl.glClearBufferfv(GL2ES3.GL_COLOR, 0, backgroundColor); + gl.glUseProgram(program.program()); + vertexOffset.put(0, (float)(Math.sin(value) * 0.5f)); + vertexOffset.put(1, (float)(Math.cos(value) * 0.6f)); + gl.glVertexAttrib4fv(0, vertexOffset); + gl.glDrawArrays(GL4.GL_PATCHES, 0, 3); + } + + @Override + public void dispose(final GLAutoDrawable auto) { + if( null == program ) { + return; + } + final GL4 gl = auto.getGL().getGL4(); + gl.glDeleteVertexArrays(vertexArray.length, vertexArray, 0); + program.destroy(gl); + } + + @Override + public void reshape(final GLAutoDrawable auto, final int x, final int y, final int width, final int height) { + // final GL4 gl = auto.getGL().getGL4(); + } + + private ShaderProgram createProgram(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + final String vertexSource = + "#version 440 core \n" + + " \n" + + "layout (location = 0) in vec4 offset; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " const vec4 vertices[3] = vec4[3] ( \n" + + " vec4( 0.25, 0.25, 0.5, 1.0), \n" + + " vec4(-0.25, -0.25, 0.5, 1.0), \n" + + " vec4( 0.25, -0.25, 0.5, 1.0)); \n" + + " gl_Position = vertices[gl_VertexID] + offset; \n" + + "} \n"; + final String tessCtrlSource = + "#version 440 core \n" + + "layout (vertices = 3) out; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " if (gl_InvocationID == 0) \n" + + " { \n" + + " gl_TessLevelInner[0] = 5.0; \n" + + " gl_TessLevelOuter[0] = 5.0; \n" + + " gl_TessLevelOuter[1] = 5.0; \n" + + " gl_TessLevelOuter[2] = 5.0; \n" + + " } \n" + + " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n" + + "} \n"; + final String tessEvalSource = + "#version 440 core \n" + + " \n" + + "layout (triangles, equal_spacing, cw) in; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + \n" + + " (gl_TessCoord.y * gl_in[1].gl_Position) + \n" + + " (gl_TessCoord.z * gl_in[2].gl_Position); \n" + + "} \n"; + final String fragmentSource = + "#version 440 core \n" + + " \n" + + "out vec4 color; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " color = vec4(1.0, 1.0, 1.0, 1.0); \n" + + "} \n"; + + final ShaderCode vertexShader = createShader(gl, GL2ES2.GL_VERTEX_SHADER, vertexSource); + if( null == vertexShader ) { + return null; + } + final ShaderCode tessCtrlShader = createShader(gl, GL4.GL_TESS_CONTROL_SHADER, tessCtrlSource); + if( null == tessCtrlShader ) { + vertexShader.destroy(gl); + return null; + } + final ShaderCode tessEvalShader = createShader(gl, GL4.GL_TESS_EVALUATION_SHADER, tessEvalSource); + if( null == tessEvalShader ) { + vertexShader.destroy(gl); + tessCtrlShader.destroy(gl); + return null; + } + final ShaderCode fragmentShader = createShader(gl, GL2ES2.GL_FRAGMENT_SHADER, fragmentSource); + if( null == fragmentShader ) { + vertexShader.destroy(gl); + tessCtrlShader.destroy(gl); + tessEvalShader.destroy(gl); + return null; + } + + final ShaderProgram program = new ShaderProgram(); + + program.init(gl); + program.add(vertexShader); + program.add(tessCtrlShader); + program.add(tessEvalShader); + program.add(fragmentShader); + + program.link(gl, System.err); + if( !program.validateProgram(gl, System.out) ) { + System.err.println("[error] Program linking failed."); + program.destroy(gl); + return null; + } else { + return program; + } + } + + private ShaderCode createShader(final GL4 gl, final int shaderType, final String source) { + final String[][] sources = new String[1][1]; + sources[0] = new String[]{ source }; + final ShaderCode shader = new ShaderCode(shaderType, sources.length, sources); + + final boolean compiled = shader.compile(gl, System.err); + if (!compiled) { + System.err.println("[error] Shader compilation failed."); + shader.destroy(gl); + return null; + } else { + return shader; + } + } + +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01bGL4.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01bGL4.java index 7be26e400..bcf4fa6ca 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01bGL4.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01bGL4.java @@ -61,6 +61,12 @@ public class TessellationShader01bGL4 implements GLEventListener { @Override public void init(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + program = createProgram(auto); + if( null == program ) { + return; + } + final double theta = System.currentTimeMillis() / ANIMATION_RATE; vertexOffset = FloatBuffer.allocate(4); vertexOffset.put(0, (float)(Math.sin(theta) * 0.5f)); @@ -74,8 +80,6 @@ public class TessellationShader01bGL4 implements GLEventListener { backgroundColor.put(2, 0.25f); backgroundColor.put(3, 1.0f); - final GL4 gl = auto.getGL().getGL4(); - program = createProgram(auto); gl.glGenVertexArrays(vertexArray.length, vertexArray, 0); gl.glBindVertexArray(vertexArray[0]); gl.glPatchParameteri(GL4.GL_PATCH_VERTICES, 3); @@ -84,6 +88,9 @@ public class TessellationShader01bGL4 implements GLEventListener { @Override public void display(final GLAutoDrawable auto) { + if( null == program ) { + return; + } final GL4 gl = auto.getGL().getGL4(); final double value = System.currentTimeMillis() / ANIMATION_RATE; gl.glClearBufferfv(GL2ES3.GL_COLOR, 0, backgroundColor); @@ -96,6 +103,9 @@ public class TessellationShader01bGL4 implements GLEventListener { @Override public void dispose(final GLAutoDrawable auto) { + if( null == program ) { + return; + } final GL4 gl = auto.getGL().getGL4(); gl.glDeleteVertexArrays(vertexArray.length, vertexArray, 0); program.destroy(gl); @@ -132,11 +142,13 @@ public class TessellationShader01bGL4 implements GLEventListener { sp.add(gl, tcs, System.err); sp.add(gl, tes, System.err); sp.add(gl, fs, System.err); - if(!sp.link(gl, System.err)) { - throw new GLException("Couldn't link program: "+sp); - } } - - return sp; + if( !sp.link(gl, System.err) ) { + System.err.println("[error] Couldn't link program: "+sp); + sp.destroy(gl); + return null; + } else { + return sp; + } } } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestTessellationShader01GL4NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestTessellationShader01GL4NEWT.java index a05dcec06..ca2638581 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestTessellationShader01GL4NEWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestTessellationShader01GL4NEWT.java @@ -39,7 +39,7 @@ import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; import com.jogamp.newt.opengl.GLWindow; -import com.jogamp.opengl.test.junit.jogl.demos.gl4.TessellationShader01aGL4; +import com.jogamp.opengl.test.junit.jogl.demos.gl4.TessellationShader01aGLSL440CoreHardcoded; import com.jogamp.opengl.test.junit.jogl.demos.gl4.TessellationShader01bGL4; import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.QuitAdapter; @@ -65,7 +65,7 @@ public class TestTessellationShader01GL4NEWT extends UITestCase { public void test01_01a() throws InterruptedException { final GLCapabilities caps = getCaps(GLProfile.GL4); if( null == caps ) { return; } - testImpl(caps, new TessellationShader01aGL4()); + testImpl(caps, new TessellationShader01aGLSL440CoreHardcoded()); } @Test -- cgit v1.2.3