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