JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
GLArrayDataServer.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 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 */
28
29package com.jogamp.opengl.util;
30
31import java.nio.Buffer;
32import java.nio.ByteBuffer;
33import java.nio.FloatBuffer;
34import java.nio.IntBuffer;
35import java.nio.ShortBuffer;
36
37import com.jogamp.opengl.GL;
38import com.jogamp.opengl.GL2ES2;
39import com.jogamp.opengl.GLArrayData;
40import com.jogamp.opengl.GLBufferStorage;
41import com.jogamp.opengl.GLException;
42import com.jogamp.opengl.fixedfunc.GLPointerFuncUtil;
43
44import com.jogamp.common.nio.Buffers;
45
46import jogamp.opengl.util.GLArrayHandler;
47import jogamp.opengl.util.GLArrayHandlerInterleaved;
48import jogamp.opengl.util.GLDataArrayHandler;
49import jogamp.opengl.util.GLFixedArrayHandler;
50import jogamp.opengl.util.GLFixedArrayHandlerFlat;
51import jogamp.opengl.util.glsl.GLSLArrayHandler;
52import jogamp.opengl.util.glsl.GLSLArrayHandlerFlat;
53import jogamp.opengl.util.glsl.GLSLArrayHandlerInterleaved;
54
55
57
58 //
59 // lifetime matters
60 //
61
62 /**
63 * Create a VBO, using a predefined fixed function array index
64 * and starting with a given Buffer object incl it's stride
65 *
66 * On profiles GL2 and ES1 the fixed function pipeline behavior is as expected.
67 * On profile ES2 the fixed function emulation will transform these calls to
68 * EnableVertexAttribArray and VertexAttribPointer calls,
69 * and a predefined vertex attribute variable name will be chosen.
70 *
71 * The default name mapping will be used,
72 * see {@link GLPointerFuncUtil#getPredefinedArrayIndexName(int)}.
73 *
74 * @param index The GL array index
75 * @param compsPerElement component count per element
76 * @param dataType The component's OpenGL data type
77 * @param normalized Whether the data shall be normalized
78 * @param stride in bytes from one element to the other. If zero, compsPerElement * compSizeInBytes
79 * @param buffer the user define data
80 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
81 *
82 * @see com.jogamp.opengl.GLContext#getPredefinedArrayIndexName(int)
83 */
84 public static GLArrayDataServer createFixed(final int index, final int compsPerElement, final int dataType, final boolean normalized, final int stride,
85 final Buffer buffer, final int vboUsage)
86 throws GLException
87 {
88 return new GLArrayDataServer(null, index, compsPerElement, dataType, normalized, stride, buffer, buffer.limit(), DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
89 false, GLFixedArrayHandler.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, false);
90 }
91
92 /**
93 * Create a VBO, using a predefined fixed function array index
94 * and starting with a new created Buffer object with initialElementCount size
95 *
96 * On profiles GL2 and ES1 the fixed function pipeline behavior is as expected.
97 * On profile ES2 the fixed function emulation will transform these calls to
98 * EnableVertexAttribArray and VertexAttribPointer calls,
99 * and a predefined vertex attribute variable name will be chosen.
100 *
101 * The default name mapping will be used,
102 * see {@link GLPointerFuncUtil#getPredefinedArrayIndexName(int)}.
103 *
104 * @param index The GL array index
105 * @param compsPerElement component count per element
106 * @param dataType The component's OpenGL data type
107 * @param normalized Whether the data shall be normalized
108 * @param initialElementCount
109 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
110 *
111 * @see com.jogamp.opengl.GLContext#getPredefinedArrayIndexName(int)
112 */
113 public static GLArrayDataServer createFixed(final int index, final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount,
114 final int vboUsage)
115 throws GLException
116 {
117 return new GLArrayDataServer(null, index, compsPerElement, dataType, normalized, 0, null, initialElementCount, DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
118 false, GLFixedArrayHandler.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, false);
119 }
120
121 /**
122 * Create a VBO, using a custom GLSL array attribute name
123 * and starting with a new created Buffer object with initialElementCount size
124 * @param name The custom name for the GL attribute
125 * @param compsPerElement component count per element
126 * @param dataType The component's OpenGL data type
127 * @param normalized Whether the data shall be normalized
128 * @param initialElementCount
129 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
130 */
131 public static GLArrayDataServer createGLSL(final String name, final int compsPerElement,
132 final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
133 throws GLException
134 {
135 return new GLArrayDataServer(name, -1, compsPerElement, dataType, normalized, 0, null, initialElementCount,
136 DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */, true, GLSLArrayHandler.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, true);
137 }
138
139 /**
140 * Create a VBO, using a custom GLSL array attribute name
141 * intended for GPU buffer storage mapping, see {@link GLBufferStorage}, via {@link #mapStorage(GL, int)} and {@link #mapStorage(GL, long, long, int)}.
142 * @param name The custom name for the GL attribute
143 * @param compsPerElement component count per element
144 * @param dataType The component's OpenGL data type
145 * @param normalized Whether the data shall be normalized
146 * @param mappedElementCount
147 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
148 */
149 public static GLArrayDataServer createGLSLMapped(final String name, final int compsPerElement,
150 final int dataType, final boolean normalized, final int mappedElementCount, final int vboUsage)
151 throws GLException
152 {
153 final GLArrayDataServer ads = new GLArrayDataServer(name, -1, compsPerElement, dataType, normalized, 0, null, 0 /* initialElementCount */,
154 DEFAULT_GROWTH_FACTOR, mappedElementCount, true, GLSLArrayHandler.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, true);
155 ads.seal(true);
156 return ads;
157 }
158
159 /**
160 * Create a VBO, using a custom GLSL array attribute name
161 * and starting with a given Buffer object incl it's stride
162 * @param name The custom name for the GL attribute
163 * @param compsPerElement component count per element
164 * @param dataType The component's OpenGL data type
165 * @param normalized Whether the data shall be normalized
166 * @param stride in bytes from one element to the other. If zero, compsPerElement * compSizeInBytes
167 * @param buffer the user define data
168 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
169 */
170 public static GLArrayDataServer createGLSL(final String name, final int compsPerElement,
171 final int dataType, final boolean normalized, final int stride, final Buffer buffer,
172 final int vboUsage)
173 throws GLException
174 {
175 return new GLArrayDataServer(name, -1, compsPerElement, dataType, normalized, stride, buffer, buffer.limit(), DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
176 true, GLSLArrayHandler.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, true);
177 }
178
179 /**
180 * Create a VBO data object for any target w/o render pipeline association, ie {@link GL#GL_ELEMENT_ARRAY_BUFFER}.
181 *
182 * Hence no index, name for a fixed function pipeline nor vertex attribute is given.
183 *
184 * @param compsPerElement component count per element
185 * @param dataType The component's OpenGL data type
186 * @param stride in bytes from one element to the other. If zero, compsPerElement * compSizeInBytes
187 * @param buffer the user define data
188 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
189 * @param vboTarget {@link GL#GL_ELEMENT_ARRAY_BUFFER}, ..
190 * {@link GL#glGenBuffers(int, int[], int)
191 */
192 public static GLArrayDataServer createData(final int compsPerElement, final int dataType, final int stride,
193 final Buffer buffer, final int vboUsage, final int vboTarget)
194 throws GLException
195 {
196 return new GLArrayDataServer(null, -1, compsPerElement, dataType, false, stride, buffer, buffer.limit(), DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
197 false, GLDataArrayHandler.class, 0, 0, vboUsage, vboTarget, false);
198 }
199
200 /**
201 * Create a VBO data object for any target w/o render pipeline association, ie {@link GL#GL_ELEMENT_ARRAY_BUFFER}.
202 *
203 * Hence no index, name for a fixed function pipeline nor vertex attribute is given.
204 *
205 * @param compsPerElement component count per element
206 * @param dataType The component's OpenGL data type
207 * @param initialElementCount
208 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
209 * @param vboTarget {@link GL#GL_ELEMENT_ARRAY_BUFFER}, ..
210 */
211 public static GLArrayDataServer createData(final int compsPerElement, final int dataType, final int initialElementCount,
212 final int vboUsage, final int vboTarget)
213 throws GLException
214 {
215 return new GLArrayDataServer(null, -1, compsPerElement, dataType, false, 0, null, initialElementCount, DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
216 false, GLDataArrayHandler.class, 0, 0, vboUsage, vboTarget, false);
217 }
218
219 /**
220 * Create a VBO data object for any target w/o render pipeline association, i.e. {@link GL#GL_ELEMENT_ARRAY_BUFFER},
221 * intended for GPU buffer storage mapping, see {@link GLBufferStorage}, via {@link #mapStorage(GL, int)} and {@link #mapStorage(GL, long, long, int)}.
222 * <p>
223 * No index, name for a fixed function pipeline nor vertex attribute is given.
224 * </p>
225 *
226 * @param compsPerElement component count per element
227 * @param dataType The component's OpenGL data type
228 * @param mappedElementCount
229 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
230 * @param vboTarget {@link GL#GL_ELEMENT_ARRAY_BUFFER}, ..
231 */
232 public static GLArrayDataServer createDataMapped(final int compsPerElement, final int dataType, final int mappedElementCount,
233 final int vboUsage, final int vboTarget)
234 throws GLException
235 {
236 return new GLArrayDataServer(null, -1, compsPerElement, dataType, false, 0, null, 0 /* initialElementCount */, DEFAULT_GROWTH_FACTOR, mappedElementCount,
237 false, GLDataArrayHandler.class, 0, 0, vboUsage, vboTarget, false);
238 }
239
240 /**
241 * Create a VBO for fixed function interleaved array data
242 * starting with a new created Buffer object with initialElementCount size.
243 * <p>User needs to <i>configure</i> the interleaved segments via {@link #addFixedSubArray(int, int, int)}.</p>
244 *
245 * @param compsPerElement The total number of all interleaved components per element.
246 * @param dataType The component's OpenGL data type
247 * @param normalized Whether the data shall be normalized
248 * @param initialElementCount The initial number of all interleaved elements
249 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
250 */
251 public static GLArrayDataServer createFixedInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount,
252 final int vboUsage)
253 throws GLException
254 {
255 return new GLArrayDataServer(GLPointerFuncUtil.mgl_InterleaveArray, -1, compsPerElement, dataType, false, 0, null, initialElementCount, DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
256 false, GLArrayHandlerInterleaved.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, false);
257 }
258
259 /**
260 * Create a VBO for fixed function interleaved array data
261 * intended for GPU buffer storage mapping, see {@link GLBufferStorage}, via {@link #mapStorage(GL, int)} and {@link #mapStorage(GL, long, long, int)}.
262 * <p>User needs to <i>configure</i> the interleaved segments via {@link #addFixedSubArray(int, int, int)}.</p>
263 *
264 * @param compsPerElement The total number of all interleaved components per element.
265 * @param dataType The component's OpenGL data type
266 * @param normalized Whether the data shall be normalized
267 * @param mappedElementCount The total number of all interleaved elements
268 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
269 */
270 public static GLArrayDataServer createFixedInterleavedMapped(final int compsPerElement, final int dataType, final boolean normalized, final int mappedElementCount,
271 final int vboUsage)
272 throws GLException
273 {
274 final GLArrayDataServer ads = new GLArrayDataServer(GLPointerFuncUtil.mgl_InterleaveArray, -1, compsPerElement, dataType, false, 0, null, 0 /* initialElementCount */, DEFAULT_GROWTH_FACTOR, mappedElementCount,
275 false, GLArrayHandlerInterleaved.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, false);
276 ads.seal(true);
277 return ads;
278 }
279
280 /**
281 * Create a VBO for fixed function interleaved array data
282 * starting with a given Buffer object incl it's stride
283 * <p>User needs to <i>configure</i> the interleaved segments via {@link #addFixedSubArray(int, int, int)}.</p>
284 *
285 * @param compsPerElement The total number of all interleaved components per element.
286 * @param dataType The component's OpenGL data type
287 * @param normalized Whether the data shall be normalized
288 * @param stride in bytes from one element of a sub-array to the other. If zero, compsPerElement * compSizeInBytes
289 * @param buffer The user define data of all interleaved elements
290 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
291 */
292 public static GLArrayDataServer createFixedInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int stride, final Buffer buffer,
293 final int vboUsage)
294 throws GLException
295 {
296 return new GLArrayDataServer(GLPointerFuncUtil.mgl_InterleaveArray, -1, compsPerElement, dataType, normalized, stride, buffer, buffer.limit(), DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
297 false, GLArrayHandlerInterleaved.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, false);
298 }
299
300 /**
301 * Configure a segment of this fixed function interleaved array (see {@link #createFixedInterleaved(int, int, boolean, int, int)}).
302 * <p>
303 * This method may be called several times as long the sum of interleaved components does not
304 * exceed the total component count of the created interleaved array.</p>
305 * <p>
306 * The memory of the the interleaved array is being used.</p>
307 * <p>
308 * Must be called before using the array, eg: {@link #seal(boolean)}, {@link #putf(float)}, .. </p>
309 *
310 * @param index The GL array index, maybe -1 if vboTarget is {@link GL#GL_ELEMENT_ARRAY_BUFFER}
311 * @param comps This interleaved array segment's component count per element
312 * @param vboTarget {@link GL#GL_ARRAY_BUFFER} or {@link GL#GL_ELEMENT_ARRAY_BUFFER}
313 */
314 public GLArrayData addFixedSubArray(final int index, final int comps, final int vboTarget) {
315 if(interleavedOffset >= getCompsPerElem() * getBytesPerComp()) {
316 final int iOffC = interleavedOffset / getBytesPerComp();
317 throw new GLException("Interleaved offset > total components ("+iOffC+" > "+getCompsPerElem()+")");
318 }
319 if(usesGLSL) {
320 throw new GLException("buffer uses GLSL");
321 }
322 final int subStrideB = ( 0 == getStride() ) ? getCompsPerElem() * getBytesPerComp() : getStride();
323 final GLArrayDataWrapper ad;
324 if( 0 < mappedElemCount ) {
326 index, comps, getCompType(),
327 getNormalized(), subStrideB, mappedElemCount,
328 getVBOName(), interleavedOffset, getVBOUsage(), vboTarget);
329 } else {
331 index, comps, getCompType(),
332 getNormalized(), subStrideB, getBuffer(),
333 getVBOName(), interleavedOffset, getVBOUsage(), vboTarget);
334 }
335 ad.setVBOEnabled(isVBO());
336 interleavedOffset += comps * getBytesPerComp();
338 glArrayHandler.addSubHandler(new GLFixedArrayHandlerFlat(ad));
339 }
340 return ad;
341 }
342
343 /**
344 * Create a VBO for GLSL interleaved array data
345 * starting with a new created Buffer object with initialElementCount size.
346 * <p>User needs to <i>configure</i> the interleaved segments via {@link #addGLSLSubArray(int, int, int)}.</p>
347 *
348 * @param compsPerElement The total number of all interleaved components per element.
349 * @param dataType The component's OpenGL data type
350 * @param normalized Whether the data shall be normalized
351 * @param initialElementCount The initial number of all interleaved elements
352 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
353 */
354 public static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount,
355 final int vboUsage)
356 throws GLException
357 {
358 return new GLArrayDataServer(GLPointerFuncUtil.mgl_InterleaveArray, -1, compsPerElement, dataType, normalized, 0, null, initialElementCount, DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
359 false, GLSLArrayHandlerInterleaved.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, true);
360 }
361
362 /**
363 * Create a VBO for GLSL interleaved array data
364 * intended for GPU buffer storage mapping, see {@link GLBufferStorage}, via {@link #mapStorage(GL, int)} and {@link #mapStorage(GL, long, long, int)}.
365 * <p>User needs to <i>configure</i> the interleaved segments via {@link #addGLSLSubArray(int, int, int)}.</p>
366 *
367 * @param compsPerElement The total number of all interleaved components per element.
368 * @param dataType The component's OpenGL data type
369 * @param normalized Whether the data shall be normalized
370 * @param mappedElementCount The total number of all interleaved elements
371 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
372 */
373 public static GLArrayDataServer createGLSLInterleavedMapped(final int compsPerElement, final int dataType, final boolean normalized, final int mappedElementCount, final int vboUsage)
374 throws GLException
375 {
376 final GLArrayDataServer ads = new GLArrayDataServer(GLPointerFuncUtil.mgl_InterleaveArray, -1, compsPerElement, dataType, normalized, 0, null, 0 /* initialElementCount */, DEFAULT_GROWTH_FACTOR, mappedElementCount,
377 false, GLSLArrayHandlerInterleaved.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, true);
378 ads.seal(true);
379 return ads;
380 }
381
382 /**
383 * Create a VBO for GLSL interleaved array data
384 * starting with a given Buffer object incl it's stride
385 * <p>User needs to <i>configure</i> the interleaved segments via {@link #addGLSLSubArray(int, int, int)}.</p>
386 *
387 * @param compsPerElement The total number of all interleaved components per element.
388 * @param dataType The component's OpenGL data type
389 * @param normalized Whether the data shall be normalized
390 * @param stride in bytes from one element of a sub-array to the other. If zero, compsPerElement * compSizeInBytes
391 * @param buffer The user define data of all interleaved elements
392 * @param vboUsage {@link GL2ES2#GL_STREAM_DRAW}, {@link GL#GL_STATIC_DRAW} or {@link GL#GL_DYNAMIC_DRAW}
393 */
394 public static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int stride, final Buffer buffer,
395 final int vboUsage)
396 throws GLException
397 {
398 return new GLArrayDataServer(GLPointerFuncUtil.mgl_InterleaveArray, -1, compsPerElement, dataType, normalized, stride, buffer, buffer.limit(), DEFAULT_GROWTH_FACTOR, 0 /* mappedElementCount */,
399 false, GLSLArrayHandlerInterleaved.class, 0, 0, vboUsage, GL.GL_ARRAY_BUFFER, true);
400 }
401
402 /**
403 * Configure a segment of this GLSL interleaved array (see {@link #createGLSLInterleaved(int, int, boolean, int, int)}).
404 * <p>
405 * This method may be called several times as long the sum of interleaved components does not
406 * exceed the total component count of the created interleaved array.</p>
407 * <p>
408 * The memory of the the interleaved array is being used.</p>
409 * <p>
410 * Must be called before using the array, eg: {@link #seal(boolean)}, {@link #putf(float)}, .. </p>
411 * @param name The custom name for the GL attribute, maybe null if vboTarget is {@link GL#GL_ELEMENT_ARRAY_BUFFER}
412 * @param comps This interleaved array segment's component count per element
413 * @param vboTarget {@link GL#GL_ARRAY_BUFFER} or {@link GL#GL_ELEMENT_ARRAY_BUFFER}
414 */
415 public GLArrayDataWrapper addGLSLSubArray(final String name, final int comps, final int vboTarget) {
416 if(interleavedOffset >= getCompsPerElem() * getBytesPerComp()) {
417 final int iOffC = interleavedOffset / getBytesPerComp();
418 throw new GLException("Interleaved offset > total components ("+iOffC+" > "+getCompsPerElem()+")");
419 }
420 if(!usesGLSL) {
421 throw new GLException("buffer uses fixed function");
422 }
423 final int subStrideB = ( 0 == getStride() ) ? getCompsPerElem() * getBytesPerComp() : getStride();
424 final GLArrayDataWrapper ad;
425 if( 0 < mappedElemCount ) {
427 name, comps, getCompType(),
428 getNormalized(), subStrideB, mappedElemCount,
429 getVBOName(), interleavedOffset, getVBOUsage(), vboTarget);
430 } else {
432 name, comps, getCompType(),
433 getNormalized(), subStrideB, getBuffer(),
434 getVBOName(), interleavedOffset, getVBOUsage(), vboTarget);
435 }
436 ad.setVBOEnabled(isVBO());
437 interleavedOffset += comps * getBytesPerComp();
439 glArrayHandler.addSubHandler(new GLSLArrayHandlerFlat(ad));
440 }
441 return ad;
442 }
443
444 public final void setInterleavedOffset(final int interleavedOffset) {
445 this.interleavedOffset = interleavedOffset;
446 }
447
448 public final int getInterleavedOffset() {
449 return interleavedOffset;
450 }
451
452 //
453 // Data matters GLArrayData
454 //
455
456 //
457 // Data and GL state modification ..
458 //
459
460 @Override
461 public void destroy(final GL gl) {
462 // super.destroy(gl):
463 // - GLArrayDataClient.destroy(gl): disables & clears client-side buffer
464 // - GLArrayDataWrapper.destroy(gl) (clears all values 'vboName' ..)
465 final int _vboName = vboName;
466 super.destroy(gl);
467 if(_vboName!=0) {
468 final int[] tmp = new int[] { _vboName } ;
469 gl.glDeleteBuffers(1, tmp, 0);
470 vboName = 0;
471 }
472 }
473
474 //
475 // data matters
476 //
477
478 /**
479 * Convenient way do disable the VBO behavior and
480 * switch to client side data one
481 * Only possible if buffer is defined.
482 */
483 @Override
484 public void setVBOEnabled(final boolean vboUsage) {
485 checkSeal(false);
486 super.setVBOEnabled(vboUsage);
487 }
488
489 public GLBufferStorage mapStorage(final GL gl, final int access) {
490 if( null != this.getBuffer() ) {
491 throw new IllegalStateException("user buffer not null");
492 }
493 if( null != mappedStorage ) {
494 throw new IllegalStateException("already mapped: "+mappedStorage);
495 }
496 checkSeal(true);
497 bindBuffer(gl, true);
499 final GLBufferStorage storage = gl.mapBuffer(getVBOTarget(), access);
500 setMappedBuffer(storage);
501 bindBuffer(gl, false);
502 seal(false);
503 rewind();
504 return storage;
505 }
506 public GLBufferStorage mapStorage(final GL gl, final long offset, final long length, final int access) {
507 if( null != this.getBuffer() ) {
508 throw new IllegalStateException("user buffer not null");
509 }
510 if( null != mappedStorage ) {
511 throw new IllegalStateException("already mapped: "+mappedStorage);
512 }
513 checkSeal(true);
514 bindBuffer(gl, true);
516 final GLBufferStorage storage = gl.mapBufferRange(getVBOTarget(), offset, length, access);
517 setMappedBuffer(storage);
518 bindBuffer(gl, false);
519 seal(false);
520 rewind();
521 return storage;
522 }
523 private final void setMappedBuffer(final GLBufferStorage storage) {
524 mappedStorage = storage;
525 final ByteBuffer bb = storage.getMappedBuffer();
526 if(compClazz==ByteBuffer.class) {
527 buffer = bb;
528 } else if(compClazz==ShortBuffer.class) {
529 buffer = bb.asShortBuffer();
530 } else if(compClazz==IntBuffer.class) {
531 buffer = bb.asIntBuffer();
532 } else if(compClazz==FloatBuffer.class) {
533 buffer = bb.asFloatBuffer();
534 } else {
535 throw new GLException("Given Buffer Class not supported: "+compClazz+":\n\t"+this);
536 }
537 }
538
539 public void unmapStorage(final GL gl) {
540 if( null == mappedStorage ) {
541 throw new IllegalStateException("not mapped");
542 }
543 mappedStorage = null;
544 buffer = null;
545 seal(true);
546 bindBuffer(gl, true);
548 bindBuffer(gl, false);
549 }
550
551 @Override
552 public String toString() {
553 return "GLArrayDataServer["+name+
554 ", index "+index+
555 ", location "+location+
556 ", isVertexAttribute "+isVertexAttr+
557 ", usesGLSL "+usesGLSL+
558 ", usesShaderState "+(null!=shaderState)+
559 ", dataType 0x"+Integer.toHexString(compType)+
560 ", bufferClazz "+compClazz+
561 ", compsPerElem "+compsPerElement+
562 ", stride "+strideB+"b "+strideL+"c"+
563 ", initElemCount "+initElemCount+
564 ", mappedElemCount "+mappedElemCount+
565 ", "+elemStatsToString()+
566 ", mappedStorage "+mappedStorage+
567 ", vboEnabled "+vboEnabled+
568 ", vboName "+vboName+
569 ", vboUsage 0x"+Integer.toHexString(vboUsage)+
570 ", vboTarget 0x"+Integer.toHexString(vboTarget)+
571 ", vboOffset "+vboOffset+
572 ", bufferEnabled "+bufferEnabled+
573 ", bufferWritten "+bufferWritten+
574 ", buffer "+buffer+
575 ", alive "+alive+
576 "]";
577 }
578
579 //
580 // non public matters ..
581 //
582
583 protected GLArrayDataServer(final String name, final int index, final int comps, final int dataType, final boolean normalized,
584 final int stride, final Buffer data, final int initialElementCount, final float growthFactor,
585 final int mappedElementCount, final boolean isVertexAttribute,
586 final Class<? extends GLArrayHandler> handlerClass, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget, final boolean usesGLSL)
587 throws GLException
588 {
589 super(name, index, comps, dataType, normalized, stride, data, initialElementCount, growthFactor, mappedElementCount,
591
592 vboEnabled=true;
593 }
594
595 @Override
596 protected void init_vbo(final GL gl) {
597 super.init_vbo(gl);
598 if(vboEnabled && vboName==0) {
599 final int[] tmp = new int[1];
600 gl.glGenBuffers(1, tmp, 0);
601 vboName = tmp[0];
602 if(0 < interleavedOffset) {
603 glArrayHandler.setSubArrayVBOName(vboName);
604 }
605 }
606 }
607
608 /**
609 * Copy Constructor
610 * <p>
611 * Buffer is {@link Buffers#slice(Buffer) sliced}, i.e. sharing content but using own state.
612 * </p>
613 * <p>
614 * All other values are simply copied.
615 * </p>
616 */
618 super(src);
619 this.interleavedOffset = src.interleavedOffset;
620 this.mappedStorage = src.mappedStorage;
621 }
622
623 private int interleavedOffset = 0;
624 private GLBufferStorage mappedStorage = null;
625}
626
OpenGL buffer storage object reflecting it's.
final ByteBuffer getMappedBuffer()
Returns the mapped ByteBuffer, or null if not mapped.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
final void checkSeal(final boolean test)
static final float DEFAULT_GROWTH_FACTOR
Default growth factor using the golden ratio 1.618.
void seal(final GL gl, final boolean seal)
Convenience method calling seal(boolean) and enableBuffer(GL, boolean).
boolean bindBuffer(final GL gl, final boolean bind)
if bind is true and the data uses VBO, the latter will be bound and data written to the GPU if requir...
static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboUsage)
Create a VBO for GLSL interleaved array data starting with a given Buffer object incl it's stride.
final void setInterleavedOffset(final int interleavedOffset)
static GLArrayDataServer createFixedInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboUsage)
Create a VBO for fixed function interleaved array data starting with a given Buffer object incl it's ...
static GLArrayDataServer createFixedInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO for fixed function interleaved array data starting with a new created Buffer object with...
static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO for GLSL interleaved array data starting with a new created Buffer object with initialEl...
static GLArrayDataServer createGLSL(final String name, final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO, using a custom GLSL array attribute name and starting with a new created Buffer object ...
static GLArrayDataServer createGLSLMapped(final String name, final int compsPerElement, final int dataType, final boolean normalized, final int mappedElementCount, final int vboUsage)
Create a VBO, using a custom GLSL array attribute name intended for GPU buffer storage mapping,...
GLBufferStorage mapStorage(final GL gl, final int access)
static GLArrayDataServer createData(final int compsPerElement, final int dataType, final int stride, final Buffer buffer, final int vboUsage, final int vboTarget)
Create a VBO data object for any target w/o render pipeline association, ie GL#GL_ELEMENT_ARRAY_BUFFE...
static GLArrayDataServer createFixed(final int index, final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO, using a predefined fixed function array index and starting with a new created Buffer ob...
GLArrayDataServer(final String name, final int index, final int comps, final int dataType, final boolean normalized, final int stride, final Buffer data, final int initialElementCount, final float growthFactor, final int mappedElementCount, final boolean isVertexAttribute, final Class<? extends GLArrayHandler > handlerClass, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget, final boolean usesGLSL)
static GLArrayDataServer createGLSL(final String name, final int compsPerElement, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboUsage)
Create a VBO, using a custom GLSL array attribute name and starting with a given Buffer object incl i...
GLArrayData addFixedSubArray(final int index, final int comps, final int vboTarget)
Configure a segment of this fixed function interleaved array (see createFixedInterleaved(int,...
GLArrayDataWrapper addGLSLSubArray(final String name, final int comps, final int vboTarget)
Configure a segment of this GLSL interleaved array (see createGLSLInterleaved(int,...
static GLArrayDataServer createData(final int compsPerElement, final int dataType, final int initialElementCount, final int vboUsage, final int vboTarget)
Create a VBO data object for any target w/o render pipeline association, ie GL#GL_ELEMENT_ARRAY_BUFFE...
void setVBOEnabled(final boolean vboUsage)
Convenient way do disable the VBO behavior and switch to client side data one Only possible if buffer...
static GLArrayDataServer createGLSLInterleavedMapped(final int compsPerElement, final int dataType, final boolean normalized, final int mappedElementCount, final int vboUsage)
Create a VBO for GLSL interleaved array data intended for GPU buffer storage mapping,...
static GLArrayDataServer createFixed(final int index, final int compsPerElement, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboUsage)
Create a VBO, using a predefined fixed function array index and starting with a given Buffer object i...
static GLArrayDataServer createFixedInterleavedMapped(final int compsPerElement, final int dataType, final boolean normalized, final int mappedElementCount, final int vboUsage)
Create a VBO for fixed function interleaved array data intended for GPU buffer storage mapping,...
static GLArrayDataServer createDataMapped(final int compsPerElement, final int dataType, final int mappedElementCount, final int vboUsage, final int vboTarget)
Create a VBO data object for any target w/o render pipeline association, i.e.
GLArrayDataServer(final GLArrayDataServer src)
Copy Constructor.
GLBufferStorage mapStorage(final GL gl, final long offset, final long length, final int access)
static GLArrayDataWrapper createFixed(final int index, final int comps, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
Create a VBO, using a predefined fixed function array index, wrapping the given data.
static GLArrayDataWrapper createGLSL(final String name, final int comps, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
Create a VBO, using a custom GLSL array attribute name, wrapping the given data.
final int strideB
stride in bytes; strideB >= compsPerElement * bytesPerComp
void setVBOEnabled(final boolean vboEnabled)
Enable or disable use of VBO.
final int strideL
stride in logical components
The total number of bytes hold by the referenced buffer is: getComponentSize()* getComponentNumber() ...
int getVBOTarget()
The VBO target or 0 if not a VBO.
int getCompsPerElem()
The number of components per element.
String elemStatsToString()
Returns a string with detailed buffer element stats, i.e.
boolean getNormalized()
True, if GL shall normalize fixed point data while converting them into float.
boolean isVBO()
Determines whether the data is server side (VBO) and enabled, or a client side array (false).
int getVBOUsage()
The VBO usage or 0 if not a VBO.
Buffer getBuffer()
The Buffer holding the data, may be null if a GPU buffer without client bound data.
boolean isVertexAttribute()
Returns true if this data set is intended for a GLSL vertex shader attribute, otherwise false,...
int getVBOName()
The VBO name or 0 if not a VBO.
int getBytesPerComp()
The component's size in bytes.
int getCompType()
The component's GL data type, ie.
int getByteCount()
Returns the byte position (written elements) if not sealed() or the byte limit (available to read) af...
GLBufferStorage mapBufferRange(final int target, final long offset, final long length, final int access)
Returns the GLBufferStorage instance as mapped via OpenGL's native glMapBufferRange(....
GLBufferStorage mapBuffer(int target, int access)
Returns the GLBufferStorage instance as mapped via OpenGL's native glMapBuffer(..) implementation.
void glGenBuffers(int n, IntBuffer buffers)
Entry point to C language function: void {@native glGenBuffers}(GLsizei n, GLuint * buffers) Part ...
boolean glUnmapBuffer(int target)
Entry point to C language function: GLboolean {@native glUnmapBuffer}(GLenum target) Part of GL_VE...
void glDeleteBuffers(int n, IntBuffer buffers)
Entry point to C language function: void {@native glDeleteBuffers}(GLsizei n, const GLuint * buffers...
void glBufferData(int target, long size, Buffer data, int usage)
Entry point to C language function: void {@native glBufferData}(GLenum target, GLsizeiptr size,...
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Definition: GL.java:633
The total number of bytes hold by the referenced buffer is: getComponentSize()* getComponentNumber() ...