JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
GLSLMiscHelper.java
Go to the documentation of this file.
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 */
28package com.jogamp.opengl.test.junit.jogl.glsl;
29
30import com.jogamp.opengl.util.GLArrayDataServer;
31import com.jogamp.opengl.util.glsl.ShaderState;
32
33import com.jogamp.opengl.GL;
34import com.jogamp.opengl.GL2ES2;
35import com.jogamp.opengl.GLBufferStorage;
36import com.jogamp.opengl.GLDrawable;
37
38import org.junit.Assert;
39
40public class GLSLMiscHelper {
41 public static final int frames_perftest = 600; // frames
42 public static final int frames_warmup = 100; // frames
43
44 public static void validateGLArrayDataServerState(final GL2ES2 gl, final ShaderState st, final GLArrayDataServer data) {
45 final int[] qi = new int[1];
46 if(null != st) {
47 Assert.assertEquals(data, st.getAttribute(data.getName()));
48 if(st.shaderProgram().linked()) {
49 Assert.assertEquals(data.getLocation(), st.getCachedAttribLocation(data.getName()));
50 Assert.assertEquals(data.getLocation(), st.getAttribLocation(gl, data));
51 Assert.assertEquals(data.getLocation(), st.getAttribLocation(gl, data.getName()));
52 Assert.assertEquals(data.getLocation(), gl.glGetAttribLocation(st.shaderProgram().program(), data.getName()));
53 }
54 }
56 Assert.assertEquals(data.enabled()?GL.GL_TRUE:GL.GL_FALSE, qi[0]);
58 Assert.assertEquals(data.getVBOName(), qi[0]);
59 final GLBufferStorage glStore = gl.getBufferStorage(data.getVBOName());
60 Assert.assertEquals("GLBufferStorage size mismatch, storage "+glStore, data.getByteCount(), null != glStore ? glStore.getSize() : -1);
61 }
62
63 public static void pause(final long ms) throws InterruptedException {
64 final long t0 = System.currentTimeMillis();
65 while( System.currentTimeMillis() - t0 < ms) {
66 Thread.sleep(ms);
67 }
68 }
69
70 public static void displayVCArrays(final GLDrawable drawable, final GL2ES2 gl, final ShaderState st,
71 final boolean preEnable, final GLArrayDataServer vertices, final GLArrayDataServer colors,
72 final boolean postDisable, final int num, final long postDelay)
73 throws InterruptedException
74 {
75 System.err.println("screen #"+num);
76 if(preEnable) {
77 vertices.enableBuffer(gl, true);
78 // invalid - Assert.assertEquals(vertices.getVBOName(), gl.glGetBoundBuffer(GL.GL_ARRAY_BUFFER));
79 colors.enableBuffer(gl, true);
80 // invalid - Assert.assertEquals(colors.getVBOName(), gl.glGetBoundBuffer(GL.GL_ARRAY_BUFFER));
81 //
82 // Above assertions are invalid, since GLSLArrayHandler will not bind the VBO to target
83 // if the VBO is already bound to the attribute itself.
84 // validateGLArrayDataServerState(..) does check proper VBO to attribute binding.
85 }
86 Assert.assertTrue(vertices.enabled());
87 Assert.assertTrue(colors.enabled());
88
89 validateGLArrayDataServerState(gl, st, vertices);
90 validateGLArrayDataServerState(gl, st, colors);
92 gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4);
93 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
94 if(postDisable) {
95 vertices.enableBuffer(gl, false);
96 colors.enableBuffer(gl, false);
97 Assert.assertTrue(!vertices.enabled());
98 Assert.assertTrue(!colors.enabled());
99 }
100 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
101 drawable.swapBuffers();
102 if(postDelay>0) { pause(postDelay); }
103 }
104
105 public static void displayVCArraysNoChecks(final GLDrawable drawable, final GL2ES2 gl, final boolean preEnable, final GLArrayDataServer vertices, final GLArrayDataServer colors, final boolean postDisable) throws InterruptedException {
106 if(preEnable) {
107 vertices.enableBuffer(gl, true);
108 colors.enableBuffer(gl, true);
109 }
111 gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4);
112 if(postDisable) {
113 vertices.enableBuffer(gl, false);
114 colors.enableBuffer(gl, false);
115 }
116 drawable.swapBuffers();
117 }
118
119 public static GLArrayDataServer createVertices(final GL2ES2 gl, final ShaderState st, final int shaderProgram, final int location, final float[] vertices) {
120 if(null != st && 0 != shaderProgram) {
121 throw new InternalError("Use either ShaderState _or_ shader-program, not both");
122 }
123 if(null == st && 0 == shaderProgram) {
124 throw new InternalError("Pass a valid ShaderState _xor_ shader-program, not none");
125 }
126 // Allocate Vertex Array0
127 final GLArrayDataServer vDataArray = GLArrayDataServer.createGLSL("mgl_Vertex", 3, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
128 if(null != st) {
129 st.ownAttribute(vDataArray, true);
130 if(0<=location) {
131 st.bindAttribLocation(gl, location, vDataArray);
132 }
133 } else {
134 if(0<=location) {
135 vDataArray.setLocation(gl, shaderProgram, location);
136 } else {
137 vDataArray.setLocation(gl, shaderProgram);
138 }
139 }
140 Assert.assertTrue(vDataArray.isVBO());
141 Assert.assertTrue(vDataArray.isVertexAttribute());
142 Assert.assertTrue(!vDataArray.isVBOWritten());
143 Assert.assertTrue(!vDataArray.sealed());
144 int i=0;
145 vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]);
146 vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]);
147 vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]);
148 vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]); vDataArray.putf(vertices[i++]);
149 vDataArray.seal(gl, true);
150 Assert.assertTrue(vDataArray.isVBOWritten());
151 Assert.assertTrue(vDataArray.sealed());
152 Assert.assertEquals(4, vDataArray.getElemCount());
153 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
154 Assert.assertEquals(0, gl.getBoundBuffer(GL.GL_ARRAY_BUFFER)); // should be cleared ASAP
155 validateGLArrayDataServerState(gl, st, vDataArray);
156 return vDataArray;
157 }
158 public static final float[] vertices0 = new float[] { -2f, 2f, 0f,
159 2f, 2f, 0f,
160 -2f, -2f, 0f,
161 2f, -2f, 0f };
162
163 public static final float[] vertices1 = new float[] { -2f, 1f, 0f,
164 2f, 1f, 0f,
165 -2f, -1f, 0f,
166 2f, -1f, 0f };
167
168 public static GLArrayDataServer createColors(final GL2ES2 gl, final ShaderState st, final int shaderProgram, final int location, final float[] colors) {
169 if(null != st && 0 != shaderProgram) {
170 throw new InternalError("Use either ShaderState _or_ shader-program, not both");
171 }
172 if(null == st && 0 == shaderProgram) {
173 throw new InternalError("Pass a valid ShaderState _xor_ shader-program, not none");
174 }
175 final GLArrayDataServer cDataArray = GLArrayDataServer.createGLSL("mgl_Color", 4, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
176 if(null != st) {
177 st.ownAttribute(cDataArray, true);
178 if(0<=location) {
179 st.bindAttribLocation(gl, location, cDataArray);
180 }
181 } else {
182 if(0<=location) {
183 cDataArray.setLocation(gl, shaderProgram, location);
184 } else {
185 cDataArray.setLocation(gl, shaderProgram);
186 }
187 }
188 int i=0;
189 cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]);
190 cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]);
191 cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]);
192 cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]); cDataArray.putf(colors[i++]);
193 cDataArray.seal(gl, true);
194 Assert.assertTrue(cDataArray.isVBO());
195 Assert.assertTrue(cDataArray.isVertexAttribute());
196 Assert.assertTrue(cDataArray.isVBOWritten());
197 Assert.assertTrue(cDataArray.sealed());
198 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
199 Assert.assertEquals(0, gl.getBoundBuffer(GL.GL_ARRAY_BUFFER)); // should be cleared ASAP
200 validateGLArrayDataServerState(gl, st, cDataArray);
201 return cDataArray;
202 }
203 public static final float[] colors0 = new float[] { 1f, 0f, 0f, 1f,
204 0f, 0f, 1f, 1f,
205 1f, 0f, 0f, 1f,
206 1f, 0f, 1f, 1f };
207
208 public static final float[] colors1 = new float[] { 1f, 0f, 1f, 1f,
209 0f, 1f, 0f, 1f,
210 1f, 0f, 1f, 1f,
211 1f, 0f, 1f, 1f };
212
213}
OpenGL buffer storage object reflecting it's.
final long getSize()
Return the buffer's storage size.
static void validateGLArrayDataServerState(final GL2ES2 gl, final ShaderState st, final GLArrayDataServer data)
static GLArrayDataServer createColors(final GL2ES2 gl, final ShaderState st, final int shaderProgram, final int location, final float[] colors)
static GLArrayDataServer createVertices(final GL2ES2 gl, final ShaderState st, final int shaderProgram, final int location, final float[] vertices)
static void displayVCArrays(final GLDrawable drawable, final GL2ES2 gl, final ShaderState st, final boolean preEnable, final GLArrayDataServer vertices, final GLArrayDataServer colors, final boolean postDisable, final int num, final long postDelay)
static void displayVCArraysNoChecks(final GLDrawable drawable, final GL2ES2 gl, final boolean preEnable, final GLArrayDataServer vertices, final GLArrayDataServer colors, final boolean postDisable)
final boolean isVBOWritten()
Is the buffer written to the VBO ?
void seal(final GL gl, final boolean seal)
Convenience method calling seal(boolean) and enableBuffer(GL, boolean).
static GLArrayDataServer createGLSL(final String name, final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO, using a custom GLSL array attribute name and starting with a new created Buffer object ...
final boolean isVBO()
Determines whether the data is server side (VBO) and enabled, or a client side array (false).
final int getByteCount()
Returns the byte position (written elements) if not sealed() or the byte limit (available to read) af...
final int getElemCount()
Returns the element position (written elements) if not sealed() or the element limit (available to re...
final boolean isVertexAttribute()
Returns true if this data set is intended for a GLSL vertex shader attribute, otherwise false,...
final int setLocation(final int v)
Sets the given location of the shader attribute.
final String getName()
The name of the reflecting shader array attribute.
final int getVBOName()
The VBO name or 0 if not a VBO.
final int getLocation()
Returns the shader attribute location for this name, -1 if not yet determined.
int program()
Returns the shader program name, which is non zero if valid.
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
void ownAttribute(final GLArrayData attribute, final boolean own)
Binds or unbinds the GLArrayData lifecycle to this ShaderState.
int getAttribLocation(final GL2ES2 gl, final String name)
Gets the location of a shader attribute with given name.
int getCachedAttribLocation(final String name)
Gets the cached location of a shader attribute.
GLArrayData getAttribute(final String name)
Get the previous cached vertex attribute data.
void bindAttribLocation(final GL2ES2 gl, final int location, final String name)
Binds a shader attribute to a location.
void glGetVertexAttribiv(int index, int pname, IntBuffer params)
Entry point to C language function: void {@native glGetVertexAttribiv}(GLuint index,...
int glGetAttribLocation(int program, String name)
Entry point to C language function: GLint {@native glGetAttribLocation}(GLuint program,...
static final int GL_VERTEX_ATTRIB_ARRAY_ENABLED
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_ARB_vertex_program Alias for: GL_VERTEX_ATTRIB_ARRAY_ENABLED_AR...
Definition: GL2ES2.java:605
static final int GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_ARB_vertex_buffer_object Alias for: GL_VERTEX_ATTRIB_ARRAY_BUFF...
Definition: GL2ES2.java:599
int getBoundBuffer(int target)
GLBufferStorage getBufferStorage(int bufferName)
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
static final int GL_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
Definition: GL.java:673
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
static final int GL_NO_ERROR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_NO_ERROR" with expres...
Definition: GL.java:481
int glGetError()
Entry point to C language function: GLenum {@native glGetError}() Part of GL_ES_VERSION_2_0,...
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
static final int GL_FALSE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FALSE" with expressio...
Definition: GL.java:251
static final int GL_TRIANGLE_STRIP
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLE_STRIP" with ...
Definition: GL.java:760
static final int GL_TRUE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRUE" with expression...
Definition: GL.java:308
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_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Definition: GL.java:633