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 545:fdcdcd8b9fd1
next561:533f6b478071
next586:2a3ba82cf243
author nkeynes
date Thu Nov 29 09:28:28 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Refactor GLX support and implement pbuffer rendering support
file annotate diff log raw
nkeynes@545
     1
/**
nkeynes@545
     2
 * $Id: video_x11.c,v 1.20 2007-10-31 12:05:23 nkeynes Exp $
nkeynes@545
     3
 *
nkeynes@545
     4
 * Shared functions for all X11-based display drivers.
nkeynes@545
     5
 *
nkeynes@545
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@545
     7
 *
nkeynes@545
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@545
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@545
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@545
    11
 * (at your option) any later version.
nkeynes@545
    12
 *
nkeynes@545
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@545
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@545
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@545
    16
 * GNU General Public License for more details.
nkeynes@545
    17
 */
nkeynes@545
    18
nkeynes@545
    19
#include <X11/Xlib.h>
nkeynes@545
    20
#include <GL/glx.h>
nkeynes@545
    21
#include <GL/gl.h>
nkeynes@545
    22
#include "dream.h"
nkeynes@545
    23
#include "pvr2/pvr2.h"
nkeynes@545
    24
#include "drivers/video_glx.h"
nkeynes@545
    25
#include "drivers/gl_common.h"
nkeynes@545
    26
nkeynes@545
    27
/**
nkeynes@545
    28
 * General X11 parameters. The front-end driver is expected to set this up
nkeynes@545
    29
 * by calling video_glx_init after initializing itself.
nkeynes@545
    30
 */
nkeynes@545
    31
Display *video_x11_display = NULL;
nkeynes@545
    32
Window video_x11_window = 0;
nkeynes@545
    33
static gboolean glsl_loaded = FALSE;
nkeynes@545
    34
nkeynes@545
    35
static int glx_version = 100;
nkeynes@545
    36
static XVisualInfo *glx_visual;
nkeynes@545
    37
static GLXFBConfig glx_fbconfig;
nkeynes@545
    38
static GLXContext glx_context = NULL;
nkeynes@545
    39
static gboolean glx_is_initialized = FALSE;
nkeynes@545
    40
static gboolean glx_fbconfig_supported = FALSE;
nkeynes@545
    41
static gboolean glx_pbuffer_supported = FALSE;
nkeynes@545
    42
static int glx_pbuffer_texture = 0; 
nkeynes@545
    43
nkeynes@545
    44
/* Prototypes for pbuffer support methods */
nkeynes@545
    45
static void glx_pbuffer_init( display_driver_t driver );
nkeynes@545
    46
static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height );
nkeynes@545
    47
static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
nkeynes@545
    48
static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
nkeynes@545
    49
static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer );
nkeynes@545
    50
static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
nkeynes@545
    51
static gboolean glx_pbuffer_display_blank( uint32_t colour );
nkeynes@545
    52
static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
nkeynes@545
    53
nkeynes@545
    54
/**
nkeynes@545
    55
 * Test if a specific extension is supported. From opengl.org
nkeynes@545
    56
 * @param extension extension name to check for
nkeynes@545
    57
 * @return TRUE if supported, otherwise FALSE.
nkeynes@545
    58
 */
nkeynes@545
    59
gboolean isServerGLXExtensionSupported( Display *display, int screen, 
nkeynes@545
    60
					const char *extension )
nkeynes@545
    61
{
nkeynes@545
    62
    const char *extensions = NULL;
nkeynes@545
    63
    const char *start;
nkeynes@545
    64
    char *where, *terminator;
nkeynes@545
    65
nkeynes@545
    66
    /* Extension names should not have spaces. */
nkeynes@545
    67
    where = strchr(extension, ' ');
nkeynes@545
    68
    if (where || *extension == '\0')
nkeynes@545
    69
	return 0;
nkeynes@545
    70
    extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
nkeynes@545
    71
    start = extensions;
nkeynes@545
    72
    for (;;) {
nkeynes@545
    73
	where = strstr((const char *) start, extension);
nkeynes@545
    74
	if (!where)
nkeynes@545
    75
	    break;
nkeynes@545
    76
	terminator = where + strlen(extension);
nkeynes@545
    77
	if (where == start || *(where - 1) == ' ')
nkeynes@545
    78
	    if (*terminator == ' ' || *terminator == '\0')
nkeynes@545
    79
		return TRUE;
nkeynes@545
    80
	start = terminator;
nkeynes@545
    81
    }
nkeynes@545
    82
    return FALSE;
nkeynes@545
    83
}
nkeynes@545
    84
