JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGPUMemSec01NEWT.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.acore;
29
30import com.jogamp.common.nio.Buffers;
31import com.jogamp.opengl.util.GLPixelStorageModes;
32import com.jogamp.opengl.test.junit.util.NEWTGLContext;
33import com.jogamp.opengl.test.junit.util.UITestCase;
34
35import java.io.IOException;
36import java.nio.ByteBuffer;
37
38import com.jogamp.opengl.GL;
39import com.jogamp.opengl.GL2ES2;
40import com.jogamp.opengl.GL2GL3;
41import com.jogamp.opengl.GLCapabilities;
42import com.jogamp.opengl.GLDrawable;
43import com.jogamp.opengl.GLException;
44import com.jogamp.opengl.GLProfile;
45
46import org.junit.Assert;
47import org.junit.Test;
48import org.junit.FixMethodOrder;
49import org.junit.runners.MethodSorters;
50
51@FixMethodOrder(MethodSorters.NAME_ASCENDING)
52public class TestGPUMemSec01NEWT extends UITestCase {
53 static String hexString(final int i) {
54 return "0x"+Integer.toHexString(i);
55 }
56 static String exceptionMsg(final String pre, final int format, final int type, final int components, final int width, final int height, final int rl1, final int rl4, final int rl8) {
57 return pre +
58 ": fmt "+hexString(format)+", type "+hexString(type)+", comps "+components+
59 ", "+width+"x"+height+
60 ", rowlenA1 "+rl1+", rowlenA4 "+rl4+", rowlenA8 "+rl8;
61 }
62
63 static NEWTGLContext.WindowContext createCurrentGLOffscreenWindow(final GLProfile glp, final int width, final int height) throws GLException, InterruptedException {
64 final GLCapabilities caps = new GLCapabilities(glp);
65 caps.setOnscreen(false);
66 final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(
67 caps, width, height, true);
68 final GL gl = winctx.context.getGL();
69
70 // System.err.println("Pre GL Error: 0x"+Integer.toHexString(gl.glGetError()));
71 // System.err.println(winctx.drawable);
72 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
73
74 // misc GL setup
75 gl.glClearColor(1, 1, 1, 1);
77 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
78 gl.glViewport(0, 0, width, height);
80 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
81
82 return winctx;
83 }
84
85 static int readPixelsCheck(final GL gl, final int format, final int type, final int components, final int width, final int height) throws InterruptedException {
86 int expectedExceptions = 0;
87
88 final int rowlenA1 = width * components;
89
90 final int rowlenA4 = ( ( width * components + 3 ) / 4 ) * 4 ;
91 Assert.assertTrue(rowlenA4 % 4 == 0);
92
93 final int rowlenA8 = ( ( width * components + 7 ) / 8 ) * 8 ;
94 Assert.assertTrue(rowlenA8 % 8 == 0);
95
97 psm.setPackAlignment(gl, 1);
98
99 Exception ee = null;
100
101 // ok size !
102 try {
103 final ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1);
104 gl.glReadPixels(0, 0, width, height, format, type, bb);
105 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
106 } catch(final IndexOutOfBoundsException e) {
107 ee = e;
108 }
109 Assert.assertNull(
110 exceptionMsg("Unexpected IndexOutOfBoundsException (size ok, alignment 1)",
111 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
112 ee = null;
113
114
115 // too small -10 !
116 try {
117 final ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1-10);
118 gl.glReadPixels(0, 0, width, height, format, type, bb);
119 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
120 } catch(final IndexOutOfBoundsException e) {
121 ee = e;
122 System.err.println(
123 exceptionMsg("OK Expected IndexOutOfBoundsException (size-10 bytes)",
124 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+
125 ": "+ee.getMessage());
126 expectedExceptions++;
127 }
128 Assert.assertNotNull(
129 exceptionMsg("Expected IndexOutOfBoundsException (size-10 bytes)",
130 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
131 ee = null;
132
133 // too small size/4 !
134 try {
135 final ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1/4);
136 gl.glReadPixels(0, 0, width, height, format, type, bb);
137 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
138 } catch(final IndexOutOfBoundsException e) {
139 ee = e;
140 System.err.println(
141 exceptionMsg("OK Expected IndexOutOfBoundsException (size/4 bytes)",
142 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+
143 ": "+ee.getMessage());
144 expectedExceptions++;
145 }
146 Assert.assertNotNull(
147 exceptionMsg("Expected IndexOutOfBoundsException (size/4 bytes)",
148 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
149 ee = null;
150
151 //
152 // Alignment test
153 //
154 psm.setPackAlignment(gl, 4);
155
156 // ok size !
157 try {
158 final ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA4);
159 gl.glReadPixels(0, 0, width, height, format, type, bb);
160 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
161 } catch(final IndexOutOfBoundsException e) {
162 ee = e;
163 }
164 Assert.assertNull(
165 exceptionMsg("Unexpected IndexOutOfBoundsException (size ok, alignment 4)",
166 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
167 ee = null;
168
169 // too small if rowlenA1%4 > 0
170 try {
171 final ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1);
172 gl.glReadPixels(0, 0, width, height, format, type, bb);
173 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
174 } catch(final IndexOutOfBoundsException e) {
175 ee = e;
176 if(rowlenA1%4>0) {
177 System.err.println(
178 exceptionMsg("OK Expected IndexOutOfBoundsException (alignment 4)",
179 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+
180 ": "+ee.getMessage());
181 expectedExceptions++;
182 }
183 }
184 if(rowlenA1%4>0) {
185 Assert.assertNotNull(
186 exceptionMsg("Expected IndexOutOfBoundsException (alignment 4)",
187 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
188 } else {
189 Assert.assertNull(
190 exceptionMsg("Unexpected IndexOutOfBoundsException (alignment 4)",
191 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
192 }
193 ee = null;
194
195 psm.setPackAlignment(gl, 8);
196
197 // ok size !
198 try {
199 final ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA8);
200 gl.glReadPixels(0, 0, width, height, format, type, bb);
201 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
202 } catch(final IndexOutOfBoundsException e) {
203 ee = e;
204 }
205 Assert.assertNull(
206 exceptionMsg("Unexpected IndexOutOfBoundsException (size ok, alignment 8)",
207 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
208 ee = null;
209
210 // too small if rowlenA1%8 > 0
211 try {
212 final ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1);
213 gl.glReadPixels(0, 0, width, height, format, type, bb);
214 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
215 } catch(final IndexOutOfBoundsException e) {
216 ee = e;
217 if(rowlenA1%8>0) {
218 System.err.println(
219 exceptionMsg("OK Expected IndexOutOfBoundsException (alignment 8)",
220 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+
221 ": "+ee.getMessage());
222 expectedExceptions++;
223 }
224 }
225 if(rowlenA1%8>0) {
226 Assert.assertNotNull(
227 exceptionMsg("Expected IndexOutOfBoundsException (alignment 8)",
228 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
229 } else {
230 Assert.assertNull(
231 exceptionMsg("Unexpected IndexOutOfBoundsException (alignment 8)",
232 format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee);
233 }
234 ee = null;
235
236 psm.restore(gl);
237
238 return expectedExceptions;
239 }
240
241 @Test
242 public void testReadPixelsGL_640x480xRGBAxUB() throws InterruptedException {
243 final GLProfile glp = GLProfile.getDefault();
244 final int width = 640;
245 final int height= 480;
246
247 // preset ..
248 final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(glp, width, height);
249 final GLDrawable drawable = winctx.context.getGLDrawable();
250 final GL gl = winctx.context.getGL();
251
252 // 2 x too small - 0 x alignment
253 Assert.assertEquals(2, readPixelsCheck(gl, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, 4, width, height));
254
255 drawable.swapBuffers();
256 Thread.sleep(50);
257
259 }
260
261 @Test
262 public void testReadPixelsGL_99x100xRGBxUB() throws InterruptedException {
263 final GLProfile glp = GLProfile.getGL2ES2();
264 final int wwidth = 640;
265 final int wheight= 480;
266 final int rwidth = 99;
267 final int rheight= 100;
268
269 // preset ..
270 final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(glp, wwidth, wheight);
271 final GLDrawable drawable = winctx.context.getGLDrawable();
272 final GL gl = winctx.context.getGL();
273
274 // 2 x too small - 1 x alignment
275 Assert.assertEquals(3, readPixelsCheck(gl, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, 4, rwidth, rheight));
276
277 drawable.swapBuffers();
278 Thread.sleep(50);
279
281 }
282
283 @Test
284 public void testReadPixelsGL2GL3_640x480xRGBxUB() throws InterruptedException {
285 final GLProfile glp = GLProfile.getGL2ES2();
286 if(!glp.isGL2ES3()) {
287 System.err.println("GL2ES3 n/a skip test");
288 return;
289 }
290 final int width = 640;
291 final int height= 480;
292
293 // preset ..
294 final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(glp, width, height);
295 final GLDrawable drawable = winctx.context.getGLDrawable();
296 final GL gl = winctx.context.getGL();
297
298 // 2 x too small - 0 x alignment
299 Assert.assertEquals(2, readPixelsCheck(gl, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, 3, width, height));
300
301 drawable.swapBuffers();
302 Thread.sleep(50);
303
305 }
306
307 @Test
308 public void testReadPixelsGL2GL3_99x100xRGBxUB() throws InterruptedException {
309 final GLProfile glp = GLProfile.getGL2ES2();
310 if(!glp.isGL2ES3()) {
311 System.err.println("GL2ES3 n/a skip test");
312 return;
313 }
314 final int wwidth = 640;
315 final int wheight= 480;
316 final int rwidth = 99;
317 final int rheight= 100;
318
319 // preset ..
320 final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(glp, wwidth, wheight);
321 final GLDrawable drawable = winctx.context.getGLDrawable();
322 final GL gl = winctx.context.getGL();
323
324 // 2 x too small - 2 x alignment
325 Assert.assertEquals(4, readPixelsCheck(gl, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, 3, rwidth, rheight));
326
327 drawable.swapBuffers();
328 Thread.sleep(50);
329
331 }
332
333 @Test
334 public void testReadPixelsGL2GL3_640x480xREDxUB() throws InterruptedException {
335 final GLProfile glp = GLProfile.getGL2ES2();
336 if(!glp.isGL2ES3()) {
337 System.err.println("GL2ES3 n/a skip test");
338 return;
339 }
340 final int width = 640;
341 final int height= 480;
342
343 // preset ..
344 final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(glp, width, height);
345 final GLDrawable drawable = winctx.context.getGLDrawable();
346 final GL2GL3 gl = winctx.context.getGL().getGL2GL3();
347
348 // 2 x too small - 0 x alignment
349 Assert.assertEquals(2, readPixelsCheck(gl, GL2ES2.GL_RED, GL.GL_UNSIGNED_BYTE, 1, width, height));
350
351 drawable.swapBuffers();
352 Thread.sleep(50);
353
355 }
356
357 @Test
358 public void testReadPixelsGL2GL3_102x100xREDxUB() throws InterruptedException {
359 final GLProfile glp = GLProfile.getGL2ES2();
360 if(!glp.isGL2ES3()) {
361 System.err.println("GL2ES3 n/a skip test");
362 return;
363 }
364 final int wwidth = 640;
365 final int wheight= 480;
366 final int rwidth = 102;
367 final int rheight= 100;
368
369 // preset ..
370 final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(glp, wwidth, wheight);
371 final GLDrawable drawable = winctx.context.getGLDrawable();
372 final GL2GL3 gl = winctx.context.getGL().getGL2GL3();
373
374 // 2 x too small - 2 x alignment
375 Assert.assertEquals(4, readPixelsCheck(gl, GL2ES2.GL_RED, GL.GL_UNSIGNED_BYTE, 1, rwidth, rheight));
376
377 drawable.swapBuffers();
378 Thread.sleep(50);
379
381 }
382
383 public static void main(final String args[]) throws IOException {
384 final String tstname = TestGPUMemSec01NEWT.class.getName();
385 org.junit.runner.JUnitCore.main(tstname);
386 }
387}
388
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
Specifies a set of OpenGL capabilities.
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 GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
final boolean isGL2ES3()
Indicates whether this profile is capable of GL2ES3.
static WindowContext createWindow(final GLCapabilities caps, final int width, final int height, final boolean debugGL)
static void destroyWindow(final WindowContext winctx)
Utility to safely set and restore the PACK and UNPACK pixel storage mode, regardless of the GLProfile...
final void restore(final GL gl)
Restores PACK and UNPACK pixel storage mode previously saved w/ saveAll(GL) or savePack(GL) and saveU...
final void setPackAlignment(final GL gl, final int packAlignment)
Sets the GL#GL_PACK_ALIGNMENT.
static final int GL_RED
GL_ES_VERSION_3_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_NV_blend_equation_advanced, GL_EXT_texture_rg A...
Definition: GL2ES2.java:596
GL getGL()
Casts this object to the GL interface.
GL2GL3 getGL2GL3()
Casts this object to the GL2GL3 interface.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
void swapBuffers()
Swaps the front and back buffers of this drawable.
void glReadPixels(int x, int y, int width, int height, int format, int type, Buffer pixels)
Entry point to C language function: void {@native glReadPixels}(GLint x, GLint y,...
static final int GL_RGB
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGB" with expression ...
Definition: GL.java:374
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
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_...
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,...
static final int GL_RGBA
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGBA" with expression...
Definition: GL.java:150
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_UNSIGNED_BYTE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_BYTE" with e...
Definition: GL.java:284