1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
/*
* Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
* ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
* DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use
* in the design, construction, operation or maintenance of any nuclear
* facility.
*
* Sun gratefully acknowledges that this software was originally authored
* and developed by Kenneth Bradley Russell and Christopher John Kline.
*/
package com.jogamp.opengl.util;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GL2GL3;
import javax.media.opengl.GLES2;
import javax.media.opengl.GLException;
import com.jogamp.common.nio.Buffers;
/**
* Utility routines for dealing with direct buffers.
*
* @author Kenneth Russel, et.al.
*/
public class GLBuffers extends Buffers {
/**
* @param glType GL primitive type
* @return false if one of GL primitive unsigned types, otherwise true
* GL_UNSIGNED_BYTE, <br/>
* GL_UNSIGNED_SHORT, <br/>
* GL_UNSIGNED_INT, <br/>
* GL_HILO16_NV <br/>
*/
public static final boolean isSignedGLType(int glType) {
switch (glType) { // 29
case GL.GL_UNSIGNED_BYTE:
case GL.GL_UNSIGNED_SHORT:
case GL.GL_UNSIGNED_INT:
case GL2.GL_HILO16_NV:
return false;
}
return true;
}
/**
* @param glType GL primitive type
* @return false if one of GL primitive floating point types, otherwise true
* GL_FLOAT, <br/>
* GL_HALF_FLOAT, <br/>
* GL_HALF_FLOAT_OES, <br/>
* GL_DOUBLE <br/>
*/
public static final boolean isGLTypeFixedPoint(int glType) {
switch(glType) {
case GL.GL_FLOAT:
case GL.GL_HALF_FLOAT:
case GLES2.GL_HALF_FLOAT_OES:
case GL2GL3.GL_DOUBLE:
return false;
default:
return true;
}
}
/**
* @param glType shall be one of (31) <br/>
* GL_BYTE, GL_UNSIGNED_BYTE, <br/>
* GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, <br/>
* <br/>
* GL_SHORT, GL_UNSIGNED_SHORT, <br/>
* GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, <br/>
* GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, <br/>
* GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, <br/>
* GL_UNSIGNED_SHORT_8_8_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, <br/>
* GL.GL_HALF_FLOAT, GLES2.GL_HALF_FLOAT_OES: <br/>
* <br/>
* GL_FIXED, GL_INT <br/>
* GL_UNSIGNED_INT, GL_UNSIGNED_INT_8_8_8_8, <br/>
* GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, <br/>
* GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_24_8, <br/>
* GL_UNSIGNED_INT_10F_11F_11F_REV, GL_UNSIGNED_INT_5_9_9_9_REV <br/>
* GL_HILO16_NV, GL_SIGNED_HILO16_NV <br/>
* <br/>
* GL2GL3.GL_FLOAT_32_UNSIGNED_INT_24_8_REV <br/>
* <br/>
* GL_FLOAT, GL_DOUBLE <br/>
*
* @return -1 if glType is unhandled, otherwise the actual value > 0
*/
public static final int sizeOfGLType(int glType) {
switch (glType) { // 29
// case GL2.GL_BITMAP:
case GL.GL_BYTE:
case GL.GL_UNSIGNED_BYTE:
case GL2GL3.GL_UNSIGNED_BYTE_3_3_2:
case GL2GL3.GL_UNSIGNED_BYTE_2_3_3_REV:
return SIZEOF_BYTE;
case GL.GL_SHORT:
case GL.GL_UNSIGNED_SHORT:
case GL.GL_UNSIGNED_SHORT_5_6_5:
case GL2GL3.GL_UNSIGNED_SHORT_5_6_5_REV:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL2GL3.GL_UNSIGNED_SHORT_5_5_5_1:
case GL2GL3.GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL2.GL_UNSIGNED_SHORT_8_8_APPLE:
case GL2.GL_UNSIGNED_SHORT_8_8_REV_APPLE:
case GL.GL_HALF_FLOAT:
case GLES2.GL_HALF_FLOAT_OES:
return SIZEOF_SHORT;
case GL.GL_FIXED:
case GL2ES2.GL_INT:
case GL.GL_UNSIGNED_INT:
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8:
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8_REV:
case GL2GL3.GL_UNSIGNED_INT_10_10_10_2:
case GL2GL3.GL_UNSIGNED_INT_2_10_10_10_REV:
case GL2GL3.GL_UNSIGNED_INT_24_8:
case GL2GL3.GL_UNSIGNED_INT_10F_11F_11F_REV:
case GL2GL3.GL_UNSIGNED_INT_5_9_9_9_REV:
case GL2.GL_HILO16_NV:
case GL2.GL_SIGNED_HILO16_NV:
return SIZEOF_INT;
case GL2GL3.GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
return SIZEOF_LONG;
case GL.GL_FLOAT:
return SIZEOF_FLOAT;
case GL2GL3.GL_DOUBLE:
return SIZEOF_DOUBLE;
}
return -1;
}
/**
* @param glType shall be one of (31) <br/>
* GL_BYTE, GL_UNSIGNED_BYTE, <br/>
* GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, <br/>
* <br/>
* GL_SHORT, GL_UNSIGNED_SHORT, <br/>
* GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, <br/>
* GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, <br/>
* GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, <br/>
* GL_UNSIGNED_SHORT_8_8_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, <br/>
* GL_HALF_FLOAT, GL_HALF_FLOAT_OES <br/>
* <br/>
* GL_FIXED, GL_INT <br/>
* GL_UNSIGNED_INT, GL_UNSIGNED_INT_8_8_8_8, <br/>
* GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, <br/>
* GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_24_8, <br/>
* GL_UNSIGNED_INT_10F_11F_11F_REV, GL_UNSIGNED_INT_5_9_9_9_REV <br/>
* GL_HILO16_NV, GL_SIGNED_HILO16_NV <br/>
* <br/>
* GL_FLOAT_32_UNSIGNED_INT_24_8_REV <br/>
* <br/>
* GL_FLOAT, GL_DOUBLE <br/>
*
* @return null if glType is unhandled, otherwise the new Buffer object
*/
public static final Buffer newDirectGLBuffer(int glType, int numElements) {
switch (glType) { // 29
case GL.GL_BYTE:
case GL.GL_UNSIGNED_BYTE:
case GL2GL3.GL_UNSIGNED_BYTE_3_3_2:
case GL2GL3.GL_UNSIGNED_BYTE_2_3_3_REV:
return newDirectByteBuffer(numElements);
case GL.GL_SHORT:
case GL.GL_UNSIGNED_SHORT:
case GL.GL_UNSIGNED_SHORT_5_6_5:
case GL2GL3.GL_UNSIGNED_SHORT_5_6_5_REV:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL2GL3.GL_UNSIGNED_SHORT_5_5_5_1:
case GL2GL3.GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL2.GL_UNSIGNED_SHORT_8_8_APPLE:
case GL2.GL_UNSIGNED_SHORT_8_8_REV_APPLE:
case GL.GL_HALF_FLOAT:
case GLES2.GL_HALF_FLOAT_OES:
return newDirectShortBuffer(numElements);
case GL.GL_FIXED:
case GL2ES2.GL_INT:
case GL.GL_UNSIGNED_INT:
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8:
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8_REV:
case GL2GL3.GL_UNSIGNED_INT_10_10_10_2:
case GL2GL3.GL_UNSIGNED_INT_2_10_10_10_REV:
case GL2GL3.GL_UNSIGNED_INT_24_8:
case GL2GL3.GL_UNSIGNED_INT_10F_11F_11F_REV:
case GL2GL3.GL_UNSIGNED_INT_5_9_9_9_REV:
case GL2.GL_HILO16_NV:
case GL2.GL_SIGNED_HILO16_NV:
return newDirectIntBuffer(numElements);
case GL2GL3.GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
return newDirectLongBuffer(numElements);
case GL.GL_FLOAT:
return newDirectFloatBuffer(numElements);
case GL2.GL_DOUBLE:
return newDirectDoubleBuffer(numElements);
}
return null;
}
/**
* @param glType shall be one of (31) <br/>
* GL_BYTE, GL_UNSIGNED_BYTE, <br/>
* GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, <br/>
* <br/>
* GL_SHORT, GL_UNSIGNED_SHORT, <br/>
* GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, <br/>
* GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, <br/>
* GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, <br/>
* GL_UNSIGNED_SHORT_8_8_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, <br/>
* GL_HALF_FLOAT, GL_HALF_FLOAT_OES <br/>
* <br/>
* GL_FIXED, GL_INT <br/>
* GL_UNSIGNED_INT, GL_UNSIGNED_INT_8_8_8_8, <br/>
* GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, <br/>
* GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_24_8, <br/>
* GL_UNSIGNED_INT_10F_11F_11F_REV, GL_UNSIGNED_INT_5_9_9_9_REV <br/>
* GL_HILO16_NV, GL_SIGNED_HILO16_NV <br/>
* <br/>
* GL_FLOAT_32_UNSIGNED_INT_24_8_REV <br/>
* <br/>
* GL_FLOAT, GL_DOUBLE <br/>
* @return null if glType is unhandled or parent is null or bufLen is 0, otherwise the new Buffer object
*/
public static final Buffer sliceGLBuffer(ByteBuffer parent, int bytePos, int byteLen, int glType) {
if (parent == null || byteLen == 0) {
return null;
}
final int parentPos = parent.position();
final int parentLimit = parent.limit();
parent.position(bytePos);
parent.limit(bytePos + byteLen);
Buffer res = null;
switch (glType) { // 29
case GL.GL_BYTE:
case GL.GL_UNSIGNED_BYTE:
case GL2GL3.GL_UNSIGNED_BYTE_3_3_2:
case GL2GL3.GL_UNSIGNED_BYTE_2_3_3_REV:
res = parent.slice().order(parent.order()); // slice and duplicate may change byte order
break;
case GL.GL_SHORT:
case GL.GL_UNSIGNED_SHORT:
case GL.GL_UNSIGNED_SHORT_5_6_5:
case GL2GL3.GL_UNSIGNED_SHORT_5_6_5_REV:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL2GL3.GL_UNSIGNED_SHORT_5_5_5_1:
case GL2GL3.GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL2.GL_UNSIGNED_SHORT_8_8_APPLE:
case GL2.GL_UNSIGNED_SHORT_8_8_REV_APPLE:
case GL.GL_HALF_FLOAT:
case GLES2.GL_HALF_FLOAT_OES:
res = parent.slice().order(parent.order()).asShortBuffer(); // slice and duplicate may change byte order
break;
case GL.GL_FIXED:
case GL2GL3.GL_INT:
case GL2ES2.GL_UNSIGNED_INT:
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8:
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8_REV:
case GL2GL3.GL_UNSIGNED_INT_10_10_10_2:
case GL2GL3.GL_UNSIGNED_INT_2_10_10_10_REV:
case GL2GL3.GL_UNSIGNED_INT_24_8:
case GL2GL3.GL_UNSIGNED_INT_10F_11F_11F_REV:
case GL2GL3.GL_UNSIGNED_INT_5_9_9_9_REV:
case GL2.GL_HILO16_NV:
case GL2.GL_SIGNED_HILO16_NV:
res = parent.slice().order(parent.order()).asIntBuffer(); // slice and duplicate may change byte order
break;
case GL2GL3.GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
res = parent.slice().order(parent.order()).asLongBuffer(); // slice and duplicate may change byte order
break;
case GL.GL_FLOAT:
res = parent.slice().order(parent.order()).asFloatBuffer(); // slice and duplicate may change byte order
break;
case GL2.GL_DOUBLE:
res = parent.slice().order(parent.order()).asDoubleBuffer(); // slice and duplicate may change byte order
break;
}
parent.position(parentPos).limit(parentLimit);
return res;
}
private static final int glGetInteger(GL gl, int pname, int[] tmp) {
gl.glGetIntegerv(pname, tmp, 0);
return tmp[0];
}
/**
* Returns the number of bytes required to read/write a memory buffer via OpenGL
* using the current GL pixel storage state and the given parameters.
*
* <p>This method is security critical, hence it throws an exception (fail-fast)
* in case of an invalid alignment. In case we forgot to handle
* proper values, please contact the maintainer.</p>
*
* @param gl the current GL object
*
* @param tmp a pass through integer array of size >= 1 used to store temp data (performance)
*
* @param bytesPerPixel bytes per pixel, i.e. via {@link #bytesPerPixel(int, int)}.
* @param width in pixels
* @param height in pixels
* @param depth in pixels
* @param pack true for read mode GPU -> CPU (pack), otherwise false for write mode CPU -> GPU (unpack)
* @return required minimum size of the buffer in bytes
* @throws GLException if alignment is invalid. Please contact the maintainer if this is our bug.
*/
public static final int sizeof(GL gl, int tmp[],
int bytesPerPixel, int width, int height, int depth,
boolean pack) {
int rowLength = 0;
int skipRows = 0;
int skipPixels = 0;
int alignment = 1;
int imageHeight = 0;
int skipImages = 0;
if (pack) {
alignment = glGetInteger(gl, GL.GL_PACK_ALIGNMENT, tmp);
if(gl.isGL2GL3()) {
rowLength = glGetInteger(gl, GL2GL3.GL_PACK_ROW_LENGTH, tmp);
skipRows = glGetInteger(gl, GL2GL3.GL_PACK_SKIP_ROWS, tmp);
skipPixels = glGetInteger(gl, GL2GL3.GL_PACK_SKIP_PIXELS, tmp);
if (depth > 1) {
imageHeight = glGetInteger(gl, GL2GL3.GL_PACK_IMAGE_HEIGHT, tmp);
skipImages = glGetInteger(gl, GL2GL3.GL_PACK_SKIP_IMAGES, tmp);
}
}
} else {
alignment = glGetInteger(gl, GL.GL_UNPACK_ALIGNMENT, tmp);
if(gl.isGL2GL3 ()) {
rowLength = glGetInteger(gl, GL2GL3.GL_UNPACK_ROW_LENGTH, tmp);
skipRows = glGetInteger(gl, GL2GL3.GL_UNPACK_SKIP_ROWS, tmp);
skipPixels = glGetInteger(gl, GL2GL3.GL_UNPACK_SKIP_PIXELS, tmp);
if (depth > 1) {
imageHeight = glGetInteger(gl, GL2GL3.GL_UNPACK_IMAGE_HEIGHT, tmp);
skipImages = glGetInteger(gl, GL2GL3.GL_UNPACK_SKIP_IMAGES, tmp);
}
}
}
// Try to deal somewhat correctly with potentially invalid values
width = Math.max(0, width );
height = Math.max(1, height); // min 1D
depth = Math.max(1, depth ); // min 1 * imageSize
skipRows = Math.max(0, skipRows);
skipPixels = Math.max(0, skipPixels);
alignment = Math.max(1, alignment);
skipImages = Math.max(0, skipImages);
imageHeight = ( imageHeight > 0 ) ? imageHeight : height;
rowLength = ( rowLength > 0 ) ? rowLength : width;
int rowLengthInBytes = rowLength * bytesPerPixel;
int skipBytes = skipPixels * bytesPerPixel;
switch(alignment) {
case 1:
break;
case 2:
case 4:
case 8: {
// x % 2n == x & (2n - 1)
int remainder = rowLengthInBytes & ( alignment - 1 );
if (remainder > 0) {
rowLengthInBytes += alignment - remainder;
}
remainder = skipBytes & ( alignment - 1 );
if (remainder > 0) {
skipBytes += alignment - remainder;
}
}
break;
default:
throw new GLException("Invalid alignment "+alignment+", must be 2**n (1,2,4,8). Pls notify the maintainer in case this is our bug.");
}
/**
* skipImages, depth, skipPixels and skipRows are static offsets.
*
* skipImages and depth are in multiples of image size.
*
* skipBytes and rowLengthInBytes are aligned
*
* rowLengthInBytes is the aligned byte offset
* from line n to line n+1 at the same x-axis position.
*/
return
skipBytes + // aligned skipPixels * bpp
( skipImages + depth - 1 ) * imageHeight * rowLengthInBytes + // aligned whole images
( skipRows + height - 1 ) * rowLengthInBytes + // aligned lines
width * bytesPerPixel; // last line
}
/**
* Returns the number of bytes required to read/write a memory buffer via OpenGL
* using the current GL pixel storage state and the given parameters.
*
* <p>This method is security critical, hence it throws an exception (fail-fast)
* in case either the format, type or alignment is unhandled. In case we forgot to handle
* proper values, please contact the maintainer.</p>
*
* <p> See {@link #bytesPerPixel(int, int)}. </p>
*
* @param gl the current GL object
*
* @param tmp a pass through integer array of size >= 1 used to store temp data (performance)
*
* @param format must be one of (27) <br/>
* GL_COLOR_INDEX GL_STENCIL_INDEX <br/>
* GL_DEPTH_COMPONENT GL_DEPTH_STENCIL <br/>
* GL_RED GL_RED_INTEGER <br/>
* GL_GREEN GL_GREEN_INTEGER <br/>
* GL_BLUE GL_BLUE_INTEGER <br/>
* GL_ALPHA GL_LUMINANCE (12) <br/>
* <br/>
* GL_LUMINANCE_ALPHA GL_RG <br/>
* GL_RG_INTEGER GL_HILO_NV <br/>
* GL_SIGNED_HILO_NV (5) <br/>
* <br/>
* GL_YCBCR_422_APPLE <br/>
* <br/>
* GL_RGB GL_RGB_INTEGER <br/>
* GL_BGR GL_BGR_INTEGER (4)<br/>
* <br/>
* GL_RGBA GL_RGBA_INTEGER <br/>
* GL_BGRA GL_BGRA_INTEGER <br/>
* GL_ABGR_EXT (5)<br/>
*
* @param type must be one of (32) <br/>
* GL_BITMAP, <br/>
* GL_BYTE, GL_UNSIGNED_BYTE, <br/>
* GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, <br/>
* <br/>
* GL_SHORT, GL_UNSIGNED_SHORT, <br/>
* GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, <br/>
* GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, <br/>
* GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, <br/>
* GL_UNSIGNED_SHORT_8_8_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, <br/>
* GL_HALF_FLOAT, GL_HALF_FLOAT_OES <br/>
* <br/>
* GL_FIXED, GL_INT <br/>
* GL_UNSIGNED_INT, GL_UNSIGNED_INT_8_8_8_8, <br/>
* GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, <br/>
* GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_24_8, <br/>
* GL_UNSIGNED_INT_10F_11F_11F_REV, GL_UNSIGNED_INT_5_9_9_9_REV <br/>
* GL_HILO16_NV, GL_SIGNED_HILO16_NV <br/>
* <br/>
* GL_FLOAT_32_UNSIGNED_INT_24_8_REV <br/>
* <br/>
* GL_FLOAT, GL_DOUBLE <br/>
*
* @param width in pixels
* @param height in pixels
* @param depth in pixels
* @param pack true for read mode GPU -> CPU, otherwise false for write mode CPU -> GPU
* @return required minimum size of the buffer in bytes
* @throws GLException if format, type or alignment is not handled. Please contact the maintainer if this is our bug.
*/
public static final int sizeof(GL gl, int tmp[],
int format, int type, int width, int height, int depth,
boolean pack) throws GLException {
if (width < 0) return 0;
if (height < 0) return 0;
if (depth < 0) return 0;
final int bytesPerPixel = bytesPerPixel(format, type);
return sizeof(gl, tmp, bytesPerPixel, width, height, depth, pack);
}
/**
* Returns the number of bytes required for one pixel with the the given OpenGL format and type.
*
* <p>This method is security critical, hence it throws an exception (fail-fast)
* in case either the format, type or alignment is unhandled. In case we forgot to handle
* proper values, please contact the maintainer.</p>
*
* <p> See {@link #componentCount(int)}. </p>
*
* @param format must be one of (27) <br/>
* GL_COLOR_INDEX GL_STENCIL_INDEX <br/>
* GL_DEPTH_COMPONENT GL_DEPTH_STENCIL <br/>
* GL_RED GL_RED_INTEGER <br/>
* GL_GREEN GL_GREEN_INTEGER <br/>
* GL_BLUE GL_BLUE_INTEGER <br/>
* GL_ALPHA GL_LUMINANCE (12) <br/>
* <br/>
* GL_LUMINANCE_ALPHA GL_RG <br/>
* GL_RG_INTEGER GL_HILO_NV <br/>
* GL_SIGNED_HILO_NV (5) <br/>
* <br/>
* GL_YCBCR_422_APPLE <br/>
* <br/>
* GL_RGB GL_RGB_INTEGER <br/>
* GL_BGR GL_BGR_INTEGER (4)<br/>
* <br/>
* GL_RGBA GL_RGBA_INTEGER <br/>
* GL_BGRA GL_BGRA_INTEGER <br/>
* GL_ABGR_EXT (5)<br/>
*
* @param type must be one of (32) <br/>
* GL_BITMAP, <br/>
* GL_BYTE, GL_UNSIGNED_BYTE, <br/>
* GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, <br/>
* <br/>
* GL_SHORT, GL_UNSIGNED_SHORT, <br/>
* GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, <br/>
* GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, <br/>
* GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, <br/>
* GL_UNSIGNED_SHORT_8_8_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, <br/>
* GL_HALF_FLOAT, GL_HALF_FLOAT_OES <br/>
* <br/>
* GL_FIXED, GL_INT <br/>
* GL_UNSIGNED_INT, GL_UNSIGNED_INT_8_8_8_8, <br/>
* GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, <br/>
* GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_24_8, <br/>
* GL_UNSIGNED_INT_10F_11F_11F_REV, GL_UNSIGNED_INT_5_9_9_9_REV <br/>
* GL_HILO16_NV, GL_SIGNED_HILO16_NV <br/>
* <br/>
* GL_FLOAT_32_UNSIGNED_INT_24_8_REV <br/>
* <br/>
* GL_FLOAT, GL_DOUBLE <br/>
*
* @return required size of one pixel in bytes
* @throws GLException if format or type alignment is not handled. Please contact the maintainer if this is our bug.
*/
public static final int bytesPerPixel(int format, int type) throws GLException {
int compSize = 0;
int compCount = componentCount(format);
switch (type) /* 30 */ {
case GL2.GL_BITMAP:
if (GL2.GL_COLOR_INDEX == format || GL2GL3.GL_STENCIL_INDEX == format) {
compSize = 1;
}
case GL.GL_BYTE:
case GL.GL_UNSIGNED_BYTE:
compSize = 1;
break;
case GL.GL_SHORT:
case GL.GL_UNSIGNED_SHORT:
case GL.GL_HALF_FLOAT:
case GLES2.GL_HALF_FLOAT_OES:
compSize = 2;
break;
case GL.GL_FIXED:
case GL2ES2.GL_INT:
case GL.GL_UNSIGNED_INT:
case GL.GL_FLOAT:
compSize = 4;
break;
case GL2GL3.GL_DOUBLE:
compSize = 8;
break;
case GL2GL3.GL_UNSIGNED_BYTE_3_3_2:
case GL2GL3.GL_UNSIGNED_BYTE_2_3_3_REV:
compSize = 1;
compCount = 1;
break;
case GL.GL_UNSIGNED_SHORT_5_6_5:
case GL2GL3.GL_UNSIGNED_SHORT_5_6_5_REV:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4:
case GL2GL3.GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL2GL3.GL_UNSIGNED_SHORT_5_5_5_1:
case GL2GL3.GL_UNSIGNED_SHORT_1_5_5_5_REV:
compSize = 2;
compCount = 1;
case GL2.GL_UNSIGNED_SHORT_8_8_APPLE:
case GL2.GL_UNSIGNED_SHORT_8_8_REV_APPLE:
compSize = 2;
compCount = 1;
break;
case GL2.GL_HILO16_NV:
case GL2.GL_SIGNED_HILO16_NV:
compSize = 2;
compCount = 2;
break;
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8:
case GL2GL3.GL_UNSIGNED_INT_8_8_8_8_REV:
case GL2GL3.GL_UNSIGNED_INT_10_10_10_2:
case GL2GL3.GL_UNSIGNED_INT_2_10_10_10_REV:
case GL2GL3.GL_UNSIGNED_INT_24_8:
case GL2GL3.GL_UNSIGNED_INT_10F_11F_11F_REV:
case GL2GL3.GL_UNSIGNED_INT_5_9_9_9_REV:
compSize = 4;
compCount = 1;
break;
case GL2GL3.GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
compSize = 8;
compCount = 1;
break;
default:
throw new GLException("type 0x"+Integer.toHexString(type)+"/"+"format 0x"+Integer.toHexString(format)+" not supported [yet], pls notify the maintainer in case this is our bug.");
}
return compCount * compSize;
}
/**
* Returns the number of components required for the given OpenGL format.
*
* <p>This method is security critical, hence it throws an exception (fail-fast)
* in case either the format, type or alignment is unhandled. In case we forgot to handle
* proper values, please contact the maintainer.</p>
*
* @param format must be one of (27) <br/>
* GL_COLOR_INDEX GL_STENCIL_INDEX <br/>
* GL_DEPTH_COMPONENT GL_DEPTH_STENCIL <br/>
* GL_RED GL_RED_INTEGER <br/>
* GL_GREEN GL_GREEN_INTEGER <br/>
* GL_BLUE GL_BLUE_INTEGER <br/>
* GL_ALPHA GL_LUMINANCE (12) <br/>
* <br/>
* GL_LUMINANCE_ALPHA GL_RG <br/>
* GL_RG_INTEGER GL_HILO_NV <br/>
* GL_SIGNED_HILO_NV (5) <br/>
* <br/>
* GL_YCBCR_422_APPLE <br/>
* <br/>
* GL_RGB GL_RGB_INTEGER <br/>
* GL_BGR GL_BGR_INTEGER (4)<br/>
* <br/>
* GL_RGBA GL_RGBA_INTEGER <br/>
* GL_BGRA GL_BGRA_INTEGER <br/>
* GL_ABGR_EXT (5)<br/>
*
* @return number of components required for the given OpenGL format
* @throws GLException if format is not handled. Please contact the maintainer if this is our bug.
*/
public static final int componentCount(int format) throws GLException {
final int compCount;
switch (format) /* 26 */ {
case GL2.GL_COLOR_INDEX:
case GL2GL3.GL_STENCIL_INDEX:
case GL2GL3.GL_DEPTH_COMPONENT:
case GL2GL3.GL_DEPTH_STENCIL:
case GL2GL3.GL_RED:
case GL2GL3.GL_RED_INTEGER:
case GL2GL3.GL_GREEN:
case GL2GL3.GL_GREEN_INTEGER:
case GL2GL3.GL_BLUE:
case GL2GL3.GL_BLUE_INTEGER:
case GL.GL_ALPHA:
case GL.GL_LUMINANCE:
compCount = 1;
break;
case GL.GL_LUMINANCE_ALPHA:
case GL2GL3.GL_RG:
case GL2GL3.GL_RG_INTEGER:
case GL2.GL_HILO_NV:
case GL2.GL_SIGNED_HILO_NV:
compCount = 2;
break;
case GL.GL_RGB:
case GL2GL3.GL_RGB_INTEGER:
case GL2GL3.GL_BGR:
case GL2GL3.GL_BGR_INTEGER:
compCount = 3;
break;
case GL2.GL_YCBCR_422_APPLE:
compCount = 3;
break;
case GL.GL_RGBA:
case GL2GL3.GL_RGBA_INTEGER:
case GL.GL_BGRA:
case GL2GL3.GL_BGRA_INTEGER:
case GL2.GL_ABGR_EXT:
compCount = 4;
break;
/* FIXME ??
case GL.GL_HILO_NV:
elements = 2;
break; */
default:
throw new GLException("format 0x"+Integer.toHexString(format)+" not supported [yet], pls notify the maintainer in case this is our bug.");
}
return compCount;
}
public static final int getNextPowerOf2(int number) {
if (((number-1) & number) == 0) {
//ex: 8 -> 0b1000; 8-1=7 -> 0b0111; 0b1000&0b0111 == 0
return number;
}
int power = 0;
while (number > 0) {
number = number>>1;
power++;
}
return (1<<power);
}
//----------------------------------------------------------------------
// Conversion routines
//
public final static float[] getFloatArray(double[] source) {
int i = source.length;
float[] dest = new float[i--];
while (i >= 0) {
dest[i] = (float) source[i];
i--;
}
return dest;
}
}
|