28package com.jogamp.opengl.test.junit.jogl.glsl;
30import com.jogamp.opengl.util.GLArrayDataServer;
31import com.jogamp.opengl.util.PMVMatrix;
32import com.jogamp.opengl.util.glsl.ShaderCode;
33import com.jogamp.opengl.util.glsl.ShaderProgram;
34import com.jogamp.opengl.util.glsl.ShaderState;
35import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
36import com.jogamp.opengl.test.junit.util.MiscUtils;
37import com.jogamp.opengl.test.junit.util.NEWTGLContext;
38import com.jogamp.opengl.test.junit.util.UITestCase;
40import java.io.IOException;
42import com.jogamp.math.FloatUtil;
43import com.jogamp.opengl.GL;
44import com.jogamp.opengl.GL2ES2;
45import com.jogamp.opengl.GLCapabilities;
46import com.jogamp.opengl.GLContext;
47import com.jogamp.opengl.GLDrawable;
48import com.jogamp.opengl.GLProfile;
49import com.jogamp.opengl.GLUniformData;
50import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
52import org.junit.Assert;
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
63 static long durationPerTest = 10;
65 static final int vertices0_loc = 0;
66 static final int colors0_loc = 5;
69 public
void test01ShaderStatePerformanceDouble() throws InterruptedException {
72 System.err.println(
"CCC01: GLProfile.initSingleton(); START");
74 System.err.println(
"CCC01: GLProfile.initSingleton(); DONE ");
77 System.err.println(
"CCC01: Win + Ctx creation incl 1st makeCurrent.");
80 final GLDrawable drawable = winctx.context.getGLDrawable();
82 System.err.println(winctx.context);
91 "shader/bin",
"RedSquareShader",
true);
93 "shader/bin",
"RedSquareShader",
true);
95 "shader/bin",
"RedSquareShader2",
true);
104 Assert.assertTrue(0 == sp1.
program());
105 Assert.assertTrue(sp1.
init(gl));
106 Assert.assertTrue(0 != sp1.
program());
107 Assert.assertTrue(sp1.
link(gl, System.err));
113 Assert.assertTrue(sp0.
init(gl));
114 Assert.assertTrue(sp0.
link(gl, System.err));
122 st.
uniform(gl, pmvMatrixUniform);
151 st.
uniform(gl, pmvMatrixUniform);
175 final long t0 = System.currentTimeMillis();
189 final long t1 = System.currentTimeMillis();
190 final long dt = t1 - t0;
191 final double fps = ( frames * 1000.0 ) / dt;
192 final String fpsS = String.valueOf(fps);
193 final int fpsSp = fpsS.indexOf(
'.');
194 System.err.println(
"testShaderState01PerformanceDouble: "+dt/1000.0 +
"s: "+ frames +
"f, " + fpsS.substring(0, fpsSp+2) +
" fps, "+dt/frames+
" ms/f");
203 test1XShaderStateValidation(
true);
207 test1XShaderStateValidation(
false);
210 private void test1XShaderStateValidation(
final boolean linkSP1)
throws InterruptedException {
213 System.err.println(
"CCC01: GLProfile.initSingleton(); START");
215 System.err.println(
"CCC01: GLProfile.initSingleton(); DONE ");
218 System.err.println(
"CCC01: Win + Ctx creation incl 1st makeCurrent.");
219 final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(
221 final GLDrawable drawable = winctx.context.getGLDrawable();
223 System.err.println(winctx.context);
237 System.err.println(
"CCC01: swap - release");
238 winctx.drawable.swapBuffers();
239 winctx.context.release();
241 System.err.println(
"CCC01: makeCurrent");
242 final int res = winctx.context.makeCurrent();
249 final ShaderState st =
new ShaderState();
252 "shader/bin",
"RedSquareShader",
true);
254 "shader/bin",
"RedSquareShader",
true);
256 "shader/bin",
"RedSquareShader2",
true);
257 rsVp0.defaultShaderCustomization(gl,
true,
true);
258 rsFp0.defaultShaderCustomization(gl,
true,
true);
259 rsFp1.defaultShaderCustomization(gl,
true,
true);
261 final ShaderProgram sp1 =
new ShaderProgram();
265 Assert.assertTrue(0 == sp1.program());
266 Assert.assertTrue(sp1.init(gl));
267 Assert.assertTrue(0 != sp1.program());
268 Assert.assertTrue(!sp1.inUse());
269 Assert.assertTrue(!sp1.linked());
272 Assert.assertTrue(sp1.link(gl, System.err));
273 Assert.assertTrue(sp1.linked());
277 final ShaderProgram sp0 =
new ShaderProgram();
282 Assert.assertTrue(0 == sp0.program());
283 Assert.assertTrue(sp0.init(gl));
284 Assert.assertTrue(0 != sp0.program());
285 Assert.assertTrue(!sp0.inUse());
286 Assert.assertTrue(!sp0.linked());
289 st.attachShaderProgram(gl, sp0,
false);
290 Assert.assertTrue(!sp0.inUse());
291 Assert.assertTrue(!sp0.linked());
294 final GLArrayDataServer vertices0 = GLSLMiscHelper.createVertices(gl, st, 0, vertices0_loc, GLSLMiscHelper.vertices0);
295 System.err.println(
"vertices0: " + vertices0);
296 vertices0.enableBuffer(gl,
false);
297 Assert.assertEquals(vertices0_loc, vertices0.getLocation());
300 final GLArrayDataServer colors0 = GLSLMiscHelper.createColors(gl, st, 0, colors0_loc, GLSLMiscHelper.colors0);
301 System.err.println(
"colors0: " + colors0);
302 colors0.enableBuffer(gl,
false);
303 Assert.assertEquals(colors0_loc, colors0.getLocation());
305 Assert.assertTrue(sp0.link(gl, System.err));
306 Assert.assertTrue(sp0.linked());
309 Assert.assertEquals(vertices0_loc, vertices0.getLocation());
310 Assert.assertEquals(vertices0_loc, st.getAttribLocation(gl, vertices0.getName()));
311 Assert.assertEquals(vertices0_loc, gl.
glGetAttribLocation(st.shaderProgram().program(), vertices0.getName()));
313 Assert.assertEquals(colors0_loc, colors0.getLocation());
314 Assert.assertEquals(colors0_loc, st.getAttribLocation(gl, colors0.getName()));
315 Assert.assertEquals(colors0_loc, gl.
glGetAttribLocation(st.shaderProgram().program(), colors0.getName()));
317 st.useProgram(gl,
true);
318 Assert.assertTrue(sp0.inUse());
322 final PMVMatrix pmvMatrix =
new PMVMatrix();
324 st.ownUniform(pmvMatrixUniform);
327 st.uniform(gl, pmvMatrixUniform);
329 Assert.assertEquals(pmvMatrixUniform, st.getUniform(
"mgl_PMVMatrix"));
332 final GLArrayDataServer vertices1 = GLSLMiscHelper.createVertices(gl, st, 0, -1, GLSLMiscHelper.vertices1);
333 System.err.println(
"vertices1: " + vertices1);
334 vertices1.enableBuffer(gl,
false);
337 final GLArrayDataServer colors1 = GLSLMiscHelper.createColors(gl, st, 0, -1, GLSLMiscHelper.colors1);
338 System.err.println(
"colors1: " + colors1);
339 colors1.enableBuffer(gl,
false);
347 pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
348 pmvMatrix.glLoadIdentity();
350 pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
351 pmvMatrix.glLoadIdentity();
352 pmvMatrix.glTranslatef(0, 0, -10);
353 st.uniform(gl, pmvMatrixUniform);
358 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices0, colors0,
true, 1, durationPerTest);
361 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices1, colors1,
true, 2, durationPerTest);
364 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices0, colors0,
true, 3, durationPerTest);
367 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices1, colors1,
true, 4, durationPerTest);
370 st.attachShaderProgram(gl, sp1,
true);
371 Assert.assertTrue(sp1.inUse());
372 Assert.assertTrue(sp1.linked());
376 Assert.assertEquals(vertices0_loc, vertices0.getLocation());
377 Assert.assertEquals(vertices0_loc, st.getAttribLocation(gl, vertices0.getName()));
378 Assert.assertEquals(vertices0_loc, gl.
glGetAttribLocation(st.shaderProgram().program(), vertices0.getName()));
380 Assert.assertEquals(colors0_loc, colors0.getLocation());
381 Assert.assertEquals(colors0_loc, st.getAttribLocation(gl, colors0.getName()));
382 Assert.assertEquals(colors0_loc, gl.
glGetAttribLocation(st.shaderProgram().program(), colors0.getName()));
386 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices0, colors0,
true, 10, durationPerTest);
389 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices1, colors1,
true, 20, durationPerTest);
392 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices0, colors0,
true, 30, durationPerTest);
395 GLSLMiscHelper.displayVCArrays(drawable, gl, st,
true, vertices1, colors1,
true, 40, durationPerTest);
400 NEWTGLContext.destroyWindow(winctx);
403 public static void main(
final String args[])
throws IOException {
404 System.err.println(
"main - start");
405 boolean wait =
false;
406 for(
int i=0; i<args.length; i++) {
407 if(args[i].equals(
"-time")) {
408 durationPerTest =
MiscUtils.
atoi(args[++i], (
int)durationPerTest);
410 if(args[i].equals(
"-wait")) {
415 while(-1 == System.in.read()) ;
419 }
catch (
final Exception e) {
424 org.junit.runner.JUnitCore.
main(tstname);
425 System.err.println(
"main - end");
Basic Float math utility functions.
static final float QUARTER_PI
The value PI/4, i.e.
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
Specifies a set of OpenGL capabilities.
Abstraction for an OpenGL rendering context.
static final int CONTEXT_CURRENT
Indicates that the context was made current during the last call to makeCurrent, value {@value}.
static final int CONTEXT_CURRENT_NEW
Indicates that a newly-created context was made current during the last call to makeCurrent,...
Specifies the the OpenGL profile.
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
static void initSingleton()
Static initialization of JOGL.
static final float[] vertices0
static final int frames_perftest
static final float[] colors0
static GLArrayDataServer createColors(final GL2ES2 gl, final ShaderState st, final int shaderProgram, final int location, final float[] colors)
static final float[] vertices1
static GLArrayDataServer createVertices(final GL2ES2 gl, final ShaderState st, final int shaderProgram, final int location, final float[] vertices)
static final int frames_warmup
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 final float[] colors1
static void displayVCArraysNoChecks(final GLDrawable drawable, final GL2ES2 gl, final boolean preEnable, final GLArrayDataServer vertices, final GLArrayDataServer colors, final boolean postDisable)
Testing different vertex attribute (VA) data sets on one shader and shader state in general.
void test01ShaderStatePerformanceDouble()
static void main(final String args[])
void test11ShaderStateValidationSP1Linked()
void test12ShaderStateValidationSP1Unlinked()
static int atoi(final String str, final int def)
static WindowContext createWindow(final GLCapabilities caps, final int width, final int height, final boolean debugGL)
static void destroyWindow(final WindowContext winctx)
void enableBuffer(final GL gl, final boolean enable)
Enables the buffer if enable is true, and transfers the data if required.
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
final void glTranslatef(final float x, final float y, final float z)
Translate the current matrix.
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
final void gluPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Multiply the current matrix with the perspective/frustum matrix.
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Convenient shader code class to use and instantiate vertex or fragment programs.
final int defaultShaderCustomization(final GL2ES2 gl, final boolean preludeVersion, final boolean addDefaultPrecision)
Default customization of this shader source code.
static ShaderCode create(final GL2ES2 gl, final int type, final int count, final Class<?> context, final String[] sourceFiles, final boolean mutableStringBuilder)
Creates a complete ShaderCode object while reading all shader source of sourceFiles,...
int program()
Returns the shader program name, which is non zero if valid.
synchronized final boolean init(final GL2ES2 gl)
Creates the empty GL program object using GL2ES2#glCreateProgram(), if not already created.
synchronized boolean link(final GL2ES2 gl, final PrintStream verboseOut)
Links the shader code to the program.
synchronized void add(final ShaderCode shaderCode)
Adds a new shader to this program.
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
synchronized boolean attachShaderProgram(final GL2ES2 gl, final ShaderProgram prog, final boolean enable)
Attach or switch a shader program.
synchronized void destroy(final GL2ES2 gl)
Calls release(gl, true, true, true).
boolean uniform(final GL2ES2 gl, final GLUniformData data)
Set the uniform data, if it's location is valid, i.e.
void ownUniform(final GLUniformData uniform)
Bind the GLUniform lifecycle to this ShaderState.
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...
int glGetAttribLocation(int program, String name)
Entry point to C language function: GLint {@native glGetAttribLocation}(GLuint program,...
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...
GL getGL()
Casts this object to the GL interface.
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.
An abstraction for an OpenGL rendering target.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
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...
int glGetError()
Entry point to C language function: GLenum {@native glGetError}() Part of GL_ES_VERSION_2_0,...
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...
void glViewport(int x, int y, int width, int height)
Entry point to C language function: void {@native glViewport}(GLint x, GLint y, GLsizei width,...
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.