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 805:b355f7b3ff2e
prev736:a02d1475ccfd
next806:6ef1ce4a9dbc
author nkeynes
date Thu Aug 07 23:53:17 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Add ability to bind a render buffer to a texture, with output going to the texture.
(RTT work in progress)
file annotate diff log raw
nkeynes@545
     1
/**
nkeynes@586
     2
 * $Id$
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@653
    19
#include <string.h>
nkeynes@586
    20
#include "display.h"
nkeynes@545
    21
#include <X11/Xlib.h>
nkeynes@545
    22
#include <GL/glx.h>
nkeynes@676
    23
#include <string.h>
nkeynes@653
    24
#include "pvr2/pvr2.h"
nkeynes@653
    25
#include "pvr2/glutil.h"
nkeynes@545
    26
#include "drivers/video_glx.h"
nkeynes@653
    27
#include "drivers/video_gl.h"
nkeynes@545
    28
nkeynes@545
    29
/**
nkeynes@545
    30
 * General X11 parameters. The front-end driver is expected to set this up
nkeynes@545
    31
 * by calling video_glx_init after initializing itself.
nkeynes@545
    32
 */
nkeynes@545
    33
Display *video_x11_display = NULL;
nkeynes@545
    34
Window video_x11_window = 0;
nkeynes@545
    35
nkeynes@545
    36
static int glx_version = 100;
nkeynes@606
    37
static XVisualInfo *glx_visual = NULL;
nkeynes@545
    38
static GLXFBConfig glx_fbconfig;
nkeynes@545
    39
static GLXContext glx_context = NULL;
nkeynes@545
    40
static gboolean glx_is_initialized = FALSE;
nkeynes@545
    41
static gboolean glx_fbconfig_supported = FALSE;
nkeynes@545
    42
static gboolean glx_pbuffer_supported = FALSE;
nkeynes@669
    43
static GLuint glx_pbuffer_texture = 0; 
nkeynes@545
    44
nkeynes@545
    45
/* Prototypes for pbuffer support methods */
nkeynes@545
    46
static void glx_pbuffer_init( display_driver_t driver );
nkeynes@805
    47
static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
nkeynes@545
    48
static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
nkeynes@545
    49
static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
nkeynes@805
    50
static void glx_pbuffer_finish_render( render_buffer_t buffer );
nkeynes@669
    51
static void glx_pbuffer_display_render_buffer( render_buffer_t buffer );
nkeynes@545
    52
static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
nkeynes@669
    53
static void glx_pbuffer_display_blank( uint32_t colour );
nkeynes@545
    54
static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
nkeynes@545
    55
nkeynes@545
    56
/**
nkeynes@545
    57
 * Test if a specific extension is supported. From opengl.org
nkeynes@545
    58
 * @param extension extension name to check for
nkeynes@545
    59
 * @return TRUE if supported, otherwise FALSE.
nkeynes@545
    60
 */
nkeynes@545
    61
gboolean isServerGLXExtensionSupported( Display *display, int screen, 
nkeynes@736
    62
                                        const char *extension )
nkeynes@545
    63
{
nkeynes@545
    64
    const char *extensions = NULL;
nkeynes@545
    65
    const char *start;
nkeynes@545
    66
    char *where, *terminator;
nkeynes@545
    67
nkeynes@545
    68
    /* Extension names should not have spaces. */
nkeynes@545
    69
    where = strchr(extension, ' ');
nkeynes@545
    70
    if (where || *extension == '\0')
nkeynes@736
    71
        return 0;
nkeynes@545
    72
    extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
nkeynes@545
    73
    start = extensions;
nkeynes@545
    74
    for (;;) {
nkeynes@736
    75
        where = strstr((const char *) start, extension);
nkeynes@736
    76
        if (!where)
nkeynes@736
    77
            break;
nkeynes@736
    78
        terminator = where + strlen(extension);
nkeynes@736
    79
        if (where == start || *(where - 1) == ' ')
nkeynes@736
    80
            if (*terminator == ' ' || *terminator == '\0')
nkeynes@736
    81
                return TRUE;
nkeynes@736
    82
        start = terminator;
nkeynes@545
    83
    }
nkeynes@545
    84
    return FALSE;
nkeynes@545
    85
}
nkeynes@545
    86
