Search
lxdream.org :: lxdream/src/drivers/video_glx.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_glx.c
changeset 1159:580436b01b6c
prev1134:f502f3d32f90
next1222:a4545699a82b
author nkeynes
date Wed Jan 19 17:50:09 2011 +1000 (9 years ago)
permissions -rw-r--r--
last change Implement vertex array range support, and move buffer operations to gl_vbo.c
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Shared functions for all X11-based display drivers.
     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 <stdlib.h>
    20 #include <string.h>
    21 #include "display.h"
    22 #include <X11/Xlib.h>
    23 #include <GL/glx.h>
    24 #include "pvr2/pvr2.h"
    25 #include "pvr2/glutil.h"
    26 #include "drivers/video_glx.h"
    27 #include "drivers/video_gl.h"
    29 /**
    30  * General X11 parameters. The front-end driver is expected to set this up
    31  * by calling video_glx_init after initializing itself.
    32  */
    33 Display *video_x11_display = NULL;
    34 Window video_x11_window = 0;
    36 static int glx_version = 100;
    37 static XVisualInfo *glx_visual = NULL;
    38 static GLXFBConfig glx_fbconfig;
    39 static GLXContext glx_context = NULL;
    40 static gboolean glx_is_initialized = FALSE;
    41 static gboolean glx_fbconfig_supported = FALSE;
    42 static gboolean glx_pbuffer_supported = FALSE;
    43 static GLuint glx_pbuffer_texture = 0; 
    45 static void video_glx_swap_buffers( void );
    46 static void video_glx_print_info( FILE *out );
    48 /* Prototypes for pbuffer support methods */
    49 static void glx_pbuffer_init( display_driver_t driver );
    50 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
    51 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
    52 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
    53 static void glx_pbuffer_finish_render( render_buffer_t buffer );
    54 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer );
    55 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    56 static void glx_pbuffer_display_blank( uint32_t colour );
    57 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    59 /**
    60  * Test if a specific extension is supported. From opengl.org
    61  * @param extension extension name to check for
    62  * @return TRUE if supported, otherwise FALSE.
    63  */
    64 gboolean isServerGLXExtensionSupported( Display *display, int screen, 
    65                                         const char *extension )
    66 {
    67     const char *extensions = NULL;
    68     const char *start;
    69     char *where, *terminator;
    71     /* Extension names should not have spaces. */
    72     where = strchr(extension, ' ');
    73     if (where || *extension == '\0')
    74         return 0;
    75     extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
    76     start = extensions;
    77     for (;;) {
    78         where = strstr((const char *) start, extension);
    79         if (!where)
    80             break;
    81         terminator = where + strlen(extension);
    82         if (where == start || *(where - 1) == ' ')
    83             if (*terminator == ' ' || *terminator == '\0')
    84                 return TRUE;
    85         start = terminator;
    86     }
    87     return FALSE;
    88 }
    90 gboolean video_glx_init( Display *display, int screen )
    91 {
    92     int major, minor;
    93     int glx_major, glx_minor, glx_error;
    95     if( glx_is_initialized ) {
    96         return TRUE;
    97     }
    99     Bool result = XQueryExtension( display, "GLX", &glx_major, &glx_minor, &glx_error ) &&
   100                   glXQueryVersion( display, &major, &minor );
   101     if( result == False ) {
   102         ERROR( "GLX not supported on display" );
   103         return FALSE;
   104     }
   106     glx_version = (major*100) + minor;
   108 #ifdef APPLE_BUILD
   109     /* fbconfig is broken on at least the 10.5 GLX implementation */
   110     glx_fbconfig_supported = FALSE;
   111 #else
   112     glx_fbconfig_supported = (glx_version >= 103 || 
   113             isServerGLXExtensionSupported(display, screen,
   114                     "GLX_SGIX_fbconfig") );
   115 #endif
   116     glx_pbuffer_supported = (glx_version >= 103 ||
   117             isServerGLXExtensionSupported(display, screen,
   118                     "GLX_SGIX_pbuffer") );
   119 //    glx_fbconfig_supported = FALSE;
   120     if( glx_fbconfig_supported ) {
   121         int nelem;
   122         int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
   123                 GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
   124                 GLX_RENDER_TYPE, GLX_RGBA_BIT, 
   125                 GLX_DEPTH_SIZE, 24, 
   126                 GLX_STENCIL_SIZE, 8, 0 };
   127         GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
   128                 fb_attribs, &nelem );
   130         if( configs == NULL || nelem == 0 ) {
   131             /* Try a 16-bit depth buffer and see if it helps */
   132             fb_attribs[5] = 16;
   133             configs = glXChooseFBConfig( display, screen, fb_attribs, &nelem );
   134             if( nelem > 0 ) {
   135                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   136             }
   138         }
   139         if( configs == NULL || nelem == 0 ) {
   140             /* Still didn't work. Fallback to 1.2 methods */
   141             glx_fbconfig_supported = FALSE;
   142             glx_pbuffer_supported = FALSE;
   143         } else {
   144             glx_fbconfig = configs[0];
   145             glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
   146             XFree(configs);
   147         }
   148     }
   150     if( !glx_fbconfig_supported ) {
   151         int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, 0 };
   152         glx_visual = glXChooseVisual( display, screen, attribs );
   153         if( glx_visual == NULL ) {
   154             /* Try the 16-bit fallback here too */
   155             attribs[2] = 16;
   156             glx_visual = glXChooseVisual( display, screen, attribs );
   157             if( glx_visual != NULL ) {
   158                 WARN( "Using a 16-bit depth buffer - expect video glitches" );
   159             }
   160         }
   161     }
   163     if( glx_visual == NULL ) {
   164         return FALSE;
   165     }
   167     glx_is_initialized = TRUE;
   168     return TRUE;
   169 }
   171 static void video_glx_print_info( FILE *out )
   172 {
   173     XWindowAttributes attr;
   175     if( !XGetWindowAttributes(video_x11_display, video_x11_window, &attr) )
   176         return; /* Failed */
   177     int screen = XScreenNumberOfScreen(attr.screen);
   179     fprintf( out, "GLX Server: %s %s\n", glXQueryServerString(video_x11_display, screen, GLX_VENDOR),
   180             glXQueryServerString(video_x11_display, screen, GLX_VERSION) );
   181     fprintf( out, "GLX Client: %s %s\n", glXGetClientString(video_x11_display, GLX_VENDOR),
   182             glXGetClientString(video_x11_display, GLX_VERSION) );
   183     fprintf( out, "GLX Server Extensions:\n" );
   184     fprint_extensions( out, glXQueryServerString(video_x11_display, screen, GLX_EXTENSIONS) );
   185     fprintf( out, "GLX Client Extensions:\n" );
   186     fprint_extensions( out, glXGetClientString(video_x11_display, GLX_EXTENSIONS) );
   187     fprintf( out, "GLX Extensions:\n" );
   188     fprint_extensions( out, glXQueryExtensionsString(video_x11_display, screen) );
   190 }
   192 gboolean video_glx_init_context( Display *display, Window window )
   193 {
   194     if( glx_fbconfig_supported ) {
   195         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   196                 GLX_RGBA_TYPE, NULL, True );
   197         if( glx_context == NULL ) {
   198             ERROR( "Unable to create a GLX Context.");
   199             return FALSE;
   200         }
   202         if( glXMakeCurrent( display, window,
   203                 glx_context ) == False ) {
   204             ERROR( "Unable to prepare GLX context for drawing" );
   205             glXDestroyContext( display, glx_context );
   206             return FALSE;
   207         }
   208     } else {
   209         glx_context = glXCreateContext( display, glx_visual, None, True );
   210         if( glx_context == NULL ) {
   211             ERROR( "Unable to create a GLX Context.");
   212             return FALSE;
   213         }
   215         if( glXMakeCurrent( display, window, glx_context ) == False ) {
   216             ERROR( "Unable to prepare GLX context for drawing" );
   217             glXDestroyContext( display, glx_context );
   218             return FALSE;
   219         }
   220     }
   222     if( !glXIsDirect(display, glx_context) ) {
   223         WARN( "Not using direct rendering - this is likely to be slow" );
   224     }
   226     video_x11_display = display;
   227     video_x11_window = window;
   229     return TRUE;
   230 }
   232 gboolean video_glx_init_driver( display_driver_t driver )
   233 {
   234     driver->swap_buffers = video_glx_swap_buffers;
   235     driver->print_info = video_glx_print_info;
   236     driver->capabilities.has_gl = TRUE;
   237     if( gl_fbo_is_supported() ) { // First preference
   238         gl_fbo_init(driver);
   239     } else if( glx_pbuffer_supported ) {
   240         glx_pbuffer_init(driver);
   241     } else {
   242         ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
   243         video_glx_shutdown();
   244         return FALSE;
   245     }
   246     gl_vbo_init(driver);
   247     return TRUE;
   248 }
   251 void video_glx_shutdown()
   252 {
   253     //    texcache_gl_shutdown();
   254     glx_is_initialized = FALSE;
   255     if( glx_context != NULL ) {
   256         glXDestroyContext( video_x11_display, glx_context );
   257         glx_context = NULL;
   258     }
   259     if( glx_visual != NULL ) {
   260         XFree(glx_visual);
   261         glx_visual = NULL;
   262     }
   263 }
   266 XVisualInfo *video_glx_get_visual()
   267 {
   268     return glx_visual;
   269 }
   272 int video_glx_load_font( const gchar *font_name )
   273 {
   274     int lists;
   275     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   276     if (font == NULL)
   277         return -1;
   279     lists = glGenLists(96);
   280     glXUseXFont(font->fid, 32, 96, lists);
   281     XFreeFont(video_x11_display, font);
   282     return lists;
   283 }
   286 static void video_glx_swap_buffers( void )
   287 {
   288     glXSwapBuffers( video_x11_display, video_x11_window );
   289 }
   291 void video_glx_make_window_current( void )
   292 {
   293     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   294 }
   297 // Pbuffer support
   299 /**
   300  * Construct the initial frame buffers and allocate ids for everything.
   301  * The render handling driver methods are set to the fbo versions.
   302  */
   303 static void glx_pbuffer_init( display_driver_t driver ) 
   304 {
   305     GLint stencil_bits = 0;
   307     /* Retrieve the number of stencil bits */
   308     glGetIntegerv( GL_STENCIL_BITS, &stencil_bits );
   309     driver->capabilities.stencil_bits = stencil_bits;
   311     glGenTextures( 1, &glx_pbuffer_texture );
   312     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   313     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   314     driver->set_render_target = glx_pbuffer_set_render_target;
   315     driver->finish_render = glx_pbuffer_finish_render;
   316     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   317     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   318     driver->display_blank = glx_pbuffer_display_blank;
   319     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   320 }
   322 void glx_pbuffer_shutdown()
   323 {
   324     glDeleteTextures( 1, &glx_pbuffer_texture );
   325 }
   327 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   328 {
   329     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   330             GLX_PRESERVED_CONTENTS, True, 0 };
   331     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   332     if( pb == (GLXPbuffer)NULL ) {
   333         ERROR( "Unable to create pbuffer" );
   334         return NULL;
   335     }
   336     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   337     buffer->width = width;
   338     buffer->height = height;
   339     buffer->buf_id = pb;
   340     buffer->tex_id = tex_id;
   341     return buffer;
   342 }
   344 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   345 {
   346     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   347     buffer->buf_id = 0;
   348     free( buffer );
   349 }
   351 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   352 {
   353     glFinish();
   354     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   355         ERROR( "Make context current (pbuffer) failed!" );
   356     }
   357     /* setup the gl context */
   358     glViewport( 0, 0, buffer->width, buffer->height );
   359     glDrawBuffer(GL_FRONT);
   361     return TRUE;
   362 }
   364 static void glx_pbuffer_finish_render( render_buffer_t buffer )
   365 {
   366     glFinish();
   367     if( buffer->tex_id != 0 ) {
   368         // The pbuffer should already be the current context, but just in case...
   369         glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   370         glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->tex_id );
   371         glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   372     }
   373 }
   376 /**
   377  * Render the texture holding the given buffer to the front window
   378  * buffer.
   379  */
   380 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   381 {
   382     glFinish();
   383     glReadBuffer( GL_FRONT );
   384     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   385     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
   386     glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   387     video_glx_make_window_current();
   388     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   389 }
   391 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   392 {
   393     glFinish();
   394     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   395     GLenum type = colour_formats[frame->colour_format].type;
   396     GLenum format = colour_formats[frame->colour_format].format;
   397     int bpp = colour_formats[frame->colour_format].bpp;
   398     int rowstride = (frame->rowstride / bpp) - frame->width;
   400     gl_reset_state();
   401     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   402     glRasterPos2f(0.375, frame->height-0.375);
   403     glPixelZoom( 1.0, 1.0 );
   404     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   405     glFlush();
   406 }
   408 static void glx_pbuffer_display_blank( uint32_t colour )
   409 {
   410     glFinish();
   411     video_glx_make_window_current();
   412     gl_display_blank( colour );
   413 }
   415 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   416                                                 int rowstride, int format )
   417 {
   418     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   419     glReadBuffer( GL_FRONT );
   420     return gl_read_render_buffer( target, buffer, rowstride, format );
   421 }
.