Search
lxdream.org :: lxdream/src/pvr2/glutil.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glutil.c
changeset 1276:e20a69167093
prev1275:83b15705cdde
next1282:9f445c5e252b
author nkeynes
date Tue Mar 20 17:54:58 2012 +1000 (10 years ago)
permissions -rw-r--r--
last change Fix missing return statement
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     return FALSE;
    31 }
    33 gboolean isGLSecondaryColorSupported()
    34 {
    35     return isGLExtensionSupported("GL_EXT_secondary_color");
    36 }
    38 gboolean isGLVertexBufferSupported()
    39 {
    40     return isGLExtensionSupported("GL_ARB_vertex_buffer_object");
    41 }
    43 gboolean isGLPixelBufferSupported()
    44 {
    45     return isGLExtensionSupported("GL_ARB_pixel_buffer_object");
    46 }
    48 gboolean isGLMirroredTextureSupported()
    49 {
    50     return isGLExtensionSupported("GL_ARB_texture_mirrored_repeat");
    51 }
    53 gboolean isGLBGRATextureSupported()
    54 {
    55     /* Note: e.g. Tegra 3 reports GL_EXT_bgra, but it doesn't actually work.
    56      * Need to check this with NVIDIA, in meantime assume GLES2 doesn't have
    57      * BGRA support */
    58     return !isOpenGLES2() && isGLExtensionSupported("GL_EXT_bgra");
    59 }
    62 gboolean isGLShaderSupported()
    63 {
    64     return isOpenGLES2() || (isGLExtensionSupported("GL_ARB_fragment_shader") &&
    65     isGLExtensionSupported("GL_ARB_vertex_shader") &&
    66     isGLExtensionSupported("GL_ARB_shading_language_100"));
    67 }
    69 /**
    70  * Check if there's at least 2 texture units
    71  */
    72 gboolean isGLMultitextureSupported()
    73 {
    74     if( !isOpenGLES2() && !isGLExtensionSupported("GL_ARB_multitexture") )
    75         return FALSE;
    76     int units = 0;
    78 #if defined(GL_MAX_TEXTURE_UNITS)
    79         glGetIntegerv(GL_MAX_TEXTURE_UNITS, &units);
    80 #elif defined(GL_MAX_TEXTURE_IMAGE_UNITS)
    81         glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &units);
    82 #endif
    83     return units >= 2;
    84 }
    86 gboolean isGLVertexRangeSupported()
    87 {
    88     return isGLExtensionSupported("GL_APPLE_vertex_array_range") ||
    89             isGLExtensionSupported("GL_NV_vertex_array_range");
    90 }
    92 /**
    93  * Test if a specific extension is supported. From opengl.org
    94  * @param extension extension name to check for
    95  * @return TRUE if supported, otherwise FALSE.
    96  */
    97 gboolean isGLExtensionSupported( const char *extension )
    98 {
    99     const GLubyte *extensions = NULL;
   100     const GLubyte *start;
   101     GLubyte *where, *terminator;
   103     /* Extension names should not have spaces. */
   104     where = (GLubyte *) strchr(extension, ' ');
   105     if (where || *extension == '\0')
   106         return 0;
   107     extensions = glGetString(GL_EXTENSIONS);
   108     if( extensions == NULL ) {
   109         /* No GL available, so we're pretty sure the extension isn't
   110          * available either. */
   111         return FALSE;
   112     }
   113     /* It takes a bit of care to be fool-proof about parsing the
   114        OpenGL extensions string. Don't be fooled by sub-strings,
   115        etc. */
   116     start = extensions;
   117     for (;;) {
   118         where = (GLubyte *) strstr((const char *) start, extension);
   119         if (!where)
   120             break;
   121         terminator = where + strlen(extension);
   122         if (where == start || *(where - 1) == ' ')
   123             if (*terminator == ' ' || *terminator == '\0')
   124                 return TRUE;
   125         start = terminator;
   126     }
   127     return FALSE;
   128 }
   130 int compare_charp( const void *a, const void *b )
   131 {
   132     const char **ca = (const char **)a;
   133     const char **cb = (const char **)b;
   134     return strcmp(*ca, *cb);
   135 }
   137 #define DEFAULT_TERMINAL_COLUMNS 80
   138 #define DEFAULT_COLUMN_WIDTH 34
   140 int glGetMaxColourAttachments()
   141 {
   142 #ifdef GL_MAX_COLOR_ATTACHMENTS
   143     GLint result = 0;
   144     glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &result);
   145     return result;
   146 #else
   147     return 1;
   148 #endif
   149 }
   152 /**
   153  * Define an orthographic projection matrix
   154  * Note: row-major order
   155  */
   156 void defineOrthoMatrix( GLfloat *matrix, GLfloat width, GLfloat height, GLfloat znear, GLfloat zfar )
   157 {
   158     matrix[0] =  2/width;
   159     matrix[1] =  0;
   160     matrix[2] =  0;
   161     matrix[3] =  0;
   163     matrix[4] =  0;
   164     matrix[5] = -2/height;
   165     matrix[6] =  0;
   166     matrix[7] =  0;
   168     matrix[8] =  0;
   169     matrix[9] =  0;
   170     matrix[10]= -2/(zfar-znear);
   171     matrix[11]=  0;
   173     matrix[12]= -1;
   174     matrix[13]=  1;
   175     matrix[14]= -(zfar+znear)/(zfar-znear);
   176     matrix[15]=  1;
   177 }
   179 /**
   180  * Format a GL extension list (or other space-separated string) nicely, and
   181  * print to the given output stream.
   182  */
   184 void fprint_extensions( FILE *out, const char *extensions )
   185 {
   186     unsigned int i, j, count, maxlen = DEFAULT_COLUMN_WIDTH, columns, per_column, terminal_columns;
   187     const char *terminal_columns_str = getenv("COLUMNS");
   188     if( terminal_columns_str == NULL || (terminal_columns = strtol(terminal_columns_str,0,10)) == 0 )
   189         terminal_columns = DEFAULT_TERMINAL_COLUMNS;
   191     if( extensions == NULL || extensions[0] == '\0' )
   192         return;
   194     gchar *ext_dup = g_strdup(extensions);
   195     gchar **ext_split = g_strsplit(g_strstrip(extensions), " ", 0);
   196     for( count = 0; ext_split[count] != NULL; count++ ) {
   197         unsigned len = strlen(ext_split[count]);
   198         if( len > maxlen )
   199             maxlen = len;
   200     }
   202     columns = terminal_columns / (maxlen+2);
   203     if( columns == 0 )
   204         columns = 1;
   205     per_column = (count+columns-1) / columns;
   207     qsort(ext_split, count, sizeof(gchar *), compare_charp);
   209     for( i=0; i<per_column; i++ ) {
   210         for( j=0; j<columns; j++ ) {
   211             unsigned idx = i + (j*per_column);
   212             if( idx < count )
   213                 fprintf( out, "  %-*s", maxlen, ext_split[idx] );
   214         }
   215         fprintf( out, "\n" );
   216     }
   217     g_strfreev(ext_split);
   218     g_free(ext_dup);
   219 }
   221 void glPrintInfo( FILE *out )
   222 {
   223     fprintf( out, "GL Vendor: %s\n", glGetString(GL_VENDOR) );
   224     fprintf( out, "GL Renderer: %s\n", glGetString(GL_RENDERER) );
   225     fprintf( out, "GL Version: %s\n", glGetString(GL_VERSION) );
   226     if( isGLShaderSupported() ) {
   227          fprintf( out, "SL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION) );
   228     }
   230     fprintf( out, "GL Extensions:\n" );
   232     fprint_extensions( out, (const gchar *)glGetString(GL_EXTENSIONS) );
   233     if( display_driver && display_driver->print_info ) {
   234         fprintf( out, "\n");
   235         display_driver->print_info(out);
   236     }
   237 }
   239 gboolean gl_check_error(const char *context)
   240 {
   241     GLint err = glGetError();
   242     if( err != 0 ) {
   243         const char *s;
   244         switch( err ) {
   245         case GL_INVALID_ENUM: s = "Invalid enum"; break;
   246         case GL_INVALID_VALUE: s = "Invalid value"; break;
   247         case GL_INVALID_OPERATION: s = "Invalid operation"; break;
   248 #ifdef GL_STACK_OVERFLOW
   249         case GL_STACK_OVERFLOW: s = "Stack overflow"; break;
   250 #endif
   251 #ifdef GL_STACK_UNDERFLOW
   252         case GL_STACK_UNDERFLOW: s = "Stack underflow"; break;
   253 #endif
   254         case GL_OUT_OF_MEMORY:   s = "Out of memory"; break;
   255         default: s = "Unknown error"; break;
   256         }
   257         if( context ) {
   258             WARN( "%s: GL error: %x (%s)\n", context, err, s );
   259         } else {
   260             WARN( "GL error: %x (%s)\n", err, s );
   261         }
   262         return FALSE;
   263     }
   264     return TRUE;
   265 }
.