2 * OpenGL ES 2.0 memory performance estimator
3 * Copyright (C) 2009 Nokia
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * \author Sami Kyöstilä <sami.kyostila@nokia.com>
21 * EGL and OpenGL ES utility functions
25 #include <sys/types.h>
28 #include <GLES2/gl2ext.h>
33 eglSwapBuffers(ctx.dpy, ctx.surface);
36 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
40 bool loadRawTexture(GLenum target, int level, GLenum internalFormat, int width,
41 int height, GLenum format, GLenum type, const std::string& fileName)
43 int fd = open(fileName.c_str(), O_RDONLY);
52 if (fstat(fd, &sb) == -1)
58 void* pixels = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
59 glTexImage2D(target, level, internalFormat, width, height, 0, format, type, pixels);
61 munmap(pixels, sb.st_size);
68 bool loadCompressedTexture(GLenum target, int level, GLenum internalFormat, int width,
69 int height, const std::string& fileName)
71 int fd = open(fileName.c_str(), O_RDONLY);
80 if (fstat(fd, &sb) == -1)
86 void* pixels = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
87 glCompressedTexImage2D(target, level, internalFormat, width, height, 0, sb.st_size, pixels);
89 munmap(pixels, sb.st_size);
96 GLint createProgram(const std::string& vertSrc, const std::string& fragSrc)
101 const char* vs = vertSrc.c_str();
102 const char* fs = fragSrc.c_str();
104 GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
105 glShaderSource(vertexShader, 1, &vs, 0);
106 glCompileShader(vertexShader);
107 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
110 glGetShaderInfoLog(vertexShader, sizeof(infoLog), &logLength, infoLog);
111 printf("Vertex shader compilation failed:\n%s\n", infoLog);
115 GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
116 glShaderSource(fragmentShader, 1, &fs, 0);
117 glCompileShader(fragmentShader);
118 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
121 glGetShaderInfoLog(fragmentShader, sizeof(infoLog), &logLength, infoLog);
122 printf("Fragment shader compilation failed:\n%s\n", infoLog);
126 GLint program = glCreateProgram();
127 glAttachShader(program, fragmentShader);
128 glAttachShader(program, vertexShader);
129 glLinkProgram(program);
130 glGetProgramiv(program, GL_LINK_STATUS, &success);
133 glGetProgramInfoLog(program, sizeof(infoLog), &logLength, infoLog);
134 printf("Program linking failed:\n%s\n", infoLog);
140 std::string textureFormatName(GLenum format, GLenum type)
144 case GL_UNSIGNED_BYTE:
145 return ((format == GL_RGB) ? "rgb888" : "rgba8888");
146 case GL_UNSIGNED_SHORT_5_6_5:
148 case GL_UNSIGNED_SHORT_4_4_4_4:
150 case GL_UNSIGNED_SHORT_5_5_5_1:
152 case GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
154 case GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
156 case GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
157 return "rgba_pvrtc4";
158 case GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
159 return "rgba_pvrtc2";
160 case GL_ETC1_RGB8_OES:
167 #define DUMP_CFG_ATTRIB(attr, consts, bits) \
171 eglGetConfigAttrib(dpy, config, attr, &value); \
173 printf("%-32s: %10d (0x%x)\n", #attr, value, value); \
176 for (i = 0; i < sizeof(consts) / sizeof(consts[0]); i++) \
178 if (value == consts[i].value) \
180 printf("%-44s %s\n", "", consts[i].name); \
186 for (i = 0; i < sizeof(bits) / sizeof(bits[0]); i++) \
188 if (value & bits[i].value) \
190 printf("%-44s %s\n", "", bits[i].name); \
196 #define DECLARE_CONSTANTS(NAME) \
197 static const struct { const char* name; EGLint value; } NAME[] =
199 #define C(constant) {#constant, constant}
201 #define DECLARE_BITFIELD(NAME) \
202 static const struct { const char* name; EGLint value; } NAME[] =
204 #define B(bit) {#bit, bit}
206 DECLARE_CONSTANTS(defaultConsts)
210 DECLARE_BITFIELD(defaultBits)
214 DECLARE_CONSTANTS(configCaveatConsts)
218 C(EGL_NON_CONFORMANT_CONFIG),
221 DECLARE_CONSTANTS(transparentTypeConsts)
224 C(EGL_TRANSPARENT_RGB),
227 DECLARE_CONSTANTS(colorBufferTypeConsts)
231 C(EGL_LUMINANCE_BUFFER),
234 DECLARE_BITFIELD(surfaceTypeBits)
239 B(EGL_VG_COLORSPACE_LINEAR_BIT),
240 B(EGL_VG_ALPHA_FORMAT_PRE_BIT),
241 B(EGL_MULTISAMPLE_RESOLVE_BOX_BIT),
242 B(EGL_SWAP_BEHAVIOR_PRESERVED_BIT),
245 DECLARE_BITFIELD(renderableTypeBits)
247 B(EGL_OPENGL_ES_BIT),
249 B(EGL_OPENGL_ES2_BIT),
254 #undef DECLARE_CONSTANTS
256 #undef DECLARE_BITFIELD
258 void dumpConfig(EGLDisplay dpy, EGLConfig config)
260 DUMP_CFG_ATTRIB(EGL_BUFFER_SIZE, defaultConsts, defaultBits);
261 DUMP_CFG_ATTRIB(EGL_ALPHA_SIZE, defaultConsts, defaultBits);
262 DUMP_CFG_ATTRIB(EGL_BLUE_SIZE, defaultConsts, defaultBits);
263 DUMP_CFG_ATTRIB(EGL_GREEN_SIZE, defaultConsts, defaultBits);
264 DUMP_CFG_ATTRIB(EGL_RED_SIZE, defaultConsts, defaultBits);
265 DUMP_CFG_ATTRIB(EGL_DEPTH_SIZE, defaultConsts, defaultBits);
266 DUMP_CFG_ATTRIB(EGL_STENCIL_SIZE, defaultConsts, defaultBits);
267 DUMP_CFG_ATTRIB(EGL_CONFIG_CAVEAT, configCaveatConsts, defaultBits);
268 DUMP_CFG_ATTRIB(EGL_CONFIG_ID, defaultConsts, defaultBits);
269 DUMP_CFG_ATTRIB(EGL_LEVEL, defaultConsts, defaultBits);
270 DUMP_CFG_ATTRIB(EGL_MAX_PBUFFER_HEIGHT, defaultConsts, defaultBits);
271 DUMP_CFG_ATTRIB(EGL_MAX_PBUFFER_PIXELS, defaultConsts, defaultBits);
272 DUMP_CFG_ATTRIB(EGL_MAX_PBUFFER_WIDTH, defaultConsts, defaultBits);
273 DUMP_CFG_ATTRIB(EGL_NATIVE_RENDERABLE, defaultConsts, defaultBits);
274 DUMP_CFG_ATTRIB(EGL_NATIVE_VISUAL_ID, defaultConsts, defaultBits);
275 DUMP_CFG_ATTRIB(EGL_NATIVE_VISUAL_TYPE, defaultConsts, defaultBits);
276 DUMP_CFG_ATTRIB(EGL_SAMPLES, defaultConsts, defaultBits);
277 DUMP_CFG_ATTRIB(EGL_SAMPLE_BUFFERS, defaultConsts, defaultBits);
278 DUMP_CFG_ATTRIB(EGL_SURFACE_TYPE, defaultConsts, surfaceTypeBits);
279 DUMP_CFG_ATTRIB(EGL_TRANSPARENT_TYPE, transparentTypeConsts, defaultBits);
280 DUMP_CFG_ATTRIB(EGL_TRANSPARENT_BLUE_VALUE, defaultConsts, defaultBits);
281 DUMP_CFG_ATTRIB(EGL_TRANSPARENT_GREEN_VALUE, defaultConsts, defaultBits);
282 DUMP_CFG_ATTRIB(EGL_TRANSPARENT_RED_VALUE, defaultConsts, defaultBits);
283 DUMP_CFG_ATTRIB(EGL_BIND_TO_TEXTURE_RGB, defaultConsts, defaultBits);
284 DUMP_CFG_ATTRIB(EGL_BIND_TO_TEXTURE_RGBA, defaultConsts, defaultBits);
285 DUMP_CFG_ATTRIB(EGL_MIN_SWAP_INTERVAL, defaultConsts, defaultBits);
286 DUMP_CFG_ATTRIB(EGL_MAX_SWAP_INTERVAL, defaultConsts, defaultBits);
287 DUMP_CFG_ATTRIB(EGL_LUMINANCE_SIZE, defaultConsts, defaultBits);
288 DUMP_CFG_ATTRIB(EGL_ALPHA_MASK_SIZE, defaultConsts, defaultBits);
289 DUMP_CFG_ATTRIB(EGL_COLOR_BUFFER_TYPE, colorBufferTypeConsts, defaultBits);
290 DUMP_CFG_ATTRIB(EGL_RENDERABLE_TYPE, defaultConsts, renderableTypeBits);
291 DUMP_CFG_ATTRIB(EGL_CONFORMANT, defaultConsts, renderableTypeBits);
294 #undef DUMP_CFG_ATTRIB
296 /* This is after http://www.opengl.org/resources/features/OGLextensions/ */
297 static bool isExtensionSupported(const std::string& extensions, const std::string& name)
300 const char *where, *terminator;
302 /* Extension names should not have spaces. */
303 where = strchr(name.c_str(), ' ');
304 if (where || !name.size())
309 /* It takes a bit of care to be fool-proof about parsing the
310 OpenGL extensions string. Don't be fooled by sub-strings,
312 start = extensions.c_str();
315 where = strstr(start, name.c_str());
318 terminator = where + strlen(name.c_str());
319 if (where == start || *(where - 1) == ' ')
320 if (*terminator == ' ' || *terminator == '\0')
327 bool isEGLExtensionSupported(const std::string& name)
329 return isExtensionSupported(eglQueryString(ctx.dpy, EGL_EXTENSIONS), name);
332 bool isGLExtensionSupported(const std::string& name)
334 return isExtensionSupported((const char*)glGetString(GL_EXTENSIONS), name);