JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestMapBufferRead01NEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 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.test.junit.util.NEWTGLContext;
32import com.jogamp.opengl.test.junit.util.UITestCase;
33
34import static org.junit.Assert.assertEquals;
35
36import java.io.IOException;
37import java.nio.ByteBuffer;
38import java.nio.ByteOrder;
39import java.nio.FloatBuffer;
40
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;
46
47import org.junit.Assert;
48import org.junit.Test;
49import org.junit.FixMethodOrder;
50import org.junit.runners.MethodSorters;
51
52/**
53 * Verifies content of buffer storage's content
54 * as well as general buffer- and buffer-storage tracking.
55 * <p>
56 * Implementation uses ByteBuffer and Buffers or NIO API.
57 * </p>
58 *
59 * @author Luz, et.al.
60 */
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62public class TestMapBufferRead01NEWT extends UITestCase {
63 static final boolean DEBUG = false;
64
65 @Test
66 public void testWriteRead01aMap() throws InterruptedException {
68 System.err.println("Test requires GL2/GL3 profile.");
69 return;
70 }
71 testWriteRead01(createVerticesBB(false), false /* useRange */);
72 }
73 @Test
74 public void testWriteRead01bMap() throws InterruptedException {
76 System.err.println("Test requires GL2/GL3 profile.");
77 return;
78 }
79 testWriteRead01(createVerticesBB(true), false /* useRange */);
80 }
81
82 @Test
83 public void testWriteRead02aMapRange() throws InterruptedException {
85 System.err.println("Test requires GL3 or GLES3 profile.");
86 return;
87 }
88 testWriteRead01(createVerticesBB(false), true/* useRange */);
89 }
90 @Test
91 public void testWriteRead02bMapRange() throws InterruptedException {
93 System.err.println("Test requires GL3 or GLES3 profile.");
94 return;
95 }
96 testWriteRead01(createVerticesBB(true), true /* useRange */);
97 }
98
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 };
100
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();
107 } else {
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]);
112 }
113 res.rewind();
114 }
115 if(DEBUG) {
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));
119 }
120 }
121 return res;
122 }
123
124 private void testWriteRead01(final ByteBuffer verticiesBB, final boolean useRange) throws InterruptedException {
125 // Validate incoming ByteBuffer first
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);
132
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);
138 try {
139 final GL gl = winctx.context.getGL();
140
141 final int[] vertexBuffer = new int[1];
142
143 gl.glGenBuffers(1, vertexBuffer, 0);
144
145 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBuffer[0]);
146
147 gl.glBufferData(GL.GL_ARRAY_BUFFER, verticiesBB.capacity(), verticiesBB, GL2ES3.GL_STATIC_READ);
148 // gl.glBufferData(GL.GL_ARRAY_BUFFER, verticiesBB.capacity(), verticiesBB, GL.GL_STATIC_DRAW);
149
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());
154
155 final int floatOffset, byteOffset, mapByteLength;
156 final ByteBuffer bb;
157 if( useRange ) {
158 floatOffset = 3;
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);
162 } else {
163 floatOffset = 0;
164 byteOffset = 0;
165 mapByteLength = verticiesBB.capacity();
166 bb = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL2ES3.GL_READ_ONLY);
167 }
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());
174
175 if(DEBUG) {
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));
179 }
180 }
181 for(int i=0; i < bb.capacity(); i+=4) {
182 Assert.assertEquals(verticiesBB.getFloat(byteOffset+i), bb.getFloat(i), 0.0001f);
183 }
184 gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
185 Assert.assertEquals("Buffer storage's bytes-buffer not null after unmap", null, bufferStorage.getMappedBuffer());
186 } finally {
187 NEWTGLContext.destroyWindow(winctx);
188 }
189 }
190 public static void main(final String args[]) throws IOException {
191 final String tstname = TestMapBufferRead01NEWT.class.getName();
192 org.junit.runner.JUnitCore.main(tstname);
193 }
194}
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static final String GLES3
The embedded OpenGL profile ES 3.x, with x >= 0.
Definition: GLProfile.java:588
static final String GL3
The desktop OpenGL core profile 3.x, with x >= 1.
Definition: GLProfile.java:576
static final String GL2GL3
The intersection of the desktop GL3 and GL2 profile.
Definition: GLProfile.java:597
Verifies content of buffer storage's content as well as general buffer- and buffer-storage tracking.