filename | src/pvr2/glutil.c |
changeset | 1258:f8a9c0fd2abb |
prev | 1248:0ea1904e2b14 |
next | 1275:83b15705cdde |
author | nkeynes |
date | Mon Mar 05 15:00:14 2012 +1000 (12 years ago) |
permissions | -rw-r--r-- |
last change | Revert to using GL_QUADS when available, fallback to fan-strip when it's not (GLES) |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * GL-based support functions
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
19 #include <string.h>
20 #include <stdlib.h>
21 #include <glib/gstrfuncs.h>
22 #include "pvr2/glutil.h"
24 gboolean isOpenGLES2()
25 {
26 const char *str = glGetString(GL_VERSION);
27 if( strncmp(str, "OpenGL ES 2.", 12) == 0 ) {
28 return TRUE;
29 }
30 }
32 gboolean isGLSecondaryColorSupported()
33 {
34 return isGLExtensionSupported("GL_EXT_secondary_color");
35 }
37 gboolean isGLVertexBufferSupported()
38 {
39 return isGLExtensionSupported("GL_ARB_vertex_buffer_object");
40 }
42 gboolean isGLPixelBufferSupported()
43 {
44 return isGLExtensionSupported("GL_ARB_pixel_buffer_object");
45 }
47 gboolean isGLMirroredTextureSupported()
48 {
49 return isGLExtensionSupported("GL_ARB_texture_mirrored_repeat");
50 }
53 gboolean isGLShaderSupported()
54 {
55 return isOpenGLES2() || (isGLExtensionSupported("GL_ARB_fragment_shader") &&
56 isGLExtensionSupported("GL_ARB_vertex_shader") &&
57 isGLExtensionSupported("GL_ARB_shading_language_100"));
58 }
60 /**
61 * Check if there's at least 2 texture units
62 */
63 gboolean isGLMultitextureSupported()
64 {
65 if( !isOpenGLES2() && !isGLExtensionSupported("GL_ARB_multitexture") )
66 return FALSE;
67 int units = 0;
69 #if defined(GL_MAX_TEXTURE_UNITS)
70 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &units);
71 #elif defined(GL_MAX_TEXTURE_IMAGE_UNITS)
72 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &units);
73 #endif
74 return units >= 2;
75 }
77 gboolean isGLVertexRangeSupported()
78 {
79 return isGLExtensionSupported("GL_APPLE_vertex_array_range") ||
80 isGLExtensionSupported("GL_NV_vertex_array_range");
81 }
83 /**
84 * Test if a specific extension is supported. From opengl.org
85 * @param extension extension name to check for
86 * @return TRUE if supported, otherwise FALSE.
87 */
88 gboolean isGLExtensionSupported( const char *extension )
89 {
90 const GLubyte *extensions = NULL;
91 const GLubyte *start;
92 GLubyte *where, *terminator;
94 /* Extension names should not have spaces. */
95 where = (GLubyte *) strchr(extension, ' ');
96 if (where || *extension == '\0')
97 return 0;
98 extensions = glGetString(GL_EXTENSIONS);
99 if( extensions == NULL ) {
100 /* No GL available, so we're pretty sure the extension isn't
101 * available either. */
102 return FALSE;
103 }
104 /* It takes a bit of care to be fool-proof about parsing the
105 OpenGL extensions string. Don't be fooled by sub-strings,
106 etc. */
107 start = extensions;
108 for (;;) {
109 where = (GLubyte *) strstr((const char *) start, extension);
110 if (!where)
111 break;
112 terminator = where + strlen(extension);
113 if (where == start || *(where - 1) == ' ')
114 if (*terminator == ' ' || *terminator == '\0')
115 return TRUE;
116 start = terminator;
117 }
118 return FALSE;
119 }
121 int compare_charp( const void *a, const void *b )
122 {
123 const char **ca = (const char **)a;
124 const char **cb = (const char **)b;
125 return strcmp(*ca, *cb);
126 }
128 #define DEFAULT_TERMINAL_COLUMNS 80
129 #define DEFAULT_COLUMN_WIDTH 34
131 int glGetMaxColourAttachments()
132 {
133 #ifdef GL_MAX_COLOR_ATTACHMENTS
134 GLint result = 0;
135 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &result);
136 return result;
137 #else
138 return 1;
139 #endif
140 }
143 /**
144 * Define an orthographic projection matrix
145 * Note: row-major order
146 */
147 void defineOrthoMatrix( GLfloat *matrix, GLfloat width, GLfloat height, GLfloat znear, GLfloat zfar )
148 {
149 matrix[0] = 2/width;
150 matrix[1] = 0;
151 matrix[2] = 0;
152 matrix[3] = 0;
154 matrix[4] = 0;
155 matrix[5] = -2/height;
156 matrix[6] = 0;
157 matrix[7] = 0;
159 matrix[8] = 0;
160 matrix[9] = 0;
161 matrix[10]= -2/(zfar-znear);
162 matrix[11]= 0;
164 matrix[12]= -1;
165 matrix[13]= 1;
166 matrix[14]= -(zfar+znear)/(zfar-znear);
167 matrix[15]= 1;
168 }
170 /**
171 * Format a GL extension list (or other space-separated string) nicely, and
172 * print to the given output stream.
173 */
175 void fprint_extensions( FILE *out, const char *extensions )
176 {
177 unsigned int i, j, count, maxlen = DEFAULT_COLUMN_WIDTH, columns, per_column, terminal_columns;
178 const char *terminal_columns_str = getenv("COLUMNS");
179 if( terminal_columns_str == NULL || (terminal_columns = strtol(terminal_columns_str,0,10)) == 0 )
180 terminal_columns = DEFAULT_TERMINAL_COLUMNS;
182 if( extensions == NULL || extensions[0] == '\0' )
183 return;
185 gchar *ext_dup = g_strdup(extensions);
186 gchar **ext_split = g_strsplit(g_strstrip(extensions), " ", 0);
187 for( count = 0; ext_split[count] != NULL; count++ ) {
188 unsigned len = strlen(ext_split[count]);
189 if( len > maxlen )
190 maxlen = len;
191 }
193 columns = terminal_columns / (maxlen+2);
194 if( columns == 0 )
195 columns = 1;
196 per_column = (count+columns-1) / columns;
198 qsort(ext_split, count, sizeof(gchar *), compare_charp);
200 for( i=0; i<per_column; i++ ) {
201 for( j=0; j<columns; j++ ) {
202 unsigned idx = i + (j*per_column);
203 if( idx < count )
204 fprintf( out, " %-*s", maxlen, ext_split[idx] );
205 }
206 fprintf( out, "\n" );
207 }
208 g_strfreev(ext_split);
209 g_free(ext_dup);
210 }
212 void glPrintInfo( FILE *out )
213 {
214 fprintf( out, "GL Vendor: %s\n", glGetString(GL_VENDOR) );
215 fprintf( out, "GL Renderer: %s\n", glGetString(GL_RENDERER) );
216 fprintf( out, "GL Version: %s\n", glGetString(GL_VERSION) );
217 if( isGLShaderSupported() ) {
218 fprintf( out, "SL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION) );
219 }
221 fprintf( out, "GL Extensions:\n" );
223 fprint_extensions( out, (const gchar *)glGetString(GL_EXTENSIONS) );
224 if( display_driver && display_driver->print_info ) {
225 fprintf( out, "\n");
226 display_driver->print_info(out);
227 }
228 }
230 gboolean gl_check_error(const char *context)
231 {
232 GLint err = glGetError();
233 if( err != 0 ) {
234 const char *s;
235 switch( err ) {
236 case GL_INVALID_ENUM: s = "Invalid enum"; break;
237 case GL_INVALID_VALUE: s = "Invalid value"; break;
238 case GL_INVALID_OPERATION: s = "Invalid operation"; break;
239 #ifdef GL_STACK_OVERFLOW
240 case GL_STACK_OVERFLOW: s = "Stack overflow"; break;
241 #endif
242 #ifdef GL_STACK_UNDERFLOW
243 case GL_STACK_UNDERFLOW: s = "Stack underflow"; break;
244 #endif
245 case GL_OUT_OF_MEMORY: s = "Out of memory"; break;
246 default: s = "Unknown error"; break;
247 }
248 if( context ) {
249 WARN( "%s: GL error: %x (%s)\n", context, err, s );
250 } else {
251 WARN( "GL error: %x (%s)\n", err, s );
252 }
253 return FALSE;
254 }
255 return TRUE;
256 }
.