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         s = 1; // assume 1 for everything else
421     }
422     return s;
423 }
424 
glUtilsPackPointerData(unsigned char * dst,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)425 void glUtilsPackPointerData(unsigned char *dst, unsigned char *src,
426                      int size, GLenum type, unsigned int stride,
427                      unsigned int datalen)
428 {
429     unsigned int  vsize = size * glSizeof(type);
430     switch (type) {
431     case GL_INT_2_10_10_10_REV:
432     case GL_UNSIGNED_INT_2_10_10_10_REV:
433         vsize = vsize / 4;
434         break;
435     default:
436         break;
437     }
438 
439     if (stride == 0) stride = vsize;
440 
441     if (stride == vsize) {
442         memcpy(dst, src, datalen);
443     } else {
444         for (unsigned int i = 0; i < datalen; i += vsize) {
445             memcpy(dst, src, vsize);
446             dst += vsize;
447             src += stride;
448         }
449     }
450 }
451 
glUtilsWritePackPointerData(void * _stream,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)452 void glUtilsWritePackPointerData(void* _stream, unsigned char *src,
453                                  int size, GLenum type, unsigned int stride,
454                                  unsigned int datalen)
455 {
456     IOStream* stream = reinterpret_cast<IOStream*>(_stream);
457 
458     unsigned int  vsize = size * glSizeof(type);
459     if (stride == 0) stride = vsize;
460 
461     if (stride == vsize) {
462         stream->writeFully(src, datalen);
463     } else {
464         for (unsigned int i = 0; i < datalen; i += vsize) {
465             stream->writeFully(src, (size_t)vsize);
466             src += stride;
467         }
468     }
469 }
470 
471 #ifndef GL_RGBA16F
472 #define GL_RGBA16F                        0x881A
473 #endif // GL_RGBA16F
474 
glUtilsPixelBitSize(GLenum format,GLenum type)475 int glUtilsPixelBitSize(GLenum format, GLenum type)
476 {
477     int components = 0;
478     int componentsize = 0;
479     int pixelsize = 0;
480     switch(type) {
481     case GL_BYTE:
482     case GL_UNSIGNED_BYTE:
483         componentsize = 8;
484         break;
485     case GL_SHORT:
486     case GL_HALF_FLOAT:
487     case GL_UNSIGNED_SHORT:
488         componentsize = 16;
489         break;
490     case GL_INT:
491     case GL_UNSIGNED_INT:
492     case GL_FLOAT:
493     case GL_FIXED:
494         componentsize = 32;
495         break;
496     case GL_UNSIGNED_SHORT_5_6_5:
497     case GL_UNSIGNED_SHORT_4_4_4_4:
498     case GL_UNSIGNED_SHORT_5_5_5_1:
499     case GL_RGB565_OES:
500     case GL_RGB5_A1_OES:
501     case GL_RGBA4_OES:
502         pixelsize = 16;
503         break;
504     case GL_UNSIGNED_INT_2_10_10_10_REV:
505     case GL_UNSIGNED_INT_24_8_OES:
506         pixelsize = 32;
507         break;
508     default:
509         ERR("glUtilsPixelBitSize: unknown pixel type %d - assuming pixel data 0\n", type);
510         componentsize = 0;
511     }
512 
513     if (pixelsize == 0) {
514         switch(format) {
515 #if 0
516         case GL_RED:
517         case GL_GREEN:
518         case GL_BLUE:
519 #endif
520         case GL_ALPHA:
521         case GL_LUMINANCE:
522         case GL_DEPTH_COMPONENT:
523         case GL_DEPTH_STENCIL_OES:
524             components = 1;
525             break;
526         case GL_LUMINANCE_ALPHA:
527             components = 2;
528             break;
529         case GL_RGB:
530 #if 0
531         case GL_BGR:
532 #endif
533             components = 3;
534             break;
535         case GL_RGBA:
536         case GL_BGRA_EXT:
537             components = 4;
538             break;
539         case GL_RGBA16F:
540             pixelsize = 64;
541             break;
542         default:
543             ERR("glUtilsPixelBitSize: unknown pixel format %d\n", format);
544             components = 0;
545         }
546         if (pixelsize == 0) {
547             pixelsize = components * componentsize;
548         }
549     }
550 
551     return pixelsize;
552 }
553 
554 // pack a list of strings into one.
glUtilsPackStrings(char * ptr,char ** strings,GLint * length,GLsizei count)555 void glUtilsPackStrings(char *ptr,  char **strings,  GLint *length, GLsizei count)
556 {
557     char *p = ptr;
558     *p = '\0';
559     for (int i = 0; i < count; i++) {
560         int l=0;
561         if (strings[i]!=NULL) {
562             if (length == NULL || length[i] < 0) {
563                 l = strlen(strings[i]);
564                 strcat(p, strings[i]);
565             } else {
566                 l = length[i];
567                 strncat(p, strings[i], l);
568             }
569         }
570         p += l;
571     }
572 }
573 
574 // claculate the length of a list of strings
glUtilsCalcShaderSourceLen(char ** strings,GLint * length,GLsizei count)575 int glUtilsCalcShaderSourceLen( char **strings,  GLint *length, GLsizei count)
576 {
577     int len = 0;
578     for (int i = 0; i < count; i++) {
579         int l;
580         if (length == NULL || length[i] < 0) {
581             l = strings[i]!=NULL ? strlen(strings[i]) : 0;
582         } else {
583             l = length[i];
584         }
585         len += l;
586     }
587     return len;
588 
589 }
590 
591 // helper to get GL_COLOR_ATTACHMENTn names
glUtilsColorAttachmentName(int i)592 GLenum glUtilsColorAttachmentName(int i) {
593 #undef COLOR_ATTACHMENT_CASE
594 #define COLOR_ATTACHMENT_CASE(i) \
595     case i: \
596         return GL_COLOR_ATTACHMENT##i; \
597 
598     switch (i) {
599         COLOR_ATTACHMENT_CASE(0)
600         COLOR_ATTACHMENT_CASE(1)
601         COLOR_ATTACHMENT_CASE(2)
602         COLOR_ATTACHMENT_CASE(3)
603         COLOR_ATTACHMENT_CASE(4)
604         COLOR_ATTACHMENT_CASE(5)
605         COLOR_ATTACHMENT_CASE(6)
606         COLOR_ATTACHMENT_CASE(7)
607         COLOR_ATTACHMENT_CASE(8)
608         COLOR_ATTACHMENT_CASE(9)
609         COLOR_ATTACHMENT_CASE(10)
610         COLOR_ATTACHMENT_CASE(11)
611         COLOR_ATTACHMENT_CASE(12)
612         COLOR_ATTACHMENT_CASE(13)
613         COLOR_ATTACHMENT_CASE(14)
614         COLOR_ATTACHMENT_CASE(15)
615     }
616     return GL_NONE;
617 #undef COLOR_ATTACHMENT_CASE
618 }
619 
glUtilsColorAttachmentIndex(GLenum attachment)620 int glUtilsColorAttachmentIndex(GLenum attachment) {
621 #undef COLOR_ATTACHMENT_CASE
622 #define COLOR_ATTACHMENT_CASE(i) \
623     case GL_COLOR_ATTACHMENT##i: \
624         return i; \
625 
626     switch (attachment) {
627         COLOR_ATTACHMENT_CASE(0)
628         COLOR_ATTACHMENT_CASE(1)
629         COLOR_ATTACHMENT_CASE(2)
630         COLOR_ATTACHMENT_CASE(3)
631         COLOR_ATTACHMENT_CASE(4)
632         COLOR_ATTACHMENT_CASE(5)
633         COLOR_ATTACHMENT_CASE(6)
634         COLOR_ATTACHMENT_CASE(7)
635         COLOR_ATTACHMENT_CASE(8)
636         COLOR_ATTACHMENT_CASE(9)
637         COLOR_ATTACHMENT_CASE(10)
638         COLOR_ATTACHMENT_CASE(11)
639         COLOR_ATTACHMENT_CASE(12)
640         COLOR_ATTACHMENT_CASE(13)
641         COLOR_ATTACHMENT_CASE(14)
642         COLOR_ATTACHMENT_CASE(15)
643     }
644     return -1;
645 #undef COLOR_ATTACHMENT_CASE
646 }
647 
648 struct glUtilsDrawArraysIndirectCommand {
649     GLuint count;
650     GLuint primCount;
651     GLuint first;
652     GLuint reserved;
653 };
654 
655 struct glUtilsDrawElementsIndirectCommand {
656     GLuint count;
657     GLuint primCount;
658     GLuint first;
659     GLint baseVertex;
660     GLuint reserved;
661 };
662 
glUtilsIndirectStructSize(IndirectCommandType cmdType)663 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType) {
664     switch (cmdType) {
665     case INDIRECT_COMMAND_DRAWARRAYS:
666         return sizeof(glUtilsDrawArraysIndirectCommand);
667     case INDIRECT_COMMAND_DRAWELEMENTS:
668         return sizeof(glUtilsDrawElementsIndirectCommand);
669     }
670     return 4;
671 }
672