nkeynes@545
    87
gboolean video_glx_init( Display *display, int screen )
nkeynes@545
    88
{
nkeynes@545
    89
    int major, minor;
nkeynes@545
    90
nkeynes@545
    91
    if( glx_is_initialized ) {
nkeynes@545
    92
        return TRUE;
nkeynes@545
    93
    }
nkeynes@545
    94
nkeynes@545
    95
    Bool result = glXQueryVersion( display, &major, &minor );
nkeynes@545
    96
    if( result != False ) {
nkeynes@545
    97
        glx_version = (major*100) + minor;
nkeynes@545
    98
    }
nkeynes@736
    99
nkeynes@545
   100
    glx_fbconfig_supported = (glx_version >= 103 || 
nkeynes@736
   101
            isServerGLXExtensionSupported(display, screen,
nkeynes@736
   102
                    "GLX_SGIX_fbconfig") );
nkeynes@545
   103
    glx_pbuffer_supported = (glx_version >= 103 ||
nkeynes@736
   104
            isServerGLXExtensionSupported(display, screen,
nkeynes@736
   105
                    "GLX_SGIX_pbuffer") );
nkeynes@545
   106
    if( glx_fbconfig_supported ) {
nkeynes@736
   107
        int nelem;
nkeynes@545
   108
        int fb_attribs[] = { GLX_DRAWABLE_TYPE, 
nkeynes@736
   109
                GLX_PBUFFER_BIT|GLX_WINDOW_BIT, 
nkeynes@736
   110
                GLX_RENDER_TYPE, GLX_RGBA_BIT, 
nkeynes@736
   111
                GLX_DEPTH_SIZE, 24, 0 };
nkeynes@736
   112
        GLXFBConfig *configs = glXChooseFBConfig( display, screen, 
nkeynes@736
   113
                fb_attribs, &nelem );
nkeynes@545
   114
nkeynes@736
   115
        if( configs == NULL || nelem == 0 ) {
nkeynes@736
   116
            /* Try a 16-bit depth buffer and see if it helps */
nkeynes@736
   117
            fb_attribs[5] = 16;
nkeynes@736
   118
            configs = glXChooseFBConfig( display, screen, fb_attribs, &nelem );
nkeynes@736
   119
            if( nelem > 0 ) {
nkeynes@676
   120
                WARN( "Using a 16-bit depth buffer - expect video glitches" );
nkeynes@676
   121
            }
nkeynes@676
   122
nkeynes@676
   123
        }
nkeynes@676
   124
        if( configs == NULL || nelem == 0 ) {
nkeynes@736
   125
            /* Still didn't work. Fallback to 1.2 methods */
nkeynes@736
   126
            glx_fbconfig_supported = FALSE;
nkeynes@736
   127
            glx_pbuffer_supported = FALSE;
nkeynes@736
   128
        } else {
nkeynes@736
   129
            glx_fbconfig = configs[0];
nkeynes@736
   130
            glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
nkeynes@736
   131
            XFree(configs);
nkeynes@736
   132
        }
nkeynes@545
   133
    }
nkeynes@545
   134
nkeynes@545
   135
    if( !glx_fbconfig_supported ) {
nkeynes@545
   136
        int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, 0 };
nkeynes@736
   137
        glx_visual = glXChooseVisual( display, screen, attribs );
nkeynes@676
   138
        if( glx_visual == NULL ) {
nkeynes@676
   139
            /* Try the 16-bit fallback here too */
nkeynes@676
   140
            attribs[2] = 16;
nkeynes@676
   141
            glx_visual = glXChooseVisual( display, screen, attribs );
nkeynes@676
   142
            if( glx_visual != NULL ) {
nkeynes@676
   143
                WARN( "Using a 16-bit depth buffer - expect video glitches" );
nkeynes@676
   144
            }
nkeynes@676
   145
        }
nkeynes@545
   146
    }
