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 805:b355f7b3ff2e
prev798:eb5c0d1863cd
next857:3d8944884eaa
author nkeynes
date Thu Aug 07 23:53:17 2008 +0000 (13 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)
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 16
    38 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
    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_finish_render( render_buffer_t buffer );
    42 static void gl_fbo_display_render_buffer( render_buffer_t buffer );
    43 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    44 static void gl_fbo_display_blank( uint32_t colour );
    45 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    47 extern uint32_t video_width, video_height;
    49 /**
    50  * Framebuffer info structure
    51  */
    52 struct gl_fbo_info {
    53     GLuint fb_id;
    54     GLuint depth_id;
    55     GLuint stencil_id;
    56     GLuint tex_ids[MAX_TEXTURES_PER_FB];
    57     int width, height;
    58 };
    60 static GLint gl_fbo_max_attachments = 0;
    61 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
    63 #define ATTACHMENT_POINT(n) (GL_COLOR_ATTACHMENT0_EXT+(n))
    64 static int last_used_fbo;
    66 gboolean gl_fbo_is_supported()
    67 {
    68     return isGLExtensionSupported("GL_EXT_framebuffer_object");
    69 }
    71 /**
    72  * Construct the initial frame buffers and allocate ids for everything.
    73  * The render handling driver methods are set to the fbo versions.
    74  */
    75 void gl_fbo_init( display_driver_t driver ) 
    76 {
    77     int i,j;
    78     GLuint fbids[MAX_FRAMEBUFFERS];
    79     GLuint rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
    81     glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &gl_fbo_max_attachments);
    82     glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids[0] );
    83     glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids[0] );
    84     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
    85         fbo[i].fb_id = fbids[i];
    86         fbo[i].depth_id = rbids[i*2];
    87         fbo[i].stencil_id = rbids[i*2+1];
    88         fbo[i].width = -1;
    89         fbo[i].height = -1;
    90         for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
    91             fbo[i].tex_ids[j] = -1;
    92         }
    93     }
    94     last_used_fbo = 0;
    96     driver->create_render_buffer = gl_fbo_create_render_buffer;
    97     driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
    98     driver->set_render_target = gl_fbo_set_render_target;
    99     driver->finish_render = gl_fbo_finish_render;
   100     driver->display_render_buffer = gl_fbo_display_render_buffer;
   101     driver->load_frame_buffer = gl_fbo_load_frame_buffer;
   102     driver->display_blank = gl_fbo_display_blank;
   103     driver->read_render_buffer = gl_fbo_read_render_buffer;
   105     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
   106     glDrawBuffer(GL_FRONT);
   107     glReadBuffer(GL_FRONT);
   108 }
   110 void gl_fbo_shutdown()
   111 {
   112     int i;
   113     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   114     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   115         glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
   116         glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
   117     }
   118 }
   120 void gl_fbo_setup_framebuffer( int bufno, int width, int height )
   121 {
   122     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
   123     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   124     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
   125     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
   126                                  GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   127     /* Stencil doesn't work on ATI, and we're not using it at the moment anyway, so...
   128     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   129     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height);
   130     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
   131 				 GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   132      */
   133     fbo[bufno].width = width;
   134     fbo[bufno].height = height;
   135 }
   137 int gl_fbo_get_framebuffer( int width, int height ) 
   138 {
   139     int bufno = -1, i;
   140     /* find a compatible framebuffer context */
   141     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   142         if( fbo[i].width == -1 && bufno == -1 ) {
   143             bufno = i;
   144         } else if( fbo[i].width == width && fbo[i].height == height ) {
   145             bufno = i;
   146             break;
   147         }
   148     }
   149     if( bufno == -1 ) {
   150         bufno = last_used_fbo + 1;
   151         if( bufno > MAX_FRAMEBUFFERS ) {
   152             bufno = 0;
   153         }
   154         last_used_fbo = bufno;
   155     }
   156     if( fbo[bufno].width == width && fbo[bufno].height == height ) {
   157         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
   158     } else {
   159         gl_fbo_setup_framebuffer( bufno, width, height );
   160     } 
   161     return bufno;
   162 }
   164 /**
   165  * Attach a texture to the framebuffer. The texture must already be initialized
   166  * to the correct dimensions etc.
   167  */
   168 static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
   169     int attach = -1, i;
   170     for( i=0; i<gl_fbo_max_attachments; i++ ) {
   171         if( fbo[fbo_no].tex_ids[i] == tex_id ) {
   172             glDrawBuffer(ATTACHMENT_POINT(i));
   173             glReadBuffer(ATTACHMENT_POINT(i)); 
   174             return ATTACHMENT_POINT(i); // already attached
   175         } else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
   176             attach = i;
   177         }
   178     }
   179     if( attach == -1 ) {
   180         attach = 0;
   181     }
   182     fbo[fbo_no].tex_ids[attach] = tex_id;
   183     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, 0 ); // Ensure the output texture is unbound
   184     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINT(attach), 
   185                               GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
   186     /* Set draw/read buffers by default */
   187     glDrawBuffer(ATTACHMENT_POINT(attach));
   188     glReadBuffer(ATTACHMENT_POINT(attach)); 
   191     GLint status = glGetError();
   192     if( status != GL_NO_ERROR ) {
   193         ERROR( "GL error setting render target (%x)!", status );
   194     }
   195     status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   196     if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
   197         ERROR( "Framebuffer failure: %x", status );
   198         exit(1);
   199     }
   201     return ATTACHMENT_POINT(attach);
   202 }
   204 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   205 {
   206     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   207     buffer->width = width;
   208     buffer->height = height;
   209     buffer->tex_id = tex_id;
   210     if( tex_id == 0 ) {
   211         GLuint tex;
   212         glGenTextures( 1, &tex );
   213         buffer->buf_id = tex;
   214         glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex );
   215         glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
   216     } else {
   217         buffer->buf_id = tex_id;
   218         glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   219     }
   220     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
   221     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
   222     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   223     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   224     return buffer;
   225 }
   227 /**
   228  * Ensure the texture in the given render buffer is not attached to a 
   229  * framebuffer (ie, so we can safely use it as a texture during the rendering
   230  * cycle, or before deletion).
   231  */
   232 static void gl_fbo_detach_render_buffer( render_buffer_t buffer )
   233 {
   234     int i,j;
   235     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   236         if( fbo[i].width == buffer->width && fbo[i].height == buffer->height ) {
   237             for( j=0; j<gl_fbo_max_attachments; j++ ) {
   238                 if( fbo[i].tex_ids[j] == buffer->buf_id ) {
   239                     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[i].fb_id);
   240                     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINT(j), 
   241                                               GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
   242                     fbo[i].tex_ids[j] = -1;
   243                     return;
   244                 }                    
   245             }
   246             break;
   247         }
   248     }
   249 }
   251 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
   252 {
   253     int i,j;
   255     gl_fbo_detach_render_buffer( buffer );
   257     if( buffer->buf_id != buffer->tex_id ) {
   258         // If tex_id was set at buffer creation, we don't own the texture.
   259         // Otherwise, delete it now.
   260         GLuint tex = buffer->buf_id; 
   261         glDeleteTextures( 1, &tex );
   262     }
   263     buffer->buf_id = 0;
   264     buffer->tex_id = 0;
   265     free( buffer );
   266 }
   268 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   269 {
   270     glFinish();
   271     glGetError();
   272     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   273     gl_fbo_attach_texture( fb, buffer->buf_id );
   274     /* setup the gl context */
   275     glViewport( 0, 0, buffer->width, buffer->height );
   277     return TRUE;
   278 }
   280 static void gl_fbo_finish_render( render_buffer_t buffer )
   281 {
   282     glFinish();
   283     glGetError();
   284     gl_fbo_detach_render_buffer(buffer);
   285 }
   287 /**
   288  * Render the texture holding the given buffer to the front window
   289  * buffer.
   290  */
   291 static void gl_fbo_display_render_buffer( render_buffer_t buffer )
   292 {
   293     glFinish();
   294     gl_fbo_detach();
   295     gl_display_render_buffer( buffer );
   296 }
   298 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   299 {
   300     glFinish();
   301     gl_fbo_detach();
   302     gl_load_frame_buffer( frame, buffer->buf_id );
   303 }
   305 static void gl_fbo_display_blank( uint32_t colour )
   306 {
   307     glFinish();
   308     gl_fbo_detach();
   309     gl_display_blank( colour );
   310 }
   312 void gl_fbo_detach()
   313 {
   314     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   315     glDrawBuffer( GL_FRONT );
   316     glReadBuffer( GL_FRONT );
   317 }    
   319 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   320                                            int rowstride, int format )
   321 {
   322     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   323     gl_fbo_attach_texture( fb, buffer->buf_id );
   324     return gl_read_render_buffer( target, buffer, rowstride, format );
   325 }
   327 #else
   328 gboolean gl_fbo_is_supported()
   329 {
   330     return FALSE;
   331 }
   333 void gl_fbo_init( display_driver_t driver ) 
   334 {
   335 }
   337 #endif
.