4 * GL-based support functions
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
21 #include <glib/gstrfuncs.h>
22 #include "pvr2/glutil.h"
24 gboolean isGLSecondaryColorSupported()
26 return isGLExtensionSupported("GL_EXT_secondary_color");
29 gboolean isGLVertexBufferSupported()
31 return isGLExtensionSupported("GL_ARB_vertex_buffer_object");
34 gboolean isGLPixelBufferSupported()
36 return isGLExtensionSupported("GL_ARB_pixel_buffer_object");
39 gboolean isGLMirroredTextureSupported()
41 return isGLExtensionSupported("GL_ARB_texture_mirrored_repeat");
45 * Check if there's at least 2 texture units
47 gboolean isGLMultitextureSupported()
49 if( !isGLExtensionSupported("GL_ARB_multitexture") )
53 #if defined(GL_MAX_TEXTURE_UNITS)
54 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &units);
55 #elif defined(GL_MAX_TEXTURE_IMAGE_UNITS)
56 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &units);
61 gboolean isGLVertexRangeSupported()
63 return isGLExtensionSupported("GL_APPLE_vertex_array_range") ||
64 isGLExtensionSupported("GL_NV_vertex_array_range");
68 * Test if a specific extension is supported. From opengl.org
69 * @param extension extension name to check for
70 * @return TRUE if supported, otherwise FALSE.
72 gboolean isGLExtensionSupported( const char *extension )
74 const GLubyte *extensions = NULL;
76 GLubyte *where, *terminator;
78 /* Extension names should not have spaces. */
79 where = (GLubyte *) strchr(extension, ' ');
80 if (where || *extension == '\0')
82 extensions = glGetString(GL_EXTENSIONS);
83 if( extensions == NULL ) {
84 /* No GL available, so we're pretty sure the extension isn't
85 * available either. */
88 /* It takes a bit of care to be fool-proof about parsing the
89 OpenGL extensions string. Don't be fooled by sub-strings,
93 where = (GLubyte *) strstr((const char *) start, extension);
96 terminator = where + strlen(extension);
97 if (where == start || *(where - 1) == ' ')
98 if (*terminator == ' ' || *terminator == '\0')
105 int compare_charp( const void *a, const void *b )
107 const char **ca = (const char **)a;
108 const char **cb = (const char **)b;
109 return strcmp(*ca, *cb);
112 #define DEFAULT_TERMINAL_COLUMNS 80
113 #define DEFAULT_COLUMN_WIDTH 34
116 * Format a GL extension list (or other space-separated string) nicely, and
117 * print to the given output stream.
120 void fprint_extensions( FILE *out, const char *extensions )
122 unsigned int i, j, count, maxlen = DEFAULT_COLUMN_WIDTH, columns, per_column, terminal_columns;
123 const char *terminal_columns_str = getenv("COLUMNS");
124 if( terminal_columns_str == NULL || (terminal_columns = strtol(terminal_columns_str,0,10)) == 0 )
125 terminal_columns = DEFAULT_TERMINAL_COLUMNS;
127 if( extensions == NULL || extensions[0] == '\0' )
130 gchar *ext_dup = g_strdup(extensions);
131 gchar **ext_split = g_strsplit(g_strstrip(extensions), " ", 0);
132 for( count = 0; ext_split[count] != NULL; count++ ) {
133 unsigned len = strlen(ext_split[count]);
138 columns = terminal_columns / (maxlen+2);
141 per_column = (count+columns-1) / columns;
143 qsort(ext_split, count, sizeof(gchar *), compare_charp);
145 for( i=0; i<per_column; i++ ) {
146 for( j=0; j<columns; j++ ) {
147 unsigned idx = i + (j*per_column);
149 fprintf( out, " %-*s", maxlen, ext_split[idx] );
151 fprintf( out, "\n" );
153 g_strfreev(ext_split);
157 void glPrintInfo( FILE *out )
159 fprintf( out, "GL Vendor: %s\n", glGetString(GL_VENDOR) );
160 fprintf( out, "GL Renderer: %s\n", glGetString(GL_RENDERER) );
161 fprintf( out, "GL Version: %s\n", glGetString(GL_VERSION) );
162 if( glsl_is_supported() ) {
163 const char * version = glsl_get_version();
164 fprintf( out, "SL Version: %s\n", version );
167 fprintf( out, "GL Extensions:\n" );
169 fprint_extensions( out, (const gchar *)glGetString(GL_EXTENSIONS) );
170 if( display_driver && display_driver->print_info ) {
172 display_driver->print_info(out);
176 gboolean gl_check_error(const char *context)
178 GLint err = glGetError();
182 case GL_INVALID_ENUM: s = "Invalid enum"; break;
183 case GL_INVALID_VALUE: s = "Invalid value"; break;
184 case GL_INVALID_OPERATION: s = "Invalid operation"; break;
185 #ifdef GL_STACK_OVERFLOW
186 case GL_STACK_OVERFLOW: s = "Stack overflow"; break;
188 #ifdef GL_STACK_UNDERFLOW
189 case GL_STACK_UNDERFLOW: s = "Stack underflow"; break;
191 case GL_OUT_OF_MEMORY: s = "Out of memory"; break;
192 default: s = "Unknown error"; break;
195 WARN( "%s: GL error: %x (%s)\n", context, err, s );
197 WARN( "GL error: %x (%s)\n", err, s );
.