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 798:eb5c0d1863cd
prev758:99ae000d4e09
next805:b355f7b3ff2e
author nkeynes
date Sun Aug 03 02:55:27 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Check the MAX_COLOR_ATTACHMENTS value rather than assuming it's at least 4, since
it may in fact be less.
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 );
    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 GLint gl_fbo_max_attachments = 0;
    60 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
    62 #define ATTACHMENT_POINT(n) (GL_COLOR_ATTACHMENT0_EXT+(n))
    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     glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &gl_fbo_max_attachments);
    81     glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids[0] );
    82     glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids[0] );
    83     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
    84         fbo[i].fb_id = fbids[i];
    85         fbo[i].depth_id = rbids[i*2];
    86         fbo[i].stencil_id = rbids[i*2+1];
    87         fbo[i].width = -1;
    88         fbo[i].height = -1;
    89         for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
    90             fbo[i].tex_ids[j] = -1;
    91         }
    92     }
    93     last_used_fbo = 0;
    95     driver->create_render_buffer = gl_fbo_create_render_buffer;
    96     driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
    97     driver->set_render_target = gl_fbo_set_render_target;
    98     driver->display_render_buffer = gl_fbo_display_render_buffer;
    99     driver->load_frame_buffer = gl_fbo_load_frame_buffer;
   100     driver->display_blank = gl_fbo_display_blank;
   101     driver->read_render_buffer = gl_fbo_read_render_buffer;
   103     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
   104     glDrawBuffer(GL_FRONT);
   105     glReadBuffer(GL_FRONT);
   106 }
   108 void gl_fbo_shutdown()
   109 {
   110     int i;
   111     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   112     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   113         glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
   114         glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
   115     }
   116 }
   118 void gl_fbo_setup_framebuffer( int bufno, int width, int height )
   119 {
   120     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
   121     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   122     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
   123     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
   124                                  GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   125     /* Stencil doesn't work on ATI, and we're not using it at the moment anyway, so...
   126     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   127     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height);
   128     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
   129 				 GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   130      */
   131     fbo[bufno].width = width;
   132     fbo[bufno].height = height;
   133 }
   135 int gl_fbo_get_framebuffer( int width, int height ) 
   136 {
   137     int bufno = -1, i;
   138     /* find a compatible framebuffer context */
   139     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   140         if( fbo[i].width == -1 && bufno == -1 ) {
   141             bufno = i;
   142         } else if( fbo[i].width == width && fbo[i].height == height ) {
   143             bufno = i;
   144             break;
   145         }
   146     }
   147     if( bufno == -1 ) {
   148         bufno = last_used_fbo + 1;
   149         if( bufno > MAX_FRAMEBUFFERS ) {
   150             bufno = 0;
   151         }
   152         last_used_fbo = bufno;
   153     }
   154     if( fbo[bufno].width == width && fbo[bufno].height == height ) {
   155         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
   156     } else {
   157         gl_fbo_setup_framebuffer( bufno, width, height );
   158     } 
   159     return bufno;
   160 }
   162 /**
   163  * Attach a texture to the framebuffer. The texture must already be initialized
   164  * to the correct dimensions etc.
   165  */
   166 static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
   167     int attach = -1, i;
   168     for( i=0; i<gl_fbo_max_attachments; i++ ) {
   169         if( fbo[fbo_no].tex_ids[i] == tex_id ) {
   170             glDrawBuffer(ATTACHMENT_POINT(i));
   171             glReadBuffer(ATTACHMENT_POINT(i)); 
   172             return ATTACHMENT_POINT(i); // already attached
   173         } else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
   174             attach = i;
   175         }
   176     }
   177     if( attach == -1 ) {
   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_POINT(attach), 
   183                               GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
   184     /* Set draw/read buffers by default */
   185     glDrawBuffer(ATTACHMENT_POINT(attach));
   186     glReadBuffer(ATTACHMENT_POINT(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_POINT(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_POINT(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
.