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_GREEN_BITS:
129     case GL_MAX_MODELVIEW_STACK_DEPTH:
130     case GL_MAX_PROJECTION_STACK_DEPTH:
131     case GL_MAX_TEXTURE_STACK_DEPTH:
132     case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
133     case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
134     case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
135     case GL_MAX_TEXTURE_SIZE:
136     case GL_TEXTURE_GEN_MODE_OES:
137     case GL_TEXTURE_ENV_MODE:
138     case GL_FOG_MODE:
139     case GL_FOG_DENSITY:
140     case GL_FOG_START:
141     case GL_FOG_END:
142     case GL_SPOT_EXPONENT:
143     case GL_CONSTANT_ATTENUATION:
144     case GL_LINEAR_ATTENUATION:
145     case GL_QUADRATIC_ATTENUATION:
146     case GL_SHININESS:
147     case GL_LIGHT_MODEL_TWO_SIDE:
148     case GL_POINT_SIZE:
149     case GL_POINT_SIZE_MIN:
150     case GL_POINT_SIZE_MAX:
151     case GL_POINT_FADE_THRESHOLD_SIZE:
152     case GL_CULL_FACE:
153     case GL_CULL_FACE_MODE:
154     case GL_FRONT_FACE:
155     case GL_SHADE_MODEL:
156     case GL_DEPTH_WRITEMASK:
157     case GL_DEPTH_CLEAR_VALUE:
158     case GL_STENCIL_FAIL:
159     case GL_STENCIL_PASS_DEPTH_FAIL:
160     case GL_STENCIL_PASS_DEPTH_PASS:
161     case GL_STENCIL_REF:
162     case GL_STENCIL_WRITEMASK:
163     case GL_MATRIX_MODE:
164     case GL_MODELVIEW_STACK_DEPTH:
165     case GL_PROJECTION_STACK_DEPTH:
166     case GL_TEXTURE_STACK_DEPTH:
167     case GL_ALPHA_TEST_FUNC:
168     case GL_ALPHA_TEST_REF:
169     case GL_ALPHA_TEST:
170     case GL_DITHER:
171     case GL_BLEND_DST:
172     case GL_BLEND_SRC:
173     case GL_BLEND:
174     case GL_LOGIC_OP_MODE:
175     case GL_SCISSOR_TEST:
176     case GL_MAX_TEXTURE_UNITS:
177     case GL_ACTIVE_TEXTURE:
178     case GL_ALPHA_BITS:
179     case GL_ARRAY_BUFFER_BINDING:
180     case GL_BLUE_BITS:
181     case GL_CLIENT_ACTIVE_TEXTURE:
182     case GL_CLIP_PLANE0:
183     case GL_CLIP_PLANE1:
184     case GL_CLIP_PLANE2:
185     case GL_CLIP_PLANE3:
186     case GL_CLIP_PLANE4:
187     case GL_CLIP_PLANE5:
188     case GL_COLOR_ARRAY:
189     case GL_COLOR_ARRAY_BUFFER_BINDING:
190     case GL_COLOR_ARRAY_SIZE:
191     case GL_COLOR_ARRAY_STRIDE:
192     case GL_COLOR_ARRAY_TYPE:
193     case GL_COLOR_LOGIC_OP:
194     case GL_COLOR_MATERIAL:
195     case GL_PACK_ALIGNMENT:
196     case GL_PERSPECTIVE_CORRECTION_HINT:
197     case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES:
198     case GL_POINT_SIZE_ARRAY_STRIDE_OES:
199     case GL_POINT_SIZE_ARRAY_TYPE_OES:
200     case GL_POINT_SMOOTH:
201     case GL_POINT_SMOOTH_HINT:
202     case GL_POINT_SPRITE_OES:
203     case GL_COORD_REPLACE_OES:
204     case GL_COMBINE_ALPHA:
205     case GL_SRC0_RGB:
206     case GL_SRC1_RGB:
207     case GL_SRC2_RGB:
208     case GL_OPERAND0_RGB:
209     case GL_OPERAND1_RGB:
210     case GL_OPERAND2_RGB:
211     case GL_SRC0_ALPHA:
212     case GL_SRC1_ALPHA:
213     case GL_SRC2_ALPHA:
214     case GL_OPERAND0_ALPHA:
215     case GL_OPERAND1_ALPHA:
216     case GL_OPERAND2_ALPHA:
217     case GL_RGB_SCALE:
218     case GL_ALPHA_SCALE:
219     case GL_COMBINE_RGB:
220     case GL_POLYGON_OFFSET_FACTOR:
221     case GL_POLYGON_OFFSET_FILL:
222     case GL_POLYGON_OFFSET_UNITS:
223     case GL_RED_BITS:
224     case GL_RESCALE_NORMAL:
225     case GL_SAMPLE_ALPHA_TO_COVERAGE:
226     case GL_SAMPLE_ALPHA_TO_ONE:
227     case GL_SAMPLE_BUFFERS:
228     case GL_SAMPLE_COVERAGE:
229     case GL_SAMPLE_COVERAGE_INVERT:
230     case GL_SAMPLE_COVERAGE_VALUE:
231     case GL_SAMPLES:
232     case GL_MAX_SAMPLES_EXT:
233     case GL_STENCIL_BITS:
234     case GL_STENCIL_CLEAR_VALUE:
235     case GL_STENCIL_FUNC:
236     case GL_STENCIL_TEST:
237     case GL_STENCIL_VALUE_MASK:
238     case GL_STENCIL_BACK_FUNC:
239     case GL_STENCIL_BACK_VALUE_MASK:
240     case GL_STENCIL_BACK_REF:
241     case GL_STENCIL_BACK_FAIL:
242     case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
243     case GL_STENCIL_BACK_PASS_DEPTH_PASS:
244     case GL_STENCIL_BACK_WRITEMASK:
245     case GL_TEXTURE_2D:
246     case GL_TEXTURE_BINDING_2D:
247     case GL_TEXTURE_BINDING_CUBE_MAP:
248     case GL_TEXTURE_BINDING_EXTERNAL_OES:
249     case GL_TEXTURE_COORD_ARRAY:
250     case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING:
251     case GL_TEXTURE_COORD_ARRAY_SIZE:
252     case GL_TEXTURE_COORD_ARRAY_STRIDE:
253     case GL_TEXTURE_COORD_ARRAY_TYPE:
254     case GL_UNPACK_ALIGNMENT:
255     case GL_VERTEX_ARRAY:
256     case GL_VERTEX_ARRAY_BUFFER_BINDING:
257     case GL_VERTEX_ARRAY_SIZE:
258     case GL_VERTEX_ARRAY_STRIDE:
259     case GL_VERTEX_ARRAY_TYPE:
260     case GL_SPOT_CUTOFF:
261     case GL_TEXTURE_MIN_FILTER:
262     case GL_TEXTURE_MAG_FILTER:
263     case GL_TEXTURE_WRAP_S:
264     case GL_TEXTURE_WRAP_T:
265     case GL_GENERATE_MIPMAP:
266     case GL_GENERATE_MIPMAP_HINT:
267     case GL_RENDERBUFFER_WIDTH_OES:
268     case GL_RENDERBUFFER_HEIGHT_OES:
269     case GL_RENDERBUFFER_INTERNAL_FORMAT_OES:
270     case GL_RENDERBUFFER_RED_SIZE_OES:
271     case GL_RENDERBUFFER_GREEN_SIZE_OES:
272     case GL_RENDERBUFFER_BLUE_SIZE_OES:
273     case GL_RENDERBUFFER_ALPHA_SIZE_OES:
274     case GL_RENDERBUFFER_DEPTH_SIZE_OES:
275     case GL_RENDERBUFFER_STENCIL_SIZE_OES:
276     case GL_RENDERBUFFER_BINDING:
277     case GL_FRAMEBUFFER_BINDING:
278     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_OES:
279     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_OES:
280     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_OES:
281     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_OES:
282     case GL_FENCE_STATUS_NV:
283     case GL_FENCE_CONDITION_NV:
284     case GL_TEXTURE_WIDTH_QCOM:
285     case GL_TEXTURE_HEIGHT_QCOM:
286     case GL_TEXTURE_DEPTH_QCOM:
287     case GL_TEXTURE_INTERNAL_FORMAT_QCOM:
288     case GL_TEXTURE_FORMAT_QCOM:
289     case GL_TEXTURE_TYPE_QCOM:
290     case GL_TEXTURE_IMAGE_VALID_QCOM:
291     case GL_TEXTURE_NUM_LEVELS_QCOM:
292     case GL_TEXTURE_TARGET_QCOM:
293     case GL_TEXTURE_OBJECT_VALID_QCOM:
294     case GL_BLEND_EQUATION_RGB_OES:
295     case GL_BLEND_EQUATION_ALPHA_OES:
296     case GL_BLEND_DST_RGB_OES:
297     case GL_BLEND_SRC_RGB_OES:
298     case GL_BLEND_DST_ALPHA_OES:
299     case GL_BLEND_SRC_ALPHA_OES:
300     case GL_MAX_LIGHTS:
301     case GL_SHADER_TYPE:
302     case GL_COMPILE_STATUS:
303     case GL_SHADER_SOURCE_LENGTH:
304     case GL_CURRENT_PROGRAM:
305     case GL_SUBPIXEL_BITS:
306     case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
307     case GL_NUM_SHADER_BINARY_FORMATS:
308     case GL_SHADER_COMPILER:
309     case GL_MAX_VERTEX_ATTRIBS:
310     case GL_MAX_VERTEX_UNIFORM_VECTORS:
311     case GL_MAX_VARYING_VECTORS:
312     case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
313     case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
314     case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
315     case GL_MAX_RENDERBUFFER_SIZE:
316     case GL_MAX_TEXTURE_IMAGE_UNITS:
317     case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
318     case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
319     case GL_LINE_WIDTH:
320     case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
321     case GL_MAX_UNIFORM_BUFFER_BINDINGS:
322     case GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS:
323     case GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS:
324     case GL_UNIFORM_BLOCK_BINDING:
325     case GL_UNIFORM_BLOCK_DATA_SIZE:
326     case GL_UNIFORM_BLOCK_NAME_LENGTH:
327     case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
328     case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
329     case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
330     case GL_CURRENT_QUERY:
331     case GL_QUERY_RESULT:
332     case GL_QUERY_RESULT_AVAILABLE:
333     case GL_READ_BUFFER:
334 
335     case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
336     case GL_ACTIVE_ATTRIBUTES:
337     case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
338     case GL_ACTIVE_UNIFORM_BLOCKS:
339     case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH:
340     case GL_ACTIVE_UNIFORMS:
341     case GL_ACTIVE_UNIFORM_MAX_LENGTH:
342     case GL_ATTACHED_SHADERS:
343     case GL_DELETE_STATUS:
344     case GL_INFO_LOG_LENGTH:
345     case GL_LINK_STATUS:
346     case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
347     case GL_PROGRAM_SEPARABLE:
348     case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
349     case GL_TRANSFORM_FEEDBACK_VARYINGS:
350     case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH:
351     case GL_VALIDATE_STATUS:
352         s = 1;
353         break;
354     case GL_ALIASED_LINE_WIDTH_RANGE:
355     case GL_ALIASED_POINT_SIZE_RANGE:
356     case GL_DEPTH_RANGE:
357     case GL_MAX_VIEWPORT_DIMS:
358     case GL_SMOOTH_POINT_SIZE_RANGE:
359     case GL_SMOOTH_LINE_WIDTH_RANGE:
360     case GL_SAMPLE_POSITION:
361         s= 2;
362         break;
363     case GL_SPOT_DIRECTION:
364     case GL_POINT_DISTANCE_ATTENUATION:
365     case GL_CURRENT_NORMAL:
366     case GL_COMPUTE_WORK_GROUP_SIZE:
367         s =  3;
368         break;
369     case GL_CURRENT_VERTEX_ATTRIB:
370     case GL_CURRENT_TEXTURE_COORDS:
371     case GL_CURRENT_COLOR:
372     case GL_FOG_COLOR:
373     case GL_AMBIENT:
374     case GL_DIFFUSE:
375     case GL_SPECULAR:
376     case GL_EMISSION:
377     case GL_POSITION:
378     case GL_LIGHT_MODEL_AMBIENT:
379     case GL_TEXTURE_ENV_COLOR:
380     case GL_SCISSOR_BOX:
381     case GL_VIEWPORT:
382     case GL_TEXTURE_CROP_RECT_OES:
383     case GL_COLOR_CLEAR_VALUE:
384     case GL_COLOR_WRITEMASK:
385     case GL_AMBIENT_AND_DIFFUSE:
386     case GL_BLEND_COLOR:
387         s =  4;
388         break;
389     case GL_MODELVIEW_MATRIX:
390     case GL_PROJECTION_MATRIX:
391     case GL_TEXTURE_MATRIX:
392         s = 16;
393     break;
394     default:
395         ERR("glUtilsParamSize: unknow param 0x%08x\n", param);
396         s = 1; // assume 1
397     }
398     return s;
399 }
400 
glUtilsPackPointerData(unsigned char * dst,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)401 void glUtilsPackPointerData(unsigned char *dst, unsigned char *src,
402                      int size, GLenum type, unsigned int stride,
403                      unsigned int datalen)
404 {
405     unsigned int  vsize = size * glSizeof(type);
406     switch (type) {
407     case GL_INT_2_10_10_10_REV:
408     case GL_UNSIGNED_INT_2_10_10_10_REV:
409         vsize = vsize / 4;
410         break;
411     default:
412         break;
413     }
414 
415     if (stride == 0) stride = vsize;
416 
417     if (stride == vsize) {
418         memcpy(dst, src, datalen);
419     } else {
420         for (unsigned int i = 0; i < datalen; i += vsize) {
421             memcpy(dst, src, vsize);
422             dst += vsize;
423             src += stride;
424         }
425     }
426 }
427 
glUtilsWritePackPointerData(void * _stream,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)428 void glUtilsWritePackPointerData(void* _stream, unsigned char *src,
429                                  int size, GLenum type, unsigned int stride,
430                                  unsigned int datalen)
431 {
432     IOStream* stream = reinterpret_cast<IOStream*>(_stream);
433 
434     unsigned int  vsize = size * glSizeof(type);
435     if (stride == 0) stride = vsize;
436 
437     if (stride == vsize) {
438         stream->writeFully(src, datalen);
439     } else {
440         for (unsigned int i = 0; i < datalen; i += vsize) {
441             stream->writeFully(src, (size_t)vsize);
442             src += stride;
443         }
444     }
445 }
446 
glUtilsPixelBitSize(GLenum format,GLenum type)447 int glUtilsPixelBitSize(GLenum format, GLenum type)
448 {
449     int components = 0;
450     int componentsize = 0;
451     int pixelsize = 0;
452     switch(type) {
453     case GL_BYTE:
454     case GL_UNSIGNED_BYTE:
455         componentsize = 8;
456         break;
457     case GL_SHORT:
458     case GL_UNSIGNED_SHORT:
459     case GL_UNSIGNED_SHORT_5_6_5:
460     case GL_UNSIGNED_SHORT_4_4_4_4:
461     case GL_UNSIGNED_SHORT_5_5_5_1:
462     case GL_RGB565_OES:
463     case GL_RGB5_A1_OES:
464     case GL_RGBA4_OES:
465         pixelsize = 16;
466         break;
467     case GL_INT:
468     case GL_UNSIGNED_INT:
469     case GL_FLOAT:
470     case GL_FIXED:
471     case GL_UNSIGNED_INT_24_8_OES:
472         pixelsize = 32;
473         break;
474     default:
475         ERR("glUtilsPixelBitSize: unknown pixel type - assuming pixel data 0\n");
476         componentsize = 0;
477     }
478 
479     if (pixelsize == 0) {
480         switch(format) {
481 #if 0
482         case GL_RED:
483         case GL_GREEN:
484         case GL_BLUE:
485 #endif
486         case GL_ALPHA:
487         case GL_LUMINANCE:
488         case GL_DEPTH_COMPONENT:
489         case GL_DEPTH_STENCIL_OES:
490             components = 1;
491             break;
492         case GL_LUMINANCE_ALPHA:
493             components = 2;
494             break;
495         case GL_RGB:
496 #if 0
497         case GL_BGR:
498 #endif
499             components = 3;
500             break;
501         case GL_RGBA:
502         case GL_BGRA_EXT:
503             components = 4;
504             break;
505         default:
506             ERR("glUtilsPixelBitSize: unknown pixel format...\n");
507             components = 0;
508         }
509         pixelsize = components * componentsize;
510     }
511 
512     return pixelsize;
513 }
514 
515 // pack a list of strings into one.
glUtilsPackStrings(char * ptr,char ** strings,GLint * length,GLsizei count)516 void glUtilsPackStrings(char *ptr,  char **strings,  GLint *length, GLsizei count)
517 {
518     char *p = ptr;
519     *p = '\0';
520     for (int i = 0; i < count; i++) {
521         int l=0;
522         if (strings[i]!=NULL) {
523             if (length == NULL || length[i] < 0) {
524                 l = strlen(strings[i]);
525                 strcat(p, strings[i]);
526             } else {
527                 l = length[i];
528                 strncat(p, strings[i], l);
529             }
530         }
531         p += l;
532     }
533 }
534 
535 // claculate the length of a list of strings
glUtilsCalcShaderSourceLen(char ** strings,GLint * length,GLsizei count)536 int glUtilsCalcShaderSourceLen( char **strings,  GLint *length, GLsizei count)
537 {
538     int len = 0;
539     for (int i = 0; i < count; i++) {
540         int l;
541         if (length == NULL || length[i] < 0) {
542             l = strings[i]!=NULL ? strlen(strings[i]) : 0;
543         } else {
544             l = length[i];
545         }
546         len += l;
547     }
548     return len;
549 
550 }
551 
552 // helper to get GL_COLOR_ATTACHMENTn names
glUtilsColorAttachmentName(int i)553 GLenum glUtilsColorAttachmentName(int i) {
554 #undef COLOR_ATTACHMENT_CASE
555 #define COLOR_ATTACHMENT_CASE(i) \
556     case i: \
557         return GL_COLOR_ATTACHMENT##i; \
558 
559     switch (i) {
560         COLOR_ATTACHMENT_CASE(0)
561         COLOR_ATTACHMENT_CASE(1)
562         COLOR_ATTACHMENT_CASE(2)
563         COLOR_ATTACHMENT_CASE(3)
564         COLOR_ATTACHMENT_CASE(4)
565         COLOR_ATTACHMENT_CASE(5)
566         COLOR_ATTACHMENT_CASE(6)
567         COLOR_ATTACHMENT_CASE(7)
568         COLOR_ATTACHMENT_CASE(8)
569         COLOR_ATTACHMENT_CASE(9)
570         COLOR_ATTACHMENT_CASE(10)
571         COLOR_ATTACHMENT_CASE(11)
572         COLOR_ATTACHMENT_CASE(12)
573         COLOR_ATTACHMENT_CASE(13)
574         COLOR_ATTACHMENT_CASE(14)
575         COLOR_ATTACHMENT_CASE(15)
576     }
577     return GL_NONE;
578 #undef COLOR_ATTACHMENT_CASE
579 }
580 
glUtilsColorAttachmentIndex(GLenum attachment)581 int glUtilsColorAttachmentIndex(GLenum attachment) {
582 #undef COLOR_ATTACHMENT_CASE
583 #define COLOR_ATTACHMENT_CASE(i) \
584     case GL_COLOR_ATTACHMENT##i: \
585         return i; \
586 
587     switch (attachment) {
588         COLOR_ATTACHMENT_CASE(0)
589         COLOR_ATTACHMENT_CASE(1)
590         COLOR_ATTACHMENT_CASE(2)
591         COLOR_ATTACHMENT_CASE(3)
592         COLOR_ATTACHMENT_CASE(4)
593         COLOR_ATTACHMENT_CASE(5)
594         COLOR_ATTACHMENT_CASE(6)
595         COLOR_ATTACHMENT_CASE(7)
596         COLOR_ATTACHMENT_CASE(8)
597         COLOR_ATTACHMENT_CASE(9)
598         COLOR_ATTACHMENT_CASE(10)
599         COLOR_ATTACHMENT_CASE(11)
600         COLOR_ATTACHMENT_CASE(12)
601         COLOR_ATTACHMENT_CASE(13)
602         COLOR_ATTACHMENT_CASE(14)
603         COLOR_ATTACHMENT_CASE(15)
604     }
605     return -1;
606 #undef COLOR_ATTACHMENT_CASE
607 }
608 
609 struct glUtilsDrawArraysIndirectCommand {
610     GLuint count;
611     GLuint primCount;
612     GLuint first;
613     GLuint reserved;
614 };
615 
616 struct glUtilsDrawElementsIndirectCommand {
617     GLuint count;
618     GLuint primCount;
619     GLuint first;
620     GLint baseVertex;
621     GLuint reserved;
622 };
623 
glUtilsIndirectStructSize(IndirectCommandType cmdType)624 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType) {
625     switch (cmdType) {
626     case INDIRECT_COMMAND_DRAWARRAYS:
627         return sizeof(glUtilsDrawArraysIndirectCommand);
628     case INDIRECT_COMMAND_DRAWELEMENTS:
629         return sizeof(glUtilsDrawElementsIndirectCommand);
630     }
631     return 4;
632 }
633