• 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 <GLcommon/GLutils.h>
17 
18 #include "base/Lock.h"
19 
20 #include <GLES/gl.h>
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 #include <GLES3/gl3.h>
24 #include <GLES3/gl31.h>
25 #include <GLES3/gl3ext.h>
26 
isPowerOf2(int num)27 bool isPowerOf2(int num) {
28     return (num & (num -1)) == 0;
29 }
30 
31 android::base::StaticLock sGlobalSettingsLock;
32 
33 static uint32_t s_gles2Gles = 0;
34 static uint32_t s_coreProfile = 0;
35 
setGles2Gles(bool isGles2Gles)36 void setGles2Gles(bool isGles2Gles) {
37     __atomic_store_n(&s_gles2Gles, isGles2Gles ? 1 : 0, __ATOMIC_RELAXED);
38 }
39 
isGles2Gles()40 bool isGles2Gles() {
41     return __atomic_load_n(&s_gles2Gles, __ATOMIC_RELAXED) != 0;
42 }
43 
setCoreProfile(bool isCore)44 void setCoreProfile(bool isCore) {
45     __atomic_store_n(&s_coreProfile, isCore ? 1 : 0, __ATOMIC_RELAXED);
46 }
47 
isCoreProfile()48 bool isCoreProfile() {
49     return __atomic_load_n(&s_coreProfile, __ATOMIC_RELAXED) != 0;
50 }
51 
glFormatToChannelBits(GLenum colorFormat,GLenum depthFormat,GLenum stencilFormat)52 FramebufferChannelBits glFormatToChannelBits(GLenum colorFormat,
53                                              GLenum depthFormat,
54                                              GLenum stencilFormat) {
55     FramebufferChannelBits res = {};
56 
57 #define COLOR_BITS_CASE(format, r, g, b, a) \
58     case format: res.red = r; res.green = g; res.blue = b; res.alpha = a; break; \
59 
60     switch (colorFormat) {
61     COLOR_BITS_CASE(GL_ALPHA, 0, 0, 0, 8)
62     COLOR_BITS_CASE(GL_LUMINANCE, 0, 0, 0, 0)
63     COLOR_BITS_CASE(GL_LUMINANCE_ALPHA, 0, 0, 0, 8)
64     COLOR_BITS_CASE(GL_RGB, 8, 8, 8, 0)
65     COLOR_BITS_CASE(GL_RGBA, 8, 8, 8, 8)
66     COLOR_BITS_CASE(GL_R11F_G11F_B10F, 11, 11, 10, 0)
67     COLOR_BITS_CASE(GL_R16F, 16, 0, 0, 0)
68     COLOR_BITS_CASE(GL_R16I, 16, 0, 0, 0)
69     COLOR_BITS_CASE(GL_R16UI, 16, 0, 0, 0)
70     COLOR_BITS_CASE(GL_R32F, 32, 0, 0, 0)
71     COLOR_BITS_CASE(GL_R32I, 32, 0, 0, 0)
72     COLOR_BITS_CASE(GL_R32UI, 32, 0, 0, 0)
73     COLOR_BITS_CASE(GL_R8, 8, 0, 0, 0)
74     COLOR_BITS_CASE(GL_R8I, 8, 0, 0, 0)
75     COLOR_BITS_CASE(GL_R8_SNORM, 8, 0, 0, 0)
76     COLOR_BITS_CASE(GL_R8UI, 8, 0, 0, 0)
77     COLOR_BITS_CASE(GL_RG16F, 16, 0, 0, 0)
78     COLOR_BITS_CASE(GL_RG16I, 16, 0, 0, 0)
79     COLOR_BITS_CASE(GL_RG16UI, 16, 0, 0, 0)
80     COLOR_BITS_CASE(GL_RG32F, 32, 0, 0, 0)
81     COLOR_BITS_CASE(GL_RG32I, 32, 0, 0, 0)
82     COLOR_BITS_CASE(GL_RG32UI, 32, 0, 0, 0)
83     COLOR_BITS_CASE(GL_RG8, 8, 8, 0, 0)
84     COLOR_BITS_CASE(GL_RG8I, 8, 8, 0, 0)
85     COLOR_BITS_CASE(GL_RG8_SNORM, 8, 8, 0, 0)
86     COLOR_BITS_CASE(GL_RG8UI, 8, 8, 0, 0)
87     COLOR_BITS_CASE(GL_RGB10_A2, 10, 10, 10, 2)
88     COLOR_BITS_CASE(GL_RGB10_A2UI, 10, 10, 10, 2)
89     COLOR_BITS_CASE(GL_RGB16F, 16, 16, 16, 0)
90     COLOR_BITS_CASE(GL_RGB16I, 16, 16, 16, 0)
91     COLOR_BITS_CASE(GL_RGB16UI, 16, 16, 16, 0)
92     COLOR_BITS_CASE(GL_RGB32F, 32, 32, 32, 0)
93     COLOR_BITS_CASE(GL_RGB32I, 32, 32, 32, 0)
94     COLOR_BITS_CASE(GL_RGB32UI, 32, 32, 32, 0)
95     COLOR_BITS_CASE(GL_RGB565, 5, 6, 5, 0)
96     COLOR_BITS_CASE(GL_RGB5_A1, 5, 5, 5, 1)
97     COLOR_BITS_CASE(GL_RGB8, 8, 8, 8, 0)
98     COLOR_BITS_CASE(GL_RGB8I, 8, 8, 8, 0)
99     COLOR_BITS_CASE(GL_RGB8_SNORM, 8, 8, 8, 0)
100     COLOR_BITS_CASE(GL_RGB8UI, 8, 8, 8, 0)
101     COLOR_BITS_CASE(GL_RGB9_E5, 9, 9, 9, 0)
102     COLOR_BITS_CASE(GL_RGBA16F, 16, 16, 16, 16)
103     COLOR_BITS_CASE(GL_RGBA16I, 16, 16, 16, 16)
104     COLOR_BITS_CASE(GL_RGBA16UI, 16, 16, 16, 16)
105     COLOR_BITS_CASE(GL_RGBA32F, 32, 32, 32, 32)
106     COLOR_BITS_CASE(GL_RGBA32I, 32, 32, 32, 32)
107     COLOR_BITS_CASE(GL_RGBA32UI, 32, 32, 32, 32)
108     COLOR_BITS_CASE(GL_RGBA4, 4, 4, 4, 4)
109     COLOR_BITS_CASE(GL_RGBA8, 8, 8, 8, 8)
110     COLOR_BITS_CASE(GL_RGBA8I, 8, 8, 8, 8)
111     COLOR_BITS_CASE(GL_RGBA8_SNORM, 8, 8, 8, 8)
112     COLOR_BITS_CASE(GL_RGBA8UI, 8, 8, 8, 8)
113     COLOR_BITS_CASE(GL_SRGB8, 8, 8, 8, 0)
114     COLOR_BITS_CASE(GL_SRGB8_ALPHA8, 8, 8, 8, 8)
115     }
116 
117 #define DEPTH_BITS_CASE(format, d) \
118     case format: res.depth = d; break; \
119 
120     switch (depthFormat) {
121     DEPTH_BITS_CASE(GL_DEPTH_COMPONENT16, 16)
122     DEPTH_BITS_CASE(GL_DEPTH_COMPONENT24, 24)
123     DEPTH_BITS_CASE(GL_DEPTH_COMPONENT32F, 32)
124     DEPTH_BITS_CASE(GL_DEPTH24_STENCIL8, 24)
125     DEPTH_BITS_CASE(GL_DEPTH32F_STENCIL8, 32)
126     DEPTH_BITS_CASE(GL_STENCIL_INDEX8, 0)
127     }
128 
129 #define STENCIL_BITS_CASE(format, d) \
130     case format: res.stencil = d; break; \
131 
132     switch (stencilFormat) {
133     STENCIL_BITS_CASE(GL_DEPTH_COMPONENT16, 0)
134     STENCIL_BITS_CASE(GL_DEPTH_COMPONENT24, 0)
135     STENCIL_BITS_CASE(GL_DEPTH_COMPONENT32F, 0)
136     STENCIL_BITS_CASE(GL_DEPTH24_STENCIL8, 8)
137     STENCIL_BITS_CASE(GL_DEPTH32F_STENCIL8, 8)
138     STENCIL_BITS_CASE(GL_STENCIL_INDEX8, 8)
139     }
140 
141     return res;
142 }
143 
baseFormatOfInternalFormat(GLint internalformat)144 GLenum baseFormatOfInternalFormat(GLint internalformat) {
145 
146     switch (internalformat) {
147     case GL_ALPHA:
148     case GL_LUMINANCE:
149     case GL_LUMINANCE_ALPHA:
150     case GL_RGB:
151     case GL_RGBA:
152         return internalformat;
153     case GL_R8:
154     case GL_R8_SNORM:
155     case GL_R16F:
156     case GL_R32F:
157         return GL_RED;
158     case GL_R8I:
159     case GL_R8UI:
160     case GL_R16I:
161     case GL_R16UI:
162     case GL_R32I:
163     case GL_R32UI:
164         return GL_RED_INTEGER;
165     case GL_RG8:
166     case GL_RG8_SNORM:
167     case GL_RG16F:
168     case GL_RG32F:
169         return GL_RG;
170     case GL_RG8I:
171     case GL_RG8UI:
172     case GL_RG16I:
173     case GL_RG16UI:
174     case GL_RG32I:
175     case GL_RG32UI:
176         return GL_RG_INTEGER;
177     case GL_RGB565:
178     case GL_RGB8:
179     case GL_SRGB8:
180     case GL_RGB8_SNORM:
181     case GL_RGB9_E5:
182     case GL_R11F_G11F_B10F:
183     case GL_RGB16F:
184     case GL_RGB32F:
185         return GL_RGB;
186     case GL_RGB8I:
187     case GL_RGB8UI:
188     case GL_RGB16I:
189     case GL_RGB16UI:
190     case GL_RGB32I:
191     case GL_RGB32UI:
192         return GL_RGB_INTEGER;
193     case GL_RGBA4:
194     case GL_RGB5_A1:
195     case GL_RGBA8:
196     case GL_SRGB8_ALPHA8:
197     case GL_RGBA8_SNORM:
198     case GL_RGB10_A2:
199     case GL_RGBA16F:
200     case GL_RGBA32F:
201         return GL_RGBA;
202     case GL_RGBA8I:
203     case GL_RGBA8UI:
204     case GL_RGB10_A2UI:
205     case GL_RGBA16I:
206     case GL_RGBA16UI:
207     case GL_RGBA32I:
208     case GL_RGBA32UI:
209         return GL_RGBA_INTEGER;
210     case GL_DEPTH_COMPONENT16:
211     case GL_DEPTH_COMPONENT24:
212     case GL_DEPTH_COMPONENT32F:
213         return GL_DEPTH_COMPONENT;
214     case GL_DEPTH24_STENCIL8:
215     case GL_DEPTH32F_STENCIL8:
216         return GL_DEPTH_STENCIL;
217     case GL_STENCIL_INDEX8:
218         return GL_STENCIL;
219     }
220 
221     fprintf(stderr, "%s: warning: unrecognized internal format 0x%x\n", __func__,
222             internalformat);
223     return internalformat;
224 }
225 
accurateTypeOfInternalFormat(GLint internalformat)226 GLenum accurateTypeOfInternalFormat(GLint internalformat) {
227     switch (internalformat) {
228     case GL_ALPHA:
229     case GL_LUMINANCE:
230     case GL_LUMINANCE_ALPHA:
231     case GL_RGB:
232     case GL_RGBA:
233     case GL_R8:
234     case GL_R8_SNORM:
235     case GL_RG8:
236     case GL_RG8_SNORM:
237     case GL_RGB8:
238     case GL_SRGB8:
239     case GL_RGB8_SNORM:
240     case GL_RGBA8:
241     case GL_SRGB8_ALPHA8:
242     case GL_RGBA8_SNORM:
243     case GL_R8UI:
244     case GL_RG8UI:
245     case GL_RGB8UI:
246     case GL_RGBA8UI:
247         return GL_UNSIGNED_BYTE;
248     case GL_R8I:
249     case GL_RG8I:
250     case GL_RGB8I:
251     case GL_RGBA8I:
252         return GL_BYTE;
253     case GL_R16UI:
254     case GL_RG16UI:
255     case GL_RGB16UI:
256     case GL_RGBA16UI:
257     case GL_DEPTH_COMPONENT16:
258         return GL_UNSIGNED_SHORT;
259     case GL_R16I:
260     case GL_RG16I:
261     case GL_RGB16I:
262     case GL_RGBA16I:
263         return GL_SHORT;
264     case GL_R32UI:
265     case GL_RG32UI:
266     case GL_RGB32UI:
267     case GL_RGBA32UI:
268     case GL_DEPTH_COMPONENT24:
269         return GL_UNSIGNED_INT;
270     case GL_R32I:
271     case GL_RG32I:
272     case GL_RGB32I:
273     case GL_RGBA32I:
274         return GL_INT;
275     case GL_R16F:
276     case GL_RG16F:
277     case GL_RGB16F:
278     case GL_RGBA16F:
279         return GL_HALF_FLOAT;
280     case GL_R32F:
281     case GL_RG32F:
282     case GL_RGB32F:
283     case GL_RGBA32F:
284     case GL_DEPTH_COMPONENT32F:
285         return GL_FLOAT;
286     case GL_DEPTH24_STENCIL8:
287         return GL_UNSIGNED_INT_24_8;
288     case GL_DEPTH32F_STENCIL8:
289         return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
290     case GL_RGB565:
291         return GL_UNSIGNED_SHORT_5_6_5;
292     case GL_RGB9_E5:
293         return GL_UNSIGNED_INT_5_9_9_9_REV;
294     case GL_R11F_G11F_B10F:
295         return GL_UNSIGNED_INT_10F_11F_11F_REV;
296     case GL_RGBA4:
297         return GL_UNSIGNED_SHORT_4_4_4_4;
298     case GL_RGB5_A1:
299         return GL_UNSIGNED_SHORT_5_5_5_1;
300     case GL_RGB10_A2:
301     case GL_RGB10_A2UI:
302         return GL_UNSIGNED_INT_2_10_10_10_REV;
303     case GL_STENCIL_INDEX8:
304         return GL_UNSIGNED_BYTE;
305     }
306 
307     fprintf(stderr, "%s: warning: unrecognized internal format 0x%x\n", __func__,
308             internalformat);
309     return GL_UNSIGNED_BYTE;
310 }
311