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(createVerticesBB(
false),
false );
76 System.err.println(
"Test requires GL2/GL3 profile.");
79 testWriteRead01(createVerticesBB(
true),
false );
85 System.err.println(
"Test requires GL3 or GLES3 profile.");
88 testWriteRead01(createVerticesBB(
false),
true);
93 System.err.println(
"Test requires GL3 or GLES3 profile.");
96 testWriteRead01(createVerticesBB(
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 ByteBuffer createVerticesBB(
final boolean useBuffersAPI) {
102 final ByteBuffer res;
103 if( useBuffersAPI ) {
104 res = Buffers.newDirectByteBuffer(Buffers.SIZEOF_FLOAT*vertexData.length);
105 final FloatBuffer resF = res.asFloatBuffer();
106 resF.put(vertexData, 0, vertexData.length).rewind();
108 res = ByteBuffer.allocate(Buffers.SIZEOF_FLOAT*vertexData.length);
109 res.order(ByteOrder.nativeOrder());
110 for(
int i=0; i<vertexData.length; i++) {
111 res.putFloat(vertexData[i]);
116 System.err.println(
"java "+res);
117 for(
int i=0; i < res.capacity(); i+=4) {
118 System.err.println(
"java ["+i+
"]: "+res.getFloat(i));
124 private void testWriteRead01(
final ByteBuffer verticiesBB,
final boolean useRange)
throws InterruptedException {
126 assertEquals(0, verticiesBB.position());
127 assertEquals(Buffers.SIZEOF_FLOAT*vertexData.length, verticiesBB.limit());
128 assertEquals(Buffers.SIZEOF_FLOAT*vertexData.length, verticiesBB.capacity());
129 assertEquals(Buffers.SIZEOF_FLOAT*vertexData.length, verticiesBB.remaining());
130 assertEquals(-0.3f, verticiesBB.getFloat(Buffers.SIZEOF_FLOAT*0), 0.05f);
131 assertEquals( 0.6f, verticiesBB.getFloat(Buffers.SIZEOF_FLOAT*8), 0.05f);
133 final GLProfile glp = GLProfile.getMaxProgrammable(
true);
134 final GLCapabilities caps =
new GLCapabilities(glp);
135 caps.setOnscreen(
false);
136 final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(
137 caps, 800, 600,
true);
139 final GL gl = winctx.context.getGL();
141 final int[] vertexBuffer =
new int[1];
143 gl.glGenBuffers(1, vertexBuffer, 0);
145 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBuffer[0]);
147 gl.glBufferData(GL.GL_ARRAY_BUFFER, verticiesBB.capacity(), verticiesBB, GL2ES3.GL_STATIC_READ);
150 final int bufferName = gl.getBoundBuffer(GL.GL_ARRAY_BUFFER);
151 final GLBufferStorage bufferStorage = gl.getBufferStorage(bufferName);
152 System.err.println(
"gpu-01 GL_ARRAY_BUFFER -> bufferName "+bufferName+
" -> "+bufferStorage);
153 Assert.assertEquals(
"Buffer storage's bytes-buffer not null before map",
null, bufferStorage.getMappedBuffer());
155 final int floatOffset, byteOffset, mapByteLength;
159 byteOffset = Buffers.SIZEOF_FLOAT*floatOffset;
160 mapByteLength = verticiesBB.capacity()-byteOffset;
161 bb = gl.glMapBufferRange(GL.GL_ARRAY_BUFFER, byteOffset, mapByteLength, GL.GL_MAP_READ_BIT);
165 mapByteLength = verticiesBB.capacity();
166 bb = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL2ES3.GL_READ_ONLY);
168 System.err.println(
"gpu-02 mapped GL_ARRAY_BUFFER, floatOffset "+floatOffset+
", byteOffset "+byteOffset+
", mapByteLength "+mapByteLength+
" -> "+bb);
169 System.err.println(
"gpu-03 GL_ARRAY_BUFFER -> bufferName "+bufferName+
" -> "+bufferStorage);
170 Assert.assertNotNull(bb);
171 Assert.assertEquals(
"BufferStorage size less byteOffset not equals buffer storage size", bufferStorage.getSize()-byteOffset, bb.capacity());
172 Assert.assertEquals(
"BufferStorage's bytes-buffer not equal with mapped bytes-buffer", bufferStorage.getMappedBuffer(), bb);
173 Assert.assertEquals(
"Buffer storage size not equals mapByteLength", mapByteLength, bb.capacity());
176 System.err.println(
"floatOffset "+floatOffset+
", byteOffset "+byteOffset);
177 for(
int i=0; i < bb.capacity(); i+=4) {
178 System.err.println(
"gpu "+i+
": "+bb.getFloat(i));
181 for(
int i=0; i < bb.capacity(); i+=4) {
182 Assert.assertEquals(verticiesBB.getFloat(byteOffset+i), bb.getFloat(i), 0.0001f);
184 gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
185 Assert.assertEquals(
"Buffer storage's bytes-buffer not null after unmap",
null, bufferStorage.getMappedBuffer());
187 NEWTGLContext.destroyWindow(winctx);
190 public static void main(
final String args[])
throws IOException {
192 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.
static void main(final String args[])
void testWriteRead02aMapRange()
void testWriteRead02bMapRange()
void testWriteRead01aMap()
void testWriteRead01bMap()