JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestCPUSourcingAPINEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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
29package com.jogamp.opengl.test.junit.jogl.acore;
30
31import java.nio.FloatBuffer;
32import java.nio.ShortBuffer;
33
34import com.jogamp.common.nio.Buffers;
35import com.jogamp.opengl.test.junit.util.MiscUtils;
36import com.jogamp.opengl.test.junit.util.UITestCase;
37import com.jogamp.opengl.util.GLBuffers;
38
39import com.jogamp.opengl.GL;
40import com.jogamp.opengl.GL2;
41import com.jogamp.opengl.GL2ES2;
42import com.jogamp.opengl.GLAutoDrawable;
43import com.jogamp.opengl.GLCapabilities;
44import com.jogamp.opengl.GLDrawableFactory;
45import com.jogamp.opengl.GLEventListener;
46import com.jogamp.opengl.GLException;
47import com.jogamp.opengl.GLOffscreenAutoDrawable;
48import com.jogamp.opengl.GLProfile;
49
50import org.junit.Assert;
51import org.junit.Test;
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55/**
56 * CPU sourced data API entry not allowed on ES3 and GL core >= 3.0.
57 *
58 * See https://jogamp.org/bugzilla/show_bug.cgi?id=852#c1
59 */
60@FixMethodOrder(MethodSorters.NAME_ASCENDING)
61public class TestCPUSourcingAPINEWT extends UITestCase {
62 static long duration = 500; // ms
63
64 static class Demo implements GLEventListener {
65 private final static float[] vertexColorData = new float[]{
66 0.0f, 0.75f, 0.0f, 1,0,0,
67 -0.5f, -0.75f, 0.0f, 0,1,0,
68 0.9f, -0.75f, 0.0f, 0,0,1
69 };
70 private final FloatBuffer vertexColorDataBuffer = GLBuffers.newDirectFloatBuffer(vertexColorData);
71
72 private final short[] indices = new short[]{0, 1, 2};
73 private final ShortBuffer indicesBuffer = GLBuffers.newDirectShortBuffer(indices);
74
75
76 private int vertID = -1;
77 private int fragID = -1;
78 private int progID = -1;
79
80 private static int createShader(final GL2ES2 gl, final int type,
81 final String[] srcLines){
82 final int shaderID = gl.glCreateShader(type);
83 assert shaderID > 0;
84 final int[] lengths = new int[srcLines.length];
85 for (int i = 0; i < srcLines.length; i++) {
86 lengths[i] = srcLines[i].length();
87 }
88 gl.glShaderSource(shaderID, srcLines.length, srcLines, lengths, 0);
89 gl.glCompileShader(shaderID);
90 return shaderID;
91 }
92
93 private void initShaders(final GL2ES2 gl) {
94 final String[] vertSrc = new String[]{
95 "#version 150\n",
96 "in vec4 vPosition;\n",
97 "in vec4 vColor;\n",
98 "out vec4 pColor;\n",
99 "void main() {\n",
100 " pColor = vColor;\n",
101 " gl_Position = vPosition;\n",
102 "}\n"
103 };
104 vertID = createShader(gl, GL2ES2.GL_VERTEX_SHADER, vertSrc);
105
106 final String[] fragSrc = new String[]{
107 "#version 150\n",
108 "in vec4 pColor;\n",
109 "void main() {\n",
110 " gl_FragColor = pColor;\n",
111 "}\n"
112 };
113 fragID = createShader(gl, GL2ES2.GL_FRAGMENT_SHADER, fragSrc);
114
115 // We're done with the compiler
117
118 progID = gl.glCreateProgram();
119 assert progID > 0;
120 gl.glAttachShader(progID, vertID);
121 gl.glAttachShader(progID, fragID);
122 gl.glLinkProgram(progID);
123 gl.glValidateProgram(progID);
124 }
125
126 @Override
127 public void init(final GLAutoDrawable drawable) {
128 final GL2ES2 gl = drawable.getGL().getGL2ES2();
131 initShaders(gl);
132
133 gl.setSwapInterval(1);
134 }
135
136 @Override
137 public void dispose(final GLAutoDrawable drawable) {
138 final GL2ES2 gl = drawable.getGL().getGL2ES2();
139 gl.glDetachShader(progID, fragID);
140 gl.glDetachShader(progID, vertID);
141 gl.glDeleteProgram(progID);
142 gl.glDeleteShader(fragID);
143 gl.glDeleteShader(vertID);
144 }
145
146 private void displayCPUSourcing(final GL2 gl) {
147 final int posLoc = gl.glGetAttribLocation(progID, "vPosition");
148 final int colorLoc = gl.glGetAttribLocation(progID, "vColor");
149 gl.glEnableVertexAttribArray(posLoc);
150 gl.glEnableVertexAttribArray(colorLoc);
151
152 final int stride = 6 * Buffers.SIZEOF_FLOAT;
153 // final int cOff = 3 * Buffers.SIZEOF_FLOAT;
154 gl.glVertexAttribPointer(posLoc, 3, GL.GL_FLOAT, false, stride, vertexColorDataBuffer);
155 vertexColorDataBuffer.position(3); // move to cOff
156 gl.glVertexAttribPointer(colorLoc,3, GL.GL_FLOAT, false, stride, vertexColorDataBuffer);
157 vertexColorDataBuffer.position(0); // rewind cOff
158
159 gl.glDrawElements(GL.GL_TRIANGLES, 3, GL.GL_UNSIGNED_SHORT, indicesBuffer);
160
162 gl.glDisableVertexAttribArray(colorLoc);
163 }
164
165 @Override
166 public void display(final GLAutoDrawable drawable) {
167 final GL2ES2 gl = drawable.getGL().getGL2ES2();
168 gl.glClearColor(0x44, 0x44, 0x44, 0);
170 gl.glUseProgram(progID);
171
172 // Hard casting is of course invalid!
173 // But we need to 'fake' compatibility mode to trigger CPU-sourcing w/ GL3 core
174 displayCPUSourcing((GL2) gl);
175
176 gl.glUseProgram(0);
177 }
178
179 @Override
180 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int w, final int h) {
181 }
182 }
183
184 private void testImpl(final GLProfile profile) throws InterruptedException {
185 final GLCapabilities capabilities = new GLCapabilities(profile);
186 final GLDrawableFactory factory = GLDrawableFactory.getFactory(profile);
187 final GLOffscreenAutoDrawable glad = factory.createOffscreenAutoDrawable(null, capabilities, null, 512, 512);
188
189 final Demo vaoTest = new Demo();
190 glad.addGLEventListener(vaoTest);
191 glad.display();
192
193 glad.destroy();
194 }
195
196 @Test
197 public void test01GL2CPUSource() throws GLException, InterruptedException {
199 System.err.println("GL2 n/a");
200 return;
201 }
202 testImpl(GLProfile.get(GLProfile.GL2));
203 }
204
205 @Test
206 public void test02GL3CPUSource() throws GLException, InterruptedException {
208 if( !glp.isGL3ES3() && !glp.isGL2ES2() ) {
209 System.err.println("No GL core profile available, got "+glp);
210 return;
211 }
212 GLException exp = null;
213 try {
214 testImpl(glp);
215 } catch(final GLException gle) {
216 exp = gle;
217 System.err.println("Expected Exception: "+exp.getMessage());
218 }
219 Assert.assertNotNull("Excpected GLException missing due to CPU Sourcing w/ GL3 core context", exp);
220 }
221
222 public static void main(final String args[]) {
223 for(int i=0; i<args.length; i++) {
224 if(args[i].equals("-time")) {
225 i++;
226 duration = MiscUtils.atol(args[i], duration);
227 }
228 }
229 org.junit.runner.JUnitCore.main(TestCPUSourcingAPINEWT.class.getName());
230 }
231}
Specifies a set of OpenGL capabilities.
abstract GLOffscreenAutoDrawable createOffscreenAutoDrawable(AbstractGraphicsDevice device, GLCapabilitiesImmutable caps, GLCapabilitiesChooser chooser, int width, int height)
Creates a realized GLOffscreenAutoDrawable incl it's offscreen NativeSurface with the given capabilit...
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
final boolean isGL2ES2()
Indicates whether this profile is capable of GL2ES2.
final boolean isGL3ES3()
Indicates whether this profile is capable of GL3ES3.
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
static GLProfile getMaxProgrammableCore(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the programmable shader core pipeline only.
Definition: GLProfile.java:854
CPU sourced data API entry not allowed on ES3 and GL core >= 3.0.
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
Utility routines for dealing with direct buffers.
Definition: GLBuffers.java:60
void glDrawElements(int mode, int count, int type, Buffer indices)
Entry point to C language function: void {@native glDrawElements}(GLenum mode, GLsizei count,...
static final int GL_VERTEX_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_EXT_vertex_shader, GL_ARB_vertex_shader Alias for: GL_VERTEX_SH...
Definition: GL2ES2.java:39
void glEnableVertexAttribArray(int index)
Entry point to C language function: void {@native glEnableVertexAttribArray}(GLuint index) Part of...
void glCompileShader(int shader)
Entry point to C language function: void {@native glCompileShader}(GLuint shader) Part of GL_ES_VE...
void glDeleteShader(int shader)
Entry point to C language function: void {@native glDeleteShader}(GLuint shader) Part of GL_ES_VER...
void glValidateProgram(int program)
Entry point to C language function: void {@native glValidateProgram}(GLuint program) Part of GL_ES...
void glDetachShader(int program, int shader)
Entry point to C language function: void {@native glDetachShader}(GLuint program,...
int glGetAttribLocation(int program, String name)
Entry point to C language function: GLint {@native glGetAttribLocation}(GLuint program,...
int glCreateProgram()
Entry point to C language function: GLuint {@native glCreateProgram}() Part of GL_ES_VERSION_2_0,...
void glUseProgram(int program)
Entry point to C language function: void {@native glUseProgram}(GLuint program) Part of GL_ES_VERS...
void glReleaseShaderCompiler()
Start: GL_ARB_ES2_compatibility functions, which are part of ES2 core as well.
void glDisableVertexAttribArray(int index)
Entry point to C language function: void {@native glDisableVertexAttribArray}(GLuint index) Part o...
void glShaderSource(int shader, int count, String[] string, IntBuffer length)
Entry point to C language function: void {@native glShaderSource}(GLuint shader, GLsizei count,...
void glAttachShader(int program, int shader)
Entry point to C language function: void {@native glAttachShader}(GLuint program,...
void glDeleteProgram(int program)
Entry point to C language function: void {@native glDeleteProgram}(GLuint program) Part of GL_ES_V...
int glCreateShader(int type)
Entry point to C language function: GLuint {@native glCreateShader}(GLenum type) Part of GL_ES_VER...
void glLinkProgram(int program)
Entry point to C language function: void {@native glLinkProgram}(GLuint program) Part of GL_ES_VER...
static final int GL_FRAGMENT_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_ATI_fragment_shader, GL_ARB_fragment_shader Alias for: GL_FRAGM...
Definition: GL2ES2.java:541
void glVertexAttribPointer(int indx, int size, int type, boolean normalized, int stride, Buffer ptr)
Entry point to C language function: void {@native glVertexAttribPointer}(GLuint indx,...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
Platform-independent GLAutoDrawable specialization, exposing offscreen functionality.
static final int GL_TRIANGLES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLES" with expre...
Definition: GL.java:145
static final int GL_FLOAT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FLOAT" with expressio...
Definition: GL.java:786
void glDisable(int cap)
Entry point to C language function: void {@native glDisable}(GLenum cap) Part of GL_ES_VERSION_2_0...
static final int GL_UNSIGNED_SHORT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_SHORT" with ...
Definition: GL.java:346
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
Definition: GL.java:43
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
static final int GL_CULL_FACE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_CULL_FACE" with expre...
Definition: GL.java:720