diff options
Diffstat (limited to 'src/junit/com/jogamp')
5 files changed, 633 insertions, 76 deletions
diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/es2/RedSquare0.java b/src/junit/com/jogamp/test/junit/jogl/demos/es2/RedSquare0.java new file mode 100644 index 000000000..229635ae5 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/demos/es2/RedSquare0.java @@ -0,0 +1,198 @@ +/** + * Copyright 2010 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.test.junit.jogl.demos.es2; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.opengl.util.Animator; +import com.jogamp.opengl.util.GLArrayDataWrapper; +import com.jogamp.opengl.util.PMVMatrix; +import com.jogamp.test.junit.jogl.demos.es2.shader.RedSquareShader; +import com.jogamp.test.junit.util.GLSLSimpleProgram; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.FloatBuffer; +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLContext; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLUniformData; +import org.junit.Assert; + +public class RedSquare0 implements GLEventListener { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream pbaos = new PrintStream(baos); + GLSLSimpleProgram myShader; + PMVMatrix pmvMatrix; + int mgl_PMVMatrix; + GLUniformData pmvMatrixUniform; + int mgl_Vertex; + int mgl_Color; + long t0; + + public void init(GLAutoDrawable glad) { + GLContext context = glad.getContext(); + context.makeCurrent(); + GL2ES2 gl = context.getGL().getGL2ES2(); + myShader = GLSLSimpleProgram.create(gl, RedSquareShader.VERTEX_SHADER_TEXT, RedSquareShader.FRAGMENT_SHADER_TEXT, true); + gl.glUseProgram(myShader.getShaderProgram()); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // setup mgl_PMVMatrix + pmvMatrix = new PMVMatrix(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + mgl_PMVMatrix = gl.glGetUniformLocation(myShader.getShaderProgram(), "mgl_PMVMatrix"); + Assert.assertTrue(0 <= mgl_PMVMatrix); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); + pmvMatrixUniform.setLocation(mgl_PMVMatrix); + gl.glUniform(pmvMatrixUniform); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Allocate Vertex Array + int components = 3; + int numElements = 4; + mgl_Vertex = gl.glGetAttribLocation(myShader.getShaderProgram(), "mgl_Vertex"); + Assert.assertTrue(0 <= mgl_Vertex); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + FloatBuffer buffer = Buffers.newDirectFloatBuffer(numElements * components); + GLArrayDataWrapper vertices = GLArrayDataWrapper.createGLSL(gl, "mgl_Vertex", 3, gl.GL_FLOAT, false, 0, buffer, -1, 0); + { + // Fill them up + FloatBuffer verticeb = (FloatBuffer) vertices.getBuffer(); + verticeb.put(-2); + verticeb.put(2); + verticeb.put(0); + verticeb.put(2); + verticeb.put(2); + verticeb.put(0); + verticeb.put(-2); + verticeb.put(-2); + verticeb.put(0); + verticeb.put(2); + verticeb.put(-2); + verticeb.put(0); + } + buffer.flip(); + vertices.setLocation(mgl_Vertex); + gl.glEnableVertexAttribArray(mgl_Vertex); + gl.glVertexAttribPointer(vertices); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Allocate Color Array + components = 4; + numElements = 4; + mgl_Color = gl.glGetAttribLocation(myShader.getShaderProgram(), "mgl_Color"); + Assert.assertTrue(0 <= mgl_Color); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + buffer = Buffers.newDirectFloatBuffer(numElements * components); + GLArrayDataWrapper colors = GLArrayDataWrapper.createGLSL(gl, "mgl_Color", 4, gl.GL_FLOAT, false, 0, buffer, -1, 0); + { + // Fill them up + FloatBuffer colorb = (FloatBuffer) colors.getBuffer(); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + colorb.put(1); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + } + buffer.flip(); + colors.setLocation(mgl_Color); + gl.glEnableVertexAttribArray(mgl_Color); + gl.glVertexAttribPointer(colors); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // OpenGL Render Settings + gl.glClearColor(0, 0, 0, 1); + gl.glEnable(GL2ES2.GL_DEPTH_TEST); + gl.glUseProgram(0); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + t0 = System.currentTimeMillis(); + } + + public void reshape(GLAutoDrawable glad, int x, int y, int width, int height) { + GL2ES2 gl = glad.getGL().getGL2ES2(); + gl.glUseProgram(myShader.getShaderProgram()); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Set location in front of camera + pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.gluPerspective(45.0F, (float) width / (float) height, 1.0F, 100.0F); + //pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f); + gl.glUniform(pmvMatrixUniform); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glUseProgram(0); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } + + public void display(GLAutoDrawable glad) { + long t1 = System.currentTimeMillis(); + + GL2ES2 gl = glad.getGL().getGL2ES2(); + gl.glUseProgram(myShader.getShaderProgram()); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // One rotation every four seconds + pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glTranslatef(0, 0, -10); + float ang = ((float) (t1 - t0) * 360.0F) / 4000.0F; + pmvMatrix.glRotatef(ang, 0, 0, 1); + pmvMatrix.glRotatef(ang, 0, 1, 0); + gl.glUniform(pmvMatrixUniform); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Draw a square + gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glUseProgram(0); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } + + public void dispose(GLAutoDrawable glad) { + GL2ES2 gl = glad.getGL().getGL2ES2(); + gl.glDisableVertexAttribArray(mgl_Vertex); + gl.glDisableVertexAttribArray(mgl_Color); + myShader.release(gl); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + pmvMatrix.destroy(); + pmvMatrix = null; + System.err.println("dispose done"); + } +} diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/es2/shader/RedSquareShader.java b/src/junit/com/jogamp/test/junit/jogl/demos/es2/shader/RedSquareShader.java new file mode 100644 index 000000000..5a365be11 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/demos/es2/shader/RedSquareShader.java @@ -0,0 +1,68 @@ +/** + * Copyright 2010 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.test.junit.jogl.demos.es2.shader; + +public class RedSquareShader { + public static String VERTEX_SHADER_TEXT = + " #ifdef GL_ES\n" + + " #define MEDIUMP mediump\n" + + " #define HIGHP highp\n" + + "#else\n" + + " #define MEDIUMP\n" + + " #define HIGHP\n" + + "#endif\n" + + "\n" + + "uniform MEDIUMP mat4 mgl_PMVMatrix[2];\n" + + "attribute HIGHP vec4 mgl_Vertex;\n" + + "attribute HIGHP vec4 mgl_Color;\n" + + "varying HIGHP vec4 frontColor;\n" + + "\n" + + "void main(void)\n" + + "{\n" + + " frontColor=mgl_Color;\n" + + " gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex;\n" + + "}\n" ; + + public static String FRAGMENT_SHADER_TEXT = + "#ifdef GL_ES\n" + + " #define MEDIUMP mediump\n" + + " #define HIGHP highp\n" + + "#else\n" + + " #define MEDIUMP\n" + + " #define HIGHP\n" + + "#endif\n" + + "\n" + + "varying HIGHP vec4 frontColor;\n" + + "\n" + + "void main (void)\n" + + "{\n" + + " gl_FragColor = frontColor;\n" + + "}\n" ; + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/glsl/TestGLSLSimple01NEWT.java b/src/junit/com/jogamp/test/junit/jogl/glsl/TestGLSLSimple01NEWT.java new file mode 100644 index 000000000..3a420e1ef --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/glsl/TestGLSLSimple01NEWT.java @@ -0,0 +1,143 @@ +/** + * Copyright 2010 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.test.junit.jogl.glsl; + +import com.jogamp.test.junit.jogl.demos.es2.RedSquare0; +import com.jogamp.test.junit.util.GLSLSimpleProgram; +import com.jogamp.test.junit.util.UITestCase; + + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLContext; +import javax.media.opengl.GLProfile; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.util.Animator; +import com.jogamp.test.junit.jogl.demos.es2.shader.RedSquareShader; +import com.jogamp.test.junit.util.MiscUtils; + +import java.io.IOException; +import javax.media.opengl.GL2ES2; +import org.junit.AfterClass; + +public class TestGLSLSimple01NEWT extends UITestCase { + static long durationPerTest = 100; // ms + + @BeforeClass + public static void initClass() { + System.err.println("class init"); + GLProfile.initSingleton(true); + } + + @AfterClass + public static void tearDownClass() { + System.err.println("class tear down .."); + GLProfile.shutdown(); + System.err.println("class tear down end"); + } + + @Test(timeout=5000) + public void testGLSLCompilation01() { + GLProfile glp = GLProfile.get(GLProfile.GL2ES2); + Assert.assertNotNull(glp); + GLCapabilities caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + + GLWindow window = GLWindow.create(caps); + Assert.assertNotNull(window); + window.setSize(800, 600); + window.setVisible(true); + Assert.assertTrue(window.isNativeValid()); + + GLContext context = window.getContext(); + context.setSynchronized(true); + + // trigger native creation of drawable/context + window.display(); + Assert.assertTrue(window.isRealized()); + Assert.assertTrue(window.getContext().isCreated()); + + context.makeCurrent(); + + // given + + GL2ES2 gl = context.getGL().getGL2ES2(); + GLSLSimpleProgram myShader = GLSLSimpleProgram.create(gl, + RedSquareShader.VERTEX_SHADER_TEXT, + RedSquareShader.FRAGMENT_SHADER_TEXT, + true); + + myShader.release(gl); + context.release(); + window.destroy(); + } + + @Test(timeout=5000) + public void testGLSLUse01() throws InterruptedException { + GLProfile glp = GLProfile.get(GLProfile.GL2ES2); + Assert.assertNotNull(glp); + GLCapabilities caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + + GLWindow window = GLWindow.create(caps); + Assert.assertNotNull(window); + window.setSize(800, 600); + window.setVisible(true); + Assert.assertTrue(window.isNativeValid()); + window.addGLEventListener(new RedSquare0()); + + Animator animator = new Animator(window); + animator.start(); + Assert.assertEquals(true, animator.isAnimating()); + while(animator.isAnimating() && animator.getDuration()<durationPerTest) { + Thread.sleep(100); + } + Assert.assertEquals(true, animator.isAnimating()); + + window.destroy(); + animator.stop(); + } + + public static void main(String args[]) throws IOException { + System.err.println("main - start"); + for(int i=0; i<args.length; i++) { + if(args[i].equals("-time")) { + durationPerTest = MiscUtils.atoi(args[++i], (int)durationPerTest); + } + } + String tstname = TestGLSLSimple01NEWT.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + System.err.println("main - end"); + } + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/glsl/TestTransformFeedbackVaryingsBug407NEWT.java b/src/junit/com/jogamp/test/junit/jogl/glsl/TestTransformFeedbackVaryingsBug407NEWT.java index cf625dba9..f44aab380 100644 --- a/src/junit/com/jogamp/test/junit/jogl/glsl/TestTransformFeedbackVaryingsBug407NEWT.java +++ b/src/junit/com/jogamp/test/junit/jogl/glsl/TestTransformFeedbackVaryingsBug407NEWT.java @@ -8,24 +8,18 @@ import java.io.PrintStream; import javax.media.opengl.GL3; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLContext; -import javax.media.opengl.GLDrawable; -import javax.media.opengl.GLDrawableFactory; import javax.media.opengl.GLProfile; -import org.junit.After; import org.junit.Assert; import org.junit.Assume; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import com.jogamp.newt.Display; -import com.jogamp.newt.NewtFactory; -import com.jogamp.newt.Screen; -import com.jogamp.newt.Window; +import com.jogamp.newt.opengl.GLWindow; import com.jogamp.opengl.util.glsl.ShaderUtil; import java.io.IOException; +import org.junit.AfterClass; /** * Bug 'Function glTransformFeedbackVaryings incorrectly passes argument' @@ -33,7 +27,6 @@ import java.io.IOException; */ public class TestTransformFeedbackVaryingsBug407NEWT extends UITestCase { - private GLContext context; private String VERTEX_SHADER_TEXT; @BeforeClass @@ -41,12 +34,59 @@ public class TestTransformFeedbackVaryingsBug407NEWT extends UITestCase { GLProfile.initSingleton(true); } - @Before - public void setUp() { + @AfterClass + public static void tearDownClass() { + GLProfile.shutdown(); + } + + class MyShader { + int shaderProgram; + int vertShader; + + MyShader(int shaderProgram, int vertShader) { + this.shaderProgram = shaderProgram; + this.vertShader = vertShader; + } + } + + private MyShader getShader(GL3 gl, String text, int type) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream pbaos = new PrintStream(baos); + + int shaderProgram = gl.glCreateProgram(); + + int vertShader = gl.glCreateShader(type); + + String[] lines = new String[]{text}; + int[] lengths = new int[]{lines[0].length()}; + gl.glShaderSource(vertShader, lines.length, lines, lengths, 0); + gl.glCompileShader(vertShader); + + if(!ShaderUtil.isShaderStatusValid(gl, vertShader, gl.GL_COMPILE_STATUS, pbaos)) { + System.out.println("getShader:postCompile: "+baos.toString()); + Assert.assertTrue(false); + } + pbaos.flush(); baos.reset(); + + gl.glAttachShader(shaderProgram, vertShader); + + return new MyShader(shaderProgram, vertShader); + } + + private void releaseShader(GL3 gl, MyShader myShader) { + if(null!=myShader) { + gl.glDetachShader(myShader.shaderProgram, myShader.vertShader); + gl.glDeleteShader(myShader.vertShader); + gl.glDeleteProgram(myShader.shaderProgram); + } + } + + + private GLWindow prepareTest() { if(!GLProfile.isGL3Available()) { System.err.println("GL3 not available"); System.err.println(GLProfile.glAvailabilityToString()); - return; + return null; } VERTEX_SHADER_TEXT = "#version 150 \n" @@ -57,9 +97,7 @@ public class TestTransformFeedbackVaryingsBug407NEWT extends UITestCase { + " Position = vec4(1.0, 1.0, 1.0, 1.0); \n" + "} \n"; - GLCapabilities caps; - Window window; - GLDrawable drawable; + GLCapabilities caps; GLProfile glp = null; try { @@ -73,55 +111,28 @@ public class TestTransformFeedbackVaryingsBug407NEWT extends UITestCase { caps.setOnscreen(true); caps.setDoubleBuffered(true); - Display display = NewtFactory.createDisplay(null); // local display - Screen screen = NewtFactory.createScreen(display, 0); // screen 0 - - window = NewtFactory.createWindow(screen, caps); + GLWindow window = GLWindow.create(caps); + Assert.assertNotNull(window); window.setUndecorated(true); window.setSize(800, 600); window.setVisible(true); + Assert.assertTrue(window.isNativeValid()); - drawable = GLDrawableFactory.getFactory(glp).createGLDrawable(window); - drawable.setRealized(true); + // trigger native creation of drawable/context + window.display(); + Assert.assertTrue(window.isRealized()); + Assert.assertTrue(window.getContext().isCreated()); - context = drawable.createContext(null); - - context.makeCurrent(); + return window; } - @After - public void tearDown() { - if(null!=context) { - context.release(); + private void cleanupTest(GLWindow window) { + if(null!=window) { + window.destroy(); } } - private int getShader(GL3 gl, String text, int type) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream pbaos = new PrintStream(baos); - - int shaderProgram = gl.glCreateProgram(); - - int vertShader = gl.glCreateShader(type); - - String[] lines = new String[]{text}; - int[] lengths = new int[]{lines[0].length()}; - gl.glShaderSource(vertShader, lines.length, lines, lengths, 0); - gl.glCompileShader(vertShader); - - if(!ShaderUtil.isShaderStatusValid(gl, vertShader, gl.GL_COMPILE_STATUS, pbaos)) { - System.out.println("getShader:postCompile: "+baos.toString()); - Assert.assertTrue(false); - } - pbaos.flush(); baos.reset(); - - gl.glAttachShader(shaderProgram, vertShader); - - - return shaderProgram; - } - - @Test + @Test(timeout=1000) public void testGlTransformFeedbackVaryings_WhenVarNameOK() { if(!GLProfile.isGL3Available()) { return; @@ -129,29 +140,41 @@ public class TestTransformFeedbackVaryingsBug407NEWT extends UITestCase { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream pbaos = new PrintStream(baos); + GLWindow window = prepareTest(); + GLContext context = window.getContext(); + context.makeCurrent(); + // given GL3 gl = context.getGL().getGL3(); - int shaderProgram = getShader(gl, VERTEX_SHADER_TEXT, GL3.GL_VERTEX_SHADER); + MyShader myShader = getShader(gl, VERTEX_SHADER_TEXT, GL3.GL_VERTEX_SHADER); String[] vars = new String[]{"Position"}; // when - gl.glTransformFeedbackVaryings(shaderProgram, 1, vars, GL3.GL_SEPARATE_ATTRIBS); - gl.glLinkProgram(shaderProgram); + gl.glTransformFeedbackVaryings(myShader.shaderProgram, 1, vars, GL3.GL_SEPARATE_ATTRIBS); + gl.glLinkProgram(myShader.shaderProgram); // then - if(!ShaderUtil.isProgramValid(gl, shaderProgram, pbaos)) { + boolean error = false; + + if(!ShaderUtil.isProgramValid(gl, myShader.shaderProgram, pbaos)) { System.out.println("Error (unexpected link error) - testGlTransformFeedbackVaryings_WhenVarNameOK:postLink: "+baos.toString()); - Assert.assertTrue(false); + error = true; } pbaos.flush(); baos.reset(); Assert.assertEquals(GL3.GL_NO_ERROR, gl.glGetError()); + + releaseShader(gl, myShader); + context.release(); + cleanupTest(window); + + Assert.assertFalse(error); } - @Test + @Test(timeout=1000) public void testGlTransformFeedbackVaryings_WhenVarNameWrong() { if(!GLProfile.isGL3Available()) { return; @@ -159,44 +182,48 @@ public class TestTransformFeedbackVaryingsBug407NEWT extends UITestCase { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream pbaos = new PrintStream(baos); + GLWindow window = prepareTest(); + GLContext context = window.getContext(); + context.makeCurrent(); + // given GL3 gl = context.getGL().getGL3(); - int shaderProgram = getShader(gl, VERTEX_SHADER_TEXT, GL3.GL_VERTEX_SHADER); + MyShader myShader = getShader(gl, VERTEX_SHADER_TEXT, GL3.GL_VERTEX_SHADER); String[] vars = new String[]{"PPPosition"}; // when - gl.glTransformFeedbackVaryings(shaderProgram, 1, vars, GL3.GL_SEPARATE_ATTRIBS); - gl.glLinkProgram(shaderProgram); + gl.glTransformFeedbackVaryings(myShader.shaderProgram, 1, vars, GL3.GL_SEPARATE_ATTRIBS); + gl.glLinkProgram(myShader.shaderProgram); // then - if(!ShaderUtil.isProgramValid(gl, shaderProgram, pbaos)) { + boolean error = false; + + if(!ShaderUtil.isProgramValid(gl, myShader.shaderProgram, pbaos)) { System.out.println("GOOD (expected link error) - testGlTransformFeedbackVaryings_WhenVarNameWrong:postLink: "+baos.toString()); // should be invalid, due to wrong var name } else { - Assert.assertTrue(false); + // oops + System.out.println("Error (unexpected link success) - testGlTransformFeedbackVaryings_WhenVarNameWrong link worked, but it should not"); + error = true; } pbaos.flush(); baos.reset(); Assert.assertEquals(GL3.GL_NO_ERROR, gl.glGetError()); // You cannot assume this error message - Assert.assertTrue(baos.toString().contains("(named PPPosition)")); + + releaseShader(gl, myShader); + context.release(); + cleanupTest(window); + + Assert.assertFalse(error); } public static void main(String args[]) throws IOException { String tstname = TestTransformFeedbackVaryingsBug407NEWT.class.getName(); - org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] { - tstname, - "filtertrace=true", - "haltOnError=false", - "haltOnFailure=false", - "showoutput=true", - "outputtoformatters=true", - "logfailedtests=true", - "logtestlistenerevents=true", - "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter", - "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); + org.junit.runner.JUnitCore.main(tstname); } } diff --git a/src/junit/com/jogamp/test/junit/util/GLSLSimpleProgram.java b/src/junit/com/jogamp/test/junit/util/GLSLSimpleProgram.java new file mode 100644 index 000000000..82be0eb45 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/util/GLSLSimpleProgram.java @@ -0,0 +1,121 @@ +/** + * Copyright 2010 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.test.junit.util; + +import com.jogamp.opengl.util.glsl.ShaderUtil; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import org.junit.Assert; + +public class GLSLSimpleProgram { + private int shaderProgram; + private int vertShader; + private int fragShader; + + private GLSLSimpleProgram(int shaderProgram, int vertShader, int fragShader) { + this.shaderProgram = shaderProgram; + this.vertShader = vertShader; + this.fragShader = fragShader; + } + + public static GLSLSimpleProgram create(GL2ES2 gl, String vertShaderCode, String fragShaderCode, boolean link) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream pbaos = new PrintStream(baos); + + int vertShader = gl.glCreateShader(GL2ES2.GL_VERTEX_SHADER); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + int fragShader = gl.glCreateShader(GL2ES2.GL_FRAGMENT_SHADER); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + String[] vlines = new String[] { vertShaderCode }; + int[] vlengths = new int[] { vlines[0].length() }; + gl.glShaderSource(vertShader, vlines.length, vlines, vlengths, 0); + gl.glCompileShader(vertShader); + if(!ShaderUtil.isShaderStatusValid(gl, vertShader, gl.GL_COMPILE_STATUS, pbaos)) { + System.out.println("getShader:postCompile vertShader: "+baos.toString()); + Assert.assertTrue(false); + } + pbaos.flush(); baos.reset(); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + + String[] flines = new String[] { fragShaderCode }; + int[] flengths = new int[] { flines[0].length() }; + gl.glShaderSource(fragShader, flines.length, flines, flengths, 0); + gl.glCompileShader(fragShader); + if(!ShaderUtil.isShaderStatusValid(gl, fragShader, gl.GL_COMPILE_STATUS, pbaos)) { + System.out.println("getShader:postCompile fragShader: "+baos.toString()); + Assert.assertTrue(false); + } + pbaos.flush(); baos.reset(); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + int shaderProgram = gl.glCreateProgram(); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glAttachShader(shaderProgram, vertShader); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glAttachShader(shaderProgram, fragShader); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + if(link) { + gl.glLinkProgram(shaderProgram); + if(!ShaderUtil.isProgramValid(gl, shaderProgram, pbaos)) { + System.out.println("Error (GLSL link error): "+baos.toString()); + Assert.assertTrue(false); + } + } + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + GLSLSimpleProgram res = new GLSLSimpleProgram(shaderProgram, vertShader, fragShader); + return res; + } + + public void release(GL2ES2 gl) { + gl.glUseProgram(0); + gl.glDetachShader(shaderProgram, vertShader); + gl.glDeleteShader(vertShader); + gl.glDetachShader(shaderProgram, fragShader); + gl.glDeleteShader(fragShader); + gl.glDeleteProgram(shaderProgram); + } + + public int getFragShader() { + return fragShader; + } + + public int getShaderProgram() { + return shaderProgram; + } + + public int getVertShader() { + return vertShader; + } +} |