Search
lxdream.org :: lxdream/src/drivers/gl_fbo.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/gl_fbo.c
changeset 758:99ae000d4e09
prev736:a02d1475ccfd
next798:eb5c0d1863cd
author nkeynes
date Mon Jul 21 00:37:13 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix compiler warnings
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * GL framebuffer-based driver shell. This requires the EXT_framebuffer_object
     5  * extension, but is much nicer/faster/etc than pbuffers when it's available.
     6  * This is (optionally) used indirectly by the top-level GLX driver.
     7  *
     8  * Strategy-wise, we maintain 2 framebuffers with up to 4 target colour
     9  * buffers a piece. Effectively this reserves one fb for display output,
    10  * and the other for texture output (each fb has a single size).
    11  *
    12  * Copyright (c) 2005 Nathan Keynes.
    13  *
    14  * This program is free software; you can redistribute it and/or modify
    15  * it under the terms of the GNU General Public License as published by
    16  * the Free Software Foundation; either version 2 of the License, or
    17  * (at your option) any later version.
    18  *
    19  * This program is distributed in the hope that it will be useful,
    20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    22  * GNU General Public License for more details.
    23  */
    25 #define GL_GLEXT_PROTOTYPES 1
    27 #include <stdlib.h>
    28 #include "lxdream.h"
    29 #include "display.h"
    30 #include "drivers/video_gl.h"
    31 #include "pvr2/glutil.h"
    33 #ifdef HAVE_OPENGL_FBO
    35 #define MAX_FRAMEBUFFERS 2
    36 #define MAX_TEXTURES_PER_FB 4
    38 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height );
    39 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer );
    40 static gboolean gl_fbo_set_render_target( render_buffer_t buffer );
    41 static void gl_fbo_display_render_buffer( render_buffer_t buffer );
    42 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    43 static void gl_fbo_display_blank( uint32_t colour );
    44 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    46 extern uint32_t video_width, video_height;
    48 /**
    49  * Framebuffer info structure
    50  */
    51 struct gl_fbo_info {
    52     GLuint fb_id;
    53     GLuint depth_id;
    54     GLuint stencil_id;
    55     GLuint tex_ids[MAX_TEXTURES_PER_FB];
    56     int width, height;
    57 };
    59 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
    60 const static int ATTACHMENT_POINTS[MAX_TEXTURES_PER_FB] = {
    61         GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, 
    62         GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT };
    63 static int last_used_fbo;
    65 gboolean gl_fbo_is_supported()
    66 {
    67     return isGLExtensionSupported("GL_EXT_framebuffer_object");
    68 }
    70 /**
    71  * Construct the initial frame buffers and allocate ids for everything.
    72  * The render handling driver methods are set to the fbo versions.
    73  */
    74 void gl_fbo_init( display_driver_t driver ) 
    75 {
    76     int i,j;
    77     GLuint fbids[MAX_FRAMEBUFFERS];
    78     GLuint rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
    80     glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids[0] );
    81     glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids[0] );
    82     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
    83         fbo[i].fb_id = fbids[i];
    84         fbo[i].depth_id = rbids[i*2];
    85         fbo[i].stencil_id = rbids[i*2+1];
    86         fbo[i].width = -1;
    87         fbo[i].height = -1;
    88         for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
    89             fbo[i].tex_ids[j] = -1;
    90         }
    91     }
    92     last_used_fbo = 0;
    94     driver->create_render_buffer = gl_fbo_create_render_buffer;
    95     driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
    96     driver->set_render_target = gl_fbo_set_render_target;
    97     driver->display_render_buffer = gl_fbo_display_render_buffer;
    98     driver->load_frame_buffer = gl_fbo_load_frame_buffer;
    99     driver->display_blank = gl_fbo_display_blank;
   100     driver->read_render_buffer = gl_fbo_read_render_buffer;
   102     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
   103     glDrawBuffer(GL_FRONT);
   104     glReadBuffer(GL_FRONT);
   105 }
   107 void gl_fbo_shutdown()
   108 {
   109     int i;
   110     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   111     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   112         glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
   113         glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
   114     }
   115 }
   117 void gl_fbo_setup_framebuffer( int bufno, int width, int height )
   118 {
   119     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
   120     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   121     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
   122     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
   123                                  GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   124     /* Stencil doesn't work on ATI, and we're not using it at the moment anyway, so...
   125     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   126     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height);
   127     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
   128 				 GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   129      */
   130     fbo[bufno].width = width;
   131     fbo[bufno].height = height;
   132 }
   134 int gl_fbo_get_framebuffer( int width, int height ) 
   135 {
   136     int bufno = -1, i;
   137     /* find a compatible framebuffer context */
   138     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   139         if( fbo[i].width == -1 && bufno == -1 ) {
   140             bufno = i;
   141         } else if( fbo[i].width == width && fbo[i].height == height ) {
   142             bufno = i;
   143             break;
   144         }
   145     }
   146     if( bufno == -1 ) {
   147         bufno = last_used_fbo + 1;
   148         if( bufno > MAX_FRAMEBUFFERS ) {
   149             bufno = 0;
   150         }
   151         last_used_fbo = bufno;
   152     }
   153     if( fbo[bufno].width == width && fbo[bufno].height == height ) {
   154         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
   155     } else {
   156         gl_fbo_setup_framebuffer( bufno, width, height );
   157     } 
   158     return bufno;
   159 }
   161 /**
   162  * Attach a texture to the framebuffer. The texture must already be initialized
   163  * to the correct dimensions etc.
   164  */
   165 static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
   166     int attach = -1, i;
   167     for( i=0; i<MAX_TEXTURES_PER_FB; i++ ) {
   168         if( fbo[fbo_no].tex_ids[i] == tex_id ) {
   169             glDrawBuffer(ATTACHMENT_POINTS[i]);
   170             glReadBuffer(ATTACHMENT_POINTS[i]); 
   171             return ATTACHMENT_POINTS[i]; // already attached
   172         } else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
   173             attach = i;
   174         }
   175     }
   176     if( attach == -1 ) {
   177         /* should never happen */
   178         attach = 0;
   179     }
   180     fbo[fbo_no].tex_ids[attach] = tex_id;
   181     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, 0 ); // Ensure the output texture is unbound
   182     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[attach], 
   183                               GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
   184     /* Set draw/read buffers by default */
   185     glDrawBuffer(ATTACHMENT_POINTS[attach]);
   186     glReadBuffer(ATTACHMENT_POINTS[attach]); 
   189     GLint status = glGetError();
   190     if( status != GL_NO_ERROR ) {
   191         ERROR( "GL error setting render target (%x)!", status );
   192     }
   193     status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   194     if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
   195         ERROR( "Framebuffer failure: %x", status );
   196         exit(1);
   197     }
   199     return ATTACHMENT_POINTS[attach];
   200 }
   202 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height )
   203 {
   204     GLuint tex;
   205     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   206     buffer->width = width;
   207     buffer->height = height;
   208     glGenTextures( 1, &tex );
   209     buffer->buf_id = tex;
   210     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex );
   211     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
   212     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
   213     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   214     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   215     glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
   216     return buffer;
   217 }
   219 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
   220 {
   221     int i,j;
   222     GLuint tex = buffer->buf_id;
   223     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   224         for( j=0; j < MAX_TEXTURES_PER_FB; j++ ) {
   225             if( fbo[i].tex_ids[j] == buffer->buf_id ) {
   226                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[i].fb_id);
   227                 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[j], 
   228                                           GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
   229                 fbo[i].tex_ids[j] = -1;
   230             }
   231         }
   232     }
   234     glDeleteTextures( 1, &tex );
   235     buffer->buf_id = 0;
   236     free( buffer );
   237 }
   239 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   240 {
   241     glFinish();
   242     glGetError();
   243     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   244     gl_fbo_attach_texture( fb, buffer->buf_id );
   245     /* setup the gl context */
   246     glViewport( 0, 0, buffer->width, buffer->height );
   248     return TRUE;
   249 }
   251 /**
   252  * Render the texture holding the given buffer to the front window
   253  * buffer.
   254  */
   255 static void gl_fbo_display_render_buffer( render_buffer_t buffer )
   256 {
   257     glFinish();
   258     gl_fbo_detach();
   259     gl_display_render_buffer( buffer );
   260 }
   262 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   263 {
   264     glFinish();
   265     gl_fbo_detach();
   266     gl_load_frame_buffer( frame, buffer->buf_id );
   267 }
   269 static void gl_fbo_display_blank( uint32_t colour )
   270 {
   271     glFinish();
   272     gl_fbo_detach();
   273     gl_display_blank( colour );
   274 }
   276 void gl_fbo_detach()
   277 {
   278     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   279     glDrawBuffer( GL_FRONT );
   280     glReadBuffer( GL_FRONT );
   281 }    
   283 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   284                                            int rowstride, int format )
   285 {
   286     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   287     gl_fbo_attach_texture( fb, buffer->buf_id );
   288     return gl_read_render_buffer( target, buffer, rowstride, format );
   289 }
   291 #else
   292 gboolean gl_fbo_is_supported()
   293 {
   294     return FALSE;
   295 }
   297 void gl_fbo_init( display_driver_t driver ) 
   298 {
   299 }
   301 #endif
.