• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "glUtils.h"
17 #include <string.h>
18 #include "ErrorLog.h"
19 #include <IOStream.h>
20 
21 #include <GLES3/gl31.h>
22 
glSizeof(GLenum type)23 size_t glSizeof(GLenum type)
24 {
25     size_t retval = 0;
26     switch(type) {
27     case GL_BYTE:
28     case GL_UNSIGNED_BYTE:
29         retval = 1;
30         break;
31     case GL_SHORT:
32     case GL_UNSIGNED_SHORT:
33     case GL_HALF_FLOAT:
34     case GL_HALF_FLOAT_OES:
35         retval = 2;
36         break;
37     case GL_IMAGE_2D:
38     case GL_IMAGE_3D:
39     case GL_UNSIGNED_INT:
40     case GL_INT:
41     case GL_FLOAT:
42     case GL_FIXED:
43     case GL_BOOL:
44         retval =  4;
45         break;
46 #ifdef GL_DOUBLE
47     case GL_DOUBLE:
48         retval = 8;
49         break;
50 #endif
51     case GL_FLOAT_VEC2:
52     case GL_INT_VEC2:
53     case GL_UNSIGNED_INT_VEC2:
54     case GL_BOOL_VEC2:
55         retval = 8;
56         break;
57     case GL_INT_VEC3:
58     case GL_UNSIGNED_INT_VEC3:
59     case GL_BOOL_VEC3:
60     case GL_FLOAT_VEC3:
61         retval = 12;
62         break;
63     case GL_FLOAT_VEC4:
64     case GL_BOOL_VEC4:
65     case GL_INT_VEC4:
66     case GL_UNSIGNED_INT_VEC4:
67     case GL_FLOAT_MAT2:
68         retval = 16;
69         break;
70     case GL_FLOAT_MAT3:
71         retval = 36;
72         break;
73     case GL_FLOAT_MAT4:
74         retval = 64;
75         break;
76     case GL_FLOAT_MAT2x3:
77     case GL_FLOAT_MAT3x2:
78         retval = 4 * 6;
79         break;
80     case GL_FLOAT_MAT2x4:
81     case GL_FLOAT_MAT4x2:
82         retval = 4 * 8;
83         break;
84     case GL_FLOAT_MAT3x4:
85     case GL_FLOAT_MAT4x3:
86         retval = 4 * 12;
87         break;
88     case GL_SAMPLER_2D:
89     case GL_SAMPLER_3D:
90     case GL_SAMPLER_CUBE:
91         retval = 4;
92         break;
93     case GL_UNSIGNED_SHORT_4_4_4_4:
94 	case GL_UNSIGNED_SHORT_5_5_5_1:
95 	case GL_UNSIGNED_SHORT_5_6_5:
96 	case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
97 	case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
98         retval = 2;
99         break;
100 	case GL_INT_2_10_10_10_REV:
101 	case GL_UNSIGNED_INT_10F_11F_11F_REV:
102 	case GL_UNSIGNED_INT_5_9_9_9_REV:
103 	case GL_UNSIGNED_INT_2_10_10_10_REV:
104 	case GL_UNSIGNED_INT_24_8_OES:;
105         retval = 4;
106         break;
107 	case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
108 		retval = 4 + 4;
109         break;
110     default:
111         ALOGE("**** ERROR unknown type 0x%x (%s,%d)\n", type, __FUNCTION__,__LINE__);
112         retval = 4;
113     }
114     return retval;
115 
116 }
117 
glUtilsParamSize(GLenum param)118 size_t glUtilsParamSize(GLenum param)
119 {
120     size_t s = 0;
121 
122     switch(param)
123     {
124     case GL_DEPTH_TEST:
125     case GL_DEPTH_FUNC:
126     case GL_DEPTH_BITS:
127     case GL_MAX_CLIP_PLANES:
128     case GL_MAX_COLOR_ATTACHMENTS:
129     case GL_MAX_DRAW_BUFFERS:
130     case GL_GREEN_BITS:
131     case GL_MAX_MODELVIEW_STACK_DEPTH:
132     case GL_MAX_PROJECTION_STACK_DEPTH:
133     case GL_MAX_TEXTURE_STACK_DEPTH:
134     case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
135     case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
136     case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
137     case GL_MAX_TEXTURE_SIZE:
138     case GL_TEXTURE_GEN_MODE_OES:
139     case GL_TEXTURE_ENV_MODE:
140     case GL_FOG_MODE:
141     case GL_FOG_DENSITY:
142     case GL_FOG_START:
143     case GL_FOG_END:
144     case GL_SPOT_EXPONENT:
145     case GL_CONSTANT_ATTENUATION:
146     case GL_LINEAR_ATTENUATION:
147     case GL_QUADRATIC_ATTENUATION:
148     case GL_SHININESS:
149     case GL_LIGHT_MODEL_TWO_SIDE:
150     case GL_POINT_SIZE:
151     case GL_POINT_SIZE_MIN:
152     case GL_POINT_SIZE_MAX:
153     case GL_POINT_FADE_THRESHOLD_SIZE:
154     case GL_CULL_FACE:
155     case GL_CULL_FACE_MODE:
156     case GL_FRONT_FACE:
157     case GL_SHADE_MODEL:
158     case GL_DEPTH_WRITEMASK:
159     case GL_DEPTH_CLEAR_VALUE:
160     case GL_STENCIL_FAIL:
161     case GL_STENCIL_PASS_DEPTH_FAIL:
162     case GL_STENCIL_PASS_DEPTH_PASS:
163     case GL_STENCIL_REF:
164     case GL_STENCIL_WRITEMASK:
165     case GL_MATRIX_MODE:
166     case GL_MODELVIEW_STACK_DEPTH:
167     case GL_PROJECTION_STACK_DEPTH:
168     case GL_TEXTURE_STACK_DEPTH:
169     case GL_ALPHA_TEST_FUNC:
170     case GL_ALPHA_TEST_REF:
171     case GL_ALPHA_TEST:
172     case GL_DITHER:
173     case GL_BLEND_DST:
174     case GL_BLEND_SRC:
175     case GL_BLEND:
176     case GL_LOGIC_OP_MODE:
177     case GL_SCISSOR_TEST:
178     case GL_MAX_TEXTURE_UNITS:
179     case GL_ACTIVE_TEXTURE:
180     case GL_ALPHA_BITS:
181     case GL_ARRAY_BUFFER_BINDING:
182     case GL_BLUE_BITS:
183     case GL_CLIENT_ACTIVE_TEXTURE:
184     case GL_CLIP_PLANE0:
185     case GL_CLIP_PLANE1:
186     case GL_CLIP_PLANE2:
187     case GL_CLIP_PLANE3:
188     case GL_CLIP_PLANE4:
189     case GL_CLIP_PLANE5:
190     case GL_COLOR_ARRAY:
191     case GL_COLOR_ARRAY_BUFFER_BINDING:
192     case GL_COLOR_ARRAY_SIZE:
193     case GL_COLOR_ARRAY_STRIDE:
194     case GL_COLOR_ARRAY_TYPE:
195     case GL_COLOR_LOGIC_OP:
196     case GL_COLOR_MATERIAL:
197     case GL_PACK_ALIGNMENT:
198     case GL_PERSPECTIVE_CORRECTION_HINT:
199     case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES:
200     case GL_POINT_SIZE_ARRAY_STRIDE_OES:
201     case GL_POINT_SIZE_ARRAY_TYPE_OES:
202     case GL_POINT_SMOOTH:
203     case GL_POINT_SMOOTH_HINT:
204     case GL_POINT_SPRITE_OES:
205     case GL_COORD_REPLACE_OES:
206     case GL_COMBINE_ALPHA:
207     case GL_SRC0_RGB:
208     case GL_SRC1_RGB:
209     case GL_SRC2_RGB:
210     case GL_OPERAND0_RGB:
211     case GL_OPERAND1_RGB:
212     case GL_OPERAND2_RGB:
213     case GL_SRC0_ALPHA:
214     case GL_SRC1_ALPHA:
215     case GL_SRC2_ALPHA:
216     case GL_OPERAND0_ALPHA:
217     case GL_OPERAND1_ALPHA:
218     case GL_OPERAND2_ALPHA:
219     case GL_RGB_SCALE:
220     case GL_ALPHA_SCALE:
221     case GL_COMBINE_RGB:
222     case GL_POLYGON_OFFSET_FACTOR:
223     case GL_POLYGON_OFFSET_FILL:
224     case GL_POLYGON_OFFSET_UNITS:
225     case GL_RED_BITS:
226     case GL_RESCALE_NORMAL:
227     case GL_SAMPLE_ALPHA_TO_COVERAGE:
228     case GL_SAMPLE_ALPHA_TO_ONE:
229     case GL_SAMPLE_BUFFERS:
230     case GL_SAMPLE_COVERAGE:
231     case GL_SAMPLE_COVERAGE_INVERT:
232     case GL_SAMPLE_COVERAGE_VALUE:
233     case GL_SAMPLES:
234     case GL_MAX_SAMPLES_EXT:
235     case GL_STENCIL_BITS:
236     case GL_STENCIL_CLEAR_VALUE:
237     case GL_STENCIL_FUNC:
238     case GL_STENCIL_TEST:
239     case GL_STENCIL_VALUE_MASK:
240     case GL_STENCIL_BACK_FUNC:
241     case GL_STENCIL_BACK_VALUE_MASK:
242     case GL_STENCIL_BACK_REF:
243     case GL_STENCIL_BACK_FAIL:
244     case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
245     case GL_STENCIL_BACK_PASS_DEPTH_PASS:
246     case GL_STENCIL_BACK_WRITEMASK:
247     case GL_TEXTURE_2D:
248     case GL_TEXTURE_BASE_LEVEL:
249     case GL_TEXTURE_BINDING_2D:
250     case GL_TEXTURE_BINDING_CUBE_MAP:
251     case GL_TEXTURE_BINDING_EXTERNAL_OES:
252     case GL_TEXTURE_COMPARE_FUNC:
253     case GL_TEXTURE_COMPARE_MODE:
254     case GL_TEXTURE_COORD_ARRAY:
255     case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING:
256     case GL_TEXTURE_COORD_ARRAY_SIZE:
257     case GL_TEXTURE_COORD_ARRAY_STRIDE:
258     case GL_TEXTURE_COORD_ARRAY_TYPE:
259     case GL_TEXTURE_IMMUTABLE_FORMAT:
260     case GL_UNPACK_ALIGNMENT:
261     case GL_VERTEX_ARRAY:
262     case GL_VERTEX_ARRAY_BUFFER_BINDING:
263     case GL_VERTEX_ARRAY_SIZE:
264     case GL_VERTEX_ARRAY_STRIDE:
265     case GL_VERTEX_ARRAY_TYPE:
266     case GL_SPOT_CUTOFF:
267     case GL_TEXTURE_MIN_FILTER:
268     case GL_TEXTURE_MAG_FILTER:
269     case GL_TEXTURE_MAX_LOD:
270     case GL_TEXTURE_MIN_LOD:
271     case GL_TEXTURE_WRAP_S:
272     case GL_TEXTURE_WRAP_T:
273     case GL_TEXTURE_WRAP_R:
274     case GL_TEXTURE_SWIZZLE_R:
275     case GL_TEXTURE_SWIZZLE_G:
276     case GL_TEXTURE_SWIZZLE_B:
277     case GL_TEXTURE_SWIZZLE_A:
278     case GL_GENERATE_MIPMAP:
279     case GL_GENERATE_MIPMAP_HINT:
280     case GL_RENDERBUFFER_WIDTH_OES:
281     case GL_RENDERBUFFER_HEIGHT_OES:
282     case GL_RENDERBUFFER_INTERNAL_FORMAT_OES:
283     case GL_RENDERBUFFER_RED_SIZE_OES:
284     case GL_RENDERBUFFER_GREEN_SIZE_OES:
285     case GL_RENDERBUFFER_BLUE_SIZE_OES:
286     case GL_RENDERBUFFER_ALPHA_SIZE_OES:
287     case GL_RENDERBUFFER_DEPTH_SIZE_OES:
288     case GL_RENDERBUFFER_STENCIL_SIZE_OES:
289     case GL_RENDERBUFFER_BINDING:
290     case GL_FRAMEBUFFER_BINDING:
291     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_OES:
292     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_OES:
293     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_OES:
294     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_OES:
295     case GL_FENCE_STATUS_NV:
296     case GL_FENCE_CONDITION_NV:
297     case GL_TEXTURE_WIDTH_QCOM:
298     case GL_TEXTURE_HEIGHT_QCOM:
299     case GL_TEXTURE_DEPTH_QCOM:
300     case GL_TEXTURE_INTERNAL_FORMAT_QCOM:
301     case GL_TEXTURE_FORMAT_QCOM:
302     case GL_TEXTURE_TYPE_QCOM:
303     case GL_TEXTURE_IMAGE_VALID_QCOM:
304     case GL_TEXTURE_NUM_LEVELS_QCOM:
305     case GL_TEXTURE_TARGET_QCOM:
306     case GL_TEXTURE_OBJECT_VALID_QCOM:
307     case GL_BLEND_EQUATION_RGB_OES:
308     case GL_BLEND_EQUATION_ALPHA_OES:
309     case GL_BLEND_DST_RGB_OES:
310     case GL_BLEND_SRC_RGB_OES:
311     case GL_BLEND_DST_ALPHA_OES:
312     case GL_BLEND_SRC_ALPHA_OES:
313     case GL_MAX_LIGHTS:
314     case GL_SHADER_TYPE:
315     case GL_COMPILE_STATUS:
316     case GL_SHADER_SOURCE_LENGTH:
317     case GL_CURRENT_PROGRAM:
318     case GL_SUBPIXEL_BITS:
319     case GL_MAX_3D_TEXTURE_SIZE:
320     case GL_MAX_ARRAY_TEXTURE_LAYERS:
321     case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
322     case GL_NUM_SHADER_BINARY_FORMATS:
323     case GL_SHADER_COMPILER:
324     case GL_MAX_VERTEX_ATTRIBS:
325     case GL_MAX_VERTEX_UNIFORM_VECTORS:
326     case GL_MAX_VARYING_VECTORS:
327     case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
328     case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
329     case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
330     case GL_MAX_RENDERBUFFER_SIZE:
331     case GL_MAX_TEXTURE_IMAGE_UNITS:
332     case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
333     case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
334     case GL_LINE_WIDTH:
335     case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
336     case GL_MAX_UNIFORM_BUFFER_BINDINGS:
337     case GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS:
338     case GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS:
339     case GL_UNIFORM_BLOCK_BINDING:
340     case GL_UNIFORM_BLOCK_DATA_SIZE:
341     case GL_UNIFORM_BLOCK_NAME_LENGTH:
342     case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
343     case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
344     case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
345     case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT:
346     case GL_CURRENT_QUERY:
347     case GL_QUERY_RESULT:
348     case GL_QUERY_RESULT_AVAILABLE:
349     case GL_READ_BUFFER:
350     case GL_NUM_PROGRAM_BINARY_FORMATS:
351     case GL_PROGRAM_BINARY_FORMATS:
352 
353     case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
354     case GL_ACTIVE_ATTRIBUTES:
355     case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
356     case GL_ACTIVE_UNIFORM_BLOCKS:
357     case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH:
358     case GL_ACTIVE_UNIFORMS:
359     case GL_ACTIVE_UNIFORM_MAX_LENGTH:
360     case GL_ATTACHED_SHADERS:
361     case GL_DELETE_STATUS:
362     case GL_INFO_LOG_LENGTH:
363     case GL_LINK_STATUS:
364     case GL_PROGRAM_BINARY_LENGTH:
365     case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
366     case GL_PROGRAM_SEPARABLE:
367     case GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT:
368     case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
369     case GL_TRANSFORM_FEEDBACK_VARYINGS:
370     case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH:
371     case GL_TRANSFORM_FEEDBACK_ACTIVE:
372 #define GL_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS 0x8C8A
373 #define GL_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS 0x8C80
374     case GL_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS:
375     case GL_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS:
376     case GL_VALIDATE_STATUS:
377         s = 1;
378         break;
379     case GL_ALIASED_LINE_WIDTH_RANGE:
380     case GL_ALIASED_POINT_SIZE_RANGE:
381     case GL_DEPTH_RANGE:
382     case GL_MAX_VIEWPORT_DIMS:
383     case GL_SMOOTH_POINT_SIZE_RANGE:
384     case GL_SMOOTH_LINE_WIDTH_RANGE:
385     case GL_SAMPLE_POSITION:
386         s= 2;
387         break;
388     case GL_SPOT_DIRECTION:
389     case GL_POINT_DISTANCE_ATTENUATION:
390     case GL_CURRENT_NORMAL:
391     case GL_COMPUTE_WORK_GROUP_SIZE:
392         s =  3;
393         break;
394     case GL_CURRENT_VERTEX_ATTRIB:
395     case GL_CURRENT_TEXTURE_COORDS:
396     case GL_CURRENT_COLOR:
397     case GL_FOG_COLOR:
398     case GL_AMBIENT:
399     case GL_DIFFUSE:
400     case GL_SPECULAR:
401     case GL_EMISSION:
402     case GL_POSITION:
403     case GL_LIGHT_MODEL_AMBIENT:
404     case GL_TEXTURE_ENV_COLOR:
405     case GL_SCISSOR_BOX:
406     case GL_VIEWPORT:
407     case GL_TEXTURE_CROP_RECT_OES:
408     case GL_COLOR_CLEAR_VALUE:
409     case GL_COLOR_WRITEMASK:
410     case GL_AMBIENT_AND_DIFFUSE:
411     case GL_BLEND_COLOR:
412         s =  4;
413         break;
414     case GL_MODELVIEW_MATRIX:
415     case GL_PROJECTION_MATRIX:
416     case GL_TEXTURE_MATRIX:
417         s = 16;
418     break;
419     default:
420         ERR("glUtilsParamSize: unknow param 0x%08x\n", param);
421         s = 1; // assume 1
422     }
423     return s;
424 }
425 
glUtilsPackPointerData(unsigned char * dst,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)426 void glUtilsPackPointerData(unsigned char *dst, unsigned char *src,
427                      int size, GLenum type, unsigned int stride,
428                      unsigned int datalen)
429 {
430     unsigned int  vsize = size * glSizeof(type);
431     switch (type) {
432     case GL_INT_2_10_10_10_REV:
433     case GL_UNSIGNED_INT_2_10_10_10_REV:
434         vsize = vsize / 4;
435         break;
436     default:
437         break;
438     }
439 
440     if (stride == 0) stride = vsize;
441 
442     if (stride == vsize) {
443         memcpy(dst, src, datalen);
444     } else {
445         for (unsigned int i = 0; i < datalen; i += vsize) {
446             memcpy(dst, src, vsize);
447             dst += vsize;
448             src += stride;
449         }
450     }
451 }
452 
glUtilsWritePackPointerData(void * _stream,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)453 void glUtilsWritePackPointerData(void* _stream, unsigned char *src,
454                                  int size, GLenum type, unsigned int stride,
455                                  unsigned int datalen)
456 {
457     IOStream* stream = reinterpret_cast<IOStream*>(_stream);
458 
459     unsigned int  vsize = size * glSizeof(type);
460     if (stride == 0) stride = vsize;
461 
462     if (stride == vsize) {
463         stream->writeFully(src, datalen);
464     } else {
465         for (unsigned int i = 0; i < datalen; i += vsize) {
466             stream->writeFully(src, (size_t)vsize);
467             src += stride;
468         }
469     }
470 }
471 
472 #ifndef GL_RGBA16F
473 #define GL_RGBA16F                        0x881A
474 #endif // GL_RGBA16F
475 
glUtilsPixelBitSize(GLenum format,GLenum type)476 int glUtilsPixelBitSize(GLenum format, GLenum type)
477 {
478     int components = 0;
479     int componentsize = 0;
480     int pixelsize = 0;
481     switch(type) {
482     case GL_BYTE:
483     case GL_UNSIGNED_BYTE:
484         componentsize = 8;
485         break;
486     case GL_SHORT:
487     case GL_HALF_FLOAT:
488     case GL_UNSIGNED_SHORT:
489         componentsize = 16;
490         break;
491     case GL_INT:
492     case GL_UNSIGNED_INT:
493     case GL_FLOAT:
494     case GL_FIXED:
495         componentsize = 32;
496         break;
497     case GL_UNSIGNED_SHORT_5_6_5:
498     case GL_UNSIGNED_SHORT_4_4_4_4:
499     case GL_UNSIGNED_SHORT_5_5_5_1:
500     case GL_RGB565_OES:
501     case GL_RGB5_A1_OES:
502     case GL_RGBA4_OES:
503         pixelsize = 16;
504         break;
505     case GL_UNSIGNED_INT_2_10_10_10_REV:
506     case GL_UNSIGNED_INT_24_8_OES:
507         pixelsize = 32;
508         break;
509     default:
510         ERR("glUtilsPixelBitSize: unknown pixel type %d - assuming pixel data 0\n", type);
511         componentsize = 0;
512     }
513 
514     if (pixelsize == 0) {
515         switch(format) {
516 #if 0
517         case GL_RED:
518         case GL_GREEN:
519         case GL_BLUE:
520 #endif
521         case GL_ALPHA:
522         case GL_LUMINANCE:
523         case GL_DEPTH_COMPONENT:
524         case GL_DEPTH_STENCIL_OES:
525             components = 1;
526             break;
527         case GL_LUMINANCE_ALPHA:
528             components = 2;
529             break;
530         case GL_RGB:
531 #if 0
532         case GL_BGR:
533 #endif
534             components = 3;
535             break;
536         case GL_RGBA:
537         case GL_BGRA_EXT:
538             components = 4;
539             break;
540         case GL_RGBA16F:
541             pixelsize = 64;
542             break;
543         default:
544             ERR("glUtilsPixelBitSize: unknown pixel format %d\n", format);
545             components = 0;
546         }
547         if (pixelsize == 0) {
548             pixelsize = components * componentsize;
549         }
550     }
551 
552     return pixelsize;
553 }
554 
555 // pack a list of strings into one.
glUtilsPackStrings(char * ptr,char ** strings,GLint * length,GLsizei count)556 void glUtilsPackStrings(char *ptr,  char **strings,  GLint *length, GLsizei count)
557 {
558     char *p = ptr;
559     *p = '\0';
560     for (int i = 0; i < count; i++) {
561         int l=0;
562         if (strings[i]!=NULL) {
563             if (length == NULL || length[i] < 0) {
564                 l = strlen(strings[i]);
565                 strcat(p, strings[i]);
566             } else {
567                 l = length[i];
568                 strncat(p, strings[i], l);
569             }
570         }
571         p += l;
572     }
573 }
574 
575 // claculate the length of a list of strings
glUtilsCalcShaderSourceLen(char ** strings,GLint * length,GLsizei count)576 int glUtilsCalcShaderSourceLen( char **strings,  GLint *length, GLsizei count)
577 {
578     int len = 0;
579     for (int i = 0; i < count; i++) {
580         int l;
581         if (length == NULL || length[i] < 0) {
582             l = strings[i]!=NULL ? strlen(strings[i]) : 0;
583         } else {
584             l = length[i];
585         }
586         len += l;
587     }
588     return len;
589 
590 }
591 
592 // helper to get GL_COLOR_ATTACHMENTn names
glUtilsColorAttachmentName(int i)593 GLenum glUtilsColorAttachmentName(int i) {
594 #undef COLOR_ATTACHMENT_CASE
595 #define COLOR_ATTACHMENT_CASE(i) \
596     case i: \
597         return GL_COLOR_ATTACHMENT##i; \
598 
599     switch (i) {
600         COLOR_ATTACHMENT_CASE(0)
601         COLOR_ATTACHMENT_CASE(1)
602         COLOR_ATTACHMENT_CASE(2)
603         COLOR_ATTACHMENT_CASE(3)
604         COLOR_ATTACHMENT_CASE(4)
605         COLOR_ATTACHMENT_CASE(5)
606         COLOR_ATTACHMENT_CASE(6)
607         COLOR_ATTACHMENT_CASE(7)
608         COLOR_ATTACHMENT_CASE(8)
609         COLOR_ATTACHMENT_CASE(9)
610         COLOR_ATTACHMENT_CASE(10)
611         COLOR_ATTACHMENT_CASE(11)
612         COLOR_ATTACHMENT_CASE(12)
613         COLOR_ATTACHMENT_CASE(13)
614         COLOR_ATTACHMENT_CASE(14)
615         COLOR_ATTACHMENT_CASE(15)
616     }
617     return GL_NONE;
618 #undef COLOR_ATTACHMENT_CASE
619 }
620 
glUtilsColorAttachmentIndex(GLenum attachment)621 int glUtilsColorAttachmentIndex(GLenum attachment) {
622 #undef COLOR_ATTACHMENT_CASE
623 #define COLOR_ATTACHMENT_CASE(i) \
624     case GL_COLOR_ATTACHMENT##i: \
625         return i; \
626 
627     switch (attachment) {
628         COLOR_ATTACHMENT_CASE(0)
629         COLOR_ATTACHMENT_CASE(1)
630         COLOR_ATTACHMENT_CASE(2)
631         COLOR_ATTACHMENT_CASE(3)
632         COLOR_ATTACHMENT_CASE(4)
633         COLOR_ATTACHMENT_CASE(5)
634         COLOR_ATTACHMENT_CASE(6)
635         COLOR_ATTACHMENT_CASE(7)
636         COLOR_ATTACHMENT_CASE(8)
637         COLOR_ATTACHMENT_CASE(9)
638         COLOR_ATTACHMENT_CASE(10)
639         COLOR_ATTACHMENT_CASE(11)
640         COLOR_ATTACHMENT_CASE(12)
641         COLOR_ATTACHMENT_CASE(13)
642         COLOR_ATTACHMENT_CASE(14)
643         COLOR_ATTACHMENT_CASE(15)
644     }
645     return -1;
646 #undef COLOR_ATTACHMENT_CASE
647 }
648 
649 struct glUtilsDrawArraysIndirectCommand {
650     GLuint count;
651     GLuint primCount;
652     GLuint first;
653     GLuint reserved;
654 };
655 
656 struct glUtilsDrawElementsIndirectCommand {
657     GLuint count;
658     GLuint primCount;
659     GLuint first;
660     GLint baseVertex;
661     GLuint reserved;
662 };
663 
glUtilsIndirectStructSize(IndirectCommandType cmdType)664 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType) {
665     switch (cmdType) {
666     case INDIRECT_COMMAND_DRAWARRAYS:
667         return sizeof(glUtilsDrawArraysIndirectCommand);
668     case INDIRECT_COMMAND_DRAWELEMENTS:
669         return sizeof(glUtilsDrawElementsIndirectCommand);
670     }
671     return 4;
672 }
673