nkeynes@606
   147
nkeynes@606
   148
    if( glx_visual == NULL ) {
nkeynes@606
   149
        return FALSE;
nkeynes@606
   150
    }
nkeynes@606
   151
nkeynes@545
   152
    glx_is_initialized = TRUE;
nkeynes@545
   153
    return TRUE;
nkeynes@545
   154
}
nkeynes@545
   155
nkeynes@545
   156
gboolean video_glx_init_context( Display *display, Window window )
nkeynes@545
   157
{
nkeynes@545
   158
    if( glx_fbconfig_supported ) {
nkeynes@545
   159
        glx_context = glXCreateNewContext( display, glx_fbconfig, 
nkeynes@736
   160
                GLX_RGBA_TYPE, NULL, True );
nkeynes@736
   161
        if( glx_context == NULL ) {
nkeynes@736
   162
            ERROR( "Unable to create a GLX Context.");
nkeynes@736
   163
            return FALSE;
nkeynes@736
   164
        }
nkeynes@545
   165
nkeynes@736
   166
        if( glXMakeContextCurrent( display, window, window, 
nkeynes@736
   167
                glx_context ) == False ) {
nkeynes@736
   168
            ERROR( "Unable to prepare GLX context for drawing" );
nkeynes@736
   169
            glXDestroyContext( display, glx_context );
nkeynes@736
   170
            return FALSE;
nkeynes@736
   171
        }
nkeynes@545
   172
    } else {
nkeynes@545
   173
        glx_context = glXCreateContext( display, glx_visual, None, True );
nkeynes@736
   174
        if( glx_context == NULL ) {
nkeynes@736
   175
            ERROR( "Unable to create a GLX Context.");
nkeynes@736
   176
            return FALSE;
nkeynes@736
   177
        }
nkeynes@736
   178
nkeynes@736
   179
        if( glXMakeCurrent( display, window, glx_context ) == False ) {
nkeynes@736
   180
            ERROR( "Unable to prepare GLX context for drawing" );
nkeynes@736
   181
            glXDestroyContext( display, glx_context );
nkeynes@736
   182
            return FALSE;
nkeynes@736
   183
        }
nkeynes@545
   184
    }
nkeynes@545
   185
nkeynes@545
   186
    if( !glXIsDirect(display, glx_context) ) {
nkeynes@736
   187
        WARN( "Not using direct rendering - this is likely to be slow" );
nkeynes@545
   188
    }
nkeynes@545
   189
nkeynes@545
   190
    video_x11_display = display;
nkeynes@545
   191
    video_x11_window = window;
nkeynes@545
   192
nkeynes@545
   193
    return TRUE;
nkeynes@545
   194
}
nkeynes@545
   195
nkeynes@545
   196
gboolean video_glx_init_driver( display_driver_t driver )
nkeynes@545
   197
{
nkeynes@545
   198
    if( gl_fbo_is_supported() ) { // First preference
nkeynes@736
   199
        gl_fbo_init(driver);
nkeynes@545
   200
    } else if( glx_pbuffer_supported ) {
nkeynes@736
   201
        glx_pbuffer_init(driver);
nkeynes@545
   202
    } else {
nkeynes@545
   203
        ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
nkeynes@545
   204
        video_glx_shutdown();
nkeynes@736
   205
        return FALSE;
nkeynes@545
   206
    }
nkeynes@545
   207
    return TRUE;
nkeynes@545
   208
}
nkeynes@545
   209
nkeynes@545
   210
nkeynes@545
   211
