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 639:162ee7614b60
prev635:76c63aac3590
next1066:ddffe9d2b332
author nkeynes
date Mon Feb 18 09:21:43 2008 +0000 (11 years ago)
branchlxdream-render
permissions -rw-r--r--
last change More render WIP - initial glrender.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 <string.h>
    20 #include "display.h"
    21 #include <X11/Xlib.h>
    22 #include <GL/glx.h>
    23 #include "pvr2/pvr2.h"
    24 #include "pvr2/glutil.h"
    25 #include "drivers/video_glx.h"
    26 #include "drivers/video_gl.h"
    28 /**
    29  * General X11 parameters. The front-end driver is expected to set this up
    30  * by calling video_glx_init after initializing itself.
    31  */
    32 Display *video_x11_display = NULL;
    33 Window video_x11_window = 0;
    34 static gboolean glsl_loaded = FALSE;
    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 int glx_pbuffer_texture = 0; 
    45 /* Prototypes for pbuffer support methods */
    46 static void glx_pbuffer_init( display_driver_t driver );
    47 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height );
    48 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
    49 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
    50 static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer );
    51 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    52 static gboolean glx_pbuffer_display_blank( uint32_t colour );
    53 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    55 /**
    56  * Test if a specific extension is supported. From opengl.org
    57  * @param extension extension name to check for
    58  * @return TRUE if supported, otherwise FALSE.
    59  */
    60 gboolean isServerGLXExtensionSupported( Display *display, int screen, 
    61 					const char *extension )
    62 {
    63     const char *extensions = NULL;
    64     const char *start;
    65     char *where, *terminator;
    67     /* Extension names should not have spaces. */
    68     where = strchr(extension, ' ');
    69     if (where || *extension == '\0')
    70 	return 0;
    71     extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
    72     start = extensions;
    73     for (;;) {
    74 	where = strstr((const char *) start, extension);
    75 	if (!where)
    76 	    break;
    77 	terminator = where + strlen(extension);
    78 	if (where == start || *(where - 1) == ' ')
    79 	    if (*terminator == ' ' || *terminator == '\0')
    80 		return TRUE;
    81 	start = terminator;
    82     }
    83     return FALSE;
    84 }
    86 gboolean video_glx_init( Display *display, int screen )
    87 {
    88     int major, minor;
    90     if( glx_is_initialized ) {
    91         return TRUE;
    92     }
    94     Bool result = glXQueryVersion( display, &major, &minor );
    95     if( result != False ) {
    96         glx_version = (major*100) + minor;
    97     }
    99     glx_fbconfig_supported = (glx_version >= 103 || 
   100 			      isServerGLXExtensionSupported(display, screen,
   101 						      "GLX_SGIX_fbconfig") );
   102     glx_pbuffer_supported = (glx_version >= 103 ||
   103 			     isServerGLXExtensionSupported(display, screen,
   104 						     "GLX_SGIX_pbuffer") );
   106     if( glx_fbconfig_supported ) {
   107 	int nelem;
   108         int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
   109 			     GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
   110 			     GLX_RENDER_TYPE, GLX_RGBA_BIT, 
   111 			     GLX_DEPTH_SIZE, 24, 0 };
   112 	GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
   113 						  fb_attribs, &nelem );
   115 	if( configs == NULL || nelem == 0 ) {
   116 	    /* Didn't work. Fallback to 1.2 methods */
   117 	    glx_fbconfig_supported = FALSE;
   118 	    glx_pbuffer_supported = FALSE;
   119 	} else {
   120 	    glx_fbconfig = configs[0];
   121 	    glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
   122 	    XFree(configs);
   123 	}
   124     }
   126     if( !glx_fbconfig_supported ) {
   127         int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, 0 };
   128 	glx_visual = glXChooseVisual( display, screen, attribs );
   129     }
   131     if( glx_visual == NULL ) {
   132         return FALSE;
   133     }
   135     glx_is_initialized = TRUE;
   136     return TRUE;
   137 }
   139 gboolean video_glx_init_context( Display *display, Window window )
   140 {
   141     if( glx_fbconfig_supported ) {
   142         glx_context = glXCreateNewContext( display, glx_fbconfig, 
   143 					   GLX_RGBA_TYPE, NULL, True );
   144 	if( glx_context == NULL ) {
   145 	    ERROR( "Unable to create a GLX Context.");
   146 	    return FALSE;
   147 	}
   149 	if( glXMakeContextCurrent( display, window, window, 
   150 				   glx_context ) == False ) {
   151 	    ERROR( "Unable to prepare GLX context for drawing" );
   152 	    glXDestroyContext( display, glx_context );
   153 	    return FALSE;
   154 	}
   155     } else {
   156         glx_context = glXCreateContext( display, glx_visual, None, True );
   157 	if( glx_context == NULL ) {
   158 	    ERROR( "Unable to create a GLX Context.");
   159 	    return FALSE;
   160 	}
   162 	if( glXMakeCurrent( display, window, glx_context ) == False ) {
   163 	    ERROR( "Unable to prepare GLX context for drawing" );
   164 	    glXDestroyContext( display, glx_context );
   165 	    return FALSE;
   166 	}
   167     }
   169     if( !glXIsDirect(display, glx_context) ) {
   170     	WARN( "Not using direct rendering - this is likely to be slow" );
   171     }
   173     if( glsl_is_supported() ) {
   174 	//if( !glsl_load_shaders( glsl_vertex_shader_src, glsl_fragment_shader_src ) ) {
   175 	if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
   176             WARN( "Unable to load GL shaders" );
   177         }
   178     }
   180     pvr2_setup_gl_context();
   181     video_x11_display = display;
   182     video_x11_window = window;
   184     return TRUE;
   185 }
   187 gboolean video_glx_init_driver( display_driver_t driver )
   188 {
   189     if( gl_fbo_is_supported() ) { // First preference
   190 	gl_fbo_init(driver);
   191     } else if( glx_pbuffer_supported ) {
   192 	glx_pbuffer_init(driver);
   193     } else {
   194         ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
   195         video_glx_shutdown();
   196 	return FALSE;
   197     }
   198     return TRUE;
   199 }
   202 void video_glx_shutdown()
   203 {
   204   //    texcache_gl_shutdown();
   205     glx_is_initialized = FALSE;
   206     if( glx_context != NULL ) {
   207         glXDestroyContext( video_x11_display, glx_context );
   208 	glx_context = NULL;
   209     }
   210     if( glx_visual != NULL ) {
   211         XFree(glx_visual);
   212 	glx_visual = NULL;
   213     }
   214 }
   217 XVisualInfo *video_glx_get_visual()
   218 {
   219     return glx_visual;
   220 }
   223 int video_glx_load_font( const gchar *font_name )
   224 {
   225     int lists;
   226     XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
   227     if (font == NULL)
   228 	return -1;
   230     lists = glGenLists(96);
   231     glXUseXFont(font->fid, 32, 96, lists);
   232     XFreeFont(video_x11_display, font);
   233     return lists;
   234 }
   237 void video_glx_swap_buffers( void )
   238 {
   239     glXSwapBuffers( video_x11_display, video_x11_window );
   240 }
   242 void video_glx_make_window_current( void )
   243 {
   244     glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
   245 }
   248 // Pbuffer support
   250 /**
   251  * Construct the initial frame buffers and allocate ids for everything.
   252  * The render handling driver methods are set to the fbo versions.
   253  */
   254 static void glx_pbuffer_init( display_driver_t driver ) 
   255 {
   256     glGenTextures( 1, &glx_pbuffer_texture );
   257     driver->create_render_buffer = glx_pbuffer_create_render_buffer;
   258     driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
   259     driver->set_render_target = glx_pbuffer_set_render_target;
   260     driver->display_render_buffer = glx_pbuffer_display_render_buffer;
   261     driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
   262     driver->display_blank = glx_pbuffer_display_blank;
   263     driver->read_render_buffer = glx_pbuffer_read_render_buffer;
   264 }
   266 void glx_pbuffer_shutdown()
   267 {
   268     glDeleteTextures( 1, &glx_pbuffer_texture );
   269 }
   271 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height )
   272 {
   273     int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
   274                       GLX_PRESERVED_CONTENTS, True, 0 };
   275     GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
   276     if( pb == (GLXPbuffer)NULL ) {
   277 	ERROR( "Unable to create pbuffer" );
   278 	return NULL;
   279     }
   280     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   281     buffer->width = width;
   282     buffer->height = height;
   283     buffer->buf_id = pb;
   284     return buffer;
   285 }
   287 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
   288 {
   289     glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
   290     buffer->buf_id = 0;
   291     free( buffer );
   292 }
   294 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
   295 {
   296     glFinish();
   297     if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
   298 	ERROR( "Make context current (pbuffer) failed!" );
   299     }
   300     /* setup the gl context */
   301     glViewport( 0, 0, buffer->width, buffer->height );
   302     glDrawBuffer(GL_FRONT);
   304     return TRUE;
   305 }
   307 /**
   308  * Render the texture holding the given buffer to the front window
   309  * buffer.
   310  */
   311 static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer )
   312 {
   313     glFinish();
   314     glReadBuffer( GL_FRONT );
   315     glDrawBuffer( GL_FRONT );
   316     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   317     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
   318     glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
   319     video_glx_make_window_current();
   320     gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
   321     return TRUE;
   322 }
   324 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   325 {
   326     glFinish();
   327     glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
   328     GLenum type = colour_formats[frame->colour_format].type;
   329     GLenum format = colour_formats[frame->colour_format].format;
   330     int bpp = colour_formats[frame->colour_format].bpp;
   331     int rowstride = (frame->rowstride / bpp) - frame->width;
   333     gl_reset_state();
   334     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   335     glRasterPos2f(0.375, frame->height-0.375);
   336     glPixelZoom( 1.0, 1.0 );
   337     glDrawPixels( frame->width, frame->height, format, type, frame->data );
   338     glFlush();
   339 }
   341 static gboolean glx_pbuffer_display_blank( uint32_t colour )
   342 {
   343     glFinish();
   344     video_glx_make_window_current();
   345     return gl_display_blank( colour );
   346 }
   348 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   349 					       int rowstride, int format )
   350 {
   351     glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
   352     glReadBuffer( GL_FRONT );
   353     return gl_read_render_buffer( target, buffer, rowstride, format );
   354 }
.