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 "gfxstream/guest/IOStream.h"
20 
21 #include <GLES3/gl31.h>
22 
23 using gfxstream::guest::IOStream;
24 
isSamplerType(GLenum type)25 bool isSamplerType(GLenum type) {
26     switch (type) {
27         case GL_SAMPLER_2D:
28         case GL_SAMPLER_3D:
29         case GL_SAMPLER_CUBE:
30         case GL_SAMPLER_2D_SHADOW:
31         case GL_SAMPLER_2D_ARRAY:
32         case GL_SAMPLER_2D_ARRAY_SHADOW:
33         case GL_SAMPLER_2D_MULTISAMPLE:
34         case GL_SAMPLER_CUBE_SHADOW:
35         case GL_SAMPLER_BUFFER_OES:
36         case GL_INT_SAMPLER_2D:
37         case GL_INT_SAMPLER_3D:
38         case GL_INT_SAMPLER_CUBE:
39         case GL_INT_SAMPLER_2D_ARRAY:
40         case GL_INT_SAMPLER_2D_MULTISAMPLE:
41         case GL_INT_SAMPLER_BUFFER_OES:
42         case GL_UNSIGNED_INT_SAMPLER_2D:
43         case GL_UNSIGNED_INT_SAMPLER_3D:
44         case GL_UNSIGNED_INT_SAMPLER_CUBE:
45         case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
46         case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
47         case GL_UNSIGNED_INT_SAMPLER_BUFFER_OES:
48             return true;
49         default:
50             return false;
51     }
52 }
53 
isIntegerType(GLenum type)54 bool isIntegerType(GLenum type) {
55     switch (type) {
56         case GL_UNSIGNED_INT:
57         case GL_INT:
58         case GL_INT_VEC2:
59         case GL_UNSIGNED_INT_VEC2:
60         case GL_INT_VEC3:
61         case GL_UNSIGNED_INT_VEC3:
62         case GL_INT_VEC4:
63         case GL_UNSIGNED_INT_VEC4:
64         case GL_INT_IMAGE_2D:
65         case GL_INT_IMAGE_3D:
66         case GL_INT_IMAGE_CUBE:
67         case GL_INT_IMAGE_2D_ARRAY:
68         case GL_INT_IMAGE_BUFFER_OES:
69         case GL_UNSIGNED_INT_IMAGE_2D:
70         case GL_UNSIGNED_INT_IMAGE_3D:
71         case GL_UNSIGNED_INT_IMAGE_CUBE:
72         case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
73         case GL_UNSIGNED_INT_ATOMIC_COUNTER:
74         case GL_UNSIGNED_INT_IMAGE_BUFFER_OES:
75             return true;
76         default:
77             return false;
78     }
79 }
80 
isUnsignedIntType(GLenum type)81 bool isUnsignedIntType(GLenum type) {
82     switch (type) {
83         case GL_UNSIGNED_INT:
84         case GL_UNSIGNED_INT_VEC2:
85         case GL_UNSIGNED_INT_VEC3:
86         case GL_UNSIGNED_INT_VEC4:
87         case GL_UNSIGNED_INT_IMAGE_2D:
88         case GL_UNSIGNED_INT_IMAGE_3D:
89         case GL_UNSIGNED_INT_IMAGE_CUBE:
90         case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
91         case GL_UNSIGNED_INT_ATOMIC_COUNTER:
92         case GL_UNSIGNED_INT_IMAGE_BUFFER_OES:
93             return true;
94         default:
95             return false;
96     }
97 }
98 
isBoolType(GLenum type)99 bool isBoolType(GLenum type) {
100     switch (type) {
101         case GL_BOOL:
102         case GL_BOOL_VEC2:
103         case GL_BOOL_VEC3:
104         case GL_BOOL_VEC4:
105             return true;
106         default:
107             return false;
108     }
109 }
110 
getColumnsOfType(GLenum type)111 uint32_t getColumnsOfType(GLenum type) {
112     switch (type) {
113     case GL_BYTE:
114     case GL_UNSIGNED_BYTE:
115     case GL_SHORT:
116     case GL_UNSIGNED_SHORT:
117     case GL_HALF_FLOAT:
118     case GL_HALF_FLOAT_OES:
119     case GL_IMAGE_2D:
120     case GL_IMAGE_3D:
121     case GL_UNSIGNED_INT:
122     case GL_INT:
123     case GL_FLOAT:
124     case GL_FIXED:
125     case GL_BOOL:
126         return 1;
127 #ifdef GL_DOUBLE
128     case GL_DOUBLE:
129         return 1;
130     case GL_DOUBLE_VEC2:
131     case GL_DOUBLE_MAT2:
132     case GL_DOUBLE_MAT2x3:
133     case GL_DOUBLE_MAT2x4:
134         return 2;
135     case GL_DOUBLE_VEC3:
136     case GL_DOUBLE_MAT3:
137     case GL_DOUBLE_MAT3x2:
138     case GL_DOUBLE_MAT3x4:
139         return 3;
140     case GL_DOUBLE_VEC4:
141     case GL_DOUBLE_MAT4:
142     case GL_DOUBLE_MAT4x2:
143     case GL_DOUBLE_MAT4x3:
144         return 4;
145 #endif
146     case GL_FLOAT_VEC2:
147     case GL_INT_VEC2:
148     case GL_UNSIGNED_INT_VEC2:
149     case GL_BOOL_VEC2:
150     case GL_FLOAT_MAT2:
151     case GL_FLOAT_MAT2x3:
152     case GL_FLOAT_MAT2x4:
153         return 2;
154     case GL_INT_VEC3:
155     case GL_UNSIGNED_INT_VEC3:
156     case GL_BOOL_VEC3:
157     case GL_FLOAT_VEC3:
158     case GL_FLOAT_MAT3:
159     case GL_FLOAT_MAT3x2:
160     case GL_FLOAT_MAT3x4:
161         return 3;
162     case GL_FLOAT_VEC4:
163     case GL_BOOL_VEC4:
164     case GL_INT_VEC4:
165     case GL_UNSIGNED_INT_VEC4:
166     case GL_FLOAT_MAT4:
167     case GL_FLOAT_MAT4x2:
168     case GL_FLOAT_MAT4x3:
169         return 4;
170     case GL_SAMPLER_2D:
171     case GL_SAMPLER_3D:
172     case GL_SAMPLER_CUBE:
173     case GL_SAMPLER_2D_SHADOW:
174     case GL_SAMPLER_2D_ARRAY:
175     case GL_SAMPLER_2D_ARRAY_SHADOW:
176     case GL_SAMPLER_2D_MULTISAMPLE:
177     case GL_SAMPLER_CUBE_SHADOW:
178     case GL_SAMPLER_BUFFER_OES:
179     case GL_INT_SAMPLER_2D:
180     case GL_INT_SAMPLER_3D:
181     case GL_INT_SAMPLER_CUBE:
182     case GL_INT_SAMPLER_2D_ARRAY:
183     case GL_INT_SAMPLER_2D_MULTISAMPLE:
184     case GL_INT_SAMPLER_BUFFER_OES:
185     case GL_UNSIGNED_INT_SAMPLER_2D:
186     case GL_UNSIGNED_INT_SAMPLER_3D:
187     case GL_UNSIGNED_INT_SAMPLER_CUBE:
188     case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
189     case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
190     case GL_UNSIGNED_INT_SAMPLER_BUFFER_OES:
191     case GL_IMAGE_CUBE:
192     case GL_IMAGE_2D_ARRAY:
193     case GL_IMAGE_BUFFER_OES:
194     case GL_INT_IMAGE_2D:
195     case GL_INT_IMAGE_3D:
196     case GL_INT_IMAGE_CUBE:
197     case GL_INT_IMAGE_2D_ARRAY:
198     case GL_INT_IMAGE_BUFFER_OES:
199     case GL_UNSIGNED_INT_IMAGE_2D:
200     case GL_UNSIGNED_INT_IMAGE_3D:
201     case GL_UNSIGNED_INT_IMAGE_CUBE:
202     case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
203     case GL_UNSIGNED_INT_ATOMIC_COUNTER:
204     case GL_UNSIGNED_INT_IMAGE_BUFFER_OES:
205     default:
206         return 1;
207     }
208 }
209 
getRowsOfType(GLenum type)210 uint32_t getRowsOfType(GLenum type) {
211     switch (type) {
212     case GL_BYTE:
213     case GL_UNSIGNED_BYTE:
214     case GL_SHORT:
215     case GL_UNSIGNED_SHORT:
216     case GL_HALF_FLOAT:
217     case GL_HALF_FLOAT_OES:
218     case GL_IMAGE_2D:
219     case GL_IMAGE_3D:
220     case GL_UNSIGNED_INT:
221     case GL_INT:
222     case GL_FLOAT:
223     case GL_FIXED:
224     case GL_BOOL:
225         return 1;
226 #ifdef GL_DOUBLE
227     case GL_DOUBLE:
228     case GL_DOUBLE_VEC2:
229     case GL_DOUBLE_VEC3:
230     case GL_DOUBLE_VEC4:
231         return 1;
232     case GL_DOUBLE_MAT2:
233     case GL_DOUBLE_MAT3x2:
234     case GL_DOUBLE_MAT4x2:
235         return 2;
236     case GL_DOUBLE_MAT3:
237     case GL_DOUBLE_MAT2x3:
238     case GL_DOUBLE_MAT4x3:
239         return 3;
240     case GL_DOUBLE_MAT4:
241     case GL_DOUBLE_MAT3x4:
242     case GL_DOUBLE_MAT2x4:
243         return 4;
244 #endif
245     case GL_FLOAT_VEC2:
246     case GL_INT_VEC2:
247     case GL_UNSIGNED_INT_VEC2:
248     case GL_BOOL_VEC2:
249     case GL_INT_VEC3:
250     case GL_UNSIGNED_INT_VEC3:
251     case GL_BOOL_VEC3:
252     case GL_FLOAT_VEC3:
253     case GL_FLOAT_VEC4:
254     case GL_BOOL_VEC4:
255     case GL_INT_VEC4:
256     case GL_UNSIGNED_INT_VEC4:
257         return 1;
258     case GL_FLOAT_MAT2:
259     case GL_FLOAT_MAT3x2:
260     case GL_FLOAT_MAT4x2:
261         return 2;
262     case GL_FLOAT_MAT3:
263     case GL_FLOAT_MAT2x3:
264     case GL_FLOAT_MAT4x3:
265         return 3;
266     case GL_FLOAT_MAT4:
267     case GL_FLOAT_MAT2x4:
268     case GL_FLOAT_MAT3x4:
269         return 4;
270     case GL_SAMPLER_2D:
271     case GL_SAMPLER_3D:
272     case GL_SAMPLER_CUBE:
273     case GL_SAMPLER_2D_SHADOW:
274     case GL_SAMPLER_2D_ARRAY:
275     case GL_SAMPLER_2D_ARRAY_SHADOW:
276     case GL_SAMPLER_2D_MULTISAMPLE:
277     case GL_SAMPLER_CUBE_SHADOW:
278     case GL_SAMPLER_BUFFER_OES:
279     case GL_INT_SAMPLER_2D:
280     case GL_INT_SAMPLER_3D:
281     case GL_INT_SAMPLER_CUBE:
282     case GL_INT_SAMPLER_2D_ARRAY:
283     case GL_INT_SAMPLER_2D_MULTISAMPLE:
284     case GL_INT_SAMPLER_BUFFER_OES:
285     case GL_UNSIGNED_INT_SAMPLER_2D:
286     case GL_UNSIGNED_INT_SAMPLER_3D:
287     case GL_UNSIGNED_INT_SAMPLER_CUBE:
288     case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
289     case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
290     case GL_UNSIGNED_INT_SAMPLER_BUFFER_OES:
291     case GL_IMAGE_CUBE:
292     case GL_IMAGE_2D_ARRAY:
293     case GL_IMAGE_BUFFER_OES:
294     case GL_INT_IMAGE_2D:
295     case GL_INT_IMAGE_3D:
296     case GL_INT_IMAGE_CUBE:
297     case GL_INT_IMAGE_2D_ARRAY:
298     case GL_INT_IMAGE_BUFFER_OES:
299     case GL_UNSIGNED_INT_IMAGE_2D:
300     case GL_UNSIGNED_INT_IMAGE_3D:
301     case GL_UNSIGNED_INT_IMAGE_CUBE:
302     case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
303     case GL_UNSIGNED_INT_ATOMIC_COUNTER:
304     case GL_UNSIGNED_INT_IMAGE_BUFFER_OES:
305     default:
306         return 1;
307     }
308 }
309 
getAttributeCountOfType(GLenum type)310 uint32_t getAttributeCountOfType(GLenum type) {
311     switch (type) {
312     case GL_BYTE:
313     case GL_UNSIGNED_BYTE:
314     case GL_SHORT:
315     case GL_UNSIGNED_SHORT:
316     case GL_HALF_FLOAT:
317     case GL_HALF_FLOAT_OES:
318     case GL_IMAGE_2D:
319     case GL_IMAGE_3D:
320     case GL_UNSIGNED_INT:
321     case GL_INT:
322     case GL_FLOAT:
323     case GL_FIXED:
324     case GL_BOOL:
325         return 1;
326 #ifdef GL_DOUBLE
327     case GL_DOUBLE:
328     case GL_DOUBLE_VEC2:
329     case GL_DOUBLE_VEC3:
330     case GL_DOUBLE_VEC4:
331         return 1;
332     case GL_DOUBLE_MAT2:
333     case GL_DOUBLE_MAT2x3:
334     case GL_DOUBLE_MAT2x4:
335         return 4;
336     case GL_DOUBLE_MAT3:
337     case GL_DOUBLE_MAT3x2:
338     case GL_DOUBLE_MAT3x4:
339         return 6;
340     case GL_DOUBLE_MAT4:
341     case GL_DOUBLE_MAT4x2:
342     case GL_DOUBLE_MAT4x3:
343         return 8;
344 #endif
345     case GL_FLOAT_VEC2:
346     case GL_INT_VEC2:
347     case GL_UNSIGNED_INT_VEC2:
348     case GL_BOOL_VEC2:
349     case GL_INT_VEC3:
350     case GL_UNSIGNED_INT_VEC3:
351     case GL_BOOL_VEC3:
352     case GL_FLOAT_VEC3:
353     case GL_FLOAT_VEC4:
354     case GL_BOOL_VEC4:
355     case GL_INT_VEC4:
356     case GL_UNSIGNED_INT_VEC4:
357         return 1;
358     case GL_FLOAT_MAT2:
359     case GL_FLOAT_MAT2x3:
360     case GL_FLOAT_MAT2x4:
361         return 2;
362     case GL_FLOAT_MAT3:
363     case GL_FLOAT_MAT3x2:
364     case GL_FLOAT_MAT3x4:
365         return 3;
366     case GL_FLOAT_MAT4:
367     case GL_FLOAT_MAT4x2:
368     case GL_FLOAT_MAT4x3:
369         return 4;
370     case GL_SAMPLER_2D:
371     case GL_SAMPLER_3D:
372     case GL_SAMPLER_CUBE:
373     case GL_SAMPLER_2D_SHADOW:
374     case GL_SAMPLER_2D_ARRAY:
375     case GL_SAMPLER_2D_ARRAY_SHADOW:
376     case GL_SAMPLER_2D_MULTISAMPLE:
377     case GL_SAMPLER_CUBE_SHADOW:
378     case GL_SAMPLER_BUFFER_OES:
379     case GL_INT_SAMPLER_2D:
380     case GL_INT_SAMPLER_3D:
381     case GL_INT_SAMPLER_CUBE:
382     case GL_INT_SAMPLER_2D_ARRAY:
383     case GL_INT_SAMPLER_2D_MULTISAMPLE:
384     case GL_INT_SAMPLER_BUFFER_OES:
385     case GL_UNSIGNED_INT_SAMPLER_2D:
386     case GL_UNSIGNED_INT_SAMPLER_3D:
387     case GL_UNSIGNED_INT_SAMPLER_CUBE:
388     case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
389     case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
390     case GL_UNSIGNED_INT_SAMPLER_BUFFER_OES:
391     case GL_IMAGE_CUBE:
392     case GL_IMAGE_2D_ARRAY:
393     case GL_IMAGE_BUFFER_OES:
394     case GL_INT_IMAGE_2D:
395     case GL_INT_IMAGE_3D:
396     case GL_INT_IMAGE_CUBE:
397     case GL_INT_IMAGE_2D_ARRAY:
398     case GL_INT_IMAGE_BUFFER_OES:
399     case GL_UNSIGNED_INT_IMAGE_2D:
400     case GL_UNSIGNED_INT_IMAGE_3D:
401     case GL_UNSIGNED_INT_IMAGE_CUBE:
402     case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
403     case GL_UNSIGNED_INT_ATOMIC_COUNTER:
404     case GL_UNSIGNED_INT_IMAGE_BUFFER_OES:
405     default:
406         return 1;
407     }
408 }
409 
glSizeof(GLenum type)410 size_t glSizeof(GLenum type)
411 {
412     size_t retval = 0;
413     switch(type) {
414     case GL_BYTE:
415     case GL_UNSIGNED_BYTE:
416         retval = 1;
417         break;
418     case GL_SHORT:
419     case GL_UNSIGNED_SHORT:
420     case GL_HALF_FLOAT:
421     case GL_HALF_FLOAT_OES:
422         retval = 2;
423         break;
424     case GL_IMAGE_2D:
425     case GL_IMAGE_3D:
426     case GL_UNSIGNED_INT:
427     case GL_INT:
428     case GL_FLOAT:
429     case GL_FIXED:
430     case GL_BOOL:
431         retval =  4;
432         break;
433 #ifdef GL_DOUBLE
434     case GL_DOUBLE:
435         retval = 8;
436         break;
437     case GL_DOUBLE_VEC2:
438         retval = 16;
439         break;
440     case GL_DOUBLE_VEC3:
441         retval = 24;
442         break;
443     case GL_DOUBLE_VEC4:
444         retval = 32;
445         break;
446     case GL_DOUBLE_MAT2:
447         retval = 8 * 4;
448         break;
449     case GL_DOUBLE_MAT3:
450         retval = 8 * 9;
451         break;
452     case GL_DOUBLE_MAT4:
453         retval = 8 * 16;
454         break;
455     case GL_DOUBLE_MAT2x3:
456     case GL_DOUBLE_MAT3x2:
457         retval = 8 * 6;
458         break;
459     case GL_DOUBLE_MAT2x4:
460     case GL_DOUBLE_MAT4x2:
461         retval = 8 * 8;
462         break;
463     case GL_DOUBLE_MAT3x4:
464     case GL_DOUBLE_MAT4x3:
465         retval = 8 * 12;
466         break;
467 #endif
468     case GL_FLOAT_VEC2:
469     case GL_INT_VEC2:
470     case GL_UNSIGNED_INT_VEC2:
471     case GL_BOOL_VEC2:
472         retval = 8;
473         break;
474     case GL_INT_VEC3:
475     case GL_UNSIGNED_INT_VEC3:
476     case GL_BOOL_VEC3:
477     case GL_FLOAT_VEC3:
478         retval = 12;
479         break;
480     case GL_FLOAT_VEC4:
481     case GL_BOOL_VEC4:
482     case GL_INT_VEC4:
483     case GL_UNSIGNED_INT_VEC4:
484     case GL_FLOAT_MAT2:
485         retval = 16;
486         break;
487     case GL_FLOAT_MAT3:
488         retval = 36;
489         break;
490     case GL_FLOAT_MAT4:
491         retval = 64;
492         break;
493     case GL_FLOAT_MAT2x3:
494     case GL_FLOAT_MAT3x2:
495         retval = 4 * 6;
496         break;
497     case GL_FLOAT_MAT2x4:
498     case GL_FLOAT_MAT4x2:
499         retval = 4 * 8;
500         break;
501     case GL_FLOAT_MAT3x4:
502     case GL_FLOAT_MAT4x3:
503         retval = 4 * 12;
504         break;
505     case GL_SAMPLER_2D:
506     case GL_SAMPLER_3D:
507     case GL_SAMPLER_CUBE:
508     case GL_SAMPLER_2D_SHADOW:
509     case GL_SAMPLER_2D_ARRAY:
510     case GL_SAMPLER_2D_ARRAY_SHADOW:
511     case GL_SAMPLER_2D_MULTISAMPLE:
512     case GL_SAMPLER_CUBE_SHADOW:
513     case GL_SAMPLER_BUFFER_OES:
514     case GL_INT_SAMPLER_2D:
515     case GL_INT_SAMPLER_3D:
516     case GL_INT_SAMPLER_CUBE:
517     case GL_INT_SAMPLER_2D_ARRAY:
518     case GL_INT_SAMPLER_2D_MULTISAMPLE:
519     case GL_INT_SAMPLER_BUFFER_OES:
520     case GL_UNSIGNED_INT_SAMPLER_2D:
521     case GL_UNSIGNED_INT_SAMPLER_3D:
522     case GL_UNSIGNED_INT_SAMPLER_CUBE:
523     case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
524     case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
525     case GL_UNSIGNED_INT_SAMPLER_BUFFER_OES:
526     case GL_IMAGE_CUBE:
527     case GL_IMAGE_2D_ARRAY:
528     case GL_IMAGE_BUFFER_OES:
529     case GL_INT_IMAGE_2D:
530     case GL_INT_IMAGE_3D:
531     case GL_INT_IMAGE_CUBE:
532     case GL_INT_IMAGE_2D_ARRAY:
533     case GL_INT_IMAGE_BUFFER_OES:
534     case GL_UNSIGNED_INT_IMAGE_2D:
535     case GL_UNSIGNED_INT_IMAGE_3D:
536     case GL_UNSIGNED_INT_IMAGE_CUBE:
537     case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
538     case GL_UNSIGNED_INT_ATOMIC_COUNTER:
539     case GL_UNSIGNED_INT_IMAGE_BUFFER_OES:
540         retval = 4;
541         break;
542     case GL_UNSIGNED_SHORT_4_4_4_4:
543 	case GL_UNSIGNED_SHORT_5_5_5_1:
544 	case GL_UNSIGNED_SHORT_5_6_5:
545 	case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
546 	case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
547         retval = 2;
548         break;
549 	case GL_INT_2_10_10_10_REV:
550 	case GL_UNSIGNED_INT_10F_11F_11F_REV:
551 	case GL_UNSIGNED_INT_5_9_9_9_REV:
552 	case GL_UNSIGNED_INT_2_10_10_10_REV:
553 	case GL_UNSIGNED_INT_24_8_OES:;
554         retval = 4;
555         break;
556 	case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
557 		retval = 4 + 4;
558         break;
559     default:
560         ALOGE("**** ERROR unknown type 0x%x (%s,%d)\n", type, __FUNCTION__,__LINE__);
561         retval = 4;
562     }
563     return retval;
564 
565 }
566 
glUtilsParamSize(GLenum param)567 size_t glUtilsParamSize(GLenum param)
568 {
569     size_t s = 0;
570 
571     switch(param)
572     {
573     case GL_DEPTH_TEST:
574     case GL_DEPTH_FUNC:
575     case GL_DEPTH_BITS:
576     case GL_MAX_CLIP_PLANES:
577     case GL_MAX_COLOR_ATTACHMENTS:
578     case GL_MAX_DRAW_BUFFERS:
579     case GL_GREEN_BITS:
580     case GL_MAX_MODELVIEW_STACK_DEPTH:
581     case GL_MAX_PROJECTION_STACK_DEPTH:
582     case GL_MAX_TEXTURE_STACK_DEPTH:
583     case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
584     case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
585     case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
586     case GL_MAX_TEXTURE_SIZE:
587     case GL_TEXTURE_GEN_MODE_OES:
588     case GL_TEXTURE_ENV_MODE:
589     case GL_FOG_MODE:
590     case GL_FOG_DENSITY:
591     case GL_FOG_START:
592     case GL_FOG_END:
593     case GL_SPOT_EXPONENT:
594     case GL_CONSTANT_ATTENUATION:
595     case GL_LINEAR_ATTENUATION:
596     case GL_QUADRATIC_ATTENUATION:
597     case GL_SHININESS:
598     case GL_LIGHT_MODEL_TWO_SIDE:
599     case GL_POINT_SIZE:
600     case GL_POINT_SIZE_MIN:
601     case GL_POINT_SIZE_MAX:
602     case GL_POINT_FADE_THRESHOLD_SIZE:
603     case GL_CULL_FACE:
604     case GL_CULL_FACE_MODE:
605     case GL_FRONT_FACE:
606     case GL_SHADE_MODEL:
607     case GL_DEPTH_WRITEMASK:
608     case GL_DEPTH_CLEAR_VALUE:
609     case GL_STENCIL_FAIL:
610     case GL_STENCIL_PASS_DEPTH_FAIL:
611     case GL_STENCIL_PASS_DEPTH_PASS:
612     case GL_STENCIL_REF:
613     case GL_STENCIL_WRITEMASK:
614     case GL_MATRIX_MODE:
615     case GL_MODELVIEW_STACK_DEPTH:
616     case GL_PROJECTION_STACK_DEPTH:
617     case GL_TEXTURE_STACK_DEPTH:
618     case GL_ALPHA_TEST_FUNC:
619     case GL_ALPHA_TEST_REF:
620     case GL_ALPHA_TEST:
621     case GL_DITHER:
622     case GL_BLEND_DST:
623     case GL_BLEND_SRC:
624     case GL_BLEND:
625     case GL_LOGIC_OP_MODE:
626     case GL_SCISSOR_TEST:
627     case GL_MAX_TEXTURE_UNITS:
628     case GL_ACTIVE_TEXTURE:
629     case GL_ALPHA_BITS:
630     case GL_ARRAY_BUFFER_BINDING:
631     case GL_BLUE_BITS:
632     case GL_CLIENT_ACTIVE_TEXTURE:
633     case GL_CLIP_PLANE0:
634     case GL_CLIP_PLANE1:
635     case GL_CLIP_PLANE2:
636     case GL_CLIP_PLANE3:
637     case GL_CLIP_PLANE4:
638     case GL_CLIP_PLANE5:
639     case GL_COLOR_ARRAY:
640     case GL_COLOR_ARRAY_BUFFER_BINDING:
641     case GL_COLOR_ARRAY_SIZE:
642     case GL_COLOR_ARRAY_STRIDE:
643     case GL_COLOR_ARRAY_TYPE:
644     case GL_COLOR_LOGIC_OP:
645     case GL_COLOR_MATERIAL:
646     case GL_PACK_ALIGNMENT:
647     case GL_PERSPECTIVE_CORRECTION_HINT:
648     case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES:
649     case GL_POINT_SIZE_ARRAY_STRIDE_OES:
650     case GL_POINT_SIZE_ARRAY_TYPE_OES:
651     case GL_POINT_SMOOTH:
652     case GL_POINT_SMOOTH_HINT:
653     case GL_POINT_SPRITE_OES:
654     case GL_COORD_REPLACE_OES:
655     case GL_COMBINE_ALPHA:
656     case GL_SRC0_RGB:
657     case GL_SRC1_RGB:
658     case GL_SRC2_RGB:
659     case GL_OPERAND0_RGB:
660     case GL_OPERAND1_RGB:
661     case GL_OPERAND2_RGB:
662     case GL_SRC0_ALPHA:
663     case GL_SRC1_ALPHA:
664     case GL_SRC2_ALPHA:
665     case GL_OPERAND0_ALPHA:
666     case GL_OPERAND1_ALPHA:
667     case GL_OPERAND2_ALPHA:
668     case GL_RGB_SCALE:
669     case GL_ALPHA_SCALE:
670     case GL_COMBINE_RGB:
671     case GL_POLYGON_OFFSET_FACTOR:
672     case GL_POLYGON_OFFSET_FILL:
673     case GL_POLYGON_OFFSET_UNITS:
674     case GL_RED_BITS:
675     case GL_RESCALE_NORMAL:
676     case GL_SAMPLE_ALPHA_TO_COVERAGE:
677     case GL_SAMPLE_ALPHA_TO_ONE:
678     case GL_SAMPLE_BUFFERS:
679     case GL_SAMPLE_COVERAGE:
680     case GL_SAMPLE_COVERAGE_INVERT:
681     case GL_SAMPLE_COVERAGE_VALUE:
682     case GL_SAMPLES:
683     case GL_MAX_SAMPLES_EXT:
684     case GL_STENCIL_BITS:
685     case GL_STENCIL_CLEAR_VALUE:
686     case GL_STENCIL_FUNC:
687     case GL_STENCIL_TEST:
688     case GL_STENCIL_VALUE_MASK:
689     case GL_STENCIL_BACK_FUNC:
690     case GL_STENCIL_BACK_VALUE_MASK:
691     case GL_STENCIL_BACK_REF:
692     case GL_STENCIL_BACK_FAIL:
693     case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
694     case GL_STENCIL_BACK_PASS_DEPTH_PASS:
695     case GL_STENCIL_BACK_WRITEMASK:
696     case GL_TEXTURE_2D:
697     case GL_TEXTURE_BASE_LEVEL:
698     case GL_TEXTURE_BINDING_2D:
699     case GL_TEXTURE_BINDING_CUBE_MAP:
700     case GL_TEXTURE_BINDING_EXTERNAL_OES:
701     case GL_TEXTURE_COMPARE_FUNC:
702     case GL_TEXTURE_COMPARE_MODE:
703     case GL_TEXTURE_COORD_ARRAY:
704     case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING:
705     case GL_TEXTURE_COORD_ARRAY_SIZE:
706     case GL_TEXTURE_COORD_ARRAY_STRIDE:
707     case GL_TEXTURE_COORD_ARRAY_TYPE:
708     case GL_TEXTURE_IMMUTABLE_FORMAT:
709     case GL_UNPACK_ALIGNMENT:
710     case GL_VERTEX_ARRAY:
711     case GL_VERTEX_ARRAY_BUFFER_BINDING:
712     case GL_VERTEX_ARRAY_SIZE:
713     case GL_VERTEX_ARRAY_STRIDE:
714     case GL_VERTEX_ARRAY_TYPE:
715     case GL_SPOT_CUTOFF:
716     case GL_TEXTURE_MIN_FILTER:
717     case GL_TEXTURE_MAG_FILTER:
718     case GL_TEXTURE_MAX_LOD:
719     case GL_TEXTURE_MIN_LOD:
720     case GL_TEXTURE_WRAP_S:
721     case GL_TEXTURE_WRAP_T:
722     case GL_TEXTURE_WRAP_R:
723     case GL_TEXTURE_SWIZZLE_R:
724     case GL_TEXTURE_SWIZZLE_G:
725     case GL_TEXTURE_SWIZZLE_B:
726     case GL_TEXTURE_SWIZZLE_A:
727     case GL_GENERATE_MIPMAP:
728     case GL_GENERATE_MIPMAP_HINT:
729     case GL_RENDERBUFFER_WIDTH_OES:
730     case GL_RENDERBUFFER_HEIGHT_OES:
731     case GL_RENDERBUFFER_INTERNAL_FORMAT_OES:
732     case GL_RENDERBUFFER_RED_SIZE_OES:
733     case GL_RENDERBUFFER_GREEN_SIZE_OES:
734     case GL_RENDERBUFFER_BLUE_SIZE_OES:
735     case GL_RENDERBUFFER_ALPHA_SIZE_OES:
736     case GL_RENDERBUFFER_DEPTH_SIZE_OES:
737     case GL_RENDERBUFFER_STENCIL_SIZE_OES:
738     case GL_RENDERBUFFER_BINDING:
739     case GL_FRAMEBUFFER_BINDING:
740     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_OES:
741     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_OES:
742     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_OES:
743     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_OES:
744     case GL_FENCE_STATUS_NV:
745     case GL_FENCE_CONDITION_NV:
746     case GL_TEXTURE_WIDTH_QCOM:
747     case GL_TEXTURE_HEIGHT_QCOM:
748     case GL_TEXTURE_DEPTH_QCOM:
749     case GL_TEXTURE_INTERNAL_FORMAT_QCOM:
750     case GL_TEXTURE_FORMAT_QCOM:
751     case GL_TEXTURE_TYPE_QCOM:
752     case GL_TEXTURE_IMAGE_VALID_QCOM:
753     case GL_TEXTURE_NUM_LEVELS_QCOM:
754     case GL_TEXTURE_TARGET_QCOM:
755     case GL_TEXTURE_OBJECT_VALID_QCOM:
756     case GL_BLEND_EQUATION_RGB_OES:
757     case GL_BLEND_EQUATION_ALPHA_OES:
758     case GL_BLEND_DST_RGB_OES:
759     case GL_BLEND_SRC_RGB_OES:
760     case GL_BLEND_DST_ALPHA_OES:
761     case GL_BLEND_SRC_ALPHA_OES:
762     case GL_MAX_LIGHTS:
763     case GL_SHADER_TYPE:
764     case GL_COMPILE_STATUS:
765     case GL_SHADER_SOURCE_LENGTH:
766     case GL_CURRENT_PROGRAM:
767     case GL_SUBPIXEL_BITS:
768     case GL_MAX_3D_TEXTURE_SIZE:
769     case GL_MAX_ARRAY_TEXTURE_LAYERS:
770     case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
771     case GL_NUM_SHADER_BINARY_FORMATS:
772     case GL_SHADER_COMPILER:
773     case GL_MAX_VERTEX_ATTRIBS:
774     case GL_MAX_VERTEX_UNIFORM_VECTORS:
775     case GL_MAX_VARYING_VECTORS:
776     case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
777     case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
778     case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
779     case GL_MAX_RENDERBUFFER_SIZE:
780     case GL_MAX_TEXTURE_IMAGE_UNITS:
781     case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
782     case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
783     case GL_LINE_WIDTH:
784     case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
785     case GL_MAX_UNIFORM_BUFFER_BINDINGS:
786     case GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS:
787     case GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS:
788     case GL_UNIFORM_BLOCK_BINDING:
789     case GL_UNIFORM_BLOCK_DATA_SIZE:
790     case GL_UNIFORM_BLOCK_NAME_LENGTH:
791     case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
792     case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
793     case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
794     case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT:
795     case GL_CURRENT_QUERY:
796     case GL_QUERY_RESULT:
797     case GL_QUERY_RESULT_AVAILABLE:
798     case GL_READ_BUFFER:
799     case GL_NUM_PROGRAM_BINARY_FORMATS:
800     case GL_PROGRAM_BINARY_FORMATS:
801 
802     case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
803     case GL_ACTIVE_ATTRIBUTES:
804     case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
805     case GL_ACTIVE_UNIFORM_BLOCKS:
806     case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH:
807     case GL_ACTIVE_UNIFORMS:
808     case GL_ACTIVE_UNIFORM_MAX_LENGTH:
809     case GL_ATTACHED_SHADERS:
810     case GL_DELETE_STATUS:
811     case GL_INFO_LOG_LENGTH:
812     case GL_LINK_STATUS:
813     case GL_PROGRAM_BINARY_LENGTH:
814     case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
815     case GL_PROGRAM_SEPARABLE:
816     case GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT:
817     case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
818     case GL_TRANSFORM_FEEDBACK_VARYINGS:
819     case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH:
820     case GL_TRANSFORM_FEEDBACK_ACTIVE:
821 #define GL_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS 0x8C8A
822 #define GL_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS 0x8C80
823     case GL_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS:
824     case GL_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS:
825     case GL_VALIDATE_STATUS:
826         s = 1;
827         break;
828     case GL_ALIASED_LINE_WIDTH_RANGE:
829     case GL_ALIASED_POINT_SIZE_RANGE:
830     case GL_DEPTH_RANGE:
831     case GL_MAX_VIEWPORT_DIMS:
832     case GL_SMOOTH_POINT_SIZE_RANGE:
833     case GL_SMOOTH_LINE_WIDTH_RANGE:
834     case GL_SAMPLE_POSITION:
835         s= 2;
836         break;
837     case GL_SPOT_DIRECTION:
838     case GL_POINT_DISTANCE_ATTENUATION:
839     case GL_CURRENT_NORMAL:
840     case GL_COMPUTE_WORK_GROUP_SIZE:
841         s =  3;
842         break;
843     case GL_CURRENT_VERTEX_ATTRIB:
844     case GL_CURRENT_TEXTURE_COORDS:
845     case GL_CURRENT_COLOR:
846     case GL_FOG_COLOR:
847     case GL_AMBIENT:
848     case GL_DIFFUSE:
849     case GL_SPECULAR:
850     case GL_EMISSION:
851     case GL_POSITION:
852     case GL_LIGHT_MODEL_AMBIENT:
853     case GL_TEXTURE_ENV_COLOR:
854     case GL_SCISSOR_BOX:
855     case GL_VIEWPORT:
856     case GL_TEXTURE_CROP_RECT_OES:
857     case GL_COLOR_CLEAR_VALUE:
858     case GL_COLOR_WRITEMASK:
859     case GL_AMBIENT_AND_DIFFUSE:
860     case GL_BLEND_COLOR:
861         s =  4;
862         break;
863     case GL_MODELVIEW_MATRIX:
864     case GL_PROJECTION_MATRIX:
865     case GL_TEXTURE_MATRIX:
866         s = 16;
867     break;
868     default:
869         s = 1; // assume 1 for everything else
870     }
871     return s;
872 }
873 
glUtilsPackPointerData(unsigned char * dst,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)874 void glUtilsPackPointerData(unsigned char *dst, unsigned char *src,
875                      int size, GLenum type, unsigned int stride,
876                      unsigned int datalen)
877 {
878     unsigned int  vsize = size * glSizeof(type);
879     switch (type) {
880     case GL_INT_2_10_10_10_REV:
881     case GL_UNSIGNED_INT_2_10_10_10_REV:
882         vsize = vsize / 4;
883         break;
884     default:
885         break;
886     }
887 
888     if (stride == 0) stride = vsize;
889 
890     if (stride == vsize) {
891         memcpy(dst, src, datalen);
892     } else {
893         for (unsigned int i = 0; i < datalen; i += vsize) {
894             memcpy(dst, src, vsize);
895             dst += vsize;
896             src += stride;
897         }
898     }
899 }
900 
glUtilsWritePackPointerData(void * _stream,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)901 void glUtilsWritePackPointerData(void* _stream, unsigned char *src,
902                                  int size, GLenum type, unsigned int stride,
903                                  unsigned int datalen)
904 {
905     IOStream* stream = reinterpret_cast<IOStream*>(_stream);
906 
907     unsigned int  vsize = size * glSizeof(type);
908     if (stride == 0) stride = vsize;
909 
910     if (stride == vsize) {
911         stream->writeFully(src, datalen);
912     } else {
913         for (unsigned int i = 0; i < datalen; i += vsize) {
914             stream->writeFully(src, (size_t)vsize);
915             src += stride;
916         }
917     }
918 }
919 
920 #ifndef GL_RGBA16F
921 #define GL_RGBA16F                        0x881A
922 #endif // GL_RGBA16F
923 
glUtilsPixelBitSize(GLenum format,GLenum type)924 int glUtilsPixelBitSize(GLenum format, GLenum type)
925 {
926     int components = 0;
927     int componentsize = 0;
928     int pixelsize = 0;
929     switch(type) {
930     case GL_BYTE:
931     case GL_UNSIGNED_BYTE:
932         componentsize = 8;
933         break;
934     case GL_SHORT:
935     case GL_HALF_FLOAT:
936     case GL_UNSIGNED_SHORT:
937         componentsize = 16;
938         break;
939     case GL_INT:
940     case GL_UNSIGNED_INT:
941     case GL_FLOAT:
942     case GL_FIXED:
943         componentsize = 32;
944         break;
945     case GL_UNSIGNED_SHORT_5_6_5:
946     case GL_UNSIGNED_SHORT_4_4_4_4:
947     case GL_UNSIGNED_SHORT_5_5_5_1:
948     case GL_RGB565_OES:
949     case GL_RGB5_A1_OES:
950     case GL_RGBA4_OES:
951         pixelsize = 16;
952         break;
953     case GL_UNSIGNED_INT_2_10_10_10_REV:
954     case GL_UNSIGNED_INT_24_8_OES:
955         pixelsize = 32;
956         break;
957     default:
958         ERR("glUtilsPixelBitSize: unknown pixel type %d - assuming pixel data 0\n", type);
959         componentsize = 0;
960     }
961 
962     if (pixelsize == 0) {
963         switch(format) {
964 #if 0
965         case GL_RED:
966         case GL_GREEN:
967         case GL_BLUE:
968 #endif
969         case GL_ALPHA:
970         case GL_LUMINANCE:
971         case GL_DEPTH_COMPONENT:
972         case GL_DEPTH_STENCIL_OES:
973             components = 1;
974             break;
975         case GL_LUMINANCE_ALPHA:
976             components = 2;
977             break;
978         case GL_RGB:
979 #if 0
980         case GL_BGR:
981 #endif
982             components = 3;
983             break;
984         case GL_RGBA:
985         case GL_BGRA_EXT:
986             components = 4;
987             break;
988         case GL_RGBA16F:
989             pixelsize = 64;
990             break;
991         default:
992             ERR("glUtilsPixelBitSize: unknown pixel format %d\n", format);
993             components = 0;
994         }
995         if (pixelsize == 0) {
996             pixelsize = components * componentsize;
997         }
998     }
999 
1000     return pixelsize;
1001 }
1002 
1003 // pack a list of strings into one.
glUtilsPackStrings(char * ptr,char ** strings,GLint * length,GLsizei count)1004 void glUtilsPackStrings(char *ptr,  char **strings,  GLint *length, GLsizei count)
1005 {
1006     char *p = ptr;
1007     *p = '\0';
1008     for (int i = 0; i < count; i++) {
1009         int l=0;
1010         if (strings[i]!=NULL) {
1011             if (length == NULL || length[i] < 0) {
1012                 l = strlen(strings[i]);
1013                 strcat(p, strings[i]);
1014             } else {
1015                 l = length[i];
1016                 strncat(p, strings[i], l);
1017             }
1018         }
1019         p += l;
1020     }
1021 }
1022 
1023 // claculate the length of a list of strings
glUtilsCalcShaderSourceLen(char ** strings,GLint * length,GLsizei count)1024 int glUtilsCalcShaderSourceLen( char **strings,  GLint *length, GLsizei count)
1025 {
1026     int len = 0;
1027     for (int i = 0; i < count; i++) {
1028         int l;
1029         if (length == NULL || length[i] < 0) {
1030             l = strings[i]!=NULL ? strlen(strings[i]) : 0;
1031         } else {
1032             l = length[i];
1033         }
1034         len += l;
1035     }
1036     return len;
1037 
1038 }
1039 
1040 // helper to get GL_COLOR_ATTACHMENTn names
glUtilsColorAttachmentName(int i)1041 GLenum glUtilsColorAttachmentName(int i) {
1042 #undef COLOR_ATTACHMENT_CASE
1043 #define COLOR_ATTACHMENT_CASE(i) \
1044     case i: \
1045         return GL_COLOR_ATTACHMENT##i; \
1046 
1047     switch (i) {
1048         COLOR_ATTACHMENT_CASE(0)
1049         COLOR_ATTACHMENT_CASE(1)
1050         COLOR_ATTACHMENT_CASE(2)
1051         COLOR_ATTACHMENT_CASE(3)
1052         COLOR_ATTACHMENT_CASE(4)
1053         COLOR_ATTACHMENT_CASE(5)
1054         COLOR_ATTACHMENT_CASE(6)
1055         COLOR_ATTACHMENT_CASE(7)
1056         COLOR_ATTACHMENT_CASE(8)
1057         COLOR_ATTACHMENT_CASE(9)
1058         COLOR_ATTACHMENT_CASE(10)
1059         COLOR_ATTACHMENT_CASE(11)
1060         COLOR_ATTACHMENT_CASE(12)
1061         COLOR_ATTACHMENT_CASE(13)
1062         COLOR_ATTACHMENT_CASE(14)
1063         COLOR_ATTACHMENT_CASE(15)
1064     }
1065     return GL_NONE;
1066 #undef COLOR_ATTACHMENT_CASE
1067 }
1068 
glUtilsColorAttachmentIndex(GLenum attachment)1069 int glUtilsColorAttachmentIndex(GLenum attachment) {
1070 #undef COLOR_ATTACHMENT_CASE
1071 #define COLOR_ATTACHMENT_CASE(i) \
1072     case GL_COLOR_ATTACHMENT##i: \
1073         return i; \
1074 
1075     switch (attachment) {
1076         COLOR_ATTACHMENT_CASE(0)
1077         COLOR_ATTACHMENT_CASE(1)
1078         COLOR_ATTACHMENT_CASE(2)
1079         COLOR_ATTACHMENT_CASE(3)
1080         COLOR_ATTACHMENT_CASE(4)
1081         COLOR_ATTACHMENT_CASE(5)
1082         COLOR_ATTACHMENT_CASE(6)
1083         COLOR_ATTACHMENT_CASE(7)
1084         COLOR_ATTACHMENT_CASE(8)
1085         COLOR_ATTACHMENT_CASE(9)
1086         COLOR_ATTACHMENT_CASE(10)
1087         COLOR_ATTACHMENT_CASE(11)
1088         COLOR_ATTACHMENT_CASE(12)
1089         COLOR_ATTACHMENT_CASE(13)
1090         COLOR_ATTACHMENT_CASE(14)
1091         COLOR_ATTACHMENT_CASE(15)
1092     }
1093     return -1;
1094 #undef COLOR_ATTACHMENT_CASE
1095 }
1096 
1097 struct glUtilsDrawArraysIndirectCommand {
1098     GLuint count;
1099     GLuint primCount;
1100     GLuint first;
1101     GLuint reserved;
1102 };
1103 
1104 struct glUtilsDrawElementsIndirectCommand {
1105     GLuint count;
1106     GLuint primCount;
1107     GLuint first;
1108     GLint baseVertex;
1109     GLuint reserved;
1110 };
1111 
glUtilsIndirectStructSize(IndirectCommandType cmdType)1112 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType) {
1113     switch (cmdType) {
1114     case INDIRECT_COMMAND_DRAWARRAYS:
1115         return sizeof(glUtilsDrawArraysIndirectCommand);
1116     case INDIRECT_COMMAND_DRAWELEMENTS:
1117         return sizeof(glUtilsDrawElementsIndirectCommand);
1118     }
1119     return 4;
1120 }
1121 
colorRenderableFormat(GLint internalformat,GLenum texturetype,int majorVersion,int minorVersion,bool hasColorBufferFloatExtension,bool hasColorBufferHalfFloatExtension)1122 bool colorRenderableFormat(GLint internalformat, GLenum texturetype, int majorVersion, __attribute__((unused)) int minorVersion, bool hasColorBufferFloatExtension, bool hasColorBufferHalfFloatExtension) {
1123     switch (internalformat) {
1124         case GL_RGB:
1125         case GL_RGBA:
1126         case GL_BGRA_EXT:
1127             switch (texturetype) {
1128                 case GL_FLOAT:
1129                 case GL_HALF_FLOAT_OES:
1130                 case GL_UNSIGNED_INT_10F_11F_11F_REV:
1131                 case GL_UNSIGNED_INT_2_10_10_10_REV:
1132                     return false;
1133                 default:
1134                     return true;
1135             }
1136             break;
1137         case GL_R8:
1138         case GL_RG8:
1139         case GL_RGB8:
1140         case GL_RGB565:
1141         case GL_RGBA4:
1142         case GL_RGB5_A1:
1143         case GL_RGBA8:
1144         case GL_RGB10_A2:
1145         case GL_RGB10_A2UI:
1146         case GL_SRGB8_ALPHA8:
1147         case GL_R8I:
1148         case GL_R8UI:
1149         case GL_R16I:
1150         case GL_R16UI:
1151         case GL_R32I:
1152         case GL_R32UI:
1153         case GL_RG8I:
1154         case GL_RG8UI:
1155         case GL_RG16I:
1156         case GL_RG16UI:
1157         case GL_RG32I:
1158         case GL_RG32UI:
1159         case GL_RGBA8I:
1160         case GL_RGBA8UI:
1161         case GL_RGBA16I:
1162         case GL_RGBA16UI:
1163         case GL_RGBA32I:
1164         case GL_RGBA32UI:
1165         case GL_BGRA8_EXT:
1166             return true;
1167         case GL_R16F:
1168         case GL_RG16F:
1169         case GL_RGBA16F:
1170         case GL_R32F:
1171         case GL_RG32F:
1172         case GL_RGBA32F:
1173         case GL_R11F_G11F_B10F:
1174             return majorVersion >= 3 && hasColorBufferFloatExtension;
1175         case GL_RGB16F:
1176             return majorVersion >= 3 && hasColorBufferHalfFloatExtension;
1177         default:
1178             return false;
1179     }
1180 }
1181 
depthRenderableFormat(GLint internalformat)1182 bool depthRenderableFormat(GLint internalformat) {
1183     switch (internalformat) {
1184         case GL_DEPTH_STENCIL:
1185         case GL_DEPTH_COMPONENT:
1186         case GL_DEPTH_COMPONENT16:
1187         case GL_DEPTH_COMPONENT24:
1188         case GL_DEPTH_COMPONENT32F:
1189         case GL_DEPTH24_STENCIL8:
1190         case GL_DEPTH32F_STENCIL8:
1191         case 0X81A7: // GL_DEPTH_COMPONENT32
1192             return true;
1193         default:
1194             return false;
1195     }
1196 }
1197 
stencilRenderableFormat(GLint internalformat)1198 bool stencilRenderableFormat(GLint internalformat) {
1199     switch (internalformat) {
1200         case GL_STENCIL_INDEX:
1201         case GL_DEPTH_STENCIL:
1202         case GL_STENCIL_INDEX8:
1203         case GL_DEPTH24_STENCIL8:
1204         case GL_DEPTH32F_STENCIL8:
1205             return true;
1206         default:
1207             return false;
1208     }
1209 }
1210