void video_glx_shutdown()
nkeynes@545
   212
{
nkeynes@736
   213
    //    texcache_gl_shutdown();
nkeynes@545
   214
    glx_is_initialized = FALSE;
nkeynes@545
   215
    if( glx_context != NULL ) {
nkeynes@545
   216
        glXDestroyContext( video_x11_display, glx_context );
nkeynes@736
   217
        glx_context = NULL;
nkeynes@545
   218
    }
nkeynes@545
   219
    if( glx_visual != NULL ) {
nkeynes@545
   220
        XFree(glx_visual);
nkeynes@736
   221
        glx_visual = NULL;
nkeynes@545
   222
    }
nkeynes@545
   223
}
nkeynes@545
   224
nkeynes@545
   225
nkeynes@545
   226
XVisualInfo *video_glx_get_visual()
nkeynes@545
   227
{
nkeynes@545
   228
    return glx_visual;
nkeynes@545
   229
}
nkeynes@545
   230
nkeynes@545
   231
nkeynes@545
   232
int video_glx_load_font( const gchar *font_name )
nkeynes@545
   233
{
nkeynes@545
   234
    int lists;
nkeynes@545
   235
    XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
nkeynes@545
   236
    if (font == NULL)
nkeynes@736
   237
        return -1;
nkeynes@736
   238
nkeynes@545
   239
    lists = glGenLists(96);
nkeynes@545
   240
    glXUseXFont(font->fid, 32, 96, lists);
nkeynes@545
   241
    XFreeFont(video_x11_display, font);
nkeynes@545
   242
    return lists;
nkeynes@545
   243
}
nkeynes@545
   244
nkeynes@545
   245
nkeynes@545
   246
void video_glx_swap_buffers( void )
nkeynes@545
   247
{
nkeynes@545
   248
    glXSwapBuffers( video_x11_display, video_x11_window );
nkeynes@545
   249
}
nkeynes@545
   250
nkeynes@545
   251
void video_glx_make_window_current( void )
nkeynes@545
   252
{
nkeynes@545
   253
    glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
nkeynes@545
   254
}
nkeynes@545
   255
nkeynes@545
   256
nkeynes@545
   257
// Pbuffer support
nkeynes@545
   258
nkeynes@545
   259
/**
nkeynes@545
   260
 * Construct the initial frame buffers and allocate ids for everything.
nkeynes@545
   261
 * The render handling driver methods are set to the fbo versions.
nkeynes@545
   262
 */
nkeynes@545
   263
static void glx_pbuffer_init( display_driver_t driver ) 
nkeynes@545
   264
{
nkeynes@545
   265
    glGenTextures( 1, &glx_pbuffer_texture );
nkeynes@545
   266
    driver->create_render_buffer = glx_pbuffer_create_render_buffer;
nkeynes@545
   267
    driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
nkeynes@545
   268
    driver->set_render_target = glx_pbuffer_set_render_target;
nkeynes@805
   269
    driver->finish_render = glx_pbuffer_finish_render;
nkeynes@545
   270
    driver->display_render_buffer = glx_pbuffer_display_render_buffer;
nkeynes@545
   271
    driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
nkeynes@545
   272
    driver->display_blank = glx_pbuffer_display_blank;
nkeynes@545
   273
    driver->read_render_buffer = glx_pbuffer_read_render_buffer;
nkeynes@545
   274
}
nkeynes@545
   275
nkeynes@545
   276
void glx_pbuffer_shutdown()
nkeynes@545
   277
{
nkeynes@545
   278
    glDeleteTextures( 1, &glx_pbuffer_texture );
nkeynes@545
   279
}
nkeynes@545
   280
nkeynes@805
   281
static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
nkeynes@545
   282
{
nkeynes@545
   283
    int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
nkeynes@736
   284
            GLX_PRESERVED_CONTENTS, True, 0 };
nkeynes@545
   285
    GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
nkeynes@545
   286
    if( pb == (GLXPbuffer)NULL ) {
nkeynes@736
   287
        ERROR( "Unable to create pbuffer" );
nkeynes@736
   288
        return NULL;
nkeynes@545
   289
    }
nkeynes@545
   290
    render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
nkeynes@545
   291
    buffer->width = width;
nkeynes@545
   292
    buffer->height = height;