nkeynes@545
    85
gboolean video_glx_init( Display *display, int screen )
nkeynes@545
    86
{
nkeynes@545
    87
    int major, minor;
nkeynes@545
    88
nkeynes@545
    89
    if( glx_is_initialized ) {
nkeynes@545
    90
        return TRUE;
nkeynes@545
    91
    }
nkeynes@545
    92
nkeynes@545
    93
    Bool result = glXQueryVersion( display, &major, &minor );
nkeynes@545
    94
    if( result != False ) {
nkeynes@545
    95
        glx_version = (major*100) + minor;
nkeynes@545
    96
    }
nkeynes@545
    97
    
nkeynes@545
    98
    glx_fbconfig_supported = (glx_version >= 103 || 
nkeynes@545
    99
			      isServerGLXExtensionSupported(display, screen,
nkeynes@545
   100
						      "GLX_SGIX_fbconfig") );
nkeynes@545
   101
    glx_pbuffer_supported = (glx_version >= 103 ||
nkeynes@545
   102
			     isServerGLXExtensionSupported(display, screen,
nkeynes@545
   103
						     "GLX_SGIX_pbuffer") );
nkeynes@545
   104
    
nkeynes@545
   105
    if( glx_fbconfig_supported ) {
nkeynes@545
   106
	int nelem;
nkeynes@545
   107
        int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
nkeynes@545
   108
			     GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
nkeynes@545
   109
			     GLX_RENDER_TYPE, GLX_RGBA_BIT, 
nkeynes@545
   110
			     GLX_DEPTH_SIZE, 24, 0 };
nkeynes@545
   111
	GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
nkeynes@545
   112
						  fb_attribs, &nelem );
nkeynes@545
   113
nkeynes@545
   114
	if( configs == NULL || nelem == 0 ) {
nkeynes@545
   115
	    /* Didn't work. Fallback to 1.2 methods */
nkeynes@545
   116
	    glx_fbconfig_supported = FALSE;
nkeynes@545
   117
	    glx_pbuffer_supported = FALSE;
nkeynes@545
   118
	} else {
nkeynes@545
   119
	    glx_fbconfig = configs[0];
nkeynes@545
   120
	    glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
nkeynes@545
   121
	    XFree(configs);
nkeynes@545
   122
	}
nkeynes@545
   123
    }
nkeynes@545
   124
nkeynes@545
   125
    if( !glx_fbconfig_supported ) {
nkeynes@545
   126
        int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, 0 };
nkeynes@545
   127
	glx_visual = glXChooseVisual( display, screen, attribs );
nkeynes@545
   128
    }
nkeynes@545
   129
    glx_is_initialized = TRUE;
nkeynes@545
   130
    return TRUE;
nkeynes@545
   131
}
nkeynes@545
   132
nkeynes@545
   133
gboolean video_glx_init_context( Display *display, Window window )
nkeynes@545
   134
{
nkeynes@545
   135
    if( glx_fbconfig_supported ) {
nkeynes@545
   136
        glx_context = glXCreateNewContext( display, glx_fbconfig, 
nkeynes@545
   137
					   GLX_RGBA_TYPE, NULL, True );
nkeynes@545
   138
	if( glx_context == NULL ) {
nkeynes@545
   139
	    ERROR( "Unable to create a GLX Context.");
nkeynes@545
   140
	    return FALSE;
nkeynes@545
   141
	}
nkeynes@545
   142
nkeynes@545
   143
	if( glXMakeContextCurrent( display, window, window, 
nkeynes@545
   144
				   glx_context ) == False ) {
nkeynes@545
   145
	    ERROR( "Unable to prepare GLX context for drawing" );
nkeynes@545
   146
	    glXDestroyContext( display, glx_context );
nkeynes@545
   147
	    return FALSE;
nkeynes@545
   148
	}
nkeynes@545
   149
    } else {
nkeynes@545
   150
        glx_context = glXCreateContext( display, glx_visual, None, True );
nkeynes@545
   151
	if( glx_context == NULL ) {
nkeynes@545
   152
	    ERROR( "Unable to create a GLX Context.");
nkeynes@545
   153
	    return FALSE;
nkeynes@545
   154
	}
nkeynes@545
   155
	
nkeynes@545
   156
	if( glXMakeCurrent( display, window, glx_context ) == False ) {
nkeynes@545
   157
	    ERROR( "Unable to prepare GLX context for drawing" );
nkeynes@545
   158
	    glXDestroyContext( display, glx_context );
nkeynes@545
   159
	    return FALSE;
nkeynes@545
   160
	}
nkeynes@545
   161
    }
nkeynes@545
   162
nkeynes@545
   163
    if( !glXIsDirect(display, glx_context) ) {
nkeynes@545
   164
    	WARN( "Not using direct rendering - this is likely to be slow" );
nkeynes@545
   165
    }
nkeynes@545
   166
nkeynes@545
   167
    texcache_gl_init();
nkeynes@545
   168
    video_x11_display = display;
nkeynes@545
   169
    video_x11_window = window;
nkeynes@545
   170
nkeynes@545
   171
    return TRUE;
nkeynes@545
   172
}
nkeynes@545
   173
