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 #ifndef __GL_UTILS_H__
17 #define __GL_UTILS_H__
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #ifdef GL_API
23     #undef GL_API
24 #endif
25 #define GL_API
26 
27 #ifdef GL_APIENTRY
28     #undef GL_APIENTRY
29 #endif
30 
31 #ifdef GL_APIENTRYP
32     #undef GL_APIENTRYP
33 #endif
34 #define GL_APIENTRYP
35 
36 #ifndef ANDROID
37 #define GL_APIENTRY
38 #endif
39 
40 #include <GLES/gl.h>
41 #include <GLES/glext.h>
42 #include <GLES2/gl2.h>
43 #include <GLES2/gl2ext.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 typedef enum {
50     INDIRECT_COMMAND_DRAWARRAYS = 0,
51     INDIRECT_COMMAND_DRAWELEMENTS = 1,
52 } IndirectCommandType;
53 
54     size_t glSizeof(GLenum type);
55     size_t glUtilsParamSize(GLenum param);
56     void   glUtilsPackPointerData(unsigned char *dst, unsigned char *str,
57                            int size, GLenum type, unsigned int stride,
58                            unsigned int datalen);
59     void glUtilsWritePackPointerData(void* stream, unsigned char *src,
60                                     int size, GLenum type, unsigned int stride,
61                                     unsigned int datalen);
62     int glUtilsPixelBitSize(GLenum format, GLenum type);
63     void   glUtilsPackStrings(char *ptr, char **strings, GLint *length, GLsizei count);
64     int glUtilsCalcShaderSourceLen(char **strings, GLint *length, GLsizei count);
65     GLenum glUtilsColorAttachmentName(int i);
66     int glUtilsColorAttachmentIndex(GLenum attachment);
67 
68     GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType);
69 
70 #ifdef __cplusplus
71 };
72 #endif
73 
74 namespace GLUtils {
75 
minmax(const T * indices,int count,int * min,int * max)76     template <class T> void minmax(const T *indices, int count, int *min, int *max) {
77         *min = -1;
78         *max = -1;
79         const T *ptr = indices;
80         for (int i = 0; i < count; i++) {
81             if (*min == -1 || *ptr < *min) *min = *ptr;
82             if (*max == -1 || *ptr > *max) *max = *ptr;
83             ptr++;
84         }
85     }
86 
minmaxExcept(const T * indices,int count,int * min,int * max,bool shouldExclude,T whatExclude)87     template <class T> void minmaxExcept
88         (const T *indices, int count, int *min, int *max,
89          bool shouldExclude, T whatExclude) {
90 
91         if (!shouldExclude) return minmax(indices, count, min, max);
92 
93         *min = -1;
94         *max = -1;
95         const T *ptr = indices;
96         for (int i = 0; i < count; i++) {
97             if (*ptr != whatExclude) {
98                 if (*min == -1 || *ptr < *min) *min = *ptr;
99                 if (*max == -1 || *ptr > *max) *max = *ptr;
100             }
101             ptr++;
102         }
103     }
104 
shiftIndices(T * indices,int count,int offset)105     template <class T> void shiftIndices(T *indices, int count,  int offset) {
106         T *ptr = indices;
107         for (int i = 0; i < count; i++) {
108             *ptr += offset;
109             ptr++;
110         }
111     }
112 
113 
shiftIndices(const T * src,T * dst,int count,int offset)114     template <class T> void shiftIndices(const T *src, T *dst, int count, int offset)
115     {
116         for (int i = 0; i < count; i++) {
117             *dst = *src + offset;
118             dst++;
119             src++;
120         }
121     }
122 
shiftIndicesExcept(T * indices,int count,int offset,bool shouldExclude,T whatExclude)123     template <class T> void shiftIndicesExcept
124         (T *indices, int count, int offset,
125          bool shouldExclude, T whatExclude) {
126 
127         if (!shouldExclude) return shiftIndices(indices, count, offset);
128 
129         T *ptr = indices;
130         for (int i = 0; i < count; i++) {
131             if (*ptr != whatExclude) {
132                 *ptr += offset;
133             }
134             ptr++;
135         }
136     }
137 
shiftIndicesExcept(const T * src,T * dst,int count,int offset,bool shouldExclude,T whatExclude)138     template <class T> void shiftIndicesExcept
139         (const T *src, T *dst, int count, int offset,
140          bool shouldExclude, T whatExclude) {
141 
142         if (!shouldExclude) return shiftIndices(src, dst, count, offset);
143 
144         for (int i = 0; i < count; i++) {
145             if (*src == whatExclude) {
146                 *dst = *src;
147             } else {
148                 *dst = *src + offset;
149             }
150             dst++;
151             src++;
152         }
153     }
154 
primitiveRestartIndex()155     template<class T> T primitiveRestartIndex() {
156         return -1;
157     }
158 
159 }; // namespace GLUtils
160 #endif
161