JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestMapBufferRead02NEWT.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 FloatBuffer and Buffers or NIO API.
57 * </p>
58 *
59 * @author Luz, et.al.
60 */
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62public class TestMapBufferRead02NEWT 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(createVerticesFB(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(createVerticesFB(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(createVerticesFB(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(createVerticesFB(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 FloatBuffer createVerticesFB(final boolean useBuffersAPI) {
102 final FloatBuffer res;
103 if( useBuffersAPI ) {
104 res = Buffers.newDirectFloatBuffer(vertexData);
105 } else {
106 res = FloatBuffer.allocate(vertexData.length);
107 for(int i=0; i<vertexData.length; i++) {
108 res.put(vertexData[i]);
109 }
110 res.rewind();
111 }
112 if(DEBUG) {
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));
116 }
117 }
118 return res;
119 }
120
121 private void testWriteRead01(final FloatBuffer verticiesFB, final boolean useRange) throws InterruptedException {
122 // Validate incoming ByteBuffer first
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);
129
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);
135 try {
136 final GL gl = winctx.context.getGL();
137
138 final int[] vertexBuffer = new int[1];
139
140 gl.glGenBuffers(1, vertexBuffer, 0);
141
142 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBuffer[0]);
143
144 gl.glBufferData(GL.GL_ARRAY_BUFFER, Buffers.SIZEOF_FLOAT*verticiesFB.remaining(), verticiesFB, GL2ES3.GL_STATIC_READ);
145 // gl.glBufferData(GL.GL_ARRAY_BUFFER, Buffers.SIZEOF_FLOAT*verticiesBB.remaining(), verticiesBB, GL.GL_STATIC_DRAW);
146
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());
151
152 final int floatOffset, byteOffset, mapByteLength;
153 final ByteBuffer bb;
154 if( useRange ) {
155 floatOffset = 3;
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);
159 } else {
160 floatOffset = 0;
161 byteOffset = 0;
162 mapByteLength = Buffers.SIZEOF_FLOAT*verticiesFB.remaining();
163 bb = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL2ES3.GL_READ_ONLY);
164 }
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());
171
172 if(DEBUG) {
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));
176 }
177 }
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);
180 }
181 gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
182 Assert.assertEquals("Buffer storage's bytes-buffer not null after unmap", null, bufferStorage.getMappedBuffer());
183 } finally {
184 NEWTGLContext.destroyWindow(winctx);
185 }
186 }
187 public static void main(final String args[]) throws IOException {
188 final String tstname = TestMapBufferRead02NEWT.class.getName();
189 org.junit.runner.JUnitCore.main(tstname);
190 }
191}
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.