Search
lxdream.org :: lxdream/src/pvr2/glutil.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glutil.c
changeset 1236:d93175c36387
prev1223:61684ca88599
next1245:01e0020adf88
author nkeynes
date Fri Feb 24 21:17:47 2012 +1000 (9 years ago)
permissions -rw-r--r--
last change Factor video_width/video_height out into video_gl.c
Convert immediate-mode bits in video_gl.c into a structure for glDrawArray
Move setOrtho into defineOrthoMatrix in glutil.c
Rearrange various GL settings to keep a consistent state
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 isGLSecondaryColorSupported()
    25 {
    26     return isGLExtensionSupported("GL_EXT_secondary_color");
    27 }
    29 gboolean isGLVertexBufferSupported()
    30 {
    31     return isGLExtensionSupported("GL_ARB_vertex_buffer_object");
    32 }
    34 gboolean isGLPixelBufferSupported()
    35 {
    36     return isGLExtensionSupported("GL_ARB_pixel_buffer_object");
    37 }
    39 gboolean isGLMirroredTextureSupported()
    40 {
    41     return isGLExtensionSupported("GL_ARB_texture_mirrored_repeat");
    42 }
    44 /**
    45  * Check if there's at least 2 texture units
    46  */
    47 gboolean isGLMultitextureSupported()
    48 {
    49     if( !isGLExtensionSupported("GL_ARB_multitexture") )
    50         return FALSE;
    51     int units = 0;
    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);
    57 #endif
    58     return units >= 2;
    59 }
    61 gboolean isGLVertexRangeSupported()
    62 {
    63     return isGLExtensionSupported("GL_APPLE_vertex_array_range") ||
    64             isGLExtensionSupported("GL_NV_vertex_array_range");
    65 }
    67 /**
    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.
    71  */
    72 gboolean isGLExtensionSupported( const char *extension )
    73 {
    74     const GLubyte *extensions = NULL;
    75     const GLubyte *start;
    76     GLubyte *where, *terminator;
    78     /* Extension names should not have spaces. */
    79     where = (GLubyte *) strchr(extension, ' ');
    80     if (where || *extension == '\0')
    81         return 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. */
    86         return FALSE;
    87     }
    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,
    90        etc. */
    91     start = extensions;
    92     for (;;) {
    93         where = (GLubyte *) strstr((const char *) start, extension);
    94         if (!where)
    95             break;
    96         terminator = where + strlen(extension);
    97         if (where == start || *(where - 1) == ' ')
    98             if (*terminator == ' ' || *terminator == '\0')
    99                 return TRUE;
   100         start = terminator;
   101     }
   102     return FALSE;
   103 }
   105 int compare_charp( const void *a, const void *b )
   106 {
   107     const char **ca = (const char **)a;
   108     const char **cb = (const char **)b;
   109     return strcmp(*ca, *cb);
   110 }
   112 #define DEFAULT_TERMINAL_COLUMNS 80
   113 #define DEFAULT_COLUMN_WIDTH 34
   115 int glGetMaxColourAttachments()
   116 {
   117 #ifdef GL_MAX_COLOR_ATTACHMENTS
   118     GLint result = 0;
   119     glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &result);
   120     return result;
   121 #else
   122     return 1;
   123 #endif
   124 }
   127 /**
   128  * Define an orthographic projection matrix
   129  * Note: row-major order
   130  */
   131 void defineOrthoMatrix( GLfloat *matrix, GLfloat width, GLfloat height, GLfloat znear, GLfloat zfar )
   132 {
   133     matrix[0] =  2/width;
   134     matrix[1] =  0;
   135     matrix[2] =  0;
   136     matrix[3] =  0;
   138     matrix[4] =  0;
   139     matrix[5] = -2/height;
   140     matrix[6] =  0;
   141     matrix[7] =  0;
   143     matrix[8] =  0;
   144     matrix[9] =  0;
   145     matrix[10]= -2/(zfar-znear);
   146     matrix[11]=  0;
   148     matrix[12]= -1;
   149     matrix[13]=  1;
   150     matrix[14]= -(zfar+znear)/(zfar-znear);
   151     matrix[15]=  1;
   152 }
   154 /**
   155  * Format a GL extension list (or other space-separated string) nicely, and
   156  * print to the given output stream.
   157  */
   159 void fprint_extensions( FILE *out, const char *extensions )
   160 {
   161     unsigned int i, j, count, maxlen = DEFAULT_COLUMN_WIDTH, columns, per_column, terminal_columns;
   162     const char *terminal_columns_str = getenv("COLUMNS");
   163     if( terminal_columns_str == NULL || (terminal_columns = strtol(terminal_columns_str,0,10)) == 0 )
   164         terminal_columns = DEFAULT_TERMINAL_COLUMNS;
   166     if( extensions == NULL || extensions[0] == '\0' )
   167         return;
   169     gchar *ext_dup = g_strdup(extensions);
   170     gchar **ext_split = g_strsplit(g_strstrip(extensions), " ", 0);
   171     for( count = 0; ext_split[count] != NULL; count++ ) {
   172         unsigned len = strlen(ext_split[count]);
   173         if( len > maxlen )
   174             maxlen = len;
   175     }
   177     columns = terminal_columns / (maxlen+2);
   178     if( columns == 0 )
   179         columns = 1;
   180     per_column = (count+columns-1) / columns;
   182     qsort(ext_split, count, sizeof(gchar *), compare_charp);
   184     for( i=0; i<per_column; i++ ) {
   185         for( j=0; j<columns; j++ ) {
   186             unsigned idx = i + (j*per_column);
   187             if( idx < count )
   188                 fprintf( out, "  %-*s", maxlen, ext_split[idx] );
   189         }
   190         fprintf( out, "\n" );
   191     }
   192     g_strfreev(ext_split);
   193     g_free(ext_dup);
   194 }
   196 void glPrintInfo( FILE *out )
   197 {
   198     fprintf( out, "GL Vendor: %s\n", glGetString(GL_VENDOR) );
   199     fprintf( out, "GL Renderer: %s\n", glGetString(GL_RENDERER) );
   200     fprintf( out, "GL Version: %s\n", glGetString(GL_VERSION) );
   201     if( glsl_is_supported() ) {
   202          const char * version = glsl_get_version();
   203          fprintf( out, "SL Version: %s\n", version );
   204     }
   206     fprintf( out, "GL Extensions:\n" );
   208     fprint_extensions( out, (const gchar *)glGetString(GL_EXTENSIONS) );
   209     if( display_driver && display_driver->print_info ) {
   210         fprintf( out, "\n");
   211         display_driver->print_info(out);
   212     }
   213 }
   215 gboolean gl_check_error(const char *context)
   216 {
   217     GLint err = glGetError();
   218     if( err != 0 ) {
   219         const char *s;
   220         switch( err ) {
   221         case GL_INVALID_ENUM: s = "Invalid enum"; break;
   222         case GL_INVALID_VALUE: s = "Invalid value"; break;
   223         case GL_INVALID_OPERATION: s = "Invalid operation"; break;
   224 #ifdef GL_STACK_OVERFLOW
   225         case GL_STACK_OVERFLOW: s = "Stack overflow"; break;
   226 #endif
   227 #ifdef GL_STACK_UNDERFLOW
   228         case GL_STACK_UNDERFLOW: s = "Stack underflow"; break;
   229 #endif
   230         case GL_OUT_OF_MEMORY:   s = "Out of memory"; break;
   231         default: s = "Unknown error"; break;
   232         }
   233         if( context ) {
   234             WARN( "%s: GL error: %x (%s)\n", context, err, s );
   235         } else {
   236             WARN( "GL error: %x (%s)\n", err, s );
   237         }
   238         return FALSE;
   239     }
   240     return TRUE;
   241 }
.