28package com.jogamp.opengl.test.junit.jogl.acore;
30import com.jogamp.common.nio.Buffers;
31import com.jogamp.opengl.test.junit.util.NEWTGLContext;
32import com.jogamp.opengl.test.junit.util.UITestCase;
34import static org.junit.Assert.assertEquals;
36import java.io.IOException;
37import java.nio.ByteBuffer;
38import java.nio.ByteOrder;
39import java.nio.FloatBuffer;
41import com.jogamp.opengl.GL;
42import com.jogamp.opengl.GL2ES3;
43import com.jogamp.opengl.GLBufferStorage;
44import com.jogamp.opengl.GLCapabilities;
45import com.jogamp.opengl.GLProfile;
47import org.junit.Assert;
49import org.junit.FixMethodOrder;
50import org.junit.runners.MethodSorters;
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
63 static final boolean DEBUG =
false;
68 System.err.println(
"Test requires GL2/GL3 profile.");
71 testWriteRead01(createVerticesFB(
false),
false );
76 System.err.println(
"Test requires GL2/GL3 profile.");
79 testWriteRead01(createVerticesFB(
true),
false );
85 System.err.println(
"Test requires GL3 or GLES3 profile.");
88 testWriteRead01(createVerticesFB(
false),
true);
93 System.err.println(
"Test requires GL3 or GLES3 profile.");
96 testWriteRead01(createVerticesFB(
true),
true );
99 static final float[] vertexData =
new float[] { -0.3f, -0.2f, -0.1f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f };
101 static FloatBuffer createVerticesFB(
final boolean useBuffersAPI) {
102 final FloatBuffer res;
103 if( useBuffersAPI ) {
104 res = Buffers.newDirectFloatBuffer(vertexData);
106 res = FloatBuffer.allocate(vertexData.length);
107 for(
int i=0; i<vertexData.length; i++) {
108 res.put(vertexData[i]);
113 System.err.println(
"java "+res);
114 for(
int i=0; i < res.remaining(); i+=4) {
115 System.err.println(
"java ["+i+
"]: "+res.get(i));
121 private void testWriteRead01(
final FloatBuffer verticiesFB,
final boolean useRange)
throws InterruptedException {
123 assertEquals(0, verticiesFB.position());
124 assertEquals(vertexData.length, verticiesFB.limit());
125 assertEquals(vertexData.length, verticiesFB.capacity());
126 assertEquals(vertexData.length, verticiesFB.remaining());
127 assertEquals(-0.3f, verticiesFB.get(0), 0.05f);
128 assertEquals( 0.6f, verticiesFB.get(8), 0.05f);
130 final GLProfile glp = GLProfile.getMaxProgrammable(
true);
131 final GLCapabilities caps =
new GLCapabilities(glp);
132 caps.setOnscreen(
false);
133 final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(
134 caps, 800, 600,
true);
136 final GL gl = winctx.context.getGL();
138 final int[] vertexBuffer =
new int[1];
140 gl.glGenBuffers(1, vertexBuffer, 0);
142 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBuffer[0]);
144 gl.glBufferData(GL.GL_ARRAY_BUFFER, Buffers.SIZEOF_FLOAT*verticiesFB.remaining(), verticiesFB, GL2ES3.GL_STATIC_READ);
147 final int bufferName = gl.getBoundBuffer(GL.GL_ARRAY_BUFFER);
148 final GLBufferStorage bufferStorage = gl.getBufferStorage(bufferName);
149 System.err.println(
"gpu-01 GL_ARRAY_BUFFER -> bufferName "+bufferName+
" -> "+bufferStorage);
150 Assert.assertEquals(
"Buffer storage's bytes-buffer not null before map",
null, bufferStorage.getMappedBuffer());
152 final int floatOffset, byteOffset, mapByteLength;
156 byteOffset = Buffers.SIZEOF_FLOAT*floatOffset;
157 mapByteLength = Buffers.SIZEOF_FLOAT*verticiesFB.remaining()-byteOffset;
158 bb = gl.glMapBufferRange(GL.GL_ARRAY_BUFFER, byteOffset, mapByteLength, GL.GL_MAP_READ_BIT);
162 mapByteLength = Buffers.SIZEOF_FLOAT*verticiesFB.remaining();
163 bb = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL2ES3.GL_READ_ONLY);
165 System.err.println(
"gpu-02 mapped GL_ARRAY_BUFFER, floatOffset "+floatOffset+
", byteOffset "+byteOffset+
", mapByteLength "+mapByteLength+
" -> "+bb);
166 System.err.println(
"gpu-03 GL_ARRAY_BUFFER -> bufferName "+bufferName+
" -> "+bufferStorage);
167 Assert.assertNotNull(bb);
168 Assert.assertEquals(
"BufferStorage size less byteOffset not equals buffer storage size", bufferStorage.getSize()-byteOffset, bb.capacity());
169 Assert.assertEquals(
"BufferStorage's bytes-buffer not equal with mapped bytes-buffer", bufferStorage.getMappedBuffer(), bb);
170 Assert.assertEquals(
"Buffer storage size not equals mapByteLength", mapByteLength, bb.remaining());
173 System.err.println(
"floatOffset "+floatOffset+
", byteOffset "+byteOffset);
174 for(
int i=0; i < bb.remaining(); i+=4) {
175 System.err.println(
"gpu "+i+
": "+bb.getFloat(i));
178 for(
int i=0; i < bb.remaining(); i+=4) {
179 Assert.assertEquals(verticiesFB.get( (byteOffset+i) / Buffers.SIZEOF_FLOAT ), bb.getFloat(i), 0.0001f);
181 gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
182 Assert.assertEquals(
"Buffer storage's bytes-buffer not null after unmap",
null, bufferStorage.getMappedBuffer());
184 NEWTGLContext.destroyWindow(winctx);
187 public static void main(
final String args[])
throws IOException {
189 org.junit.runner.JUnitCore.
main(tstname);
Specifies the the OpenGL profile.
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
static final String GLES3
The embedded OpenGL profile ES 3.x, with x >= 0.
static final String GL3
The desktop OpenGL core profile 3.x, with x >= 1.
static final String GL2GL3
The intersection of the desktop GL3 and GL2 profile.
Verifies content of buffer storage's content as well as general buffer- and buffer-storage tracking.
void testWriteRead01aMap()
void testWriteRead02aMapRange()
void testWriteRead01bMap()
static void main(final String args[])
void testWriteRead02bMapRange()