nkeynes@545
   293
    buffer->buf_id = pb;
nkeynes@805
   294
    buffer->tex_id = tex_id;
nkeynes@545
   295
    return buffer;
nkeynes@545
   296
}
nkeynes@545
   297
nkeynes@545
   298
static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
nkeynes@545
   299
{
nkeynes@545
   300
    glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
nkeynes@545
   301
    buffer->buf_id = 0;
nkeynes@545
   302
    free( buffer );
nkeynes@545
   303
}
nkeynes@545
   304
nkeynes@545
   305
static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
nkeynes@545
   306
{
nkeynes@545
   307
    glFinish();
nkeynes@545
   308
    if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
nkeynes@736
   309
        ERROR( "Make context current (pbuffer) failed!" );
nkeynes@545
   310
    }
nkeynes@545
   311
    /* setup the gl context */
nkeynes@545
   312
    glViewport( 0, 0, buffer->width, buffer->height );
nkeynes@545
   313
    glDrawBuffer(GL_FRONT);
nkeynes@736
   314
nkeynes@545
   315
    return TRUE;
nkeynes@545
   316
}
nkeynes@545
   317
nkeynes@805
   318
static void glx_pbuffer_finish_render( render_buffer_t buffer )
nkeynes@805
   319
{
nkeynes@805
   320
    glFinish();
nkeynes@805
   321
    if( buffer->tex_id != 0 ) {
nkeynes@805
   322
        // The pbuffer should already be the current context, but just in case...
nkeynes@805
   323
        glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
nkeynes@805
   324
        glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->tex_id );
nkeynes@805
   325
        glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
nkeynes@805
   326
    }
nkeynes@805
   327
}
nkeynes@805
   328
    
nkeynes@805
   329
nkeynes@545
   330
/**
nkeynes@545
   331
 * Render the texture holding the given buffer to the front window
nkeynes@545
   332
 * buffer.
nkeynes@545
   333
 */
nkeynes@669
   334
static void glx_pbuffer_display_render_buffer( render_buffer_t buffer )
nkeynes@545
   335
{
nkeynes@545
   336
    glFinish();
nkeynes@545
   337
    glReadBuffer( GL_FRONT );
nkeynes@545
   338
    glDrawBuffer( GL_FRONT );
nkeynes@545
   339
    glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
nkeynes@545
   340
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
nkeynes@545
   341
    glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
nkeynes@545
   342
    video_glx_make_window_current();
nkeynes@545
   343
    gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
nkeynes@545
   344
}
nkeynes@545
   345
nkeynes@545
   346
static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
nkeynes@545
   347
{
nkeynes@545
   348
    glFinish();
nkeynes@545
   349
    glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
nkeynes@545
   350
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@545
   351
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@545
   352
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@545
   353
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@736
   354
nkeynes@545
   355
    gl_reset_state();
nkeynes@545
   356
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@545
   357
    glRasterPos2f(0.375, frame->height-0.375);
nkeynes@545
   358
    glPixelZoom( 1.0, 1.0 );
nkeynes@545
   359
    glDrawPixels( frame->width, frame->height, format, type, frame->data );
nkeynes@545
   360
    glFlush();
nkeynes@545
   361
}
nkeynes@545
   362
nkeynes@669
   363
static void glx_pbuffer_display_blank( uint32_t colour )
nkeynes@545
   364
{
nkeynes@545
   365
    glFinish();
nkeynes@545
   366
    video_glx_make_window_current();
nkeynes@669
   367
    gl_display_blank( colour );
nkeynes@545
   368
}
nkeynes@545
   369
nkeynes@545
   370
static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@736
   371
                                                int rowstride, int format )
nkeynes@545
   372
{
nkeynes@545
   373
    glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
nkeynes@545
   374
    glReadBuffer( GL_FRONT );
nkeynes@545
   375
    return gl_read_render_buffer( target, buffer, rowstride, format );
nkeynes@545
   376
}
nkeynes@545
   377
nkeynes@545
   378
.