1 /**
2  * Copyright 2010 JogAmp Community. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice, this list of
8  *       conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *       of conditions and the following disclaimer in the documentation and/or other materials
12  *       provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  * or implied, of JogAmp Community.
27  */
28 package com.jogamp.opengl.test.junit.jogl.glsl;
29 
30 import com.jogamp.opengl.test.junit.util.UITestCase;
31 import com.jogamp.opengl.util.Animator;
32 
33 import com.jogamp.opengl.GL;
34 import com.jogamp.opengl.GL2ES2;
35 import com.jogamp.opengl.GL2GL3;
36 import com.jogamp.opengl.GLAutoDrawable;
37 import com.jogamp.opengl.GLCapabilities;
38 import com.jogamp.opengl.GLEventListener;
39 import com.jogamp.opengl.GLException;
40 import com.jogamp.opengl.GLProfile;
41 import com.jogamp.opengl.awt.GLCanvas;
42 
43 import java.awt.Frame;
44 
45 import org.junit.Assert;
46 import org.junit.Assume;
47 import org.junit.BeforeClass;
48 import org.junit.AfterClass;
49 import org.junit.Test;
50 import org.junit.FixMethodOrder;
51 import org.junit.runners.MethodSorters;
52 
53 /**
54  * Duplicates bug 459, where a vertex shader won't compile when 8 bits of stencil are requested.
55  * This bug is Windows-only; it works on Mac OS X and CentOS.
56  */
57 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
58 public class TestShaderCompilationBug459AWT extends UITestCase {
59     static int width, height;
60     static long duration = 500; // ms
61     /** Exception in shader code sets this, since it won't bubble up through AWT. */
62     GLException glexception;
63 
64     @BeforeClass
initClass()65     public static void initClass() {
66         width  = 512;
67         height = 512;
68     }
69 
70     @AfterClass
releaseClass()71     public static void releaseClass() {
72     }
73 
74     @Test
compileShader()75     public void compileShader() throws InterruptedException {
76         final GLProfile glp = GLProfile.get(GLProfile.GL2GL3);
77         final GLCapabilities caps = new GLCapabilities(glp);
78         // commenting out this line makes it work
79         caps.setStencilBits(8);
80 
81         // commenting in this line also makes it work
82         //caps.setSampleBuffers(true);
83 
84         final Frame frame = new Frame("Bug 459 shader compilation test");
85         Assert.assertNotNull(frame);
86 
87         final GLCanvas glCanvas = new GLCanvas(caps);
88         Assert.assertNotNull(glCanvas);
89         frame.add(glCanvas);
90 
91         glCanvas.addGLEventListener(new GLEventListener() {
92             /* @Override */
93             public void init(final GLAutoDrawable drawable) {
94                 final String code = "void main(void){gl_Position = vec4(0,0,0,1);}";
95 
96                 final GL2GL3 gl = drawable.getGL().getGL2GL3();
97                 final int id = gl.glCreateShader(GL2ES2.GL_VERTEX_SHADER);
98 
99                 try {
100                     gl.glShaderSource(id, 1, new String[] { code }, (int[])null, 0);
101                     gl.glCompileShader(id);
102 
103                     final int[] compiled = new int[1];
104                     gl.glGetShaderiv(id, GL2ES2.GL_COMPILE_STATUS, compiled, 0);
105                     if (compiled[0] == GL.GL_FALSE) {
106                         final int[] logLength = new int[1];
107                         gl.glGetShaderiv(id, GL2ES2.GL_INFO_LOG_LENGTH, logLength, 0);
108 
109                         final byte[] log = new byte[logLength[0]];
110                         gl.glGetShaderInfoLog(id, logLength[0], (int[])null, 0, log, 0);
111 
112                         System.err.println("Error compiling the shader: " + new String(log));
113 
114                         gl.glDeleteShader(id);
115                     }
116                     else {
117                         System.out.println("Shader compiled: id=" + id);
118                     }
119                 }
120                 catch( final GLException e ) {
121                     glexception = e;
122                 }
123             }
124 
125             /* @Override */
126             public void dispose(final GLAutoDrawable drawable) {
127             }
128 
129             /* @Override */
130             public void display(final GLAutoDrawable drawable) {
131             }
132 
133             /* @Override */
134             public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
135             }
136         });
137 
138         final Animator animator = new Animator(glCanvas);
139         try {
140             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
141                 public void run() {
142                     frame.setSize(512, 512);
143                     frame.setVisible(true);
144                 } } );
145         } catch(final Exception ex) {
146             throw new RuntimeException(ex);
147         }
148         animator.setUpdateFPSFrames(1, null);
149         animator.start();
150 
151         while(animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
152             Thread.sleep(100);
153         }
154 
155         Assert.assertTrue( glexception != null ? glexception.getMessage() : "", glexception == null );
156         Assert.assertNotNull(frame);
157         Assert.assertNotNull(glCanvas);
158         Assert.assertNotNull(animator);
159 
160         animator.stop();
161         Assert.assertEquals(false, animator.isAnimating());
162         try {
163             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
164                 public void run() {
165                     frame.setVisible(false);
166                     frame.remove(glCanvas);
167                     frame.dispose();
168                 }});
169         } catch( final Throwable throwable ) {
170             throwable.printStackTrace();
171             Assume.assumeNoException( throwable );
172         }
173     }
174 
main(final String args[])175     public static void main(final String args[]) {
176         org.junit.runner.JUnitCore.main(TestShaderCompilationBug459AWT.class.getName());
177     }
178 }
179