nkeynes@545
   174
gboolean video_glx_init_driver( display_driver_t driver )
nkeynes@545
   175
{
nkeynes@545
   176
    if( gl_fbo_is_supported() ) { // First preference
nkeynes@545
   177
	gl_fbo_init(driver);
nkeynes@545
   178
    } else if( glx_pbuffer_supported ) {
nkeynes@545
   179
	glx_pbuffer_init(driver);
nkeynes@545
   180
    } else {
nkeynes@545
   181
        ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
nkeynes@545
   182
        video_glx_shutdown();
nkeynes@545
   183
	return FALSE;
nkeynes@545
   184
    }
nkeynes@545
   185
    return TRUE;
nkeynes@545
   186
}
nkeynes@545
   187
nkeynes@545
   188
nkeynes@545
   189
void video_glx_shutdown()
nkeynes@545
   190
{
nkeynes@545
   191
  //    texcache_gl_shutdown();
nkeynes@545
   192
    glx_is_initialized = FALSE;
nkeynes@545
   193
    if( glx_context != NULL ) {
nkeynes@545
   194
        glXDestroyContext( video_x11_display, glx_context );
nkeynes@545
   195
	glx_context = NULL;
nkeynes@545
   196
    }
nkeynes@545
   197
    if( glx_visual != NULL ) {
nkeynes@545
   198
        XFree(glx_visual);
nkeynes@545
   199
	glx_visual = NULL;
nkeynes@545
   200
    }
nkeynes@545
   201
}
nkeynes@545
   202
nkeynes@545
   203
nkeynes@545
   204
XVisualInfo *video_glx_get_visual()
nkeynes@545
   205
{
nkeynes@545
   206
    return glx_visual;
nkeynes@545
   207
}
nkeynes@545
   208
nkeynes@545
   209
nkeynes@545
   210
int video_glx_load_font( const gchar *font_name )
nkeynes@545
   211
{
nkeynes@545
   212
    int lists;
nkeynes@545
   213
    XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
nkeynes@545
   214
    if (font == NULL)
nkeynes@545
   215
	return -1;
nkeynes@545
   216
    
nkeynes@545
   217
    lists = glGenLists(96);
nkeynes@545
   218
    glXUseXFont(font->fid, 32, 96, lists);
nkeynes@545
   219
    XFreeFont(video_x11_display, font);
nkeynes@545
   220
    return lists;
nkeynes@545
   221
}
nkeynes@545
   222
nkeynes@545
   223
nkeynes@545
   224
void video_glx_swap_buffers( void )
nkeynes@545
   225
{
nkeynes@545
   226
    glXSwapBuffers( video_x11_display, video_x11_window );
nkeynes@545
   227
}
nkeynes@545
   228
nkeynes@545
   229
void video_glx_make_window_current( void )
nkeynes@545
   230
{
nkeynes@545
   231
    glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
nkeynes@545
   232
}
nkeynes@545
   233
nkeynes@545
   234
nkeynes@545
   235
// Pbuffer support
nkeynes@545
   236
nkeynes@545
   237
/**
nkeynes@545
   238
 * Construct the initial frame buffers and allocate ids for everything.
nkeynes@545
   239
 * The render handling driver methods are set to the fbo versions.
nkeynes@545
   240
 */
nkeynes@545
   241
static void glx_pbuffer_init( display_driver_t driver ) 
nkeynes@545
   242
{
nkeynes@545
   243
    glGenTextures( 1, &glx_pbuffer_texture );
nkeynes@545
   244
    driver->create_render_buffer = glx_pbuffer_create_render_buffer;
nkeynes@545
   245
    driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
nkeynes@545
   246
    driver->set_render_target = glx_pbuffer_set_render_target;
nkeynes@545
   247
    driver->display_render_buffer = glx_pbuffer_display_render_buffer;
nkeynes@545
   248
    driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
nkeynes@545
   249
    driver->display_blank = glx_pbuffer_display_blank;
nkeynes@545
   250
    driver->read_render_buffer = glx_pbuffer_read_render_buffer;
nkeynes@545
   251
}
nkeynes@545
   252
nkeynes@545
   253
void glx_pbuffer_shutdown()
nkeynes@545
   254
{
nkeynes@545
   255
    glDeleteTextures( 1, &glx_pbuffer_texture );
nkeynes@545
   256
}
nkeynes@545
   257
nkeynes@545
   258
static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height )
nkeynes@545
   259
{
nkeynes@545
   260
    int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
nkeynes@545
   261
                      GLX_PRESERVED_CONTENTS, True, 0 };
nkeynes@545
   262
    GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
nkeynes@545
   263
    if( pb == (GLXPbuffer)NULL ) {
nkeynes@545
   264
	ERROR( "Unable to create pbuffer" );
nkeynes@545
   265
	return NULL;
nkeynes@545
   266
    }
nkeynes@545
   267
    render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
nkeynes@545
   268
    buffer->width = width;
nkeynes@545
   269
    buffer->height = height;
nkeynes@545
   270
    buffer->buf_id = pb;
nkeynes@545
   271
    return buffer;
nkeynes@545
   272
}
nkeynes@545
   273
nkeynes@545
   274
static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
nkeynes@545
   275
{
nkeynes@545
   276
    glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
nkeynes@545
   277
    buffer->buf_id = 0;
nkeynes@545
   278
    free( buffer );
nkeynes@545
   279
}
nkeynes@545
   280
nkeynes@545
   281
static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
nkeynes@545
   282
{
nkeynes@545
   283
    glFinish();
nkeynes@545
   284
    if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
nkeynes@545
   285
	ERROR( "Make context current (pbuffer) failed!" );
nkeynes@545
   286
    }
nkeynes@545
   287
    /* setup the gl context */
nkeynes@545
   288
    glViewport( 0, 0, buffer->width, buffer->height );
nkeynes@545
   289
    glDrawBuffer(GL_FRONT);
nkeynes@545
   290
    
nkeynes@545
   291
    return TRUE;
nkeynes@545
   292
}
nkeynes@545
   293
nkeynes@545
   294
/**
nkeynes@545
   295
 * Render the texture holding the given buffer to the front window
nkeynes@545
   296
 * buffer.
nkeynes@545
   297
 */
nkeynes@545
   298
static gboolean glx_pbuffer_display_render_buffer( render_buffer_t buffer )
nkeynes@545
   299
{
nkeynes@545
   300
    glFinish();
nkeynes@545
   301
    glReadBuffer( GL_FRONT );
nkeynes@545
   302
    glDrawBuffer( GL_FRONT );
nkeynes@545
   303
    glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
nkeynes@545
   304
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
nkeynes@545
   305
    glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
nkeynes@545
   306
    video_glx_make_window_current();
nkeynes@545
   307
    gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
nkeynes@545
   308
    return TRUE;
nkeynes@545
   309
}
nkeynes@545
   310
nkeynes@545
   311
static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
nkeynes@545
   312
{
nkeynes@545
   313
    glFinish();
nkeynes@545
   314
    glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
nkeynes@545
   315
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@545
   316
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@545
   317
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@545
   318
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@545
   319
    
nkeynes@545
   320
    gl_reset_state();
nkeynes@545
   321
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@545
   322
    glRasterPos2f(0.375, frame->height-0.375);
nkeynes@545
   323
    glPixelZoom( 1.0, 1.0 );
nkeynes@545
   324
    glDrawPixels( frame->width, frame->height, format, type, frame->data );
nkeynes@545
   325
    glFlush();
nkeynes@545
   326
}
nkeynes@545
   327
nkeynes@545
   328
static gboolean glx_pbuffer_display_blank( uint32_t colour )
nkeynes@545
   329
{
nkeynes@545
   330
    glFinish();
nkeynes@545
   331
    video_glx_make_window_current();
nkeynes@545
   332
    return gl_display_blank( colour );
nkeynes@545
   333
}
nkeynes@545
   334
nkeynes@545
   335
static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@545
   336
					       int rowstride, int format )
nkeynes@545
   337
{
nkeynes@545
   338
    glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
nkeynes@545
   339
    glReadBuffer( GL_FRONT );
nkeynes@545
   340
    return gl_read_render_buffer( target, buffer, rowstride, format );
nkeynes@545
   341
}
nkeynes@545
   342
nkeynes@545
   343
.